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

threadCommands.ts

203 lines6.7 KB

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