Open History to see the last commit
1import * as Crypto from "effect/Crypto";
2import { Atom } from "effect/unstable/reactivity";
3import {
4 WS_METHODS,
5 type EnvironmentId,
6 type OrchestrationShellSnapshot,
7} from "@t3tools/contracts";
8
9import { createOptimisticThreadLifecycle } from "./threadLifecycle.ts";
10import { canSnooze } from "./threadSettled.ts";
11
12import {
13 createAtomCommandScheduler,
14 createEnvironmentCommand,
15 createEnvironmentRpcCommand,
16} from "./runtime.ts";
17import {
18 type ArchiveThreadInput,
19 type CreateThreadInput,
20 type DeleteThreadInput,
21 type InterruptThreadTurnInput,
22 type LinkThreadPullRequestInput,
23 type RespondToThreadApprovalInput,
24 type RespondToThreadUserInputInput,
25 type DismissThreadUserInputInput,
26 type RevertThreadCheckpointInput,
27 type SetThreadInteractionModeInput,
28 type SetThreadRuntimeModeInput,
29 type PinThreadInput,
30 type ReorderPinnedThreadInput,
31 type ReorderActiveThreadInput,
32 type SetThreadAutoSettleInput,
33 type SettleThreadInput,
34 type SnoozeThreadInput,
35 type StartThreadTurnInput,
36 type StopThreadSessionInput,
37 type UnarchiveThreadInput,
38 type UnlinkThreadPullRequestInput,
39 type UnpinThreadInput,
40 type UnsettleThreadInput,
41 type UnsnoozeThreadInput,
42 type UpdateThreadMetadataInput,
43 archiveThread,
44 createThread,
45 deleteThread,
46 interruptThreadTurn,
47 linkThreadPullRequest,
48 respondToThreadApproval,
49 respondToThreadUserInput,
50 dismissThreadUserInput,
51 revertThreadCheckpoint,
52 setThreadInteractionMode,
53 setThreadRuntimeMode,
54 pinThread,
55 reorderPinnedThread,
56 reorderActiveThread,
57 setThreadAutoSettle,
58 settleThread,
59 snoozeThread,
60 startThreadTurn,
61 stopThreadSession,
62 unarchiveThread,
63 unlinkThreadPullRequest,
64 unpinThread,
65 unsettleThread,
66 unsnoozeThread,
67 updateThreadMetadata,
68} from "../operations/commands.ts";
69import type { EnvironmentRegistry } from "../connection/registry.ts";
70
71export type {
72 ArchiveThreadInput,
73 CreateThreadInput,
74 DeleteThreadInput,
75 InterruptThreadTurnInput,
76 LinkThreadPullRequestInput,
77 RespondToThreadApprovalInput,
78 RespondToThreadUserInputInput,
79 DismissThreadUserInputInput,
80 RevertThreadCheckpointInput,
81 SetThreadInteractionModeInput,
82 SetThreadRuntimeModeInput,
83 PinThreadInput,
84 ReorderPinnedThreadInput,
85 ReorderActiveThreadInput,
86 SetThreadAutoSettleInput,
87 SettleThreadInput,
88 SnoozeThreadInput,
89 StartThreadTurnInput,
90 StopThreadSessionInput,
91 UnarchiveThreadInput,
92 UnlinkThreadPullRequestInput,
93 UnpinThreadInput,
94 UnsettleThreadInput,
95 UnsnoozeThreadInput,
96 UpdateThreadMetadataInput,
97} from "../operations/commands.ts";
98
99export function createThreadEnvironmentAtoms<R, E>(
100 runtime: Atom.AtomRuntime<EnvironmentRegistry | Crypto.Crypto | R, E>,
101 snapshotAtom: (environmentId: EnvironmentId) => Atom.Atom<OrchestrationShellSnapshot | null>,
102) {
103 const scheduler = createAtomCommandScheduler();
104 const concurrency = {
105 mode: "serial" as const,
106 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
107 JSON.stringify([environmentId, input.threadId]),
108 };
109 const commands = {
110 create: createEnvironmentCommand(runtime, {
111 label: "environment-data:commands:thread:create",
112 execute: (input: CreateThreadInput) => createThread(input),
113 scheduler,
114 concurrency,
115 }),
116 delete: createEnvironmentCommand(runtime, {
117 label: "environment-data:commands:thread:delete",
118 execute: (input: DeleteThreadInput) => deleteThread(input),
119 scheduler,
120 concurrency,
121 }),
122 archive: createEnvironmentCommand(runtime, {
123 label: "environment-data:commands:thread:archive",
124 execute: (input: ArchiveThreadInput) => archiveThread(input),
125 scheduler,
126 concurrency,
127 }),
128 unarchive: createEnvironmentCommand(runtime, {
129 label: "environment-data:commands:thread:unarchive",
130 execute: (input: UnarchiveThreadInput) => unarchiveThread(input),
131 scheduler,
132 concurrency,
133 }),
134 settle: createEnvironmentCommand(runtime, {
135 label: "environment-data:commands:thread:settle",
136 execute: (input: SettleThreadInput) => settleThread(input),
137 scheduler,
138 concurrency,
139 }),
140 unsettle: createEnvironmentCommand(runtime, {
141 label: "environment-data:commands:thread:unsettle",
142 execute: (input: UnsettleThreadInput) => unsettleThread(input),
143 scheduler,
144 concurrency,
145 }),
146 snooze: createEnvironmentCommand(runtime, {
147 label: "environment-data:commands:thread:snooze",
148 execute: (input: SnoozeThreadInput) => snoozeThread(input),
149 scheduler,
150 concurrency,
151 }),
152 unsnooze: createEnvironmentCommand(runtime, {
153 label: "environment-data:commands:thread:unsnooze",
154 execute: (input: UnsnoozeThreadInput) => unsnoozeThread(input),
155 scheduler,
156 concurrency,
157 }),
158 pin: createEnvironmentCommand(runtime, {
159 label: "environment-data:commands:thread:pin",
160 execute: (input: PinThreadInput) => pinThread(input),
161 scheduler,
162 concurrency,
163 }),
164 unpin: createEnvironmentCommand(runtime, {
165 label: "environment-data:commands:thread:unpin",
166 execute: (input: UnpinThreadInput) => unpinThread(input),
167 scheduler,
168 concurrency,
169 }),
170 reorderPin: createEnvironmentCommand(runtime, {
171 label: "environment-data:commands:thread:reorder-pin",
172 execute: (input: ReorderPinnedThreadInput) => reorderPinnedThread(input),
173 scheduler,
174 concurrency,
175 }),
176 setAutoSettle: createEnvironmentCommand(runtime, {
177 label: "environment-data:commands:thread:set-auto-settle",
178 execute: (input: SetThreadAutoSettleInput) => setThreadAutoSettle(input),
179 scheduler,
180 concurrency,
181 }),
182 reorderActive: createEnvironmentCommand(runtime, {
183 label: "environment-data:commands:thread:reorder-active",
184 execute: (input: ReorderActiveThreadInput) => reorderActiveThread(input),
185 scheduler,
186 concurrency,
187 }),
188 updateMetadata: createEnvironmentCommand(runtime, {
189 label: "environment-data:commands:thread:update-metadata",
190 execute: (input: UpdateThreadMetadataInput) => updateThreadMetadata(input),
191 scheduler,
192 concurrency,
193 }),
194 linkPullRequest: createEnvironmentCommand(runtime, {
195 label: "environment-data:commands:thread:link-pull-request",
196 execute: (input: LinkThreadPullRequestInput) => linkThreadPullRequest(input),
197 scheduler,
198 concurrency,
199 }),
200 unlinkPullRequest: createEnvironmentCommand(runtime, {
201 label: "environment-data:commands:thread:unlink-pull-request",
202 execute: (input: UnlinkThreadPullRequestInput) => unlinkThreadPullRequest(input),
203 scheduler,
204 concurrency,
205 }),
206 setRuntimeMode: createEnvironmentCommand(runtime, {
207 label: "environment-data:commands:thread:set-runtime-mode",
208 execute: (input: SetThreadRuntimeModeInput) => setThreadRuntimeMode(input),
209 scheduler,
210 concurrency,
211 }),
212 setInteractionMode: createEnvironmentCommand(runtime, {
213 label: "environment-data:commands:thread:set-interaction-mode",
214 execute: (input: SetThreadInteractionModeInput) => setThreadInteractionMode(input),
215 scheduler,
216 concurrency,
217 }),
218 startTurn: createEnvironmentCommand(runtime, {
219 label: "environment-data:commands:thread:start-turn",
220 execute: (input: StartThreadTurnInput) => startThreadTurn(input),
221 scheduler,
222 concurrency,
223 }),
224 interruptTurn: createEnvironmentCommand(runtime, {
225 label: "environment-data:commands:thread:interrupt-turn",
226 execute: (input: InterruptThreadTurnInput) => interruptThreadTurn(input),
227 scheduler,
228 concurrency,
229 }),
230 respondToApproval: createEnvironmentCommand(runtime, {
231 label: "environment-data:commands:thread:respond-to-approval",
232 execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input),
233 scheduler,
234 concurrency,
235 }),
236 respondToUserInput: createEnvironmentCommand(runtime, {
237 label: "environment-data:commands:thread:respond-to-user-input",
238 execute: (input: RespondToThreadUserInputInput) => respondToThreadUserInput(input),
239 scheduler,
240 concurrency,
241 }),
242 dismissUserInput: createEnvironmentCommand(runtime, {
243 label: "environment-data:commands:thread:dismiss-user-input",
244 execute: (input: DismissThreadUserInputInput) => dismissThreadUserInput(input),
245 scheduler,
246 concurrency,
247 }),
248 revertCheckpoint: createEnvironmentCommand(runtime, {
249 label: "environment-data:commands:thread:revert-checkpoint",
250 execute: (input: RevertThreadCheckpointInput) => revertThreadCheckpoint(input),
251 scheduler,
252 concurrency,
253 }),
254 stopSession: createEnvironmentCommand(runtime, {
255 label: "environment-data:commands:thread:stop-session",
256 execute: (input: StopThreadSessionInput) => stopThreadSession(input),
257 scheduler,
258 concurrency,
259 }),
260 uploadFeedback: createEnvironmentRpcCommand(runtime, {
261 label: "environment-data:commands:thread:upload-feedback",
262 tag: WS_METHODS.providerUploadFeedback,
263 scheduler,
264 concurrency,
265 }),
266 };
267 const optimistic = createOptimisticThreadLifecycle(snapshotAtom);
268 return {
269 ...commands,
270 snapshotAtom: optimistic.snapshotAtom,
271 settle: optimistic.wrap(commands.settle, (thread, _input, now, accepted) =>
272 !accepted &&
273 (!canSnooze(thread, { now }) ||
274 thread.session?.status === "starting" ||
275 thread.session?.status === "running")
276 ? thread
277 : {
278 ...thread,
279 hasPendingApprovals: false,
280 hasPendingUserInput: false,
281 settledOverride: "settled",
282 settledAt: thread.settledOverride === "settled" ? (thread.settledAt ?? now) : now,
283 unsettledAt: null,
284 activeOrderKey: null,
285 pinnedAt: null,
286 pinOrderKey: null,
287 snoozedAt: null,
288 snoozedUntil: null,
289 },
290 ),
291 unsettle: optimistic.wrap(commands.unsettle, (thread, input, now) => ({
292 ...thread,
293 settledOverride: input.reason === "user" ? "active" : null,
294 settledAt: null,
295 unsettledAt: thread.settledOverride === "active" ? (thread.unsettledAt ?? null) : now,
296 })),
297 snooze: optimistic.wrap(commands.snooze, (thread, input, now, accepted) =>
298 (!accepted && !canSnooze(thread, { now })) ||
299 !(Date.parse(input.snoozedUntil) > Date.parse(now))
300 ? thread
301 : {
302 ...thread,
303 hasPendingApprovals: false,
304 hasPendingUserInput: false,
305 snoozedUntil: input.snoozedUntil,
306 snoozedAt: thread.snoozedUntil === input.snoozedUntil ? (thread.snoozedAt ?? now) : now,
307 },
308 ),
309 unsnooze: optimistic.wrap(commands.unsnooze, (thread) => ({
310 ...thread,
311 snoozedUntil: null,
312 snoozedAt: null,
313 })),
314 pin: optimistic.wrap(commands.pin, (thread, input, now) => ({
315 ...thread,
316 pinnedAt: thread.pinnedAt ?? now,
317 pinOrderKey: thread.pinnedAt == null ? (input.orderKey ?? null) : thread.pinOrderKey,
318 ...(thread.settledOverride === "settled"
319 ? {
320 settledOverride: "active" as const,
321 settledAt: null,
322 unsettledAt: now,
323 }
324 : {}),
325 snoozedUntil: null,
326 snoozedAt: null,
327 })),
328 unpin: optimistic.wrap(commands.unpin, (thread) => ({
329 ...thread,
330 pinnedAt: null,
331 pinOrderKey: null,
332 })),
333 reorderPin: optimistic.wrap(commands.reorderPin, (thread, input) => ({
334 ...thread,
335 pinOrderKey: input.orderKey,
336 })),
337 reorderActive: optimistic.wrap(commands.reorderActive, (thread, input) => ({
338 ...thread,
339 activeOrderKey: input.orderKey,
340 })),
341 };
342}