1213 lines · 41.0 KB · TypeScript
RawDownload
import {  ChatAttachment,  IsoDateTime,  MessageId,  NonNegativeInt,  OrchestrationArchivedThreadSummary,  OrchestrationCheckpointFile,  OrchestrationProposedPlanId,  OrchestrationReadModel,  ProjectScript,  TurnId,  type OrchestrationCheckpointSummary,  type OrchestrationLatestTurn,  type OrchestrationMessage,  type OrchestrationProposedPlan,  type OrchestrationProject,  type OrchestrationSession,  type OrchestrationThread,  type OrchestrationThreadActivity,  ModelSelection,  ProjectId,  ThreadId,} from "@t3tools/contracts";import { Effect, Layer, Option, Schema, Struct } from "effect";import * as SqlClient from "effect/unstable/sql/SqlClient";import * as SqlSchema from "effect/unstable/sql/SqlSchema"; import {  isPersistenceError,  toPersistenceDecodeError,  toPersistenceSqlError,  type ProjectionRepositoryError,} from "../../persistence/Errors.ts";import { ProjectionCheckpoint } from "../../persistence/Services/ProjectionCheckpoints.ts";import { ProjectionProject } from "../../persistence/Services/ProjectionProjects.ts";import { ProjectionState } from "../../persistence/Services/ProjectionState.ts";import { ProjectionThreadActivity } from "../../persistence/Services/ProjectionThreadActivities.ts";import { ProjectionThreadMessage } from "../../persistence/Services/ProjectionThreadMessages.ts";import { ProjectionThreadProposedPlan } from "../../persistence/Services/ProjectionThreadProposedPlans.ts";import { ProjectionThreadSession } from "../../persistence/Services/ProjectionThreadSessions.ts";import { ProjectionThread } from "../../persistence/Services/ProjectionThreads.ts";import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";import {  ProjectionSnapshotQuery,  type ProjectionSnapshotCounts,  type ProjectionThreadCheckpointContext,  type ProjectionSnapshotQueryShape,} from "../Services/ProjectionSnapshotQuery.ts"; const decodeReadModel = Schema.decodeUnknownEffect(OrchestrationReadModel);const ProjectionProjectDbRowSchema = ProjectionProject.mapFields(  Struct.assign({    defaultModelSelection: Schema.NullOr(Schema.fromJsonString(ModelSelection)),    scripts: Schema.fromJsonString(Schema.Array(ProjectScript)),  }),);const ProjectionThreadMessageDbRowSchema = ProjectionThreadMessage.mapFields(  Struct.assign({    isStreaming: Schema.Number,    attachments: Schema.NullOr(Schema.fromJsonString(Schema.Array(ChatAttachment))),  }),);const ProjectionThreadProposedPlanDbRowSchema = ProjectionThreadProposedPlan;const ProjectionThreadDbRowSchema = ProjectionThread.mapFields(  Struct.assign({    modelSelection: Schema.fromJsonString(ModelSelection),  }),);const ProjectionThreadActivityDbRowSchema = ProjectionThreadActivity.mapFields(  Struct.assign({    payload: Schema.fromJsonString(Schema.Unknown),    sequence: Schema.NullOr(NonNegativeInt),  }),);const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession;const ProjectionCheckpointDbRowSchema = ProjectionCheckpoint.mapFields(  Struct.assign({    files: Schema.fromJsonString(Schema.Array(OrchestrationCheckpointFile)),  }),);const ProjectionLatestTurnDbRowSchema = Schema.Struct({  threadId: ProjectionThread.fields.threadId,  turnId: TurnId,  state: Schema.String,  requestedAt: IsoDateTime,  startedAt: Schema.NullOr(IsoDateTime),  completedAt: Schema.NullOr(IsoDateTime),  assistantMessageId: Schema.NullOr(MessageId),  sourceProposedPlanThreadId: Schema.NullOr(ThreadId),  sourceProposedPlanId: Schema.NullOr(OrchestrationProposedPlanId),});const ProjectionStateDbRowSchema = ProjectionState;const ProjectionArchivedThreadSummaryRowSchema = OrchestrationArchivedThreadSummary;const ProjectionCountsRowSchema = Schema.Struct({  projectCount: Schema.Number,  threadCount: Schema.Number,});const WorkspaceRootLookupInput = Schema.Struct({  workspaceRoot: Schema.String,});const ProjectIdLookupInput = Schema.Struct({  projectId: ProjectId,});const ThreadIdLookupInput = Schema.Struct({  threadId: ThreadId,});const ProjectionProjectLookupRowSchema = ProjectionProjectDbRowSchema;const ProjectionThreadIdLookupRowSchema = Schema.Struct({  threadId: ThreadId,});const ProjectionThreadCheckpointContextThreadRowSchema = Schema.Struct({  threadId: ThreadId,  projectId: ProjectId,  workspaceRoot: Schema.String,  worktreePath: Schema.NullOr(Schema.String),});type ProjectionProjectDbRow = Schema.Schema.Type<typeof ProjectionProjectDbRowSchema>;type ProjectionThreadDbRow = Schema.Schema.Type<typeof ProjectionThreadDbRowSchema>;type ProjectionThreadMessageDbRow = Schema.Schema.Type<typeof ProjectionThreadMessageDbRowSchema>;type ProjectionThreadProposedPlanDbRow = Schema.Schema.Type<  typeof ProjectionThreadProposedPlanDbRowSchema>;type ProjectionThreadActivityDbRow = Schema.Schema.Type<typeof ProjectionThreadActivityDbRowSchema>;type ProjectionThreadSessionDbRow = Schema.Schema.Type<typeof ProjectionThreadSessionDbRowSchema>;type ProjectionCheckpointDbRow = Schema.Schema.Type<typeof ProjectionCheckpointDbRowSchema>;type ProjectionLatestTurnDbRow = Schema.Schema.Type<typeof ProjectionLatestTurnDbRowSchema>;type ProjectionStateDbRow = Schema.Schema.Type<typeof ProjectionStateDbRowSchema>; const REQUIRED_SNAPSHOT_PROJECTORS = [  ORCHESTRATION_PROJECTOR_NAMES.projects,  ORCHESTRATION_PROJECTOR_NAMES.threads,  ORCHESTRATION_PROJECTOR_NAMES.threadMessages,  ORCHESTRATION_PROJECTOR_NAMES.threadProposedPlans,  ORCHESTRATION_PROJECTOR_NAMES.threadActivities,  ORCHESTRATION_PROJECTOR_NAMES.threadSessions,  ORCHESTRATION_PROJECTOR_NAMES.checkpoints,] as const; function maxIso(left: string | null, right: string): string {  if (left === null) {    return right;  }  return left > right ? left : right;} function computeSnapshotSequence(  stateRows: ReadonlyArray<Schema.Schema.Type<typeof ProjectionStateDbRowSchema>>,): number {  if (stateRows.length === 0) {    return 0;  }  const sequenceByProjector = new Map(    stateRows.map((row) => [row.projector, row.lastAppliedSequence] as const),  );   let minSequence = Number.POSITIVE_INFINITY;  for (const projector of REQUIRED_SNAPSHOT_PROJECTORS) {    const sequence = sequenceByProjector.get(projector);    if (sequence === undefined) {      return 0;    }    if (sequence < minSequence) {      minSequence = sequence;    }  }   return Number.isFinite(minSequence) ? minSequence : 0;} function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {  return (cause: unknown): ProjectionRepositoryError =>    Schema.isSchemaError(cause)      ? toPersistenceDecodeError(decodeOperation)(cause)      : toPersistenceSqlError(sqlOperation)(cause);} const makeProjectionSnapshotQuery = Effect.gen(function* () {  const sql = yield* SqlClient.SqlClient;   const listProjectRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionProjectDbRowSchema,    execute: () =>      sql`        SELECT          project_id AS "projectId",          title,          workspace_root AS "workspaceRoot",          default_model_selection_json AS "defaultModelSelection",          scripts_json AS "scripts",          created_at AS "createdAt",          updated_at AS "updatedAt",          deleted_at AS "deletedAt"        FROM projection_projects        ORDER BY created_at ASC, project_id ASC      `,  });   const listActiveProjectRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionProjectDbRowSchema,    execute: () =>      sql`        SELECT          project_id AS "projectId",          title,          workspace_root AS "workspaceRoot",          default_model_selection_json AS "defaultModelSelection",          scripts_json AS "scripts",          created_at AS "createdAt",          updated_at AS "updatedAt",          deleted_at AS "deletedAt"        FROM projection_projects        WHERE deleted_at IS NULL        ORDER BY created_at ASC, project_id ASC      `,  });   const listThreadRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadDbRowSchema,    execute: () =>      sql`        SELECT          thread_id AS "threadId",          project_id AS "projectId",          title,          model_selection_json AS "modelSelection",          runtime_mode AS "runtimeMode",          interaction_mode AS "interactionMode",          branch,          worktree_path AS "worktreePath",          latest_turn_id AS "latestTurnId",          created_at AS "createdAt",          updated_at AS "updatedAt",          archived_at AS "archivedAt",          deleted_at AS "deletedAt"        FROM projection_threads        ORDER BY created_at ASC, thread_id ASC      `,  });   const listActiveThreadRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadDbRowSchema,    execute: () =>      sql`        SELECT          thread_id AS "threadId",          project_id AS "projectId",          title,          model_selection_json AS "modelSelection",          runtime_mode AS "runtimeMode",          interaction_mode AS "interactionMode",          branch,          worktree_path AS "worktreePath",          latest_turn_id AS "latestTurnId",          created_at AS "createdAt",          updated_at AS "updatedAt",          archived_at AS "archivedAt",          deleted_at AS "deletedAt"        FROM projection_threads        WHERE deleted_at IS NULL          AND archived_at IS NULL        ORDER BY created_at ASC, thread_id ASC      `,  });   const listThreadMessageRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadMessageDbRowSchema,    execute: () =>      sql`        SELECT          message_id AS "messageId",          thread_id AS "threadId",          turn_id AS "turnId",          role,          text,          attachments_json AS "attachments",          is_streaming AS "isStreaming",          created_at AS "createdAt",          updated_at AS "updatedAt"        FROM projection_thread_messages        ORDER BY thread_id ASC, created_at ASC, message_id ASC      `,  });   const listActiveThreadMessageRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadMessageDbRowSchema,    execute: () =>      sql`        SELECT          message_id AS "messageId",          thread_id AS "threadId",          turn_id AS "turnId",          role,          text,          attachments_json AS "attachments",          is_streaming AS "isStreaming",          created_at AS "createdAt",          updated_at AS "updatedAt"        FROM projection_thread_messages        WHERE thread_id IN (          SELECT thread_id          FROM projection_threads          WHERE deleted_at IS NULL            AND archived_at IS NULL        )        ORDER BY thread_id ASC, created_at ASC, message_id ASC      `,  });   const listThreadProposedPlanRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadProposedPlanDbRowSchema,    execute: () =>      sql`        SELECT          plan_id AS "planId",          thread_id AS "threadId",          turn_id AS "turnId",          plan_markdown AS "planMarkdown",          implemented_at AS "implementedAt",          implementation_thread_id AS "implementationThreadId",          created_at AS "createdAt",          updated_at AS "updatedAt"        FROM projection_thread_proposed_plans        ORDER BY thread_id ASC, created_at ASC, plan_id ASC      `,  });   const listActiveThreadProposedPlanRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadProposedPlanDbRowSchema,    execute: () =>      sql`        SELECT          plan_id AS "planId",          thread_id AS "threadId",          turn_id AS "turnId",          plan_markdown AS "planMarkdown",          implemented_at AS "implementedAt",          implementation_thread_id AS "implementationThreadId",          created_at AS "createdAt",          updated_at AS "updatedAt"        FROM projection_thread_proposed_plans        WHERE thread_id IN (          SELECT thread_id          FROM projection_threads          WHERE deleted_at IS NULL            AND archived_at IS NULL        )        ORDER BY thread_id ASC, created_at ASC, plan_id ASC      `,  });   const listThreadActivityRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadActivityDbRowSchema,    execute: () =>      sql`        SELECT          activity_id AS "activityId",          thread_id AS "threadId",          turn_id AS "turnId",          tone,          kind,          summary,          payload_json AS "payload",          sequence,          created_at AS "createdAt"        FROM projection_thread_activities        ORDER BY          thread_id ASC,          CASE WHEN sequence IS NULL THEN 0 ELSE 1 END ASC,          sequence ASC,          created_at ASC,          activity_id ASC      `,  });   const listActiveThreadActivityRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadActivityDbRowSchema,    execute: () =>      sql`        SELECT          activity_id AS "activityId",          thread_id AS "threadId",          turn_id AS "turnId",          tone,          kind,          summary,          payload_json AS "payload",          sequence,          created_at AS "createdAt"        FROM projection_thread_activities        WHERE thread_id IN (          SELECT thread_id          FROM projection_threads          WHERE deleted_at IS NULL            AND archived_at IS NULL        )        ORDER BY          thread_id ASC,          CASE WHEN sequence IS NULL THEN 0 ELSE 1 END ASC,          sequence ASC,          created_at ASC,          activity_id ASC      `,  });   const listThreadSessionRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadSessionDbRowSchema,    execute: () =>      sql`        SELECT          thread_id AS "threadId",          status,          provider_name AS "providerName",          provider_session_id AS "providerSessionId",          provider_thread_id AS "providerThreadId",          runtime_mode AS "runtimeMode",          active_turn_id AS "activeTurnId",          last_error AS "lastError",          updated_at AS "updatedAt"        FROM projection_thread_sessions        ORDER BY thread_id ASC      `,  });   const listActiveThreadSessionRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionThreadSessionDbRowSchema,    execute: () =>      sql`        SELECT          thread_id AS "threadId",          status,          provider_name AS "providerName",          provider_session_id AS "providerSessionId",          provider_thread_id AS "providerThreadId",          runtime_mode AS "runtimeMode",          active_turn_id AS "activeTurnId",          last_error AS "lastError",          updated_at AS "updatedAt"        FROM projection_thread_sessions        WHERE thread_id IN (          SELECT thread_id          FROM projection_threads          WHERE deleted_at IS NULL            AND archived_at IS NULL        )        ORDER BY thread_id ASC      `,  });   const listCheckpointRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionCheckpointDbRowSchema,    execute: () =>      sql`        SELECT          thread_id AS "threadId",          turn_id AS "turnId",          checkpoint_turn_count AS "checkpointTurnCount",          checkpoint_ref AS "checkpointRef",          checkpoint_status AS "status",          checkpoint_files_json AS "files",          assistant_message_id AS "assistantMessageId",          completed_at AS "completedAt"        FROM projection_turns        WHERE checkpoint_turn_count IS NOT NULL        ORDER BY thread_id ASC, checkpoint_turn_count ASC      `,  });   const listActiveCheckpointRows = SqlSchema.findAll({    Request: Schema.Void,    Result: ProjectionCheckpointDbRowSchema,    execute: () =>      sql`        SELECT          thread_id AS "threadId",          turn_id AS "turnId",          checkpoint_turn_count AS "checkpointTurnCount",          checkpoint_ref AS "checkpointRef",          checkpoint_status AS "status",          checkpoint_files_json AS "files",          assistant_message_id AS "assistantMessageId",          completed_at AS "completedAt"        FROM projection_turns        WHERE checkpoint_turn_count IS NOT NULL          AND thread_id IN (            SELECT thread_id            FROM projection_threads            WHERE deleted_at IS NULL

Lines 1–500 of 1213.