Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderAdapter.ts

138 lines3.8 KB

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