Open History to see the last commit
1import {
2 CommandId,
3 ORCHESTRATION_WS_METHODS,
4 type ClientOrchestrationCommand,
5} from "@t3tools/contracts";
6import * as Crypto from "effect/Crypto";
7import * as DateTime from "effect/DateTime";
8import * as Effect from "effect/Effect";
9
10import type { EnvironmentSupervisor } from "../connection/supervisor.ts";
11import {
12 type EnvironmentRpcFailure,
13 type EnvironmentRpcSuccess,
14 type EnvironmentRpcUnavailableError,
15 request,
16} from "../rpc/client.ts";
17
18type CommandType = ClientOrchestrationCommand["type"];
19type CommandOf<T extends CommandType> = Extract<ClientOrchestrationCommand, { readonly type: T }>;
20type CommandInput<T extends CommandType> = Omit<
21 CommandOf<T>,
22 "type" | "commandId" | "createdAt"
23> & {
24 readonly commandId?: CommandId;
25} & ("createdAt" extends keyof CommandOf<T>
26 ? {
27 readonly createdAt?: CommandOf<T>["createdAt"];
28 }
29 : {});
30
31export type CreateProjectInput = CommandInput<"project.create">;
32export type UpdateProjectInput = CommandInput<"project.meta.update">;
33export type DeleteProjectInput = CommandInput<"project.delete">;
34export type CreateThreadInput = CommandInput<"thread.create">;
35export type DeleteThreadInput = CommandInput<"thread.delete">;
36export type ArchiveThreadInput = CommandInput<"thread.archive">;
37export type UnarchiveThreadInput = CommandInput<"thread.unarchive">;
38export type SettleThreadInput = CommandInput<"thread.settle">;
39export type UnsettleThreadInput = CommandInput<"thread.unsettle">;
40export type SnoozeThreadInput = CommandInput<"thread.snooze">;
41export type UnsnoozeThreadInput = CommandInput<"thread.unsnooze">;
42export type PinThreadInput = CommandInput<"thread.pin">;
43export type UnpinThreadInput = CommandInput<"thread.unpin">;
44export type UpdateThreadMetadataInput = CommandInput<"thread.meta.update">;
45export type SetThreadRuntimeModeInput = CommandInput<"thread.runtime-mode.set">;
46export type SetThreadInteractionModeInput = CommandInput<"thread.interaction-mode.set">;
47export type StartThreadTurnInput = CommandInput<"thread.turn.start">;
48export type InterruptThreadTurnInput = CommandInput<"thread.turn.interrupt">;
49export type RespondToThreadApprovalInput = CommandInput<"thread.approval.respond">;
50export type RespondToThreadUserInputInput = CommandInput<"thread.user-input.respond">;
51export type RevertThreadCheckpointInput = CommandInput<"thread.checkpoint.revert">;
52export type StopThreadSessionInput = CommandInput<"thread.session.stop">;
53
54type DispatchTag = typeof ORCHESTRATION_WS_METHODS.dispatchCommand;
55type CommandEffect = Effect.Effect<
56 EnvironmentRpcSuccess<DispatchTag>,
57 EnvironmentRpcFailure<DispatchTag> | EnvironmentRpcUnavailableError,
58 Crypto.Crypto | EnvironmentSupervisor
59>;
60
61function commandId(input: { readonly commandId?: CommandId }) {
62 return Effect.gen(function* () {
63 if (input.commandId !== undefined) {
64 return input.commandId;
65 }
66 const crypto = yield* Crypto.Crypto;
67 return yield* crypto.randomUUIDv4.pipe(Effect.orDie, Effect.map(CommandId.make));
68 });
69}
70
71function timestampedCommandMetadata(input: {
72 readonly commandId?: CommandId;
73 readonly createdAt?: string;
74}) {
75 return Effect.all({
76 commandId: commandId(input),
77 createdAt:
78 input.createdAt === undefined
79 ? DateTime.now.pipe(Effect.map(DateTime.formatIso))
80 : Effect.succeed(input.createdAt),
81 });
82}
83
84function dispatch(command: ClientOrchestrationCommand) {
85 return request(ORCHESTRATION_WS_METHODS.dispatchCommand, command);
86}
87
88export const createProject: (input: CreateProjectInput) => CommandEffect = Effect.fn(
89 "EnvironmentCommands.createProject",
90)(function* (input) {
91 const metadata = yield* timestampedCommandMetadata(input);
92 return yield* dispatch({
93 ...input,
94 type: "project.create",
95 commandId: metadata.commandId,
96 createdAt: metadata.createdAt,
97 });
98});
99
100export const updateProject: (input: UpdateProjectInput) => CommandEffect = Effect.fn(
101 "EnvironmentCommands.updateProject",
102)(function* (input) {
103 return yield* dispatch({
104 ...input,
105 type: "project.meta.update",
106 commandId: yield* commandId(input),
107 });
108});
109
110export const deleteProject: (input: DeleteProjectInput) => CommandEffect = Effect.fn(
111 "EnvironmentCommands.deleteProject",
112)(function* (input) {
113 return yield* dispatch({
114 ...input,
115 type: "project.delete",
116 commandId: yield* commandId(input),
117 });
118});
119
120export const createThread: (input: CreateThreadInput) => CommandEffect = Effect.fn(
121 "EnvironmentCommands.createThread",
122)(function* (input) {
123 const metadata = yield* timestampedCommandMetadata(input);
124 return yield* dispatch({
125 ...input,
126 type: "thread.create",
127 commandId: metadata.commandId,
128 createdAt: metadata.createdAt,
129 });
130});
131
132export const deleteThread: (input: DeleteThreadInput) => CommandEffect = Effect.fn(
133 "EnvironmentCommands.deleteThread",
134)(function* (input) {
135 return yield* dispatch({
136 ...input,
137 type: "thread.delete",
138 commandId: yield* commandId(input),
139 });
140});
141
142export const archiveThread: (input: ArchiveThreadInput) => CommandEffect = Effect.fn(
143 "EnvironmentCommands.archiveThread",
144)(function* (input) {
145 return yield* dispatch({
146 ...input,
147 type: "thread.archive",
148 commandId: yield* commandId(input),
149 });
150});
151
152export const unarchiveThread: (input: UnarchiveThreadInput) => CommandEffect = Effect.fn(
153 "EnvironmentCommands.unarchiveThread",
154)(function* (input) {
155 return yield* dispatch({
156 ...input,
157 type: "thread.unarchive",
158 commandId: yield* commandId(input),
159 });
160});
161
162export const settleThread: (input: SettleThreadInput) => CommandEffect = Effect.fn(
163 "EnvironmentCommands.settleThread",
164)(function* (input) {
165 return yield* dispatch({
166 ...input,
167 type: "thread.settle",
168 commandId: yield* commandId(input),
169 });
170});
171
172export const unsettleThread: (input: UnsettleThreadInput) => CommandEffect = Effect.fn(
173 "EnvironmentCommands.unsettleThread",
174)(function* (input) {
175 return yield* dispatch({
176 ...input,
177 type: "thread.unsettle",
178 commandId: yield* commandId(input),
179 });
180});
181
182export const snoozeThread: (input: SnoozeThreadInput) => CommandEffect = Effect.fn(
183 "EnvironmentCommands.snoozeThread",
184)(function* (input) {
185 return yield* dispatch({
186 ...input,
187 type: "thread.snooze",
188 commandId: yield* commandId(input),
189 });
190});
191
192export const unsnoozeThread: (input: UnsnoozeThreadInput) => CommandEffect = Effect.fn(
193 "EnvironmentCommands.unsnoozeThread",
194)(function* (input) {
195 return yield* dispatch({
196 ...input,
197 type: "thread.unsnooze",
198 commandId: yield* commandId(input),
199 });
200});
201
202export const pinThread: (input: PinThreadInput) => CommandEffect = Effect.fn(
203 "EnvironmentCommands.pinThread",
204)(function* (input) {
205 return yield* dispatch({
206 ...input,
207 type: "thread.pin",
208 commandId: yield* commandId(input),
209 });
210});
211
212export const unpinThread: (input: UnpinThreadInput) => CommandEffect = Effect.fn(
213 "EnvironmentCommands.unpinThread",
214)(function* (input) {
215 return yield* dispatch({
216 ...input,
217 type: "thread.unpin",
218 commandId: yield* commandId(input),
219 });
220});
221
222export const updateThreadMetadata: (input: UpdateThreadMetadataInput) => CommandEffect = Effect.fn(
223 "EnvironmentCommands.updateThreadMetadata",
224)(function* (input) {
225 return yield* dispatch({
226 ...input,
227 type: "thread.meta.update",
228 commandId: yield* commandId(input),
229 });
230});
231
232export const setThreadRuntimeMode: (input: SetThreadRuntimeModeInput) => CommandEffect = Effect.fn(
233 "EnvironmentCommands.setThreadRuntimeMode",
234)(function* (input) {
235 const metadata = yield* timestampedCommandMetadata(input);
236 return yield* dispatch({
237 ...input,
238 type: "thread.runtime-mode.set",
239 commandId: metadata.commandId,
240 createdAt: metadata.createdAt,
241 });
242});
243
244export const setThreadInteractionMode: (input: SetThreadInteractionModeInput) => CommandEffect =
245 Effect.fn("EnvironmentCommands.setThreadInteractionMode")(function* (input) {
246 const metadata = yield* timestampedCommandMetadata(input);
247 return yield* dispatch({
248 ...input,
249 type: "thread.interaction-mode.set",
250 commandId: metadata.commandId,
251 createdAt: metadata.createdAt,
252 });
253 });
254
255export const startThreadTurn: (input: StartThreadTurnInput) => CommandEffect = Effect.fn(
256 "EnvironmentCommands.startThreadTurn",
257)(function* (input) {
258 const metadata = yield* timestampedCommandMetadata(input);
259 return yield* dispatch({
260 ...input,
261 type: "thread.turn.start",
262 commandId: metadata.commandId,
263 createdAt: metadata.createdAt,
264 });
265});
266
267export const interruptThreadTurn: (input: InterruptThreadTurnInput) => CommandEffect = Effect.fn(
268 "EnvironmentCommands.interruptThreadTurn",
269)(function* (input) {
270 const metadata = yield* timestampedCommandMetadata(input);
271 return yield* dispatch({
272 ...input,
273 type: "thread.turn.interrupt",
274 commandId: metadata.commandId,
275 createdAt: metadata.createdAt,
276 });
277});
278
279export const respondToThreadApproval: (input: RespondToThreadApprovalInput) => CommandEffect =
280 Effect.fn("EnvironmentCommands.respondToThreadApproval")(function* (input) {
281 const metadata = yield* timestampedCommandMetadata(input);
282 return yield* dispatch({
283 ...input,
284 type: "thread.approval.respond",
285 commandId: metadata.commandId,
286 createdAt: metadata.createdAt,
287 });
288 });
289
290export const respondToThreadUserInput: (input: RespondToThreadUserInputInput) => CommandEffect =
291 Effect.fn("EnvironmentCommands.respondToThreadUserInput")(function* (input) {
292 const metadata = yield* timestampedCommandMetadata(input);
293 return yield* dispatch({
294 ...input,
295 type: "thread.user-input.respond",
296 commandId: metadata.commandId,
297 createdAt: metadata.createdAt,
298 });
299 });
300
301export const revertThreadCheckpoint: (input: RevertThreadCheckpointInput) => CommandEffect =
302 Effect.fn("EnvironmentCommands.revertThreadCheckpoint")(function* (input) {
303 const metadata = yield* timestampedCommandMetadata(input);
304 return yield* dispatch({
305 ...input,
306 type: "thread.checkpoint.revert",
307 commandId: metadata.commandId,
308 createdAt: metadata.createdAt,
309 });
310 });
311
312export const stopThreadSession: (input: StopThreadSessionInput) => CommandEffect = Effect.fn(
313 "EnvironmentCommands.stopThreadSession",
314)(function* (input) {
315 const metadata = yield* timestampedCommandMetadata(input);
316 return yield* dispatch({
317 ...input,
318 type: "thread.session.stop",
319 commandId: metadata.commandId,
320 createdAt: metadata.createdAt,
321 });
322});