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