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

ProviderAdapter.ts

126 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 ProviderDriverKind,
14 ProviderUserInputAnswers,
15 ProviderRuntimeEvent,
16 ProviderSendTurnInput,
17 ProviderSession,
18 ProviderSessionStartInput,
19 ThreadId,
20 ProviderTurnStartResult,
21 TurnId,
22} from "@t3tools/contracts";
23import type * as Effect from "effect/Effect";
24import type * as Stream from "effect/Stream";
25​
26export type ProviderSessionModelSwitchMode = "in-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: ProviderDriverKind;
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: (threadId: ThreadId, turnId?: TurnId) => Effect.Effect<void, TError>;
70​
71 /**
72 * Respond to an interactive approval request.
73 */
74 readonly respondToRequest: (
75 threadId: ThreadId,
76 requestId: ApprovalRequestId,
77 decision: ProviderApprovalDecision,
78 ) => Effect.Effect<void, TError>;
79​
80 /**
81 * Respond to a structured user-input request.
82 */
83 readonly respondToUserInput: (
84 threadId: ThreadId,
85 requestId: ApprovalRequestId,
86 answers: ProviderUserInputAnswers,
87 ) => Effect.Effect<void, TError>;
88​
89 /**
90 * Stop one provider session.
91 */
92 readonly stopSession: (threadId: ThreadId) => Effect.Effect<void, TError>;
93​
94 /**
95 * List currently active provider sessions for this adapter.
96 */
97 readonly listSessions: () => Effect.Effect<ReadonlyArray<ProviderSession>>;
98​
99 /**
100 * Check whether this adapter owns an active session id.
101 */
102 readonly hasSession: (threadId: ThreadId) => Effect.Effect<boolean>;
103​
104 /**
105 * Read a provider thread snapshot.
106 */
107 readonly readThread: (threadId: ThreadId) => Effect.Effect<ProviderThreadSnapshot, TError>;
108​
109 /**
110 * Roll back a provider thread by N turns.
111 */
112 readonly rollbackThread: (
113 threadId: ThreadId,
114 numTurns: number,
115 ) => Effect.Effect<ProviderThreadSnapshot, TError>;
116​
117 /**
118 * Stop all sessions owned by this adapter.
119 */
120 readonly stopAll: () => Effect.Effect<void, TError>;
121​
122 /**
123 * Canonical runtime event stream emitted by this adapter.
124 */
125 readonly streamEvents: Stream.Stream<ProviderRuntimeEvent>;