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

threadCommands.ts

326 lines10.9 KB

1import type { ThreadId } from "@t3tools/contracts";
2import * as Crypto from "effect/Crypto";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5import { Atom } from "effect/unstable/reactivity";
6import { WS_METHODS } from "@t3tools/contracts";
7​
8import {
9 createAtomCommandScheduler,
10 createEnvironmentCommand,
11 createEnvironmentRpcCommand,
12} from "./runtime.ts";
13import {
14 type ArchiveThreadInput,
15 type CancelQueuedRunInput,
16 type CreateThreadInput,
17 type DeleteThreadInput,
18 type EditQueuedRunInput,
19 type InterruptThreadTurnInput,
20 type MarkThreadUnreadInput,
21 type ForkThreadFromRunInput,
22 type MergeThreadBackInput,
23 type PromoteQueuedRunInput,
24 type ReorderQueuedRunInput,
25 type RespondToThreadApprovalInput,
26 type RespondToThreadUserInputInput,
27 type RevertThreadCheckpointInput,
28 type SetThreadInteractionModeInput,
29 type SetThreadRuntimeModeInput,
30 type PinThreadInput,
31 type ReorderPinnedThreadInput,
32 type SettleThreadInput,
33 type SnoozeThreadInput,
34 type StartThreadTurnInput,
35 type StopThreadSessionInput,
36 type UnarchiveThreadInput,
37 type UnpinThreadInput,
38 type UnsettleThreadInput,
39 type UnsnoozeThreadInput,
40 type UpdateThreadMetadataInput,
41 type VisitThreadInput,
42 archiveThread,
43 cancelQueuedRun,
44 createThread,
45 deleteThread,
46 editQueuedRun,
47 interruptThreadTurn,
48 forkThreadFromRun,
49 markThreadUnread,
50 mergeThreadBack,
51 promoteQueuedRun,
52 reorderQueuedRun,
53 respondToThreadApproval,
54 respondToThreadUserInput,
55 revertThreadCheckpoint,
56 setThreadInteractionMode,
57 setThreadRuntimeMode,
58 pinThread,
59 reorderPinnedThread,
60 settleThread,
61 snoozeThread,
62 startThreadTurn,
63 stopThreadSession,
64 unarchiveThread,
65 unpinThread,
66 unsettleThread,
67 unsnoozeThread,
68 updateThreadMetadata,
69 visitThread,
70} from "../operations/commands.ts";
71import type { EnvironmentRegistry } from "../connection/registry.ts";
72import { EnvironmentSupervisor } from "../connection/supervisor.ts";
73import {
74 ThreadHistoryController,
75 type ThreadHistoryLoadEarlierResult,
76} from "./threadHistoryController.ts";
77​
78export type LoadEarlierThreadHistoryInput = {
79 readonly threadId: ThreadId;
80};
81​
82export type {
83 ArchiveThreadInput,
84 CancelQueuedRunInput,
85 CreateThreadInput,
86 DeleteThreadInput,
87 EditQueuedRunInput,
88 InterruptThreadTurnInput,
89 MarkThreadUnreadInput,
90 ForkThreadFromRunInput,
91 MergeThreadBackInput,
92 PromoteQueuedRunInput,
93 ReorderQueuedRunInput,
94 RespondToThreadApprovalInput,
95 RespondToThreadUserInputInput,
96 RevertThreadCheckpointInput,
97 SetThreadInteractionModeInput,
98 SetThreadRuntimeModeInput,
99 PinThreadInput,
100 ReorderPinnedThreadInput,
101 SettleThreadInput,
102 SnoozeThreadInput,
103 StartThreadTurnInput,
104 StopThreadSessionInput,
105 ThreadCommandInput,
106 UnarchiveThreadInput,
107 UnpinThreadInput,
108 UnsettleThreadInput,
109 UnsnoozeThreadInput,
110 UpdateThreadMetadataInput,
111 VisitThreadInput,
112} from "../operations/commands.ts";
113​
114export function createThreadEnvironmentAtoms<R, E>(
115 runtime: Atom.AtomRuntime<EnvironmentRegistry | Crypto.Crypto | R, E>,
116) {
117 const scheduler = createAtomCommandScheduler();
118 const concurrency = {
119 mode: "serial" as const,
120 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
121 JSON.stringify([environmentId, input.threadId]),
122 };
123 return {
124 create: createEnvironmentCommand(runtime, {
125 label: "environment-data:commands:thread:create",
126 execute: (input: CreateThreadInput) => createThread(input),
127 scheduler,
128 concurrency,
129 }),
130 delete: createEnvironmentCommand(runtime, {
131 label: "environment-data:commands:thread:delete",
132 execute: (input: DeleteThreadInput) => deleteThread(input),
133 scheduler,
134 concurrency,
135 }),
136 archive: createEnvironmentCommand(runtime, {
137 label: "environment-data:commands:thread:archive",
138 execute: (input: ArchiveThreadInput) => archiveThread(input),
139 scheduler,
140 concurrency,
141 }),
142 unarchive: createEnvironmentCommand(runtime, {
143 label: "environment-data:commands:thread:unarchive",
144 execute: (input: UnarchiveThreadInput) => unarchiveThread(input),
145 scheduler,
146 concurrency,
147 }),
148 settle: createEnvironmentCommand(runtime, {
149 label: "environment-data:commands:thread:settle",
150 execute: (input: SettleThreadInput) => settleThread(input),
151 scheduler,
152 concurrency,
153 }),
154 unsettle: createEnvironmentCommand(runtime, {
155 label: "environment-data:commands:thread:unsettle",
156 execute: (input: UnsettleThreadInput) => unsettleThread(input),
157 scheduler,
158 concurrency,
159 }),
160 snooze: createEnvironmentCommand(runtime, {
161 label: "environment-data:commands:thread:snooze",
162 execute: (input: SnoozeThreadInput) => snoozeThread(input),
163 scheduler,
164 concurrency,
165 }),
166 unsnooze: createEnvironmentCommand(runtime, {
167 label: "environment-data:commands:thread:unsnooze",
168 execute: (input: UnsnoozeThreadInput) => unsnoozeThread(input),
169 scheduler,
170 concurrency,
171 }),
172 pin: createEnvironmentCommand(runtime, {
173 label: "environment-data:commands:thread:pin",
174 execute: (input: PinThreadInput) => pinThread(input),
175 scheduler,
176 concurrency,
177 }),
178 unpin: createEnvironmentCommand(runtime, {
179 label: "environment-data:commands:thread:unpin",
180 execute: (input: UnpinThreadInput) => unpinThread(input),
181 scheduler,
182 concurrency,
183 }),
184 reorderPin: createEnvironmentCommand(runtime, {
185 label: "environment-data:commands:thread:reorder-pin",
186 execute: (input: ReorderPinnedThreadInput) => reorderPinnedThread(input),
187 scheduler,
188 concurrency,
189 }),
190 visit: createEnvironmentCommand(runtime, {
191 label: "environment-data:commands:thread:visit",
192 execute: (input: VisitThreadInput) => visitThread(input),
193 scheduler,
194 concurrency,
195 }),
196 markUnread: createEnvironmentCommand(runtime, {
197 label: "environment-data:commands:thread:mark-unread",
198 execute: (input: MarkThreadUnreadInput) => markThreadUnread(input),
199 scheduler,
200 concurrency,
201 }),
202 updateMetadata: createEnvironmentCommand(runtime, {
203 label: "environment-data:commands:thread:update-metadata",
204 execute: (input: UpdateThreadMetadataInput) => updateThreadMetadata(input),
205 scheduler,
206 concurrency,
207 }),
208 setRuntimeMode: createEnvironmentCommand(runtime, {
209 label: "environment-data:commands:thread:set-runtime-mode",
210 execute: (input: SetThreadRuntimeModeInput) => setThreadRuntimeMode(input),
211 scheduler,
212 concurrency,
213 }),
214 setInteractionMode: createEnvironmentCommand(runtime, {
215 label: "environment-data:commands:thread:set-interaction-mode",
216 execute: (input: SetThreadInteractionModeInput) => setThreadInteractionMode(input),
217 scheduler,
218 concurrency,
219 }),
220 startTurn: createEnvironmentCommand(runtime, {
221 label: "environment-data:commands:thread:start-turn",
222 execute: (input: StartThreadTurnInput) => startThreadTurn(input),
223 scheduler,
224 concurrency,
225 }),
226 interruptTurn: createEnvironmentCommand(runtime, {
227 label: "environment-data:commands:thread:interrupt-turn",
228 execute: (input: InterruptThreadTurnInput) => interruptThreadTurn(input),
229 scheduler,
230 concurrency,
231 }),
232 respondToApproval: createEnvironmentCommand(runtime, {
233 label: "environment-data:commands:thread:respond-to-approval",
234 execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input),
235 scheduler,
236 concurrency,
237 }),
238 respondToUserInput: createEnvironmentCommand(runtime, {
239 label: "environment-data:commands:thread:respond-to-user-input",
240 execute: (input: RespondToThreadUserInputInput) => respondToThreadUserInput(input),
241 scheduler,
242 concurrency,
243 }),
244 revertCheckpoint: createEnvironmentCommand(runtime, {
245 label: "environment-data:commands:thread:revert-checkpoint",
246 execute: (input: RevertThreadCheckpointInput) => revertThreadCheckpoint(input),
247 scheduler,
248 concurrency,
249 }),
250 stopSession: createEnvironmentCommand(runtime, {
251 label: "environment-data:commands:thread:stop-session",
252 execute: (input: StopThreadSessionInput) => stopThreadSession(input),
253 scheduler,
254 concurrency,
255 }),
256 forkFromRun: createEnvironmentCommand(runtime, {
257 label: "environment-data:commands:thread:fork-from-run",
258 execute: (input: ForkThreadFromRunInput) => forkThreadFromRun(input),
259 scheduler,
260 concurrency: {
261 mode: "serial",
262 key: ({ environmentId, input }) => JSON.stringify([environmentId, input.sourceThreadId]),
263 },
264 }),
265 mergeBack: createEnvironmentCommand(runtime, {
266 label: "environment-data:commands:thread:merge-back",
267 execute: (input: MergeThreadBackInput) => mergeThreadBack(input),
268 scheduler,
269 concurrency: {
270 mode: "serial",
271 key: ({ environmentId, input }) =>
272 JSON.stringify([environmentId, input.sourceThreadId, input.targetThreadId]),
273 },
274 }),
275 reorderQueuedRun: createEnvironmentCommand(runtime, {
276 label: "environment-data:commands:thread:reorder-queued-run",
277 execute: (input: ReorderQueuedRunInput) => reorderQueuedRun(input),
278 scheduler,
279 concurrency,
280 }),
281 promoteQueuedRun: createEnvironmentCommand(runtime, {
282 label: "environment-data:commands:thread:promote-queued-run",
283 execute: (input: PromoteQueuedRunInput) => promoteQueuedRun(input),
284 scheduler,
285 concurrency,
286 }),
287 cancelQueuedRun: createEnvironmentCommand(runtime, {
288 label: "environment-data:commands:thread:cancel-queued-run",
289 execute: (input: CancelQueuedRunInput) => cancelQueuedRun(input),
290 scheduler,
291 concurrency,
292 }),
293 editQueuedRun: createEnvironmentCommand(runtime, {
294 label: "environment-data:commands:thread:edit-queued-run",
295 execute: (input: EditQueuedRunInput) => editQueuedRun(input),
296 scheduler,
297 concurrency,
298 }),
299 loadEarlierHistory: createEnvironmentCommand(runtime, {
300 label: "environment-data:commands:thread:load-earlier-history",
301 execute: (input: LoadEarlierThreadHistoryInput) =>
302 Effect.gen(function* () {
303 const supervisor = yield* EnvironmentSupervisor;
304 const controller = yield* Effect.serviceOption(ThreadHistoryController);
305 if (Option.isNone(controller)) {
306 return { _tag: "noop" } satisfies ThreadHistoryLoadEarlierResult;
307 }
308 return yield* controller.value.loadEarlier(
309 supervisor.target.environmentId,
310 input.threadId,
311 );
312 }),
313 scheduler,
314 concurrency: {
315 mode: "serial",
316 key: ({ environmentId, input }) => JSON.stringify([environmentId, input.threadId]),
317 },
318 }),
319 uploadFeedback: createEnvironmentRpcCommand(runtime, {
320 label: "environment-data:commands:thread:upload-feedback",
321 tag: WS_METHODS.providerUploadFeedback,
322 scheduler,
323 concurrency,
324 }),
325 };