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

commands.ts

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