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

commands.ts

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