Open History to see the last commit
1/**
2 * ProviderAdapter - Provider-specific runtime adapter contract.
3 *
4 * Defines the provider-native session/protocol operations that `ProviderService`
5 * routes to after resolving the target provider. Implementations should focus
6 * on provider behavior only and avoid cross-provider orchestration concerns.
7 *
8 * @module ProviderAdapter
9 */
10import type {
11 ApprovalRequestId,
12 ProviderApprovalDecision,
13 ProviderDriverKind,
14 ProviderUserInputAnswers,
15 ProviderRuntimeEvent,
16 ProviderSendTurnInput,
17 ProviderSession,
18 ProviderSessionStartInput,
19 ProviderUploadFeedbackInput,
20 ProviderUploadFeedbackResult,
21 ThreadId,
22 ProviderTurnStartResult,
23 TurnId,
24} from "@t3tools/contracts";
25import type * as Effect from "effect/Effect";
26import type * as Stream from "effect/Stream";
27
28export type ProviderSessionModelSwitchMode = "in-session" | "unsupported";
29
30/**
31 * How ProviderService runs manual context compaction for an adapter.
32 * Native adapters expose a start call and must emit a compacted thread state
33 * when they finish. Slash-command adapters get the command sent as a turn.
34 */
35export type ProviderCompaction<TError> =
36 | {
37 readonly type: "native";
38 readonly start: (
39 threadId: ThreadId,
40 modelSelection?: ProviderSendTurnInput["modelSelection"],
41 ) => Effect.Effect<void, TError>;
42 }
43 | { readonly type: "slash-command"; readonly command: `/${string}` };
44
45export interface ProviderAdapterCapabilities {
46 /**
47 * Declares whether changing the model on an existing session is supported.
48 */
49 readonly sessionModelSwitch: ProviderSessionModelSwitchMode;
50 /** Starts a resumed turn with no synthetic user prompt. Omitted means the
51 adapter needs an explicit continuation instruction. */
52 readonly promptlessTurnContinuation?: boolean;
53 /** False when native conversation history cannot be rewound. */
54 readonly supportsConversationRollback?: boolean;
55}
56
57export interface ProviderThreadTurnSnapshot {
58 readonly id: TurnId;
59 readonly items: ReadonlyArray<unknown>;
60}
61
62export interface ProviderThreadSnapshot {
63 readonly threadId: ThreadId;
64 readonly turns: ReadonlyArray<ProviderThreadTurnSnapshot>;
65}
66
67export interface ProviderAdapterShape<TError> {
68 /**
69 * Provider kind implemented by this adapter.
70 */
71 readonly provider: ProviderDriverKind;
72 readonly capabilities: ProviderAdapterCapabilities;
73
74 /**
75 * Start a provider-backed session.
76 */
77 readonly startSession: (
78 input: ProviderSessionStartInput,
79 ) => Effect.Effect<ProviderSession, TError>;
80
81 /**
82 * Send a turn to an active provider session.
83 */
84 readonly sendTurn: (
85 input: ProviderSendTurnInput,
86 ) => Effect.Effect<ProviderTurnStartResult, TError>;
87
88 /** Omitted when this adapter does not support manual context compaction. */
89 readonly compaction?: ProviderCompaction<TError>;
90
91 /**
92 * Interrupt an active turn.
93 */
94 readonly interruptTurn: (threadId: ThreadId, turnId?: TurnId) => Effect.Effect<void, TError>;
95
96 /**
97 * Respond to an interactive approval request.
98 */
99 readonly respondToRequest: (
100 threadId: ThreadId,
101 requestId: ApprovalRequestId,
102 decision: ProviderApprovalDecision,
103 ) => Effect.Effect<void, TError>;
104
105 /**
106 * Respond to a structured user-input request.
107 */
108 readonly respondToUserInput: (
109 threadId: ThreadId,
110 requestId: ApprovalRequestId,
111 answers: ProviderUserInputAnswers,
112 ) => Effect.Effect<void, TError>;
113
114 /**
115 * Stop one provider session.
116 */
117 readonly stopSession: (threadId: ThreadId) => Effect.Effect<void, TError>;
118
119 /**
120 * List currently active provider sessions for this adapter.
121 */
122 readonly listSessions: () => Effect.Effect<ReadonlyArray<ProviderSession>>;
123
124 /**
125 * Check whether this adapter owns an active session id.
126 */
127 readonly hasSession: (threadId: ThreadId) => Effect.Effect<boolean>;
128
129 /**
130 * Read a provider thread snapshot.
131 */
132 readonly readThread: (threadId: ThreadId) => Effect.Effect<ProviderThreadSnapshot, TError>;
133
134 /**
135 * Roll back a provider thread by N turns.
136 */
137 readonly rollbackThread: (
138 threadId: ThreadId,
139 numTurns: number,
140 ) => Effect.Effect<ProviderThreadSnapshot, TError>;
141
142 /**
143 * Upload a thread to the provider when the adapter supports feedback.
144 */
145 readonly uploadFeedback?: (
146 input: ProviderUploadFeedbackInput,
147 ) => Effect.Effect<ProviderUploadFeedbackResult, TError>;
148
149 /**
150 * Stop all sessions owned by this adapter.
151 */
152 readonly stopAll: () => Effect.Effect<void, TError>;
153
154 /**
155 * Canonical runtime event stream emitted by this adapter.
156 */
157 readonly streamEvents: Stream.Stream<ProviderRuntimeEvent>;
158}