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

commands.ts

385 lines12.7 KB

1import {
2 CommandId,
3 ORCHESTRATION_WS_METHODS,
4 type OrchestrationGenerateHandoverInput,
5 type ClientOrchestrationCommand,
6} from "@t3tools/contracts";
7import * as Crypto from "effect/Crypto";
8import * as DateTime from "effect/DateTime";
9import * as Effect from "effect/Effect";
10​
11import type { EnvironmentSupervisor } from "../connection/supervisor.ts";
12import {
13 type EnvironmentRpcFailure,
14 type EnvironmentRpcSuccess,
15 type EnvironmentRpcUnavailableError,
16 request,
17} from "../rpc/client.ts";
18​
19type CommandType = ClientOrchestrationCommand["type"];
20type CommandOf<T extends CommandType> = Extract<ClientOrchestrationCommand, { readonly type: T }>;
21type CommandInput<T extends CommandType> = Omit<
22 CommandOf<T>,
23 "type" | "commandId" | "createdAt"
24> & {
25 readonly commandId?: CommandId;
26} & ("createdAt" extends keyof CommandOf<T>
27 ? {
28 readonly createdAt?: CommandOf<T>["createdAt"];
29 }
30 : {});
31​
32export type CreateProjectInput = CommandInput<"project.create">;
33export type UpdateProjectInput = CommandInput<"project.meta.update">;
34export type DeleteProjectInput = CommandInput<"project.delete">;
35export type CreateThreadInput = CommandInput<"thread.create">;
36export type DeleteThreadInput = CommandInput<"thread.delete">;
37export type ArchiveThreadInput = CommandInput<"thread.archive">;
38export type UnarchiveThreadInput = CommandInput<"thread.unarchive">;
39export type SettleThreadInput = CommandInput<"thread.settle">;
40export type UnsettleThreadInput = CommandInput<"thread.unsettle">;
41export type SnoozeThreadInput = CommandInput<"thread.snooze">;
42export type UnsnoozeThreadInput = CommandInput<"thread.unsnooze">;
43export type PinThreadInput = CommandInput<"thread.pin">;
44export type UnpinThreadInput = CommandInput<"thread.unpin">;
45export type ReorderPinnedThreadInput = CommandInput<"thread.pin.reorder">;
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">;
58export type StopThreadSessionInput = CommandInput<"thread.session.stop">;
59​
60type DispatchTag = typeof ORCHESTRATION_WS_METHODS.dispatchCommand;
61type GenerateHandoverTag = typeof ORCHESTRATION_WS_METHODS.generateHandover;
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 });
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 });
90​
91function dispatch(command: ClientOrchestrationCommand) {
92 return request(ORCHESTRATION_WS_METHODS.dispatchCommand, command);
94​
95export const generateThreadHandover = (input: OrchestrationGenerateHandoverInput) =>
96 request(ORCHESTRATION_WS_METHODS.generateHandover, input) satisfies Effect.Effect<
97 EnvironmentRpcSuccess<GenerateHandoverTag>,
98 EnvironmentRpcFailure<GenerateHandoverTag> | EnvironmentRpcUnavailableError,
99 EnvironmentSupervisor
100 >;
101​
102export const createProject: (input: CreateProjectInput) => CommandEffect = Effect.fn(
103 "EnvironmentCommands.createProject",
104)(function* (input) {
105 const metadata = yield* timestampedCommandMetadata(input);
106 return yield* dispatch({
107 ...input,
108 type: "project.create",
109 commandId: metadata.commandId,
110 createdAt: metadata.createdAt,
111 });
112});
113​
114export const updateProject: (input: UpdateProjectInput) => CommandEffect = Effect.fn(
115 "EnvironmentCommands.updateProject",
116)(function* (input) {
117 return yield* dispatch({
118 ...input,
119 type: "project.meta.update",
120 commandId: yield* commandId(input),
121 });
122});
123​
124export const deleteProject: (input: DeleteProjectInput) => CommandEffect = Effect.fn(
125 "EnvironmentCommands.deleteProject",
126)(function* (input) {
127 return yield* dispatch({
128 ...input,
129 type: "project.delete",
130 commandId: yield* commandId(input),
131 });
132});
133​
134export const createThread: (input: CreateThreadInput) => CommandEffect = Effect.fn(
135 "EnvironmentCommands.createThread",
136)(function* (input) {
137 const metadata = yield* timestampedCommandMetadata(input);
138 return yield* dispatch({
139 ...input,
140 type: "thread.create",
141 commandId: metadata.commandId,
142 createdAt: metadata.createdAt,
143 });
144});
145​
146export const deleteThread: (input: DeleteThreadInput) => CommandEffect = Effect.fn(
147 "EnvironmentCommands.deleteThread",
148)(function* (input) {
149 return yield* dispatch({
150 ...input,
151 type: "thread.delete",
152 commandId: yield* commandId(input),
153 });
154});
155​
156export const archiveThread: (input: ArchiveThreadInput) => CommandEffect = Effect.fn(
157 "EnvironmentCommands.archiveThread",
158)(function* (input) {
159 return yield* dispatch({
160 ...input,
161 type: "thread.archive",
162 commandId: yield* commandId(input),
163 });
164});
165​
166export const unarchiveThread: (input: UnarchiveThreadInput) => CommandEffect = Effect.fn(
167 "EnvironmentCommands.unarchiveThread",
168)(function* (input) {
169 return yield* dispatch({
170 ...input,
171 type: "thread.unarchive",
172 commandId: yield* commandId(input),
173 });
174});
175​
176export const settleThread: (input: SettleThreadInput) => CommandEffect = Effect.fn(
177 "EnvironmentCommands.settleThread",
178)(function* (input) {
179 return yield* dispatch({
180 ...input,
181 type: "thread.settle",
182 commandId: yield* commandId(input),
183 });
184});
185​
186export const unsettleThread: (input: UnsettleThreadInput) => CommandEffect = Effect.fn(
187 "EnvironmentCommands.unsettleThread",
188)(function* (input) {
189 return yield* dispatch({
190 ...input,
191 type: "thread.unsettle",
192 commandId: yield* commandId(input),
193 });
194});
195​
196export const snoozeThread: (input: SnoozeThreadInput) => CommandEffect = Effect.fn(
197 "EnvironmentCommands.snoozeThread",
198)(function* (input) {
199 return yield* dispatch({
200 ...input,
201 type: "thread.snooze",
202 commandId: yield* commandId(input),
203 });
204});
205​
206export const unsnoozeThread: (input: UnsnoozeThreadInput) => CommandEffect = Effect.fn(
207 "EnvironmentCommands.unsnoozeThread",
208)(function* (input) {
209 return yield* dispatch({
210 ...input,
211 type: "thread.unsnooze",
212 commandId: yield* commandId(input),
213 });
214});
215​
216export const pinThread: (input: PinThreadInput) => CommandEffect = Effect.fn(
217 "EnvironmentCommands.pinThread",
218)(function* (input) {
219 return yield* dispatch({
220 ...input,
221 type: "thread.pin",
222 commandId: yield* commandId(input),
223 });
224});
225​
226export const unpinThread: (input: UnpinThreadInput) => CommandEffect = Effect.fn(
227 "EnvironmentCommands.unpinThread",
228)(function* (input) {
229 return yield* dispatch({
230 ...input,
231 type: "thread.unpin",
232 commandId: yield* commandId(input),
233 });
234});
235​
236export const reorderPinnedThread: (input: ReorderPinnedThreadInput) => CommandEffect = Effect.fn(
237 "EnvironmentCommands.reorderPinnedThread",
238)(function* (input) {
239 return yield* dispatch({
240 ...input,
241 type: "thread.pin.reorder",
242 commandId: yield* commandId(input),
243 });
244});
245​
246export const reorderActiveThread: (input: ReorderActiveThreadInput) => CommandEffect = Effect.fn(
247 "EnvironmentCommands.reorderActiveThread",
248)(function* (input) {
249 return yield* dispatch({
250 ...input,
251 type: "thread.active.reorder",
252 commandId: yield* commandId(input),
253 });
254});
255​
256export const updateThreadMetadata: (input: UpdateThreadMetadataInput) => CommandEffect = Effect.fn(
257 "EnvironmentCommands.updateThreadMetadata",
258)(function* (input) {
259 return yield* dispatch({
260 ...input,
261 type: "thread.meta.update",
262 commandId: yield* commandId(input),
263 });
264});
265​
266export const linkThreadPullRequest: (input: LinkThreadPullRequestInput) => CommandEffect =
267 Effect.fn("EnvironmentCommands.linkThreadPullRequest")(function* (input) {
268 return yield* dispatch({
269 ...input,
270 type: "thread.pull-request.link",
271 commandId: yield* commandId(input),
272 });
273 });
274​
275export const unlinkThreadPullRequest: (input: UnlinkThreadPullRequestInput) => CommandEffect =
276 Effect.fn("EnvironmentCommands.unlinkThreadPullRequest")(function* (input) {
277 return yield* dispatch({
278 ...input,
279 type: "thread.pull-request.unlink",
280 commandId: yield* commandId(input),
281 });
282 });
283​
284export const setThreadRuntimeMode: (input: SetThreadRuntimeModeInput) => CommandEffect = Effect.fn(
285 "EnvironmentCommands.setThreadRuntimeMode",
286)(function* (input) {
287 const metadata = yield* timestampedCommandMetadata(input);
288 return yield* dispatch({
289 ...input,
290 type: "thread.runtime-mode.set",
291 commandId: metadata.commandId,
292 createdAt: metadata.createdAt,
293 });
294});
295​
296export const setThreadInteractionMode: (input: SetThreadInteractionModeInput) => CommandEffect =
297 Effect.fn("EnvironmentCommands.setThreadInteractionMode")(function* (input) {
298 const metadata = yield* timestampedCommandMetadata(input);
299 return yield* dispatch({
300 ...input,
301 type: "thread.interaction-mode.set",
302 commandId: metadata.commandId,
303 createdAt: metadata.createdAt,
304 });
305 });
306​
307export const startThreadTurn: (input: StartThreadTurnInput) => CommandEffect = Effect.fn(
308 "EnvironmentCommands.startThreadTurn",
309)(function* (input) {
310 const metadata = yield* timestampedCommandMetadata(input);
311 return yield* dispatch({
312 ...input,
313 type: "thread.turn.start",
314 commandId: metadata.commandId,
315 createdAt: metadata.createdAt,
316 });
317});
318​
319export const interruptThreadTurn: (input: InterruptThreadTurnInput) => CommandEffect = Effect.fn(
320 "EnvironmentCommands.interruptThreadTurn",
321)(function* (input) {
322 const metadata = yield* timestampedCommandMetadata(input);
323 return yield* dispatch({
324 ...input,
325 type: "thread.turn.interrupt",
326 commandId: metadata.commandId,
327 createdAt: metadata.createdAt,
328 });
329});
330​
331export const respondToThreadApproval: (input: RespondToThreadApprovalInput) => CommandEffect =
332 Effect.fn("EnvironmentCommands.respondToThreadApproval")(function* (input) {
333 const metadata = yield* timestampedCommandMetadata(input);
334 return yield* dispatch({
335 ...input,
336 type: "thread.approval.respond",
337 commandId: metadata.commandId,
338 createdAt: metadata.createdAt,
339 });
340 });
341​
342export const respondToThreadUserInput: (input: RespondToThreadUserInputInput) => CommandEffect =
343 Effect.fn("EnvironmentCommands.respondToThreadUserInput")(function* (input) {
344 const metadata = yield* timestampedCommandMetadata(input);
345 return yield* dispatch({
346 ...input,
347 type: "thread.user-input.respond",
348 commandId: metadata.commandId,
349 createdAt: metadata.createdAt,
350 });
351 });
352​
353export const dismissThreadUserInput: (input: DismissThreadUserInputInput) => CommandEffect =
354 Effect.fn("EnvironmentCommands.dismissThreadUserInput")(function* (input) {
355 const metadata = yield* timestampedCommandMetadata(input);
356 return yield* dispatch({
357 ...input,
358 type: "thread.user-input.dismiss",
359 commandId: metadata.commandId,
360 createdAt: metadata.createdAt,
361 });
362 });
363​
364export const revertThreadCheckpoint: (input: RevertThreadCheckpointInput) => CommandEffect =
365 Effect.fn("EnvironmentCommands.revertThreadCheckpoint")(function* (input) {
366 const metadata = yield* timestampedCommandMetadata(input);
367 return yield* dispatch({
368 ...input,
369 type: "thread.checkpoint.revert",
370 commandId: metadata.commandId,
371 createdAt: metadata.createdAt,
372 });
373 });
374​
375export const stopThreadSession: (input: StopThreadSessionInput) => CommandEffect = Effect.fn(
376 "EnvironmentCommands.stopThreadSession",
377)(function* (input) {
378 const metadata = yield* timestampedCommandMetadata(input);
379 return yield* dispatch({
380 ...input,
381 type: "thread.session.stop",
382 commandId: metadata.commandId,
383 createdAt: metadata.createdAt,
384 });
385});