Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestration.ts

1,069 lines34.6 KB

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