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