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

commands.ts

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