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

commands.ts

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