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

threadCommands.ts

221 lines7.5 KB

1import * as Crypto from "effect/Crypto";
2import { Atom } from "effect/unstable/reactivity";
3import { WS_METHODS } from "@t3tools/contracts";
4​
5import {
6 createAtomCommandScheduler,
7 createEnvironmentCommand,
8 createEnvironmentRpcCommand,
9} from "./runtime.ts";
10import {
11 type ArchiveThreadInput,
12 type CreateThreadInput,
13 type DeleteThreadInput,
14 type InterruptThreadTurnInput,
15 type RespondToThreadApprovalInput,
16 type RespondToThreadUserInputInput,
17 type RevertThreadCheckpointInput,
18 type SetThreadInteractionModeInput,
19 type SetThreadRuntimeModeInput,
20 type PinThreadInput,
21 type ReorderPinnedThreadInput,
22 type SettleThreadInput,
23 type SnoozeThreadInput,
24 type StartThreadTurnInput,
25 type StopThreadSessionInput,
26 type UnarchiveThreadInput,
27 type UnpinThreadInput,
28 type UnsettleThreadInput,
29 type UnsnoozeThreadInput,
30 type UpdateThreadMetadataInput,
31 archiveThread,
32 createThread,
33 deleteThread,
34 interruptThreadTurn,
35 respondToThreadApproval,
36 respondToThreadUserInput,
37 revertThreadCheckpoint,
38 setThreadInteractionMode,
39 setThreadRuntimeMode,
40 pinThread,
41 reorderPinnedThread,
42 settleThread,
43 snoozeThread,
44 startThreadTurn,
45 stopThreadSession,
46 unarchiveThread,
47 unpinThread,
48 unsettleThread,
49 unsnoozeThread,
50 updateThreadMetadata,
51} from "../operations/commands.ts";
52import type { EnvironmentRegistry } from "../connection/registry.ts";
53​
54export type {
55 ArchiveThreadInput,
56 CreateThreadInput,
57 DeleteThreadInput,
58 InterruptThreadTurnInput,
59 RespondToThreadApprovalInput,
60 RespondToThreadUserInputInput,
61 RevertThreadCheckpointInput,
62 SetThreadInteractionModeInput,
63 SetThreadRuntimeModeInput,
64 PinThreadInput,
65 ReorderPinnedThreadInput,
66 SettleThreadInput,
67 SnoozeThreadInput,
68 StartThreadTurnInput,
69 StopThreadSessionInput,
70 UnarchiveThreadInput,
71 UnpinThreadInput,
72 UnsettleThreadInput,
73 UnsnoozeThreadInput,
74 UpdateThreadMetadataInput,
75} from "../operations/commands.ts";
76​
77export function createThreadEnvironmentAtoms<R, E>(
78 runtime: Atom.AtomRuntime<EnvironmentRegistry | Crypto.Crypto | R, E>,
79) {
80 const scheduler = createAtomCommandScheduler();
81 const concurrency = {
82 mode: "serial" as const,
83 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
84 JSON.stringify([environmentId, input.threadId]),
85 };
86 return {
87 create: createEnvironmentCommand(runtime, {
88 label: "environment-data:commands:thread:create",
89 execute: (input: CreateThreadInput) => createThread(input),
90 scheduler,
91 concurrency,
92 }),
93 delete: createEnvironmentCommand(runtime, {
94 label: "environment-data:commands:thread:delete",
95 execute: (input: DeleteThreadInput) => deleteThread(input),
96 scheduler,
97 concurrency,
98 }),
99 archive: createEnvironmentCommand(runtime, {
100 label: "environment-data:commands:thread:archive",
101 execute: (input: ArchiveThreadInput) => archiveThread(input),
102 scheduler,
103 concurrency,
104 }),
105 unarchive: createEnvironmentCommand(runtime, {
106 label: "environment-data:commands:thread:unarchive",
107 execute: (input: UnarchiveThreadInput) => unarchiveThread(input),
108 scheduler,
109 concurrency,
110 }),
111 settle: createEnvironmentCommand(runtime, {
112 label: "environment-data:commands:thread:settle",
113 execute: (input: SettleThreadInput) => settleThread(input),
114 scheduler,
115 concurrency,
116 }),
117 unsettle: createEnvironmentCommand(runtime, {
118 label: "environment-data:commands:thread:unsettle",
119 execute: (input: UnsettleThreadInput) => unsettleThread(input),
120 scheduler,
121 concurrency,
122 }),
123 snooze: createEnvironmentCommand(runtime, {
124 label: "environment-data:commands:thread:snooze",
125 execute: (input: SnoozeThreadInput) => snoozeThread(input),
126 scheduler,
127 concurrency,
128 }),
129 unsnooze: createEnvironmentCommand(runtime, {
130 label: "environment-data:commands:thread:unsnooze",
131 execute: (input: UnsnoozeThreadInput) => unsnoozeThread(input),
132 scheduler,
133 concurrency,
134 }),
135 pin: createEnvironmentCommand(runtime, {
136 label: "environment-data:commands:thread:pin",
137 execute: (input: PinThreadInput) => pinThread(input),
138 scheduler,
139 concurrency,
140 }),
141 unpin: createEnvironmentCommand(runtime, {
142 label: "environment-data:commands:thread:unpin",
143 execute: (input: UnpinThreadInput) => unpinThread(input),
144 scheduler,
145 concurrency,
146 }),
147 reorderPin: createEnvironmentCommand(runtime, {
148 label: "environment-data:commands:thread:reorder-pin",
149 execute: (input: ReorderPinnedThreadInput) => reorderPinnedThread(input),
150 scheduler,
151 concurrency,
152 }),
153 updateMetadata: createEnvironmentCommand(runtime, {
154 label: "environment-data:commands:thread:update-metadata",
155 execute: (input: UpdateThreadMetadataInput) => updateThreadMetadata(input),
156 scheduler,
157 concurrency,
158 }),
159 setRuntimeMode: createEnvironmentCommand(runtime, {
160 label: "environment-data:commands:thread:set-runtime-mode",
161 execute: (input: SetThreadRuntimeModeInput) => setThreadRuntimeMode(input),
162 scheduler,
163 concurrency,
164 }),
165 setInteractionMode: createEnvironmentCommand(runtime, {
166 label: "environment-data:commands:thread:set-interaction-mode",
167 execute: (input: SetThreadInteractionModeInput) => setThreadInteractionMode(input),
168 scheduler,
169 concurrency,
170 }),
171 startTurn: createEnvironmentCommand(runtime, {
172 label: "environment-data:commands:thread:start-turn",
173 execute: (input: StartThreadTurnInput) => startThreadTurn(input),
174 scheduler,
175 concurrency,
176 }),
177 interruptTurn: createEnvironmentCommand(runtime, {
178 label: "environment-data:commands:thread:interrupt-turn",
179 execute: (input: InterruptThreadTurnInput) => interruptThreadTurn(input),
180 scheduler,
181 // Stop is pressed repeatedly while a turn winds down (especially on
182 // high-latency remote links). singleFlight shares the in-flight request
183 // per thread instead of serializing every click behind it (#8618).
184 concurrency: {
185 mode: "singleFlight" as const,
186 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
187 JSON.stringify([environmentId, input.threadId]),
188 },
189 }),
190 respondToApproval: createEnvironmentCommand(runtime, {
191 label: "environment-data:commands:thread:respond-to-approval",
192 execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input),
193 scheduler,
194 concurrency,
195 }),
196 respondToUserInput: createEnvironmentCommand(runtime, {
197 label: "environment-data:commands:thread:respond-to-user-input",
198 execute: (input: RespondToThreadUserInputInput) => respondToThreadUserInput(input),
199 scheduler,
200 concurrency,
201 }),
202 revertCheckpoint: createEnvironmentCommand(runtime, {
203 label: "environment-data:commands:thread:revert-checkpoint",
204 execute: (input: RevertThreadCheckpointInput) => revertThreadCheckpoint(input),
205 scheduler,
206 concurrency,
207 }),
208 stopSession: createEnvironmentCommand(runtime, {
209 label: "environment-data:commands:thread:stop-session",
210 execute: (input: StopThreadSessionInput) => stopThreadSession(input),
211 scheduler,
212 concurrency,
213 }),
214 uploadFeedback: createEnvironmentRpcCommand(runtime, {
215 label: "environment-data:commands:thread:upload-feedback",
216 tag: WS_METHODS.providerUploadFeedback,
217 scheduler,
218 concurrency,
219 }),
220 };