Open History to see the last commit
1import {
2 OrchestrationV2AppThread,
3 OrchestrationV2TurnItem,
4 type OrchestrationV2ThreadProjection,
5 type ScopedThreadRef,
6} from "@t3tools/contracts";
7import * as Option from "effect/Option";
8import * as Schema from "effect/Schema";
9import { AsyncResult, Atom } from "effect/reactivity";
10
11import {
12 deriveThreadQueueWorkflowState,
13 getUserQueuedThreadRuns,
14 type ThreadQueueWorkflowState,
15} from "./threadWorkflows.ts";
16import { deriveThreadTurnSubagents, type ThreadTurnSubagents } from "./threadSubagents.ts";
17import { presentRunStop, type RunStopPresentation } from "./threadExecution.ts";
18import {
19 deriveThreadCheckpointSummaries,
20 type ThreadCheckpointSummary,
21} from "./threadCheckpoints.ts";
22import type {
23 EnvironmentThread,
24 EnvironmentThreadStructure,
25 ThreadStructure,
26 ThreadStructureThread,
27 ThreadStructureTurnItem,
28 ThreadStructureVisibleTurnItem,
29} from "./models.ts";
30import { EMPTY_ENVIRONMENT_THREAD_STATE, type EnvironmentThreadState } from "./threadState.ts";
31import {
32 createQuestionHistoryProjector,
33 derivePendingThreadRequests,
34 type PendingThreadRequests,
35} from "./threadRequests.ts";
36import { arrayElementsEqual, parseThreadKey, threadKey } from "./entities.ts";
37import { getProjectionArrayReplacementIndex } from "./orchestrationV2Projection.ts";
38
39const EMPTY_VISIBLE_TURN_ITEMS: OrchestrationV2ThreadProjection["visibleTurnItems"] = Object.freeze(
40 [],
41);
42const EMPTY_THREAD_STRUCTURE_TURN_ITEMS: ReadonlyArray<ThreadStructureVisibleTurnItem> =
43 Object.freeze([]);
44const EMPTY_CHECKPOINT_SUMMARIES: ReadonlyArray<ThreadCheckpointSummary> = Object.freeze([]);
45
46/**
47 * Changes when the agent may have changed the workspace, so file views and host images refresh.
48 * File tools are explicit; commands, MCP or dynamic tools and subagents are included because
49 * they can mutate the workspace without reporting the paths they touched.
50 */
51export function deriveWorkspaceMutationId(
52 visibleTurnItems: ReadonlyArray<ThreadStructureVisibleTurnItem>,
53 latestCheckpointCompletedAt: string | null,
54) {
55 let itemId: string | null = null;
56 for (let index = visibleTurnItems.length - 1; index >= 0; index -= 1) {
57 const item = visibleTurnItems[index]?.item;
58 if (!item) continue;
59 if (
60 item.type !== "command_execution" &&
61 item.type !== "file_change" &&
62 item.type !== "dynamic_tool" &&
63 item.type !== "subagent"
64 ) {
65 continue;
66 }
67 if (item.status === "pending" || item.status === "running" || item.status === "waiting") {
68 continue;
69 }
70 itemId = item.id;
71 break;
72 }
73 return itemId === null && latestCheckpointCompletedAt === null
74 ? null
75 : JSON.stringify([itemId, latestCheckpointCompletedAt]);
76}
77
78type TurnItem = OrchestrationV2ThreadProjection["turnItems"][number];
79type VisibleTurnItem = OrchestrationV2ThreadProjection["visibleTurnItems"][number];
80type AppThread = OrchestrationV2ThreadProjection["thread"];
81
82// Items are stripped once each: unchanged items keep their identity across
83// structure changes, which the incremental timeline relies on.
84const structureTurnItems = new WeakMap<TurnItem, ThreadStructureTurnItem>();
85const structureVisibleTurnItems = new WeakMap<VisibleTurnItem, ThreadStructureVisibleTurnItem>();
86
87function toThreadStructureTurnItem(item: TurnItem): ThreadStructureTurnItem {
88 if (item.type !== "assistant_message" && item.type !== "reasoning") return item;
89 let structure = structureTurnItems.get(item);
90 if (structure === undefined) {
91 const { text: _text, updatedAt: _updatedAt, ...rest } = item;
92 structure = rest;
93 structureTurnItems.set(item, structure);
94 }
95 return structure;
96}
97
98function toThreadStructureVisibleTurnItem(
99 visibleItem: VisibleTurnItem,
100): ThreadStructureVisibleTurnItem {
101 const item = toThreadStructureTurnItem(visibleItem.item);
102 if (item === visibleItem.item) return visibleItem;
103 let structure = structureVisibleTurnItems.get(visibleItem);
104 if (structure === undefined) {
105 structure = { ...visibleItem, item };
106 structureVisibleTurnItems.set(visibleItem, structure);
107 }
108 return structure;
109}
110
111/** Whether the structure kept every field of this row: true for all but streamed items. */
112export function isCompleteTurnItemRow(row: ThreadStructureVisibleTurnItem): row is VisibleTurnItem {
113 return row.item.type !== "assistant_message" && row.item.type !== "reasoning";
114}
115
116// The open thread is marked visited several times while a reply streams. Visit
117// state is read from the shell, or from lastVisitedAtAtom while the thread is
118// not in the shell index yet, so a mark is not structure.
119function toThreadStructureThread(thread: AppThread): ThreadStructureThread {
120 const { updatedAt: _updatedAt, lastVisitedAt: _lastVisitedAt, ...structure } = thread;
121 return structure;
122}
123
124function toThreadStructure(projection: OrchestrationV2ThreadProjection): ThreadStructure {
125 const { updatedAt: _updatedAt, ...structure } = projection;
126 return {
127 ...structure,
128 thread: toThreadStructureThread(projection.thread),
129 turnItems: projection.turnItems.map(toThreadStructureTurnItem),
130 visibleTurnItems: projection.visibleTurnItems.map(toThreadStructureVisibleTurnItem),
131 };
132}
133
134const sameTurnItem = Schema.toEquivalence(OrchestrationV2TurnItem);
135const sameAppThread = Schema.toEquivalence(OrchestrationV2AppThread);
136
137// Overlays previous's structure on next, so the fields the strip functions
138// remove are exactly the ones ignored here.
139function sameApartFromStripped<T extends object>(
140 same: (left: T, right: T) => boolean,
141 previous: T,
142 previousStructure: object,
143 next: T,
144): boolean {
145 return (
146 Object.keys(previous).length === Object.keys(next).length &&
147 same(Object.assign({}, next, previousStructure), next)
148 );
149}
150
151function sameTurnItemStructure(previous: TurnItem, next: TurnItem): boolean {
152 if (previous === next) return true;
153 const structure = toThreadStructureTurnItem(previous);
154 if (previous.id !== next.id || previous.type !== next.type || structure === previous) {
155 return false;
156 }
157 return sameApartFromStripped(sameTurnItem, previous, structure, next);
158}
159
160function sameProjectionArray<T>(
161 previous: ReadonlyArray<T>,
162 next: ReadonlyArray<T>,
163 same: (previous: T, next: T) => boolean,
164): boolean {
165 if (previous === next) return true;
166 if (previous.length !== next.length) return false;
167 const replacedIndex = getProjectionArrayReplacementIndex(previous, next);
168 if (replacedIndex !== undefined) return same(previous[replacedIndex]!, next[replacedIndex]!);
169 return previous.every((item, index) => same(item, next[index]!));
170}
171
172function sameVisibleTurnItemStructure(before: VisibleTurnItem, after: VisibleTurnItem): boolean {
173 return (
174 before === after ||
175 (before.position === after.position &&
176 before.visibility === after.visibility &&
177 before.sourceThreadId === after.sourceThreadId &&
178 before.sourceItemId === after.sourceItemId &&
179 sameTurnItemStructure(before.item, after.item))
180 );
181}
182
183function isStreamingAssistantMessage(visibleItem: VisibleTurnItem): boolean {
184 return visibleItem.item.type === "assistant_message" && visibleItem.item.streaming;
185}
186
187/** Whether `next` only streams more text into assistant messages that were already streaming. */
188function sameApartFromStreamedAssistantText(
189 previous: OrchestrationV2ThreadProjection["visibleTurnItems"],
190 next: OrchestrationV2ThreadProjection["visibleTurnItems"],
191): boolean {
192 return sameProjectionArray(
193 previous,
194 next,
195 (before, after) =>
196 before === after ||
197 (isStreamingAssistantMessage(before) &&
198 isStreamingAssistantMessage(after) &&
199 sameVisibleTurnItemStructure(before, after)),
200 );
201}
202
203/**
204 * Whether `previous` and `next` have the same ThreadStructure: they differ
205 * only by text streamed into existing assistant and reasoning items or by a
206 * visit mark. The same items exist, with the same status, and every other
207 * field is unchanged.
208 */
209export function sameThreadStructure(
210 previous: OrchestrationV2ThreadProjection,
211 next: OrchestrationV2ThreadProjection,
212): boolean {
213 if (previous === next) return true;
214 for (const key of Object.keys(next) as Array<keyof OrchestrationV2ThreadProjection>) {
215 if (
216 key === "thread" ||
217 key === "turnItems" ||
218 key === "visibleTurnItems" ||
219 key === "updatedAt"
220 ) {
221 continue;
222 }
223 if (previous[key] !== next[key]) return false;
224 }
225 if (
226 previous.thread !== next.thread &&
227 !sameApartFromStripped(
228 sameAppThread,
229 previous.thread,
230 toThreadStructureThread(previous.thread),
231 next.thread,
232 )
233 ) {
234 return false;
235 }
236 return (
237 sameProjectionArray(previous.turnItems, next.turnItems, sameTurnItemStructure) &&
238 sameProjectionArray(
239 previous.visibleTurnItems,
240 next.visibleTurnItems,
241 sameVisibleTurnItemStructure,
242 )
243 );
244}
245
246export function createEnvironmentThreadDetailAtoms<E>(
247 threadStateAtom: (
248 environmentId: ScopedThreadRef["environmentId"],
249 threadId: ScopedThreadRef["threadId"],
250 ) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentThreadState, E>>,
251) {
252 const threadStateValueAtomFamily = Atom.family((key: string) => {
253 const ref = parseThreadKey(key);
254 return Atom.make((get) =>
255 AsyncResult.getOrElse(
256 get(threadStateAtom(ref.environmentId, ref.threadId)),
257 () => EMPTY_ENVIRONMENT_THREAD_STATE,
258 ),
259 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-state-value:${key}`));
260 });
261
262 const threadAtomFamily = Atom.family((key: string) => {
263 const ref = parseThreadKey(key);
264 let previousProjection: OrchestrationV2ThreadProjection | null = null;
265 let previousValue: EnvironmentThread | null = null;
266 return Atom.make((get) => {
267 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
268 if (projection === previousProjection) return previousValue;
269 previousProjection = projection;
270 previousValue = projection === null ? null : { environmentId: ref.environmentId, projection };
271 return previousValue;
272 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread:${key}`));
273 });
274
275 // Streamed assistant and reasoning text replaces an item several times a
276 // second. Readers of the thread's structure (runs, requests, which items
277 // exist and their status) keep the previous value through those updates.
278 const quietThreadAtomFamily = Atom.family((key: string) => {
279 let previousProjection: OrchestrationV2ThreadProjection | null = null;
280 let previous: EnvironmentThreadStructure | null = null;
281 return Atom.make((get) => {
282 const next = get(threadAtomFamily(key));
283 if (next === null) {
284 previousProjection = null;
285 previous = null;
286 } else if (
287 previousProjection === null ||
288 !sameThreadStructure(previousProjection, next.projection)
289 ) {
290 previous = {
291 environmentId: next.environmentId,
292 projection: toThreadStructure(next.projection),
293 };
294 }
295 // Comparing the latest projection lets consecutive replacements bypass history scans.
296 previousProjection = next?.projection ?? null;
297 return previous;
298 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-quiet-thread:${key}`));
299 });
300
301 const visibleTurnItemsAtomFamily = Atom.family((key: string) => {
302 const projectQuestionHistory = createQuestionHistoryProjector();
303 return Atom.make((get): OrchestrationV2ThreadProjection["visibleTurnItems"] => {
304 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
305 return projection === null ? EMPTY_VISIBLE_TURN_ITEMS : projectQuestionHistory(projection);
306 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-visible-turn-items:${key}`));
307 });
308
309 const quietVisibleTurnItemsAtomFamily = Atom.family((key: string) => {
310 const projectQuestionHistory = createQuestionHistoryProjector<ThreadStructureVisibleTurnItem>();
311 return Atom.make((get) => {
312 const thread = get(quietThreadAtomFamily(key));
313 return thread === null
314 ? EMPTY_THREAD_STRUCTURE_TURN_ITEMS
315 : projectQuestionHistory(thread.projection);
316 }).pipe(
317 Atom.setIdleTTL(0),
318 Atom.withLabel(`environment-quiet-thread-visible-turn-items:${key}`),
319 );
320 });
321
322 // Holds while only streamed assistant text moves, so the timeline re-renders on structure;
323 // each streaming message reads its text through assistantMessageTextAtom.
324 const timelineVisibleTurnItemsAtomFamily = Atom.family((key: string) => {
325 let previous = EMPTY_VISIBLE_TURN_ITEMS;
326 return Atom.make((get) => {
327 const next = get(visibleTurnItemsAtomFamily(key));
328 if (!sameApartFromStreamedAssistantText(previous, next)) previous = next;
329 return previous;
330 }).pipe(
331 Atom.setIdleTTL(0),
332 Atom.withLabel(`environment-thread-timeline-visible-turn-items:${key}`),
333 );
334 });
335
336 const assistantMessageTextAtomFamily = Atom.family((key: string) => {
337 const separator = key.lastIndexOf("\u0000");
338 const thread = key.slice(0, separator);
339 const itemId = key.slice(separator + 1);
340 return Atom.make((get) => {
341 const item = get(visibleTurnItemsAtomFamily(thread)).find(
342 (visibleItem) => visibleItem.item.id === itemId,
343 )?.item;
344 return item?.type === "assistant_message" ? item.text : null;
345 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-assistant-message-text:${key}`));
346 });
347
348 const queueWorkflowAtomFamily = Atom.family((key: string) => {
349 let previous: Pick<
350 OrchestrationV2ThreadProjection,
351 "thread" | "runs" | "messages" | "providerThreads" | "providerSessions" | "providerTurns"
352 > | null = null;
353 let value: ThreadQueueWorkflowState | null = null;
354 return Atom.make((get) => {
355 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
356 if (projection === null) {
357 previous = null;
358 value = null;
359 } else if (
360 previous === null ||
361 projection.thread !== previous.thread ||
362 projection.runs !== previous.runs ||
363 projection.messages !== previous.messages ||
364 projection.providerThreads !== previous.providerThreads ||
365 projection.providerSessions !== previous.providerSessions ||
366 projection.providerTurns !== previous.providerTurns
367 ) {
368 value = deriveThreadQueueWorkflowState(projection);
369 const { thread, runs, messages, providerThreads, providerSessions, providerTurns } =
370 projection;
371 previous = { thread, runs, messages, providerThreads, providerSessions, providerTurns };
372 }
373 return value;
374 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-queue:${key}`));
375 });
376 const runStopAtomFamily = Atom.family((key: string) => {
377 let previous: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems"> | null = null;
378 let stop: RunStopPresentation | null = null;
379 return Atom.make((get) => {
380 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
381 if (projection === null) {
382 previous = null;
383 stop = null;
384 } else if (
385 projection.runs !== previous?.runs ||
386 projection.turnItems !== previous?.turnItems
387 ) {
388 const { runs, turnItems } = projection;
389 previous = { runs, turnItems };
390 const next = presentRunStop(previous);
391 if (
392 next?.state !== stop?.state ||
393 next?.runId !== stop?.runId ||
394 next?.requestedAt !== stop?.requestedAt ||
395 next?.background !== stop?.background
396 )
397 stop = next;
398 }
399 return stop;
400 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-run-stop:${key}`));
401 });
402 const queuedCountAtomFamily = Atom.family((key: string) => {
403 let previous: Pick<OrchestrationV2ThreadProjection, "runs" | "messages"> | null = null;
404 let count = 0;
405 return Atom.make((get) => {
406 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
407 if (projection === null) {
408 previous = null;
409 count = 0;
410 } else if (projection.runs !== previous?.runs || projection.messages !== previous?.messages) {
411 const { runs, messages } = projection;
412 previous = { runs, messages };
413 count = getUserQueuedThreadRuns(previous).length;
414 }
415 return count;
416 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-queue-count:${key}`));
417 });
418
419 const turnSubagentsAtomFamily = Atom.family((key: string) => {
420 let previous: Pick<OrchestrationV2ThreadProjection, "runs" | "subagents"> | null = null;
421 let value: ThreadTurnSubagents | null = null;
422 return Atom.make((get) => {
423 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
424 if (projection === null) {
425 previous = null;
426 value = null;
427 } else if (
428 projection.runs !== previous?.runs ||
429 projection.subagents !== previous?.subagents
430 ) {
431 const { runs, subagents } = projection;
432 previous = { runs, subagents };
433 value = deriveThreadTurnSubagents(previous);
434 }
435 return value;
436 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-turn-subagents:${key}`));
437 });
438
439 const checkpointSummariesAtomFamily = Atom.family((key: string) => {
440 let previous: Pick<OrchestrationV2ThreadProjection, "checkpoints" | "messages"> | null = null;
441 let value = EMPTY_CHECKPOINT_SUMMARIES;
442 return Atom.make((get) => {
443 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
444 if (projection === null) {
445 previous = null;
446 value = EMPTY_CHECKPOINT_SUMMARIES;
447 } else if (
448 projection.checkpoints !== previous?.checkpoints ||
449 projection.messages !== previous?.messages
450 ) {
451 const { checkpoints, messages } = projection;
452 previous = { checkpoints, messages };
453 value = deriveThreadCheckpointSummaries(previous);
454 }
455 return value;
456 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-checkpoint-summaries:${key}`));
457 });
458
459 const lastVisitedAtAtomFamily = Atom.family((key: string) =>
460 Atom.make(
461 (get) =>
462 Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.thread.lastVisitedAt ?? null,
463 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-last-visited:${key}`)),
464 );
465 const updatedAtAtomFamily = Atom.family((key: string) =>
466 Atom.make(
467 (get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.updatedAt ?? null,
468 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-updated-at:${key}`)),
469 );
470 const worktreePathAtomFamily = Atom.family((key: string) =>
471 Atom.make(
472 (get) =>
473 Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.thread.worktreePath ?? null,
474 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-worktree:${key}`)),
475 );
476 const pendingRequestsAtomFamily = Atom.family((key: string) => {
477 let previous: Pick<OrchestrationV2ThreadProjection, "runtimeRequests" | "turnItems"> | null =
478 null;
479 let value: PendingThreadRequests | null = null;
480 return Atom.make((get) => {
481 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
482 if (projection === null) {
483 previous = null;
484 value = null;
485 return value;
486 }
487 const runtimeRequests = projection.runtimeRequests.filter(
488 (request) => request.status === "pending",
489 );
490 const ids = new Set(runtimeRequests.map((request) => request.id));
491 const turnItems =
492 ids.size === 0
493 ? []
494 : projection.turnItems.filter(
495 (item) =>
496 (item.type === "approval_request" || item.type === "user_input_request") &&
497 ids.has(item.requestId),
498 );
499 if (
500 previous === null ||
501 !arrayElementsEqual(previous.runtimeRequests, runtimeRequests) ||
502 !arrayElementsEqual(previous.turnItems, turnItems)
503 ) {
504 previous = { runtimeRequests, turnItems };
505 value = derivePendingThreadRequests(previous);
506 }
507 return value;
508 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-pending-requests:${key}`));
509 });
510
511 const statusAtomFamily = Atom.family((key: string) =>
512 Atom.make((get) => get(threadStateValueAtomFamily(key)).status).pipe(
513 Atom.setIdleTTL(0),
514 Atom.withLabel(`environment-thread-status:${key}`),
515 ),
516 );
517 const sequenceAtomFamily = Atom.family((key: string) =>
518 Atom.make((get) => get(threadStateValueAtomFamily(key)).sequence).pipe(
519 Atom.setIdleTTL(0),
520 Atom.withLabel(`environment-thread-sequence:${key}`),
521 ),
522 );
523 const errorAtomFamily = Atom.family((key: string) =>
524 Atom.make((get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).error)).pipe(
525 Atom.setIdleTTL(0),
526 Atom.withLabel(`environment-thread-error:${key}`),
527 ),
528 );
529 const historyAtomFamily = Atom.family((key: string) =>
530 Atom.make((get) => get(threadStateValueAtomFamily(key)).history).pipe(
531 Atom.setIdleTTL(0),
532 Atom.withLabel(`environment-thread-history:${key}`),
533 ),
534 );
535
536 const workspaceMutationIdAtomFamily = Atom.family((key: string) =>
537 Atom.make((get) =>
538 deriveWorkspaceMutationId(
539 get(quietVisibleTurnItemsAtomFamily(key)),
540 get(checkpointSummariesAtomFamily(key)).at(-1)?.completedAt ?? null,
541 ),
542 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-workspace-mutation-id:${key}`)),
543 );
544
545 return {
546 worktreePathAtom: (ref: ScopedThreadRef) => worktreePathAtomFamily(threadKey(ref)),
547 workspaceMutationIdAtom: (ref: ScopedThreadRef) =>
548 workspaceMutationIdAtomFamily(threadKey(ref)),
549 updatedAtAtom: (ref: ScopedThreadRef) => updatedAtAtomFamily(threadKey(ref)),
550 lastVisitedAtAtom: (ref: ScopedThreadRef) => lastVisitedAtAtomFamily(threadKey(ref)),
551 pendingRequestsAtom: (ref: ScopedThreadRef) => pendingRequestsAtomFamily(threadKey(ref)),
552 queueWorkflowAtom: (ref: ScopedThreadRef) => queueWorkflowAtomFamily(threadKey(ref)),
553 queuedCountAtom: (ref: ScopedThreadRef) => queuedCountAtomFamily(threadKey(ref)),
554 runStopAtom: (ref: ScopedThreadRef) => runStopAtomFamily(threadKey(ref)),
555 turnSubagentsAtom: (ref: ScopedThreadRef) => turnSubagentsAtomFamily(threadKey(ref)),
556 checkpointSummariesAtom: (ref: ScopedThreadRef) =>
557 checkpointSummariesAtomFamily(threadKey(ref)),
558 stateAtom: (ref: ScopedThreadRef) => threadStateValueAtomFamily(threadKey(ref)),
559 threadAtom: (ref: ScopedThreadRef) => threadAtomFamily(threadKey(ref)),
560 quietThreadAtom: (ref: ScopedThreadRef) => quietThreadAtomFamily(threadKey(ref)),
561 visibleTurnItemsAtom: (ref: ScopedThreadRef) => visibleTurnItemsAtomFamily(threadKey(ref)),
562 quietVisibleTurnItemsAtom: (ref: ScopedThreadRef) =>
563 quietVisibleTurnItemsAtomFamily(threadKey(ref)),
564 timelineVisibleTurnItemsAtom: (ref: ScopedThreadRef) =>
565 timelineVisibleTurnItemsAtomFamily(threadKey(ref)),
566 assistantMessageTextAtom: (ref: ScopedThreadRef, itemId: string) =>
567 assistantMessageTextAtomFamily(`${threadKey(ref)}\u0000${itemId}`),
568 statusAtom: (ref: ScopedThreadRef) => statusAtomFamily(threadKey(ref)),
569 sequenceAtom: (ref: ScopedThreadRef) => sequenceAtomFamily(threadKey(ref)),
570 errorAtom: (ref: ScopedThreadRef) => errorAtomFamily(threadKey(ref)),
571 historyAtom: (ref: ScopedThreadRef) => historyAtomFamily(threadKey(ref)),
572 };
573}