Open History to see the last commit
1import { Option, Schema, SchemaIssue, Struct } from "effect";
2import { ClaudeModelOptions, CodexModelOptions } from "./model";
3import { RepositoryIdentity } from "./environment";
4import {
5 ApprovalRequestId,
6 CheckpointRef,
7 CommandId,
8 EventId,
9 IsoDateTime,
10 MessageId,
11 NonNegativeInt,
12 ProjectId,
13 ProviderItemId,
14 ThreadId,
15 TrimmedNonEmptyString,
16 TurnId,
17} from "./baseSchemas";
18
19export const ORCHESTRATION_WS_METHODS = {
20 getSnapshot: "orchestration.getSnapshot",
21 dispatchCommand: "orchestration.dispatchCommand",
22 getTurnDiff: "orchestration.getTurnDiff",
23 getFullThreadDiff: "orchestration.getFullThreadDiff",
24 replayEvents: "orchestration.replayEvents",
25} as const;
26
27export const ProviderKind = Schema.Literals(["codex", "claudeAgent"]);
28export type ProviderKind = typeof ProviderKind.Type;
29export const ProviderApprovalPolicy = Schema.Literals([
30 "untrusted",
31 "on-failure",
32 "on-request",
33 "never",
34]);
35export type ProviderApprovalPolicy = typeof ProviderApprovalPolicy.Type;
36export const ProviderSandboxMode = Schema.Literals([
37 "read-only",
38 "workspace-write",
39 "danger-full-access",
40]);
41export type ProviderSandboxMode = typeof ProviderSandboxMode.Type;
42
43export const DEFAULT_PROVIDER_KIND: ProviderKind = "codex";
44
45export const CodexModelSelection = Schema.Struct({
46 provider: Schema.Literal("codex"),
47 model: TrimmedNonEmptyString,
48 options: Schema.optionalKey(CodexModelOptions),
49});
50export type CodexModelSelection = typeof CodexModelSelection.Type;
51
52export const ClaudeModelSelection = Schema.Struct({
53 provider: Schema.Literal("claudeAgent"),
54 model: TrimmedNonEmptyString,
55 options: Schema.optionalKey(ClaudeModelOptions),
56});
57export type ClaudeModelSelection = typeof ClaudeModelSelection.Type;
58
59export const ModelSelection = Schema.Union([CodexModelSelection, ClaudeModelSelection]);
60export type ModelSelection = typeof ModelSelection.Type;
61
62export const RuntimeMode = Schema.Literals(["approval-required", "full-access"]);
63export type RuntimeMode = typeof RuntimeMode.Type;
64export const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access";
65export const ProviderInteractionMode = Schema.Literals(["default", "plan"]);
66export type ProviderInteractionMode = typeof ProviderInteractionMode.Type;
67export const DEFAULT_PROVIDER_INTERACTION_MODE: ProviderInteractionMode = "default";
68export const ProviderRequestKind = Schema.Literals(["command", "file-read", "file-change"]);
69export type ProviderRequestKind = typeof ProviderRequestKind.Type;
70export const AssistantDeliveryMode = Schema.Literals(["buffered", "streaming"]);
71export type AssistantDeliveryMode = typeof AssistantDeliveryMode.Type;
72export const ProviderApprovalDecision = Schema.Literals([
73 "accept",
74 "acceptForSession",
75 "decline",
76 "cancel",
77]);
78export type ProviderApprovalDecision = typeof ProviderApprovalDecision.Type;
79export const ProviderUserInputAnswers = Schema.Record(Schema.String, Schema.Unknown);
80export type ProviderUserInputAnswers = typeof ProviderUserInputAnswers.Type;
81
82export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
83export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;
84export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
85const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
86const CHAT_ATTACHMENT_ID_MAX_CHARS = 128;
87// Correlation id is command id by design in this model.
88export const CorrelationId = CommandId;
89export type CorrelationId = typeof CorrelationId.Type;
90
91const ChatAttachmentId = TrimmedNonEmptyString.check(
92 Schema.isMaxLength(CHAT_ATTACHMENT_ID_MAX_CHARS),
93 Schema.isPattern(/^[a-z0-9_-]+$/i),
94);
95export type ChatAttachmentId = typeof ChatAttachmentId.Type;
96
97export const ChatImageAttachment = Schema.Struct({
98 type: Schema.Literal("image"),
99 id: ChatAttachmentId,
100 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
101 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
102 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
103});
104export type ChatImageAttachment = typeof ChatImageAttachment.Type;
105
106const UploadChatImageAttachment = Schema.Struct({
107 type: Schema.Literal("image"),
108 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
109 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
110 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
111 dataUrl: TrimmedNonEmptyString.check(
112 Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
113 ),
114});
115export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type;
116
117export const ChatAttachment = Schema.Union([ChatImageAttachment]);
118export type ChatAttachment = typeof ChatAttachment.Type;
119const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);
120export type UploadChatAttachment = typeof UploadChatAttachment.Type;
121
122export const ProjectScriptIcon = Schema.Literals([
123 "play",
124 "test",
125 "lint",
126 "configure",
127 "build",
128 "debug",
129]);
130export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
131
132export const ProjectScript = Schema.Struct({
133 id: TrimmedNonEmptyString,
134 name: TrimmedNonEmptyString,
135 command: TrimmedNonEmptyString,
136 icon: ProjectScriptIcon,
137 runOnWorktreeCreate: Schema.Boolean,
138});
139export type ProjectScript = typeof ProjectScript.Type;
140
141export const OrchestrationProject = Schema.Struct({
142 id: ProjectId,
143 title: TrimmedNonEmptyString,
144 workspaceRoot: TrimmedNonEmptyString,
145 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
146 defaultModelSelection: Schema.NullOr(ModelSelection),
147 scripts: Schema.Array(ProjectScript),
148 createdAt: IsoDateTime,
149 updatedAt: IsoDateTime,
150 deletedAt: Schema.NullOr(IsoDateTime),
151});
152export type OrchestrationProject = typeof OrchestrationProject.Type;
153
154export const OrchestrationMessageRole = Schema.Literals(["user", "assistant", "system"]);
155export type OrchestrationMessageRole = typeof OrchestrationMessageRole.Type;
156
157export const OrchestrationMessage = Schema.Struct({
158 id: MessageId,
159 role: OrchestrationMessageRole,
160 text: Schema.String,
161 attachments: Schema.optional(Schema.Array(ChatAttachment)),
162 turnId: Schema.NullOr(TurnId),
163 streaming: Schema.Boolean,
164 createdAt: IsoDateTime,
165 updatedAt: IsoDateTime,
166});
167export type OrchestrationMessage = typeof OrchestrationMessage.Type;
168
169export const OrchestrationProposedPlanId = TrimmedNonEmptyString;
170export type OrchestrationProposedPlanId = typeof OrchestrationProposedPlanId.Type;
171
172export const OrchestrationProposedPlan = Schema.Struct({
173 id: OrchestrationProposedPlanId,
174 turnId: Schema.NullOr(TurnId),
175 planMarkdown: TrimmedNonEmptyString,
176 implementedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(() => null)),
177 implementationThreadId: Schema.NullOr(ThreadId).pipe(Schema.withDecodingDefault(() => null)),
178 createdAt: IsoDateTime,
179 updatedAt: IsoDateTime,
180});
181export type OrchestrationProposedPlan = typeof OrchestrationProposedPlan.Type;
182
183const SourceProposedPlanReference = Schema.Struct({
184 threadId: ThreadId,
185 planId: OrchestrationProposedPlanId,
186});
187
188export const OrchestrationSessionStatus = Schema.Literals([
189 "idle",
190 "starting",
191 "running",
192 "ready",
193 "interrupted",
194 "stopped",
195 "error",
196]);
197export type OrchestrationSessionStatus = typeof OrchestrationSessionStatus.Type;
198
199export const OrchestrationSession = Schema.Struct({
200 threadId: ThreadId,
201 status: OrchestrationSessionStatus,
202 providerName: Schema.NullOr(TrimmedNonEmptyString),
203 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
204 activeTurnId: Schema.NullOr(TurnId),
205 lastError: Schema.NullOr(TrimmedNonEmptyString),
206 updatedAt: IsoDateTime,
207});
208export type OrchestrationSession = typeof OrchestrationSession.Type;
209
210export const OrchestrationCheckpointFile = Schema.Struct({
211 path: TrimmedNonEmptyString,
212 kind: TrimmedNonEmptyString,
213 additions: NonNegativeInt,
214 deletions: NonNegativeInt,
215});
216export type OrchestrationCheckpointFile = typeof OrchestrationCheckpointFile.Type;
217
218export const OrchestrationCheckpointStatus = Schema.Literals(["ready", "missing", "error"]);
219export type OrchestrationCheckpointStatus = typeof OrchestrationCheckpointStatus.Type;
220
221export const OrchestrationCheckpointSummary = Schema.Struct({
222 turnId: TurnId,
223 checkpointTurnCount: NonNegativeInt,
224 checkpointRef: CheckpointRef,
225 status: OrchestrationCheckpointStatus,
226 files: Schema.Array(OrchestrationCheckpointFile),
227 assistantMessageId: Schema.NullOr(MessageId),
228 completedAt: IsoDateTime,
229});
230export type OrchestrationCheckpointSummary = typeof OrchestrationCheckpointSummary.Type;
231
232export const OrchestrationThreadActivityTone = Schema.Literals([
233 "info",
234 "tool",
235 "approval",
236 "error",
237]);
238export type OrchestrationThreadActivityTone = typeof OrchestrationThreadActivityTone.Type;
239
240export const OrchestrationThreadActivity = Schema.Struct({
241 id: EventId,
242 tone: OrchestrationThreadActivityTone,
243 kind: TrimmedNonEmptyString,
244 summary: TrimmedNonEmptyString,
245 payload: Schema.Unknown,
246 turnId: Schema.NullOr(TurnId),
247 sequence: Schema.optional(NonNegativeInt),
248 createdAt: IsoDateTime,
249});
250export type OrchestrationThreadActivity = typeof OrchestrationThreadActivity.Type;
251
252const OrchestrationLatestTurnState = Schema.Literals([
253 "running",
254 "interrupted",
255 "completed",
256 "error",
257]);
258export type OrchestrationLatestTurnState = typeof OrchestrationLatestTurnState.Type;
259
260export const OrchestrationLatestTurn = Schema.Struct({
261 turnId: TurnId,
262 state: OrchestrationLatestTurnState,
263 requestedAt: IsoDateTime,
264 startedAt: Schema.NullOr(IsoDateTime),
265 completedAt: Schema.NullOr(IsoDateTime),
266 assistantMessageId: Schema.NullOr(MessageId),
267 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
268});
269export type OrchestrationLatestTurn = typeof OrchestrationLatestTurn.Type;
270
271export const OrchestrationThread = Schema.Struct({
272 id: ThreadId,
273 projectId: ProjectId,
274 title: TrimmedNonEmptyString,
275 modelSelection: ModelSelection,
276 runtimeMode: RuntimeMode,
277 interactionMode: ProviderInteractionMode.pipe(
278 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
279 ),
280 branch: Schema.NullOr(TrimmedNonEmptyString),
281 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
282 latestTurn: Schema.NullOr(OrchestrationLatestTurn),
283 createdAt: IsoDateTime,
284 updatedAt: IsoDateTime,
285 archivedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(() => null)),
286 deletedAt: Schema.NullOr(IsoDateTime),
287 messages: Schema.Array(OrchestrationMessage),
288 proposedPlans: Schema.Array(OrchestrationProposedPlan).pipe(Schema.withDecodingDefault(() => [])),
289 activities: Schema.Array(OrchestrationThreadActivity),
290 checkpoints: Schema.Array(OrchestrationCheckpointSummary),
291 session: Schema.NullOr(OrchestrationSession),
292});
293export type OrchestrationThread = typeof OrchestrationThread.Type;
294
295export const OrchestrationReadModel = Schema.Struct({
296 snapshotSequence: NonNegativeInt,
297 projects: Schema.Array(OrchestrationProject),
298 threads: Schema.Array(OrchestrationThread),
299 updatedAt: IsoDateTime,
300});
301export type OrchestrationReadModel = typeof OrchestrationReadModel.Type;
302
303export const ProjectCreateCommand = Schema.Struct({
304 type: Schema.Literal("project.create"),
305 commandId: CommandId,
306 projectId: ProjectId,
307 title: TrimmedNonEmptyString,
308 workspaceRoot: TrimmedNonEmptyString,
309 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
310 createdAt: IsoDateTime,
311});
312
313const ProjectMetaUpdateCommand = Schema.Struct({
314 type: Schema.Literal("project.meta.update"),
315 commandId: CommandId,
316 projectId: ProjectId,
317 title: Schema.optional(TrimmedNonEmptyString),
318 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
319 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
320 scripts: Schema.optional(Schema.Array(ProjectScript)),
321});
322
323const ProjectDeleteCommand = Schema.Struct({
324 type: Schema.Literal("project.delete"),
325 commandId: CommandId,
326 projectId: ProjectId,
327});
328
329const ThreadCreateCommand = Schema.Struct({
330 type: Schema.Literal("thread.create"),
331 commandId: CommandId,
332 threadId: ThreadId,
333 projectId: ProjectId,
334 title: TrimmedNonEmptyString,
335 modelSelection: ModelSelection,
336 runtimeMode: RuntimeMode,
337 interactionMode: ProviderInteractionMode.pipe(
338 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
339 ),
340 branch: Schema.NullOr(TrimmedNonEmptyString),
341 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
342 createdAt: IsoDateTime,
343});
344
345const ThreadDeleteCommand = Schema.Struct({
346 type: Schema.Literal("thread.delete"),
347 commandId: CommandId,
348 threadId: ThreadId,
349});
350
351const ThreadArchiveCommand = Schema.Struct({
352 type: Schema.Literal("thread.archive"),
353 commandId: CommandId,
354 threadId: ThreadId,
355});
356
357const ThreadUnarchiveCommand = Schema.Struct({
358 type: Schema.Literal("thread.unarchive"),
359 commandId: CommandId,
360 threadId: ThreadId,
361});
362
363const ThreadMetaUpdateCommand = Schema.Struct({
364 type: Schema.Literal("thread.meta.update"),
365 commandId: CommandId,
366 threadId: ThreadId,
367 title: Schema.optional(TrimmedNonEmptyString),
368 modelSelection: Schema.optional(ModelSelection),
369 branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
370 worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
371});
372
373const ThreadRuntimeModeSetCommand = Schema.Struct({
374 type: Schema.Literal("thread.runtime-mode.set"),
375 commandId: CommandId,
376 threadId: ThreadId,
377 runtimeMode: RuntimeMode,
378 createdAt: IsoDateTime,
379});
380
381const ThreadInteractionModeSetCommand = Schema.Struct({
382 type: Schema.Literal("thread.interaction-mode.set"),
383 commandId: CommandId,
384 threadId: ThreadId,
385 interactionMode: ProviderInteractionMode,
386 createdAt: IsoDateTime,
387});
388
389const ThreadTurnStartBootstrapCreateThread = Schema.Struct({
390 projectId: ProjectId,
391 title: TrimmedNonEmptyString,
392 modelSelection: ModelSelection,
393 runtimeMode: RuntimeMode,
394 interactionMode: ProviderInteractionMode,
395 branch: Schema.NullOr(TrimmedNonEmptyString),
396 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
397 createdAt: IsoDateTime,
398});
399
400const ThreadTurnStartBootstrapPrepareWorktree = Schema.Struct({
401 projectCwd: TrimmedNonEmptyString,
402 baseBranch: TrimmedNonEmptyString,
403 branch: Schema.optional(TrimmedNonEmptyString),
404});
405
406const ThreadTurnStartBootstrap = Schema.Struct({
407 createThread: Schema.optional(ThreadTurnStartBootstrapCreateThread),
408 prepareWorktree: Schema.optional(ThreadTurnStartBootstrapPrepareWorktree),
409 runSetupScript: Schema.optional(Schema.Boolean),
410});
411
412export type ThreadTurnStartBootstrap = typeof ThreadTurnStartBootstrap.Type;
413
414export const ThreadTurnStartCommand = Schema.Struct({
415 type: Schema.Literal("thread.turn.start"),
416 commandId: CommandId,
417 threadId: ThreadId,
418 message: Schema.Struct({
419 messageId: MessageId,
420 role: Schema.Literal("user"),
421 text: Schema.String,
422 attachments: Schema.Array(ChatAttachment),
423 }),
424 modelSelection: Schema.optional(ModelSelection),
425 titleSeed: Schema.optional(TrimmedNonEmptyString),
426 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
427 interactionMode: ProviderInteractionMode.pipe(
428 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
429 ),
430 bootstrap: Schema.optional(ThreadTurnStartBootstrap),
431 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
432 createdAt: IsoDateTime,
433});
434
435const ClientThreadTurnStartCommand = Schema.Struct({
436 type: Schema.Literal("thread.turn.start"),
437 commandId: CommandId,
438 threadId: ThreadId,
439 message: Schema.Struct({
440 messageId: MessageId,
441 role: Schema.Literal("user"),
442 text: Schema.String,
443 attachments: Schema.Array(UploadChatAttachment),
444 }),
445 modelSelection: Schema.optional(ModelSelection),
446 titleSeed: Schema.optional(TrimmedNonEmptyString),
447 runtimeMode: RuntimeMode,
448 interactionMode: ProviderInteractionMode,
449 bootstrap: Schema.optional(ThreadTurnStartBootstrap),
450 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
451 createdAt: IsoDateTime,
452});
453
454const ThreadTurnInterruptCommand = Schema.Struct({
455 type: Schema.Literal("thread.turn.interrupt"),
456 commandId: CommandId,
457 threadId: ThreadId,
458 turnId: Schema.optional(TurnId),
459 createdAt: IsoDateTime,
460});
461
462const ThreadApprovalRespondCommand = Schema.Struct({
463 type: Schema.Literal("thread.approval.respond"),
464 commandId: CommandId,
465 threadId: ThreadId,
466 requestId: ApprovalRequestId,
467 decision: ProviderApprovalDecision,
468 createdAt: IsoDateTime,
469});
470
471const ThreadUserInputRespondCommand = Schema.Struct({
472 type: Schema.Literal("thread.user-input.respond"),
473 commandId: CommandId,
474 threadId: ThreadId,
475 requestId: ApprovalRequestId,
476 answers: ProviderUserInputAnswers,
477 createdAt: IsoDateTime,
478});
479
480const ThreadCheckpointRevertCommand = Schema.Struct({
481 type: Schema.Literal("thread.checkpoint.revert"),
482 commandId: CommandId,
483 threadId: ThreadId,
484 turnCount: NonNegativeInt,
485 createdAt: IsoDateTime,
486});
487
488const ThreadSessionStopCommand = Schema.Struct({
489 type: Schema.Literal("thread.session.stop"),
490 commandId: CommandId,
491 threadId: ThreadId,
492 createdAt: IsoDateTime,
493});
494
495const DispatchableClientOrchestrationCommand = Schema.Union([
496 ProjectCreateCommand,
497 ProjectMetaUpdateCommand,
498 ProjectDeleteCommand,
499 ThreadCreateCommand,
500 ThreadDeleteCommand,
501 ThreadArchiveCommand,
502 ThreadUnarchiveCommand,
503 ThreadMetaUpdateCommand,
504 ThreadRuntimeModeSetCommand,
505 ThreadInteractionModeSetCommand,
506 ThreadTurnStartCommand,
507 ThreadTurnInterruptCommand,
508 ThreadApprovalRespondCommand,
509 ThreadUserInputRespondCommand,
510 ThreadCheckpointRevertCommand,
511 ThreadSessionStopCommand,
512]);
513export type DispatchableClientOrchestrationCommand =
514 typeof DispatchableClientOrchestrationCommand.Type;
515
516export const ClientOrchestrationCommand = Schema.Union([
517 ProjectCreateCommand,
518 ProjectMetaUpdateCommand,
519 ProjectDeleteCommand,
520 ThreadCreateCommand,
521 ThreadDeleteCommand,
522 ThreadArchiveCommand,
523 ThreadUnarchiveCommand,
524 ThreadMetaUpdateCommand,
525 ThreadRuntimeModeSetCommand,
526 ThreadInteractionModeSetCommand,
527 ClientThreadTurnStartCommand,
528 ThreadTurnInterruptCommand,
529 ThreadApprovalRespondCommand,
530 ThreadUserInputRespondCommand,
531 ThreadCheckpointRevertCommand,
532 ThreadSessionStopCommand,
533]);
534export type ClientOrchestrationCommand = typeof ClientOrchestrationCommand.Type;
535
536const ThreadSessionSetCommand = Schema.Struct({
537 type: Schema.Literal("thread.session.set"),
538 commandId: CommandId,
539 threadId: ThreadId,
540 session: OrchestrationSession,
541 createdAt: IsoDateTime,
542});
543
544const ThreadMessageAssistantDeltaCommand = Schema.Struct({
545 type: Schema.Literal("thread.message.assistant.delta"),
546 commandId: CommandId,
547 threadId: ThreadId,
548 messageId: MessageId,
549 delta: Schema.String,
550 turnId: Schema.optional(TurnId),
551 createdAt: IsoDateTime,
552});
553
554const ThreadMessageAssistantCompleteCommand = Schema.Struct({
555 type: Schema.Literal("thread.message.assistant.complete"),
556 commandId: CommandId,
557 threadId: ThreadId,
558 messageId: MessageId,
559 turnId: Schema.optional(TurnId),
560 createdAt: IsoDateTime,
561});
562
563const ThreadProposedPlanUpsertCommand = Schema.Struct({
564 type: Schema.Literal("thread.proposed-plan.upsert"),
565 commandId: CommandId,
566 threadId: ThreadId,
567 proposedPlan: OrchestrationProposedPlan,
568 createdAt: IsoDateTime,
569});
570
571const ThreadTurnDiffCompleteCommand = Schema.Struct({
572 type: Schema.Literal("thread.turn.diff.complete"),
573 commandId: CommandId,
574 threadId: ThreadId,
575 turnId: TurnId,
576 completedAt: IsoDateTime,
577 checkpointRef: CheckpointRef,
578 status: OrchestrationCheckpointStatus,
579 files: Schema.Array(OrchestrationCheckpointFile),
580 assistantMessageId: Schema.optional(MessageId),
581 checkpointTurnCount: NonNegativeInt,
582 createdAt: IsoDateTime,
583});
584
585const ThreadActivityAppendCommand = Schema.Struct({
586 type: Schema.Literal("thread.activity.append"),
587 commandId: CommandId,
588 threadId: ThreadId,
589 activity: OrchestrationThreadActivity,
590 createdAt: IsoDateTime,
591});
592
593const ThreadRevertCompleteCommand = Schema.Struct({
594 type: Schema.Literal("thread.revert.complete"),
595 commandId: CommandId,
596 threadId: ThreadId,
597 turnCount: NonNegativeInt,
598 createdAt: IsoDateTime,
599});
600
601const InternalOrchestrationCommand = Schema.Union([
602 ThreadSessionSetCommand,
603 ThreadMessageAssistantDeltaCommand,
604 ThreadMessageAssistantCompleteCommand,
605 ThreadProposedPlanUpsertCommand,
606 ThreadTurnDiffCompleteCommand,
607 ThreadActivityAppendCommand,
608 ThreadRevertCompleteCommand,
609]);
610export type InternalOrchestrationCommand = typeof InternalOrchestrationCommand.Type;
611
612export const OrchestrationCommand = Schema.Union([
613 DispatchableClientOrchestrationCommand,
614 InternalOrchestrationCommand,
615]);
616export type OrchestrationCommand = typeof OrchestrationCommand.Type;
617
618export const OrchestrationEventType = Schema.Literals([
619 "project.created",
620 "project.meta-updated",
621 "project.deleted",
622 "thread.created",
623 "thread.deleted",
624 "thread.archived",
625 "thread.unarchived",
626 "thread.meta-updated",
627 "thread.runtime-mode-set",
628 "thread.interaction-mode-set",
629 "thread.message-sent",
630 "thread.turn-start-requested",
631 "thread.turn-interrupt-requested",
632 "thread.approval-response-requested",
633 "thread.user-input-response-requested",
634 "thread.checkpoint-revert-requested",
635 "thread.reverted",
636 "thread.session-stop-requested",
637 "thread.session-set",
638 "thread.proposed-plan-upserted",
639 "thread.turn-diff-completed",
640 "thread.activity-appended",
641]);
642export type OrchestrationEventType = typeof OrchestrationEventType.Type;
643
644export const OrchestrationAggregateKind = Schema.Literals(["project", "thread"]);
645export type OrchestrationAggregateKind = typeof OrchestrationAggregateKind.Type;
646export const OrchestrationActorKind = Schema.Literals(["client", "server", "provider"]);
647
648export const ProjectCreatedPayload = Schema.Struct({
649 projectId: ProjectId,
650 title: TrimmedNonEmptyString,
651 workspaceRoot: TrimmedNonEmptyString,
652 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
653 defaultModelSelection: Schema.NullOr(ModelSelection),
654 scripts: Schema.Array(ProjectScript),
655 createdAt: IsoDateTime,
656 updatedAt: IsoDateTime,
657});
658
659export const ProjectMetaUpdatedPayload = Schema.Struct({
660 projectId: ProjectId,
661 title: Schema.optional(TrimmedNonEmptyString),
662 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
663 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
664 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
665 scripts: Schema.optional(Schema.Array(ProjectScript)),
666 updatedAt: IsoDateTime,
667});
668
669export const ProjectDeletedPayload = Schema.Struct({
670 projectId: ProjectId,
671 deletedAt: IsoDateTime,
672});
673
674export const ThreadCreatedPayload = Schema.Struct({
675 threadId: ThreadId,
676 projectId: ProjectId,
677 title: TrimmedNonEmptyString,
678 modelSelection: ModelSelection,
679 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
680 interactionMode: ProviderInteractionMode.pipe(
681 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
682 ),
683 branch: Schema.NullOr(TrimmedNonEmptyString),
684 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
685 createdAt: IsoDateTime,
686 updatedAt: IsoDateTime,
687});
688
689export const ThreadDeletedPayload = Schema.Struct({
690 threadId: ThreadId,
691 deletedAt: IsoDateTime,
692});
693
694export const ThreadArchivedPayload = Schema.Struct({
695 threadId: ThreadId,
696 archivedAt: IsoDateTime,
697 updatedAt: IsoDateTime,
698});
699
700export const ThreadUnarchivedPayload = Schema.Struct({
701 threadId: ThreadId,
702 updatedAt: IsoDateTime,
703});
704
705export const ThreadMetaUpdatedPayload = Schema.Struct({
706 threadId: ThreadId,
707 title: Schema.optional(TrimmedNonEmptyString),
708 modelSelection: Schema.optional(ModelSelection),
709 branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
710 worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
711 updatedAt: IsoDateTime,
712});
713
714export const ThreadRuntimeModeSetPayload = Schema.Struct({
715 threadId: ThreadId,
716 runtimeMode: RuntimeMode,
717 updatedAt: IsoDateTime,
718});
719
720export const ThreadInteractionModeSetPayload = Schema.Struct({
721 threadId: ThreadId,
722 interactionMode: ProviderInteractionMode.pipe(
723 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
724 ),
725 updatedAt: IsoDateTime,
726});
727
728export const ThreadMessageSentPayload = Schema.Struct({
729 threadId: ThreadId,
730 messageId: MessageId,
731 role: OrchestrationMessageRole,
732 text: Schema.String,
733 attachments: Schema.optional(Schema.Array(ChatAttachment)),
734 turnId: Schema.NullOr(TurnId),
735 streaming: Schema.Boolean,
736 createdAt: IsoDateTime,
737 updatedAt: IsoDateTime,
738});
739
740export const ThreadTurnStartRequestedPayload = Schema.Struct({
741 threadId: ThreadId,
742 messageId: MessageId,
743 modelSelection: Schema.optional(ModelSelection),
744 titleSeed: Schema.optional(TrimmedNonEmptyString),
745 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
746 interactionMode: ProviderInteractionMode.pipe(
747 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
748 ),
749 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
750 createdAt: IsoDateTime,
751});
752
753export const ThreadTurnInterruptRequestedPayload = Schema.Struct({
754 threadId: ThreadId,
755 turnId: Schema.optional(TurnId),
756 createdAt: IsoDateTime,
757});
758
759export const ThreadApprovalResponseRequestedPayload = Schema.Struct({
760 threadId: ThreadId,
761 requestId: ApprovalRequestId,
762 decision: ProviderApprovalDecision,
763 createdAt: IsoDateTime,
764});
765
766const ThreadUserInputResponseRequestedPayload = Schema.Struct({
767 threadId: ThreadId,
768 requestId: ApprovalRequestId,
769 answers: ProviderUserInputAnswers,
770 createdAt: IsoDateTime,
771});
772
773export const ThreadCheckpointRevertRequestedPayload = Schema.Struct({
774 threadId: ThreadId,
775 turnCount: NonNegativeInt,
776 createdAt: IsoDateTime,
777});
778
779export const ThreadRevertedPayload = Schema.Struct({
780 threadId: ThreadId,
781 turnCount: NonNegativeInt,
782});
783
784export const ThreadSessionStopRequestedPayload = Schema.Struct({
785 threadId: ThreadId,
786 createdAt: IsoDateTime,
787});
788
789export const ThreadSessionSetPayload = Schema.Struct({
790 threadId: ThreadId,
791 session: OrchestrationSession,
792});
793
794export const ThreadProposedPlanUpsertedPayload = Schema.Struct({
795 threadId: ThreadId,
796 proposedPlan: OrchestrationProposedPlan,
797});
798
799export const ThreadTurnDiffCompletedPayload = Schema.Struct({
800 threadId: ThreadId,
801 turnId: TurnId,
802 checkpointTurnCount: NonNegativeInt,
803 checkpointRef: CheckpointRef,
804 status: OrchestrationCheckpointStatus,
805 files: Schema.Array(OrchestrationCheckpointFile),
806 assistantMessageId: Schema.NullOr(MessageId),
807 completedAt: IsoDateTime,
808});
809
810export const ThreadActivityAppendedPayload = Schema.Struct({
811 threadId: ThreadId,
812 activity: OrchestrationThreadActivity,
813});
814
815export const OrchestrationEventMetadata = Schema.Struct({
816 providerTurnId: Schema.optional(TrimmedNonEmptyString),
817 providerItemId: Schema.optional(ProviderItemId),
818 adapterKey: Schema.optional(TrimmedNonEmptyString),
819 requestId: Schema.optional(ApprovalRequestId),
820 ingestedAt: Schema.optional(IsoDateTime),
821});
822export type OrchestrationEventMetadata = typeof OrchestrationEventMetadata.Type;
823
824const EventBaseFields = {
825 sequence: NonNegativeInt,
826 eventId: EventId,
827 aggregateKind: OrchestrationAggregateKind,
828 aggregateId: Schema.Union([ProjectId, ThreadId]),
829 occurredAt: IsoDateTime,
830 commandId: Schema.NullOr(CommandId),
831 causationEventId: Schema.NullOr(EventId),
832 correlationId: Schema.NullOr(CommandId),
833 metadata: OrchestrationEventMetadata,
834} as const;
835
836export const OrchestrationEvent = Schema.Union([
837 Schema.Struct({
838 ...EventBaseFields,
839 type: Schema.Literal("project.created"),
840 payload: ProjectCreatedPayload,
841 }),
842 Schema.Struct({
843 ...EventBaseFields,
844 type: Schema.Literal("project.meta-updated"),
845 payload: ProjectMetaUpdatedPayload,
846 }),
847 Schema.Struct({
848 ...EventBaseFields,
849 type: Schema.Literal("project.deleted"),
850 payload: ProjectDeletedPayload,
851 }),
852 Schema.Struct({
853 ...EventBaseFields,
854 type: Schema.Literal("thread.created"),
855 payload: ThreadCreatedPayload,
856 }),
857 Schema.Struct({
858 ...EventBaseFields,
859 type: Schema.Literal("thread.deleted"),
860 payload: ThreadDeletedPayload,
861 }),
862 Schema.Struct({
863 ...EventBaseFields,
864 type: Schema.Literal("thread.archived"),
865 payload: ThreadArchivedPayload,
866 }),
867 Schema.Struct({
868 ...EventBaseFields,
869 type: Schema.Literal("thread.unarchived"),
870 payload: ThreadUnarchivedPayload,
871 }),
872 Schema.Struct({
873 ...EventBaseFields,
874 type: Schema.Literal("thread.meta-updated"),
875 payload: ThreadMetaUpdatedPayload,
876 }),
877 Schema.Struct({
878 ...EventBaseFields,
879 type: Schema.Literal("thread.runtime-mode-set"),
880 payload: ThreadRuntimeModeSetPayload,
881 }),
882 Schema.Struct({
883 ...EventBaseFields,
884 type: Schema.Literal("thread.interaction-mode-set"),
885 payload: ThreadInteractionModeSetPayload,
886 }),
887 Schema.Struct({
888 ...EventBaseFields,
889 type: Schema.Literal("thread.message-sent"),
890 payload: ThreadMessageSentPayload,
891 }),
892 Schema.Struct({
893 ...EventBaseFields,
894 type: Schema.Literal("thread.turn-start-requested"),
895 payload: ThreadTurnStartRequestedPayload,
896 }),
897 Schema.Struct({
898 ...EventBaseFields,
899 type: Schema.Literal("thread.turn-interrupt-requested"),
900 payload: ThreadTurnInterruptRequestedPayload,
901 }),
902 Schema.Struct({
903 ...EventBaseFields,
904 type: Schema.Literal("thread.approval-response-requested"),
905 payload: ThreadApprovalResponseRequestedPayload,
906 }),
907 Schema.Struct({
908 ...EventBaseFields,
909 type: Schema.Literal("thread.user-input-response-requested"),
910 payload: ThreadUserInputResponseRequestedPayload,
911 }),
912 Schema.Struct({
913 ...EventBaseFields,
914 type: Schema.Literal("thread.checkpoint-revert-requested"),
915 payload: ThreadCheckpointRevertRequestedPayload,
916 }),
917 Schema.Struct({
918 ...EventBaseFields,
919 type: Schema.Literal("thread.reverted"),
920 payload: ThreadRevertedPayload,
921 }),
922 Schema.Struct({
923 ...EventBaseFields,
924 type: Schema.Literal("thread.session-stop-requested"),
925 payload: ThreadSessionStopRequestedPayload,
926 }),
927 Schema.Struct({
928 ...EventBaseFields,
929 type: Schema.Literal("thread.session-set"),
930 payload: ThreadSessionSetPayload,
931 }),
932 Schema.Struct({
933 ...EventBaseFields,
934 type: Schema.Literal("thread.proposed-plan-upserted"),
935 payload: ThreadProposedPlanUpsertedPayload,
936 }),
937 Schema.Struct({
938 ...EventBaseFields,
939 type: Schema.Literal("thread.turn-diff-completed"),
940 payload: ThreadTurnDiffCompletedPayload,
941 }),
942 Schema.Struct({
943 ...EventBaseFields,
944 type: Schema.Literal("thread.activity-appended"),
945 payload: ThreadActivityAppendedPayload,
946 }),
947]);
948export type OrchestrationEvent = typeof OrchestrationEvent.Type;
949
950export const OrchestrationCommandReceiptStatus = Schema.Literals(["accepted", "rejected"]);
951export type OrchestrationCommandReceiptStatus = typeof OrchestrationCommandReceiptStatus.Type;
952
953export const TurnCountRange = Schema.Struct({
954 fromTurnCount: NonNegativeInt,
955 toTurnCount: NonNegativeInt,
956}).check(
957 Schema.makeFilter(
958 (input) =>
959 input.fromTurnCount <= input.toTurnCount ||
960 new SchemaIssue.InvalidValue(Option.some(input.fromTurnCount), {
961 message: "fromTurnCount must be less than or equal to toTurnCount",
962 }),
963 { identifier: "OrchestrationTurnDiffRange" },
964 ),
965);
966
967export const ThreadTurnDiff = TurnCountRange.mapFields(
968 Struct.assign({
969 threadId: ThreadId,
970 diff: Schema.String,
971 }),
972 { unsafePreserveChecks: true },
973);
974
975export const ProviderSessionRuntimeStatus = Schema.Literals([
976 "starting",
977 "running",
978 "stopped",
979 "error",
980]);
981export type ProviderSessionRuntimeStatus = typeof ProviderSessionRuntimeStatus.Type;
982
983const ProjectionThreadTurnStatus = Schema.Literals([
984 "running",
985 "completed",
986 "interrupted",
987 "error",
988]);
989export type ProjectionThreadTurnStatus = typeof ProjectionThreadTurnStatus.Type;
990
991const ProjectionCheckpointRow = Schema.Struct({
992 threadId: ThreadId,
993 turnId: TurnId,
994 checkpointTurnCount: NonNegativeInt,
995 checkpointRef: CheckpointRef,
996 status: OrchestrationCheckpointStatus,
997 files: Schema.Array(OrchestrationCheckpointFile),
998 assistantMessageId: Schema.NullOr(MessageId),
999 completedAt: IsoDateTime,
1000});
1001export type ProjectionCheckpointRow = typeof ProjectionCheckpointRow.Type;
1002
1003export const ProjectionPendingApprovalStatus = Schema.Literals(["pending", "resolved"]);
1004export type ProjectionPendingApprovalStatus = typeof ProjectionPendingApprovalStatus.Type;
1005
1006export const ProjectionPendingApprovalDecision = Schema.NullOr(ProviderApprovalDecision);
1007export type ProjectionPendingApprovalDecision = typeof ProjectionPendingApprovalDecision.Type;
1008
1009export const DispatchResult = Schema.Struct({
1010 sequence: NonNegativeInt,
1011});
1012export type DispatchResult = typeof DispatchResult.Type;
1013
1014export const OrchestrationGetSnapshotInput = Schema.Struct({});
1015export type OrchestrationGetSnapshotInput = typeof OrchestrationGetSnapshotInput.Type;
1016const OrchestrationGetSnapshotResult = OrchestrationReadModel;
1017export type OrchestrationGetSnapshotResult = typeof OrchestrationGetSnapshotResult.Type;
1018
1019export const OrchestrationGetTurnDiffInput = TurnCountRange.mapFields(
1020 Struct.assign({ threadId: ThreadId }),
1021 { unsafePreserveChecks: true },
1022);
1023export type OrchestrationGetTurnDiffInput = typeof OrchestrationGetTurnDiffInput.Type;
1024
1025export const OrchestrationGetTurnDiffResult = ThreadTurnDiff;
1026export type OrchestrationGetTurnDiffResult = typeof OrchestrationGetTurnDiffResult.Type;
1027
1028export const OrchestrationGetFullThreadDiffInput = Schema.Struct({
1029 threadId: ThreadId,
1030 toTurnCount: NonNegativeInt,
1031});
1032export type OrchestrationGetFullThreadDiffInput = typeof OrchestrationGetFullThreadDiffInput.Type;
1033
1034export const OrchestrationGetFullThreadDiffResult = ThreadTurnDiff;
1035export type OrchestrationGetFullThreadDiffResult = typeof OrchestrationGetFullThreadDiffResult.Type;
1036
1037export const OrchestrationReplayEventsInput = Schema.Struct({
1038 fromSequenceExclusive: NonNegativeInt,
1039});
1040export type OrchestrationReplayEventsInput = typeof OrchestrationReplayEventsInput.Type;
1041
1042const OrchestrationReplayEventsResult = Schema.Array(OrchestrationEvent);
1043export type OrchestrationReplayEventsResult = typeof OrchestrationReplayEventsResult.Type;
1044
1045export const OrchestrationRpcSchemas = {
1046 getSnapshot: {
1047 input: OrchestrationGetSnapshotInput,
1048 output: OrchestrationGetSnapshotResult,
1049 },
1050 dispatchCommand: {
1051 input: ClientOrchestrationCommand,
1052 output: DispatchResult,
1053 },
1054 getTurnDiff: {
1055 input: OrchestrationGetTurnDiffInput,
1056 output: OrchestrationGetTurnDiffResult,
1057 },
1058 getFullThreadDiff: {
1059 input: OrchestrationGetFullThreadDiffInput,
1060 output: OrchestrationGetFullThreadDiffResult,
1061 },
1062 replayEvents: {
1063 input: OrchestrationReplayEventsInput,
1064 output: OrchestrationReplayEventsResult,
1065 },
1066} as const;
1067
1068export class OrchestrationGetSnapshotError extends Schema.TaggedErrorClass<OrchestrationGetSnapshotError>()(
1069 "OrchestrationGetSnapshotError",
1070 {
1071 message: TrimmedNonEmptyString,
1072 cause: Schema.optional(Schema.Defect),
1073 },
1074) {}
1075
1076export class OrchestrationDispatchCommandError extends Schema.TaggedErrorClass<OrchestrationDispatchCommandError>()(
1077 "OrchestrationDispatchCommandError",
1078 {
1079 message: TrimmedNonEmptyString,
1080 cause: Schema.optional(Schema.Defect),
1081 },
1082) {}
1083
1084export class OrchestrationGetTurnDiffError extends Schema.TaggedErrorClass<OrchestrationGetTurnDiffError>()(
1085 "OrchestrationGetTurnDiffError",
1086 {
1087 message: TrimmedNonEmptyString,
1088 cause: Schema.optional(Schema.Defect),
1089 },
1090) {}
1091
1092export class OrchestrationGetFullThreadDiffError extends Schema.TaggedErrorClass<OrchestrationGetFullThreadDiffError>()(
1093 "OrchestrationGetFullThreadDiffError",
1094 {
1095 message: TrimmedNonEmptyString,
1096 cause: Schema.optional(Schema.Defect),
1097 },
1098) {}
1099
1100export class OrchestrationReplayEventsError extends Schema.TaggedErrorClass<OrchestrationReplayEventsError>()(
1101 "OrchestrationReplayEventsError",
1102 {
1103 message: TrimmedNonEmptyString,
1104 cause: Schema.optional(Schema.Defect),
1105 },
1106) {}