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