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

decider.ts

619 lines17.4 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 runtimeMode: command.runtimeMode,
161 interactionMode: command.interactionMode,
162 branch: command.branch,
163 worktreePath: command.worktreePath,
164 createdAt: command.createdAt,
165 updatedAt: command.createdAt,
166 },
167 };
168 }
169​
170 case "thread.delete": {
171 yield* requireThread({
172 readModel,
173 command,
174 threadId: command.threadId,
175 });
176 const occurredAt = nowIso();
177 return {
178 ...withEventBase({
179 aggregateKind: "thread",
180 aggregateId: command.threadId,
181 occurredAt,
182 commandId: command.commandId,
183 }),
184 type: "thread.deleted",
185 payload: {
186 threadId: command.threadId,
187 deletedAt: occurredAt,
188 },
189 };
190 }
191​
192 case "thread.meta.update": {
193 yield* requireThread({
194 readModel,
195 command,
196 threadId: command.threadId,
197 });
198 const occurredAt = nowIso();
199 return {
200 ...withEventBase({
201 aggregateKind: "thread",
202 aggregateId: command.threadId,
203 occurredAt,
204 commandId: command.commandId,
205 }),
206 type: "thread.meta-updated",
207 payload: {
208 threadId: command.threadId,
209 ...(command.title !== undefined ? { title: command.title } : {}),
210 ...(command.model !== undefined ? { model: command.model } : {}),
211 ...(command.branch !== undefined ? { branch: command.branch } : {}),
212 ...(command.worktreePath !== undefined ? { worktreePath: command.worktreePath } : {}),
213 updatedAt: occurredAt,
214 },
215 };
216 }
217​
218 case "thread.runtime-mode.set": {
219 yield* requireThread({
220 readModel,
221 command,
222 threadId: command.threadId,
223 });
224 const occurredAt = nowIso();
225 return {
226 ...withEventBase({
227 aggregateKind: "thread",
228 aggregateId: command.threadId,
229 occurredAt,
230 commandId: command.commandId,
231 }),
232 type: "thread.runtime-mode-set",
233 payload: {
234 threadId: command.threadId,
235 runtimeMode: command.runtimeMode,
236 updatedAt: occurredAt,
237 },
238 };
239 }
240​
241 case "thread.interaction-mode.set": {
242 yield* requireThread({
243 readModel,
244 command,
245 threadId: command.threadId,
246 });
247 const occurredAt = nowIso();
248 return {
249 ...withEventBase({
250 aggregateKind: "thread",
251 aggregateId: command.threadId,
252 occurredAt,
253 commandId: command.commandId,
254 }),
255 type: "thread.interaction-mode-set",
256 payload: {
257 threadId: command.threadId,
258 interactionMode: command.interactionMode,
259 updatedAt: occurredAt,
260 },
261 };
262 }
263​
264 case "thread.turn.start": {
265 yield* requireThread({
266 readModel,
267 command,
268 threadId: command.threadId,
269 });
270 const userMessageEvent: Omit<OrchestrationEvent, "sequence"> = {
271 ...withEventBase({
272 aggregateKind: "thread",
273 aggregateId: command.threadId,
274 occurredAt: command.createdAt,
275 commandId: command.commandId,
276 }),
277 type: "thread.message-sent",
278 payload: {
279 threadId: command.threadId,
280 messageId: command.message.messageId,
281 role: "user",
282 text: command.message.text,
283 attachments: command.message.attachments,
284 turnId: null,
285 streaming: false,
286 createdAt: command.createdAt,
287 updatedAt: command.createdAt,
288 },
289 };
290 const turnStartRequestedEvent: Omit<OrchestrationEvent, "sequence"> = {
291 ...withEventBase({
292 aggregateKind: "thread",
293 aggregateId: command.threadId,
294 occurredAt: command.createdAt,
295 commandId: command.commandId,
296 }),
297 causationEventId: userMessageEvent.eventId,
298 type: "thread.turn-start-requested",
299 payload: {
300 threadId: command.threadId,
301 messageId: command.message.messageId,
302 ...(command.provider !== undefined ? { provider: command.provider } : {}),
303 ...(command.model !== undefined ? { model: command.model } : {}),
304 ...(command.modelOptions !== undefined ? { modelOptions: command.modelOptions } : {}),
305 assistantDeliveryMode: command.assistantDeliveryMode ?? DEFAULT_ASSISTANT_DELIVERY_MODE,
306 runtimeMode:
307 readModel.threads.find((entry) => entry.id === command.threadId)?.runtimeMode ??
308 command.runtimeMode,
309 interactionMode:
310 readModel.threads.find((entry) => entry.id === command.threadId)?.interactionMode ??
311 command.interactionMode,
312 createdAt: command.createdAt,
313 },
314 };
315 return [userMessageEvent, turnStartRequestedEvent];
316 }
317​
318 case "thread.turn.interrupt": {
319 yield* requireThread({
320 readModel,
321 command,
322 threadId: command.threadId,
323 });
324 return {
325 ...withEventBase({
326 aggregateKind: "thread",
327 aggregateId: command.threadId,
328 occurredAt: command.createdAt,
329 commandId: command.commandId,
330 }),
331 type: "thread.turn-interrupt-requested",
332 payload: {
333 threadId: command.threadId,
334 ...(command.turnId !== undefined ? { turnId: command.turnId } : {}),
335 createdAt: command.createdAt,
336 },
337 };
338 }
339​
340 case "thread.approval.respond": {
341 yield* requireThread({
342 readModel,
343 command,
344 threadId: command.threadId,
345 });
346 return {
347 ...withEventBase({
348 aggregateKind: "thread",
349 aggregateId: command.threadId,
350 occurredAt: command.createdAt,
351 commandId: command.commandId,
352 metadata: {
353 requestId: command.requestId,
354 },
355 }),
356 type: "thread.approval-response-requested",
357 payload: {
358 threadId: command.threadId,
359 requestId: command.requestId,
360 decision: command.decision,
361 createdAt: command.createdAt,
362 },
363 };
364 }
365​
366 case "thread.user-input.respond": {
367 yield* requireThread({
368 readModel,
369 command,
370 threadId: command.threadId,
371 });
372 return {
373 ...withEventBase({
374 aggregateKind: "thread",
375 aggregateId: command.threadId,
376 occurredAt: command.createdAt,
377 commandId: command.commandId,
378 metadata: {
379 requestId: command.requestId,
380 },
381 }),
382 type: "thread.user-input-response-requested",
383 payload: {
384 threadId: command.threadId,
385 requestId: command.requestId,
386 answers: command.answers,
387 createdAt: command.createdAt,
388 },
389 };
390 }
391​
392 case "thread.checkpoint.revert": {
393 yield* requireThread({
394 readModel,
395 command,
396 threadId: command.threadId,
397 });
398 return {
399 ...withEventBase({
400 aggregateKind: "thread",
401 aggregateId: command.threadId,
402 occurredAt: command.createdAt,
403 commandId: command.commandId,
404 }),
405 type: "thread.checkpoint-revert-requested",
406 payload: {
407 threadId: command.threadId,
408 turnCount: command.turnCount,
409 createdAt: command.createdAt,
410 },
411 };
412 }
413​
414 case "thread.session.stop": {
415 yield* requireThread({
416 readModel,
417 command,
418 threadId: command.threadId,
419 });
420 return {
421 ...withEventBase({
422 aggregateKind: "thread",
423 aggregateId: command.threadId,
424 occurredAt: command.createdAt,
425 commandId: command.commandId,
426 }),
427 type: "thread.session-stop-requested",
428 payload: {
429 threadId: command.threadId,
430 createdAt: command.createdAt,
431 },
432 };
433 }
434​
435 case "thread.session.set": {
436 yield* requireThread({
437 readModel,
438 command,
439 threadId: command.threadId,
440 });
441 return {
442 ...withEventBase({
443 aggregateKind: "thread",
444 aggregateId: command.threadId,
445 occurredAt: command.createdAt,
446 commandId: command.commandId,
447 metadata: {},
448 }),
449 type: "thread.session-set",
450 payload: {
451 threadId: command.threadId,
452 session: command.session,
453 },
454 };
455 }
456​
457 case "thread.message.assistant.delta": {
458 yield* requireThread({
459 readModel,
460 command,
461 threadId: command.threadId,
462 });
463 return {
464 ...withEventBase({
465 aggregateKind: "thread",
466 aggregateId: command.threadId,
467 occurredAt: command.createdAt,
468 commandId: command.commandId,
469 }),
470 type: "thread.message-sent",
471 payload: {
472 threadId: command.threadId,
473 messageId: command.messageId,
474 role: "assistant",
475 text: command.delta,
476 turnId: command.turnId ?? null,
477 streaming: true,
478 createdAt: command.createdAt,
479 updatedAt: command.createdAt,
480 },
481 };
482 }
483​
484 case "thread.message.assistant.complete": {
485 yield* requireThread({
486 readModel,
487 command,
488 threadId: command.threadId,
489 });
490 return {
491 ...withEventBase({
492 aggregateKind: "thread",
493 aggregateId: command.threadId,
494 occurredAt: command.createdAt,
495 commandId: command.commandId,
496 }),
497 type: "thread.message-sent",
498 payload: {
499 threadId: command.threadId,
500 messageId: command.messageId,
501 role: "assistant",
502 text: "",
503 turnId: command.turnId ?? null,
504 streaming: false,
505 createdAt: command.createdAt,
506 updatedAt: command.createdAt,
507 },
508 };
509 }
510​
511 case "thread.proposed-plan.upsert": {
512 yield* requireThread({
513 readModel,
514 command,
515 threadId: command.threadId,
516 });
517 return {
518 ...withEventBase({
519 aggregateKind: "thread",
520 aggregateId: command.threadId,
521 occurredAt: command.createdAt,
522 commandId: command.commandId,
523 }),
524 type: "thread.proposed-plan-upserted",
525 payload: {
526 threadId: command.threadId,
527 proposedPlan: command.proposedPlan,
528 },
529 };
530 }
531​
532 case "thread.turn.diff.complete": {
533 yield* requireThread({
534 readModel,
535 command,
536 threadId: command.threadId,
537 });
538 return {
539 ...withEventBase({
540 aggregateKind: "thread",
541 aggregateId: command.threadId,
542 occurredAt: command.createdAt,
543 commandId: command.commandId,
544 }),
545 type: "thread.turn-diff-completed",
546 payload: {
547 threadId: command.threadId,
548 turnId: command.turnId,
549 checkpointTurnCount: command.checkpointTurnCount,
550 checkpointRef: command.checkpointRef,
551 status: command.status,
552 files: command.files,
553 assistantMessageId: command.assistantMessageId ?? null,
554 completedAt: command.completedAt,
555 },
556 };
557 }
558​
559 case "thread.revert.complete": {
560 yield* requireThread({
561 readModel,
562 command,
563 threadId: command.threadId,
564 });
565 return {
566 ...withEventBase({
567 aggregateKind: "thread",
568 aggregateId: command.threadId,
569 occurredAt: command.createdAt,
570 commandId: command.commandId,
571 }),
572 type: "thread.reverted",
573 payload: {
574 threadId: command.threadId,
575 turnCount: command.turnCount,
576 },
577 };
578 }
579​
580 case "thread.activity.append": {
581 yield* requireThread({
582 readModel,
583 command,
584 threadId: command.threadId,
585 });
586 const requestId =
587 typeof command.activity.payload === "object" &&
588 command.activity.payload !== null &&
589 "requestId" in command.activity.payload &&
590 typeof (command.activity.payload as { requestId?: unknown }).requestId === "string"
591 ? ((command.activity.payload as { requestId: string })
592 .requestId as OrchestrationEvent["metadata"]["requestId"])
593 : undefined;
594 return {
595 ...withEventBase({
596 aggregateKind: "thread",
597 aggregateId: command.threadId,
598 occurredAt: command.createdAt,
599 commandId: command.commandId,
600 ...(requestId !== undefined ? { metadata: { requestId } } : {}),
601 }),
602 type: "thread.activity-appended",
603 payload: {
604 threadId: command.threadId,
605 activity: command.activity,
606 },
607 };
608 }
609​
610 default: {
611 command satisfies never;
612 const fallback = command as never as { type: string };
613 return yield* new OrchestrationCommandInvariantError({
614 commandType: fallback.type,
615 detail: `Unknown command type: ${fallback.type}`,
616 });
617 }
618 }
619});