Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

26.4 KB
Raw Blame
import {
  OrchestrationReadModel,
  OrchestrationThread,
  type OrchestrationCheckpointSummary,
  type OrchestrationLatestTurn,
  type OrchestrationMessage,
  type OrchestrationProposedPlan,
  type OrchestrationProject,
  type OrchestrationSession,
  type OrchestrationThreadActivity,
} from "@akeru/contracts";
import * as Effect from "effect/Effect";
import {
  isPersistenceError,
  toPersistenceDecodeError,
  toPersistenceSqlError,
} from "../../../persistence/Errors.ts";
import { type ProjectionSnapshotQueryShape } from "../../Services/ProjectionSnapshotQuery.ts";
import {
  type ProjectionSnapshotDependencies,
  toPersistenceSqlOrDecodeError,
  maxIso,
  mapLatestTurn,
  mapSessionRow,
  mapTitleRegeneration,
  computeSnapshotSequence,
  mapBotRow,
  mapGroupRow,
  decodeReadModel,
  mapProposedPlanRow,
} from "../ProjectionSnapshotRows.ts";
import type { createEnvironmentRows } from "./EnvironmentRows.ts";
import type { createProjectIdentity } from "./ProjectIdentity.ts";

export function createReadModels({
  sql,
  listProjectRows,
  listBotRows,
  listGroupRows,
  listDelegationRows,
  projectionMcpServerRepository,
  routineRepository,
  listThreadRows,
  listThreadMessageRows,
  listThreadProposedPlanRows,
  listThreadActivityRows,
  listThreadSessionRows,
  listCheckpointRows,
  listLatestTurnRows,
  listProjectionStateRows,
  resolveRepositoryIdentitiesForProjects,
}: Pick<
  ProjectionSnapshotDependencies &
    ReturnType<typeof createEnvironmentRows> &
    ReturnType<typeof createProjectIdentity>,
  | "sql"
  | "listProjectRows"
  | "listBotRows"
  | "listGroupRows"
  | "listDelegationRows"
  | "projectionMcpServerRepository"
  | "routineRepository"
  | "listThreadRows"
  | "listThreadMessageRows"
  | "listThreadProposedPlanRows"
  | "listThreadActivityRows"
  | "listThreadSessionRows"
  | "listCheckpointRows"
  | "listLatestTurnRows"
  | "listProjectionStateRows"
  | "resolveRepositoryIdentitiesForProjects"
>) {
  const getSnapshot: ProjectionSnapshotQueryShape["getSnapshot"] = () =>
    sql
      .withTransaction(
        Effect.all([
          listProjectRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listProjects:query",
                "ProjectionSnapshotQuery.getSnapshot:listProjects:decodeRows",
              ),
            ),
          ),
          listBotRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listBots:query",
                "ProjectionSnapshotQuery.getSnapshot:listBots:decodeRows",
              ),
            ),
          ),
          listGroupRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listGroups:query",
                "ProjectionSnapshotQuery.getSnapshot:listGroups:decodeRows",
              ),
            ),
          ),
          listDelegationRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listDelegations:query",
                "ProjectionSnapshotQuery.getSnapshot:listDelegations:decodeRows",
              ),
            ),
          ),
          projectionMcpServerRepository.listAll(),
          routineRepository.listAll,
          routineRepository.listAllRuns,
          routineRepository.listSkillAssignments,
          listThreadRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listThreads:query",
                "ProjectionSnapshotQuery.getSnapshot:listThreads:decodeRows",
              ),
            ),
          ),
          listThreadMessageRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listThreadMessages:query",
                "ProjectionSnapshotQuery.getSnapshot:listThreadMessages:decodeRows",
              ),
            ),
          ),
          listThreadProposedPlanRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listThreadProposedPlans:query",
                "ProjectionSnapshotQuery.getSnapshot:listThreadProposedPlans:decodeRows",
              ),
            ),
          ),
          listThreadActivityRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listThreadActivities:query",
                "ProjectionSnapshotQuery.getSnapshot:listThreadActivities:decodeRows",
              ),
            ),
          ),
          listThreadSessionRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listThreadSessions:query",
                "ProjectionSnapshotQuery.getSnapshot:listThreadSessions:decodeRows",
              ),
            ),
          ),
          listCheckpointRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listCheckpoints:query",
                "ProjectionSnapshotQuery.getSnapshot:listCheckpoints:decodeRows",
              ),
            ),
          ),
          listLatestTurnRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listLatestTurns:query",
                "ProjectionSnapshotQuery.getSnapshot:listLatestTurns:decodeRows",
              ),
            ),
          ),
          listProjectionStateRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getSnapshot:listProjectionState:query",
                "ProjectionSnapshotQuery.getSnapshot:listProjectionState:decodeRows",
              ),
            ),
          ),
        ]),
      )
      .pipe(
        Effect.flatMap(
          ([
            projectRows,
            botRows,
            groupRows,
            delegationRows,
            mcpServers,
            routines,
            routineRuns,
            skillAssignments,
            threadRows,
            messageRows,
            proposedPlanRows,
            activityRows,
            sessionRows,
            checkpointRows,
            latestTurnRows,
            stateRows,
          ]) =>
            Effect.gen(function* () {
              const messagesByThread = new Map<string, Array<OrchestrationMessage>>();
              const proposedPlansByThread = new Map<string, Array<OrchestrationProposedPlan>>();
              const activitiesByThread = new Map<string, Array<OrchestrationThreadActivity>>();
              const checkpointsByThread = new Map<string, Array<OrchestrationCheckpointSummary>>();
              const sessionsByThread = new Map<string, OrchestrationSession>();
              const latestTurnByThread = new Map<string, OrchestrationLatestTurn>();

              let updatedAt: string | null = null;

              for (const row of projectRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (const row of botRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (const row of groupRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (const row of delegationRows) {
                updatedAt = maxIso(updatedAt, row.delegation.updatedAt);
              }

              for (const mcpServer of mcpServers) {
                updatedAt = maxIso(updatedAt, mcpServer.updatedAt);
              }

              for (const row of threadRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (const row of stateRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (const row of messageRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
                const threadMessages = messagesByThread.get(row.threadId) ?? [];
                threadMessages.push({
                  id: row.messageId,
                  role: row.role,
                  text: row.text,
                  ...(row.attachments !== null ? { attachments: row.attachments } : {}),
                  turnId: row.turnId,
                  respondingBotId: row.respondingBotId ?? null,
                  ...(row.authorPersonId ? { authorPersonId: row.authorPersonId } : {}),
                  ...(row.authorDisplayName ? { authorDisplayName: row.authorDisplayName } : {}),
                  ...(row.channelOrigin ? { channelOrigin: row.channelOrigin } : {}),
                  ...(row.channelDelivery ? { channelDelivery: row.channelDelivery } : {}),
                  reactions: row.reactions,
                  streaming: row.isStreaming === 1,
                  createdAt: row.createdAt,
                  updatedAt: row.updatedAt,
                });
                messagesByThread.set(row.threadId, threadMessages);
              }

              for (const row of proposedPlanRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
                const threadProposedPlans = proposedPlansByThread.get(row.threadId) ?? [];
                threadProposedPlans.push({
                  id: row.planId,
                  turnId: row.turnId,
                  planMarkdown: row.planMarkdown,
                  implementedAt: row.implementedAt,
                  implementationThreadId: row.implementationThreadId,
                  createdAt: row.createdAt,
                  updatedAt: row.updatedAt,
                });
                proposedPlansByThread.set(row.threadId, threadProposedPlans);
              }

              for (const row of activityRows) {
                updatedAt = maxIso(updatedAt, row.createdAt);
                const threadActivities = activitiesByThread.get(row.threadId) ?? [];
                threadActivities.push({
                  id: row.activityId,
                  tone: row.tone,
                  kind: row.kind,
                  summary: row.summary,
                  payload: row.payload,
                  turnId: row.turnId,
                  ...(row.sequence !== null ? { sequence: row.sequence } : {}),
                  createdAt: row.createdAt,
                });
                activitiesByThread.set(row.threadId, threadActivities);
              }

              for (const row of checkpointRows) {
                updatedAt = maxIso(updatedAt, row.completedAt);
                const threadCheckpoints = checkpointsByThread.get(row.threadId) ?? [];
                threadCheckpoints.push({
                  turnId: row.turnId,
                  checkpointTurnCount: row.checkpointTurnCount,
                  checkpointRef: row.checkpointRef,
                  status: row.status,
                  files: row.files,
                  assistantMessageId: row.assistantMessageId,
                  completedAt: row.completedAt,
                });
                checkpointsByThread.set(row.threadId, threadCheckpoints);
              }

              for (const row of latestTurnRows) {
                updatedAt = maxIso(updatedAt, row.requestedAt);

                if (row.startedAt !== null) {
                  updatedAt = maxIso(updatedAt, row.startedAt);
                }

                if (row.completedAt !== null) {
                  updatedAt = maxIso(updatedAt, row.completedAt);
                }

                if (latestTurnByThread.has(row.threadId)) {
                  continue;
                }

                latestTurnByThread.set(row.threadId, mapLatestTurn(row));
              }

              for (const row of sessionRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
                sessionsByThread.set(row.threadId, mapSessionRow(row));
              }

              const repositoryIdentities = yield* resolveRepositoryIdentitiesForProjects(
                projectRows,
                { includeDeleted: true },
              );

              const projects: ReadonlyArray<OrchestrationProject> = projectRows.map((row) => ({
                id: row.projectId,
                title: row.title,
                workspaceRoot: row.workspaceRoot,
                repositoryIdentity: repositoryIdentities.get(row.projectId) ?? null,
                defaultModelSelection: row.defaultModelSelection,
                defaultThreadEnvMode: row.defaultThreadEnvMode,
                faviconPath: row.faviconPath ?? null,
                scripts: row.scripts,
                createdAt: row.createdAt,
                updatedAt: row.updatedAt,
                deletedAt: row.deletedAt,
              }));

              const threads: ReadonlyArray<OrchestrationThread> = threadRows.map((row) => ({
                id: row.threadId,
                projectId: row.projectId,
                botId: row.botId,
                groupId: row.groupId,
                parentThreadId: row.parentThreadId ?? null,
                parentDelegationId: row.parentDelegationId ?? null,
                respondingBotId: row.respondingBotId ?? null,
                title: row.title,
                modelSelection: row.modelSelection,
                runtimeMode: row.runtimeMode,
                interactionMode: row.interactionMode,
                branch: row.branch,
                worktreePath: row.worktreePath,
                ...(row.linkedPullRequest === null
                  ? {}
                  : { linkedPullRequest: row.linkedPullRequest }),
                latestTurn: latestTurnByThread.get(row.threadId) ?? null,
                createdAt: row.createdAt,
                updatedAt: row.updatedAt,
                archivedAt: row.archivedAt,
                settledOverride: row.settledOverride,
                settledAt: row.settledAt,
                unsettledAt: row.unsettledAt,
                snoozedUntil: row.snoozedUntil,
                snoozedAt: row.snoozedAt,
                pinnedAt: row.pinnedAt,
                pinOrderKey: row.pinOrderKey ?? null,
                titleRegeneration: mapTitleRegeneration(row),
                deletedAt: row.deletedAt,
                messages: messagesByThread.get(row.threadId) ?? [],
                proposedPlans: proposedPlansByThread.get(row.threadId) ?? [],
                activities: activitiesByThread.get(row.threadId) ?? [],
                checkpoints: checkpointsByThread.get(row.threadId) ?? [],
                session: sessionsByThread.get(row.threadId) ?? null,
              }));

              const snapshot = {
                snapshotSequence: computeSnapshotSequence(stateRows),
                projects,
                bots: botRows.map(mapBotRow),
                groups: groupRows.map(mapGroupRow),
                delegations: delegationRows.map((row) => row.delegation),
                mcpServers,
                routines,
                routineRuns,
                skillAssignments,
                threads,
                updatedAt: updatedAt ?? "1970-01-01T00:00:00.000Z",
              };

              return yield* decodeReadModel(snapshot).pipe(
                Effect.mapError(
                  toPersistenceDecodeError("ProjectionSnapshotQuery.getSnapshot:decodeReadModel"),
                ),
              );
            }),
        ),
        Effect.mapError((error) => {
          if (isPersistenceError(error)) {
            return error;
          }

          return toPersistenceSqlError("ProjectionSnapshotQuery.getSnapshot:query")(error);
        }),
      );

  const getCommandReadModel: ProjectionSnapshotQueryShape["getCommandReadModel"] = () =>
    sql
      .withTransaction(
        Effect.all([
          listProjectRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listProjects:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listProjects:decodeRows",
              ),
            ),
          ),
          listBotRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listBots:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listBots:decodeRows",
              ),
            ),
          ),
          listGroupRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listGroups:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listGroups:decodeRows",
              ),
            ),
          ),
          listDelegationRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listDelegations:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listDelegations:decodeRows",
              ),
            ),
          ),
          projectionMcpServerRepository.listAll(),
          routineRepository.listAll,
          routineRepository.listAllRuns,
          routineRepository.listSkillAssignments,
          listThreadRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listThreads:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listThreads:decodeRows",
              ),
            ),
          ),
          listThreadProposedPlanRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listThreadProposedPlans:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listThreadProposedPlans:decodeRows",
              ),
            ),
          ),
          listThreadSessionRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listThreadSessions:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listThreadSessions:decodeRows",
              ),
            ),
          ),
          listLatestTurnRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listLatestTurns:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listLatestTurns:decodeRows",
              ),
            ),
          ),
          listProjectionStateRows(undefined).pipe(
            Effect.mapError(
              toPersistenceSqlOrDecodeError(
                "ProjectionSnapshotQuery.getCommandReadModel:listProjectionState:query",
                "ProjectionSnapshotQuery.getCommandReadModel:listProjectionState:decodeRows",
              ),
            ),
          ),
        ]),
      )
      .pipe(
        Effect.flatMap(
          ([
            projectRows,
            botRows,
            groupRows,
            delegationRows,
            mcpServers,
            routines,
            routineRuns,
            skillAssignments,
            threadRows,
            proposedPlanRows,
            sessionRows,
            latestTurnRows,
            stateRows,
          ]) =>
            Effect.sync(() => {
              let updatedAt: string | null = null;
              const projects: OrchestrationProject[] = [];
              const threads: OrchestrationThread[] = [];

              for (let index = 0; index < projectRows.length; index += 1) {
                const row = projectRows[index];

                if (!row) {
                  continue;
                }

                updatedAt = maxIso(updatedAt, row.updatedAt);
                projects.push({
                  id: row.projectId,
                  title: row.title,
                  workspaceRoot: row.workspaceRoot,
                  defaultModelSelection: row.defaultModelSelection,
                  defaultThreadEnvMode: row.defaultThreadEnvMode,
                  faviconPath: row.faviconPath ?? null,
                  scripts: row.scripts,
                  createdAt: row.createdAt,
                  updatedAt: row.updatedAt,
                  deletedAt: row.deletedAt,
                });
              }

              for (const row of botRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (const row of groupRows) {
                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (const row of delegationRows) {
                updatedAt = maxIso(updatedAt, row.delegation.updatedAt);
              }

              for (const mcpServer of mcpServers) {
                updatedAt = maxIso(updatedAt, mcpServer.updatedAt);
              }

              for (let index = 0; index < threadRows.length; index += 1) {
                const row = threadRows[index];

                if (!row) {
                  continue;
                }

                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (let index = 0; index < proposedPlanRows.length; index += 1) {
                const row = proposedPlanRows[index];

                if (!row) {
                  continue;
                }

                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (let index = 0; index < sessionRows.length; index += 1) {
                const row = sessionRows[index];

                if (!row) {
                  continue;
                }

                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              for (let index = 0; index < latestTurnRows.length; index += 1) {
                const row = latestTurnRows[index];

                if (!row) {
                  continue;
                }

                updatedAt = maxIso(updatedAt, row.requestedAt);

                if (row.startedAt !== null) {
                  updatedAt = maxIso(updatedAt, row.startedAt);
                }

                if (row.completedAt !== null) {
                  updatedAt = maxIso(updatedAt, row.completedAt);
                }
              }

              for (let index = 0; index < stateRows.length; index += 1) {
                const row = stateRows[index];

                if (!row) {
                  continue;
                }

                updatedAt = maxIso(updatedAt, row.updatedAt);
              }

              const latestTurnByThread = new Map<string, OrchestrationLatestTurn>();

              for (let index = 0; index < latestTurnRows.length; index += 1) {
                const row = latestTurnRows[index];

                if (!row) {
                  continue;
                }

                latestTurnByThread.set(row.threadId, mapLatestTurn(row));
              }

              const proposedPlansByThread = new Map<string, Array<OrchestrationProposedPlan>>();
              const sessionByThread = new Map<string, OrchestrationSession>();

              for (let index = 0; index < sessionRows.length; index += 1) {
                const row = sessionRows[index];

                if (!row) {
                  continue;
                }

                sessionByThread.set(row.threadId, mapSessionRow(row));
              }

              for (let index = 0; index < proposedPlanRows.length; index += 1) {
                const row = proposedPlanRows[index];

                if (!row) {
                  continue;
                }

                const threadProposedPlans = proposedPlansByThread.get(row.threadId) ?? [];
                threadProposedPlans.push(mapProposedPlanRow(row));
                proposedPlansByThread.set(row.threadId, threadProposedPlans);
              }

              for (let index = 0; index < threadRows.length; index += 1) {
                const row = threadRows[index];

                if (!row) {
                  continue;
                }

                threads.push({
                  id: row.threadId,
                  projectId: row.projectId,
                  botId: row.botId,
                  groupId: row.groupId,
                  parentThreadId: row.parentThreadId ?? null,
                  parentDelegationId: row.parentDelegationId ?? null,
                  respondingBotId: row.respondingBotId ?? null,
                  title: row.title,
                  modelSelection: row.modelSelection,
                  runtimeMode: row.runtimeMode,
                  interactionMode: row.interactionMode,
                  branch: row.branch,
                  worktreePath: row.worktreePath,
                  ...(row.linkedPullRequest === null
                    ? {}
                    : { linkedPullRequest: row.linkedPullRequest }),
                  latestTurn: latestTurnByThread.get(row.threadId) ?? null,
                  createdAt: row.createdAt,
                  updatedAt: row.updatedAt,
                  archivedAt: row.archivedAt,
                  settledOverride: row.settledOverride,
                  settledAt: row.settledAt,
                  unsettledAt: row.unsettledAt,
                  snoozedUntil: row.snoozedUntil,
                  snoozedAt: row.snoozedAt,
                  pinnedAt: row.pinnedAt,
                  pinOrderKey: row.pinOrderKey ?? null,
                  titleRegeneration: mapTitleRegeneration(row),
                  deletedAt: row.deletedAt,
                  messages: [],
                  proposedPlans: proposedPlansByThread.get(row.threadId) ?? [],
                  activities: [],
                  checkpoints: [],
                  session: sessionByThread.get(row.threadId) ?? null,
                });
              }

              return {
                snapshotSequence: computeSnapshotSequence(stateRows),
                projects,
                bots: botRows.map(mapBotRow),
                groups: groupRows.map(mapGroupRow),
                delegations: delegationRows.map((row) => row.delegation),
                mcpServers,
                routines,
                routineRuns,
                skillAssignments,
                threads,
                updatedAt: updatedAt ?? "1970-01-01T00:00:00.000Z",
              } satisfies OrchestrationReadModel;
            }),
        ),
        Effect.mapError((error) => {
          if (isPersistenceError(error)) {
            return error;
          }

          return toPersistenceSqlError("ProjectionSnapshotQuery.getCommandReadModel:query")(error);
        }),
      );

  return { getSnapshot, getCommandReadModel };
}