Open History to see the last commit
1import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
2import { effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
3import type { AnyRouter } from "@tanstack/react-router";
4import type { AtomRegistry } from "effect/reactivity";
5
6import { useComposerDraftStore } from "../composerDraftStore";
7import { createLiveRegion } from "../hooks/useLiveRegion";
8import { liveShellEnvironmentIdsAtom } from "../state/shell";
9import { environmentThreadShells } from "../state/threads";
10import { resolveActiveThreadRouteRef, resolveThreadRouteTarget } from "../threadRoutes";
11import {
12 type BackgroundThreadMemory,
13 type BackgroundThreadSnapshot,
14 nextBackgroundThreadAnnouncement,
15 resolveBackgroundThreadRequest,
16} from "./backgroundThreadAnnouncement";
17
18const backgroundThreadLiveRegion = createLiveRegion();
19const attachBackgroundThreadRegion = backgroundThreadLiveRegion.attach;
20
21function readOpenThreadKey(router: AnyRouter): string | null {
22 const target = resolveThreadRouteTarget(router.state.matches.at(-1)?.params ?? {});
23 const draft =
24 target?.kind === "draft"
25 ? useComposerDraftStore.getState().getDraftSession(target.draftId)
26 : null;
27 const ref = resolveActiveThreadRouteRef(target, draft);
28 return ref === null ? null : scopedThreadKey(ref);
29}
30
31/** Speaks status changes for threads other than the open one, for the life of the app. */
32export function startBackgroundThreadAnnouncements(
33 registry: AtomRegistry.AtomRegistry,
34 router: AnyRouter,
35): () => void {
36 let memory: BackgroundThreadMemory = new Map();
37 let openThreadKey = readOpenThreadKey(router);
38
39 const update = () => {
40 const threads = registry.get(environmentThreadShells.quietThreadShellsAtom);
41 const liveEnvironmentIds = registry.get(liveShellEnvironmentIdsAtom);
42 const now = new Date().toISOString();
43 const snapshots: BackgroundThreadSnapshot[] = [];
44 for (const thread of threads) {
45 // Subagents have no row of their own; their requests surface on the parent.
46 if (thread.archivedAt !== null || thread.lineage.relationshipToParent === "subagent")
47 continue;
48 if (!liveEnvironmentIds.has(thread.environmentId)) continue;
49 snapshots.push({
50 key: scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
51 title: thread.title,
52 ...resolveBackgroundThreadRequest(thread),
53 runId: thread.latestRun?.runId ?? null,
54 runStatus: thread.latestRun?.status ?? null,
55 activeRunId: thread.runtime?.activeRunId ?? null,
56 snoozed: effectiveSnoozed(thread, { now }),
57 });
58 }
59 const next = nextBackgroundThreadAnnouncement(memory, snapshots, openThreadKey);
60 memory = next.memory;
61 if (next.announcement !== null) backgroundThreadLiveRegion.announce(next.announcement);
62 };
63
64 // The draft store changes on every keystroke; only a new open thread matters here.
65 const updateOpenThread = () => {
66 const next = readOpenThreadKey(router);
67 if (next === openThreadKey) return;
68 openThreadKey = next;
69 update();
70 };
71
72 const unsubscribes = [
73 registry.subscribe(environmentThreadShells.quietThreadShellsAtom, update),
74 registry.subscribe(liveShellEnvironmentIdsAtom, update),
75 router.subscribe("onResolved", updateOpenThread),
76 useComposerDraftStore.subscribe(updateOpenThread),
77 ];
78 update();
79 return () => {
80 for (const unsubscribe of unsubscribes) unsubscribe();
81 };
82}
83
84/**
85 * The region the background announcements are spoken through. It lives outside
86 * the sidebar because a collapsed sidebar is inert, which silences its rows.
87 */
88export function BackgroundThreadAnnouncer() {
89 // No role="status": Orca 50 maps it to a status bar in Chromium and drops its updates.
90 return <div ref={attachBackgroundThreadRegion} aria-live="polite" className="sr-only" />;
91}