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