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

BackgroundThreadAnnouncer.tsx

92 lines3.9 KB

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