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

decider.ts

620 lines17.5 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.serviceTier !== undefined ? { serviceTier: command.serviceTier } : {}),
305 ...(command.modelOptions !== undefined ? { modelOptions: command.modelOptions } : {}),
306 assistantDeliveryMode: command.assistantDeliveryMode ?? DEFAULT_ASSISTANT_DELIVERY_MODE,
307 runtimeMode:
308 readModel.threads.find((entry) => entry.id === command.threadId)?.runtimeMode ??
309 command.runtimeMode,
310 interactionMode:
311 readModel.threads.find((entry) => entry.id === command.threadId)?.interactionMode ??
312 command.interactionMode,
313 createdAt: command.createdAt,
314 },
315 };
316 return [userMessageEvent, turnStartRequestedEvent];
317 }
318​
319 case "thread.turn.interrupt": {
320 yield* requireThread({
321 readModel,
322 command,
323 threadId: command.threadId,
324 });
325 return {
326 ...withEventBase({
327 aggregateKind: "thread",
328 aggregateId: command.threadId,
329 occurredAt: command.createdAt,
330 commandId: command.commandId,
331 }),
332 type: "thread.turn-interrupt-requested",
333 payload: {
334 threadId: command.threadId,
335 ...(command.turnId !== undefined ? { turnId: command.turnId } : {}),
336 createdAt: command.createdAt,
337 },
338 };
339 }
340​
341 case "thread.approval.respond": {
342 yield* requireThread({
343 readModel,
344 command,
345 threadId: command.threadId,
346 });
347 return {
348 ...withEventBase({
349 aggregateKind: "thread",
350 aggregateId: command.threadId,
351 occurredAt: command.createdAt,
352 commandId: command.commandId,
353 metadata: {
354 requestId: command.requestId,
355 },
356 }),
357 type: "thread.approval-response-requested",
358 payload: {
359 threadId: command.threadId,
360 requestId: command.requestId,
361 decision: command.decision,
362 createdAt: command.createdAt,
363 },
364 };
365 }
366​
367 case "thread.user-input.respond": {
368 yield* requireThread({
369 readModel,
370 command,
371 threadId: command.threadId,
372 });
373 return {
374 ...withEventBase({
375 aggregateKind: "thread",
376 aggregateId: command.threadId,
377 occurredAt: command.createdAt,
378 commandId: command.commandId,
379 metadata: {
380 requestId: command.requestId,
381 },
382 }),
383 type: "thread.user-input-response-requested",
384 payload: {
385 threadId: command.threadId,
386 requestId: command.requestId,
387 answers: command.answers,
388 createdAt: command.createdAt,
389 },
390 };
391 }
392​
393 case "thread.checkpoint.revert": {
394 yield* requireThread({
395 readModel,
396 command,
397 threadId: command.threadId,
398 });
399 return {
400 ...withEventBase({
401 aggregateKind: "thread",
402 aggregateId: command.threadId,
403 occurredAt: command.createdAt,
404 commandId: command.commandId,
405 }),
406 type: "thread.checkpoint-revert-requested",
407 payload: {
408 threadId: command.threadId,
409 turnCount: command.turnCount,
410 createdAt: command.createdAt,
411 },
412 };
413 }
414​
415 case "thread.session.stop": {
416 yield* requireThread({
417 readModel,
418 command,
419 threadId: command.threadId,
420 });
421 return {
422 ...withEventBase({
423 aggregateKind: "thread",
424 aggregateId: command.threadId,
425 occurredAt: command.createdAt,
426 commandId: command.commandId,
427 }),
428 type: "thread.session-stop-requested",
429 payload: {
430 threadId: command.threadId,
431 createdAt: command.createdAt,
432 },
433 };
434 }
435​
436 case "thread.session.set": {
437 yield* requireThread({
438 readModel,
439 command,
440 threadId: command.threadId,
441 });
442 return {
443 ...withEventBase({
444 aggregateKind: "thread",
445 aggregateId: command.threadId,
446 occurredAt: command.createdAt,
447 commandId: command.commandId,
448 metadata: {},
449 }),
450 type: "thread.session-set",
451 payload: {
452 threadId: command.threadId,
453 session: command.session,
454 },
455 };
456 }
457​
458 case "thread.message.assistant.delta": {
459 yield* requireThread({
460 readModel,
461 command,
462 threadId: command.threadId,
463 });
464 return {
465 ...withEventBase({
466 aggregateKind: "thread",
467 aggregateId: command.threadId,
468 occurredAt: command.createdAt,
469 commandId: command.commandId,
470 }),
471 type: "thread.message-sent",
472 payload: {
473 threadId: command.threadId,
474 messageId: command.messageId,
475 role: "assistant",
476 text: command.delta,
477 turnId: command.turnId ?? null,
478 streaming: true,
479 createdAt: command.createdAt,
480 updatedAt: command.createdAt,
481 },
482 };
483 }
484​
485 case "thread.message.assistant.complete": {
486 yield* requireThread({
487 readModel,
488 command,
489 threadId: command.threadId,
490 });
491 return {
492 ...withEventBase({
493 aggregateKind: "thread",
494 aggregateId: command.threadId,
495 occurredAt: command.createdAt,
496 commandId: command.commandId,
497 }),
498 type: "thread.message-sent",
499 payload: {
500 threadId: command.threadId,
501 messageId: command.messageId,
502 role: "assistant",
503 text: "",
504 turnId: command.turnId ?? null,
505 streaming: false,
506 createdAt: command.createdAt,
507 updatedAt: command.createdAt,
508 },
509 };
510 }
511​
512 case "thread.proposed-plan.upsert": {
513 yield* requireThread({
514 readModel,
515 command,
516 threadId: command.threadId,
517 });
518 return {
519 ...withEventBase({
520 aggregateKind: "thread",
521 aggregateId: command.threadId,
522 occurredAt: command.createdAt,
523 commandId: command.commandId,
524 }),
525 type: "thread.proposed-plan-upserted",
526 payload: {
527 threadId: command.threadId,
528 proposedPlan: command.proposedPlan,
529 },
530 };
531 }
532​
533 case "thread.turn.diff.complete": {
534 yield* requireThread({
535 readModel,
536 command,
537 threadId: command.threadId,
538 });
539 return {
540 ...withEventBase({
541 aggregateKind: "thread",
542 aggregateId: command.threadId,
543 occurredAt: command.createdAt,
544 commandId: command.commandId,
545 }),
546 type: "thread.turn-diff-completed",
547 payload: {
548 threadId: command.threadId,
549 turnId: command.turnId,
550 checkpointTurnCount: command.checkpointTurnCount,
551 checkpointRef: command.checkpointRef,
552 status: command.status,
553 files: command.files,
554 assistantMessageId: command.assistantMessageId ?? null,
555 completedAt: command.completedAt,
556 },
557 };
558 }
559​
560 case "thread.revert.complete": {
561 yield* requireThread({
562 readModel,
563 command,
564 threadId: command.threadId,
565 });
566 return {
567 ...withEventBase({
568 aggregateKind: "thread",
569 aggregateId: command.threadId,
570 occurredAt: command.createdAt,
571 commandId: command.commandId,
572 }),
573 type: "thread.reverted",
574 payload: {
575 threadId: command.threadId,
576 turnCount: command.turnCount,
577 },
578 };
579 }
580​
581 case "thread.activity.append": {
582 yield* requireThread({
583 readModel,
584 command,
585 threadId: command.threadId,
586 });
587 const requestId =
588 typeof command.activity.payload === "object" &&
589 command.activity.payload !== null &&
590 "requestId" in command.activity.payload &&
591 typeof (command.activity.payload as { requestId?: unknown }).requestId === "string"
592 ? ((command.activity.payload as { requestId: string })
593 .requestId as OrchestrationEvent["metadata"]["requestId"])
594 : undefined;
595 return {
596 ...withEventBase({
597 aggregateKind: "thread",
598 aggregateId: command.threadId,
599 occurredAt: command.createdAt,
600 commandId: command.commandId,
601 ...(requestId !== undefined ? { metadata: { requestId } } : {}),
602 }),
603 type: "thread.activity-appended",
604 payload: {
605 threadId: command.threadId,
606 activity: command.activity,
607 },
608 };
609 }
610​
611 default: {
612 command satisfies never;
613 const fallback = command as never as { type: string };
614 return yield* new OrchestrationCommandInvariantError({
615 commandType: fallback.type,
616 detail: `Unknown command type: ${fallback.type}`,
617 });
618 }
619 }
620});