Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

commands.ts

404 lines13.4 KB

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