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