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

threadCommands.ts

469 lines15.6 KB

1import type { ModelSelection, ThreadId } from "@t3tools/contracts";
2import * as SecureRandom from "effect/crypto/SecureRandom";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5import { Atom } from "effect/reactivity";
6import {
7 WS_METHODS,
8 type EnvironmentId,
9 type OrchestrationV2ShellSnapshot,
10} from "@t3tools/contracts";
11​
12import { createOptimisticThreadLifecycle } from "./threadLifecycle.ts";
13import * as DateTime from "effect/DateTime";
14​
15import {
16 createAtomCommandScheduler,
17 createEnvironmentCommand,
18 createEnvironmentRpcCommand,
19} from "./runtime.ts";
20import {
21 type InterruptThreadTurnInput,
22 type UpdateThreadMetadataInput,
23 archiveThread,
24 closeGuestShare,
25 resolveGuestMessage,
26 cancelQueuedRun,
27 createThread,
28 deleteThread,
29 editQueuedRun,
30 interruptThreadTurn,
31 forkSideChat,
32 forkThreadFromRun,
33 markThreadUnread,
34 mergeThreadBack,
35 promoteQueuedRun,
36 resumeThreadQueue,
37 retryWorkspacePreparation,
38 linkThreadPullRequest,
39 respondToThreadApproval,
40 respondToThreadUserInput,
41 dismissThreadUserInput,
42 revertThreadCheckpoint,
43 setThreadInteractionMode,
44 setThreadRuntimeMode,
45 pinThread,
46 reorderPinnedThread,
47 reorderActiveThread,
48 setThreadAutoSettle,
49 settleThread,
50 snoozeThread,
51 startThreadTurn,
52 stopThreadSession,
53 unarchiveThread,
54 unlinkThreadPullRequest,
55 unpinThread,
56 unsettleThread,
57 unsnoozeThread,
58 updateThreadMetadata,
59 visitThread,
60 watchThreadPullRequest,
61} from "../operations/commands.ts";
62import type { EnvironmentRegistry } from "../connection/registry.ts";
63import * as EnvironmentSupervisor from "../connection/supervisor.ts";
64import * as ThreadHistoryController from "./threadHistoryController.ts";
65​
66export type LoadEarlierThreadHistoryInput = {
67 readonly threadId: ThreadId;
68 readonly throughEntryId?: string;
69 readonly workAnchorId?: string;
70};
71​
72export function createThreadEnvironmentAtoms<R, E>(
73 runtime: Atom.AtomRuntime<EnvironmentRegistry | SecureRandom.SecureRandom | R, E>,
74 snapshotAtom: (environmentId: EnvironmentId) => Atom.Atom<OrchestrationV2ShellSnapshot | null>,
75) {
76 const scheduler = createAtomCommandScheduler();
77 const concurrency = {
78 mode: "serial" as const,
79 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
80 JSON.stringify([environmentId, input.threadId]),
81 };
82 const loadEarlierHistory = Effect.fn("ThreadCommands.loadEarlierHistory")(function* (
83 input: LoadEarlierThreadHistoryInput,
84 ) {
85 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
86 const controller = yield* Effect.serviceOption(ThreadHistoryController.ThreadHistoryController);
87 if (Option.isNone(controller)) {
88 return {
89 _tag: "noop",
90 } satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult;
91 }
92 return yield* controller.value.loadEarlier(
93 supervisor.target.environmentId,
94 input.threadId,
95 input.throughEntryId,
96 input.workAnchorId,
97 );
98 });
99​
100 // Runs whether or not the server accepted the interrupt, so a stale cached item still
101 // settles when Stop had nothing to interrupt; the interrupt's own failure still surfaces.
102 // The repair is detached: its lookups must not hold the thread's serial command key, and
103 // closing the thread cancels the repair, not the Stop that triggered it.
104 const interruptTurn = Effect.fn("ThreadCommands.interruptTurn")(function* (
105 input: InterruptThreadTurnInput,
106 ) {
107 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
108 const controller = yield* Effect.serviceOption(ThreadHistoryController.ThreadHistoryController);
109 return yield* interruptThreadTurn(input).pipe(
110 Effect.ensuring(
111 Option.match(controller, {
112 onNone: () => Effect.void,
113 onSome: (value) =>
114 value
115 .reconcilePendingWork(supervisor.target.environmentId, input.threadId)
116 .pipe(Effect.forkDetach, Effect.asVoid),
117 }),
118 ),
119 );
120 });
121​
122 const commands = {
123 create: createEnvironmentCommand(runtime, {
124 label: "environment-data:commands:thread:create",
125 execute: createThread,
126 scheduler,
127 concurrency,
128 }),
129 delete: createEnvironmentCommand(runtime, {
130 label: "environment-data:commands:thread:delete",
131 execute: deleteThread,
132 scheduler,
133 concurrency,
134 }),
135 closeGuestShare: createEnvironmentCommand(runtime, {
136 label: "environment-data:commands:thread:close-guest-share",
137 execute: closeGuestShare,
138 scheduler,
139 concurrency,
140 }),
141 resolveGuestMessage: createEnvironmentCommand(runtime, {
142 label: "environment-data:commands:thread:resolve-guest-message",
143 execute: resolveGuestMessage,
144 scheduler,
145 concurrency,
146 }),
147 archive: createEnvironmentCommand(runtime, {
148 label: "environment-data:commands:thread:archive",
149 execute: archiveThread,
150 scheduler,
151 concurrency,
152 }),
153 unarchive: createEnvironmentCommand(runtime, {
154 label: "environment-data:commands:thread:unarchive",
155 execute: unarchiveThread,
156 scheduler,
157 concurrency,
158 }),
159 settle: createEnvironmentCommand(runtime, {
160 label: "environment-data:commands:thread:settle",
161 execute: settleThread,
162 scheduler,
163 concurrency,
164 }),
165 unsettle: createEnvironmentCommand(runtime, {
166 label: "environment-data:commands:thread:unsettle",
167 execute: unsettleThread,
168 scheduler,
169 concurrency,
170 }),
171 snooze: createEnvironmentCommand(runtime, {
172 label: "environment-data:commands:thread:snooze",
173 execute: snoozeThread,
174 scheduler,
175 concurrency,
176 }),
177 unsnooze: createEnvironmentCommand(runtime, {
178 label: "environment-data:commands:thread:unsnooze",
179 execute: unsnoozeThread,
180 scheduler,
181 concurrency,
182 }),
183 pin: createEnvironmentCommand(runtime, {
184 label: "environment-data:commands:thread:pin",
185 execute: pinThread,
186 scheduler,
187 concurrency,
188 }),
189 unpin: createEnvironmentCommand(runtime, {
190 label: "environment-data:commands:thread:unpin",
191 execute: unpinThread,
192 scheduler,
193 concurrency,
194 }),
195 reorderPin: createEnvironmentCommand(runtime, {
196 label: "environment-data:commands:thread:reorder-pin",
197 execute: reorderPinnedThread,
198 scheduler,
199 concurrency,
200 }),
201 setAutoSettle: createEnvironmentCommand(runtime, {
202 label: "environment-data:commands:thread:set-auto-settle",
203 execute: setThreadAutoSettle,
204 scheduler,
205 concurrency,
206 }),
207 reorderActive: createEnvironmentCommand(runtime, {
208 label: "environment-data:commands:thread:reorder-active",
209 execute: reorderActiveThread,
210 scheduler,
211 concurrency,
212 }),
213 visit: createEnvironmentCommand(runtime, {
214 label: "environment-data:commands:thread:visit",
215 execute: visitThread,
216 scheduler,
217 concurrency,
218 }),
219 markUnread: createEnvironmentCommand(runtime, {
220 label: "environment-data:commands:thread:mark-unread",
221 execute: markThreadUnread,
222 scheduler,
223 concurrency,
224 }),
225 updateMetadata: createEnvironmentCommand(runtime, {
226 label: "environment-data:commands:thread:update-metadata",
227 execute: updateThreadMetadata,
228 scheduler,
229 concurrency,
230 }),
231 linkPullRequest: createEnvironmentCommand(runtime, {
232 label: "environment-data:commands:thread:link-pull-request",
233 execute: linkThreadPullRequest,
234 scheduler,
235 concurrency,
236 }),
237 unlinkPullRequest: createEnvironmentCommand(runtime, {
238 label: "environment-data:commands:thread:unlink-pull-request",
239 execute: unlinkThreadPullRequest,
240 scheduler,
241 concurrency,
242 }),
243 watchPullRequest: createEnvironmentCommand(runtime, {
244 label: "environment-data:commands:thread:watch-pull-request",
245 execute: watchThreadPullRequest,
246 scheduler,
247 concurrency,
248 }),
249 setModelSelection: createEnvironmentCommand(runtime, {
250 label: "environment-data:commands:thread:set-model-selection",
251 execute: (input: UpdateThreadMetadataInput & { readonly modelSelection: ModelSelection }) =>
252 updateThreadMetadata(input),
253 scheduler,
254 concurrency,
255 }),
256 setRuntimeMode: createEnvironmentCommand(runtime, {
257 label: "environment-data:commands:thread:set-runtime-mode",
258 execute: setThreadRuntimeMode,
259 scheduler,
260 concurrency,
261 }),
262 setInteractionMode: createEnvironmentCommand(runtime, {
263 label: "environment-data:commands:thread:set-interaction-mode",
264 execute: setThreadInteractionMode,
265 scheduler,
266 concurrency,
267 }),
268 startTurn: createEnvironmentCommand(runtime, {
269 label: "environment-data:commands:thread:start-turn",
270 execute: startThreadTurn,
271 scheduler,
272 concurrency,
273 }),
274 interruptTurn: createEnvironmentCommand(runtime, {
275 label: "environment-data:commands:thread:interrupt-turn",
276 execute: interruptTurn,
277 scheduler,
278 concurrency,
279 }),
280 respondToApproval: createEnvironmentCommand(runtime, {
281 label: "environment-data:commands:thread:respond-to-approval",
282 execute: respondToThreadApproval,
283 scheduler,
284 concurrency,
285 }),
286 respondToUserInput: createEnvironmentCommand(runtime, {
287 label: "environment-data:commands:thread:respond-to-user-input",
288 execute: respondToThreadUserInput,
289 scheduler,
290 concurrency,
291 }),
292 dismissUserInput: createEnvironmentCommand(runtime, {
293 label: "environment-data:commands:thread:dismiss-user-input",
294 execute: dismissThreadUserInput,
295 scheduler,
296 concurrency,
297 }),
298 revertCheckpoint: createEnvironmentCommand(runtime, {
299 label: "environment-data:commands:thread:revert-checkpoint",
300 execute: revertThreadCheckpoint,
301 scheduler,
302 concurrency,
303 }),
304 stopSession: createEnvironmentCommand(runtime, {
305 label: "environment-data:commands:thread:stop-session",
306 execute: stopThreadSession,
307 scheduler,
308 concurrency,
309 }),
310 forkFromRun: createEnvironmentCommand(runtime, {
311 label: "environment-data:commands:thread:fork-from-run",
312 execute: forkThreadFromRun,
313 scheduler,
314 concurrency: {
315 mode: "serial",
316 key: ({ environmentId, input }) => JSON.stringify([environmentId, input.sourceThreadId]),
317 },
318 }),
319 forkSideChat: createEnvironmentCommand(runtime, {
320 label: "environment-data:commands:thread:fork-side-chat",
321 execute: forkSideChat,
322 scheduler,
323 concurrency: {
324 mode: "serial",
325 key: ({ environmentId, input }) => JSON.stringify([environmentId, input.sourceThreadId]),
326 },
327 }),
328 mergeBack: createEnvironmentCommand(runtime, {
329 label: "environment-data:commands:thread:merge-back",
330 execute: mergeThreadBack,
331 scheduler,
332 concurrency: {
333 mode: "serial",
334 key: ({ environmentId, input }) =>
335 JSON.stringify([environmentId, input.sourceThreadId, input.targetThreadId]),
336 },
337 }),
338 resumeThreadQueue: createEnvironmentCommand(runtime, {
339 label: "environment-data:commands:thread:resume-queue",
340 execute: resumeThreadQueue,
341 scheduler,
342 concurrency,
343 }),
344 promoteQueuedRun: createEnvironmentCommand(runtime, {
345 label: "environment-data:commands:thread:promote-queued-run",
346 execute: promoteQueuedRun,
347 scheduler,
348 concurrency,
349 }),
350 cancelQueuedRun: createEnvironmentCommand(runtime, {
351 label: "environment-data:commands:thread:cancel-queued-run",
352 execute: cancelQueuedRun,
353 scheduler,
354 concurrency,
355 }),
356 retryWorkspacePreparation: createEnvironmentCommand(runtime, {
357 label: "environment-data:commands:thread:retry-workspace-preparation",
358 execute: retryWorkspacePreparation,
359 scheduler,
360 concurrency,
361 }),
362 editQueuedRun: createEnvironmentCommand(runtime, {
363 label: "environment-data:commands:thread:edit-queued-run",
364 execute: editQueuedRun,
365 scheduler,
366 concurrency,
367 }),
368 loadEarlierHistory: createEnvironmentCommand(runtime, {
369 label: "environment-data:commands:thread:load-earlier-history",
370 execute: loadEarlierHistory,
371 scheduler,
372 concurrency: {
373 mode: "serial",
374 key: ({ environmentId, input }) =>
375 `history:${JSON.stringify([environmentId, input.threadId])}`,
376 },
377 }),
378 uploadFeedback: createEnvironmentRpcCommand(runtime, {
379 label: "environment-data:commands:thread:upload-feedback",
380 tag: WS_METHODS.providerUploadFeedback,
381 scheduler,
382 concurrency,
383 }),
384 };
385 const optimistic = createOptimisticThreadLifecycle(snapshotAtom);
386 return {
387 ...commands,
388 snapshotAtom: optimistic.snapshotAtom,
389 pendingThreadIdsAtom: optimistic.pendingThreadIdsAtom,
390 settle: optimistic.wrap(commands.settle, (thread, _input, now, accepted) =>
391 !accepted &&
392 (thread.pendingRuntimeRequest !== null ||
393 ["preparing", "queued", "starting", "running", "waiting"].includes(thread.status))
394 ? thread
395 : {
396 ...thread,
397 pendingRuntimeRequest: null,
398 settledOverride: "settled",
399 settledAt: thread.settledOverride === "settled" ? (thread.settledAt ?? now) : now,
400 unsettledAt: null,
401 activeOrderKey: null,
402 pinnedAt: null,
403 pinOrderKey: null,
404 snoozedAt: null,
405 snoozedUntil: null,
406 },
407 ),
408 unsettle: optimistic.wrap(commands.unsettle, (thread, input, now) => ({
409 ...thread,
410 settledOverride: input.reason === "user" ? "active" : null,
411 settledAt: null,
412 unsettledAt: thread.settledOverride === "active" ? (thread.unsettledAt ?? null) : now,
413 })),
414 snooze: optimistic.wrap(commands.snooze, (thread, input, now, accepted) =>
415 (!accepted &&
416 ((thread.pendingRuntimeRequest !== null &&
417 thread.pendingRuntimeRequest.kind !== "user_input") ||
418 ["preparing", "queued", "starting"].includes(thread.status))) ||
419 !(Date.parse(input.snoozedUntil) > DateTime.toEpochMillis(now))
420 ? thread
421 : {
422 ...thread,
423 snoozedUntil: DateTime.makeUnsafe(input.snoozedUntil),
424 snoozedAt: now,
425 },
426 ),
427 unsnooze: optimistic.wrap(commands.unsnooze, (thread) => ({
428 ...thread,
429 snoozedUntil: null,
430 snoozedAt: null,
431 })),
432 setAutoSettle: optimistic.wrap(commands.setAutoSettle, (thread, input, now) => ({
433 ...thread,
434 autoSettleDisabledAt: input.enabled ? null : (thread.autoSettleDisabledAt ?? now),
435 })),
436 pin: optimistic.wrap(commands.pin, (thread, input, now) => ({
437 ...thread,
438 pinnedAt: thread.pinnedAt ?? now,
439 pinOrderKey: thread.pinnedAt == null ? (input.orderKey ?? null) : thread.pinOrderKey,
440 ...(thread.settledOverride === "settled"
441 ? {
442 settledOverride: "active" as const,
443 settledAt: null,
444 unsettledAt: now,
445 }
446 : {}),
447 snoozedUntil: null,
448 snoozedAt: null,
449 })),
450 unpin: optimistic.wrap(commands.unpin, (thread) => ({
451 ...thread,
452 pinnedAt: null,
453 pinOrderKey: null,
454 })),
455 reorderPin: optimistic.wrap(commands.reorderPin, (thread, input) => ({
456 ...thread,
457 pinOrderKey: input.orderKey,
458 })),
459 reorderActive: optimistic.wrap(commands.reorderActive, (thread, input) => ({
460 ...thread,
461 activeOrderKey: input.orderKey,
462 })),
463 // Shown at once, so a composer that releases its accepted pick keeps showing it.
464 setModelSelection: optimistic.wrap(commands.setModelSelection, (thread, input) => ({
465 ...thread,
466 modelSelection: input.modelSelection,
467 })),
468 };