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

threadDetail.ts

185 lines7.0 KB

1import type {
2 OrchestrationCheckpointSummary,
3 OrchestrationLatestTurn,
4 OrchestrationMessage,
5 OrchestrationProposedPlan,
6 OrchestrationSession,
7 OrchestrationThread,
8 OrchestrationThreadActivity,
9 ScopedThreadRef,
10} from "@t3tools/contracts";
11import * as Option from "effect/Option";
12import { AsyncResult, Atom } from "effect/unstable/reactivity";
13​
14import type { EnvironmentThread, EnvironmentThreadShell } from "./models.ts";
15import { scopeThread } from "./models.ts";
16import { EMPTY_ENVIRONMENT_THREAD_STATE, type EnvironmentThreadState } from "./threads.ts";
17import { parseThreadKey, threadKey } from "./entities.ts";
18import { THREAD_STATE_IDLE_TTL_MS } from "./threadRetention.ts";
19​
20const EMPTY_MESSAGES: ReadonlyArray<OrchestrationMessage> = Object.freeze([]);
21const EMPTY_ACTIVITIES: ReadonlyArray<OrchestrationThreadActivity> = Object.freeze([]);
22const EMPTY_PROPOSED_PLANS: ReadonlyArray<OrchestrationProposedPlan> = Object.freeze([]);
23const EMPTY_CHECKPOINTS: ReadonlyArray<OrchestrationCheckpointSummary> = Object.freeze([]);
24​
25/**
26 * Combine detail-only collections with the shell's authoritative thread metadata.
27 *
28 * Shell and detail subscriptions are intentionally independent. A cached detail can
29 * therefore briefly outlive a newer shell snapshot after reconnecting. Workspace
30 * consumers must use the shell branch/worktree/project fields so they do not target
31 * a stale checkout while retaining messages, activities, plans, and checkpoints
32 * from the detail subscription.
33 */
34export function mergeEnvironmentThread(
35 detail: EnvironmentThread | null,
36 shell: EnvironmentThreadShell | null,
37): EnvironmentThread | null {
38 if (detail === null || shell === null) {
39 return detail;
40 }
41 if (detail.environmentId !== shell.environmentId || detail.id !== shell.id) {
42 return detail;
43 }
44​
45 return {
46 ...detail,
47 environmentId: shell.environmentId,
48 id: shell.id,
49 projectId: shell.projectId,
50 title: shell.title,
51 modelSelection: shell.modelSelection,
52 runtimeMode: shell.runtimeMode,
53 interactionMode: shell.interactionMode,
54 branch: shell.branch,
55 worktreePath: shell.worktreePath,
56 latestTurn: shell.latestTurn,
57 createdAt: shell.createdAt,
58 updatedAt: shell.updatedAt,
59 archivedAt: shell.archivedAt,
60 session: shell.session,
61 };
63​
64export function createEnvironmentThreadDetailAtoms<E>(
65 threadStateAtom: (
66 environmentId: ScopedThreadRef["environmentId"],
67 threadId: ScopedThreadRef["threadId"],
68 ) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentThreadState, E>>,
69) {
70 const threadStateValueAtomFamily = Atom.family((key: string) => {
71 const ref = parseThreadKey(key);
72 return Atom.make((get) =>
73 Option.getOrElse(
74 AsyncResult.value(get(threadStateAtom(ref.environmentId, ref.threadId))),
75 () => EMPTY_ENVIRONMENT_THREAD_STATE,
76 ),
77 ).pipe(
78 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
79 Atom.withLabel(`environment-thread-state-value:${key}`),
80 );
81 });
82​
83 const threadDetailAtomFamily = Atom.family((key: string) => {
84 const ref = parseThreadKey(key);
85 let previousSource: OrchestrationThread | null = null;
86 let previousValue: EnvironmentThread | null = null;
87 return Atom.make((get) => {
88 const source = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
89 if (source === previousSource) {
90 return previousValue;
91 }
92 previousSource = source;
93 previousValue = source === null ? null : scopeThread(ref.environmentId, source);
94 return previousValue;
95 }).pipe(
96 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
97 Atom.withLabel(`environment-thread-detail:${key}`),
98 );
99 });
100​
101 const threadStatusAtomFamily = Atom.family((key: string) =>
102 Atom.make((get) => get(threadStateValueAtomFamily(key)).status).pipe(
103 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
104 Atom.withLabel(`environment-thread-status:${key}`),
105 ),
106 );
107​
108 const threadErrorAtomFamily = Atom.family((key: string) =>
109 Atom.make((get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).error)).pipe(
110 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
111 Atom.withLabel(`environment-thread-error:${key}`),
112 ),
113 );
114​
115 const threadMessagesAtomFamily = Atom.family((key: string) =>
116 Atom.make(
117 (get): ReadonlyArray<OrchestrationMessage> =>
118 get(threadDetailAtomFamily(key))?.messages ?? EMPTY_MESSAGES,
119 ).pipe(
120 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
121 Atom.withLabel(`environment-thread-messages:${key}`),
122 ),
123 );
124​
125 const threadActivitiesAtomFamily = Atom.family((key: string) =>
126 Atom.make(
127 (get): ReadonlyArray<OrchestrationThreadActivity> =>
128 get(threadDetailAtomFamily(key))?.activities ?? EMPTY_ACTIVITIES,
129 ).pipe(
130 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
131 Atom.withLabel(`environment-thread-activities:${key}`),
132 ),
133 );
134​
135 const threadProposedPlansAtomFamily = Atom.family((key: string) =>
136 Atom.make(
137 (get): ReadonlyArray<OrchestrationProposedPlan> =>
138 get(threadDetailAtomFamily(key))?.proposedPlans ?? EMPTY_PROPOSED_PLANS,
139 ).pipe(
140 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
141 Atom.withLabel(`environment-thread-proposed-plans:${key}`),
142 ),
143 );
144​
145 const threadCheckpointsAtomFamily = Atom.family((key: string) =>
146 Atom.make(
147 (get): ReadonlyArray<OrchestrationCheckpointSummary> =>
148 get(threadDetailAtomFamily(key))?.checkpoints ?? EMPTY_CHECKPOINTS,
149 ).pipe(
150 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
151 Atom.withLabel(`environment-thread-checkpoints:${key}`),
152 ),
153 );
154​
155 const threadSessionAtomFamily = Atom.family((key: string) =>
156 Atom.make(
157 (get): OrchestrationSession | null => get(threadDetailAtomFamily(key))?.session ?? null,
158 ).pipe(
159 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
160 Atom.withLabel(`environment-thread-session:${key}`),
161 ),
162 );
163​
164 const threadLatestTurnAtomFamily = Atom.family((key: string) =>
165 Atom.make(
166 (get): OrchestrationLatestTurn | null => get(threadDetailAtomFamily(key))?.latestTurn ?? null,
167 ).pipe(
168 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
169 Atom.withLabel(`environment-thread-latest-turn:${key}`),
170 ),
171 );
172​
173 return {
174 stateAtom: (ref: ScopedThreadRef) => threadStateValueAtomFamily(threadKey(ref)),
175 detailAtom: (ref: ScopedThreadRef) => threadDetailAtomFamily(threadKey(ref)),
176 statusAtom: (ref: ScopedThreadRef) => threadStatusAtomFamily(threadKey(ref)),
177 errorAtom: (ref: ScopedThreadRef) => threadErrorAtomFamily(threadKey(ref)),
178 messagesAtom: (ref: ScopedThreadRef) => threadMessagesAtomFamily(threadKey(ref)),
179 activitiesAtom: (ref: ScopedThreadRef) => threadActivitiesAtomFamily(threadKey(ref)),
180 proposedPlansAtom: (ref: ScopedThreadRef) => threadProposedPlansAtomFamily(threadKey(ref)),
181 checkpointsAtom: (ref: ScopedThreadRef) => threadCheckpointsAtomFamily(threadKey(ref)),
182 sessionAtom: (ref: ScopedThreadRef) => threadSessionAtomFamily(threadKey(ref)),
183 latestTurnAtom: (ref: ScopedThreadRef) => threadLatestTurnAtomFamily(threadKey(ref)),
184 };