@claude-code-kit/agent 0.2.0
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/LICENSE +21 -0
- package/README.md +104 -0
- package/dist/index.d.mts +722 -0
- package/dist/index.d.ts +722 -0
- package/dist/index.js +1722 -0
- package/dist/index.mjs +1670 -0
- package/package.json +71 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type definitions for @claude-code-kit/agent.
|
|
5
|
+
* This is the single source of truth for all agent-related types.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
interface TextContentPart {
|
|
9
|
+
type: "text";
|
|
10
|
+
text: string;
|
|
11
|
+
}
|
|
12
|
+
interface ImageContentPart {
|
|
13
|
+
type: "image";
|
|
14
|
+
data: string;
|
|
15
|
+
mediaType: string;
|
|
16
|
+
}
|
|
17
|
+
type ContentPart = TextContentPart | ImageContentPart;
|
|
18
|
+
interface ToolCall {
|
|
19
|
+
id: string;
|
|
20
|
+
name: string;
|
|
21
|
+
input: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
interface SystemMessage {
|
|
24
|
+
role: "system";
|
|
25
|
+
content: string;
|
|
26
|
+
}
|
|
27
|
+
interface UserMessage {
|
|
28
|
+
role: "user";
|
|
29
|
+
content: string | ContentPart[];
|
|
30
|
+
}
|
|
31
|
+
interface AssistantMessage {
|
|
32
|
+
role: "assistant";
|
|
33
|
+
content: string | ContentPart[];
|
|
34
|
+
toolCalls?: ToolCall[];
|
|
35
|
+
}
|
|
36
|
+
interface ToolResultMessage {
|
|
37
|
+
role: "tool";
|
|
38
|
+
toolCallId: string;
|
|
39
|
+
content: string | ContentPart[];
|
|
40
|
+
isError?: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Protocol-level message used in the agent loop and LLM provider communication.
|
|
44
|
+
*
|
|
45
|
+
* This type represents the canonical message format for agent conversations,
|
|
46
|
+
* tool calls, and tool results. It is distinct from the display-oriented
|
|
47
|
+
* `Message` type in `@claude-code-kit/ui`, which adds fields like `id` and
|
|
48
|
+
* `timestamp` for rendering. The `useAgent` bridge handles conversion between
|
|
49
|
+
* the two formats automatically.
|
|
50
|
+
*/
|
|
51
|
+
type Message = SystemMessage | UserMessage | AssistantMessage | ToolResultMessage;
|
|
52
|
+
interface ToolContext {
|
|
53
|
+
workingDirectory: string;
|
|
54
|
+
abortSignal: AbortSignal;
|
|
55
|
+
onProgress?: (progress: ToolProgress) => void;
|
|
56
|
+
env?: Record<string, string>;
|
|
57
|
+
}
|
|
58
|
+
interface ToolProgress {
|
|
59
|
+
percent?: number;
|
|
60
|
+
message?: string;
|
|
61
|
+
}
|
|
62
|
+
interface ToolResult {
|
|
63
|
+
content: string;
|
|
64
|
+
isError?: boolean;
|
|
65
|
+
metadata?: Record<string, unknown>;
|
|
66
|
+
}
|
|
67
|
+
interface ToolDefinition<TInput = unknown> {
|
|
68
|
+
name: string;
|
|
69
|
+
description: string;
|
|
70
|
+
inputSchema: z.ZodType<TInput>;
|
|
71
|
+
execute(input: TInput, context: ToolContext): Promise<ToolResult>;
|
|
72
|
+
isReadOnly?: boolean;
|
|
73
|
+
isDestructive?: boolean;
|
|
74
|
+
requiresConfirmation?: boolean;
|
|
75
|
+
timeout?: number;
|
|
76
|
+
}
|
|
77
|
+
type StreamChunk = {
|
|
78
|
+
type: "text";
|
|
79
|
+
text: string;
|
|
80
|
+
} | {
|
|
81
|
+
type: "tool_use_start";
|
|
82
|
+
toolCall: {
|
|
83
|
+
id: string;
|
|
84
|
+
name: string;
|
|
85
|
+
};
|
|
86
|
+
} | {
|
|
87
|
+
type: "tool_use_delta";
|
|
88
|
+
text: string;
|
|
89
|
+
} | {
|
|
90
|
+
type: "tool_use_end";
|
|
91
|
+
} | {
|
|
92
|
+
type: "thinking";
|
|
93
|
+
text: string;
|
|
94
|
+
} | {
|
|
95
|
+
type: "usage";
|
|
96
|
+
usage: {
|
|
97
|
+
inputTokens: number;
|
|
98
|
+
outputTokens: number;
|
|
99
|
+
};
|
|
100
|
+
} | {
|
|
101
|
+
type: "done";
|
|
102
|
+
stopReason?: string;
|
|
103
|
+
} | {
|
|
104
|
+
type: "error";
|
|
105
|
+
error: Error;
|
|
106
|
+
};
|
|
107
|
+
interface ProviderTool {
|
|
108
|
+
name: string;
|
|
109
|
+
description: string;
|
|
110
|
+
inputSchema: Record<string, unknown>;
|
|
111
|
+
}
|
|
112
|
+
interface ChatOptions {
|
|
113
|
+
model: string;
|
|
114
|
+
messages: Message[];
|
|
115
|
+
tools?: ProviderTool[];
|
|
116
|
+
systemPrompt?: string;
|
|
117
|
+
maxTokens?: number;
|
|
118
|
+
temperature?: number;
|
|
119
|
+
signal?: AbortSignal;
|
|
120
|
+
}
|
|
121
|
+
interface LLMProvider {
|
|
122
|
+
chat(options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
123
|
+
countTokens?(messages: Message[]): Promise<number>;
|
|
124
|
+
}
|
|
125
|
+
interface TextEvent {
|
|
126
|
+
type: "text";
|
|
127
|
+
text: string;
|
|
128
|
+
}
|
|
129
|
+
interface ToolCallEvent {
|
|
130
|
+
type: "tool_call";
|
|
131
|
+
toolCall: ToolCall;
|
|
132
|
+
}
|
|
133
|
+
interface ToolResultEvent {
|
|
134
|
+
type: "tool_result";
|
|
135
|
+
toolCallId: string;
|
|
136
|
+
result: ToolResult;
|
|
137
|
+
}
|
|
138
|
+
interface ThinkingEvent {
|
|
139
|
+
type: "thinking";
|
|
140
|
+
text: string;
|
|
141
|
+
}
|
|
142
|
+
interface UsageEvent {
|
|
143
|
+
type: "usage";
|
|
144
|
+
inputTokens: number;
|
|
145
|
+
outputTokens: number;
|
|
146
|
+
}
|
|
147
|
+
interface ErrorEvent {
|
|
148
|
+
type: "error";
|
|
149
|
+
error: Error;
|
|
150
|
+
}
|
|
151
|
+
interface DoneEvent {
|
|
152
|
+
type: "done";
|
|
153
|
+
messages: Message[];
|
|
154
|
+
}
|
|
155
|
+
type AgentEvent = TextEvent | ToolCallEvent | ToolResultEvent | ThinkingEvent | UsageEvent | ErrorEvent | DoneEvent;
|
|
156
|
+
interface PermissionRequest {
|
|
157
|
+
tool: string;
|
|
158
|
+
input: Record<string, unknown>;
|
|
159
|
+
isReadOnly?: boolean;
|
|
160
|
+
}
|
|
161
|
+
interface PermissionResult {
|
|
162
|
+
decision: "allow" | "deny";
|
|
163
|
+
reason?: string;
|
|
164
|
+
}
|
|
165
|
+
type PermissionHandler = (request: PermissionRequest) => Promise<PermissionResult>;
|
|
166
|
+
interface PermissionConfig {
|
|
167
|
+
alwaysAllow?: string[];
|
|
168
|
+
alwaysDeny?: string[];
|
|
169
|
+
sessionApproved?: Set<string>;
|
|
170
|
+
autoApproveReadOnly?: boolean;
|
|
171
|
+
onPermission?: PermissionHandler;
|
|
172
|
+
}
|
|
173
|
+
interface Session {
|
|
174
|
+
getMessages(): Message[];
|
|
175
|
+
setMessages(messages: Message[]): void;
|
|
176
|
+
clear(): void;
|
|
177
|
+
}
|
|
178
|
+
interface CompactionStrategy {
|
|
179
|
+
compact(messages: Message[], maxTokens: number): Message[] | Promise<Message[]>;
|
|
180
|
+
}
|
|
181
|
+
interface AgentConfig {
|
|
182
|
+
provider: LLMProvider;
|
|
183
|
+
model: string;
|
|
184
|
+
systemPrompt?: string;
|
|
185
|
+
tools?: ToolDefinition[];
|
|
186
|
+
maxTokens?: number;
|
|
187
|
+
temperature?: number;
|
|
188
|
+
contextLimit?: number;
|
|
189
|
+
compactionStrategy?: CompactionStrategy;
|
|
190
|
+
session?: Session;
|
|
191
|
+
permissionHandler?: PermissionHandler;
|
|
192
|
+
workingDirectory?: string;
|
|
193
|
+
maxTurns?: number;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Headless agent that runs an LLM query loop with tool execution.
|
|
198
|
+
*
|
|
199
|
+
* Stateful — maintains message history across `run()` calls.
|
|
200
|
+
* Platform-agnostic — works in Node.js scripts, CLI apps, web servers, anywhere.
|
|
201
|
+
*/
|
|
202
|
+
declare class Agent {
|
|
203
|
+
private provider;
|
|
204
|
+
private model;
|
|
205
|
+
private systemPrompt?;
|
|
206
|
+
private maxTokens?;
|
|
207
|
+
private temperature?;
|
|
208
|
+
private maxTurns;
|
|
209
|
+
private session;
|
|
210
|
+
private toolRegistry;
|
|
211
|
+
private contextManager;
|
|
212
|
+
private permissionHandler;
|
|
213
|
+
private workingDirectory;
|
|
214
|
+
private abortController;
|
|
215
|
+
constructor(config: AgentConfig);
|
|
216
|
+
/**
|
|
217
|
+
* Run the agent loop. Yields events as the agent processes the input.
|
|
218
|
+
*
|
|
219
|
+
* The loop:
|
|
220
|
+
* 1. Add user message(s) to conversation
|
|
221
|
+
* 2. Check compaction
|
|
222
|
+
* 3. Call provider.chat() with messages + tools
|
|
223
|
+
* 4. Stream chunks, accumulate text + tool calls
|
|
224
|
+
* 5. If tool_use: check permission -> execute -> add results -> loop to step 2
|
|
225
|
+
* 6. If end_turn: yield done event with full message history
|
|
226
|
+
*/
|
|
227
|
+
run(input: string | Message[]): AsyncGenerator<AgentEvent>;
|
|
228
|
+
/**
|
|
229
|
+
* Simple API that wraps run() — sends a message and returns the final text response.
|
|
230
|
+
*/
|
|
231
|
+
chat(input: string): Promise<string>;
|
|
232
|
+
/** Abort the current run. */
|
|
233
|
+
abort(): void;
|
|
234
|
+
/** Replace the provider (e.g. to switch models mid-conversation). */
|
|
235
|
+
setProvider(provider: LLMProvider): void;
|
|
236
|
+
/** Get the full message history. */
|
|
237
|
+
getMessages(): Message[];
|
|
238
|
+
/** Clear the message history. */
|
|
239
|
+
clearMessages(): void;
|
|
240
|
+
/** Add a tool to the registry. */
|
|
241
|
+
addTool(tool: ToolDefinition): void;
|
|
242
|
+
/** Remove a tool from the registry. */
|
|
243
|
+
removeTool(name: string): boolean;
|
|
244
|
+
/** Replace the permission handler at runtime. */
|
|
245
|
+
setPermissionHandler(handler: PermissionHandler): void;
|
|
246
|
+
private executeToolCalls;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Registry that holds tool definitions and provides lookup + execution.
|
|
251
|
+
*/
|
|
252
|
+
declare class ToolRegistry {
|
|
253
|
+
private tools;
|
|
254
|
+
register(tool: ToolDefinition): void;
|
|
255
|
+
unregister(name: string): boolean;
|
|
256
|
+
get(name: string): ToolDefinition | undefined;
|
|
257
|
+
has(name: string): boolean;
|
|
258
|
+
list(): ToolDefinition[];
|
|
259
|
+
/**
|
|
260
|
+
* Returns all tools in the provider-ready format (name, description, JSON Schema).
|
|
261
|
+
*/
|
|
262
|
+
toProviderFormat(): ProviderTool[];
|
|
263
|
+
/**
|
|
264
|
+
* Execute a tool by name with the given input.
|
|
265
|
+
*/
|
|
266
|
+
execute(name: string, input: Record<string, unknown>, context: ToolContext): Promise<ToolResult>;
|
|
267
|
+
clear(): void;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Estimate token count for a message using the ~4 chars/token heuristic.
|
|
272
|
+
*/
|
|
273
|
+
declare function estimateTokens(message: Message): number;
|
|
274
|
+
/**
|
|
275
|
+
* Estimate total token count for an array of messages.
|
|
276
|
+
*/
|
|
277
|
+
declare function estimateTotalTokens(messages: Message[]): number;
|
|
278
|
+
/**
|
|
279
|
+
* Manages the conversation context window, triggering compaction
|
|
280
|
+
* when approaching the token limit.
|
|
281
|
+
*/
|
|
282
|
+
declare class ContextManager {
|
|
283
|
+
private contextLimit;
|
|
284
|
+
private compactionStrategy;
|
|
285
|
+
private provider;
|
|
286
|
+
/** Compact when usage exceeds this fraction of the context limit */
|
|
287
|
+
private compactionThreshold;
|
|
288
|
+
constructor(options: {
|
|
289
|
+
contextLimit?: number;
|
|
290
|
+
compactionStrategy?: CompactionStrategy;
|
|
291
|
+
provider?: LLMProvider;
|
|
292
|
+
});
|
|
293
|
+
/**
|
|
294
|
+
* Count tokens for the messages. Uses provider.countTokens if available,
|
|
295
|
+
* otherwise falls back to heuristic estimation.
|
|
296
|
+
*/
|
|
297
|
+
countTokens(messages: Message[]): Promise<number>;
|
|
298
|
+
/**
|
|
299
|
+
* Check if compaction is needed and apply it if so.
|
|
300
|
+
* Returns the (possibly compacted) messages array.
|
|
301
|
+
*/
|
|
302
|
+
maybeCompact(messages: Message[]): Promise<Message[]>;
|
|
303
|
+
/**
|
|
304
|
+
* Force compaction (e.g. after receiving a "context too long" error from the API).
|
|
305
|
+
*/
|
|
306
|
+
forceCompact(messages: Message[]): Promise<Message[]>;
|
|
307
|
+
setContextLimit(limit: number): void;
|
|
308
|
+
setCompactionStrategy(strategy: CompactionStrategy): void;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* No-op compaction strategy that returns messages unchanged.
|
|
313
|
+
* Used when no compaction is desired.
|
|
314
|
+
*/
|
|
315
|
+
declare class NoopCompaction implements CompactionStrategy {
|
|
316
|
+
compact(messages: Message[], _maxTokens: number): Message[];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Sliding window compaction: keeps the system message (if any) plus
|
|
321
|
+
* the most recent messages that fit within the token budget.
|
|
322
|
+
*
|
|
323
|
+
* When compacting, older messages are dropped from the middle, preserving
|
|
324
|
+
* the system message at the start and the most recent messages at the end.
|
|
325
|
+
*/
|
|
326
|
+
declare class SlidingWindowCompaction implements CompactionStrategy {
|
|
327
|
+
compact(messages: Message[], maxTokens: number): Message[];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
interface CompactionResult {
|
|
331
|
+
/** The compacted message array. */
|
|
332
|
+
messages: Message[];
|
|
333
|
+
/** Estimated token count before compaction. */
|
|
334
|
+
tokensBefore: number;
|
|
335
|
+
/** Estimated token count after compaction. */
|
|
336
|
+
tokensAfter: number;
|
|
337
|
+
/** Name of the strategy that was applied. */
|
|
338
|
+
strategy: string;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* LLM-based compaction that summarizes older messages and keeps only
|
|
342
|
+
* the most recent N messages verbatim.
|
|
343
|
+
*
|
|
344
|
+
* Implements `CompactionStrategy` so it can be used with `AgentConfig.compactionStrategy`.
|
|
345
|
+
* The LLM provider must be passed in the constructor since the `compact()` interface
|
|
346
|
+
* only receives `(messages, maxTokens)`.
|
|
347
|
+
*
|
|
348
|
+
* The synchronous `compact()` method (required by the interface) kicks off an
|
|
349
|
+
* async summarization internally. Use `compactAsync()` when you need the full
|
|
350
|
+
* `CompactionResult` with token stats.
|
|
351
|
+
*/
|
|
352
|
+
declare class SummarizationCompaction implements CompactionStrategy {
|
|
353
|
+
private keepRecentN;
|
|
354
|
+
private thresholdFraction;
|
|
355
|
+
private summaryMaxTokens;
|
|
356
|
+
private summaryModel;
|
|
357
|
+
private provider;
|
|
358
|
+
constructor(provider: LLMProvider, options?: {
|
|
359
|
+
keepRecentN?: number;
|
|
360
|
+
thresholdFraction?: number;
|
|
361
|
+
summaryMaxTokens?: number;
|
|
362
|
+
/** Model to use for generating summaries. Defaults to "claude-3-5-haiku-20241022". */
|
|
363
|
+
summaryModel?: string;
|
|
364
|
+
});
|
|
365
|
+
/** Return true when the current usage exceeds the configured threshold. */
|
|
366
|
+
shouldCompact(messages: Message[], tokenCount: number, contextLimit: number): boolean;
|
|
367
|
+
/**
|
|
368
|
+
* Async compaction conforming to the CompactionStrategy interface.
|
|
369
|
+
* Uses the LLM provider to summarize older messages before dropping them.
|
|
370
|
+
*/
|
|
371
|
+
compact(messages: Message[], _maxTokens: number): Promise<Message[]>;
|
|
372
|
+
/**
|
|
373
|
+
* Async compaction that uses the LLM provider to summarize older messages.
|
|
374
|
+
* Returns a `CompactionResult` with full token stats.
|
|
375
|
+
*/
|
|
376
|
+
compactAsync(messages: Message[]): Promise<CompactionResult>;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* In-memory session that holds conversation messages.
|
|
381
|
+
* Messages are lost when the process exits.
|
|
382
|
+
*/
|
|
383
|
+
declare class InMemorySession implements Session {
|
|
384
|
+
private messages;
|
|
385
|
+
getMessages(): Message[];
|
|
386
|
+
setMessages(messages: Message[]): void;
|
|
387
|
+
clear(): void;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* JSONL-backed session that persists conversation messages to disk.
|
|
392
|
+
* Each session is stored as `{directory}/{id}.jsonl`, one JSON message per line.
|
|
393
|
+
*/
|
|
394
|
+
declare class FileSession implements Session {
|
|
395
|
+
private directory;
|
|
396
|
+
private id;
|
|
397
|
+
private messages;
|
|
398
|
+
constructor(directory: string, id: string);
|
|
399
|
+
/** Return a snapshot of the in-memory messages. */
|
|
400
|
+
getMessages(): Message[];
|
|
401
|
+
/** Replace the in-memory messages and persist to disk. */
|
|
402
|
+
setMessages(messages: Message[]): void;
|
|
403
|
+
/** Clear in-memory messages (does not delete the file). */
|
|
404
|
+
clear(): void;
|
|
405
|
+
/** Resolve the .jsonl file path for this session. */
|
|
406
|
+
filePath(): string;
|
|
407
|
+
/** Load messages from disk into memory. Returns self for chaining. */
|
|
408
|
+
load(): Promise<this>;
|
|
409
|
+
/** Write current in-memory messages to disk (overwrites the file). */
|
|
410
|
+
save(): Promise<void>;
|
|
411
|
+
/** Append a single message to the file without rewriting it. */
|
|
412
|
+
append(message: Message): Promise<void>;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Store that creates and manages file-backed sessions in a directory.
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* ```ts
|
|
419
|
+
* const store = new FileSessionStore("./sessions");
|
|
420
|
+
* const session = await store.load("my-session");
|
|
421
|
+
* // ... use session ...
|
|
422
|
+
* await store.save("my-session", session);
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
declare class FileSessionStore {
|
|
426
|
+
private directory;
|
|
427
|
+
constructor(directory: string);
|
|
428
|
+
/** Create a new (empty) session without persisting it. */
|
|
429
|
+
create(id: string): FileSession;
|
|
430
|
+
/** Load an existing session from disk. Returns null if not found. */
|
|
431
|
+
load(id: string): Promise<FileSession | null>;
|
|
432
|
+
/** Persist the current messages of a session to disk. */
|
|
433
|
+
save(id: string, session: FileSession): Promise<void>;
|
|
434
|
+
/** List all session IDs in the directory. */
|
|
435
|
+
list(): Promise<string[]>;
|
|
436
|
+
/** Delete a session file from disk. */
|
|
437
|
+
delete(id: string): Promise<void>;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Create a tiered permission handler from a configuration object.
|
|
442
|
+
*
|
|
443
|
+
* Permission tiers (checked in order):
|
|
444
|
+
* 1. Always allow list — tool is unconditionally allowed
|
|
445
|
+
* 2. Always deny list — tool is unconditionally denied
|
|
446
|
+
* 3. Session approved — tool was approved earlier in this session
|
|
447
|
+
* 4. Read-only auto-approve — if enabled, read-only tools are allowed
|
|
448
|
+
* 5. Callback — delegate to a custom handler (e.g. prompt the user)
|
|
449
|
+
* 6. Default — allow (no permission handler = auto-approve everything)
|
|
450
|
+
*/
|
|
451
|
+
declare function createPermissionHandler(config: PermissionConfig): PermissionHandler;
|
|
452
|
+
/**
|
|
453
|
+
* Permission handler that allows everything. Useful for fully automated pipelines.
|
|
454
|
+
*/
|
|
455
|
+
declare const allowAll: PermissionHandler;
|
|
456
|
+
/**
|
|
457
|
+
* Default permission handler: allows read-only tools, denies everything else.
|
|
458
|
+
* Used when no permissionHandler is configured in AgentConfig.
|
|
459
|
+
*/
|
|
460
|
+
declare const allowReadOnly: PermissionHandler;
|
|
461
|
+
/**
|
|
462
|
+
* Permission handler that denies everything. Useful for dry-run / audit mode.
|
|
463
|
+
*/
|
|
464
|
+
declare const denyAll: PermissionHandler;
|
|
465
|
+
|
|
466
|
+
interface AnthropicProviderOptions {
|
|
467
|
+
apiKey?: string;
|
|
468
|
+
baseURL?: string;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Provider adapter for the Anthropic Messages API.
|
|
472
|
+
*
|
|
473
|
+
* Requires `@anthropic-ai/sdk` as an optional peer dependency.
|
|
474
|
+
* The SDK is dynamically imported at first use.
|
|
475
|
+
*/
|
|
476
|
+
declare class AnthropicProvider implements LLMProvider {
|
|
477
|
+
private options;
|
|
478
|
+
private clientPromise;
|
|
479
|
+
constructor(options?: AnthropicProviderOptions);
|
|
480
|
+
chat(options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
481
|
+
countTokens(messages: Message[]): Promise<number>;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
interface OpenAIProviderOptions {
|
|
485
|
+
apiKey?: string;
|
|
486
|
+
baseURL?: string;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Provider adapter for OpenAI-compatible Chat Completions API.
|
|
490
|
+
*
|
|
491
|
+
* Supports any OpenAI-compatible endpoint (OpenAI, Ollama, vLLM, Groq, Together)
|
|
492
|
+
* via the `baseURL` option.
|
|
493
|
+
*
|
|
494
|
+
* Requires `openai` SDK as an optional peer dependency.
|
|
495
|
+
*/
|
|
496
|
+
declare class OpenAIProvider implements LLMProvider {
|
|
497
|
+
private options;
|
|
498
|
+
private clientPromise;
|
|
499
|
+
constructor(options?: OpenAIProviderOptions);
|
|
500
|
+
chat(options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
501
|
+
countTokens(messages: Message[]): Promise<number>;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Mock provider for testing. Takes a queue of scripted responses —
|
|
506
|
+
* each run() call shifts one response off the queue and streams it.
|
|
507
|
+
*
|
|
508
|
+
* @example
|
|
509
|
+
* ```ts
|
|
510
|
+
* const provider = new MockProvider([
|
|
511
|
+
* [{ type: "text", text: "Hello!" }, { type: "done" }],
|
|
512
|
+
* [{ type: "text", text: "Goodbye!" }, { type: "done" }],
|
|
513
|
+
* ]);
|
|
514
|
+
* ```
|
|
515
|
+
*/
|
|
516
|
+
declare class MockProvider implements LLMProvider {
|
|
517
|
+
private queue;
|
|
518
|
+
private callLog;
|
|
519
|
+
constructor(responses: StreamChunk[][]);
|
|
520
|
+
chat(options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
521
|
+
countTokens(messages: Message[]): Promise<number>;
|
|
522
|
+
/** Get the log of all chat() calls made to this provider. */
|
|
523
|
+
getCalls(): ChatOptions[];
|
|
524
|
+
/** Check if all scripted responses have been consumed. */
|
|
525
|
+
isEmpty(): boolean;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
type AuthMethodApiKey = {
|
|
529
|
+
type: "api-key";
|
|
530
|
+
envVar?: string;
|
|
531
|
+
inputLabel?: string;
|
|
532
|
+
};
|
|
533
|
+
type AuthMethodBaseUrlKey = {
|
|
534
|
+
type: "base-url-key";
|
|
535
|
+
defaultBaseURL: string;
|
|
536
|
+
envVar?: string;
|
|
537
|
+
inputLabel?: string;
|
|
538
|
+
};
|
|
539
|
+
type AuthMethodNone = {
|
|
540
|
+
type: "none";
|
|
541
|
+
};
|
|
542
|
+
type AuthMethod = AuthMethodApiKey | AuthMethodBaseUrlKey | AuthMethodNone;
|
|
543
|
+
type AuthType = AuthMethod["type"];
|
|
544
|
+
interface ProviderRegistration {
|
|
545
|
+
displayName: string;
|
|
546
|
+
description?: string;
|
|
547
|
+
authMethods: AuthMethod[];
|
|
548
|
+
defaultModel?: string;
|
|
549
|
+
models?: string[];
|
|
550
|
+
/** Factory that creates a configured LLMProvider from resolved credentials */
|
|
551
|
+
createProvider: (config: {
|
|
552
|
+
apiKey?: string;
|
|
553
|
+
baseURL?: string;
|
|
554
|
+
token?: string;
|
|
555
|
+
}) => LLMProvider;
|
|
556
|
+
}
|
|
557
|
+
type AuthFlowStep = "select-provider" | "select-auth-method" | "input-credentials" | "select-model" | "done";
|
|
558
|
+
interface AuthFlowProviderOption {
|
|
559
|
+
name: string;
|
|
560
|
+
displayName: string;
|
|
561
|
+
description?: string;
|
|
562
|
+
}
|
|
563
|
+
interface AuthFlowState {
|
|
564
|
+
step: AuthFlowStep;
|
|
565
|
+
providers?: AuthFlowProviderOption[];
|
|
566
|
+
authMethods?: AuthMethod[];
|
|
567
|
+
models?: string[];
|
|
568
|
+
currentProvider?: string;
|
|
569
|
+
currentAuthMethod?: AuthMethod;
|
|
570
|
+
currentModel?: string;
|
|
571
|
+
result?: {
|
|
572
|
+
provider: LLMProvider;
|
|
573
|
+
model: string;
|
|
574
|
+
providerName: string;
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
interface AuthStorage {
|
|
578
|
+
get(provider: string): Promise<string | null>;
|
|
579
|
+
set(provider: string, credential: string): Promise<void>;
|
|
580
|
+
delete(provider: string): Promise<void>;
|
|
581
|
+
list(): Promise<string[]>;
|
|
582
|
+
}
|
|
583
|
+
interface AuthOptions {
|
|
584
|
+
storage?: AuthStorage;
|
|
585
|
+
/** Default: ~/.claude-code-kit/credentials.json */
|
|
586
|
+
storagePath?: string;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
interface ProviderInfo {
|
|
590
|
+
name: string;
|
|
591
|
+
registration: ProviderRegistration;
|
|
592
|
+
hasCredential: boolean;
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Open provider registry — register any LLM provider with multiple auth methods,
|
|
596
|
+
* then authenticate and get a configured LLMProvider instance.
|
|
597
|
+
*
|
|
598
|
+
* Supports an interactive step-by-step flow:
|
|
599
|
+
* select-provider → select-auth-method → input-credentials → select-model → done
|
|
600
|
+
*
|
|
601
|
+
* Credential resolution order (for automatic auth):
|
|
602
|
+
* 1. Environment variable (if any auth method has `envVar`)
|
|
603
|
+
* 2. Stored credential (from AuthStorage)
|
|
604
|
+
* 3. Error (credential required but not found)
|
|
605
|
+
*/
|
|
606
|
+
declare class AuthRegistry {
|
|
607
|
+
private providers;
|
|
608
|
+
private storage;
|
|
609
|
+
constructor(options?: AuthOptions);
|
|
610
|
+
/**
|
|
611
|
+
* Register a provider. Open — any provider, any auth type.
|
|
612
|
+
* Overwrites existing registration with the same name.
|
|
613
|
+
*/
|
|
614
|
+
register(name: string, registration: ProviderRegistration): void;
|
|
615
|
+
unregister(name: string): boolean;
|
|
616
|
+
getRegistration(name: string): ProviderRegistration | undefined;
|
|
617
|
+
/**
|
|
618
|
+
* Get the list of available models for a registered provider.
|
|
619
|
+
*/
|
|
620
|
+
getModels(providerName: string): string[];
|
|
621
|
+
private resolveEnvCredential;
|
|
622
|
+
/**
|
|
623
|
+
* Authenticate and get a configured LLMProvider.
|
|
624
|
+
*
|
|
625
|
+
* Resolution order for each auth method (tried in order):
|
|
626
|
+
* 1. Environment variable (if method has `envVar`)
|
|
627
|
+
* 2. Stored credential (from AuthStorage)
|
|
628
|
+
* 3. Next auth method
|
|
629
|
+
*
|
|
630
|
+
* If no method resolves, throws.
|
|
631
|
+
*
|
|
632
|
+
* For providers with a `type: 'none'` auth method, no credential is required.
|
|
633
|
+
*/
|
|
634
|
+
authenticate(name: string): Promise<LLMProvider>;
|
|
635
|
+
/**
|
|
636
|
+
* Get a provider using only environment variables — no storage, no prompts.
|
|
637
|
+
* Throws if no env var resolves (or provider has a 'none' method).
|
|
638
|
+
*/
|
|
639
|
+
fromEnv(name: string): LLMProvider;
|
|
640
|
+
storeCredential(name: string, credential: string): Promise<void>;
|
|
641
|
+
listProviders(): Promise<ProviderInfo[]>;
|
|
642
|
+
logout(name: string): Promise<void>;
|
|
643
|
+
/**
|
|
644
|
+
* Start the interactive auth flow.
|
|
645
|
+
* Returns the initial state with all available providers listed.
|
|
646
|
+
*/
|
|
647
|
+
interactive(): AuthFlowState;
|
|
648
|
+
/**
|
|
649
|
+
* Advance the interactive flow by selecting a provider.
|
|
650
|
+
*/
|
|
651
|
+
selectProvider(providerName: string): AuthFlowState;
|
|
652
|
+
/**
|
|
653
|
+
* Advance the interactive flow by selecting an auth method.
|
|
654
|
+
* Only needed when a provider has multiple auth methods.
|
|
655
|
+
*/
|
|
656
|
+
selectAuthMethod(providerName: string, methodIndex: number): AuthFlowState;
|
|
657
|
+
/**
|
|
658
|
+
* Advance the interactive flow by providing credentials.
|
|
659
|
+
* Stores the credential and moves to model selection (or done).
|
|
660
|
+
*/
|
|
661
|
+
inputCredentials(providerName: string, method: AuthMethod, credentials: {
|
|
662
|
+
apiKey?: string;
|
|
663
|
+
baseURL?: string;
|
|
664
|
+
token?: string;
|
|
665
|
+
}): Promise<AuthFlowState>;
|
|
666
|
+
/**
|
|
667
|
+
* Advance the interactive flow by selecting a model.
|
|
668
|
+
* Returns the final 'done' state with a configured provider.
|
|
669
|
+
*/
|
|
670
|
+
selectModel(providerName: string, method: AuthMethod, model: string, credentials?: {
|
|
671
|
+
apiKey?: string;
|
|
672
|
+
baseURL?: string;
|
|
673
|
+
token?: string;
|
|
674
|
+
}): Promise<AuthFlowState>;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* File-based credential storage.
|
|
679
|
+
* Stores credentials as a JSON object `{ [provider]: credential }`.
|
|
680
|
+
*/
|
|
681
|
+
declare class FileAuthStorage implements AuthStorage {
|
|
682
|
+
private filePath;
|
|
683
|
+
constructor(filePath?: string);
|
|
684
|
+
get(provider: string): Promise<string | null>;
|
|
685
|
+
set(provider: string, credential: string): Promise<void>;
|
|
686
|
+
delete(provider: string): Promise<void>;
|
|
687
|
+
list(): Promise<string[]>;
|
|
688
|
+
private read;
|
|
689
|
+
private write;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* In-memory credential storage for testing.
|
|
693
|
+
*/
|
|
694
|
+
declare class MemoryAuthStorage implements AuthStorage {
|
|
695
|
+
private store;
|
|
696
|
+
get(provider: string): Promise<string | null>;
|
|
697
|
+
set(provider: string, credential: string): Promise<void>;
|
|
698
|
+
delete(provider: string): Promise<void>;
|
|
699
|
+
list(): Promise<string[]>;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Pre-registered common LLM providers.
|
|
704
|
+
* Each supports multiple auth methods — the user picks one during interactive flow.
|
|
705
|
+
* Users can override any of these or add their own via `registry.register()`.
|
|
706
|
+
*/
|
|
707
|
+
declare const PRESET_PROVIDERS: Record<string, ProviderRegistration>;
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Create an AuthRegistry with all preset providers pre-registered.
|
|
711
|
+
*
|
|
712
|
+
* @example
|
|
713
|
+
* ```ts
|
|
714
|
+
* const auth = createAuth();
|
|
715
|
+
* const provider = await auth.authenticate('anthropic');
|
|
716
|
+
* // or register your own
|
|
717
|
+
* auth.register('my-llm', { displayName: 'My LLM', authMethods: [...], ... });
|
|
718
|
+
* ```
|
|
719
|
+
*/
|
|
720
|
+
declare function createAuth(options?: AuthOptions): AuthRegistry;
|
|
721
|
+
|
|
722
|
+
export { Agent, type AgentConfig, type AgentEvent, AnthropicProvider, type AssistantMessage, type AuthFlowProviderOption, type AuthFlowState, type AuthFlowStep, type AuthMethod, type AuthMethodApiKey, type AuthMethodBaseUrlKey, type AuthMethodNone, type AuthOptions, AuthRegistry, type AuthStorage, type AuthType, type ChatOptions, type CompactionResult, type CompactionStrategy, type ContentPart, ContextManager, type DoneEvent, type ErrorEvent, FileAuthStorage, FileSession, FileSessionStore, type ImageContentPart, InMemorySession, type LLMProvider, MemoryAuthStorage, type Message, MockProvider, NoopCompaction, OpenAIProvider, PRESET_PROVIDERS, type PermissionConfig, type PermissionHandler, type PermissionRequest, type PermissionResult, type ProviderInfo, type ProviderRegistration, type ProviderTool, type Session, SlidingWindowCompaction, type StreamChunk, SummarizationCompaction, type SystemMessage, type TextContentPart, type TextEvent, type ThinkingEvent, type ToolCall, type ToolCallEvent, type ToolContext, type ToolDefinition, type ToolProgress, ToolRegistry, type ToolResult, type ToolResultEvent, type ToolResultMessage, type UsageEvent, type UserMessage, allowAll, allowReadOnly, createAuth, createPermissionHandler, denyAll, estimateTokens, estimateTotalTokens };
|