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

commands.ts

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