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