- Replace model-only fields with provider/model selections across orchestration - Add projection schema and migration updates for provider-backed snapshots - Update server and web tests to use the new selection shape
1359 lines
47 KiB
TypeScript
1359 lines
47 KiB
TypeScript
import { Effect, Option, Schema, SchemaIssue, SchemaTransformation, Struct } from "effect";
|
|
import { ClaudeModelOptions, CodexModelOptions } 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.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 CodexModelSelection = Schema.Struct({
|
|
provider: Schema.Literal("codex"),
|
|
model: TrimmedNonEmptyString,
|
|
options: Schema.optional(CodexModelOptions),
|
|
});
|
|
export type CodexModelSelection = typeof CodexModelSelection.Type;
|
|
|
|
export const ClaudeModelSelection = Schema.Struct({
|
|
provider: Schema.Literal("claudeAgent"),
|
|
model: TrimmedNonEmptyString,
|
|
options: Schema.optional(ClaudeModelOptions),
|
|
});
|
|
export type ClaudeModelSelection = typeof ClaudeModelSelection.Type;
|
|
|
|
export const ModelSelection = Schema.Union([CodexModelSelection, ClaudeModelSelection]);
|
|
export type ModelSelection = typeof ModelSelection.Type;
|
|
const decodeModelSelectionSync = Schema.decodeUnknownSync(ModelSelection);
|
|
|
|
const LegacyDefaultModelSelectionFields = {
|
|
defaultProvider: Schema.optional(ProviderKind),
|
|
defaultModel: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
|
|
defaultModelOptions: Schema.optional(Schema.NullOr(Schema.Unknown)),
|
|
} as const;
|
|
|
|
const LegacyThreadModelSelectionFields = {
|
|
provider: Schema.optional(ProviderKind),
|
|
model: Schema.optional(TrimmedNonEmptyString),
|
|
modelOptions: Schema.optional(Schema.NullOr(Schema.Unknown)),
|
|
} as const;
|
|
|
|
function invalidLegacyModelSelectionIssue(
|
|
value: unknown,
|
|
title: string,
|
|
cause: unknown,
|
|
): SchemaIssue.InvalidValue {
|
|
return new SchemaIssue.InvalidValue(Option.some(value), {
|
|
title,
|
|
...(Schema.isSchemaError(cause)
|
|
? { description: SchemaIssue.makeFormatterDefault()(cause.issue) }
|
|
: {}),
|
|
});
|
|
}
|
|
|
|
type LegacyDefaultModelSelectionInput = {
|
|
readonly defaultModelSelection?: ModelSelection | null | undefined;
|
|
readonly defaultProvider?: ProviderKind | undefined;
|
|
readonly defaultModel?: string | null | undefined;
|
|
readonly defaultModelOptions?: unknown | null | undefined;
|
|
};
|
|
|
|
type LegacyThreadModelSelectionInput = {
|
|
readonly modelSelection?: ModelSelection | undefined;
|
|
readonly provider?: ProviderKind | undefined;
|
|
readonly model?: string | undefined;
|
|
readonly modelOptions?: unknown | null | undefined;
|
|
};
|
|
|
|
function normalizeLegacyDefaultModelSelection(
|
|
input: LegacyDefaultModelSelectionInput,
|
|
): Effect.Effect<ModelSelection | null, SchemaIssue.InvalidValue> {
|
|
if (input.defaultModelSelection !== undefined) {
|
|
return Effect.succeed(input.defaultModelSelection);
|
|
}
|
|
if (input.defaultModel === undefined || input.defaultModel === null) {
|
|
return Effect.succeed(null);
|
|
}
|
|
return Effect.try({
|
|
try: () =>
|
|
decodeModelSelectionSync({
|
|
provider: input.defaultProvider ?? DEFAULT_PROVIDER_KIND,
|
|
model: input.defaultModel,
|
|
...(input.defaultModelOptions != null ? { options: input.defaultModelOptions } : {}),
|
|
}),
|
|
catch: (cause) =>
|
|
invalidLegacyModelSelectionIssue(
|
|
input,
|
|
"Invalid legacy default model selection payload",
|
|
cause,
|
|
),
|
|
});
|
|
}
|
|
|
|
function normalizeLegacyThreadModelSelection(
|
|
input: LegacyThreadModelSelectionInput,
|
|
): Effect.Effect<ModelSelection | undefined, SchemaIssue.InvalidValue> {
|
|
if (input.modelSelection !== undefined) {
|
|
return Effect.succeed(input.modelSelection);
|
|
}
|
|
if (input.model === undefined) {
|
|
return Effect.succeed(undefined);
|
|
}
|
|
return Effect.try({
|
|
try: () =>
|
|
decodeModelSelectionSync({
|
|
provider: input.provider ?? DEFAULT_PROVIDER_KIND,
|
|
model: input.model,
|
|
...(input.modelOptions != null ? { options: input.modelOptions } : {}),
|
|
}),
|
|
catch: (cause) =>
|
|
invalidLegacyModelSelectionIssue(
|
|
input,
|
|
"Invalid legacy thread model selection payload",
|
|
cause,
|
|
),
|
|
});
|
|
}
|
|
|
|
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,
|
|
defaultModelSelection: Schema.NullOr(ModelSelection),
|
|
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),
|
|
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),
|
|
sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
|
|
});
|
|
export type OrchestrationLatestTurn = typeof OrchestrationLatestTurn.Type;
|
|
|
|
export const OrchestrationThread = Schema.Struct({
|
|
id: ThreadId,
|
|
projectId: ProjectId,
|
|
title: TrimmedNonEmptyString,
|
|
modelSelection: ModelSelection,
|
|
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,
|
|
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
const ProjectMetaUpdateCommand = Schema.Struct({
|
|
type: Schema.Literal("project.meta.update"),
|
|
commandId: CommandId,
|
|
projectId: ProjectId,
|
|
title: Schema.optional(TrimmedNonEmptyString),
|
|
workspaceRoot: Schema.optional(TrimmedNonEmptyString),
|
|
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
|
|
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,
|
|
modelSelection: ModelSelection,
|
|
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),
|
|
modelSelection: Schema.optional(ModelSelection),
|
|
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),
|
|
}),
|
|
modelSelection: Schema.optional(ModelSelection),
|
|
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),
|
|
}),
|
|
modelSelection: Schema.optional(ModelSelection),
|
|
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,
|
|
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,
|
|
threadId: ThreadId,
|
|
messageId: MessageId,
|
|
turnId: Schema.optional(TurnId),
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
const ThreadProposedPlanUpsertCommand = Schema.Struct({
|
|
type: Schema.Literal("thread.proposed-plan.upsert"),
|
|
commandId: CommandId,
|
|
threadId: ThreadId,
|
|
proposedPlan: OrchestrationProposedPlan,
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
const ThreadTurnDiffCompleteCommand = Schema.Struct({
|
|
type: Schema.Literal("thread.turn.diff.complete"),
|
|
commandId: CommandId,
|
|
threadId: ThreadId,
|
|
turnId: TurnId,
|
|
completedAt: IsoDateTime,
|
|
checkpointRef: CheckpointRef,
|
|
status: OrchestrationCheckpointStatus,
|
|
files: Schema.Array(OrchestrationCheckpointFile),
|
|
assistantMessageId: Schema.optional(MessageId),
|
|
checkpointTurnCount: NonNegativeInt,
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
const ThreadActivityAppendCommand = Schema.Struct({
|
|
type: Schema.Literal("thread.activity.append"),
|
|
commandId: CommandId,
|
|
threadId: ThreadId,
|
|
activity: OrchestrationThreadActivity,
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
const ThreadRevertCompleteCommand = Schema.Struct({
|
|
type: Schema.Literal("thread.revert.complete"),
|
|
commandId: CommandId,
|
|
threadId: ThreadId,
|
|
turnCount: NonNegativeInt,
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
const InternalOrchestrationCommand = Schema.Union([
|
|
ThreadSessionSetCommand,
|
|
ThreadMessageAssistantDeltaCommand,
|
|
ThreadMessageAssistantCompleteCommand,
|
|
ThreadProposedPlanUpsertCommand,
|
|
ThreadTurnDiffCompleteCommand,
|
|
ThreadActivityAppendCommand,
|
|
ThreadRevertCompleteCommand,
|
|
]);
|
|
export type InternalOrchestrationCommand = typeof InternalOrchestrationCommand.Type;
|
|
|
|
export const OrchestrationCommand = Schema.Union([
|
|
DispatchableClientOrchestrationCommand,
|
|
InternalOrchestrationCommand,
|
|
]);
|
|
export type OrchestrationCommand = typeof OrchestrationCommand.Type;
|
|
|
|
export const OrchestrationEventType = Schema.Literals([
|
|
"project.created",
|
|
"project.meta-updated",
|
|
"project.deleted",
|
|
"thread.created",
|
|
"thread.deleted",
|
|
"thread.meta-updated",
|
|
"thread.runtime-mode-set",
|
|
"thread.interaction-mode-set",
|
|
"thread.message-sent",
|
|
"thread.turn-start-requested",
|
|
"thread.turn-interrupt-requested",
|
|
"thread.approval-response-requested",
|
|
"thread.user-input-response-requested",
|
|
"thread.checkpoint-revert-requested",
|
|
"thread.reverted",
|
|
"thread.session-stop-requested",
|
|
"thread.session-set",
|
|
"thread.proposed-plan-upserted",
|
|
"thread.turn-diff-completed",
|
|
"thread.activity-appended",
|
|
]);
|
|
export type OrchestrationEventType = typeof OrchestrationEventType.Type;
|
|
|
|
export const OrchestrationAggregateKind = Schema.Literals(["project", "thread"]);
|
|
export type OrchestrationAggregateKind = typeof OrchestrationAggregateKind.Type;
|
|
export const OrchestrationActorKind = Schema.Literals(["client", "server", "provider"]);
|
|
|
|
const ProjectCreatedPayloadCanonical = Schema.Struct({
|
|
projectId: ProjectId,
|
|
title: TrimmedNonEmptyString,
|
|
workspaceRoot: TrimmedNonEmptyString,
|
|
defaultModelSelection: Schema.NullOr(ModelSelection),
|
|
scripts: Schema.Array(ProjectScript),
|
|
createdAt: IsoDateTime,
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
const ProjectCreatedPayloadSource = Schema.Struct({
|
|
projectId: ProjectId,
|
|
title: TrimmedNonEmptyString,
|
|
workspaceRoot: TrimmedNonEmptyString,
|
|
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
|
|
...LegacyDefaultModelSelectionFields,
|
|
scripts: Schema.Array(ProjectScript),
|
|
createdAt: IsoDateTime,
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
type ProjectCreatedPayloadSource = typeof ProjectCreatedPayloadSource.Type;
|
|
export const ProjectCreatedPayload = ProjectCreatedPayloadSource.pipe(
|
|
Schema.decodeTo(
|
|
Schema.toType(ProjectCreatedPayloadCanonical),
|
|
SchemaTransformation.transformOrFail({
|
|
decode: (input) =>
|
|
normalizeLegacyDefaultModelSelection(input).pipe(
|
|
Effect.map((defaultModelSelection) => ({
|
|
projectId: input.projectId,
|
|
title: input.title,
|
|
workspaceRoot: input.workspaceRoot,
|
|
defaultModelSelection,
|
|
scripts: input.scripts,
|
|
createdAt: input.createdAt,
|
|
updatedAt: input.updatedAt,
|
|
})),
|
|
),
|
|
encode: (canonical) =>
|
|
Effect.succeed({
|
|
projectId: canonical.projectId,
|
|
title: canonical.title,
|
|
workspaceRoot: canonical.workspaceRoot,
|
|
defaultModelSelection: canonical.defaultModelSelection,
|
|
scripts: canonical.scripts,
|
|
createdAt: canonical.createdAt,
|
|
updatedAt: canonical.updatedAt,
|
|
} as ProjectCreatedPayloadSource),
|
|
}),
|
|
),
|
|
);
|
|
|
|
const ProjectMetaUpdatedPayloadCanonical = Schema.Struct({
|
|
projectId: ProjectId,
|
|
title: Schema.optional(TrimmedNonEmptyString),
|
|
workspaceRoot: Schema.optional(TrimmedNonEmptyString),
|
|
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
|
|
scripts: Schema.optional(Schema.Array(ProjectScript)),
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
type ProjectMetaUpdatedPayloadCanonical = typeof ProjectMetaUpdatedPayloadCanonical.Type;
|
|
const ProjectMetaUpdatedPayloadSource = Schema.Struct({
|
|
projectId: ProjectId,
|
|
title: Schema.optional(TrimmedNonEmptyString),
|
|
workspaceRoot: Schema.optional(TrimmedNonEmptyString),
|
|
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
|
|
...LegacyDefaultModelSelectionFields,
|
|
scripts: Schema.optional(Schema.Array(ProjectScript)),
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
type ProjectMetaUpdatedPayloadSource = typeof ProjectMetaUpdatedPayloadSource.Type;
|
|
export const ProjectMetaUpdatedPayload = ProjectMetaUpdatedPayloadSource.pipe(
|
|
Schema.decodeTo(
|
|
Schema.toType(ProjectMetaUpdatedPayloadCanonical),
|
|
SchemaTransformation.transformOrFail({
|
|
decode: (input: ProjectMetaUpdatedPayloadSource) =>
|
|
normalizeLegacyDefaultModelSelection(input).pipe(
|
|
Effect.map((defaultModelSelection) => ({
|
|
projectId: input.projectId,
|
|
...(input.title !== undefined ? { title: input.title } : {}),
|
|
...(input.workspaceRoot !== undefined ? { workspaceRoot: input.workspaceRoot } : {}),
|
|
...(input.defaultModelSelection !== undefined ||
|
|
input.defaultModel !== undefined ||
|
|
input.defaultProvider !== undefined ||
|
|
input.defaultModelOptions !== undefined
|
|
? { defaultModelSelection }
|
|
: {}),
|
|
...(input.scripts !== undefined ? { scripts: input.scripts } : {}),
|
|
updatedAt: input.updatedAt,
|
|
})),
|
|
),
|
|
encode: (canonical: ProjectMetaUpdatedPayloadCanonical) =>
|
|
Effect.succeed({
|
|
projectId: canonical.projectId,
|
|
...(canonical.title !== undefined ? { title: canonical.title } : {}),
|
|
...(canonical.workspaceRoot !== undefined
|
|
? { workspaceRoot: canonical.workspaceRoot }
|
|
: {}),
|
|
...(canonical.defaultModelSelection !== undefined
|
|
? { defaultModelSelection: canonical.defaultModelSelection }
|
|
: {}),
|
|
...(canonical.scripts !== undefined ? { scripts: canonical.scripts } : {}),
|
|
updatedAt: canonical.updatedAt,
|
|
} as ProjectMetaUpdatedPayloadSource),
|
|
} as any),
|
|
),
|
|
);
|
|
|
|
export const ProjectDeletedPayload = Schema.Struct({
|
|
projectId: ProjectId,
|
|
deletedAt: IsoDateTime,
|
|
});
|
|
|
|
const ThreadCreatedPayloadCanonical = Schema.Struct({
|
|
threadId: ThreadId,
|
|
projectId: ProjectId,
|
|
title: TrimmedNonEmptyString,
|
|
modelSelection: ModelSelection,
|
|
runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
|
|
interactionMode: ProviderInteractionMode.pipe(
|
|
Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
|
|
),
|
|
branch: Schema.NullOr(TrimmedNonEmptyString),
|
|
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
|
|
createdAt: IsoDateTime,
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
const ThreadCreatedPayloadSource = Schema.Struct({
|
|
threadId: ThreadId,
|
|
projectId: ProjectId,
|
|
title: TrimmedNonEmptyString,
|
|
modelSelection: Schema.optional(ModelSelection),
|
|
...LegacyThreadModelSelectionFields,
|
|
runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
|
|
interactionMode: ProviderInteractionMode.pipe(
|
|
Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
|
|
),
|
|
branch: Schema.NullOr(TrimmedNonEmptyString),
|
|
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
|
|
createdAt: IsoDateTime,
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
type ThreadCreatedPayloadSource = typeof ThreadCreatedPayloadSource.Type;
|
|
export const ThreadCreatedPayload = ThreadCreatedPayloadSource.pipe(
|
|
Schema.decodeTo(
|
|
Schema.toType(ThreadCreatedPayloadCanonical),
|
|
SchemaTransformation.transformOrFail({
|
|
decode: (input) =>
|
|
normalizeLegacyThreadModelSelection(input).pipe(
|
|
Effect.flatMap((modelSelection) =>
|
|
modelSelection === undefined
|
|
? Effect.fail(
|
|
new SchemaIssue.InvalidValue(Option.some(input), {
|
|
title: "Legacy thread.created payload is missing a model selection",
|
|
}),
|
|
)
|
|
: Effect.succeed({
|
|
threadId: input.threadId,
|
|
projectId: input.projectId,
|
|
title: input.title,
|
|
modelSelection,
|
|
runtimeMode: input.runtimeMode,
|
|
interactionMode: input.interactionMode,
|
|
branch: input.branch,
|
|
worktreePath: input.worktreePath,
|
|
createdAt: input.createdAt,
|
|
updatedAt: input.updatedAt,
|
|
}),
|
|
),
|
|
),
|
|
encode: (canonical) =>
|
|
Effect.succeed({
|
|
threadId: canonical.threadId,
|
|
projectId: canonical.projectId,
|
|
title: canonical.title,
|
|
modelSelection: canonical.modelSelection,
|
|
runtimeMode: canonical.runtimeMode,
|
|
interactionMode: canonical.interactionMode,
|
|
branch: canonical.branch,
|
|
worktreePath: canonical.worktreePath,
|
|
createdAt: canonical.createdAt,
|
|
updatedAt: canonical.updatedAt,
|
|
} as ThreadCreatedPayloadSource),
|
|
}),
|
|
),
|
|
);
|
|
|
|
export const ThreadDeletedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
deletedAt: IsoDateTime,
|
|
});
|
|
|
|
const ThreadMetaUpdatedPayloadCanonical = Schema.Struct({
|
|
threadId: ThreadId,
|
|
title: Schema.optional(TrimmedNonEmptyString),
|
|
modelSelection: Schema.optional(ModelSelection),
|
|
branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
|
|
worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
type ThreadMetaUpdatedPayloadCanonical = typeof ThreadMetaUpdatedPayloadCanonical.Type;
|
|
const ThreadMetaUpdatedPayloadSource = Schema.Struct({
|
|
threadId: ThreadId,
|
|
title: Schema.optional(TrimmedNonEmptyString),
|
|
modelSelection: Schema.optional(ModelSelection),
|
|
...LegacyThreadModelSelectionFields,
|
|
branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
|
|
worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
type ThreadMetaUpdatedPayloadSource = typeof ThreadMetaUpdatedPayloadSource.Type;
|
|
export const ThreadMetaUpdatedPayload = ThreadMetaUpdatedPayloadSource.pipe(
|
|
Schema.decodeTo(
|
|
Schema.toType(ThreadMetaUpdatedPayloadCanonical),
|
|
SchemaTransformation.transformOrFail({
|
|
decode: (input: ThreadMetaUpdatedPayloadSource) =>
|
|
normalizeLegacyThreadModelSelection(input).pipe(
|
|
Effect.map((modelSelection) => ({
|
|
threadId: input.threadId,
|
|
...(input.title !== undefined ? { title: input.title } : {}),
|
|
...(modelSelection !== undefined ? { modelSelection } : {}),
|
|
...(input.branch !== undefined ? { branch: input.branch } : {}),
|
|
...(input.worktreePath !== undefined ? { worktreePath: input.worktreePath } : {}),
|
|
updatedAt: input.updatedAt,
|
|
})),
|
|
),
|
|
encode: (canonical: ThreadMetaUpdatedPayloadCanonical) =>
|
|
Effect.succeed({
|
|
threadId: canonical.threadId,
|
|
...(canonical.title !== undefined ? { title: canonical.title } : {}),
|
|
...(canonical.modelSelection !== undefined
|
|
? { modelSelection: canonical.modelSelection }
|
|
: {}),
|
|
...(canonical.branch !== undefined ? { branch: canonical.branch } : {}),
|
|
...(canonical.worktreePath !== undefined ? { worktreePath: canonical.worktreePath } : {}),
|
|
updatedAt: canonical.updatedAt,
|
|
} as ThreadMetaUpdatedPayloadSource),
|
|
} as any),
|
|
),
|
|
);
|
|
|
|
export const ThreadRuntimeModeSetPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
runtimeMode: RuntimeMode,
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
|
|
export const ThreadInteractionModeSetPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
interactionMode: ProviderInteractionMode.pipe(
|
|
Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
|
|
),
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
|
|
export const ThreadMessageSentPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
messageId: MessageId,
|
|
role: OrchestrationMessageRole,
|
|
text: Schema.String,
|
|
attachments: Schema.optional(Schema.Array(ChatAttachment)),
|
|
turnId: Schema.NullOr(TurnId),
|
|
streaming: Schema.Boolean,
|
|
createdAt: IsoDateTime,
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
|
|
const ThreadTurnStartRequestedPayloadCanonical = Schema.Struct({
|
|
threadId: ThreadId,
|
|
messageId: MessageId,
|
|
modelSelection: Schema.optional(ModelSelection),
|
|
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,
|
|
});
|
|
type ThreadTurnStartRequestedPayloadCanonical =
|
|
typeof ThreadTurnStartRequestedPayloadCanonical.Type;
|
|
const ThreadTurnStartRequestedPayloadSource = Schema.Struct({
|
|
threadId: ThreadId,
|
|
messageId: MessageId,
|
|
modelSelection: Schema.optional(ModelSelection),
|
|
...LegacyThreadModelSelectionFields,
|
|
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,
|
|
});
|
|
type ThreadTurnStartRequestedPayloadSource = typeof ThreadTurnStartRequestedPayloadSource.Type;
|
|
export const ThreadTurnStartRequestedPayload = ThreadTurnStartRequestedPayloadSource.pipe(
|
|
Schema.decodeTo(
|
|
Schema.toType(ThreadTurnStartRequestedPayloadCanonical),
|
|
SchemaTransformation.transformOrFail({
|
|
decode: (input: ThreadTurnStartRequestedPayloadSource) =>
|
|
normalizeLegacyThreadModelSelection(input).pipe(
|
|
Effect.map((modelSelection) => ({
|
|
threadId: input.threadId,
|
|
messageId: input.messageId,
|
|
...(modelSelection !== undefined ? { modelSelection } : {}),
|
|
...(input.providerOptions !== undefined
|
|
? { providerOptions: input.providerOptions }
|
|
: {}),
|
|
...(input.assistantDeliveryMode !== undefined
|
|
? { assistantDeliveryMode: input.assistantDeliveryMode }
|
|
: {}),
|
|
runtimeMode: input.runtimeMode,
|
|
interactionMode: input.interactionMode,
|
|
...(input.sourceProposedPlan !== undefined
|
|
? { sourceProposedPlan: input.sourceProposedPlan }
|
|
: {}),
|
|
createdAt: input.createdAt,
|
|
})),
|
|
),
|
|
encode: (canonical: ThreadTurnStartRequestedPayloadCanonical) =>
|
|
Effect.succeed({
|
|
threadId: canonical.threadId,
|
|
messageId: canonical.messageId,
|
|
...(canonical.modelSelection !== undefined
|
|
? { modelSelection: canonical.modelSelection }
|
|
: {}),
|
|
...(canonical.providerOptions !== undefined
|
|
? { providerOptions: canonical.providerOptions }
|
|
: {}),
|
|
...(canonical.assistantDeliveryMode !== undefined
|
|
? { assistantDeliveryMode: canonical.assistantDeliveryMode }
|
|
: {}),
|
|
runtimeMode: canonical.runtimeMode,
|
|
interactionMode: canonical.interactionMode,
|
|
...(canonical.sourceProposedPlan !== undefined
|
|
? { sourceProposedPlan: canonical.sourceProposedPlan }
|
|
: {}),
|
|
createdAt: canonical.createdAt,
|
|
} as ThreadTurnStartRequestedPayloadSource),
|
|
} as any),
|
|
),
|
|
);
|
|
|
|
export const ThreadTurnInterruptRequestedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
turnId: Schema.optional(TurnId),
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
export const ThreadApprovalResponseRequestedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
requestId: ApprovalRequestId,
|
|
decision: ProviderApprovalDecision,
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
const ThreadUserInputResponseRequestedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
requestId: ApprovalRequestId,
|
|
answers: ProviderUserInputAnswers,
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
export const ThreadCheckpointRevertRequestedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
turnCount: NonNegativeInt,
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
export const ThreadRevertedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
turnCount: NonNegativeInt,
|
|
});
|
|
|
|
export const ThreadSessionStopRequestedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
createdAt: IsoDateTime,
|
|
});
|
|
|
|
export const ThreadSessionSetPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
session: OrchestrationSession,
|
|
});
|
|
|
|
export const ThreadProposedPlanUpsertedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
proposedPlan: OrchestrationProposedPlan,
|
|
});
|
|
|
|
export const ThreadTurnDiffCompletedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
turnId: TurnId,
|
|
checkpointTurnCount: NonNegativeInt,
|
|
checkpointRef: CheckpointRef,
|
|
status: OrchestrationCheckpointStatus,
|
|
files: Schema.Array(OrchestrationCheckpointFile),
|
|
assistantMessageId: Schema.NullOr(MessageId),
|
|
completedAt: IsoDateTime,
|
|
});
|
|
|
|
export const ThreadActivityAppendedPayload = Schema.Struct({
|
|
threadId: ThreadId,
|
|
activity: OrchestrationThreadActivity,
|
|
});
|
|
|
|
export const OrchestrationEventMetadata = Schema.Struct({
|
|
providerTurnId: Schema.optional(TrimmedNonEmptyString),
|
|
providerItemId: Schema.optional(ProviderItemId),
|
|
adapterKey: Schema.optional(TrimmedNonEmptyString),
|
|
requestId: Schema.optional(ApprovalRequestId),
|
|
ingestedAt: Schema.optional(IsoDateTime),
|
|
});
|
|
export type OrchestrationEventMetadata = typeof OrchestrationEventMetadata.Type;
|
|
|
|
const EventBaseFields = {
|
|
sequence: NonNegativeInt,
|
|
eventId: EventId,
|
|
aggregateKind: OrchestrationAggregateKind,
|
|
aggregateId: Schema.Union([ProjectId, ThreadId]),
|
|
occurredAt: IsoDateTime,
|
|
commandId: Schema.NullOr(CommandId),
|
|
causationEventId: Schema.NullOr(EventId),
|
|
correlationId: Schema.NullOr(CommandId),
|
|
metadata: OrchestrationEventMetadata,
|
|
} as const;
|
|
|
|
export const OrchestrationEvent = Schema.Union([
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("project.created"),
|
|
payload: ProjectCreatedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("project.meta-updated"),
|
|
payload: ProjectMetaUpdatedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("project.deleted"),
|
|
payload: ProjectDeletedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.created"),
|
|
payload: ThreadCreatedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.deleted"),
|
|
payload: ThreadDeletedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.meta-updated"),
|
|
payload: ThreadMetaUpdatedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.runtime-mode-set"),
|
|
payload: ThreadRuntimeModeSetPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.interaction-mode-set"),
|
|
payload: ThreadInteractionModeSetPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.message-sent"),
|
|
payload: ThreadMessageSentPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.turn-start-requested"),
|
|
payload: ThreadTurnStartRequestedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.turn-interrupt-requested"),
|
|
payload: ThreadTurnInterruptRequestedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.approval-response-requested"),
|
|
payload: ThreadApprovalResponseRequestedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.user-input-response-requested"),
|
|
payload: ThreadUserInputResponseRequestedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.checkpoint-revert-requested"),
|
|
payload: ThreadCheckpointRevertRequestedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.reverted"),
|
|
payload: ThreadRevertedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.session-stop-requested"),
|
|
payload: ThreadSessionStopRequestedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.session-set"),
|
|
payload: ThreadSessionSetPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.proposed-plan-upserted"),
|
|
payload: ThreadProposedPlanUpsertedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.turn-diff-completed"),
|
|
payload: ThreadTurnDiffCompletedPayload,
|
|
}),
|
|
Schema.Struct({
|
|
...EventBaseFields,
|
|
type: Schema.Literal("thread.activity-appended"),
|
|
payload: ThreadActivityAppendedPayload,
|
|
}),
|
|
]);
|
|
export type OrchestrationEvent = typeof OrchestrationEvent.Type;
|
|
|
|
export const OrchestrationCommandReceiptStatus = Schema.Literals(["accepted", "rejected"]);
|
|
export type OrchestrationCommandReceiptStatus = typeof OrchestrationCommandReceiptStatus.Type;
|
|
|
|
export const TurnCountRange = Schema.Struct({
|
|
fromTurnCount: NonNegativeInt,
|
|
toTurnCount: NonNegativeInt,
|
|
}).check(
|
|
Schema.makeFilter(
|
|
(input) =>
|
|
input.fromTurnCount <= input.toTurnCount ||
|
|
new SchemaIssue.InvalidValue(Option.some(input.fromTurnCount), {
|
|
message: "fromTurnCount must be less than or equal to toTurnCount",
|
|
}),
|
|
{ identifier: "OrchestrationTurnDiffRange" },
|
|
),
|
|
);
|
|
|
|
export const ThreadTurnDiff = TurnCountRange.mapFields(
|
|
Struct.assign({
|
|
threadId: ThreadId,
|
|
diff: Schema.String,
|
|
}),
|
|
{ unsafePreserveChecks: true },
|
|
);
|
|
|
|
export const ProviderSessionRuntimeStatus = Schema.Literals([
|
|
"starting",
|
|
"running",
|
|
"stopped",
|
|
"error",
|
|
]);
|
|
export type ProviderSessionRuntimeStatus = typeof ProviderSessionRuntimeStatus.Type;
|
|
|
|
const ProjectionThreadTurnStatus = Schema.Literals([
|
|
"running",
|
|
"completed",
|
|
"interrupted",
|
|
"error",
|
|
]);
|
|
export type ProjectionThreadTurnStatus = typeof ProjectionThreadTurnStatus.Type;
|
|
|
|
const ProjectionCheckpointRow = Schema.Struct({
|
|
threadId: ThreadId,
|
|
turnId: TurnId,
|
|
checkpointTurnCount: NonNegativeInt,
|
|
checkpointRef: CheckpointRef,
|
|
status: OrchestrationCheckpointStatus,
|
|
files: Schema.Array(OrchestrationCheckpointFile),
|
|
assistantMessageId: Schema.NullOr(MessageId),
|
|
completedAt: IsoDateTime,
|
|
});
|
|
export type ProjectionCheckpointRow = typeof ProjectionCheckpointRow.Type;
|
|
|
|
export const ProjectionPendingApprovalStatus = Schema.Literals(["pending", "resolved"]);
|
|
export type ProjectionPendingApprovalStatus = typeof ProjectionPendingApprovalStatus.Type;
|
|
|
|
export const ProjectionPendingApprovalDecision = Schema.NullOr(ProviderApprovalDecision);
|
|
export type ProjectionPendingApprovalDecision = typeof ProjectionPendingApprovalDecision.Type;
|
|
|
|
export const DispatchResult = Schema.Struct({
|
|
sequence: NonNegativeInt,
|
|
});
|
|
export type DispatchResult = typeof DispatchResult.Type;
|
|
|
|
export const OrchestrationGetSnapshotInput = Schema.Struct({});
|
|
export type OrchestrationGetSnapshotInput = typeof OrchestrationGetSnapshotInput.Type;
|
|
const OrchestrationGetSnapshotResult = OrchestrationReadModel;
|
|
export type OrchestrationGetSnapshotResult = typeof OrchestrationGetSnapshotResult.Type;
|
|
|
|
export const OrchestrationGetTurnDiffInput = TurnCountRange.mapFields(
|
|
Struct.assign({ threadId: ThreadId }),
|
|
{ unsafePreserveChecks: true },
|
|
);
|
|
export type OrchestrationGetTurnDiffInput = typeof OrchestrationGetTurnDiffInput.Type;
|
|
|
|
export const OrchestrationGetTurnDiffResult = ThreadTurnDiff;
|
|
export type OrchestrationGetTurnDiffResult = typeof OrchestrationGetTurnDiffResult.Type;
|
|
|
|
export const OrchestrationGetFullThreadDiffInput = Schema.Struct({
|
|
threadId: ThreadId,
|
|
toTurnCount: NonNegativeInt,
|
|
});
|
|
export type OrchestrationGetFullThreadDiffInput = typeof OrchestrationGetFullThreadDiffInput.Type;
|
|
|
|
export const OrchestrationGetFullThreadDiffResult = ThreadTurnDiff;
|
|
export type OrchestrationGetFullThreadDiffResult = typeof OrchestrationGetFullThreadDiffResult.Type;
|
|
|
|
export const OrchestrationReplayEventsInput = Schema.Struct({
|
|
fromSequenceExclusive: NonNegativeInt,
|
|
});
|
|
export type OrchestrationReplayEventsInput = typeof OrchestrationReplayEventsInput.Type;
|
|
|
|
const OrchestrationReplayEventsResult = Schema.Array(OrchestrationEvent);
|
|
export type OrchestrationReplayEventsResult = typeof OrchestrationReplayEventsResult.Type;
|
|
|
|
export const OrchestrationRpcSchemas = {
|
|
getSnapshot: {
|
|
input: OrchestrationGetSnapshotInput,
|
|
output: OrchestrationGetSnapshotResult,
|
|
},
|
|
dispatchCommand: {
|
|
input: ClientOrchestrationCommand,
|
|
output: DispatchResult,
|
|
},
|
|
getTurnDiff: {
|
|
input: OrchestrationGetTurnDiffInput,
|
|
output: OrchestrationGetTurnDiffResult,
|
|
},
|
|
getFullThreadDiff: {
|
|
input: OrchestrationGetFullThreadDiffInput,
|
|
output: OrchestrationGetFullThreadDiffResult,
|
|
},
|
|
replayEvents: {
|
|
input: OrchestrationReplayEventsInput,
|
|
output: OrchestrationReplayEventsResult,
|
|
},
|
|
} as const;
|