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