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