642 lines · 18.2 KB · TypeScript
RawDownload
import type {  OrchestrationCommand,  OrchestrationEvent,  OrchestrationReadModel,} from "@t3tools/contracts";import { Effect } from "effect"; import { OrchestrationCommandInvariantError } from "./Errors.ts";import {  requireProject,  requireProjectAbsent,  requireThread,  requireThreadAbsent,} from "./commandInvariants.ts"; const nowIso = () => new Date().toISOString(); const defaultMetadata: Omit<OrchestrationEvent, "sequence" | "type" | "payload"> = {  eventId: crypto.randomUUID() as OrchestrationEvent["eventId"],  aggregateKind: "thread",  aggregateId: "" as OrchestrationEvent["aggregateId"],  occurredAt: nowIso(),  commandId: null,  causationEventId: null,  correlationId: null,  metadata: {},}; function withEventBase(  input: Pick<OrchestrationCommand, "commandId"> & {    readonly aggregateKind: OrchestrationEvent["aggregateKind"];    readonly aggregateId: OrchestrationEvent["aggregateId"];    readonly occurredAt: string;    readonly metadata?: OrchestrationEvent["metadata"];  },): Omit<OrchestrationEvent, "sequence" | "type" | "payload"> {  return {    ...defaultMetadata,    eventId: crypto.randomUUID() as OrchestrationEvent["eventId"],    aggregateKind: input.aggregateKind,    aggregateId: input.aggregateId,    occurredAt: input.occurredAt,    commandId: input.commandId,    correlationId: input.commandId,    metadata: input.metadata ?? {},  };} export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand")(function* ({  command,  readModel,}: {  readonly command: OrchestrationCommand;  readonly readModel: OrchestrationReadModel;}): Effect.fn.Return<  Omit<OrchestrationEvent, "sequence"> | ReadonlyArray<Omit<OrchestrationEvent, "sequence">>,  OrchestrationCommandInvariantError> {  switch (command.type) {    case "project.create": {      yield* requireProjectAbsent({        readModel,        command,        projectId: command.projectId,      });       return {        ...withEventBase({          aggregateKind: "project",          aggregateId: command.projectId,          occurredAt: command.createdAt,          commandId: command.commandId,        }),        type: "project.created",        payload: {          projectId: command.projectId,          title: command.title,          workspaceRoot: command.workspaceRoot,          defaultModelSelection: command.defaultModelSelection ?? null,          scripts: [],          createdAt: command.createdAt,          updatedAt: command.createdAt,        },      };    }     case "project.meta.update": {      yield* requireProject({        readModel,        command,        projectId: command.projectId,      });      const occurredAt = nowIso();      return {        ...withEventBase({          aggregateKind: "project",          aggregateId: command.projectId,          occurredAt,          commandId: command.commandId,        }),        type: "project.meta-updated",        payload: {          projectId: command.projectId,          ...(command.title !== undefined ? { title: command.title } : {}),          ...(command.workspaceRoot !== undefined ? { workspaceRoot: command.workspaceRoot } : {}),          ...(command.defaultModelSelection !== undefined            ? { defaultModelSelection: command.defaultModelSelection }            : {}),          ...(command.scripts !== undefined ? { scripts: command.scripts } : {}),          updatedAt: occurredAt,        },      };    }     case "project.delete": {      yield* requireProject({        readModel,        command,        projectId: command.projectId,      });      const occurredAt = nowIso();      return {        ...withEventBase({          aggregateKind: "project",          aggregateId: command.projectId,          occurredAt,          commandId: command.commandId,        }),        type: "project.deleted",        payload: {          projectId: command.projectId,          deletedAt: occurredAt,        },      };    }     case "thread.create": {      yield* requireProject({        readModel,        command,        projectId: command.projectId,      });      yield* requireThreadAbsent({        readModel,        command,        threadId: command.threadId,      });      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,        }),        type: "thread.created",        payload: {          threadId: command.threadId,          projectId: command.projectId,          title: command.title,          modelSelection: command.modelSelection,          runtimeMode: command.runtimeMode,          interactionMode: command.interactionMode,          branch: command.branch,          worktreePath: command.worktreePath,          createdAt: command.createdAt,          updatedAt: command.createdAt,        },      };    }     case "thread.delete": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      const occurredAt = nowIso();      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt,          commandId: command.commandId,        }),        type: "thread.deleted",        payload: {          threadId: command.threadId,          deletedAt: occurredAt,        },      };    }     case "thread.meta.update": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      const occurredAt = nowIso();      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt,          commandId: command.commandId,        }),        type: "thread.meta-updated",        payload: {          threadId: command.threadId,          ...(command.title !== undefined ? { title: command.title } : {}),          ...(command.modelSelection !== undefined            ? { modelSelection: command.modelSelection }            : {}),          ...(command.branch !== undefined ? { branch: command.branch } : {}),          ...(command.worktreePath !== undefined ? { worktreePath: command.worktreePath } : {}),          updatedAt: occurredAt,        },      };    }     case "thread.runtime-mode.set": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      const occurredAt = nowIso();      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt,          commandId: command.commandId,        }),        type: "thread.runtime-mode-set",        payload: {          threadId: command.threadId,          runtimeMode: command.runtimeMode,          updatedAt: occurredAt,        },      };    }     case "thread.interaction-mode.set": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      const occurredAt = nowIso();      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt,          commandId: command.commandId,        }),        type: "thread.interaction-mode-set",        payload: {          threadId: command.threadId,          interactionMode: command.interactionMode,          updatedAt: occurredAt,        },      };    }     case "thread.turn.start": {      const targetThread = yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      const sourceProposedPlan = command.sourceProposedPlan;      const sourceThread = sourceProposedPlan        ? yield* requireThread({            readModel,            command,            threadId: sourceProposedPlan.threadId,          })        : null;      const sourcePlan =        sourceProposedPlan && sourceThread          ? sourceThread.proposedPlans.find((entry) => entry.id === sourceProposedPlan.planId)          : null;      if (sourceProposedPlan && !sourcePlan) {        return yield* new OrchestrationCommandInvariantError({          commandType: command.type,          detail: `Proposed plan '${sourceProposedPlan.planId}' does not exist on thread '${sourceProposedPlan.threadId}'.`,        });      }      if (sourceThread && sourceThread.projectId !== targetThread.projectId) {        return yield* new OrchestrationCommandInvariantError({          commandType: command.type,          detail: `Proposed plan '${sourceProposedPlan?.planId}' belongs to thread '${sourceThread.id}' in a different project.`,        });      }      const userMessageEvent: Omit<OrchestrationEvent, "sequence"> = {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,        }),        type: "thread.message-sent",        payload: {          threadId: command.threadId,          messageId: command.message.messageId,          role: "user",          text: command.message.text,          attachments: command.message.attachments,          turnId: null,          streaming: false,          createdAt: command.createdAt,          updatedAt: command.createdAt,        },      };      const turnStartRequestedEvent: Omit<OrchestrationEvent, "sequence"> = {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,        }),        causationEventId: userMessageEvent.eventId,        type: "thread.turn-start-requested",        payload: {          threadId: command.threadId,          messageId: command.message.messageId,          ...(command.modelSelection !== undefined            ? { modelSelection: command.modelSelection }            : {}),          runtimeMode: targetThread.runtimeMode,          interactionMode: targetThread.interactionMode,          ...(sourceProposedPlan !== undefined ? { sourceProposedPlan } : {}),          createdAt: command.createdAt,        },      };      return [userMessageEvent, turnStartRequestedEvent];    }     case "thread.turn.interrupt": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,        }),        type: "thread.turn-interrupt-requested",        payload: {          threadId: command.threadId,          ...(command.turnId !== undefined ? { turnId: command.turnId } : {}),          createdAt: command.createdAt,        },      };    }     case "thread.approval.respond": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,          metadata: {            requestId: command.requestId,          },        }),        type: "thread.approval-response-requested",        payload: {          threadId: command.threadId,          requestId: command.requestId,          decision: command.decision,          createdAt: command.createdAt,        },      };    }     case "thread.user-input.respond": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,          metadata: {            requestId: command.requestId,          },        }),        type: "thread.user-input-response-requested",        payload: {          threadId: command.threadId,          requestId: command.requestId,          answers: command.answers,          createdAt: command.createdAt,        },      };    }     case "thread.checkpoint.revert": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,        }),        type: "thread.checkpoint-revert-requested",        payload: {          threadId: command.threadId,          turnCount: command.turnCount,          createdAt: command.createdAt,        },      };    }     case "thread.session.stop": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,        }),        type: "thread.session-stop-requested",        payload: {          threadId: command.threadId,          createdAt: command.createdAt,        },      };    }     case "thread.session.set": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,          metadata: {},        }),        type: "thread.session-set",        payload: {          threadId: command.threadId,          session: command.session,        },      };    }     case "thread.message.assistant.delta": {      yield* requireThread({        readModel,        command,        threadId: command.threadId,      });      return {        ...withEventBase({          aggregateKind: "thread",          aggregateId: command.threadId,          occurredAt: command.createdAt,          commandId: command.commandId,        }),        type: "thread.message-sent",        payload: {          threadId: command.threadId,          messageId: command.messageId,          role: "assistant",          text: command.delta,          turnId: command.turnId ?? null,          streaming: true,

Lines 1–500 of 642.