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

decider.ts

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