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

threadCommands.ts

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