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

threadDetail.ts

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