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

threadDetail.ts

166 lines6.7 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";
18​
19const EMPTY_MESSAGES: ReadonlyArray<OrchestrationMessage> = Object.freeze([]);
20const EMPTY_ACTIVITIES: ReadonlyArray<OrchestrationThreadActivity> = Object.freeze([]);
21const EMPTY_PROPOSED_PLANS: ReadonlyArray<OrchestrationProposedPlan> = Object.freeze([]);
22const EMPTY_CHECKPOINTS: ReadonlyArray<OrchestrationCheckpointSummary> = Object.freeze([]);
23​
24/**
25 * Combine detail-only collections with the shell's authoritative thread metadata.
26 *
27 * Shell and detail subscriptions are intentionally independent. A cached detail can
28 * therefore briefly outlive a newer shell snapshot after reconnecting. Workspace
29 * consumers must use the shell branch/worktree/project fields so they do not target
30 * a stale checkout while retaining messages, activities, plans, and checkpoints
31 * from the detail subscription.
32 */
33export function mergeEnvironmentThread(
34 detail: EnvironmentThread | null,
35 shell: EnvironmentThreadShell | null,
36): EnvironmentThread | null {
37 if (detail === null || shell === null) {
38 return detail;
39 }
40 if (detail.environmentId !== shell.environmentId || detail.id !== shell.id) {
41 return detail;
42 }
43​
44 return {
45 ...detail,
46 environmentId: shell.environmentId,
47 id: shell.id,
48 projectId: shell.projectId,
49 title: shell.title,
50 modelSelection: shell.modelSelection,
51 runtimeMode: shell.runtimeMode,
52 interactionMode: shell.interactionMode,
53 branch: shell.branch,
54 worktreePath: shell.worktreePath,
55 latestTurn: shell.latestTurn,
56 createdAt: shell.createdAt,
57 updatedAt: shell.updatedAt,
58 archivedAt: shell.archivedAt,
59 settledOverride: shell.settledOverride,
60 settledAt: shell.settledAt,
61 snoozedUntil: shell.snoozedUntil,
62 snoozedAt: shell.snoozedAt,
63 pinnedAt: shell.pinnedAt,
64 pinOrderKey: shell.pinOrderKey,
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(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-state-value:${key}`));
83 });
84​
85 const threadDetailAtomFamily = Atom.family((key: string) => {
86 const ref = parseThreadKey(key);
87 let previousSource: OrchestrationThread | null = null;
88 let previousValue: EnvironmentThread | null = null;
89 return Atom.make((get) => {
90 const source = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
91 if (source === previousSource) {
92 return previousValue;
93 }
94 previousSource = source;
95 previousValue = source === null ? null : scopeThread(ref.environmentId, source);
96 return previousValue;
97 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-detail:${key}`));
98 });
99​
100 const threadStatusAtomFamily = Atom.family((key: string) =>
101 Atom.make((get) => get(threadStateValueAtomFamily(key)).status).pipe(
102 Atom.setIdleTTL(0),
103 Atom.withLabel(`environment-thread-status:${key}`),
104 ),
105 );
106​
107 const threadErrorAtomFamily = Atom.family((key: string) =>
108 Atom.make((get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).error)).pipe(
109 Atom.setIdleTTL(0),
110 Atom.withLabel(`environment-thread-error:${key}`),
111 ),
112 );
113​
114 const threadMessagesAtomFamily = Atom.family((key: string) =>
115 Atom.make(
116 (get): ReadonlyArray<OrchestrationMessage> =>
117 get(threadDetailAtomFamily(key))?.messages ?? EMPTY_MESSAGES,
118 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-messages:${key}`)),
119 );
120​
121 const threadActivitiesAtomFamily = Atom.family((key: string) =>
122 Atom.make(
123 (get): ReadonlyArray<OrchestrationThreadActivity> =>
124 get(threadDetailAtomFamily(key))?.activities ?? EMPTY_ACTIVITIES,
125 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-activities:${key}`)),
126 );
127​
128 const threadProposedPlansAtomFamily = Atom.family((key: string) =>
129 Atom.make(
130 (get): ReadonlyArray<OrchestrationProposedPlan> =>
131 get(threadDetailAtomFamily(key))?.proposedPlans ?? EMPTY_PROPOSED_PLANS,
132 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-proposed-plans:${key}`)),
133 );
134​
135 const threadCheckpointsAtomFamily = Atom.family((key: string) =>
136 Atom.make(
137 (get): ReadonlyArray<OrchestrationCheckpointSummary> =>
138 get(threadDetailAtomFamily(key))?.checkpoints ?? EMPTY_CHECKPOINTS,
139 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-checkpoints:${key}`)),
140 );
141​
142 const threadSessionAtomFamily = Atom.family((key: string) =>
143 Atom.make(
144 (get): OrchestrationSession | null => get(threadDetailAtomFamily(key))?.session ?? null,
145 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-session:${key}`)),
146 );
147​
148 const threadLatestTurnAtomFamily = Atom.family((key: string) =>
149 Atom.make(
150 (get): OrchestrationLatestTurn | null => get(threadDetailAtomFamily(key))?.latestTurn ?? null,
151 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-latest-turn:${key}`)),
152 );
153​
154 return {
155 stateAtom: (ref: ScopedThreadRef) => threadStateValueAtomFamily(threadKey(ref)),
156 detailAtom: (ref: ScopedThreadRef) => threadDetailAtomFamily(threadKey(ref)),
157 statusAtom: (ref: ScopedThreadRef) => threadStatusAtomFamily(threadKey(ref)),
158 errorAtom: (ref: ScopedThreadRef) => threadErrorAtomFamily(threadKey(ref)),
159 messagesAtom: (ref: ScopedThreadRef) => threadMessagesAtomFamily(threadKey(ref)),
160 activitiesAtom: (ref: ScopedThreadRef) => threadActivitiesAtomFamily(threadKey(ref)),
161 proposedPlansAtom: (ref: ScopedThreadRef) => threadProposedPlansAtomFamily(threadKey(ref)),
162 checkpointsAtom: (ref: ScopedThreadRef) => threadCheckpointsAtomFamily(threadKey(ref)),
163 sessionAtom: (ref: ScopedThreadRef) => threadSessionAtomFamily(threadKey(ref)),
164 latestTurnAtom: (ref: ScopedThreadRef) => threadLatestTurnAtomFamily(threadKey(ref)),
165 };