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

threadDetail.ts

191 lines7.2 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 "./threadState.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 settledOverride: shell.settledOverride,
61 settledAt: shell.settledAt,
62 snoozedUntil: shell.snoozedUntil,
63 snoozedAt: shell.snoozedAt,
64 pinnedAt: shell.pinnedAt,
65 pinOrderKey: shell.pinOrderKey,
66 session: shell.session,
67 };
69​
70export function createEnvironmentThreadDetailAtoms<E>(
71 threadStateAtom: (
72 environmentId: ScopedThreadRef["environmentId"],
73 threadId: ScopedThreadRef["threadId"],
74 ) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentThreadState, E>>,
75) {
76 const threadStateValueAtomFamily = Atom.family((key: string) => {
77 const ref = parseThreadKey(key);
78 return Atom.make((get) =>
79 Option.getOrElse(
80 AsyncResult.value(get(threadStateAtom(ref.environmentId, ref.threadId))),
81 () => EMPTY_ENVIRONMENT_THREAD_STATE,
82 ),
83 ).pipe(
84 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
85 Atom.withLabel(`environment-thread-state-value:${key}`),
86 );
87 });
88​
89 const threadDetailAtomFamily = Atom.family((key: string) => {
90 const ref = parseThreadKey(key);
91 let previousSource: OrchestrationThread | null = null;
92 let previousValue: EnvironmentThread | null = null;
93 return Atom.make((get) => {
94 const source = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
95 if (source === previousSource) {
96 return previousValue;
97 }
98 previousSource = source;
99 previousValue = source === null ? null : scopeThread(ref.environmentId, source);
100 return previousValue;
101 }).pipe(
102 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
103 Atom.withLabel(`environment-thread-detail:${key}`),
104 );
105 });
106​
107 const threadStatusAtomFamily = Atom.family((key: string) =>
108 Atom.make((get) => get(threadStateValueAtomFamily(key)).status).pipe(
109 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
110 Atom.withLabel(`environment-thread-status:${key}`),
111 ),
112 );
113​
114 const threadErrorAtomFamily = Atom.family((key: string) =>
115 Atom.make((get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).error)).pipe(
116 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
117 Atom.withLabel(`environment-thread-error:${key}`),
118 ),
119 );
120​
121 const threadMessagesAtomFamily = Atom.family((key: string) =>
122 Atom.make(
123 (get): ReadonlyArray<OrchestrationMessage> =>
124 get(threadDetailAtomFamily(key))?.messages ?? EMPTY_MESSAGES,
125 ).pipe(
126 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
127 Atom.withLabel(`environment-thread-messages:${key}`),
128 ),
129 );
130​
131 const threadActivitiesAtomFamily = Atom.family((key: string) =>
132 Atom.make(
133 (get): ReadonlyArray<OrchestrationThreadActivity> =>
134 get(threadDetailAtomFamily(key))?.activities ?? EMPTY_ACTIVITIES,
135 ).pipe(
136 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
137 Atom.withLabel(`environment-thread-activities:${key}`),
138 ),
139 );
140​
141 const threadProposedPlansAtomFamily = Atom.family((key: string) =>
142 Atom.make(
143 (get): ReadonlyArray<OrchestrationProposedPlan> =>
144 get(threadDetailAtomFamily(key))?.proposedPlans ?? EMPTY_PROPOSED_PLANS,
145 ).pipe(
146 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
147 Atom.withLabel(`environment-thread-proposed-plans:${key}`),
148 ),
149 );
150​
151 const threadCheckpointsAtomFamily = Atom.family((key: string) =>
152 Atom.make(
153 (get): ReadonlyArray<OrchestrationCheckpointSummary> =>
154 get(threadDetailAtomFamily(key))?.checkpoints ?? EMPTY_CHECKPOINTS,
155 ).pipe(
156 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
157 Atom.withLabel(`environment-thread-checkpoints:${key}`),
158 ),
159 );
160​
161 const threadSessionAtomFamily = Atom.family((key: string) =>
162 Atom.make(
163 (get): OrchestrationSession | null => get(threadDetailAtomFamily(key))?.session ?? null,
164 ).pipe(
165 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
166 Atom.withLabel(`environment-thread-session:${key}`),
167 ),
168 );
169​
170 const threadLatestTurnAtomFamily = Atom.family((key: string) =>
171 Atom.make(
172 (get): OrchestrationLatestTurn | null => get(threadDetailAtomFamily(key))?.latestTurn ?? null,
173 ).pipe(
174 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
175 Atom.withLabel(`environment-thread-latest-turn:${key}`),
176 ),
177 );
178​
179 return {
180 stateAtom: (ref: ScopedThreadRef) => threadStateValueAtomFamily(threadKey(ref)),
181 detailAtom: (ref: ScopedThreadRef) => threadDetailAtomFamily(threadKey(ref)),
182 statusAtom: (ref: ScopedThreadRef) => threadStatusAtomFamily(threadKey(ref)),
183 errorAtom: (ref: ScopedThreadRef) => threadErrorAtomFamily(threadKey(ref)),
184 messagesAtom: (ref: ScopedThreadRef) => threadMessagesAtomFamily(threadKey(ref)),
185 activitiesAtom: (ref: ScopedThreadRef) => threadActivitiesAtomFamily(threadKey(ref)),
186 proposedPlansAtom: (ref: ScopedThreadRef) => threadProposedPlansAtomFamily(threadKey(ref)),
187 checkpointsAtom: (ref: ScopedThreadRef) => threadCheckpointsAtomFamily(threadKey(ref)),
188 sessionAtom: (ref: ScopedThreadRef) => threadSessionAtomFamily(threadKey(ref)),
189 latestTurnAtom: (ref: ScopedThreadRef) => threadLatestTurnAtomFamily(threadKey(ref)),
190 };