Open History to see the last commit
1import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox";
2
3export type ThreadNotificationKind = "input" | "completion";
4
5export interface ThreadNotificationObservation {
6 /** Run and status key while the thread needs attention, otherwise null. */
7 readonly attention: string | null;
8 /** The thread is idle and its latest run completed. */
9 readonly settled: boolean;
10 /** When the latest run completed, if it did. */
11 readonly completedAt: number | null;
12}
13
14/**
15 * Attention notifies at once. Completion waits out the Working shelf's exit grace,
16 * because an orchestrator run can end moments before its continuation starts. Any
17 * observation that is not settled during the wait cancels it.
18 */
19export function createThreadNotificationGate<Key>(
20 notify: (key: Key, kind: ThreadNotificationKind) => void,
21) {
22 const pending = new Map<Key, { at: number; timer: ReturnType<typeof setTimeout> }>();
23 // The newest completion already notified, or deliberately skipped.
24 const handled = new Map<Key, number>();
25 // The newest completion seen settled, kept while the thread works or waits.
26 const lastSettled = new Map<Key, number>();
27 const cancel = (key: Key) => {
28 clearTimeout(pending.get(key)?.timer);
29 pending.delete(key);
30 };
31 const skip = (key: Key, at: number | null) => {
32 if (at !== null && at > (handled.get(key) ?? -Infinity)) handled.set(key, at);
33 };
34 return {
35 observe(
36 key: Key,
37 prior: ThreadNotificationObservation | undefined,
38 next: ThreadNotificationObservation,
39 options: { readonly archived: boolean; readonly quiet: boolean },
40 ): void {
41 if (next.settled && next.completedAt !== null) lastSettled.set(key, next.completedAt);
42 if (!next.settled || options.archived || options.quiet) cancel(key);
43 // The first sighting, archived threads, and stopped or silent runs never notify completion.
44 if (!prior || options.archived || options.quiet) skip(key, next.completedAt);
45 if (!prior || options.archived) return;
46 if (next.attention && next.attention !== prior.attention) {
47 // Attention replaces the completion before it, so that run must not notify later.
48 skip(key, lastSettled.get(key) ?? null);
49 cancel(key);
50 notify(key, "input");
51 return;
52 }
53 const at = next.completedAt;
54 if (!next.settled || at === null || at <= (handled.get(key) ?? -Infinity)) return;
55 if (pending.get(key)?.at === at) return;
56 cancel(key);
57 pending.set(key, {
58 at,
59 timer: setTimeout(() => {
60 pending.delete(key);
61 handled.set(key, at);
62 notify(key, "completion");
63 }, WORKING_EXIT_GRACE_MS),
64 });
65 },
66 /** Forgets threads that left the snapshot. */
67 retain(keys: ReadonlySet<Key>): void {
68 for (const key of pending.keys()) if (!keys.has(key)) cancel(key);
69 for (const key of handled.keys()) if (!keys.has(key)) handled.delete(key);
70 for (const key of lastSettled.keys()) if (!keys.has(key)) lastSettled.delete(key);
71 },
72 cancelPending(): void {
73 for (const key of pending.keys()) cancel(key);
74 },
75 clear(): void {
76 for (const key of pending.keys()) cancel(key);
77 handled.clear();
78 lastSettled.clear();
79 },
80 };
81}