ai-sdk-provider-claude-code 1.2.0 → 2.0.2
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/README.md +98 -14
- package/dist/index.cjs +169 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +55 -3
- package/dist/index.d.ts +55 -3
- package/dist/index.js +163 -71
- package/dist/index.js.map +1 -1
- package/docs/ai-sdk-v4/DEVELOPMENT-STATUS.md +18 -8
- package/docs/ai-sdk-v4/GUIDE.md +94 -67
- package/docs/ai-sdk-v4/TROUBLESHOOTING.md +25 -14
- package/docs/ai-sdk-v5/DEVELOPMENT-STATUS.md +36 -16
- package/docs/ai-sdk-v5/GUIDE.md +120 -78
- package/docs/ai-sdk-v5/TOOL_STREAMING_SUPPORT.md +16 -7
- package/docs/ai-sdk-v5/TROUBLESHOOTING.md +36 -21
- package/docs/ai-sdk-v5/V5_BREAKING_CHANGES.md +9 -8
- package/docs/ai-sdk-v5/V5_MIGRATION_PLAN.md +3 -3
- package/docs/ai-sdk-v5/V5_MIGRATION_SUMMARY.md +21 -13
- package/docs/ai-sdk-v5/V5_MIGRATION_TASKS.md +17 -17
- package/package.json +99 -93
package/README.md
CHANGED
|
@@ -9,27 +9,36 @@
|
|
|
9
9
|
|
|
10
10
|
# AI SDK Provider for Claude Code SDK
|
|
11
11
|
|
|
12
|
-
> **
|
|
12
|
+
> **Latest Release**: Version 2.x uses the Claude Agent SDK with explicit configuration defaults. Version 1.x is the previous stable release. For AI SDK v4 support, use the `ai-sdk-v4` tag or version 0.2.x.
|
|
13
13
|
|
|
14
|
-
**ai-sdk-provider-claude-code** lets you use Claude via the [Vercel AI SDK](https://sdk.vercel.ai/docs) through the official `@anthropic-ai/claude-
|
|
14
|
+
**ai-sdk-provider-claude-code** lets you use Claude via the [Vercel AI SDK](https://sdk.vercel.ai/docs) through the official `@anthropic-ai/claude-agent-sdk` and the Claude Code CLI.
|
|
15
15
|
|
|
16
16
|
## Version Compatibility
|
|
17
17
|
|
|
18
|
-
| Provider Version | AI SDK Version | NPM Tag
|
|
19
|
-
|
|
20
|
-
|
|
|
21
|
-
|
|
|
18
|
+
| Provider Version | AI SDK Version | Underlying SDK | NPM Tag | Status | Branch |
|
|
19
|
+
| ---------------- | -------------- | ------------------------------------ | -------------------- | ------ | --------------------------------------------------------------------------------------- |
|
|
20
|
+
| 2.x.x | v5 | `@anthropic-ai/claude-agent-sdk` | `latest` | Stable | `main` |
|
|
21
|
+
| 1.x.x | v5 | `@anthropic-ai/claude-code` (legacy) | `v1-claude-code-sdk` | Stable | [`v1`](https://github.com/ben-vargas/ai-sdk-provider-claude-code/tree/v1) |
|
|
22
|
+
| 0.x.x | v4 | `@anthropic-ai/claude-code` | `ai-sdk-v4` | Legacy | [`ai-sdk-v4`](https://github.com/ben-vargas/ai-sdk-provider-claude-code/tree/ai-sdk-v4) |
|
|
22
23
|
|
|
23
24
|
### Installing the Right Version
|
|
24
25
|
|
|
25
|
-
**For AI SDK v5 (recommended):**
|
|
26
|
+
**For AI SDK v5 with Claude Agent SDK (recommended):**
|
|
27
|
+
|
|
26
28
|
```bash
|
|
27
29
|
npm install ai-sdk-provider-claude-code ai
|
|
28
30
|
# or explicitly: npm install ai-sdk-provider-claude-code@latest
|
|
29
31
|
```
|
|
30
32
|
|
|
31
|
-
**For AI SDK
|
|
33
|
+
**For AI SDK v5 with Claude Code SDK (legacy):**
|
|
34
|
+
|
|
32
35
|
```bash
|
|
36
|
+
npm install ai-sdk-provider-claude-code@v1-claude-code-sdk ai
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**For AI SDK v4 (legacy):**
|
|
40
|
+
|
|
41
|
+
````bash
|
|
33
42
|
npm install ai-sdk-provider-claude-code@ai-sdk-v4 ai@^4.3.16
|
|
34
43
|
# or use specific version: npm install ai-sdk-provider-claude-code@^0.2.2
|
|
35
44
|
|
|
@@ -39,9 +48,10 @@ npm install ai-sdk-provider-claude-code@ai-sdk-v4 ai@^4.3.16
|
|
|
39
48
|
```bash
|
|
40
49
|
npm install -g @anthropic-ai/claude-code
|
|
41
50
|
claude login
|
|
42
|
-
|
|
51
|
+
````
|
|
43
52
|
|
|
44
53
|
### 2. Add the provider
|
|
54
|
+
|
|
45
55
|
```bash
|
|
46
56
|
# For v5 (recommended)
|
|
47
57
|
npm install ai-sdk-provider-claude-code ai
|
|
@@ -63,13 +73,14 @@ Please ensure you have appropriate permissions and comply with all applicable te
|
|
|
63
73
|
## Quick Start
|
|
64
74
|
|
|
65
75
|
### AI SDK v5
|
|
76
|
+
|
|
66
77
|
```typescript
|
|
67
78
|
import { streamText } from 'ai';
|
|
68
79
|
import { claudeCode } from 'ai-sdk-provider-claude-code';
|
|
69
80
|
|
|
70
81
|
const result = streamText({
|
|
71
82
|
model: claudeCode('sonnet'),
|
|
72
|
-
prompt: 'Hello, Claude!'
|
|
83
|
+
prompt: 'Hello, Claude!',
|
|
73
84
|
});
|
|
74
85
|
|
|
75
86
|
const text = await result.text;
|
|
@@ -77,23 +88,35 @@ console.log(text);
|
|
|
77
88
|
```
|
|
78
89
|
|
|
79
90
|
### AI SDK v4
|
|
91
|
+
|
|
80
92
|
```typescript
|
|
81
93
|
import { generateText } from 'ai';
|
|
82
94
|
import { claudeCode } from 'ai-sdk-provider-claude-code';
|
|
83
95
|
|
|
84
96
|
const { text } = await generateText({
|
|
85
97
|
model: claudeCode('sonnet'),
|
|
86
|
-
prompt: 'Hello, Claude!'
|
|
98
|
+
prompt: 'Hello, Claude!',
|
|
87
99
|
});
|
|
88
100
|
|
|
89
101
|
console.log(text);
|
|
90
102
|
```
|
|
91
103
|
|
|
92
|
-
## Breaking Changes
|
|
104
|
+
## Breaking Changes
|
|
105
|
+
|
|
106
|
+
### Version 2.0.0 (Claude Agent SDK Migration)
|
|
107
|
+
|
|
108
|
+
This version migrates to `@anthropic-ai/claude-agent-sdk` with **new defaults for better control**:
|
|
109
|
+
|
|
110
|
+
- **System prompt** is no longer applied by default
|
|
111
|
+
- **Filesystem settings** (CLAUDE.md, settings.json) are no longer loaded by default
|
|
112
|
+
- See [Migrating to Claude Agent SDK](#migrating-to-claude-agent-sdk) section below for migration details
|
|
113
|
+
|
|
114
|
+
### Version 1.x (AI SDK v5)
|
|
93
115
|
|
|
94
116
|
See [Breaking Changes Guide](docs/ai-sdk-v5/V5_BREAKING_CHANGES.md) for details on migrating from v0.x to v1.x.
|
|
95
117
|
|
|
96
118
|
Key changes:
|
|
119
|
+
|
|
97
120
|
- Requires AI SDK v5
|
|
98
121
|
- New streaming API pattern
|
|
99
122
|
- Updated token usage properties
|
|
@@ -110,7 +133,67 @@ Key changes:
|
|
|
110
133
|
- **[Breaking Changes](docs/ai-sdk-v5/V5_BREAKING_CHANGES.md)** - v0.x to v1.x migration guide
|
|
111
134
|
- **[Troubleshooting](docs/ai-sdk-v5/TROUBLESHOOTING.md)** - Common issues and solutions
|
|
112
135
|
- **[Examples](examples/)** - Sample scripts and patterns
|
|
113
|
-
|
|
136
|
+
- **[Tool Streaming Support](docs/ai-sdk-v5/TOOL_STREAMING_SUPPORT.md)** - Event semantics and performance notes
|
|
137
|
+
|
|
138
|
+
## Migrating to Claude Agent SDK (v2.0.0)
|
|
139
|
+
|
|
140
|
+
**Version 2.0.0** migrates from `@anthropic-ai/claude-code` to `@anthropic-ai/claude-agent-sdk`. Two defaults changed:
|
|
141
|
+
|
|
142
|
+
- System prompt is no longer applied by default.
|
|
143
|
+
- Filesystem settings (CLAUDE.md, settings.json) are not loaded by default.
|
|
144
|
+
|
|
145
|
+
Restore old behavior explicitly:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { claudeCode } from 'ai-sdk-provider-claude-code';
|
|
149
|
+
|
|
150
|
+
const model = claudeCode('sonnet', {
|
|
151
|
+
systemPrompt: { type: 'preset', preset: 'claude_code' },
|
|
152
|
+
settingSources: ['user', 'project', 'local'],
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
CLAUDE.md requires:
|
|
157
|
+
|
|
158
|
+
- `systemPrompt: { type: 'preset', preset: 'claude_code' }`
|
|
159
|
+
- `settingSources` includes `'project'`
|
|
160
|
+
|
|
161
|
+
New recommended behavior (explicit config):
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
const model = claudeCode('sonnet', {
|
|
165
|
+
systemPrompt: 'You are a helpful assistant specialized in ...',
|
|
166
|
+
settingSources: ['project'], // or omit for no filesystem settings
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
CLI install and auth are unchanged:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
npm install -g @anthropic-ai/claude-code
|
|
174
|
+
claude login
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Migrating from v1.x to v2.0.0
|
|
178
|
+
|
|
179
|
+
If you're upgrading from version 1.x:
|
|
180
|
+
|
|
181
|
+
1. **Update the package**: `npm install ai-sdk-provider-claude-code@latest`
|
|
182
|
+
2. **If you relied on default system prompt or CLAUDE.md**, add explicit configuration:
|
|
183
|
+
```ts
|
|
184
|
+
const model = claudeCode('sonnet', {
|
|
185
|
+
systemPrompt: { type: 'preset', preset: 'claude_code' },
|
|
186
|
+
settingSources: ['user', 'project', 'local'],
|
|
187
|
+
});
|
|
188
|
+
```
|
|
189
|
+
3. **If you never used CLAUDE.md or custom system prompts**, no changes needed - v2.0.0 works the same for you.
|
|
190
|
+
|
|
191
|
+
**Benefits of v2.0.0**:
|
|
192
|
+
|
|
193
|
+
- Predictable behavior across environments (no hidden filesystem settings)
|
|
194
|
+
- Better suited for CI/CD and multi-tenant applications
|
|
195
|
+
- Explicit configuration over implicit defaults
|
|
196
|
+
- Future-proof alignment with Claude Agent SDK design
|
|
114
197
|
|
|
115
198
|
## Core Features
|
|
116
199
|
|
|
@@ -120,7 +203,7 @@ Key changes:
|
|
|
120
203
|
- 🎯 Object generation with JSON schemas
|
|
121
204
|
- 🛑 AbortSignal support
|
|
122
205
|
- 🔧 Tool management (MCP servers, permissions)
|
|
123
|
-
|
|
206
|
+
- 🧩 Callbacks (hooks, canUseTool)
|
|
124
207
|
|
|
125
208
|
## Image Inputs (Streaming Only)
|
|
126
209
|
|
|
@@ -147,6 +230,7 @@ Key changes:
|
|
|
147
230
|
## Contributing
|
|
148
231
|
|
|
149
232
|
We welcome contributions, especially:
|
|
233
|
+
|
|
150
234
|
- Code structure improvements
|
|
151
235
|
- Performance optimizations
|
|
152
236
|
- Better error handling
|
package/dist/index.cjs
CHANGED
|
@@ -26,12 +26,12 @@ __export(index_exports, {
|
|
|
26
26
|
createAuthenticationError: () => createAuthenticationError,
|
|
27
27
|
createClaudeCode: () => createClaudeCode,
|
|
28
28
|
createCustomMcpServer: () => createCustomMcpServer,
|
|
29
|
-
createSdkMcpServer: () =>
|
|
29
|
+
createSdkMcpServer: () => import_claude_agent_sdk3.createSdkMcpServer,
|
|
30
30
|
createTimeoutError: () => createTimeoutError,
|
|
31
31
|
getErrorMetadata: () => getErrorMetadata,
|
|
32
32
|
isAuthenticationError: () => isAuthenticationError,
|
|
33
33
|
isTimeoutError: () => isTimeoutError,
|
|
34
|
-
tool: () =>
|
|
34
|
+
tool: () => import_claude_agent_sdk3.tool
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(index_exports);
|
|
37
37
|
|
|
@@ -326,10 +326,7 @@ Now, based on the following conversation, generate ONLY the JSON object with the
|
|
|
326
326
|
${finalPrompt}
|
|
327
327
|
|
|
328
328
|
Remember: Your ENTIRE response must be ONLY the JSON object, starting with { and ending with }`;
|
|
329
|
-
streamingParts = [
|
|
330
|
-
{ type: "text", text: finalPrompt },
|
|
331
|
-
...imagePartsInOrder
|
|
332
|
-
];
|
|
329
|
+
streamingParts = [{ type: "text", text: finalPrompt }, ...imagePartsInOrder];
|
|
333
330
|
}
|
|
334
331
|
return {
|
|
335
332
|
messagesPrompt: finalPrompt,
|
|
@@ -454,9 +451,7 @@ function createAPICallError({
|
|
|
454
451
|
data: metadata
|
|
455
452
|
});
|
|
456
453
|
}
|
|
457
|
-
function createAuthenticationError({
|
|
458
|
-
message
|
|
459
|
-
}) {
|
|
454
|
+
function createAuthenticationError({ message }) {
|
|
460
455
|
return new import_provider.LoadAPIKeyError({
|
|
461
456
|
message: message || "Authentication failed. Please ensure Claude Code SDK is properly authenticated."
|
|
462
457
|
});
|
|
@@ -480,11 +475,13 @@ function createTimeoutError({
|
|
|
480
475
|
}
|
|
481
476
|
function isAuthenticationError(error) {
|
|
482
477
|
if (error instanceof import_provider.LoadAPIKeyError) return true;
|
|
483
|
-
if (error instanceof import_provider.APICallError && error.data?.exitCode === 401)
|
|
478
|
+
if (error instanceof import_provider.APICallError && error.data?.exitCode === 401)
|
|
479
|
+
return true;
|
|
484
480
|
return false;
|
|
485
481
|
}
|
|
486
482
|
function isTimeoutError(error) {
|
|
487
|
-
if (error instanceof import_provider.APICallError && error.data?.code === "TIMEOUT")
|
|
483
|
+
if (error instanceof import_provider.APICallError && error.data?.code === "TIMEOUT")
|
|
484
|
+
return true;
|
|
488
485
|
return false;
|
|
489
486
|
}
|
|
490
487
|
function getErrorMetadata(error) {
|
|
@@ -523,6 +520,14 @@ var claudeCodeSettingsSchema = import_zod.z.object({
|
|
|
523
520
|
pathToClaudeCodeExecutable: import_zod.z.string().optional(),
|
|
524
521
|
customSystemPrompt: import_zod.z.string().optional(),
|
|
525
522
|
appendSystemPrompt: import_zod.z.string().optional(),
|
|
523
|
+
systemPrompt: import_zod.z.union([
|
|
524
|
+
import_zod.z.string(),
|
|
525
|
+
import_zod.z.object({
|
|
526
|
+
type: import_zod.z.literal("preset"),
|
|
527
|
+
preset: import_zod.z.literal("claude_code"),
|
|
528
|
+
append: import_zod.z.string().optional()
|
|
529
|
+
})
|
|
530
|
+
]).optional(),
|
|
526
531
|
maxTurns: import_zod.z.number().int().min(1).max(100).optional(),
|
|
527
532
|
maxThinkingTokens: import_zod.z.number().int().positive().max(1e5).optional(),
|
|
528
533
|
cwd: import_zod.z.string().refine(
|
|
@@ -542,48 +547,70 @@ var claudeCodeSettingsSchema = import_zod.z.object({
|
|
|
542
547
|
resume: import_zod.z.string().optional(),
|
|
543
548
|
allowedTools: import_zod.z.array(import_zod.z.string()).optional(),
|
|
544
549
|
disallowedTools: import_zod.z.array(import_zod.z.string()).optional(),
|
|
550
|
+
settingSources: import_zod.z.array(import_zod.z.enum(["user", "project", "local"])).optional(),
|
|
545
551
|
streamingInput: import_zod.z.enum(["auto", "always", "off"]).optional(),
|
|
546
552
|
// Hooks and tool-permission callback (permissive validation of shapes)
|
|
547
553
|
canUseTool: import_zod.z.any().refine((v) => v === void 0 || typeof v === "function", {
|
|
548
554
|
message: "canUseTool must be a function"
|
|
549
555
|
}).optional(),
|
|
550
|
-
hooks: import_zod.z.record(
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
556
|
+
hooks: import_zod.z.record(
|
|
557
|
+
import_zod.z.string(),
|
|
558
|
+
import_zod.z.array(
|
|
559
|
+
import_zod.z.object({
|
|
560
|
+
matcher: import_zod.z.string().optional(),
|
|
561
|
+
hooks: import_zod.z.array(import_zod.z.any()).nonempty()
|
|
562
|
+
})
|
|
563
|
+
)
|
|
564
|
+
).optional(),
|
|
565
|
+
mcpServers: import_zod.z.record(
|
|
566
|
+
import_zod.z.string(),
|
|
567
|
+
import_zod.z.union([
|
|
568
|
+
// McpStdioServerConfig
|
|
569
|
+
import_zod.z.object({
|
|
570
|
+
type: import_zod.z.literal("stdio").optional(),
|
|
571
|
+
command: import_zod.z.string(),
|
|
572
|
+
args: import_zod.z.array(import_zod.z.string()).optional(),
|
|
573
|
+
env: import_zod.z.record(import_zod.z.string(), import_zod.z.string()).optional()
|
|
574
|
+
}),
|
|
575
|
+
// McpSSEServerConfig
|
|
576
|
+
import_zod.z.object({
|
|
577
|
+
type: import_zod.z.literal("sse"),
|
|
578
|
+
url: import_zod.z.string(),
|
|
579
|
+
headers: import_zod.z.record(import_zod.z.string(), import_zod.z.string()).optional()
|
|
580
|
+
}),
|
|
581
|
+
// McpHttpServerConfig
|
|
582
|
+
import_zod.z.object({
|
|
583
|
+
type: import_zod.z.literal("http"),
|
|
584
|
+
url: import_zod.z.string(),
|
|
585
|
+
headers: import_zod.z.record(import_zod.z.string(), import_zod.z.string()).optional()
|
|
586
|
+
}),
|
|
587
|
+
// McpSdkServerConfig (in-process custom tools)
|
|
588
|
+
import_zod.z.object({
|
|
589
|
+
type: import_zod.z.literal("sdk"),
|
|
590
|
+
name: import_zod.z.string(),
|
|
591
|
+
instance: import_zod.z.any()
|
|
592
|
+
})
|
|
593
|
+
])
|
|
594
|
+
).optional(),
|
|
595
|
+
verbose: import_zod.z.boolean().optional(),
|
|
596
|
+
logger: import_zod.z.union([import_zod.z.literal(false), loggerFunctionSchema]).optional(),
|
|
597
|
+
env: import_zod.z.record(import_zod.z.string(), import_zod.z.string().optional()).optional(),
|
|
598
|
+
additionalDirectories: import_zod.z.array(import_zod.z.string()).optional(),
|
|
599
|
+
agents: import_zod.z.record(
|
|
600
|
+
import_zod.z.string(),
|
|
575
601
|
import_zod.z.object({
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
602
|
+
description: import_zod.z.string(),
|
|
603
|
+
tools: import_zod.z.array(import_zod.z.string()).optional(),
|
|
604
|
+
prompt: import_zod.z.string(),
|
|
605
|
+
model: import_zod.z.enum(["sonnet", "opus", "haiku", "inherit"]).optional()
|
|
579
606
|
})
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
607
|
+
).optional(),
|
|
608
|
+
includePartialMessages: import_zod.z.boolean().optional(),
|
|
609
|
+
fallbackModel: import_zod.z.string().optional(),
|
|
610
|
+
forkSession: import_zod.z.boolean().optional(),
|
|
611
|
+
stderr: import_zod.z.function().args(import_zod.z.string()).returns(import_zod.z.void()).optional(),
|
|
612
|
+
strictMcpConfig: import_zod.z.boolean().optional(),
|
|
613
|
+
extraArgs: import_zod.z.record(import_zod.z.string(), import_zod.z.union([import_zod.z.string(), import_zod.z.null()])).optional()
|
|
587
614
|
}).strict();
|
|
588
615
|
function validateModelId(modelId) {
|
|
589
616
|
const knownModels = ["opus", "sonnet"];
|
|
@@ -611,13 +638,19 @@ function validateSettings(settings) {
|
|
|
611
638
|
}
|
|
612
639
|
const validSettings = result.data;
|
|
613
640
|
if (validSettings.maxTurns && validSettings.maxTurns > 20) {
|
|
614
|
-
warnings.push(
|
|
641
|
+
warnings.push(
|
|
642
|
+
`High maxTurns value (${validSettings.maxTurns}) may lead to long-running conversations`
|
|
643
|
+
);
|
|
615
644
|
}
|
|
616
645
|
if (validSettings.maxThinkingTokens && validSettings.maxThinkingTokens > 5e4) {
|
|
617
|
-
warnings.push(
|
|
646
|
+
warnings.push(
|
|
647
|
+
`Very high maxThinkingTokens (${validSettings.maxThinkingTokens}) may increase response time`
|
|
648
|
+
);
|
|
618
649
|
}
|
|
619
650
|
if (validSettings.allowedTools && validSettings.disallowedTools) {
|
|
620
|
-
warnings.push(
|
|
651
|
+
warnings.push(
|
|
652
|
+
"Both allowedTools and disallowedTools are specified. Only allowedTools will be used."
|
|
653
|
+
);
|
|
621
654
|
}
|
|
622
655
|
const validateToolNames = (tools, type) => {
|
|
623
656
|
tools.forEach((tool3) => {
|
|
@@ -674,7 +707,7 @@ function getLogger(logger) {
|
|
|
674
707
|
}
|
|
675
708
|
|
|
676
709
|
// src/claude-code-language-model.ts
|
|
677
|
-
var
|
|
710
|
+
var import_claude_agent_sdk = require("@anthropic-ai/claude-agent-sdk");
|
|
678
711
|
function isAbortError(err) {
|
|
679
712
|
if (err && typeof err === "object") {
|
|
680
713
|
const e = err;
|
|
@@ -683,7 +716,7 @@ function isAbortError(err) {
|
|
|
683
716
|
}
|
|
684
717
|
return false;
|
|
685
718
|
}
|
|
686
|
-
var STREAMING_FEATURE_WARNING = "Claude
|
|
719
|
+
var STREAMING_FEATURE_WARNING = "Claude Agent SDK features (hooks/MCP/images) require streaming input. Set `streamingInput: 'always'` or provide `canUseTool` (auto streams only when canUseTool is set).";
|
|
687
720
|
function toAsyncIterablePrompt(messagesPrompt, outputStreamEnded, sessionId, contentParts) {
|
|
688
721
|
const content = contentParts && contentParts.length > 0 ? contentParts : [{ type: "text", text: messagesPrompt }];
|
|
689
722
|
const msg = {
|
|
@@ -703,8 +736,8 @@ function toAsyncIterablePrompt(messagesPrompt, outputStreamEnded, sessionId, con
|
|
|
703
736
|
};
|
|
704
737
|
}
|
|
705
738
|
var modelMap = {
|
|
706
|
-
|
|
707
|
-
|
|
739
|
+
opus: "opus",
|
|
740
|
+
sonnet: "sonnet"
|
|
708
741
|
};
|
|
709
742
|
var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
710
743
|
specificationVersion = "v2";
|
|
@@ -843,7 +876,8 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
843
876
|
if (options.topK !== void 0) unsupportedParams.push("topK");
|
|
844
877
|
if (options.presencePenalty !== void 0) unsupportedParams.push("presencePenalty");
|
|
845
878
|
if (options.frequencyPenalty !== void 0) unsupportedParams.push("frequencyPenalty");
|
|
846
|
-
if (options.stopSequences !== void 0 && options.stopSequences.length > 0)
|
|
879
|
+
if (options.stopSequences !== void 0 && options.stopSequences.length > 0)
|
|
880
|
+
unsupportedParams.push("stopSequences");
|
|
847
881
|
if (options.seed !== void 0) unsupportedParams.push("seed");
|
|
848
882
|
if (unsupportedParams.length > 0) {
|
|
849
883
|
for (const param of unsupportedParams) {
|
|
@@ -889,8 +923,6 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
889
923
|
abortController,
|
|
890
924
|
resume: this.settings.resume ?? this.sessionId,
|
|
891
925
|
pathToClaudeCodeExecutable: this.settings.pathToClaudeCodeExecutable,
|
|
892
|
-
customSystemPrompt: this.settings.customSystemPrompt,
|
|
893
|
-
appendSystemPrompt: this.settings.appendSystemPrompt,
|
|
894
926
|
maxTurns: this.settings.maxTurns,
|
|
895
927
|
maxThinkingTokens: this.settings.maxThinkingTokens,
|
|
896
928
|
cwd: this.settings.cwd,
|
|
@@ -904,6 +936,50 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
904
936
|
mcpServers: this.settings.mcpServers,
|
|
905
937
|
canUseTool: this.settings.canUseTool
|
|
906
938
|
};
|
|
939
|
+
if (this.settings.systemPrompt !== void 0) {
|
|
940
|
+
opts.systemPrompt = this.settings.systemPrompt;
|
|
941
|
+
} else if (this.settings.customSystemPrompt !== void 0) {
|
|
942
|
+
this.logger.warn(
|
|
943
|
+
"[claude-code] 'customSystemPrompt' is deprecated and will be removed in a future major release. Please use 'systemPrompt' instead (string or { type: 'preset', preset: 'claude_code', append? })."
|
|
944
|
+
);
|
|
945
|
+
opts.systemPrompt = this.settings.customSystemPrompt;
|
|
946
|
+
} else if (this.settings.appendSystemPrompt !== void 0) {
|
|
947
|
+
this.logger.warn(
|
|
948
|
+
"[claude-code] 'appendSystemPrompt' is deprecated and will be removed in a future major release. Please use 'systemPrompt: { type: 'preset', preset: 'claude_code', append: <text> }' instead."
|
|
949
|
+
);
|
|
950
|
+
opts.systemPrompt = {
|
|
951
|
+
type: "preset",
|
|
952
|
+
preset: "claude_code",
|
|
953
|
+
append: this.settings.appendSystemPrompt
|
|
954
|
+
};
|
|
955
|
+
}
|
|
956
|
+
if (this.settings.settingSources !== void 0) {
|
|
957
|
+
opts.settingSources = this.settings.settingSources;
|
|
958
|
+
}
|
|
959
|
+
if (this.settings.additionalDirectories !== void 0) {
|
|
960
|
+
opts.additionalDirectories = this.settings.additionalDirectories;
|
|
961
|
+
}
|
|
962
|
+
if (this.settings.agents !== void 0) {
|
|
963
|
+
opts.agents = this.settings.agents;
|
|
964
|
+
}
|
|
965
|
+
if (this.settings.includePartialMessages !== void 0) {
|
|
966
|
+
opts.includePartialMessages = this.settings.includePartialMessages;
|
|
967
|
+
}
|
|
968
|
+
if (this.settings.fallbackModel !== void 0) {
|
|
969
|
+
opts.fallbackModel = this.settings.fallbackModel;
|
|
970
|
+
}
|
|
971
|
+
if (this.settings.forkSession !== void 0) {
|
|
972
|
+
opts.forkSession = this.settings.forkSession;
|
|
973
|
+
}
|
|
974
|
+
if (this.settings.stderr !== void 0) {
|
|
975
|
+
opts.stderr = this.settings.stderr;
|
|
976
|
+
}
|
|
977
|
+
if (this.settings.strictMcpConfig !== void 0) {
|
|
978
|
+
opts.strictMcpConfig = this.settings.strictMcpConfig;
|
|
979
|
+
}
|
|
980
|
+
if (this.settings.extraArgs !== void 0) {
|
|
981
|
+
opts.extraArgs = this.settings.extraArgs;
|
|
982
|
+
}
|
|
907
983
|
if (this.settings.hooks) {
|
|
908
984
|
opts.hooks = this.settings.hooks;
|
|
909
985
|
}
|
|
@@ -1014,7 +1090,10 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1014
1090
|
let costUsd;
|
|
1015
1091
|
let durationMs;
|
|
1016
1092
|
let rawUsage;
|
|
1017
|
-
const warnings = this.generateAllWarnings(
|
|
1093
|
+
const warnings = this.generateAllWarnings(
|
|
1094
|
+
options,
|
|
1095
|
+
messagesPrompt
|
|
1096
|
+
);
|
|
1018
1097
|
if (messageWarnings) {
|
|
1019
1098
|
messageWarnings.forEach((warning) => {
|
|
1020
1099
|
warnings.push({
|
|
@@ -1038,7 +1117,9 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1038
1117
|
});
|
|
1039
1118
|
try {
|
|
1040
1119
|
if (this.settings.canUseTool && this.settings.permissionPromptToolName) {
|
|
1041
|
-
throw new Error(
|
|
1120
|
+
throw new Error(
|
|
1121
|
+
"canUseTool requires streamingInput mode ('auto' or 'always') and cannot be used with permissionPromptToolName (SDK constraint). Set streamingInput: 'auto' (or 'always') and remove permissionPromptToolName, or remove canUseTool."
|
|
1122
|
+
);
|
|
1042
1123
|
}
|
|
1043
1124
|
const sdkPrompt = wantsStreamInput ? toAsyncIterablePrompt(
|
|
1044
1125
|
messagesPrompt,
|
|
@@ -1046,15 +1127,13 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1046
1127
|
this.settings.resume ?? this.sessionId,
|
|
1047
1128
|
streamingContentParts
|
|
1048
1129
|
) : messagesPrompt;
|
|
1049
|
-
const response = (0,
|
|
1130
|
+
const response = (0, import_claude_agent_sdk.query)({
|
|
1050
1131
|
prompt: sdkPrompt,
|
|
1051
1132
|
options: queryOptions
|
|
1052
1133
|
});
|
|
1053
1134
|
for await (const message of response) {
|
|
1054
1135
|
if (message.type === "assistant") {
|
|
1055
|
-
text += message.message.content.map(
|
|
1056
|
-
(c) => c.type === "text" ? c.text : ""
|
|
1057
|
-
).join("");
|
|
1136
|
+
text += message.message.content.map((c) => c.type === "text" ? c.text : "").join("");
|
|
1058
1137
|
} else if (message.type === "result") {
|
|
1059
1138
|
done();
|
|
1060
1139
|
this.setSessionId(message.session_id);
|
|
@@ -1131,7 +1210,10 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1131
1210
|
options.abortSignal.addEventListener("abort", abortListener, { once: true });
|
|
1132
1211
|
}
|
|
1133
1212
|
const queryOptions = this.createQueryOptions(abortController);
|
|
1134
|
-
const warnings = this.generateAllWarnings(
|
|
1213
|
+
const warnings = this.generateAllWarnings(
|
|
1214
|
+
options,
|
|
1215
|
+
messagesPrompt
|
|
1216
|
+
);
|
|
1135
1217
|
if (messageWarnings) {
|
|
1136
1218
|
messageWarnings.forEach((warning) => {
|
|
1137
1219
|
warnings.push({
|
|
@@ -1197,7 +1279,9 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1197
1279
|
try {
|
|
1198
1280
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1199
1281
|
if (this.settings.canUseTool && this.settings.permissionPromptToolName) {
|
|
1200
|
-
throw new Error(
|
|
1282
|
+
throw new Error(
|
|
1283
|
+
"canUseTool requires streamingInput mode ('auto' or 'always') and cannot be used with permissionPromptToolName (SDK constraint). Set streamingInput: 'auto' (or 'always') and remove permissionPromptToolName, or remove canUseTool."
|
|
1284
|
+
);
|
|
1201
1285
|
}
|
|
1202
1286
|
const sdkPrompt = wantsStreamInput ? toAsyncIterablePrompt(
|
|
1203
1287
|
messagesPrompt,
|
|
@@ -1205,7 +1289,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1205
1289
|
this.settings.resume ?? this.sessionId,
|
|
1206
1290
|
streamingContentParts
|
|
1207
1291
|
) : messagesPrompt;
|
|
1208
|
-
const response = (0,
|
|
1292
|
+
const response = (0, import_claude_agent_sdk.query)({
|
|
1209
1293
|
prompt: sdkPrompt,
|
|
1210
1294
|
options: queryOptions
|
|
1211
1295
|
});
|
|
@@ -1297,7 +1381,9 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1297
1381
|
let state = toolStates.get(result.id);
|
|
1298
1382
|
const toolName = result.name ?? state?.name ?? _ClaudeCodeLanguageModel.UNKNOWN_TOOL_NAME;
|
|
1299
1383
|
if (!state) {
|
|
1300
|
-
console.warn(
|
|
1384
|
+
console.warn(
|
|
1385
|
+
`[claude-code] Received tool result for unknown tool ID: ${result.id}`
|
|
1386
|
+
);
|
|
1301
1387
|
state = {
|
|
1302
1388
|
name: toolName,
|
|
1303
1389
|
inputStarted: false,
|
|
@@ -1353,7 +1439,9 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1353
1439
|
let state = toolStates.get(error.id);
|
|
1354
1440
|
const toolName = error.name ?? state?.name ?? _ClaudeCodeLanguageModel.UNKNOWN_TOOL_NAME;
|
|
1355
1441
|
if (!state) {
|
|
1356
|
-
console.warn(
|
|
1442
|
+
console.warn(
|
|
1443
|
+
`[claude-code] Received tool error for unknown tool ID: ${error.id}`
|
|
1444
|
+
);
|
|
1357
1445
|
state = {
|
|
1358
1446
|
name: toolName,
|
|
1359
1447
|
inputStarted: true,
|
|
@@ -1394,7 +1482,9 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1394
1482
|
totalTokens: (message.usage.cache_creation_input_tokens ?? 0) + (message.usage.cache_read_input_tokens ?? 0) + (message.usage.input_tokens ?? 0) + (message.usage.output_tokens ?? 0)
|
|
1395
1483
|
};
|
|
1396
1484
|
}
|
|
1397
|
-
const finishReason = mapClaudeCodeFinishReason(
|
|
1485
|
+
const finishReason = mapClaudeCodeFinishReason(
|
|
1486
|
+
message.subtype
|
|
1487
|
+
);
|
|
1398
1488
|
this.setSessionId(message.session_id);
|
|
1399
1489
|
if (options.responseFormat?.type === "json" && accumulatedText) {
|
|
1400
1490
|
const extractedJson = this.handleJsonExtraction(accumulatedText, streamWarnings);
|
|
@@ -1427,13 +1517,17 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1427
1517
|
providerMetadata: {
|
|
1428
1518
|
"claude-code": {
|
|
1429
1519
|
sessionId: message.session_id,
|
|
1430
|
-
...message.total_cost_usd !== void 0 && {
|
|
1520
|
+
...message.total_cost_usd !== void 0 && {
|
|
1521
|
+
costUsd: message.total_cost_usd
|
|
1522
|
+
},
|
|
1431
1523
|
...message.duration_ms !== void 0 && { durationMs: message.duration_ms },
|
|
1432
1524
|
...rawUsage !== void 0 && { rawUsage },
|
|
1433
1525
|
// JSON validation warnings are collected during streaming and included
|
|
1434
1526
|
// in providerMetadata since the AI SDK's finish event doesn't support
|
|
1435
1527
|
// a top-level warnings field (unlike stream-start which was already emitted)
|
|
1436
|
-
...streamWarnings.length > 0 && {
|
|
1528
|
+
...streamWarnings.length > 0 && {
|
|
1529
|
+
warnings: warningsJson
|
|
1530
|
+
}
|
|
1437
1531
|
}
|
|
1438
1532
|
}
|
|
1439
1533
|
});
|
|
@@ -1532,9 +1626,7 @@ function createClaudeCode(options = {}) {
|
|
|
1532
1626
|
};
|
|
1533
1627
|
const provider = function(modelId, settings) {
|
|
1534
1628
|
if (new.target) {
|
|
1535
|
-
throw new Error(
|
|
1536
|
-
"The Claude Code model function cannot be called with the new keyword."
|
|
1537
|
-
);
|
|
1629
|
+
throw new Error("The Claude Code model function cannot be called with the new keyword.");
|
|
1538
1630
|
}
|
|
1539
1631
|
return createModel(modelId, settings);
|
|
1540
1632
|
};
|
|
@@ -1557,21 +1649,21 @@ function createClaudeCode(options = {}) {
|
|
|
1557
1649
|
var claudeCode = createClaudeCode();
|
|
1558
1650
|
|
|
1559
1651
|
// src/index.ts
|
|
1560
|
-
var
|
|
1652
|
+
var import_claude_agent_sdk3 = require("@anthropic-ai/claude-agent-sdk");
|
|
1561
1653
|
|
|
1562
1654
|
// src/mcp-helpers.ts
|
|
1563
|
-
var
|
|
1655
|
+
var import_claude_agent_sdk2 = require("@anthropic-ai/claude-agent-sdk");
|
|
1564
1656
|
var import_zod2 = require("zod");
|
|
1565
1657
|
function createCustomMcpServer(config) {
|
|
1566
1658
|
const defs = Object.entries(config.tools).map(
|
|
1567
|
-
([name, def]) => (0,
|
|
1659
|
+
([name, def]) => (0, import_claude_agent_sdk2.tool)(
|
|
1568
1660
|
name,
|
|
1569
1661
|
def.description,
|
|
1570
1662
|
def.inputSchema.shape,
|
|
1571
1663
|
(args, extra) => def.handler(args, extra)
|
|
1572
1664
|
)
|
|
1573
1665
|
);
|
|
1574
|
-
return (0,
|
|
1666
|
+
return (0, import_claude_agent_sdk2.createSdkMcpServer)({ name: config.name, version: config.version, tools: defs });
|
|
1575
1667
|
}
|
|
1576
1668
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1577
1669
|
0 && (module.exports = {
|