Open History to see the last commit
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 ForkThreadFromRunInput,
11 type MergeThreadBackInput,
12 type PromoteQueuedRunInput,
13 type ReorderQueuedRunInput,
14 type RespondToThreadApprovalInput,
15 type RespondToThreadUserInputInput,
16 type RevertThreadCheckpointInput,
17 type SetThreadInteractionModeInput,
18 type SetThreadRuntimeModeInput,
19 type StartThreadTurnInput,
20 type StopThreadSessionInput,
21 type UnarchiveThreadInput,
22 type UpdateThreadMetadataInput,
23 archiveThread,
24 createThread,
25 deleteThread,
26 interruptThreadTurn,
27 forkThreadFromRun,
28 mergeThreadBack,
29 promoteQueuedRun,
30 reorderQueuedRun,
31 respondToThreadApproval,
32 respondToThreadUserInput,
33 revertThreadCheckpoint,
34 setThreadInteractionMode,
35 setThreadRuntimeMode,
36 startThreadTurn,
37 stopThreadSession,
38 unarchiveThread,
39 updateThreadMetadata,
40} from "../operations/commands.ts";
41import type { EnvironmentRegistry } from "../connection/registry.ts";
42
43export type {
44 ArchiveThreadInput,
45 CreateThreadInput,
46 DeleteThreadInput,
47 InterruptThreadTurnInput,
48 ForkThreadFromRunInput,
49 MergeThreadBackInput,
50 PromoteQueuedRunInput,
51 ReorderQueuedRunInput,
52 RespondToThreadApprovalInput,
53 RespondToThreadUserInputInput,
54 RevertThreadCheckpointInput,
55 SetThreadInteractionModeInput,
56 SetThreadRuntimeModeInput,
57 StartThreadTurnInput,
58 StopThreadSessionInput,
59 ThreadCommandInput,
60 UnarchiveThreadInput,
61 UpdateThreadMetadataInput,
62} from "../operations/commands.ts";
63
64export function createThreadEnvironmentAtoms<R, E>(
65 runtime: Atom.AtomRuntime<EnvironmentRegistry | Crypto.Crypto | R, E>,
66) {
67 const scheduler = createAtomCommandScheduler();
68 const concurrency = {
69 mode: "serial" as const,
70 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
71 JSON.stringify([environmentId, input.threadId]),
72 };
73 return {
74 create: createEnvironmentCommand(runtime, {
75 label: "environment-data:commands:thread:create",
76 execute: (input: CreateThreadInput) => createThread(input),
77 scheduler,
78 concurrency,
79 }),
80 delete: createEnvironmentCommand(runtime, {
81 label: "environment-data:commands:thread:delete",
82 execute: (input: DeleteThreadInput) => deleteThread(input),
83 scheduler,
84 concurrency,
85 }),
86 archive: createEnvironmentCommand(runtime, {
87 label: "environment-data:commands:thread:archive",
88 execute: (input: ArchiveThreadInput) => archiveThread(input),
89 scheduler,
90 concurrency,
91 }),
92 unarchive: createEnvironmentCommand(runtime, {
93 label: "environment-data:commands:thread:unarchive",
94 execute: (input: UnarchiveThreadInput) => unarchiveThread(input),
95 scheduler,
96 concurrency,
97 }),
98 updateMetadata: createEnvironmentCommand(runtime, {
99 label: "environment-data:commands:thread:update-metadata",
100 execute: (input: UpdateThreadMetadataInput) => updateThreadMetadata(input),
101 scheduler,
102 concurrency,
103 }),
104 setRuntimeMode: createEnvironmentCommand(runtime, {
105 label: "environment-data:commands:thread:set-runtime-mode",
106 execute: (input: SetThreadRuntimeModeInput) => setThreadRuntimeMode(input),
107 scheduler,
108 concurrency,
109 }),
110 setInteractionMode: createEnvironmentCommand(runtime, {
111 label: "environment-data:commands:thread:set-interaction-mode",
112 execute: (input: SetThreadInteractionModeInput) => setThreadInteractionMode(input),
113 scheduler,
114 concurrency,
115 }),
116 startTurn: createEnvironmentCommand(runtime, {
117 label: "environment-data:commands:thread:start-turn",
118 execute: (input: StartThreadTurnInput) => startThreadTurn(input),
119 scheduler,
120 concurrency,
121 }),
122 interruptTurn: createEnvironmentCommand(runtime, {
123 label: "environment-data:commands:thread:interrupt-turn",
124 execute: (input: InterruptThreadTurnInput) => interruptThreadTurn(input),
125 scheduler,
126 concurrency,
127 }),
128 respondToApproval: createEnvironmentCommand(runtime, {
129 label: "environment-data:commands:thread:respond-to-approval",
130 execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input),
131 scheduler,
132 concurrency,
133 }),
134 respondToUserInput: createEnvironmentCommand(runtime, {
135 label: "environment-data:commands:thread:respond-to-user-input",
136 execute: (input: RespondToThreadUserInputInput) => respondToThreadUserInput(input),
137 scheduler,
138 concurrency,
139 }),
140 revertCheckpoint: createEnvironmentCommand(runtime, {
141 label: "environment-data:commands:thread:revert-checkpoint",
142 execute: (input: RevertThreadCheckpointInput) => revertThreadCheckpoint(input),
143 scheduler,
144 concurrency,
145 }),
146 stopSession: createEnvironmentCommand(runtime, {
147 label: "environment-data:commands:thread:stop-session",
148 execute: (input: StopThreadSessionInput) => stopThreadSession(input),
149 scheduler,
150 concurrency,
151 }),
152 forkFromRun: createEnvironmentCommand(runtime, {
153 label: "environment-data:commands:thread:fork-from-run",
154 execute: (input: ForkThreadFromRunInput) => forkThreadFromRun(input),
155 scheduler,
156 concurrency: {
157 mode: "serial",
158 key: ({ environmentId, input }) => JSON.stringify([environmentId, input.sourceThreadId]),
159 },
160 }),
161 mergeBack: createEnvironmentCommand(runtime, {
162 label: "environment-data:commands:thread:merge-back",
163 execute: (input: MergeThreadBackInput) => mergeThreadBack(input),
164 scheduler,
165 concurrency: {
166 mode: "serial",
167 key: ({ environmentId, input }) =>
168 JSON.stringify([environmentId, input.sourceThreadId, input.targetThreadId]),
169 },
170 }),
171 reorderQueuedRun: createEnvironmentCommand(runtime, {
172 label: "environment-data:commands:thread:reorder-queued-run",
173 execute: (input: ReorderQueuedRunInput) => reorderQueuedRun(input),
174 scheduler,
175 concurrency,
176 }),
177 promoteQueuedRun: createEnvironmentCommand(runtime, {
178 label: "environment-data:commands:thread:promote-queued-run",
179 execute: (input: PromoteQueuedRunInput) => promoteQueuedRun(input),
180 scheduler,
181 concurrency,
182 }),
183 };
184}