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

threadCommands.ts

348 lines11.8 KB

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 sessionCommand: createEnvironmentRpcCommand(runtime, {
267 label: "environment-data:commands:thread:session-command",
268 tag: WS_METHODS.providerSessionCommand,
269 scheduler,
270 concurrency,
271 }),
272 };
273 const optimistic = createOptimisticThreadLifecycle(snapshotAtom);
274 return {
275 ...commands,
276 snapshotAtom: optimistic.snapshotAtom,
277 settle: optimistic.wrap(commands.settle, (thread, _input, now, accepted) =>
278 !accepted &&
279 (!canSnooze(thread, { now }) ||
280 thread.session?.status === "starting" ||
281 thread.session?.status === "running")
282 ? thread
283 : {
284 ...thread,
285 hasPendingApprovals: false,
286 hasPendingUserInput: false,
287 settledOverride: "settled",
288 settledAt: thread.settledOverride === "settled" ? (thread.settledAt ?? now) : now,
289 unsettledAt: null,
290 activeOrderKey: null,
291 pinnedAt: null,
292 pinOrderKey: null,
293 snoozedAt: null,
294 snoozedUntil: null,
295 },
296 ),
297 unsettle: optimistic.wrap(commands.unsettle, (thread, input, now) => ({
298 ...thread,
299 settledOverride: input.reason === "user" ? "active" : null,
300 settledAt: null,
301 unsettledAt: thread.settledOverride === "active" ? (thread.unsettledAt ?? null) : now,
302 })),
303 snooze: optimistic.wrap(commands.snooze, (thread, input, now, accepted) =>
304 (!accepted && !canSnooze(thread, { now })) ||
305 !(Date.parse(input.snoozedUntil) > Date.parse(now))
306 ? thread
307 : {
308 ...thread,
309 hasPendingApprovals: false,
310 hasPendingUserInput: false,
311 snoozedUntil: input.snoozedUntil,
312 snoozedAt: thread.snoozedUntil === input.snoozedUntil ? (thread.snoozedAt ?? now) : now,
313 },
314 ),
315 unsnooze: optimistic.wrap(commands.unsnooze, (thread) => ({
316 ...thread,
317 snoozedUntil: null,
318 snoozedAt: null,
319 })),
320 pin: optimistic.wrap(commands.pin, (thread, input, now) => ({
321 ...thread,
322 pinnedAt: thread.pinnedAt ?? now,
323 pinOrderKey: thread.pinnedAt == null ? (input.orderKey ?? null) : thread.pinOrderKey,
324 ...(thread.settledOverride === "settled"
325 ? {
326 settledOverride: "active" as const,
327 settledAt: null,
328 unsettledAt: now,
329 }
330 : {}),
331 snoozedUntil: null,
332 snoozedAt: null,
333 })),
334 unpin: optimistic.wrap(commands.unpin, (thread) => ({
335 ...thread,
336 pinnedAt: null,
337 pinOrderKey: null,
338 })),
339 reorderPin: optimistic.wrap(commands.reorderPin, (thread, input) => ({
340 ...thread,
341 pinOrderKey: input.orderKey,
342 })),
343 reorderActive: optimistic.wrap(commands.reorderActive, (thread, input) => ({
344 ...thread,
345 activeOrderKey: input.orderKey,
346 })),
347 };