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

decider.ts

526 lines15.0 KB

1import type {
2 OrchestrationCommand,
3 OrchestrationEvent,
4 OrchestrationReadModel,
5} from "@t3tools/contracts";
6import { Effect } from "effect";
7​
8import { OrchestrationCommandInvariantError } from "./Errors.ts";
9import {
10 requireProject,
11 requireProjectAbsent,
12 requireThread,
13 requireThreadAbsent,
14} from "./commandInvariants.ts";
15​
16const nowIso = () => new Date().toISOString();
17const DEFAULT_ASSISTANT_DELIVERY_MODE = "buffered" as const;
18​
19const defaultMetadata: Omit<OrchestrationEvent, "sequence" | "type" | "payload"> = {
20 eventId: crypto.randomUUID() as OrchestrationEvent["eventId"],
21 aggregateKind: "thread",
22 aggregateId: "" as OrchestrationEvent["aggregateId"],
23 occurredAt: nowIso(),
24 commandId: null,
25 causationEventId: null,
26 correlationId: null,
27 metadata: {},
28};
29​
30function withEventBase(
31 input: Pick<OrchestrationCommand, "commandId"> & {
32 readonly aggregateKind: OrchestrationEvent["aggregateKind"];
33 readonly aggregateId: OrchestrationEvent["aggregateId"];
34 readonly occurredAt: string;
35 readonly metadata?: OrchestrationEvent["metadata"];
36 },
37): Omit<OrchestrationEvent, "sequence" | "type" | "payload"> {
38 return {
39 ...defaultMetadata,
40 eventId: crypto.randomUUID() as OrchestrationEvent["eventId"],
41 aggregateKind: input.aggregateKind,
42 aggregateId: input.aggregateId,
43 occurredAt: input.occurredAt,
44 commandId: input.commandId,
45 correlationId: input.commandId,
46 metadata: input.metadata ?? {},
47 };
49​
50export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand")(function* ({
51 command,
52 readModel,
53}: {
54 readonly command: OrchestrationCommand;
55 readonly readModel: OrchestrationReadModel;
56}): Effect.fn.Return<
57 Omit<OrchestrationEvent, "sequence"> | ReadonlyArray<Omit<OrchestrationEvent, "sequence">>,
58 OrchestrationCommandInvariantError
59> {
60 switch (command.type) {
61 case "project.create": {
62 yield* requireProjectAbsent({
63 readModel,
64 command,
65 projectId: command.projectId,
66 });
67​
68 return {
69 ...withEventBase({
70 aggregateKind: "project",
71 aggregateId: command.projectId,
72 occurredAt: command.createdAt,
73 commandId: command.commandId,
74 }),
75 type: "project.created",
76 payload: {
77 projectId: command.projectId,
78 title: command.title,
79 workspaceRoot: command.workspaceRoot,
80 defaultModel: command.defaultModel ?? null,
81 scripts: [],
82 createdAt: command.createdAt,
83 updatedAt: command.createdAt,
84 },
85 };
86 }
87​
88 case "project.meta.update": {
89 yield* requireProject({
90 readModel,
91 command,
92 projectId: command.projectId,
93 });
94 const occurredAt = nowIso();
95 return {
96 ...withEventBase({
97 aggregateKind: "project",
98 aggregateId: command.projectId,
99 occurredAt,
100 commandId: command.commandId,
101 }),
102 type: "project.meta-updated",
103 payload: {
104 projectId: command.projectId,
105 ...(command.title !== undefined ? { title: command.title } : {}),
106 ...(command.workspaceRoot !== undefined ? { workspaceRoot: command.workspaceRoot } : {}),
107 ...(command.defaultModel !== undefined ? { defaultModel: command.defaultModel } : {}),
108 ...(command.scripts !== undefined ? { scripts: command.scripts } : {}),
109 updatedAt: occurredAt,
110 },
111 };
112 }
113​
114 case "project.delete": {
115 yield* requireProject({
116 readModel,
117 command,
118 projectId: command.projectId,
119 });
120 const occurredAt = nowIso();
121 return {
122 ...withEventBase({
123 aggregateKind: "project",
124 aggregateId: command.projectId,
125 occurredAt,
126 commandId: command.commandId,
127 }),
128 type: "project.deleted",
129 payload: {
130 projectId: command.projectId,
131 deletedAt: occurredAt,
132 },
133 };
134 }
135​
136 case "thread.create": {
137 yield* requireProject({
138 readModel,
139 command,
140 projectId: command.projectId,
141 });
142 yield* requireThreadAbsent({
143 readModel,
144 command,
145 threadId: command.threadId,
146 });
147 return {
148 ...withEventBase({
149 aggregateKind: "thread",
150 aggregateId: command.threadId,
151 occurredAt: command.createdAt,
152 commandId: command.commandId,
153 }),
154 type: "thread.created",
155 payload: {
156 threadId: command.threadId,
157 projectId: command.projectId,
158 title: command.title,
159 model: command.model,
160 branch: command.branch,
161 worktreePath: command.worktreePath,
162 createdAt: command.createdAt,
163 updatedAt: command.createdAt,
164 },
165 };
166 }
167​
168 case "thread.delete": {
169 yield* requireThread({
170 readModel,
171 command,
172 threadId: command.threadId,
173 });
174 const occurredAt = nowIso();
175 return {
176 ...withEventBase({
177 aggregateKind: "thread",
178 aggregateId: command.threadId,
179 occurredAt,
180 commandId: command.commandId,
181 }),
182 type: "thread.deleted",
183 payload: {
184 threadId: command.threadId,
185 deletedAt: occurredAt,
186 },
187 };
188 }
189​
190 case "thread.meta.update": {
191 yield* requireThread({
192 readModel,
193 command,
194 threadId: command.threadId,
195 });
196 const occurredAt = nowIso();
197 return {
198 ...withEventBase({
199 aggregateKind: "thread",
200 aggregateId: command.threadId,
201 occurredAt,
202 commandId: command.commandId,
203 }),
204 type: "thread.meta-updated",
205 payload: {
206 threadId: command.threadId,
207 ...(command.title !== undefined ? { title: command.title } : {}),
208 ...(command.model !== undefined ? { model: command.model } : {}),
209 ...(command.branch !== undefined ? { branch: command.branch } : {}),
210 ...(command.worktreePath !== undefined ? { worktreePath: command.worktreePath } : {}),
211 updatedAt: occurredAt,
212 },
213 };
214 }
215​
216 case "thread.turn.start": {
217 yield* requireThread({
218 readModel,
219 command,
220 threadId: command.threadId,
221 });
222 const userMessageEvent: Omit<OrchestrationEvent, "sequence"> = {
223 ...withEventBase({
224 aggregateKind: "thread",
225 aggregateId: command.threadId,
226 occurredAt: command.createdAt,
227 commandId: command.commandId,
228 }),
229 type: "thread.message-sent",
230 payload: {
231 threadId: command.threadId,
232 messageId: command.message.messageId,
233 role: "user",
234 text: command.message.text,
235 attachments: command.message.attachments,
236 turnId: null,
237 streaming: false,
238 createdAt: command.createdAt,
239 updatedAt: command.createdAt,
240 },
241 };
242 const turnStartRequestedEvent: Omit<OrchestrationEvent, "sequence"> = {
243 ...withEventBase({
244 aggregateKind: "thread",
245 aggregateId: command.threadId,
246 occurredAt: command.createdAt,
247 commandId: command.commandId,
248 }),
249 causationEventId: userMessageEvent.eventId,
250 type: "thread.turn-start-requested",
251 payload: {
252 threadId: command.threadId,
253 messageId: command.message.messageId,
254 ...(command.model !== undefined ? { model: command.model } : {}),
255 ...(command.effort !== undefined ? { effort: command.effort } : {}),
256 assistantDeliveryMode: command.assistantDeliveryMode ?? DEFAULT_ASSISTANT_DELIVERY_MODE,
257 approvalPolicy: command.approvalPolicy,
258 sandboxMode: command.sandboxMode,
259 createdAt: command.createdAt,
260 },
261 };
262 return [userMessageEvent, turnStartRequestedEvent];
263 }
264​
265 case "thread.turn.interrupt": {
266 yield* requireThread({
267 readModel,
268 command,
269 threadId: command.threadId,
270 });
271 return {
272 ...withEventBase({
273 aggregateKind: "thread",
274 aggregateId: command.threadId,
275 occurredAt: command.createdAt,
276 commandId: command.commandId,
277 }),
278 type: "thread.turn-interrupt-requested",
279 payload: {
280 threadId: command.threadId,
281 ...(command.turnId !== undefined ? { turnId: command.turnId } : {}),
282 createdAt: command.createdAt,
283 },
284 };
285 }
286​
287 case "thread.approval.respond": {
288 yield* requireThread({
289 readModel,
290 command,
291 threadId: command.threadId,
292 });
293 return {
294 ...withEventBase({
295 aggregateKind: "thread",
296 aggregateId: command.threadId,
297 occurredAt: command.createdAt,
298 commandId: command.commandId,
299 metadata: {
300 requestId: command.requestId,
301 },
302 }),
303 type: "thread.approval-response-requested",
304 payload: {
305 threadId: command.threadId,
306 requestId: command.requestId,
307 decision: command.decision,
308 createdAt: command.createdAt,
309 },
310 };
311 }
312​
313 case "thread.checkpoint.revert": {
314 yield* requireThread({
315 readModel,
316 command,
317 threadId: command.threadId,
318 });
319 return {
320 ...withEventBase({
321 aggregateKind: "thread",
322 aggregateId: command.threadId,
323 occurredAt: command.createdAt,
324 commandId: command.commandId,
325 }),
326 type: "thread.checkpoint-revert-requested",
327 payload: {
328 threadId: command.threadId,
329 turnCount: command.turnCount,
330 createdAt: command.createdAt,
331 },
332 };
333 }
334​
335 case "thread.session.stop": {
336 yield* requireThread({
337 readModel,
338 command,
339 threadId: command.threadId,
340 });
341 return {
342 ...withEventBase({
343 aggregateKind: "thread",
344 aggregateId: command.threadId,
345 occurredAt: command.createdAt,
346 commandId: command.commandId,
347 }),
348 type: "thread.session-stop-requested",
349 payload: {
350 threadId: command.threadId,
351 createdAt: command.createdAt,
352 },
353 };
354 }
355​
356 case "thread.session.set": {
357 yield* requireThread({
358 readModel,
359 command,
360 threadId: command.threadId,
361 });
362 return {
363 ...withEventBase({
364 aggregateKind: "thread",
365 aggregateId: command.threadId,
366 occurredAt: command.createdAt,
367 commandId: command.commandId,
368 metadata: {
369 ...(command.session.providerSessionId !== null
370 ? { providerSessionId: command.session.providerSessionId }
371 : {}),
372 ...(command.session.providerThreadId !== null
373 ? { providerThreadId: command.session.providerThreadId }
374 : {}),
375 },
376 }),
377 type: "thread.session-set",
378 payload: {
379 threadId: command.threadId,
380 session: command.session,
381 },
382 };
383 }
384​
385 case "thread.message.assistant.delta": {
386 yield* requireThread({
387 readModel,
388 command,
389 threadId: command.threadId,
390 });
391 return {
392 ...withEventBase({
393 aggregateKind: "thread",
394 aggregateId: command.threadId,
395 occurredAt: command.createdAt,
396 commandId: command.commandId,
397 }),
398 type: "thread.message-sent",
399 payload: {
400 threadId: command.threadId,
401 messageId: command.messageId,
402 role: "assistant",
403 text: command.delta,
404 turnId: command.turnId ?? null,
405 streaming: true,
406 createdAt: command.createdAt,
407 updatedAt: command.createdAt,
408 },
409 };
410 }
411​
412 case "thread.message.assistant.complete": {
413 yield* requireThread({
414 readModel,
415 command,
416 threadId: command.threadId,
417 });
418 return {
419 ...withEventBase({
420 aggregateKind: "thread",
421 aggregateId: command.threadId,
422 occurredAt: command.createdAt,
423 commandId: command.commandId,
424 }),
425 type: "thread.message-sent",
426 payload: {
427 threadId: command.threadId,
428 messageId: command.messageId,
429 role: "assistant",
430 text: "",
431 turnId: command.turnId ?? null,
432 streaming: false,
433 createdAt: command.createdAt,
434 updatedAt: command.createdAt,
435 },
436 };
437 }
438​
439 case "thread.turn.diff.complete": {
440 yield* requireThread({
441 readModel,
442 command,
443 threadId: command.threadId,
444 });
445 return {
446 ...withEventBase({
447 aggregateKind: "thread",
448 aggregateId: command.threadId,
449 occurredAt: command.createdAt,
450 commandId: command.commandId,
451 }),
452 type: "thread.turn-diff-completed",
453 payload: {
454 threadId: command.threadId,
455 turnId: command.turnId,
456 checkpointTurnCount: command.checkpointTurnCount,
457 checkpointRef: command.checkpointRef,
458 status: command.status,
459 files: command.files,
460 assistantMessageId: command.assistantMessageId ?? null,
461 completedAt: command.completedAt,
462 },
463 };
464 }
465​
466 case "thread.revert.complete": {
467 yield* requireThread({
468 readModel,
469 command,
470 threadId: command.threadId,
471 });
472 return {
473 ...withEventBase({
474 aggregateKind: "thread",
475 aggregateId: command.threadId,
476 occurredAt: command.createdAt,
477 commandId: command.commandId,
478 }),
479 type: "thread.reverted",
480 payload: {
481 threadId: command.threadId,
482 turnCount: command.turnCount,
483 },
484 };
485 }
486​
487 case "thread.activity.append": {
488 yield* requireThread({
489 readModel,
490 command,
491 threadId: command.threadId,
492 });
493 const requestId =
494 typeof command.activity.payload === "object" &&
495 command.activity.payload !== null &&
496 "requestId" in command.activity.payload &&
497 typeof (command.activity.payload as { requestId?: unknown }).requestId === "string"
498 ? ((command.activity.payload as { requestId: string })
499 .requestId as OrchestrationEvent["metadata"]["requestId"])
500 : undefined;
501 return {
502 ...withEventBase({
503 aggregateKind: "thread",
504 aggregateId: command.threadId,
505 occurredAt: command.createdAt,
506 commandId: command.commandId,
507 ...(requestId !== undefined ? { metadata: { requestId } } : {}),
508 }),
509 type: "thread.activity-appended",
510 payload: {
511 threadId: command.threadId,
512 activity: command.activity,
513 },
514 };
515 }
516​
517 default: {
518 command satisfies never;
519 const fallback = command as never as { type: string };
520 return yield* new OrchestrationCommandInvariantError({
521 commandType: fallback.type,
522 detail: `Unknown command type: ${fallback.type}`,
523 });
524 }
525 }
526});