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