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

threadActivityAnnouncement.ts

175 lines6.5 KB

1import type { OrchestrationV2Run, OrchestrationV2RunStatus, RunId } from "@t3tools/contracts";
2import { latestExecutedRun } from "@t3tools/shared/orchestrationV2ThreadError";
3​
4export type ThreadActivityStatus = "working" | "approval" | "input" | "idle";
5​
6/** Background threads say these too, so a status sounds the same wherever it's heard. */
7export const THREAD_STATUS_TEXT = {
8 approval: "Needs approval",
9 input: "Needs your input",
10 done: "Done",
11 failed: "Failed",
12 stopped: "Stopped",
13} as const;
14​
15export interface ThreadActivitySnapshot {
16 readonly status: ThreadActivityStatus;
17 /** The pending approval or question, when there is one. */
18 readonly requestId: string | null;
19 readonly runId: RunId | null;
20 readonly runStatus: OrchestrationV2RunStatus | null;
22​
23export interface ThreadAnnouncementMemory {
24 readonly snapshot: ThreadActivitySnapshot;
25 /**
26 * The turn under way: the run it belongs to, "unknown" until that run shows
27 * up, or null between turns. Its ending is announced once, then it clears.
28 */
29 readonly turnRunId: RunId | "unknown" | null;
30 /** The run whose ending is already known, so a stale run is never announced as this turn's. */
31 readonly settledRunId: RunId | null;
32 /** The run shown while idle belongs to the last turn, even if it is still settling. */
33 readonly idleRunId: RunId | null;
35​
36const ACTIVE_RUN_STATUSES: ReadonlySet<OrchestrationV2RunStatus | null> = new Set([
37 "preparing",
38 "starting",
39 "running",
40 "waiting",
41]);
42​
43const STARTING_RUN_STATUSES: ReadonlySet<OrchestrationV2RunStatus | null> = new Set([
44 "queued",
45 "preparing",
46 "starting",
47]);
48​
49export function runOutcomeText(status: OrchestrationV2RunStatus | null): string | null {
50 switch (status) {
51 case "completed":
52 return THREAD_STATUS_TEXT.done;
53 case "failed":
54 return THREAD_STATUS_TEXT.failed;
55 case "interrupted":
56 case "cancelled":
57 return THREAD_STATUS_TEXT.stopped;
58 default:
59 return null;
60 }
62​
63interface RunState {
64 readonly runId: RunId;
65 readonly status: OrchestrationV2RunStatus;
67​
68/**
69 * The thread's detail hasn't caught up with the live thread shell: it isn't
70 * loaded yet, or a cached copy still shows an older run or state. Reading such
71 * a frame as the baseline replays what already happened, so it's skipped.
72 */
73export function isDetailBehindShell(detailRun: RunState | null, shellRun: RunState | null) {
74 return (
75 shellRun !== null &&
76 (detailRun?.runId !== shellRun.runId || detailRun.status !== shellRun.status)
77 );
79​
80/**
81 * A queued message cancelled before it started keeps its place in line, so it
82 * shows up as the thread's latest run. It never ran, so the turn ended however
83 * the last run that did ran.
84 */
85export function announcedRun(
86 activityRun: (RunState & { readonly startedAt: string | null }) | null,
87 runs: ReadonlyArray<OrchestrationV2Run>,
88): RunState | null {
89 if (activityRun?.status !== "cancelled" || activityRun.startedAt !== null) return activityRun;
90 const run = latestExecutedRun(runs);
91 return run === null ? null : { runId: run.id, status: run.status };
93​
94/**
95 * Decides what a screen reader hears when the open thread's turn changes state.
96 * The first snapshot of a thread is a silent baseline, so opening or switching
97 * threads never replays what already happened.
98 */
99export function nextThreadAnnouncement(
100 previous: ThreadAnnouncementMemory | null,
101 next: ThreadActivitySnapshot,
102): { readonly memory: ThreadAnnouncementMemory; readonly announcement: string | null } {
103 const idle = next.status === "idle";
104 // A run still starting up while the thread looks idle (a queued message,
105 // a resume) is the next turn's, not a leftover from the last one.
106 const idleRunId =
107 idle && !STARTING_RUN_STATUSES.has(next.runStatus) ? next.runId : (previous?.idleRunId ?? null);
108 // A run doing new work; the run left over from the last turn doesn't count.
109 const newRunId =
110 !idle && ACTIVE_RUN_STATUSES.has(next.runStatus) && next.runId !== idleRunId
111 ? next.runId
112 : null;
113​
114 if (previous === null) {
115 return {
116 memory: {
117 snapshot: next,
118 turnRunId: idle ? null : (newRunId ?? "unknown"),
119 settledRunId: runOutcomeText(next.runStatus) === null ? null : next.runId,
120 idleRunId,
121 },
122 announcement: null,
123 };
124 }
125 const memory = { ...previous, snapshot: next, idleRunId };
126 const { turnRunId } = previous;
127​
128 if (!idle) {
129 // Sending can look idle for a moment before the server reports the run,
130 // so a turn is tied to its run as soon as one appears, and only another
131 // run taking over (a queued message starting) is a new turn.
132 const turnStarted =
133 turnRunId === null ||
134 (newRunId !== null && turnRunId !== "unknown" && newRunId !== turnRunId);
135 const nextTurnRunId =
136 turnStarted || turnRunId === "unknown" ? (newRunId ?? "unknown") : turnRunId;
137 const announcement =
138 next.status === "approval"
139 ? previous.snapshot.status === "approval" && previous.snapshot.requestId === next.requestId
140 ? null
141 : THREAD_STATUS_TEXT.approval
142 : next.status === "input"
143 ? previous.snapshot.status === "input" && previous.snapshot.requestId === next.requestId
144 ? null
145 : THREAD_STATUS_TEXT.input
146 : turnStarted
147 ? "Working"
148 : null;
149 return { memory: { ...memory, turnRunId: nextTurnRunId }, announcement };
150 }
151​
152 // Work can stop a moment before the run settles; the ending waits for the run.
153 const outcome = runOutcomeText(next.runStatus);
154 if (turnRunId === null) {
155 // A run that settled unseen is history too, or the next failed send would claim it.
156 const settledRunId = outcome === null ? previous.settledRunId : next.runId;
157 return { memory: { ...memory, settledRunId }, announcement: null };
158 }
159 if (next.runStatus === "rolled_back") {
160 return { memory: { ...memory, turnRunId: null, settledRunId: next.runId }, announcement: null };
161 }
162 if (outcome === null) return { memory, announcement: null };
163 // Work stopped without a new run, e.g. a send that failed; the retry is a new turn.
164 if (next.runId === previous.settledRunId) {
165 return { memory: { ...memory, turnRunId: null }, announcement: null };
166 }
167 // The last turn's run settling while this turn waits for its own run is not this turn's ending.
168 if (turnRunId === "unknown" && next.runId === previous.idleRunId) {
169 return { memory: { ...memory, settledRunId: next.runId }, announcement: null };
170 }
171 return {
172 memory: { ...memory, turnRunId: null, settledRunId: next.runId },
173 announcement: outcome,
174 };