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

threadDetail.ts

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