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
30export interface ProviderAdapterCapabilities {
31 /**
32 * Declares whether changing the model on an existing session is supported.
33 */
34 readonly sessionModelSwitch: ProviderSessionModelSwitchMode;
35 /** Starts a resumed turn with no synthetic user prompt. Omitted means the
36 adapter needs an explicit continuation instruction. */
37 readonly promptlessTurnContinuation?: boolean;
38 /** False when native conversation history cannot be rewound. */
39 readonly supportsConversationRollback?: boolean;
40}
41
42export interface ProviderThreadTurnSnapshot {
43 readonly id: TurnId;
44 readonly items: ReadonlyArray<unknown>;
45}
46
47export interface ProviderThreadSnapshot {
48 readonly threadId: ThreadId;
49 readonly turns: ReadonlyArray<ProviderThreadTurnSnapshot>;
50}
51
52export interface ProviderAdapterShape<TError> {
53 /**
54 * Provider kind implemented by this adapter.
55 */
56 readonly provider: ProviderDriverKind;
57 readonly capabilities: ProviderAdapterCapabilities;
58
59 /**
60 * Start a provider-backed session.
61 */
62 readonly startSession: (
63 input: ProviderSessionStartInput,
64 ) => Effect.Effect<ProviderSession, TError>;
65
66 /**
67 * Send a turn to an active provider session.
68 */
69 readonly sendTurn: (
70 input: ProviderSendTurnInput,
71 ) => Effect.Effect<ProviderTurnStartResult, TError>;
72
73 readonly compactThread?: (
74 threadId: ThreadId,
75 modelSelection?: ProviderSendTurnInput["modelSelection"],
76 ) => Effect.Effect<void, TError>;
77
78 /**
79 * Interrupt an active turn.
80 */
81 readonly interruptTurn: (threadId: ThreadId, turnId?: TurnId) => Effect.Effect<void, TError>;
82
83 /**
84 * Respond to an interactive approval request.
85 */
86 readonly respondToRequest: (
87 threadId: ThreadId,
88 requestId: ApprovalRequestId,
89 decision: ProviderApprovalDecision,
90 ) => Effect.Effect<void, TError>;
91
92 /**
93 * Respond to a structured user-input request.
94 */
95 readonly respondToUserInput: (
96 threadId: ThreadId,
97 requestId: ApprovalRequestId,
98 answers: ProviderUserInputAnswers,
99 ) => Effect.Effect<void, TError>;
100
101 /**
102 * Stop one provider session.
103 */
104 readonly stopSession: (threadId: ThreadId) => Effect.Effect<void, TError>;
105
106 /**
107 * List currently active provider sessions for this adapter.
108 */
109 readonly listSessions: () => Effect.Effect<ReadonlyArray<ProviderSession>>;
110
111 /**
112 * Check whether this adapter owns an active session id.
113 */
114 readonly hasSession: (threadId: ThreadId) => Effect.Effect<boolean>;
115
116 /**
117 * Read a provider thread snapshot.
118 */
119 readonly readThread: (threadId: ThreadId) => Effect.Effect<ProviderThreadSnapshot, TError>;
120
121 /**
122 * Roll back a provider thread by N turns.
123 */
124 readonly rollbackThread: (
125 threadId: ThreadId,
126 numTurns: number,
127 ) => Effect.Effect<ProviderThreadSnapshot, TError>;
128
129 /**
130 * Upload a thread to the provider when the adapter supports feedback.
131 */
132 readonly uploadFeedback?: (
133 input: ProviderUploadFeedbackInput,
134 ) => Effect.Effect<ProviderUploadFeedbackResult, TError>;
135
136 /**
137 * Stop all sessions owned by this adapter.
138 */
139 readonly stopAll: () => Effect.Effect<void, TError>;
140
141 /**
142 * Canonical runtime event stream emitted by this adapter.
143 */
144 readonly streamEvents: Stream.Stream<ProviderRuntimeEvent>;
145}