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

commands.ts

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