Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

backgroundThreadAnnouncement.ts

88 lines4.0 KB

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;
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 };
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;
43​
44export type BackgroundThreadMemory = ReadonlyMap<string, BackgroundThreadMemoryEntry>;
45​
46/**
47 * Decides what a screen reader hears when a thread other than the open one
48 * asks for something or ends a run. `threads` holds only threads whose
49 * environment is live, so a thread is silent the first time it's seen:
50 * opening the app or reconnecting takes a baseline instead of reading out
51 * every thread.
52 */
53export function nextBackgroundThreadAnnouncement(
54 previous: BackgroundThreadMemory,
55 threads: ReadonlyArray<BackgroundThreadSnapshot>,
56 openThreadKey: string | null,
57): { readonly memory: BackgroundThreadMemory; readonly announcement: string | null } {
58 const memory = new Map<string, BackgroundThreadMemoryEntry>();
59 const announcements: string[] = [];
60 for (const thread of threads) {
61 const before = previous.get(thread.key);
62 const outcome = runOutcomeText(thread.runStatus);
63 const settled = outcome !== null || thread.runStatus === "rolled_back";
64 memory.set(thread.key, {
65 request: thread.request,
66 requestId: thread.requestId,
67 settledRunId: settled ? thread.runId : (before?.settledRunId ?? null),
68 });
69 if (before === undefined || thread.key === openThreadKey || thread.snoozed) continue;
70 if (
71 thread.request !== null &&
72 (thread.request !== before.request || thread.requestId !== before.requestId)
73 ) {
74 announcements.push(`${THREAD_STATUS_TEXT[thread.request]}: ${thread.title}`);
75 }
76 // While another run still works, the latest run's ending (a queued message
77 // cancelled before it started) isn't the thread finishing. The shell then
78 // only reports the latest run, so the working run's own ending stays quiet.
79 const anotherRunWorking = thread.activeRunId !== null && thread.activeRunId !== thread.runId;
80 if (outcome !== null && thread.runId !== before.settledRunId && !anotherRunWorking) {
81 announcements.push(`${outcome}: ${thread.title}`);
82 }
83 }
84 return {
85 memory,
86 announcement: announcements.length === 0 ? null : announcements.join(". "),
87 };