import type { OrchestrationV2HistoricalMessage } from "@t3tools/contracts"; import { ChatAttachment, CheckpointId, MessageId, ModelSelection, NodeId, OrchestrationV2AppThread, OrchestrationV2ConversationMessage, OrchestrationV2ExecutionNode, OrchestrationV2ProviderSession, OrchestrationV2PlanArtifact, OrchestrationV2ProviderCapabilities, OrchestrationV2ProviderFailure, OrchestrationV2ProviderRetry, OrchestrationV2ProviderThread, OrchestrationV2ProviderTurn, OrchestrationV2RuntimeRequest, OrchestrationV2Subagent, OrchestrationV2TurnItem, ProviderApprovalDecision, ProviderInteractionMode, ProviderDriverKind, ProviderInstanceId, PositiveInt, ProviderUserInputAnswers, ProviderSessionId, ProviderThreadId, ProviderTurnId, RuntimeMode, RuntimeRequestId, RunAttemptId, RunId, ThreadId, TurnItemId, } from "@t3tools/contracts"; import * as Context from "effect/Context"; import type * as DateTime from "effect/DateTime"; import * as Schema from "effect/Schema"; import type * as Effect from "effect/Effect"; import type * as Scope from "effect/Scope"; import type * as Stream from "effect/Stream"; import type { ProviderSelectionTransitionInput, ProviderSelectionTransitionPlan, } from "./selectionTransition.ts"; export const ProviderAdapterV2RuntimePolicy = Schema.Struct({ runtimeMode: RuntimeMode, interactionMode: ProviderInteractionMode, cwd: Schema.NullOr(Schema.String), approvalPolicy: Schema.optional(Schema.Unknown), sandboxPolicy: Schema.optional(Schema.Unknown), /** * The administrator's ceiling, when a managed policy sets one. `runtimeMode` is already under * it; adapters use it to refuse native mode choices a user makes beside T3 Code's own. */ maxRuntimeMode: Schema.optional(RuntimeMode), }); export type ProviderAdapterV2RuntimePolicy = typeof ProviderAdapterV2RuntimePolicy.Type; export const ProviderAdapterV2TurnMessage = Schema.Struct({ messageId: MessageId, text: Schema.String, attachments: Schema.Array(ChatAttachment), createdBy: OrchestrationV2ConversationMessage.fields.createdBy, creationSource: OrchestrationV2ConversationMessage.fields.creationSource, scheduledTaskId: OrchestrationV2ConversationMessage.fields.scheduledTaskId, senderThreadId: OrchestrationV2ConversationMessage.fields.senderThreadId, }); export type ProviderAdapterV2TurnMessage = typeof ProviderAdapterV2TurnMessage.Type; export const ProviderAdapterV2SessionStatus = Schema.Literals([ "starting", "ready", "running", "waiting", "stopped", "error", ]); export type ProviderAdapterV2SessionStatus = typeof ProviderAdapterV2SessionStatus.Type; export const ProviderAdapterV2Event = Schema.Union([ Schema.Struct({ type: Schema.Literal("app_thread.created"), driver: ProviderDriverKind, appThread: OrchestrationV2AppThread, createOnly: Schema.optional(Schema.Boolean), }), Schema.Struct({ type: Schema.Literal("provider_session.updated"), driver: ProviderDriverKind, providerSession: OrchestrationV2ProviderSession, }), Schema.Struct({ type: Schema.Literal("provider_thread.updated"), driver: ProviderDriverKind, providerThread: OrchestrationV2ProviderThread, /** Work remaining at this point in the event stream, including native continuations. */ pendingBackgroundWork: Schema.optional(Schema.Boolean), /** Persist idle background snapshots before a run subscriber exists. */ sessionScoped: Schema.optional(Schema.Boolean), retainedNativeTurnIds: Schema.optional(Schema.Array(Schema.String)), }), Schema.Struct({ type: Schema.Literal("provider_turn.updated"), driver: ProviderDriverKind, threadId: Schema.optional(ThreadId), providerTurn: OrchestrationV2ProviderTurn, /** Questions the adapter keeps open after this turn ends; the rest of the turn's are dismissed. */ retainedRuntimeRequestIds: Schema.optional(Schema.Array(RuntimeRequestId)), }), Schema.Struct({ type: Schema.Literal("node.updated"), driver: ProviderDriverKind, node: OrchestrationV2ExecutionNode, }), Schema.Struct({ type: Schema.Literal("subagent.updated"), driver: ProviderDriverKind, subagent: OrchestrationV2Subagent, }), Schema.Struct({ type: Schema.Literal("message.updated"), driver: ProviderDriverKind, message: OrchestrationV2ConversationMessage, }), Schema.Struct({ type: Schema.Literal("turn_item.updated"), driver: ProviderDriverKind, turnItem: OrchestrationV2TurnItem, /** Persist recovered activity independently of a run subscription. */ sessionScoped: Schema.optional(Schema.Boolean), }), /** A steer the provider queued stays `pending` until the agent reads it; `cancelled` if dropped unread. */ Schema.Struct({ type: Schema.Literal("steer.updated"), driver: ProviderDriverKind, threadId: ThreadId, runId: RunId, messageId: MessageId, status: Schema.Literals(["pending", "completed", "cancelled"]), }), // Start before the root terminal; clear only after the final covered projection. // Consumer lifetime cannot depend on a producer probe that runs ahead of its queue. Schema.Struct({ type: Schema.Literal("background_work.updated"), driver: ProviderDriverKind, providerTurnId: ProviderTurnId, pending: Schema.Boolean, }), /** Unpersisted deltas go only to clients showing the row; final output arrives in turn_item.updated. */ Schema.Struct({ type: Schema.Literal("command_output.delta"), driver: ProviderDriverKind, threadId: ThreadId, itemId: TurnItemId, chunk: Schema.String, }), /** Unpersisted predicted next prompt; arrives after the run's root terminal. */ Schema.Struct({ type: Schema.Literal("prompt_suggestion.updated"), driver: ProviderDriverKind, threadId: ThreadId, runId: RunId, text: Schema.String, }), Schema.Struct({ type: Schema.Literal("runtime_request.updated"), driver: ProviderDriverKind, threadId: Schema.optional(ThreadId), runtimeRequest: OrchestrationV2RuntimeRequest, }), Schema.Struct({ type: Schema.Literal("plan.updated"), driver: ProviderDriverKind, plan: OrchestrationV2PlanArtifact, }), Schema.Struct({ type: Schema.Literal("turn.terminal"), driver: ProviderDriverKind, providerThreadId: ProviderThreadId, providerTurnId: ProviderTurnId, runOrdinal: PositiveInt, status: Schema.Literals(["completed", "interrupted", "cancelled"]), failure: Schema.Null, reason: Schema.optional(Schema.String), threadDisposition: Schema.Literals(["reusable", "broken"]), retainedRuntimeRequestIds: Schema.optional(Schema.Array(RuntimeRequestId)), }), Schema.Struct({ type: Schema.Literal("turn.terminal"), driver: ProviderDriverKind, providerThreadId: ProviderThreadId, providerTurnId: ProviderTurnId, runOrdinal: PositiveInt, failureItemOrdinal: PositiveInt, status: Schema.Literal("failed"), failure: OrchestrationV2ProviderFailure, retry: Schema.optional(OrchestrationV2ProviderRetry), retryStartedAt: Schema.optional(Schema.DateTimeUtc), threadDisposition: Schema.Literals(["reusable", "broken"]), /** Child turns outlive root failure and report their own terminals; otherwise open children settle. */ childTurnsContinue: Schema.optional(Schema.Literal(true)), retainedRuntimeRequestIds: Schema.optional(Schema.Array(RuntimeRequestId)), }), ]); export type ProviderAdapterV2Event = typeof ProviderAdapterV2Event.Type; export class ProviderAdapterCapabilitiesError extends Schema.TaggedError()( "ProviderAdapterCapabilitiesError", { driver: ProviderDriverKind, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to read ${this.driver} provider capabilities.`; } } export class ProviderAdapterOpenSessionError extends Schema.TaggedError()( "ProviderAdapterOpenSessionError", { driver: ProviderDriverKind, providerSessionId: ProviderSessionId, requiresSignIn: Schema.optional(Schema.Literal(true)), cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to open ${this.driver} provider session ${this.providerSessionId}.`; } } export class ProviderAdapterCloseSessionError extends Schema.TaggedError()( "ProviderAdapterCloseSessionError", { driver: ProviderDriverKind, providerSessionId: ProviderSessionId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to close ${this.driver} provider session ${this.providerSessionId}.`; } } export class ProviderAdapterResumeThreadError extends Schema.TaggedError()( "ProviderAdapterResumeThreadError", { driver: ProviderDriverKind, providerSessionId: ProviderSessionId, providerThreadId: ProviderThreadId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to resume ${this.driver} provider thread ${this.providerThreadId}.`; } } /** * The native thread is live in another process that owns its writes. Starting * a replacement would split the conversation, so callers must not fall back. */ export class ProviderAdapterThreadInUseError extends Schema.TaggedError()( "ProviderAdapterThreadInUseError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, detail: Schema.String, cause: Schema.Defect(), }, ) { override get message(): string { return this.detail; } } /** * The user sent `/compact` to a driver whose session has no native compaction. */ export class ProviderResumeUnsupportedError extends Schema.TaggedError()( "ProviderResumeUnsupportedError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId }, ) { override get message(): string { return "This provider cannot resume without a new message. Send a message to continue."; } } export class ProviderCompactionUnsupportedError extends Schema.TaggedError()( "ProviderCompactionUnsupportedError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, }, ) { override get message(): string { return "This provider does not support /compact. Start a new thread to continue with a fresh context."; } } export class ProviderAdapterEnsureThreadError extends Schema.TaggedError()( "ProviderAdapterEnsureThreadError", { driver: ProviderDriverKind, threadId: ThreadId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to ensure ${this.driver} provider thread for app thread ${this.threadId}.`; } } export class ProviderAdapterReadThreadSnapshotError extends Schema.TaggedError()( "ProviderAdapterReadThreadSnapshotError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to read ${this.driver} provider thread snapshot ${this.providerThreadId}.`; } } export class ProviderAdapterRollbackThreadError extends Schema.TaggedError()( "ProviderAdapterRollbackThreadError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, checkpointId: Schema.optional(CheckpointId), cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to roll back ${this.driver} provider thread ${this.providerThreadId}.`; } } export class ProviderAdapterForkThreadError extends Schema.TaggedError()( "ProviderAdapterForkThreadError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to fork ${this.driver} provider thread ${this.providerThreadId}.`; } } export class ProviderAdapterTurnStartError extends Schema.TaggedError()( "ProviderAdapterTurnStartError", { driver: ProviderDriverKind, threadId: ThreadId, providerThreadId: ProviderThreadId, runId: RunId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to start run ${this.runId} on ${this.driver} provider thread ${this.providerThreadId}.`; } } export class ProviderAdapterSteerRunUnsupportedError extends Schema.TaggedError()( "ProviderAdapterSteerRunUnsupportedError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, }, ) { override get message(): string { return `${this.driver} provider thread ${this.providerThreadId} does not support active-run steering.`; } } export class ProviderAdapterSteerDeclinedError extends Schema.TaggedError()( "ProviderAdapterSteerDeclinedError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, providerTurnId: ProviderTurnId, }, ) {} export class ProviderAdapterSteerRunError extends Schema.TaggedError()( "ProviderAdapterSteerRunError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, providerTurnId: ProviderTurnId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to steer active run ${this.providerTurnId} on ${this.driver} provider thread ${this.providerThreadId}.`; } } export class ProviderAdapterInterruptError extends Schema.TaggedError()( "ProviderAdapterInterruptError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, providerTurnId: ProviderTurnId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to interrupt ${this.driver} provider turn ${this.providerTurnId}.`; } } /** The provider did not confirm a stop in time; its processes may still be running. */ export class ProviderAdapterInterruptTimeoutError extends Schema.TaggedError()( "ProviderAdapterInterruptTimeoutError", { driver: ProviderDriverKind, providerThreadId: ProviderThreadId, providerTurnId: ProviderTurnId, timeoutMs: Schema.Number, }, ) { override get message(): string { return `The ${this.driver} provider did not confirm stopping turn ${this.providerTurnId} within ${this.timeoutMs} ms.`; } } export class ProviderAdapterRuntimeRequestResponseError extends Schema.TaggedError()( "ProviderAdapterRuntimeRequestResponseError", { driver: ProviderDriverKind, requestId: RuntimeRequestId, cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed to respond to ${this.driver} runtime request ${this.requestId}.`; } } export class ProviderAdapterEventStreamError extends Schema.TaggedError()( "ProviderAdapterEventStreamError", { driver: ProviderDriverKind, providerSessionId: ProviderSessionId, /** Set when T3 closed the session because this thread detached from it, not the provider. */ detachedThreadId: Schema.optional(ThreadId), /** Set when T3 raised this itself; unset when the adapter reported its runtime failing. */ raisedByT3: Schema.optional(Schema.Boolean), cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return `Failed while streaming ${this.driver} provider session ${this.providerSessionId} events.`; } } // Only fixed reasons reach failure messages; detail can contain private configuration. const ProviderAdapterFailureReason = Schema.Literals([ "previous_turn_active", "attachment_unreadable", ]); export type ProviderAdapterFailureReason = typeof ProviderAdapterFailureReason.Type; export class ProviderAdapterProtocolError extends Schema.TaggedError()( "ProviderAdapterProtocolError", { driver: ProviderDriverKind, detail: Schema.String, reason: Schema.optional(ProviderAdapterFailureReason), cause: Schema.optional(Schema.Defect()), payload: Schema.optional(Schema.Unknown), }, ) { override get message(): string { return `${this.driver} provider protocol error: ${this.detail}.`; } } export class ProviderAdapterSessionStoppedError extends Schema.TaggedError()( "ProviderAdapterSessionStoppedError", { driver: ProviderDriverKind }, ) { override get message(): string { return `No running ${this.driver} session owns this thread.`; } } export const ProviderAdapterV2Error = Schema.Union([ ProviderAdapterCapabilitiesError, ProviderAdapterOpenSessionError, ProviderAdapterCloseSessionError, ProviderAdapterResumeThreadError, ProviderAdapterThreadInUseError, ProviderAdapterEnsureThreadError, ProviderAdapterReadThreadSnapshotError, ProviderAdapterRollbackThreadError, ProviderAdapterForkThreadError, ProviderAdapterTurnStartError, ProviderResumeUnsupportedError, ProviderAdapterSteerRunUnsupportedError, ProviderAdapterSteerRunError, ProviderAdapterSteerDeclinedError, ProviderAdapterInterruptError, ProviderAdapterInterruptTimeoutError, ProviderAdapterRuntimeRequestResponseError, ProviderAdapterEventStreamError, ProviderAdapterProtocolError, ProviderAdapterSessionStoppedError, ]); export type ProviderAdapterV2Error = typeof ProviderAdapterV2Error.Type; export interface ProviderAdapterV2OpenSessionInput { readonly threadId: ThreadId; readonly providerSessionId: ProviderSessionId; readonly modelSelection: ModelSelection; readonly runtimePolicy: ProviderAdapterV2RuntimePolicy; /** Labs flag, read live: emit `prompt_suggestion.updated` after completed turns, where the provider can. */ readonly promptSuggestions?: Effect.Effect; readonly resumeFromSession?: OrchestrationV2ProviderSession; readonly initialNativeThreadId?: string; readonly initialProviderItemIdentityVersion?: 2; } export interface ProviderAdapterV2EnsureThreadInput { readonly threadId: ThreadId; readonly title?: string; readonly modelSelection: ModelSelection; readonly runtimePolicy: ProviderAdapterV2RuntimePolicy; readonly providerSessionId?: ProviderSessionId; readonly existingProviderThread?: OrchestrationV2ProviderThread; readonly forceNewNativeThread?: boolean; } export interface ProviderAdapterV2TurnInput { readonly appThread: OrchestrationV2AppThread; readonly threadId: ThreadId; readonly runId: RunId; readonly runOrdinal: number; readonly nativeThreadHasTurns?: boolean; readonly providerTurnOrdinal: number; readonly restartContinuationOfRunId?: RunId; readonly resume?: true; readonly attemptId: RunAttemptId; readonly rootNodeId: NodeId; readonly providerThread: OrchestrationV2ProviderThread; readonly message: ProviderAdapterV2TurnMessage; /** Leading characters of `message.text` that are inline handoff context, not the user's words. */ readonly inlineContextLength?: number; readonly modelSelection: ModelSelection; readonly runtimePolicy: ProviderAdapterV2RuntimePolicy; readonly appContext?: ReadonlyArray<{ readonly key: string; readonly text: string }>; } export type ProviderAdapterV2ResumeTurnInput = Omit< ProviderAdapterV2TurnInput, "message" | "resume" > & { readonly messageId: MessageId; }; export function resumeTurnInput( input: ProviderAdapterV2ResumeTurnInput, ): ProviderAdapterV2TurnInput { const { messageId, ...turn } = input; return { ...turn, resume: true, message: { messageId, text: "", attachments: [], createdBy: "user", creationSource: "server" }, }; } export interface ProviderAdapterV2SteerInput { readonly threadId: ThreadId; readonly runId: RunId; readonly providerThread: OrchestrationV2ProviderThread; readonly providerTurnId: ProviderTurnId; readonly message: ProviderAdapterV2TurnMessage; /** The selection sent with the message when it differs from the running turn's. */ readonly modelSelection?: ModelSelection; /** The thread's MCP app notes; the adapter sends those the agent has not seen yet. */ readonly appContext?: ProviderAdapterV2TurnInput["appContext"]; } export interface ProviderAdapterV2InterruptInput { readonly providerThread: OrchestrationV2ProviderThread; readonly providerTurnId: ProviderTurnId; readonly requestRuntimeRestart?: boolean; } export interface ProviderAdapterV2RuntimeRequestResponseInput { readonly requestId: RuntimeRequestId; readonly decision?: ProviderApprovalDecision; readonly feedback?: string; readonly answers?: ProviderUserInputAnswers; } export interface ProviderAdapterV2ThreadSnapshot { readonly providerThread: OrchestrationV2ProviderThread; readonly providerTurns: ReadonlyArray; readonly messages: ReadonlyArray; /** Ids in `messages` that hold the agent's reasoning, not its reply. */ readonly reasoningMessageIds?: ReadonlyArray; readonly runtimeRequests: ReadonlyArray; readonly providerPayload?: unknown; } export interface ProviderAdapterV2ReadThreadSnapshotInput { readonly providerThread: OrchestrationV2ProviderThread; } export type ProviderAdapterV2RollbackTarget = | { readonly type: "thread_start"; readonly checkpointId: CheckpointId; readonly appRunOrdinal: 0; } | { readonly type: "provider_turn"; readonly checkpointId: CheckpointId; readonly appRunOrdinal: number; readonly providerTurn: OrchestrationV2ProviderTurn; readonly message?: Pick; }; export interface ProviderAdapterV2RollbackThreadInput { readonly providerThread: OrchestrationV2ProviderThread; readonly target: ProviderAdapterV2RollbackTarget; readonly providerThreadTurns: ReadonlyArray; } export interface ProviderAdapterV2ForkThreadInput { readonly sourceProviderThread: OrchestrationV2ProviderThread; readonly sourceProviderTurns?: ReadonlyArray; readonly providerTurnId?: ProviderTurnId; readonly targetThreadId: ThreadId; readonly ownerNodeId?: NodeId; readonly modelSelection?: ModelSelection; readonly runtimePolicy?: ProviderAdapterV2RuntimePolicy; } export interface ProviderAdapterV2EventSubscription { readonly events: Stream.Stream; readonly close: Effect.Effect; } export interface ProviderAdapterV2HistoricalContext { readonly messages: ReadonlyArray; readonly context: string; } export interface ProviderAdapterV2McpTool { readonly name: string; // Apps reject a `hostContext.toolInfo.tool` without one. readonly inputSchema: unknown; readonly _meta?: unknown; readonly annotations?: unknown; } export interface ProviderAdapterV2McpCallToolResult { readonly content: ReadonlyArray; readonly structuredContent?: unknown; readonly isError?: boolean; readonly _meta?: unknown; } export interface ProviderAdapterV2McpApps { readonly listTools: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly server: string; }) => Effect.Effect, ProviderAdapterV2Error>; readonly callTool: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly server: string; readonly tool: string; readonly arguments: Record; }) => Effect.Effect; readonly readResource: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly server: string; readonly uri: string; }) => Effect.Effect<{ readonly contents: ReadonlyArray }, ProviderAdapterV2Error>; } export type ProviderAdapterV2SubagentOutcome = { readonly status: "completed" | "failed" | "cancelled"; readonly completedAt: NonNullable; readonly result: OrchestrationV2Subagent["result"]; }; export interface ProviderAdapterV2HistoryTurn { readonly id: string; readonly status: "completed" | "interrupted" | "failed" | "inProgress"; readonly startedAt: DateTime.Utc; readonly completedAt: DateTime.Utc; readonly messages: ReadonlyArray<{ readonly role: "user" | "assistant"; readonly text: string; }>; } export interface ProviderAdapterV2SessionRuntime { readonly instanceId: ProviderInstanceId; readonly driver: ProviderDriverKind; readonly providerSessionId: ProviderSessionId; readonly providerSession: OrchestrationV2ProviderSession; /** False keeps conversations alive when the provider cannot restore them after a restart. */ readonly canResumeAfterRestart?: boolean; readonly events: Stream.Stream; /** Manager runtimes broadcast independent subscriptions; adapters may expose only a single-consumer stream. */ readonly subscribeEvents?: Effect.Effect; readonly hasPendingBackgroundWork?: Effect.Effect; /** Root-subscription probe; replacement checks opt in to subagents owned by the live process. */ readonly hasPendingBackgroundWorkForThread?: ( providerThread: OrchestrationV2ProviderThread, options?: { readonly includeSubagents?: boolean }, ) => Effect.Effect; /** * Set only if all session background tasks and tool calls end with its process. Releasing the * session then clears its task roster and interrupts its open tool calls; otherwise threads * must keep Waiting. */ readonly backgroundTasksEndWithProcess?: true; readonly getModelContextWindow?: ( modelSelection: ModelSelection, cwd?: string | null, ) => number | undefined; readonly canReuseContextUsage?: (previous: ModelSelection, next: ModelSelection) => boolean; /** Excludes turn-scoped options from native thread load matching. */ readonly normalizeThreadLoadModelSelection?: (selection: ModelSelection) => ModelSelection; readonly ensureThread: ( input: ProviderAdapterV2EnsureThreadInput, ) => Effect.Effect; readonly resumeThread: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly threadId?: ThreadId; readonly modelSelection?: ModelSelection; readonly runtimePolicy?: ProviderAdapterV2RuntimePolicy; /** The native session is the only copy of the thread's imported history: fail when it is gone. */ readonly importedNativeSession?: boolean; }) => Effect.Effect; /** False means the native protocol explicitly does not support history injection. */ readonly injectHistory?: ( input: ProviderAdapterV2HistoricalContext & { readonly providerThread: OrchestrationV2ProviderThread; }, ) => Effect.Effect; readonly startTurn: ( input: ProviderAdapterV2TurnInput, ) => Effect.Effect; readonly resumeTurn?: ( input: ProviderAdapterV2ResumeTurnInput, ) => Effect.Effect; readonly compactThread?: ( input: ProviderAdapterV2TurnInput, ) => Effect.Effect; readonly steerTurn: ( input: ProviderAdapterV2SteerInput, ) => Effect.Effect; /** Applies a thread's new selection to its running process where the provider can; otherwise it waits for the next turn. */ readonly applyModelSelection?: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly modelSelection: ModelSelection; }) => Effect.Effect; /** Renames the provider's native session while its process is running; otherwise the next turn does. */ readonly applyThreadTitle?: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly title: string; }) => Effect.Effect; readonly stopTask?: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly taskId: string; }) => Effect.Effect; /** * Marks or unmarks background tasks the agent will resume from, using the ids the agent saw. * Ids not running on this provider thread come back unmatched and change nothing. */ readonly awaitBackgroundTasks?: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly taskIds: ReadonlyArray; readonly awaited: boolean; }) => Effect.Effect<{ readonly matched: ReadonlyArray }, ProviderAdapterV2Error>; readonly interruptTurn: ( input: ProviderAdapterV2InterruptInput, ) => Effect.Effect; readonly stopSubagent?: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly nativeTaskId: string; /** A delayed Stop must not interrupt a task resumed after this request. */ readonly requestedAt?: NonNullable; }) => Effect.Effect; readonly unloadThread?: (input: { readonly providerThread: OrchestrationV2ProviderThread; /** Also hide it from the provider's own clients where it can; resuming it must restore it. */ readonly archive?: boolean; }) => Effect.Effect; readonly respondToRuntimeRequest: ( input: ProviderAdapterV2RuntimeRequestResponseInput, ) => Effect.Effect; readonly readThreadSnapshot: ( input: ProviderAdapterV2ReadThreadSnapshotInput, ) => Effect.Effect; /** Native turns after a known turn, oldest first. If the anchor is missing, returns no turns. */ readonly readHistoryAfter?: ( input: ProviderAdapterV2ReadThreadSnapshotInput & { readonly nativeTurnId: string; /** Keep these turns in the result for ordering, but omit their messages. */ readonly knownNativeTurnIds?: ReadonlySet; }, ) => Effect.Effect, ProviderAdapterV2Error>; readonly uploadFeedback?: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly reason?: string; }) => Effect.Effect<{ readonly feedbackId: string }, ProviderAdapterV2Error>; /** Absent means non-interactive apps with plain tool rows. */ readonly mcpApps?: ProviderAdapterV2McpApps; readonly rollbackThread: ( input: ProviderAdapterV2RollbackThreadInput, ) => Effect.Effect; readonly forkThread: ( input: ProviderAdapterV2ForkThreadInput, ) => Effect.Effect; } export class ProviderAdapterV2 extends Context.Service< ProviderAdapterV2, { readonly instanceId: ProviderInstanceId; readonly driver: ProviderDriverKind; readonly getCapabilities: () => Effect.Effect< OrchestrationV2ProviderCapabilities, ProviderAdapterV2Error >; readonly planSelectionTransition: ( input: ProviderSelectionTransitionInput, ) => Effect.Effect; readonly openSession: ( input: ProviderAdapterV2OpenSessionInput, ) => Effect.Effect; } >()("@t3tools/provider-core/server/ProviderAdapter/ProviderAdapterV2") {}