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