Open History to see the last commit
1import {
2 ChatAttachment,
3 CheckpointId,
4 MessageId,
5 ModelSelection,
6 NodeId,
7 OrchestrationV2AppThread,
8 OrchestrationV2ConversationMessage,
9 OrchestrationV2ExecutionNode,
10 OrchestrationV2ProviderSession,
11 OrchestrationV2PlanArtifact,
12 OrchestrationV2ProviderCapabilities,
13 OrchestrationV2ProviderFailure,
14 OrchestrationV2ProviderThread,
15 OrchestrationV2ProviderTurn,
16 OrchestrationV2RuntimeRequest,
17 OrchestrationV2Subagent,
18 OrchestrationV2TurnItem,
19 ProviderApprovalDecision,
20 ProviderInteractionMode,
21 ProviderDriverKind,
22 ProviderInstanceId,
23 PositiveInt,
24 ProviderUserInputAnswers,
25 ProviderSessionId,
26 ProviderThreadId,
27 ProviderTurnId,
28 RuntimeMode,
29 RuntimeRequestId,
30 RunAttemptId,
31 RunId,
32 ThreadId,
33} from "@t3tools/contracts";
34import * as Context from "effect/Context";
35import * as Schema from "effect/Schema";
36import type * as Effect from "effect/Effect";
37import type * as Scope from "effect/Scope";
38import type * as Stream from "effect/Stream";
39
40import type {
41 ProviderSelectionTransitionInput,
42 ProviderSelectionTransitionPlan,
43} from "./ProviderSelectionTransition.ts";
44
45export const ProviderAdapterV2RuntimePolicy = Schema.Struct({
46 runtimeMode: RuntimeMode,
47 interactionMode: ProviderInteractionMode,
48 cwd: Schema.NullOr(Schema.String),
49 approvalPolicy: Schema.optional(Schema.Unknown),
50 sandboxPolicy: Schema.optional(Schema.Unknown),
51 reasoningEffort: Schema.optional(Schema.String),
52});
53export type ProviderAdapterV2RuntimePolicy = typeof ProviderAdapterV2RuntimePolicy.Type;
54
55export const ProviderAdapterV2TurnMessage = Schema.Struct({
56 messageId: MessageId,
57 text: Schema.String,
58 attachments: Schema.Array(ChatAttachment),
59 createdBy: OrchestrationV2ConversationMessage.fields.createdBy,
60 creationSource: OrchestrationV2ConversationMessage.fields.creationSource,
61});
62export type ProviderAdapterV2TurnMessage = typeof ProviderAdapterV2TurnMessage.Type;
63
64export const ProviderAdapterV2SessionStatus = Schema.Literals([
65 "starting",
66 "ready",
67 "running",
68 "waiting",
69 "stopped",
70 "error",
71]);
72export type ProviderAdapterV2SessionStatus = typeof ProviderAdapterV2SessionStatus.Type;
73
74export const ProviderAdapterV2Event = Schema.Union([
75 Schema.Struct({
76 type: Schema.Literal("app_thread.created"),
77 driver: ProviderDriverKind,
78 appThread: OrchestrationV2AppThread,
79 }),
80 Schema.Struct({
81 type: Schema.Literal("provider_session.updated"),
82 driver: ProviderDriverKind,
83 providerSession: OrchestrationV2ProviderSession,
84 }),
85 Schema.Struct({
86 type: Schema.Literal("provider_thread.updated"),
87 driver: ProviderDriverKind,
88 providerThread: OrchestrationV2ProviderThread,
89 }),
90 Schema.Struct({
91 type: Schema.Literal("provider_turn.updated"),
92 driver: ProviderDriverKind,
93 threadId: Schema.optional(ThreadId),
94 providerTurn: OrchestrationV2ProviderTurn,
95 }),
96 Schema.Struct({
97 type: Schema.Literal("node.updated"),
98 driver: ProviderDriverKind,
99 node: OrchestrationV2ExecutionNode,
100 }),
101 Schema.Struct({
102 type: Schema.Literal("subagent.updated"),
103 driver: ProviderDriverKind,
104 subagent: OrchestrationV2Subagent,
105 }),
106 Schema.Struct({
107 type: Schema.Literal("message.updated"),
108 driver: ProviderDriverKind,
109 message: OrchestrationV2ConversationMessage,
110 }),
111 Schema.Struct({
112 type: Schema.Literal("turn_item.updated"),
113 driver: ProviderDriverKind,
114 turnItem: OrchestrationV2TurnItem,
115 }),
116 Schema.Struct({
117 type: Schema.Literal("runtime_request.updated"),
118 driver: ProviderDriverKind,
119 threadId: Schema.optional(ThreadId),
120 runtimeRequest: OrchestrationV2RuntimeRequest,
121 }),
122 Schema.Struct({
123 type: Schema.Literal("plan.updated"),
124 driver: ProviderDriverKind,
125 plan: OrchestrationV2PlanArtifact,
126 }),
127 Schema.Struct({
128 type: Schema.Literal("turn.terminal"),
129 driver: ProviderDriverKind,
130 providerThreadId: ProviderThreadId,
131 providerTurnId: ProviderTurnId,
132 runOrdinal: PositiveInt,
133 status: Schema.Literals(["completed", "interrupted", "cancelled"]),
134 failure: Schema.Null,
135 threadDisposition: Schema.Literals(["reusable", "broken"]),
136 }),
137 Schema.Struct({
138 type: Schema.Literal("turn.terminal"),
139 driver: ProviderDriverKind,
140 providerThreadId: ProviderThreadId,
141 providerTurnId: ProviderTurnId,
142 runOrdinal: PositiveInt,
143 failureItemOrdinal: PositiveInt,
144 status: Schema.Literal("failed"),
145 failure: OrchestrationV2ProviderFailure,
146 threadDisposition: Schema.Literals(["reusable", "broken"]),
147 }),
148]);
149export type ProviderAdapterV2Event = typeof ProviderAdapterV2Event.Type;
150
151export class ProviderAdapterCapabilitiesError extends Schema.TaggedErrorClass<ProviderAdapterCapabilitiesError>()(
152 "ProviderAdapterCapabilitiesError",
153 {
154 driver: ProviderDriverKind,
155 cause: Schema.optional(Schema.Defect()),
156 },
157) {
158 override get message(): string {
159 return `Failed to read ${this.driver} provider capabilities.`;
160 }
161}
162
163export class ProviderAdapterOpenSessionError extends Schema.TaggedErrorClass<ProviderAdapterOpenSessionError>()(
164 "ProviderAdapterOpenSessionError",
165 {
166 driver: ProviderDriverKind,
167 providerSessionId: ProviderSessionId,
168 cause: Schema.optional(Schema.Defect()),
169 },
170) {
171 override get message(): string {
172 return `Failed to open ${this.driver} provider session ${this.providerSessionId}.`;
173 }
174}
175
176export class ProviderAdapterCloseSessionError extends Schema.TaggedErrorClass<ProviderAdapterCloseSessionError>()(
177 "ProviderAdapterCloseSessionError",
178 {
179 driver: ProviderDriverKind,
180 providerSessionId: ProviderSessionId,
181 cause: Schema.optional(Schema.Defect()),
182 },
183) {
184 override get message(): string {
185 return `Failed to close ${this.driver} provider session ${this.providerSessionId}.`;
186 }
187}
188
189export class ProviderAdapterResumeThreadError extends Schema.TaggedErrorClass<ProviderAdapterResumeThreadError>()(
190 "ProviderAdapterResumeThreadError",
191 {
192 driver: ProviderDriverKind,
193 providerSessionId: ProviderSessionId,
194 providerThreadId: ProviderThreadId,
195 cause: Schema.optional(Schema.Defect()),
196 },
197) {
198 override get message(): string {
199 return `Failed to resume ${this.driver} provider thread ${this.providerThreadId}.`;
200 }
201}
202
203export class ProviderAdapterEnsureThreadError extends Schema.TaggedErrorClass<ProviderAdapterEnsureThreadError>()(
204 "ProviderAdapterEnsureThreadError",
205 {
206 driver: ProviderDriverKind,
207 threadId: ThreadId,
208 cause: Schema.optional(Schema.Defect()),
209 },
210) {
211 override get message(): string {
212 return `Failed to ensure ${this.driver} provider thread for app thread ${this.threadId}.`;
213 }
214}
215
216export class ProviderAdapterReadThreadSnapshotError extends Schema.TaggedErrorClass<ProviderAdapterReadThreadSnapshotError>()(
217 "ProviderAdapterReadThreadSnapshotError",
218 {
219 driver: ProviderDriverKind,
220 providerThreadId: ProviderThreadId,
221 cause: Schema.optional(Schema.Defect()),
222 },
223) {
224 override get message(): string {
225 return `Failed to read ${this.driver} provider thread snapshot ${this.providerThreadId}.`;
226 }
227}
228
229export class ProviderAdapterRollbackThreadError extends Schema.TaggedErrorClass<ProviderAdapterRollbackThreadError>()(
230 "ProviderAdapterRollbackThreadError",
231 {
232 driver: ProviderDriverKind,
233 providerThreadId: ProviderThreadId,
234 checkpointId: Schema.optional(CheckpointId),
235 cause: Schema.optional(Schema.Defect()),
236 },
237) {
238 override get message(): string {
239 return `Failed to roll back ${this.driver} provider thread ${this.providerThreadId}.`;
240 }
241}
242
243export class ProviderAdapterForkThreadError extends Schema.TaggedErrorClass<ProviderAdapterForkThreadError>()(
244 "ProviderAdapterForkThreadError",
245 {
246 driver: ProviderDriverKind,
247 providerThreadId: ProviderThreadId,
248 cause: Schema.optional(Schema.Defect()),
249 },
250) {
251 override get message(): string {
252 return `Failed to fork ${this.driver} provider thread ${this.providerThreadId}.`;
253 }
254}
255
256export class ProviderAdapterTurnStartError extends Schema.TaggedErrorClass<ProviderAdapterTurnStartError>()(
257 "ProviderAdapterTurnStartError",
258 {
259 driver: ProviderDriverKind,
260 threadId: ThreadId,
261 providerThreadId: ProviderThreadId,
262 runId: RunId,
263 cause: Schema.optional(Schema.Defect()),
264 },
265) {
266 override get message(): string {
267 return `Failed to start run ${this.runId} on ${this.driver} provider thread ${this.providerThreadId}.`;
268 }
269}
270
271export class ProviderAdapterSteerRunUnsupportedError extends Schema.TaggedErrorClass<ProviderAdapterSteerRunUnsupportedError>()(
272 "ProviderAdapterSteerRunUnsupportedError",
273 {
274 driver: ProviderDriverKind,
275 providerThreadId: ProviderThreadId,
276 },
277) {
278 override get message(): string {
279 return `${this.driver} provider thread ${this.providerThreadId} does not support active-run steering.`;
280 }
281}
282
283export class ProviderAdapterSteerRunError extends Schema.TaggedErrorClass<ProviderAdapterSteerRunError>()(
284 "ProviderAdapterSteerRunError",
285 {
286 driver: ProviderDriverKind,
287 providerThreadId: ProviderThreadId,
288 providerTurnId: ProviderTurnId,
289 cause: Schema.optional(Schema.Defect()),
290 },
291) {
292 override get message(): string {
293 return `Failed to steer active run ${this.providerTurnId} on ${this.driver} provider thread ${this.providerThreadId}.`;
294 }
295}
296
297export class ProviderAdapterInterruptError extends Schema.TaggedErrorClass<ProviderAdapterInterruptError>()(
298 "ProviderAdapterInterruptError",
299 {
300 driver: ProviderDriverKind,
301 providerThreadId: ProviderThreadId,
302 providerTurnId: ProviderTurnId,
303 cause: Schema.optional(Schema.Defect()),
304 },
305) {
306 override get message(): string {
307 return `Failed to interrupt ${this.driver} provider turn ${this.providerTurnId}.`;
308 }
309}
310
311export class ProviderAdapterRuntimeRequestResponseError extends Schema.TaggedErrorClass<ProviderAdapterRuntimeRequestResponseError>()(
312 "ProviderAdapterRuntimeRequestResponseError",
313 {
314 driver: ProviderDriverKind,
315 requestId: RuntimeRequestId,
316 cause: Schema.optional(Schema.Defect()),
317 },
318) {
319 override get message(): string {
320 return `Failed to respond to ${this.driver} runtime request ${this.requestId}.`;
321 }
322}
323
324export class ProviderAdapterEventStreamError extends Schema.TaggedErrorClass<ProviderAdapterEventStreamError>()(
325 "ProviderAdapterEventStreamError",
326 {
327 driver: ProviderDriverKind,
328 providerSessionId: ProviderSessionId,
329 cause: Schema.optional(Schema.Defect()),
330 },
331) {
332 override get message(): string {
333 return `Failed while streaming ${this.driver} provider session ${this.providerSessionId} events.`;
334 }
335}
336
337export class ProviderAdapterProtocolError extends Schema.TaggedErrorClass<ProviderAdapterProtocolError>()(
338 "ProviderAdapterProtocolError",
339 {
340 driver: ProviderDriverKind,
341 detail: Schema.String,
342 payload: Schema.optional(Schema.Unknown),
343 },
344) {
345 override get message(): string {
346 return `${this.driver} provider protocol error: ${this.detail}.`;
347 }
348}
349
350export const ProviderAdapterV2Error = Schema.Union([
351 ProviderAdapterCapabilitiesError,
352 ProviderAdapterOpenSessionError,
353 ProviderAdapterCloseSessionError,
354 ProviderAdapterResumeThreadError,
355 ProviderAdapterEnsureThreadError,
356 ProviderAdapterReadThreadSnapshotError,
357 ProviderAdapterRollbackThreadError,
358 ProviderAdapterForkThreadError,
359 ProviderAdapterTurnStartError,
360 ProviderAdapterSteerRunUnsupportedError,
361 ProviderAdapterSteerRunError,
362 ProviderAdapterInterruptError,
363 ProviderAdapterRuntimeRequestResponseError,
364 ProviderAdapterEventStreamError,
365 ProviderAdapterProtocolError,
366]);
367export type ProviderAdapterV2Error = typeof ProviderAdapterV2Error.Type;
368
369export interface ProviderAdapterV2OpenSessionInput {
370 readonly threadId: ThreadId;
371 readonly providerSessionId: ProviderSessionId;
372 readonly modelSelection: ModelSelection;
373 readonly runtimePolicy: ProviderAdapterV2RuntimePolicy;
374 readonly resumeFromSession?: OrchestrationV2ProviderSession;
375}
376
377export interface ProviderAdapterV2EnsureThreadInput {
378 readonly threadId: ThreadId;
379 readonly modelSelection: ModelSelection;
380 readonly runtimePolicy: ProviderAdapterV2RuntimePolicy;
381 readonly providerSessionId?: ProviderSessionId;
382 readonly existingProviderThread?: OrchestrationV2ProviderThread;
383}
384
385export interface ProviderAdapterV2TurnInput {
386 readonly appThread: OrchestrationV2AppThread;
387 readonly threadId: ThreadId;
388 readonly runId: RunId;
389 readonly runOrdinal: number;
390 readonly providerTurnOrdinal: number;
391 readonly attemptId: RunAttemptId;
392 readonly rootNodeId: NodeId;
393 readonly providerThread: OrchestrationV2ProviderThread;
394 readonly message: ProviderAdapterV2TurnMessage;
395 readonly modelSelection: ModelSelection;
396 readonly runtimePolicy: ProviderAdapterV2RuntimePolicy;
397}
398
399export interface ProviderAdapterV2SteerInput {
400 readonly threadId: ThreadId;
401 readonly runId: RunId;
402 readonly providerThread: OrchestrationV2ProviderThread;
403 readonly providerTurnId: ProviderTurnId;
404 readonly message: ProviderAdapterV2TurnMessage;
405}
406
407export interface ProviderAdapterV2InterruptInput {
408 readonly providerThread: OrchestrationV2ProviderThread;
409 readonly providerTurnId: ProviderTurnId;
410}
411
412export interface ProviderAdapterV2RuntimeRequestResponseInput {
413 readonly requestId: RuntimeRequestId;
414 readonly decision?: ProviderApprovalDecision;
415 readonly answers?: ProviderUserInputAnswers;
416 readonly response?: unknown;
417}
418
419export interface ProviderAdapterV2ThreadSnapshot {
420 readonly providerThread: OrchestrationV2ProviderThread;
421 readonly providerTurns: ReadonlyArray<OrchestrationV2ProviderTurn>;
422 readonly messages: ReadonlyArray<OrchestrationV2ConversationMessage>;
423 readonly runtimeRequests: ReadonlyArray<OrchestrationV2RuntimeRequest>;
424 readonly providerPayload?: unknown;
425}
426
427export interface ProviderAdapterV2ReadThreadSnapshotInput {
428 readonly providerThread: OrchestrationV2ProviderThread;
429}
430
431export type ProviderAdapterV2RollbackTarget =
432 | {
433 readonly type: "thread_start";
434 readonly checkpointId: CheckpointId;
435 readonly appRunOrdinal: 0;
436 }
437 | {
438 readonly type: "provider_turn";
439 readonly checkpointId: CheckpointId;
440 readonly appRunOrdinal: number;
441 readonly providerTurn: OrchestrationV2ProviderTurn;
442 };
443
444export interface ProviderAdapterV2RollbackThreadInput {
445 readonly providerThread: OrchestrationV2ProviderThread;
446 readonly target: ProviderAdapterV2RollbackTarget;
447 readonly providerThreadTurns: ReadonlyArray<OrchestrationV2ProviderTurn>;
448}
449
450export interface ProviderAdapterV2ForkThreadInput {
451 readonly sourceProviderThread: OrchestrationV2ProviderThread;
452 readonly sourceProviderTurns?: ReadonlyArray<OrchestrationV2ProviderTurn>;
453 readonly providerTurnId?: ProviderTurnId;
454 readonly targetThreadId: ThreadId;
455 readonly ownerNodeId?: NodeId;
456 readonly modelSelection?: ModelSelection;
457 readonly runtimePolicy?: ProviderAdapterV2RuntimePolicy;
458}
459
460export interface ProviderAdapterV2EventSubscription {
461 readonly events: Stream.Stream<ProviderAdapterV2Event, ProviderAdapterV2Error>;
462 readonly close: Effect.Effect<void>;
463}
464
465export interface ProviderAdapterV2SessionRuntime {
466 readonly instanceId: ProviderInstanceId;
467 readonly driver: ProviderDriverKind;
468 readonly providerSessionId: ProviderSessionId;
469 readonly providerSession: OrchestrationV2ProviderSession;
470 readonly events: Stream.Stream<ProviderAdapterV2Event, ProviderAdapterV2Error>;
471 /**
472 * Manager-owned runtimes expose a synchronous subscription so concurrent
473 * provider threads receive independent copies of the process event stream.
474 * Adapter runtimes may omit this and expose only their single-consumer event stream.
475 */
476 readonly subscribeEvents?: Effect.Effect<ProviderAdapterV2EventSubscription>;
477 readonly ensureThread: (
478 input: ProviderAdapterV2EnsureThreadInput,
479 ) => Effect.Effect<OrchestrationV2ProviderThread, ProviderAdapterV2Error>;
480 readonly resumeThread: (input: {
481 readonly providerThread: OrchestrationV2ProviderThread;
482 readonly threadId?: ThreadId;
483 readonly modelSelection?: ModelSelection;
484 readonly runtimePolicy?: ProviderAdapterV2RuntimePolicy;
485 }) => Effect.Effect<OrchestrationV2ProviderThread, ProviderAdapterV2Error>;
486 readonly startTurn: (
487 input: ProviderAdapterV2TurnInput,
488 ) => Effect.Effect<void, ProviderAdapterV2Error>;
489 readonly steerTurn: (
490 input: ProviderAdapterV2SteerInput,
491 ) => Effect.Effect<void, ProviderAdapterV2Error>;
492 readonly interruptTurn: (
493 input: ProviderAdapterV2InterruptInput,
494 ) => Effect.Effect<void, ProviderAdapterV2Error>;
495 readonly respondToRuntimeRequest: (
496 input: ProviderAdapterV2RuntimeRequestResponseInput,
497 ) => Effect.Effect<void, ProviderAdapterV2Error>;
498 readonly readThreadSnapshot: (
499 input: ProviderAdapterV2ReadThreadSnapshotInput,
500 ) => Effect.Effect<ProviderAdapterV2ThreadSnapshot, ProviderAdapterV2Error>;
501 readonly rollbackThread: (
502 input: ProviderAdapterV2RollbackThreadInput,
503 ) => Effect.Effect<ProviderAdapterV2ThreadSnapshot, ProviderAdapterV2Error>;
504 readonly forkThread: (
505 input: ProviderAdapterV2ForkThreadInput,
506 ) => Effect.Effect<OrchestrationV2ProviderThread, ProviderAdapterV2Error>;
507}
508
509export interface ProviderAdapterV2Shape {
510 readonly instanceId: ProviderInstanceId;
511 readonly driver: ProviderDriverKind;
512 readonly getCapabilities: () => Effect.Effect<
513 OrchestrationV2ProviderCapabilities,
514 ProviderAdapterV2Error
515 >;
516 readonly planSelectionTransition: (
517 input: ProviderSelectionTransitionInput,
518 ) => Effect.Effect<ProviderSelectionTransitionPlan, ProviderAdapterV2Error>;
519 readonly openSession: (
520 input: ProviderAdapterV2OpenSessionInput,
521 ) => Effect.Effect<ProviderAdapterV2SessionRuntime, ProviderAdapterV2Error, Scope.Scope>;
522}
523
524export class ProviderAdapterV2 extends Context.Service<ProviderAdapterV2, ProviderAdapterV2Shape>()(
525 "t3/orchestration-v2/ProviderAdapter/ProviderAdapterV2",
526) {}