diff --git a/desktop/src/features/channels/ui/BotActivityBar.tsx b/desktop/src/features/channels/ui/BotActivityBar.tsx index dacdc7e89ed..46650ca2d38 100644 --- a/desktop/src/features/channels/ui/BotActivityBar.tsx +++ b/desktop/src/features/channels/ui/BotActivityBar.tsx @@ -7,6 +7,10 @@ import { isMeaningfulItem, isSpineItem, } from "@/features/agents/ui/agentSessionTranscriptPresentation"; +import { + type TypingPhase, + typingPhaseSentence, +} from "@/features/messages/lib/typingStatus"; import type { UserProfileLookup } from "@/features/profile/lib/identity"; import type { ManagedAgent } from "@/shared/api/types"; import { cn } from "@/shared/lib/cn"; @@ -27,6 +31,8 @@ type BotActivityBarProps = { onOpenAgentSession: (pubkey: string, channelId?: string | null) => void; openAgentSessionPubkey: string | null; profiles?: UserProfileLookup; + /** Server-reported typing status per lowercase pubkey, when the agent sends one. */ + typingPhaseByPubkey?: Readonly>; workingBotPubkeys: string[]; variant?: "toolbar" | "inline"; }; @@ -40,6 +46,7 @@ export function BotActivityComposerAction({ onOpenAgentSession, openAgentSessionPubkey, profiles, + typingPhaseByPubkey, workingBotPubkeys, variant = "toolbar", }: BotActivityBarProps) { @@ -147,18 +154,28 @@ export function BotActivityComposerAction({ const agentAvatarUrl = (agent: BotActivityAgent) => profiles?.[agent.pubkey.toLowerCase()]?.avatarUrl ?? null; const selectedPubkey = openAgentSessionPubkey?.toLowerCase() ?? null; + // A server-reported phase is what every device shows for this agent, so it + // wins over this desktop's own transcript headline. + const singlePhase = singleWorkingAgent + ? (typingPhaseByPubkey?.[singleWorkingAgent.pubkey.toLowerCase()] ?? null) + : null; + const phaseLabel = singlePhase + ? `${singleWorkingAgent?.name ?? "Agent"} ${typingPhaseSentence(singlePhase)}` + : null; const triggerLabel = - workingAgents.length === 1 + phaseLabel ?? + (workingAgents.length === 1 ? `${workingAgents[0]?.name ?? "Agent"} is working` - : `${workingAgents.length} agents working`; + : `${workingAgents.length} agents working`); const isInline = variant === "inline"; const visibleStatusLabel = - workingAgents.length === 1 + phaseLabel ?? + (workingAgents.length === 1 ? `${workingAgents[0]?.name ?? "Agent"}: ${ activityHeadlines[headlineIndex % activityHeadlines.length] ?? "Working" }` - : `${workingAgents[0]?.name ?? "Agent"} +${workingAgents.length - 1}`; + : `${workingAgents[0]?.name ?? "Agent"} +${workingAgents.length - 1}`); return ( diff --git a/desktop/src/features/channels/ui/ChannelComposerActivityAccessory.tsx b/desktop/src/features/channels/ui/ChannelComposerActivityAccessory.tsx index f99888f0112..4d4bf65ff53 100644 --- a/desktop/src/features/channels/ui/ChannelComposerActivityAccessory.tsx +++ b/desktop/src/features/channels/ui/ChannelComposerActivityAccessory.tsx @@ -17,6 +17,9 @@ type ChannelComposerActivityAccessoryProps = { typeof BotActivityComposerAction >["openAgentSessionPubkey"]; profiles: ComponentProps["profiles"]; + typingPhaseByPubkey?: ComponentProps< + typeof BotActivityComposerAction + >["typingPhaseByPubkey"]; typingPubkeys: string[]; visible: boolean; workingBotPubkeys: string[]; @@ -29,6 +32,7 @@ export function ChannelComposerActivityAccessory({ onOpenAgentSession, openAgentSessionPubkey, profiles, + typingPhaseByPubkey, typingPubkeys, visible, workingBotPubkeys, @@ -50,6 +54,7 @@ export function ChannelComposerActivityAccessory({ onOpenAgentSession={onOpenAgentSession} openAgentSessionPubkey={openAgentSessionPubkey} profiles={profiles} + typingPhaseByPubkey={typingPhaseByPubkey} workingBotPubkeys={workingBotPubkeys} variant="inline" /> @@ -61,6 +66,7 @@ export function ChannelComposerActivityAccessory({ className="min-w-0 flex-1 py-0 pl-[calc(0.75rem+1px)] pr-0 sm:pl-[calc(1rem+1px)]" currentPubkey={currentPubkey} profiles={profiles} + typingPhaseByPubkey={typingPhaseByPubkey} typingPubkeys={typingPubkeys} /> ) : null} diff --git a/desktop/src/features/channels/ui/ChannelPane.helpers.ts b/desktop/src/features/channels/ui/ChannelPane.helpers.ts index a93eed6837a..0366dfaf509 100644 --- a/desktop/src/features/channels/ui/ChannelPane.helpers.ts +++ b/desktop/src/features/channels/ui/ChannelPane.helpers.ts @@ -108,6 +108,23 @@ export function mentionsKnownAgent( ); } +/** + * Latest typing status per lowercase pubkey in one scope: the channel itself + * (`threadHeadId` null) or one thread. Entries without a phase are omitted. + */ +export function selectTypingPhaseByPubkey( + entries: TypingIndicatorEntry[], + threadHeadId: string | null, +): Record> { + const phases: Record> = {}; + for (const entry of entries) { + if (entry.threadHeadId === threadHeadId && entry.phase) { + phases[entry.pubkey.toLowerCase()] = entry.phase; + } + } + return phases; +} + export function selectThreadComposerBotTypingPubkeys( entries: TypingIndicatorEntry[], threadHeadId: string | null, diff --git a/desktop/src/features/channels/ui/ChannelPane.tsx b/desktop/src/features/channels/ui/ChannelPane.tsx index 9b384e1a0f8..b012812f03f 100644 --- a/desktop/src/features/channels/ui/ChannelPane.tsx +++ b/desktop/src/features/channels/ui/ChannelPane.tsx @@ -53,6 +53,7 @@ import { useWelcomeComposerBanner } from "@/features/channels/ui/useWelcomeCompo import { mentionsKnownAgent, selectThreadComposerBotTypingPubkeys, + selectTypingPhaseByPubkey, shouldPrioritizeIdleAuxiliary, shouldUseFocusIdleDrawer, } from "@/features/channels/ui/ChannelPane.helpers"; @@ -347,6 +348,17 @@ export const ChannelPane = React.memo(function ChannelPane({ ); const hasThreadComposerBotActivity = threadComposerBotTypingPubkeys.length > 0; + const channelTypingPhaseByPubkey = React.useMemo( + () => selectTypingPhaseByPubkey(botTypingEntries, null), + [botTypingEntries], + ); + const threadTypingPhaseByPubkey = React.useMemo( + () => + openThreadHeadId + ? selectTypingPhaseByPubkey(botTypingEntries, openThreadHeadId) + : {}, + [botTypingEntries, openThreadHeadId], + ); const directMessageIntro = React.useMemo( () => buildDirectMessageIntro({ @@ -799,6 +811,7 @@ export const ChannelPane = React.memo(function ChannelPane({ onOpenAgentSession={onOpenAgentSession} openAgentSessionPubkey={openAgentSessionPubkey} profiles={profiles} + typingPhaseByPubkey={channelTypingPhaseByPubkey} typingPubkeys={typingPubkeys} visible={hasComposerBottomActivity} workingBotPubkeys={composerWorkingBotPubkeys} @@ -898,6 +911,7 @@ export const ChannelPane = React.memo(function ChannelPane({ onOpenAgentSession={onOpenAgentSession} openAgentSessionPubkey={openAgentSessionPubkey} profiles={profiles} + typingPhaseByPubkey={threadTypingPhaseByPubkey} workingBotPubkeys={threadComposerBotTypingPubkeys} variant="inline" /> diff --git a/desktop/src/features/messages/lib/typingStatus.test.mjs b/desktop/src/features/messages/lib/typingStatus.test.mjs new file mode 100644 index 00000000000..b691340f246 --- /dev/null +++ b/desktop/src/features/messages/lib/typingStatus.test.mjs @@ -0,0 +1,43 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { parseTypingPhase, typingPhaseSentence } from "./typingStatus.ts"; +import { selectTypingPhaseByPubkey } from "../../channels/ui/ChannelPane.helpers.ts"; + +test("typing content carries a versioned phase or nothing", () => { + assert.equal(parseTypingPhase(""), null); + assert.equal(parseTypingPhase(undefined), null); + assert.equal(parseTypingPhase('{"v":1,"phase":"working"}'), "working"); + assert.equal(parseTypingPhase('{"v":1,"phase":"idle"}'), "idle"); + assert.equal(parseTypingPhase('{"v":2,"phase":"working"}'), null); + assert.equal(parseTypingPhase('{"v":1,"phase":"dancing"}'), null); + assert.equal(parseTypingPhase("not json"), null); + assert.equal(parseTypingPhase("[1]"), null); + assert.equal( + parseTypingPhase(`{"v":1,"phase":"working","pad":"${"x".repeat(600)}"}`), + null, + ); +}); + +test("each phase reads as one status line; unknown is typing", () => { + assert.equal(typingPhaseSentence("starting"), "is starting up…"); + assert.equal(typingPhaseSentence("warming"), "is warming up a session…"); + assert.equal(typingPhaseSentence("reconnecting"), "is reconnecting…"); + assert.equal(typingPhaseSentence("working"), "is working…"); + assert.equal(typingPhaseSentence("typing"), "is typing…"); + assert.equal(typingPhaseSentence(null), "is typing…"); +}); + +test("phases are selected per scope and keyed by lowercase pubkey", () => { + const entries = [ + { pubkey: "AA", threadHeadId: null, phase: "starting" }, + { pubkey: "bb", threadHeadId: "head", phase: "working" }, + { pubkey: "cc", threadHeadId: null, phase: null }, + ]; + assert.deepEqual(selectTypingPhaseByPubkey(entries, null), { + aa: "starting", + }); + assert.deepEqual(selectTypingPhaseByPubkey(entries, "head"), { + bb: "working", + }); +}); diff --git a/desktop/src/features/messages/lib/typingStatus.ts b/desktop/src/features/messages/lib/typingStatus.ts new file mode 100644 index 00000000000..16afb1d2585 --- /dev/null +++ b/desktop/src/features/messages/lib/typingStatus.ts @@ -0,0 +1,65 @@ +/** + * Optional status carried in a kind:20002 typing indicator's content: + * `{"v":1,"phase":"working"}`. Ephemeral only, never chat or timeline. + * + * An empty or unrecognised payload is an ordinary "is typing…" signal, so + * senders and clients can adopt phases independently. `idle` ends the + * sender's typing in that scope at once instead of waiting for the TTL. + */ +export type TypingPhase = + | "starting" + | "warming" + | "reconnecting" + | "working" + | "typing" + | "idle"; + +const KNOWN_PHASES: ReadonlySet = new Set([ + "starting", + "warming", + "reconnecting", + "working", + "typing", + "idle", +]); + +/** Typing content is bounded on the relay; refuse anything larger here too. */ +const MAX_TYPING_CONTENT_LENGTH = 512; + +export function parseTypingPhase( + content: string | undefined, +): TypingPhase | null { + if (!content || content.length > MAX_TYPING_CONTENT_LENGTH) { + return null; + } + try { + const value: unknown = JSON.parse(content); + if (!value || typeof value !== "object" || Array.isArray(value)) { + return null; + } + const { v, phase } = value as { v?: unknown; phase?: unknown }; + return v === 1 && typeof phase === "string" && KNOWN_PHASES.has(phase) + ? (phase as TypingPhase) + : null; + } catch { + return null; + } +} + +/** The status-line verb phrase after the sender's name, e.g. "is starting up…". */ +export function typingPhaseSentence( + phase: TypingPhase | null | undefined, +): string { + switch (phase) { + case "starting": + return "is starting up…"; + case "warming": + return "is warming up a session…"; + case "reconnecting": + return "is reconnecting…"; + case "working": + return "is working…"; + default: + return "is typing…"; + } +} diff --git a/desktop/src/features/messages/ui/TypingIndicatorRow.tsx b/desktop/src/features/messages/ui/TypingIndicatorRow.tsx index 1ca6da6196f..0a50349b0a6 100644 --- a/desktop/src/features/messages/ui/TypingIndicatorRow.tsx +++ b/desktop/src/features/messages/ui/TypingIndicatorRow.tsx @@ -9,12 +9,18 @@ import type { Channel } from "@/shared/api/types"; import { cn } from "@/shared/lib/cn"; import { Shimmer } from "@/shared/ui/Shimmer"; import { truncateNpub } from "@/shared/lib/pubkey"; +import { + type TypingPhase, + typingPhaseSentence, +} from "@/features/messages/lib/typingStatus"; type TypingIndicatorRowProps = { channel: Channel | null; className?: string; currentPubkey?: string; profiles?: UserProfileLookup; + /** Typing status per lowercase pubkey; shown when exactly one person types. */ + typingPhaseByPubkey?: Readonly>; typingPubkeys: string[]; variant?: "default" | "activity"; }; @@ -56,6 +62,7 @@ export function TypingIndicatorRow({ className, currentPubkey, profiles, + typingPhaseByPubkey, typingPubkeys, variant = "default", }: TypingIndicatorRowProps) { @@ -134,7 +141,14 @@ export function TypingIndicatorRow({ )} data-testid="message-typing-indicator-label" > - {formatTypingLabel(labels)} + + {labels.length === 1 && + typingPhaseByPubkey?.[typingPubkeys[0]?.toLowerCase() ?? ""] + ? `${labels[0]} ${typingPhaseSentence( + typingPhaseByPubkey[typingPubkeys[0]?.toLowerCase() ?? ""], + )}` + : formatTypingLabel(labels)} +

)} diff --git a/desktop/src/features/messages/useChannelTyping.ts b/desktop/src/features/messages/useChannelTyping.ts index 0041e4edd56..ea32b41eff1 100644 --- a/desktop/src/features/messages/useChannelTyping.ts +++ b/desktop/src/features/messages/useChannelTyping.ts @@ -12,10 +12,16 @@ import { KIND_TYPING_INDICATOR, } from "@/shared/constants/kinds"; import { resolveEventAuthorPubkey } from "@/shared/lib/authors"; +import { + parseTypingPhase, + type TypingPhase, +} from "@/features/messages/lib/typingStatus"; export type TypingIndicatorEntry = { pubkey: string; threadHeadId: string | null; + /** Status carried by the latest pulse; null is plain "is typing…". */ + phase: Exclude | null; }; type TypingEntry = { @@ -23,6 +29,7 @@ type TypingEntry = { firstSeenAt: number; pubkey: string; threadHeadId: string | null; + phase: Exclude | null; }; type TypingState = Record; @@ -115,6 +122,21 @@ export function useChannelTyping( return; } + const phase = parseTypingPhase(event.content); + if (phase === "idle") { + // The sender stopped: end its typing in this scope now, not at the TTL. + setTypingByPubkey((current) => { + const next = pruneTypingState(current, now); + if (!(typingKey in next)) { + return next; + } + const updated = { ...next }; + delete updated[typingKey]; + return updated; + }); + return; + } + setTypingByPubkey((current) => { const pruned = pruneTypingState(current, now); const existing = pruned[typingKey]; @@ -125,6 +147,7 @@ export function useChannelTyping( firstSeenAt: existing?.firstSeenAt ?? now, pubkey: typingPubkey, threadHeadId, + phase, }, }; }); @@ -234,7 +257,11 @@ export function useChannelTyping( () => Object.values(typingByPubkey) .sort((left, right) => left.firstSeenAt - right.firstSeenAt) - .map(({ pubkey, threadHeadId }) => ({ pubkey, threadHeadId })), + .map(({ pubkey, threadHeadId, phase }) => ({ + pubkey, + threadHeadId, + phase, + })), [typingByPubkey], ); } diff --git a/docs/kiingo-fork-inventory.json b/docs/kiingo-fork-inventory.json index be4d945b4c3..f456be9c8fa 100644 --- a/docs/kiingo-fork-inventory.json +++ b/docs/kiingo-fork-inventory.json @@ -1530,6 +1530,23 @@ "mobile/lib/shared/community/community.dart", "mobile/test/shared/community/push_default_on_test.dart" ] + }, + { + "classification": "retain-generic", + "owner": "Buzz messaging maintainers", + "paths": [ + "desktop/src/features/channels/ui/BotActivityBar.tsx", + "desktop/src/features/channels/ui/ChannelComposerActivityAccessory.tsx", + "desktop/src/features/channels/ui/ChannelPane.helpers.ts", + "desktop/src/features/channels/ui/ChannelPane.tsx", + "desktop/src/features/messages/lib/typingStatus.test.mjs", + "desktop/src/features/messages/lib/typingStatus.ts", + "desktop/src/features/messages/ui/TypingIndicatorRow.tsx", + "desktop/src/features/messages/useChannelTyping.ts" + ], + "purpose": "Typing status phases on desktop: reads the optional {\"v\":1,\"phase\":...} content of kind:20002 typing events and shows the sender's status line in the typing slot for DMs, channels and threads (starting up, warming up a session, reconnecting, working, typing). Unknown or malformed payloads stay plain typing, \"idle\" ends that sender's typing at once, and a reported phase wins over the local transcript headline in the bot activity row. Kiingo M-4017.", + "removalCondition": "Drop when upstream reads typing status phases on desktop, or Kiingo agents stop sending phases.", + "upstreamStatus": "Generic typing-status extension (backward-compatible content field); not yet submitted to block/buzz." } ], "budgetAmendments": [