The best way to control Claude Code, Codex and more from any computer or phone

main
ts 101 lines 4.18 KB
Raw
1 import type { OrchestrationV2RunStatus, RunId } from "@t3tools/contracts";
2
3 import { msg } from "gt-react";
4
5 import { m, type MessageResolver } from "../i18n/messages";
6 import type { SidebarThreadSummary } from "../types";
7 import {
8 runOutcomeText,
9 THREAD_STATUS_TEXT,
10 } from "@t3tools/client-runtime/state/thread-activity-announcement";
11
12 export interface BackgroundThreadSnapshot {
13 readonly key: string;
14 readonly title: string;
15 readonly request: "approval" | "input" | null;
16 /** Null for a subagent's request, which the parent's shell only knows by kind. */
17 readonly requestId: string | null;
18 readonly runId: RunId | null;
19 readonly runStatus: OrchestrationV2RunStatus | null;
20 /** The run doing work now, which can be older than a queued run cancelled while it ran. */
21 readonly activeRunId: RunId | null;
22 /** A snoozed thread stays hidden until its timer, so it stays quiet too. */
23 readonly snoozed: boolean;
24 }
25
26 /** The request and its id come from the same source, so a subagent's request never takes the parent's id. */
27 export function resolveBackgroundThreadRequest(
28 thread: Pick<
29 SidebarThreadSummary,
30 "hasPendingApprovals" | "hasPendingUserInput" | "pendingSubagentRequest"
31 > & {
32 readonly source: Pick<SidebarThreadSummary["source"], "pendingRuntimeRequest">;
33 },
34 ): Pick<BackgroundThreadSnapshot, "request" | "requestId"> {
35 const ownRequestId = thread.source.pendingRuntimeRequest?.id ?? null;
36 if (thread.hasPendingApprovals) return { request: "approval", requestId: ownRequestId };
37 if (thread.pendingSubagentRequest === "approval") return { request: "approval", requestId: null };
38 if (thread.hasPendingUserInput) return { request: "input", requestId: ownRequestId };
39 if (thread.pendingSubagentRequest === "user_input") return { request: "input", requestId: null };
40 return { request: null, requestId: null };
41 }
42
43 interface BackgroundThreadMemoryEntry {
44 readonly request: BackgroundThreadSnapshot["request"];
45 readonly requestId: string | null;
46 /** The last run whose ending is known, so each run's ending is announced once. */
47 readonly settledRunId: RunId | null;
48 }
49
50 export type BackgroundThreadMemory = ReadonlyMap<string, BackgroundThreadMemoryEntry>;
51
52 /**
53 * Only live environments supply threads. First sightings establish a silent baseline
54 * so opening the app or reconnecting does not read out every thread.
55 */
56 export function nextBackgroundThreadAnnouncement(
57 previous: BackgroundThreadMemory,
58 threads: ReadonlyArray<BackgroundThreadSnapshot>,
59 openThreadKey: string | null,
60 resolve: MessageResolver = m,
61 ): { readonly memory: BackgroundThreadMemory; readonly announcement: string | null } {
62 const memory = new Map<string, BackgroundThreadMemoryEntry>();
63 const announcements: string[] = [];
64 for (const thread of threads) {
65 const before = previous.get(thread.key);
66 const outcome = runOutcomeText(thread.runStatus, resolve);
67 const settled = outcome !== null || thread.runStatus === "rolled_back";
68 memory.set(thread.key, {
69 request: thread.request,
70 requestId: thread.requestId,
71 settledRunId: settled ? thread.runId : (before?.settledRunId ?? null),
72 });
73 if (before === undefined || thread.key === openThreadKey || thread.snoozed) continue;
74 if (
75 thread.request !== null &&
76 (thread.request !== before.request || thread.requestId !== before.requestId)
77 ) {
78 announcements.push(
79 resolve(
80 msg("{status}: {title}", {
81 status: resolve(THREAD_STATUS_TEXT[thread.request]),
82 title: thread.title,
83 }),
84 ),
85 );
86 }
87 // While another run still works, the latest run's ending (a queued message
88 // cancelled before it started) isn't the thread finishing. The shell then
89 // only reports the latest run, so the working run's own ending stays quiet.
90 const anotherRunWorking = thread.activeRunId !== null && thread.activeRunId !== thread.runId;
91 if (outcome !== null && thread.runId !== before.settledRunId && !anotherRunWorking) {
92 announcements.push(
93 resolve(msg("{status}: {title}", { status: outcome, title: thread.title })),
94 );
95 }
96 }
97 return {
98 memory,
99 announcement: announcements.length === 0 ? null : announcements.join(". "),
100 };
101 }