987 lines · 31.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,} 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.Literal("codex");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";const CodexProviderStartOptions = Schema.Struct({  binaryPath: Schema.optional(TrimmedNonEmptyString),  homePath: Schema.optional(TrimmedNonEmptyString),});const ProviderStartOptions = Schema.Struct({  codex: Schema.optional(CodexProviderStartOptions),});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,  createdAt: IsoDateTime,  updatedAt: IsoDateTime,});export type OrchestrationProposedPlan = typeof OrchestrationProposedPlan.Type; 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),  sequence: Schema.optional(NonNegativeInt),  createdAt: IsoDateTime,});export type OrchestrationThreadActivity = typeof OrchestrationThreadActivity.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),});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),  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),  ),  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,  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,  createdAt: IsoDateTime,}); const DispatchableClientOrchestrationCommand = Schema.Union([  ProjectCreateCommand,  ProjectMetaUpdateCommand,  ProjectDeleteCommand,  ThreadCreateCommand,  ThreadDeleteCommand,  ThreadMetaUpdateCommand,  ThreadRuntimeModeSetCommand,  ThreadInteractionModeSetCommand,  ThreadTurnStartCommand,  ThreadTurnInterruptCommand,  ThreadApprovalRespondCommand,  ThreadUserInputRespondCommand,  ThreadCheckpointRevertCommand,  ThreadSessionStopCommand,]);export type DispatchableClientOrchestrationCommand =  typeof DispatchableClientOrchestrationCommand.Type; export const ClientOrchestrationCommand = Schema.Union([  ProjectCreateCommand,  ProjectMetaUpdateCommand,  ProjectDeleteCommand,  ThreadCreateCommand,  ThreadDeleteCommand,  ThreadMetaUpdateCommand,  ThreadRuntimeModeSetCommand,  ThreadInteractionModeSetCommand,  ClientThreadTurnStartCommand,  ThreadTurnInterruptCommand,  ThreadApprovalRespondCommand,  ThreadUserInputRespondCommand,  ThreadCheckpointRevertCommand,  ThreadSessionStopCommand,]);export type ClientOrchestrationCommand = typeof ClientOrchestrationCommand.Type; const ThreadSessionSetCommand = Schema.Struct({  type: Schema.Literal("thread.session.set"),  commandId: CommandId,  threadId: ThreadId,  session: OrchestrationSession,  createdAt: IsoDateTime,}); const ThreadMessageAssistantDeltaCommand = Schema.Struct({  type: Schema.Literal("thread.message.assistant.delta"),  commandId: CommandId,  threadId: ThreadId,  messageId: MessageId,  delta: Schema.String,  turnId: Schema.optional(TurnId),  createdAt: IsoDateTime,}); const ThreadMessageAssistantCompleteCommand = Schema.Struct({  type: Schema.Literal("thread.message.assistant.complete"),  commandId: CommandId,

Lines 1–500 of 987.