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

ProviderAdapter.ts

131 lines3.3 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 ProviderKind,
14 ProviderUserInputAnswers,
15 ProviderRuntimeEvent,
16 ProviderSendTurnInput,
17 ProviderSession,
18 ProviderSessionStartInput,
19 ThreadId,
20 ProviderTurnStartResult,
21 TurnId,
22} from "@t3tools/contracts";
23import type { Effect } from "effect";
24import type { Stream } from "effect";
25​
26export type ProviderSessionModelSwitchMode = "in-session" | "restart-session" | "unsupported";
27​
28export interface ProviderAdapterCapabilities {
29 /**
30 * Declares whether changing the model on an existing session is supported.
31 */
32 readonly sessionModelSwitch: ProviderSessionModelSwitchMode;
34​
35export interface ProviderThreadTurnSnapshot {
36 readonly id: TurnId;
37 readonly items: ReadonlyArray<unknown>;
39​
40export interface ProviderThreadSnapshot {
41 readonly threadId: ThreadId;
42 readonly turns: ReadonlyArray<ProviderThreadTurnSnapshot>;
44​
45export interface ProviderAdapterShape<TError> {
46 /**
47 * Provider kind implemented by this adapter.
48 */
49 readonly provider: ProviderKind;
50 readonly capabilities: ProviderAdapterCapabilities;
51​
52 /**
53 * Start a provider-backed session.
54 */
55 readonly startSession: (
56 input: ProviderSessionStartInput,
57 ) => Effect.Effect<ProviderSession, TError>;
58​
59 /**
60 * Send a turn to an active provider session.
61 */
62 readonly sendTurn: (
63 input: ProviderSendTurnInput,
64 ) => Effect.Effect<ProviderTurnStartResult, TError>;
65​
66 /**
67 * Interrupt an active turn.
68 */
69 readonly interruptTurn: (
70 threadId: ThreadId,
71 turnId?: TurnId,
72 ) => Effect.Effect<void, TError>;
73​
74 /**
75 * Respond to an interactive approval request.
76 */
77 readonly respondToRequest: (
78 threadId: ThreadId,
79 requestId: ApprovalRequestId,
80 decision: ProviderApprovalDecision,
81 ) => Effect.Effect<void, TError>;
82​
83 /**
84 * Respond to a structured user-input request.
85 */
86 readonly respondToUserInput: (
87 threadId: ThreadId,
88 requestId: ApprovalRequestId,
89 answers: ProviderUserInputAnswers,
90 ) => Effect.Effect<void, TError>;
91​
92 /**
93 * Stop one provider session.
94 */
95 readonly stopSession: (threadId: ThreadId) => Effect.Effect<void, TError>;
96​
97 /**
98 * List currently active provider sessions for this adapter.
99 */
100 readonly listSessions: () => Effect.Effect<ReadonlyArray<ProviderSession>>;
101​
102 /**
103 * Check whether this adapter owns an active session id.
104 */
105 readonly hasSession: (threadId: ThreadId) => Effect.Effect<boolean>;
106​
107 /**
108 * Read a provider thread snapshot.
109 */
110 readonly readThread: (
111 threadId: ThreadId,
112 ) => 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 * Stop all sessions owned by this adapter.
124 */
125 readonly stopAll: () => Effect.Effect<void, TError>;
126​
127 /**
128 * Canonical runtime event stream emitted by this adapter.
129 */
130 readonly streamEvents: Stream.Stream<ProviderRuntimeEvent>;