@agentxjs/claude-driver 1.9.5-dev → 1.9.7-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +302 -0
- package/dist/index.js +994 -0
- package/dist/index.js.map +1 -0
- package/package.json +14 -3
- package/tsconfig.json +0 -10
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { Driver, DriverConfig, DriverState, DriverStreamEvent } from '@agentxjs/core/driver';
|
|
2
|
+
export { CreateDriver, Driver, DriverConfig, DriverState, DriverStreamEvent, StopReason } from '@agentxjs/core/driver';
|
|
3
|
+
import { UserMessage } from '@agentxjs/core/agent';
|
|
4
|
+
import { McpServerConfig, SDKMessage, SDKUserMessage } from '@anthropic-ai/claude-agent-sdk';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* ClaudeDriver - Claude SDK Driver Implementation
|
|
8
|
+
*
|
|
9
|
+
* Implements the new Driver interface with clear input/output boundaries:
|
|
10
|
+
* - receive(message) returns AsyncIterable<DriverStreamEvent>
|
|
11
|
+
* - No EventBus dependency
|
|
12
|
+
* - Single session communication
|
|
13
|
+
*
|
|
14
|
+
* ```
|
|
15
|
+
* UserMessage
|
|
16
|
+
* │
|
|
17
|
+
* ▼
|
|
18
|
+
* ┌─────────────────┐
|
|
19
|
+
* │ ClaudeDriver │
|
|
20
|
+
* │ │
|
|
21
|
+
* │ receive() │──► AsyncIterable<DriverStreamEvent>
|
|
22
|
+
* │ │ │
|
|
23
|
+
* │ ▼ │
|
|
24
|
+
* │ SDK Query │
|
|
25
|
+
* └─────────────────┘
|
|
26
|
+
* │
|
|
27
|
+
* ▼
|
|
28
|
+
* Claude SDK
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* ClaudeDriver - Driver implementation for Claude SDK
|
|
34
|
+
*
|
|
35
|
+
* Implements the new Driver interface:
|
|
36
|
+
* - receive() returns AsyncIterable<DriverStreamEvent>
|
|
37
|
+
* - Clear input/output boundaries for recording/playback
|
|
38
|
+
* - Single session communication
|
|
39
|
+
*/
|
|
40
|
+
declare class ClaudeDriver implements Driver {
|
|
41
|
+
readonly name = "ClaudeDriver";
|
|
42
|
+
private _sessionId;
|
|
43
|
+
private _state;
|
|
44
|
+
private readonly config;
|
|
45
|
+
private queryLifecycle;
|
|
46
|
+
private currentTurnSubject;
|
|
47
|
+
constructor(config: DriverConfig);
|
|
48
|
+
get sessionId(): string | null;
|
|
49
|
+
get state(): DriverState;
|
|
50
|
+
/**
|
|
51
|
+
* Initialize the Driver
|
|
52
|
+
*
|
|
53
|
+
* Starts SDK subprocess and MCP servers.
|
|
54
|
+
* Must be called before receive().
|
|
55
|
+
*/
|
|
56
|
+
initialize(): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Dispose and cleanup resources
|
|
59
|
+
*
|
|
60
|
+
* Stops SDK subprocess and MCP servers.
|
|
61
|
+
* Driver cannot be used after dispose().
|
|
62
|
+
*/
|
|
63
|
+
dispose(): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Receive a user message and return stream of events
|
|
66
|
+
*
|
|
67
|
+
* This is the main method for communication.
|
|
68
|
+
* Returns an AsyncIterable that yields DriverStreamEvent.
|
|
69
|
+
*
|
|
70
|
+
* @param message - User message to send
|
|
71
|
+
* @returns AsyncIterable of stream events
|
|
72
|
+
*/
|
|
73
|
+
receive(message: UserMessage): AsyncIterable<DriverStreamEvent>;
|
|
74
|
+
/**
|
|
75
|
+
* Interrupt current operation
|
|
76
|
+
*
|
|
77
|
+
* Stops the current receive() operation gracefully.
|
|
78
|
+
* The AsyncIterable will emit an "interrupted" event and complete.
|
|
79
|
+
*/
|
|
80
|
+
interrupt(): void;
|
|
81
|
+
/**
|
|
82
|
+
* Ensure SDK lifecycle is initialized
|
|
83
|
+
*/
|
|
84
|
+
private ensureLifecycle;
|
|
85
|
+
/**
|
|
86
|
+
* Setup callbacks for a single turn
|
|
87
|
+
*/
|
|
88
|
+
private setupTurnCallbacks;
|
|
89
|
+
/**
|
|
90
|
+
* Convert SDK stream_event to DriverStreamEvent
|
|
91
|
+
*/
|
|
92
|
+
private convertStreamEvent;
|
|
93
|
+
/**
|
|
94
|
+
* Convert SDK user message (contains tool_result)
|
|
95
|
+
*/
|
|
96
|
+
private convertUserMessage;
|
|
97
|
+
/**
|
|
98
|
+
* Map SDK stop reason to our StopReason type
|
|
99
|
+
*/
|
|
100
|
+
private mapStopReason;
|
|
101
|
+
/**
|
|
102
|
+
* Yield events from Subject as AsyncIterable
|
|
103
|
+
*/
|
|
104
|
+
private yieldFromSubject;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* CreateDriver function for ClaudeDriver
|
|
108
|
+
*
|
|
109
|
+
* Factory function that creates a ClaudeDriver instance.
|
|
110
|
+
* Conforms to the CreateDriver type from @agentxjs/core/driver.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* import { createClaudeDriver } from "@agentxjs/claude-driver";
|
|
115
|
+
*
|
|
116
|
+
* const driver = createClaudeDriver({
|
|
117
|
+
* apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
118
|
+
* agentId: "my-agent",
|
|
119
|
+
* systemPrompt: "You are helpful",
|
|
120
|
+
* });
|
|
121
|
+
*
|
|
122
|
+
* await driver.initialize();
|
|
123
|
+
*
|
|
124
|
+
* for await (const event of driver.receive({ content: "Hello" })) {
|
|
125
|
+
* if (event.type === "text_delta") {
|
|
126
|
+
* process.stdout.write(event.data.text);
|
|
127
|
+
* }
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* await driver.dispose();
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
declare function createClaudeDriver(config: DriverConfig): Driver;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* SDKQueryLifecycle - Manages Claude SDK query lifecycle
|
|
137
|
+
*
|
|
138
|
+
* This class encapsulates the low-level SDK interaction:
|
|
139
|
+
* - Query initialization and lazy loading
|
|
140
|
+
* - Background listener for SDK responses
|
|
141
|
+
* - Interrupt and cleanup operations
|
|
142
|
+
*
|
|
143
|
+
* It emits events via callbacks, allowing the parent Driver
|
|
144
|
+
* to handle business logic like timeout management.
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Callbacks for SDK events
|
|
149
|
+
*/
|
|
150
|
+
interface SDKQueryCallbacks {
|
|
151
|
+
/** Called when a stream_event is received */
|
|
152
|
+
onStreamEvent?: (msg: SDKMessage) => void;
|
|
153
|
+
/** Called when a user message is received (contains tool_result) */
|
|
154
|
+
onUserMessage?: (msg: SDKMessage) => void;
|
|
155
|
+
/** Called when a result is received */
|
|
156
|
+
onResult?: (msg: SDKMessage) => void;
|
|
157
|
+
/** Called when session ID is captured */
|
|
158
|
+
onSessionIdCaptured?: (sessionId: string) => void;
|
|
159
|
+
/** Called when an error occurs */
|
|
160
|
+
onError?: (error: Error) => void;
|
|
161
|
+
/** Called when the listener exits (normally or due to error) */
|
|
162
|
+
onListenerExit?: (reason: "normal" | "abort" | "error") => void;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Configuration for SDKQueryLifecycle
|
|
166
|
+
*/
|
|
167
|
+
interface SDKQueryConfig {
|
|
168
|
+
apiKey: string;
|
|
169
|
+
baseUrl?: string;
|
|
170
|
+
model?: string;
|
|
171
|
+
systemPrompt?: string;
|
|
172
|
+
cwd?: string;
|
|
173
|
+
resumeSessionId?: string;
|
|
174
|
+
mcpServers?: Record<string, McpServerConfig>;
|
|
175
|
+
claudeCodePath?: string;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* SDKQueryLifecycle - Manages the lifecycle of a Claude SDK query
|
|
179
|
+
*
|
|
180
|
+
* Responsibilities:
|
|
181
|
+
* - Lazy initialization of SDK query
|
|
182
|
+
* - Background listener for SDK responses
|
|
183
|
+
* - Interrupt and cleanup operations
|
|
184
|
+
* - Resource management (subprocess termination)
|
|
185
|
+
*/
|
|
186
|
+
declare class SDKQueryLifecycle {
|
|
187
|
+
private readonly config;
|
|
188
|
+
private _callbacks;
|
|
189
|
+
private promptSubject;
|
|
190
|
+
private claudeQuery;
|
|
191
|
+
private isInitialized;
|
|
192
|
+
private abortController;
|
|
193
|
+
private capturedSessionId;
|
|
194
|
+
constructor(config: SDKQueryConfig, callbacks?: SDKQueryCallbacks);
|
|
195
|
+
/**
|
|
196
|
+
* Get current callbacks (for reading/modification)
|
|
197
|
+
*/
|
|
198
|
+
get callbacks(): SDKQueryCallbacks;
|
|
199
|
+
/**
|
|
200
|
+
* Update callbacks
|
|
201
|
+
*
|
|
202
|
+
* Allows changing callbacks after initialization.
|
|
203
|
+
* Useful for per-turn callback setup.
|
|
204
|
+
*/
|
|
205
|
+
setCallbacks(callbacks: Partial<SDKQueryCallbacks>): void;
|
|
206
|
+
/**
|
|
207
|
+
* Check if the query is initialized
|
|
208
|
+
*/
|
|
209
|
+
get initialized(): boolean;
|
|
210
|
+
/**
|
|
211
|
+
* Warmup the SDK query (pre-initialize)
|
|
212
|
+
*
|
|
213
|
+
* Call this early to start the SDK subprocess before the first message.
|
|
214
|
+
* This reduces latency for the first user message.
|
|
215
|
+
*
|
|
216
|
+
* @returns Promise that resolves when SDK is ready
|
|
217
|
+
*/
|
|
218
|
+
warmup(): Promise<void>;
|
|
219
|
+
/**
|
|
220
|
+
* Initialize the SDK query (lazy initialization)
|
|
221
|
+
*
|
|
222
|
+
* Creates the query and starts the background listener.
|
|
223
|
+
* Safe to call multiple times - will only initialize once.
|
|
224
|
+
*/
|
|
225
|
+
initialize(): Promise<void>;
|
|
226
|
+
/**
|
|
227
|
+
* Send a message to the SDK
|
|
228
|
+
*
|
|
229
|
+
* Must call initialize() first.
|
|
230
|
+
*/
|
|
231
|
+
send(message: SDKUserMessage): void;
|
|
232
|
+
/**
|
|
233
|
+
* Interrupt the current SDK operation
|
|
234
|
+
*/
|
|
235
|
+
interrupt(): void;
|
|
236
|
+
/**
|
|
237
|
+
* Reset state and cleanup resources
|
|
238
|
+
*
|
|
239
|
+
* This properly terminates the Claude subprocess by:
|
|
240
|
+
* 1. Completing the prompt stream (signals end of input)
|
|
241
|
+
* 2. Interrupting any ongoing operation
|
|
242
|
+
* 3. Resetting state for potential reuse
|
|
243
|
+
*/
|
|
244
|
+
reset(): void;
|
|
245
|
+
/**
|
|
246
|
+
* Dispose and cleanup all resources
|
|
247
|
+
*
|
|
248
|
+
* Should be called when the lifecycle is no longer needed.
|
|
249
|
+
*/
|
|
250
|
+
dispose(): void;
|
|
251
|
+
/**
|
|
252
|
+
* Start the background listener for SDK responses
|
|
253
|
+
*/
|
|
254
|
+
private startBackgroundListener;
|
|
255
|
+
/**
|
|
256
|
+
* Check if an error is an abort error
|
|
257
|
+
*/
|
|
258
|
+
private isAbortError;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Helper functions for Claude Driver
|
|
263
|
+
*/
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Claude API content block types
|
|
267
|
+
*/
|
|
268
|
+
type ClaudeTextBlock = {
|
|
269
|
+
type: "text";
|
|
270
|
+
text: string;
|
|
271
|
+
};
|
|
272
|
+
type ClaudeImageBlock = {
|
|
273
|
+
type: "image";
|
|
274
|
+
source: {
|
|
275
|
+
type: "base64";
|
|
276
|
+
media_type: string;
|
|
277
|
+
data: string;
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
type ClaudeDocumentBlock = {
|
|
281
|
+
type: "document";
|
|
282
|
+
source: {
|
|
283
|
+
type: "base64";
|
|
284
|
+
media_type: string;
|
|
285
|
+
data: string;
|
|
286
|
+
};
|
|
287
|
+
};
|
|
288
|
+
type ClaudeContentBlock = ClaudeTextBlock | ClaudeImageBlock | ClaudeDocumentBlock;
|
|
289
|
+
/**
|
|
290
|
+
* Build SDK content from UserMessage
|
|
291
|
+
*
|
|
292
|
+
* Converts AgentX ContentPart[] to Claude API format:
|
|
293
|
+
* - Pure text messages return as string (for efficiency)
|
|
294
|
+
* - Mixed content returns as ClaudeContentBlock[]
|
|
295
|
+
*/
|
|
296
|
+
declare function buildSDKContent(message: UserMessage): string | ClaudeContentBlock[];
|
|
297
|
+
/**
|
|
298
|
+
* Build SDK UserMessage from AgentX UserMessage
|
|
299
|
+
*/
|
|
300
|
+
declare function buildSDKUserMessage(message: UserMessage, sessionId: string): SDKUserMessage;
|
|
301
|
+
|
|
302
|
+
export { ClaudeDriver, type SDKQueryCallbacks, type SDKQueryConfig, SDKQueryLifecycle, buildSDKContent, buildSDKUserMessage, createClaudeDriver };
|