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

backgroundThreadAnnouncement.ts

126 lines5.1 KB

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