Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions desktop/src/features/channels/ui/BotActivityBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<Record<string, TypingPhase | null>>;
workingBotPubkeys: string[];
variant?: "toolbar" | "inline";
};
Expand All @@ -40,6 +46,7 @@ export function BotActivityComposerAction({
onOpenAgentSession,
openAgentSessionPubkey,
profiles,
typingPhaseByPubkey,
workingBotPubkeys,
variant = "toolbar",
}: BotActivityBarProps) {
Expand Down Expand Up @@ -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 (
<Popover onOpenChange={setOpen} open={open}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type ChannelComposerActivityAccessoryProps = {
typeof BotActivityComposerAction
>["openAgentSessionPubkey"];
profiles: ComponentProps<typeof BotActivityComposerAction>["profiles"];
typingPhaseByPubkey?: ComponentProps<
typeof BotActivityComposerAction
>["typingPhaseByPubkey"];
typingPubkeys: string[];
visible: boolean;
workingBotPubkeys: string[];
Expand All @@ -29,6 +32,7 @@ export function ChannelComposerActivityAccessory({
onOpenAgentSession,
openAgentSessionPubkey,
profiles,
typingPhaseByPubkey,
typingPubkeys,
visible,
workingBotPubkeys,
Expand All @@ -50,6 +54,7 @@ export function ChannelComposerActivityAccessory({
onOpenAgentSession={onOpenAgentSession}
openAgentSessionPubkey={openAgentSessionPubkey}
profiles={profiles}
typingPhaseByPubkey={typingPhaseByPubkey}
workingBotPubkeys={workingBotPubkeys}
variant="inline"
/>
Expand All @@ -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}
Expand Down
17 changes: 17 additions & 0 deletions desktop/src/features/channels/ui/ChannelPane.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, NonNullable<TypingIndicatorEntry["phase"]>> {
const phases: Record<string, NonNullable<TypingIndicatorEntry["phase"]>> = {};
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,
Expand Down
14 changes: 14 additions & 0 deletions desktop/src/features/channels/ui/ChannelPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { useWelcomeComposerBanner } from "@/features/channels/ui/useWelcomeCompo
import {
mentionsKnownAgent,
selectThreadComposerBotTypingPubkeys,
selectTypingPhaseByPubkey,
shouldPrioritizeIdleAuxiliary,
shouldUseFocusIdleDrawer,
} from "@/features/channels/ui/ChannelPane.helpers";
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -898,6 +911,7 @@ export const ChannelPane = React.memo(function ChannelPane({
onOpenAgentSession={onOpenAgentSession}
openAgentSessionPubkey={openAgentSessionPubkey}
profiles={profiles}
typingPhaseByPubkey={threadTypingPhaseByPubkey}
workingBotPubkeys={threadComposerBotTypingPubkeys}
variant="inline"
/>
Expand Down
43 changes: 43 additions & 0 deletions desktop/src/features/messages/lib/typingStatus.test.mjs
Original file line number Diff line number Diff line change
@@ -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",
});
});
65 changes: 65 additions & 0 deletions desktop/src/features/messages/lib/typingStatus.ts
Original file line number Diff line number Diff line change
@@ -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<string> = new Set<TypingPhase>([
"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…";
}
}
16 changes: 15 additions & 1 deletion desktop/src/features/messages/ui/TypingIndicatorRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, TypingPhase | null>>;
typingPubkeys: string[];
variant?: "default" | "activity";
};
Expand Down Expand Up @@ -56,6 +62,7 @@ export function TypingIndicatorRow({
className,
currentPubkey,
profiles,
typingPhaseByPubkey,
typingPubkeys,
variant = "default",
}: TypingIndicatorRowProps) {
Expand Down Expand Up @@ -134,7 +141,14 @@ export function TypingIndicatorRow({
)}
data-testid="message-typing-indicator-label"
>
<Shimmer>{formatTypingLabel(labels)}</Shimmer>
<Shimmer>
{labels.length === 1 &&
typingPhaseByPubkey?.[typingPubkeys[0]?.toLowerCase() ?? ""]
? `${labels[0]} ${typingPhaseSentence(
typingPhaseByPubkey[typingPubkeys[0]?.toLowerCase() ?? ""],
)}`
: formatTypingLabel(labels)}
</Shimmer>
</p>
</div>
)}
Expand Down
29 changes: 28 additions & 1 deletion desktop/src/features/messages/useChannelTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ 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<TypingPhase, "idle"> | null;
};

type TypingEntry = {
expiresAt: number;
firstSeenAt: number;
pubkey: string;
threadHeadId: string | null;
phase: Exclude<TypingPhase, "idle"> | null;
};
type TypingState = Record<string, TypingEntry>;

Expand Down Expand Up @@ -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];
Expand All @@ -125,6 +147,7 @@ export function useChannelTyping(
firstSeenAt: existing?.firstSeenAt ?? now,
pubkey: typingPubkey,
threadHeadId,
phase,
},
};
});
Expand Down Expand Up @@ -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],
);
}
Loading