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 ReorderPinnedThreadInput = CommandInput<"thread.pin.reorder">;
45export type ReorderActiveThreadInput = CommandInput<"thread.active.reorder">;
46export type UpdateThreadMetadataInput = CommandInput<"thread.meta.update">;
47export type SetThreadRuntimeModeInput = CommandInput<"thread.runtime-mode.set">;
48export type SetThreadInteractionModeInput = CommandInput<"thread.interaction-mode.set">;
49export type StartThreadTurnInput = CommandInput<"thread.turn.start">;
50export type InterruptThreadTurnInput = CommandInput<"thread.turn.interrupt">;
51export type RespondToThreadApprovalInput = CommandInput<"thread.approval.respond">;
52export type RespondToThreadUserInputInput = CommandInput<"thread.user-input.respond">;
53export type RevertThreadCheckpointInput = CommandInput<"thread.checkpoint.revert">;
54export type StopThreadSessionInput = CommandInput<"thread.session.stop">;
55
56type DispatchTag = typeof ORCHESTRATION_WS_METHODS.dispatchCommand;
57type CommandEffect = Effect.Effect<
58 EnvironmentRpcSuccess<DispatchTag>,
59 EnvironmentRpcFailure<DispatchTag> | EnvironmentRpcUnavailableError,
60 Crypto.Crypto | EnvironmentSupervisor
61>;
62
63function commandId(input: { readonly commandId?: CommandId }) {
64 return Effect.gen(function* () {
65 if (input.commandId !== undefined) {
66 return input.commandId;
67 }
68 const crypto = yield* Crypto.Crypto;
69 return yield* crypto.randomUUIDv4.pipe(Effect.orDie, Effect.map(CommandId.make));
70 });
71}
72
73function timestampedCommandMetadata(input: {
74 readonly commandId?: CommandId;
75 readonly createdAt?: string;
76}) {
77 return Effect.all({
78 commandId: commandId(input),
79 createdAt:
80 input.createdAt === undefined
81 ? DateTime.now.pipe(Effect.map(DateTime.formatIso))
82 : Effect.succeed(input.createdAt),
83 });
84}
85
86function dispatch(command: ClientOrchestrationCommand) {
87 return request(ORCHESTRATION_WS_METHODS.dispatchCommand, command);
88}
89
90export const createProject: (input: CreateProjectInput) => CommandEffect = Effect.fn(
91 "EnvironmentCommands.createProject",
92)(function* (input) {
93 const metadata = yield* timestampedCommandMetadata(input);
94 return yield* dispatch({
95 ...input,
96 type: "project.create",
97 commandId: metadata.commandId,
98 createdAt: metadata.createdAt,
99 });
100});
101
102export const updateProject: (input: UpdateProjectInput) => CommandEffect = Effect.fn(
103 "EnvironmentCommands.updateProject",
104)(function* (input) {
105 return yield* dispatch({
106 ...input,
107 type: "project.meta.update",
108 commandId: yield* commandId(input),
109 });
110});
111
112export const deleteProject: (input: DeleteProjectInput) => CommandEffect = Effect.fn(
113 "EnvironmentCommands.deleteProject",
114)(function* (input) {
115 return yield* dispatch({
116 ...input,
117 type: "project.delete",
118 commandId: yield* commandId(input),
119 });
120});
121
122export const createThread: (input: CreateThreadInput) => CommandEffect = Effect.fn(
123 "EnvironmentCommands.createThread",
124)(function* (input) {
125 const metadata = yield* timestampedCommandMetadata(input);
126 return yield* dispatch({
127 ...input,
128 type: "thread.create",
129 commandId: metadata.commandId,
130 createdAt: metadata.createdAt,
131 });
132});
133
134export const deleteThread: (input: DeleteThreadInput) => CommandEffect = Effect.fn(
135 "EnvironmentCommands.deleteThread",
136)(function* (input) {
137 return yield* dispatch({
138 ...input,
139 type: "thread.delete",
140 commandId: yield* commandId(input),
141 });
142});
143
144export const archiveThread: (input: ArchiveThreadInput) => CommandEffect = Effect.fn(
145 "EnvironmentCommands.archiveThread",
146)(function* (input) {
147 return yield* dispatch({
148 ...input,
149 type: "thread.archive",
150 commandId: yield* commandId(input),
151 });
152});
153
154export const unarchiveThread: (input: UnarchiveThreadInput) => CommandEffect = Effect.fn(
155 "EnvironmentCommands.unarchiveThread",
156)(function* (input) {
157 return yield* dispatch({
158 ...input,
159 type: "thread.unarchive",
160 commandId: yield* commandId(input),
161 });
162});
163
164export const settleThread: (input: SettleThreadInput) => CommandEffect = Effect.fn(
165 "EnvironmentCommands.settleThread",
166)(function* (input) {
167 return yield* dispatch({
168 ...input,
169 type: "thread.settle",
170 commandId: yield* commandId(input),
171 });
172});
173
174export const unsettleThread: (input: UnsettleThreadInput) => CommandEffect = Effect.fn(
175 "EnvironmentCommands.unsettleThread",
176)(function* (input) {
177 return yield* dispatch({
178 ...input,
179 type: "thread.unsettle",
180 commandId: yield* commandId(input),
181 });
182});
183
184export const snoozeThread: (input: SnoozeThreadInput) => CommandEffect = Effect.fn(
185 "EnvironmentCommands.snoozeThread",
186)(function* (input) {
187 return yield* dispatch({
188 ...input,
189 type: "thread.snooze",
190 commandId: yield* commandId(input),
191 });
192});
193
194export const unsnoozeThread: (input: UnsnoozeThreadInput) => CommandEffect = Effect.fn(
195 "EnvironmentCommands.unsnoozeThread",
196)(function* (input) {
197 return yield* dispatch({
198 ...input,
199 type: "thread.unsnooze",
200 commandId: yield* commandId(input),
201 });
202});
203
204export const pinThread: (input: PinThreadInput) => CommandEffect = Effect.fn(
205 "EnvironmentCommands.pinThread",
206)(function* (input) {
207 return yield* dispatch({
208 ...input,
209 type: "thread.pin",
210 commandId: yield* commandId(input),
211 });
212});
213
214export const unpinThread: (input: UnpinThreadInput) => CommandEffect = Effect.fn(
215 "EnvironmentCommands.unpinThread",
216)(function* (input) {
217 return yield* dispatch({
218 ...input,
219 type: "thread.unpin",
220 commandId: yield* commandId(input),
221 });
222});
223
224export const reorderPinnedThread: (input: ReorderPinnedThreadInput) => CommandEffect = Effect.fn(
225 "EnvironmentCommands.reorderPinnedThread",
226)(function* (input) {
227 return yield* dispatch({
228 ...input,
229 type: "thread.pin.reorder",
230 commandId: yield* commandId(input),
231 });
232});
233
234export const reorderActiveThread: (input: ReorderActiveThreadInput) => CommandEffect = Effect.fn(
235 "EnvironmentCommands.reorderActiveThread",
236)(function* (input) {
237 return yield* dispatch({
238 ...input,
239 type: "thread.active.reorder",
240 commandId: yield* commandId(input),
241 });
242});
243
244export const updateThreadMetadata: (input: UpdateThreadMetadataInput) => CommandEffect = Effect.fn(
245 "EnvironmentCommands.updateThreadMetadata",
246)(function* (input) {
247 return yield* dispatch({
248 ...input,
249 type: "thread.meta.update",
250 commandId: yield* commandId(input),
251 });
252});
253
254export const setThreadRuntimeMode: (input: SetThreadRuntimeModeInput) => CommandEffect = Effect.fn(
255 "EnvironmentCommands.setThreadRuntimeMode",
256)(function* (input) {
257 const metadata = yield* timestampedCommandMetadata(input);
258 return yield* dispatch({
259 ...input,
260 type: "thread.runtime-mode.set",
261 commandId: metadata.commandId,
262 createdAt: metadata.createdAt,
263 });
264});
265
266export const setThreadInteractionMode: (input: SetThreadInteractionModeInput) => CommandEffect =
267 Effect.fn("EnvironmentCommands.setThreadInteractionMode")(function* (input) {
268 const metadata = yield* timestampedCommandMetadata(input);
269 return yield* dispatch({
270 ...input,
271 type: "thread.interaction-mode.set",
272 commandId: metadata.commandId,
273 createdAt: metadata.createdAt,
274 });
275 });
276
277export const startThreadTurn: (input: StartThreadTurnInput) => CommandEffect = Effect.fn(
278 "EnvironmentCommands.startThreadTurn",
279)(function* (input) {
280 const metadata = yield* timestampedCommandMetadata(input);
281 return yield* dispatch({
282 ...input,
283 type: "thread.turn.start",
284 commandId: metadata.commandId,
285 createdAt: metadata.createdAt,
286 });
287});
288
289export const interruptThreadTurn: (input: InterruptThreadTurnInput) => CommandEffect = Effect.fn(
290 "EnvironmentCommands.interruptThreadTurn",
291)(function* (input) {
292 const metadata = yield* timestampedCommandMetadata(input);
293 return yield* dispatch({
294 ...input,
295 type: "thread.turn.interrupt",
296 commandId: metadata.commandId,
297 createdAt: metadata.createdAt,
298 });
299});
300
301export const respondToThreadApproval: (input: RespondToThreadApprovalInput) => CommandEffect =
302 Effect.fn("EnvironmentCommands.respondToThreadApproval")(function* (input) {
303 const metadata = yield* timestampedCommandMetadata(input);
304 return yield* dispatch({
305 ...input,
306 type: "thread.approval.respond",
307 commandId: metadata.commandId,
308 createdAt: metadata.createdAt,
309 });
310 });
311
312export const respondToThreadUserInput: (input: RespondToThreadUserInputInput) => CommandEffect =
313 Effect.fn("EnvironmentCommands.respondToThreadUserInput")(function* (input) {
314 const metadata = yield* timestampedCommandMetadata(input);
315 return yield* dispatch({
316 ...input,
317 type: "thread.user-input.respond",
318 commandId: metadata.commandId,
319 createdAt: metadata.createdAt,
320 });
321 });
322
323export const revertThreadCheckpoint: (input: RevertThreadCheckpointInput) => CommandEffect =
324 Effect.fn("EnvironmentCommands.revertThreadCheckpoint")(function* (input) {
325 const metadata = yield* timestampedCommandMetadata(input);
326 return yield* dispatch({
327 ...input,
328 type: "thread.checkpoint.revert",
329 commandId: metadata.commandId,
330 createdAt: metadata.createdAt,
331 });
332 });
333
334export const stopThreadSession: (input: StopThreadSessionInput) => CommandEffect = Effect.fn(
335 "EnvironmentCommands.stopThreadSession",
336)(function* (input) {
337 const metadata = yield* timestampedCommandMetadata(input);
338 return yield* dispatch({
339 ...input,
340 type: "thread.session.stop",
341 commandId: metadata.commandId,
342 createdAt: metadata.createdAt,
343 });
344});