1069 lines · 34.6 KB · TypeScript
RawDownload
import { Option, Schema, SchemaIssue, Struct } from "effect";import { ProviderModelOptions } from "./model";import {  ApprovalRequestId,  CheckpointRef,  CommandId,  EventId,  IsoDateTime,  MessageId,  NonNegativeInt,  ProjectId,  ProviderItemId,  ThreadId,  TrimmedNonEmptyString,  TurnId,  RuntimeItemId,  RuntimeTaskId,  WorkUnitId,} from "./baseSchemas"; export const ORCHESTRATION_WS_METHODS = {  getSnapshot: "orchestration.getSnapshot",  dispatchCommand: "orchestration.dispatchCommand",  getTurnDiff: "orchestration.getTurnDiff",  getFullThreadDiff: "orchestration.getFullThreadDiff",  replayEvents: "orchestration.replayEvents",} as const; export const ORCHESTRATION_WS_CHANNELS = {  domainEvent: "orchestration.domainEvent",} as const; export const ProviderKind = Schema.Literals(["codex", "claudeAgent"]);export type ProviderKind = typeof ProviderKind.Type;export const ProviderApprovalPolicy = Schema.Literals([  "untrusted",  "on-failure",  "on-request",  "never",]);export type ProviderApprovalPolicy = typeof ProviderApprovalPolicy.Type;export const ProviderSandboxMode = Schema.Literals([  "read-only",  "workspace-write",  "danger-full-access",]);export type ProviderSandboxMode = typeof ProviderSandboxMode.Type;export const DEFAULT_PROVIDER_KIND: ProviderKind = "codex"; export const CodexProviderStartOptions = Schema.Struct({  binaryPath: Schema.optional(TrimmedNonEmptyString),  homePath: Schema.optional(TrimmedNonEmptyString),}); export const ClaudeProviderStartOptions = Schema.Struct({  binaryPath: Schema.optional(TrimmedNonEmptyString),  permissionMode: Schema.optional(TrimmedNonEmptyString),  maxThinkingTokens: Schema.optional(NonNegativeInt),}); export const ProviderStartOptions = Schema.Struct({  codex: Schema.optional(CodexProviderStartOptions),  claudeAgent: Schema.optional(ClaudeProviderStartOptions),});export type ProviderStartOptions = typeof ProviderStartOptions.Type; export const RuntimeMode = Schema.Literals(["approval-required", "full-access"]);export type RuntimeMode = typeof RuntimeMode.Type;export const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access";export const ProviderInteractionMode = Schema.Literals(["default", "plan"]);export type ProviderInteractionMode = typeof ProviderInteractionMode.Type;export const DEFAULT_PROVIDER_INTERACTION_MODE: ProviderInteractionMode = "default";export const ProviderRequestKind = Schema.Literals(["command", "file-read", "file-change"]);export type ProviderRequestKind = typeof ProviderRequestKind.Type;export const AssistantDeliveryMode = Schema.Literals(["buffered", "streaming"]);export type AssistantDeliveryMode = typeof AssistantDeliveryMode.Type;export const ProviderApprovalDecision = Schema.Literals([  "accept",  "acceptForSession",  "decline",  "cancel",]);export type ProviderApprovalDecision = typeof ProviderApprovalDecision.Type;export const ProviderUserInputAnswers = Schema.Record(Schema.String, Schema.Unknown);export type ProviderUserInputAnswers = typeof ProviderUserInputAnswers.Type; export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;const CHAT_ATTACHMENT_ID_MAX_CHARS = 128;// Correlation id is command id by design in this model.export const CorrelationId = CommandId;export type CorrelationId = typeof CorrelationId.Type; const ChatAttachmentId = TrimmedNonEmptyString.check(  Schema.isMaxLength(CHAT_ATTACHMENT_ID_MAX_CHARS),  Schema.isPattern(/^[a-z0-9_-]+$/i),);export type ChatAttachmentId = typeof ChatAttachmentId.Type; export const ChatImageAttachment = Schema.Struct({  type: Schema.Literal("image"),  id: ChatAttachmentId,  name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),  mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),  sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),});export type ChatImageAttachment = typeof ChatImageAttachment.Type; const UploadChatImageAttachment = Schema.Struct({  type: Schema.Literal("image"),  name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),  mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),  sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),  dataUrl: TrimmedNonEmptyString.check(    Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),  ),});export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type; export const ChatAttachment = Schema.Union([ChatImageAttachment]);export type ChatAttachment = typeof ChatAttachment.Type;const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);export type UploadChatAttachment = typeof UploadChatAttachment.Type; export const ProjectScriptIcon = Schema.Literals([  "play",  "test",  "lint",  "configure",  "build",  "debug",]);export type ProjectScriptIcon = typeof ProjectScriptIcon.Type; export const ProjectScript = Schema.Struct({  id: TrimmedNonEmptyString,  name: TrimmedNonEmptyString,  command: TrimmedNonEmptyString,  icon: ProjectScriptIcon,  runOnWorktreeCreate: Schema.Boolean,});export type ProjectScript = typeof ProjectScript.Type; export const OrchestrationProject = Schema.Struct({  id: ProjectId,  title: TrimmedNonEmptyString,  workspaceRoot: TrimmedNonEmptyString,  defaultModel: Schema.NullOr(TrimmedNonEmptyString),  scripts: Schema.Array(ProjectScript),  createdAt: IsoDateTime,  updatedAt: IsoDateTime,  deletedAt: Schema.NullOr(IsoDateTime),});export type OrchestrationProject = typeof OrchestrationProject.Type; export const OrchestrationMessageRole = Schema.Literals(["user", "assistant", "system"]);export type OrchestrationMessageRole = typeof OrchestrationMessageRole.Type; export const OrchestrationMessage = Schema.Struct({  id: MessageId,  role: OrchestrationMessageRole,  text: Schema.String,  attachments: Schema.optional(Schema.Array(ChatAttachment)),  turnId: Schema.NullOr(TurnId),  streaming: Schema.Boolean,  createdAt: IsoDateTime,  updatedAt: IsoDateTime,});export type OrchestrationMessage = typeof OrchestrationMessage.Type; export const OrchestrationProposedPlanId = TrimmedNonEmptyString;export type OrchestrationProposedPlanId = typeof OrchestrationProposedPlanId.Type; export const OrchestrationProposedPlan = Schema.Struct({  id: OrchestrationProposedPlanId,  turnId: Schema.NullOr(TurnId),  planMarkdown: TrimmedNonEmptyString,  implementedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(() => null)),  implementationThreadId: Schema.NullOr(ThreadId).pipe(Schema.withDecodingDefault(() => null)),  createdAt: IsoDateTime,  updatedAt: IsoDateTime,});export type OrchestrationProposedPlan = typeof OrchestrationProposedPlan.Type; const SourceProposedPlanReference = Schema.Struct({  threadId: ThreadId,  planId: OrchestrationProposedPlanId,}); export const OrchestrationSessionStatus = Schema.Literals([  "idle",  "starting",  "running",  "ready",  "interrupted",  "stopped",  "error",]);export type OrchestrationSessionStatus = typeof OrchestrationSessionStatus.Type; export const OrchestrationSession = Schema.Struct({  threadId: ThreadId,  status: OrchestrationSessionStatus,  providerName: Schema.NullOr(TrimmedNonEmptyString),  runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),  activeTurnId: Schema.NullOr(TurnId),  lastError: Schema.NullOr(TrimmedNonEmptyString),  updatedAt: IsoDateTime,});export type OrchestrationSession = typeof OrchestrationSession.Type; export const OrchestrationCheckpointFile = Schema.Struct({  path: TrimmedNonEmptyString,  kind: TrimmedNonEmptyString,  additions: NonNegativeInt,  deletions: NonNegativeInt,});export type OrchestrationCheckpointFile = typeof OrchestrationCheckpointFile.Type; export const OrchestrationCheckpointStatus = Schema.Literals(["ready", "missing", "error"]);export type OrchestrationCheckpointStatus = typeof OrchestrationCheckpointStatus.Type; export const OrchestrationCheckpointSummary = Schema.Struct({  turnId: TurnId,  checkpointTurnCount: NonNegativeInt,  checkpointRef: CheckpointRef,  status: OrchestrationCheckpointStatus,  files: Schema.Array(OrchestrationCheckpointFile),  assistantMessageId: Schema.NullOr(MessageId),  completedAt: IsoDateTime,});export type OrchestrationCheckpointSummary = typeof OrchestrationCheckpointSummary.Type; export const OrchestrationThreadActivityTone = Schema.Literals([  "info",  "tool",  "approval",  "error",]);export type OrchestrationThreadActivityTone = typeof OrchestrationThreadActivityTone.Type; export const OrchestrationThreadActivity = Schema.Struct({  id: EventId,  tone: OrchestrationThreadActivityTone,  kind: TrimmedNonEmptyString,  summary: TrimmedNonEmptyString,  payload: Schema.Unknown,  turnId: Schema.NullOr(TurnId),  workUnitId: Schema.NullOr(WorkUnitId).pipe(Schema.withDecodingDefault(() => null)),  sequence: Schema.optional(NonNegativeInt),  createdAt: IsoDateTime,});export type OrchestrationThreadActivity = typeof OrchestrationThreadActivity.Type; export const OrchestrationWorkUnitKind = Schema.Literals(["primary_agent", "delegated_agent"]);export type OrchestrationWorkUnitKind = typeof OrchestrationWorkUnitKind.Type; export const OrchestrationWorkUnitState = Schema.Literals([  "queued",  "running",  "completed",  "failed",  "stopped",  "cancelled",]);export type OrchestrationWorkUnitState = typeof OrchestrationWorkUnitState.Type; export const OrchestrationWorkUnitProviderRefs = Schema.Struct({  runtimeTaskId: Schema.optional(RuntimeTaskId),  runtimeItemId: Schema.optional(RuntimeItemId),  providerThreadId: Schema.optional(TrimmedNonEmptyString),  providerTurnId: Schema.optional(TrimmedNonEmptyString),});export type OrchestrationWorkUnitProviderRefs = typeof OrchestrationWorkUnitProviderRefs.Type; export const OrchestrationWorkUnit = Schema.Struct({  id: WorkUnitId,  turnId: TurnId,  parentWorkUnitId: Schema.NullOr(WorkUnitId),  kind: OrchestrationWorkUnitKind,  state: OrchestrationWorkUnitState,  title: TrimmedNonEmptyString,  detail: Schema.NullOr(TrimmedNonEmptyString),  spawnedByActivityId: Schema.NullOr(EventId),  providerRefs: Schema.optional(OrchestrationWorkUnitProviderRefs),  startedAt: IsoDateTime,  updatedAt: IsoDateTime,  completedAt: Schema.NullOr(IsoDateTime),});export type OrchestrationWorkUnit = typeof OrchestrationWorkUnit.Type; const OrchestrationLatestTurnState = Schema.Literals([  "running",  "interrupted",  "completed",  "error",]);export type OrchestrationLatestTurnState = typeof OrchestrationLatestTurnState.Type; export const OrchestrationLatestTurn = Schema.Struct({  turnId: TurnId,  state: OrchestrationLatestTurnState,  requestedAt: IsoDateTime,  startedAt: Schema.NullOr(IsoDateTime),  completedAt: Schema.NullOr(IsoDateTime),  assistantMessageId: Schema.NullOr(MessageId),  sourceProposedPlan: Schema.optional(SourceProposedPlanReference),});export type OrchestrationLatestTurn = typeof OrchestrationLatestTurn.Type; export const OrchestrationThread = Schema.Struct({  id: ThreadId,  projectId: ProjectId,  title: TrimmedNonEmptyString,  model: TrimmedNonEmptyString,  runtimeMode: RuntimeMode,  interactionMode: ProviderInteractionMode.pipe(    Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),  ),  branch: Schema.NullOr(TrimmedNonEmptyString),  worktreePath: Schema.NullOr(TrimmedNonEmptyString),  latestTurn: Schema.NullOr(OrchestrationLatestTurn),  createdAt: IsoDateTime,  updatedAt: IsoDateTime,  deletedAt: Schema.NullOr(IsoDateTime),  messages: Schema.Array(OrchestrationMessage),  proposedPlans: Schema.Array(OrchestrationProposedPlan).pipe(Schema.withDecodingDefault(() => [])),  activities: Schema.Array(OrchestrationThreadActivity),  workUnits: Schema.Array(OrchestrationWorkUnit).pipe(Schema.withDecodingDefault(() => [])),  checkpoints: Schema.Array(OrchestrationCheckpointSummary),  session: Schema.NullOr(OrchestrationSession),});export type OrchestrationThread = typeof OrchestrationThread.Type; export const OrchestrationReadModel = Schema.Struct({  snapshotSequence: NonNegativeInt,  projects: Schema.Array(OrchestrationProject),  threads: Schema.Array(OrchestrationThread),  updatedAt: IsoDateTime,});export type OrchestrationReadModel = typeof OrchestrationReadModel.Type; export const ProjectCreateCommand = Schema.Struct({  type: Schema.Literal("project.create"),  commandId: CommandId,  projectId: ProjectId,  title: TrimmedNonEmptyString,  workspaceRoot: TrimmedNonEmptyString,  defaultModel: Schema.optional(TrimmedNonEmptyString),  createdAt: IsoDateTime,}); const ProjectMetaUpdateCommand = Schema.Struct({  type: Schema.Literal("project.meta.update"),  commandId: CommandId,  projectId: ProjectId,  title: Schema.optional(TrimmedNonEmptyString),  workspaceRoot: Schema.optional(TrimmedNonEmptyString),  defaultModel: Schema.optional(TrimmedNonEmptyString),  scripts: Schema.optional(Schema.Array(ProjectScript)),}); const ProjectDeleteCommand = Schema.Struct({  type: Schema.Literal("project.delete"),  commandId: CommandId,  projectId: ProjectId,}); const ThreadCreateCommand = Schema.Struct({  type: Schema.Literal("thread.create"),  commandId: CommandId,  threadId: ThreadId,  projectId: ProjectId,  title: TrimmedNonEmptyString,  model: TrimmedNonEmptyString,  runtimeMode: RuntimeMode,  interactionMode: ProviderInteractionMode.pipe(    Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),  ),  branch: Schema.NullOr(TrimmedNonEmptyString),  worktreePath: Schema.NullOr(TrimmedNonEmptyString),  createdAt: IsoDateTime,}); const ThreadDeleteCommand = Schema.Struct({  type: Schema.Literal("thread.delete"),  commandId: CommandId,  threadId: ThreadId,}); const ThreadMetaUpdateCommand = Schema.Struct({  type: Schema.Literal("thread.meta.update"),  commandId: CommandId,  threadId: ThreadId,  title: Schema.optional(TrimmedNonEmptyString),  model: Schema.optional(TrimmedNonEmptyString),  branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),  worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),}); const ThreadRuntimeModeSetCommand = Schema.Struct({  type: Schema.Literal("thread.runtime-mode.set"),  commandId: CommandId,  threadId: ThreadId,  runtimeMode: RuntimeMode,  createdAt: IsoDateTime,}); const ThreadInteractionModeSetCommand = Schema.Struct({  type: Schema.Literal("thread.interaction-mode.set"),  commandId: CommandId,  threadId: ThreadId,  interactionMode: ProviderInteractionMode,  createdAt: IsoDateTime,}); export const ThreadTurnStartCommand = Schema.Struct({  type: Schema.Literal("thread.turn.start"),  commandId: CommandId,  threadId: ThreadId,  message: Schema.Struct({    messageId: MessageId,    role: Schema.Literal("user"),    text: Schema.String,    attachments: Schema.Array(ChatAttachment),  }),  provider: Schema.optional(ProviderKind),  model: Schema.optional(TrimmedNonEmptyString),  modelOptions: Schema.optional(ProviderModelOptions),  providerOptions: Schema.optional(ProviderStartOptions),  assistantDeliveryMode: Schema.optional(AssistantDeliveryMode),  runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),  interactionMode: ProviderInteractionMode.pipe(    Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),  ),  sourceProposedPlan: Schema.optional(SourceProposedPlanReference),  createdAt: IsoDateTime,}); const ClientThreadTurnStartCommand = Schema.Struct({  type: Schema.Literal("thread.turn.start"),  commandId: CommandId,  threadId: ThreadId,  message: Schema.Struct({    messageId: MessageId,    role: Schema.Literal("user"),    text: Schema.String,    attachments: Schema.Array(UploadChatAttachment),  }),  provider: Schema.optional(ProviderKind),  model: Schema.optional(TrimmedNonEmptyString),  modelOptions: Schema.optional(ProviderModelOptions),  providerOptions: Schema.optional(ProviderStartOptions),  assistantDeliveryMode: Schema.optional(AssistantDeliveryMode),  runtimeMode: RuntimeMode,  interactionMode: ProviderInteractionMode,  sourceProposedPlan: Schema.optional(SourceProposedPlanReference),  createdAt: IsoDateTime,}); const ThreadTurnInterruptCommand = Schema.Struct({  type: Schema.Literal("thread.turn.interrupt"),  commandId: CommandId,  threadId: ThreadId,  turnId: Schema.optional(TurnId),  createdAt: IsoDateTime,}); const ThreadApprovalRespondCommand = Schema.Struct({  type: Schema.Literal("thread.approval.respond"),  commandId: CommandId,  threadId: ThreadId,  requestId: ApprovalRequestId,  decision: ProviderApprovalDecision,  createdAt: IsoDateTime,}); const ThreadUserInputRespondCommand = Schema.Struct({  type: Schema.Literal("thread.user-input.respond"),  commandId: CommandId,  threadId: ThreadId,  requestId: ApprovalRequestId,  answers: ProviderUserInputAnswers,  createdAt: IsoDateTime,}); const ThreadCheckpointRevertCommand = Schema.Struct({  type: Schema.Literal("thread.checkpoint.revert"),  commandId: CommandId,  threadId: ThreadId,  turnCount: NonNegativeInt,  createdAt: IsoDateTime,}); const ThreadSessionStopCommand = Schema.Struct({  type: Schema.Literal("thread.session.stop"),  commandId: CommandId,  threadId: ThreadId,

Lines 1–500 of 1069.