ai-sdk-provider-claude-code 3.4.2 → 3.4.4
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 +28 -23
- package/dist/index.cjs +95 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -3
- package/dist/index.d.ts +33 -3
- package/dist/index.js +95 -27
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
<a href="https://www.npmjs.com/package/ai-sdk-provider-claude-code"><img src="https://img.shields.io/npm/l/ai-sdk-provider-claude-code?color=00A79E" alt="License: MIT" /></a>
|
|
8
8
|
</p>
|
|
9
9
|
|
|
10
|
-
# AI SDK Provider for Claude
|
|
10
|
+
# AI SDK Provider for Claude Agent SDK
|
|
11
11
|
|
|
12
12
|
> **Latest Release**: Version 3.x supports AI SDK v6 stable with the Claude Agent SDK. For AI SDK v5 support, use the `ai-sdk-v5` tag.
|
|
13
13
|
|
|
@@ -58,9 +58,11 @@ npm install ai-sdk-provider-claude-code ai zod@^4.0.0
|
|
|
58
58
|
|
|
59
59
|
### 1. Install and authenticate the CLI
|
|
60
60
|
|
|
61
|
+
See the [official docs](https://docs.anthropic.com/en/docs/claude-code/overview) for platform-specific options.
|
|
62
|
+
|
|
61
63
|
```bash
|
|
62
|
-
|
|
63
|
-
claude login
|
|
64
|
+
curl -fsSL https://claude.ai/install.sh | bash
|
|
65
|
+
claude auth login
|
|
64
66
|
```
|
|
65
67
|
|
|
66
68
|
### 2. Add the provider
|
|
@@ -80,7 +82,7 @@ npm install ai-sdk-provider-claude-code@ai-sdk-v4 ai@^4.3.16
|
|
|
80
82
|
|
|
81
83
|
**This is an unofficial community provider** and is not affiliated with or endorsed by Anthropic or Vercel. By using this provider:
|
|
82
84
|
|
|
83
|
-
- You understand that your data will be sent to Anthropic's servers through the Claude
|
|
85
|
+
- You understand that your data will be sent to Anthropic's servers through the Claude Agent SDK
|
|
84
86
|
- You agree to comply with [Anthropic's Terms of Service](https://www.anthropic.com/legal/consumer-terms)
|
|
85
87
|
- You acknowledge this software is provided "as is" without warranties of any kind
|
|
86
88
|
|
|
@@ -198,8 +200,8 @@ const model = claudeCode('sonnet', {
|
|
|
198
200
|
CLI install and auth are unchanged:
|
|
199
201
|
|
|
200
202
|
```bash
|
|
201
|
-
|
|
202
|
-
claude login
|
|
203
|
+
curl -fsSL https://claude.ai/install.sh | bash
|
|
204
|
+
claude auth login
|
|
203
205
|
```
|
|
204
206
|
|
|
205
207
|
### Migrating from v1.x to v2.0.0
|
|
@@ -271,22 +273,25 @@ console.log(result.object); // Matches the schema above
|
|
|
271
273
|
|
|
272
274
|
This provider exposes Agent SDK options directly. Key options include:
|
|
273
275
|
|
|
274
|
-
| Option | Description
|
|
275
|
-
| --------------------------------- |
|
|
276
|
-
| `betas` | Enable beta features (e.g., `['context-1m-2025-08-07']`)
|
|
277
|
-
| `sandbox` | Configure sandbox behavior (`{ enabled: true }`)
|
|
278
|
-
| `plugins` | Load custom plugins from local paths
|
|
279
|
-
| `resumeSessionAt` | Resume session at a specific message UUID
|
|
280
|
-
| `enableFileCheckpointing` | Enable file rewind support
|
|
281
|
-
| `maxBudgetUsd` | Maximum budget in USD for the query
|
|
282
|
-
| `tools` | Tool configuration (array of names or preset)
|
|
283
|
-
| `allowDangerouslySkipPermissions` | Allow bypassing permissions
|
|
284
|
-
| `persistSession` | When `false`, disables session persistence to disk (v3.2.0+)
|
|
285
|
-
| `spawnClaudeCodeProcess` | Custom process spawner for VMs/containers (v3.2.0+)
|
|
286
|
-
| `permissionMode` | Permission mode: `'default'`, `'acceptEdits'`, `'bypassPermissions'`, `'plan'`, `'delegate'`, `'dontAsk'`
|
|
287
|
-
| `sessionId` | Use a specific session ID for deterministic tracking and correlation (v3.4.0+)
|
|
288
|
-
| `debug` | Enable programmatic debug logging from the SDK (v3.4.0+)
|
|
289
|
-
| `debugFile` | Path to a file for SDK debug log output (v3.4.0+)
|
|
276
|
+
| Option | Description |
|
|
277
|
+
| --------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
278
|
+
| `betas` | Enable beta features (e.g., `['context-1m-2025-08-07']`) |
|
|
279
|
+
| `sandbox` | Configure sandbox behavior (`{ enabled: true }`) |
|
|
280
|
+
| `plugins` | Load custom plugins from local paths |
|
|
281
|
+
| `resumeSessionAt` | Resume session at a specific message UUID |
|
|
282
|
+
| `enableFileCheckpointing` | Enable file rewind support |
|
|
283
|
+
| `maxBudgetUsd` | Maximum budget in USD for the query |
|
|
284
|
+
| `tools` | Tool configuration (array of names or preset) |
|
|
285
|
+
| `allowDangerouslySkipPermissions` | Allow bypassing permissions |
|
|
286
|
+
| `persistSession` | When `false`, disables session persistence to disk (v3.2.0+) |
|
|
287
|
+
| `spawnClaudeCodeProcess` | Custom process spawner for VMs/containers (v3.2.0+) |
|
|
288
|
+
| `permissionMode` | Permission mode: `'default'`, `'acceptEdits'`, `'bypassPermissions'`, `'plan'`, `'delegate'`, `'dontAsk'` |
|
|
289
|
+
| `sessionId` | Use a specific session ID for deterministic tracking and correlation (v3.4.0+) |
|
|
290
|
+
| `debug` | Enable programmatic debug logging from the SDK (v3.4.0+) |
|
|
291
|
+
| `debugFile` | Path to a file for SDK debug log output (v3.4.0+) |
|
|
292
|
+
| `effort` | Effort level: `'low'`, `'medium'`, `'high'`, or `'max'` |
|
|
293
|
+
| `thinking` | Thinking config: `{ type: 'adaptive' }`, `{ type: 'enabled', budgetTokens?: number }`, or `{ type: 'disabled' }` |
|
|
294
|
+
| `promptSuggestions` | Enable prompt suggestions (`boolean`) |
|
|
290
295
|
|
|
291
296
|
**Agent definitions** (`agents`) now support additional fields (v3.2.0+):
|
|
292
297
|
|
|
@@ -434,7 +439,7 @@ This provider handles Anthropic `content_block_*` stream events directly for mor
|
|
|
434
439
|
|
|
435
440
|
- **Tool input streaming** — `tool-input-delta` streams arguments incrementally; `tool-call` emits when the tool input block completes (before results), enabling “running” state in UIs.
|
|
436
441
|
- **Text streaming** — `text-start/delta/end` emitted from content blocks with proper lifecycle management.
|
|
437
|
-
- **Extended thinking** — `reasoning-start/delta/end` emitted from `thinking` content blocks (availability depends on model and request).
|
|
442
|
+
- **Extended thinking** — `reasoning-start/delta/end` emitted from `thinking` content blocks in streaming mode; `reasoning` content parts returned from `doGenerate` in non-streaming mode (availability depends on model and request).
|
|
438
443
|
|
|
439
444
|
For subagent parent/child tracking, see **Subagent Hierarchy Tracking** in this README.
|
|
440
445
|
|
package/dist/index.cjs
CHANGED
|
@@ -465,6 +465,16 @@ var claudeCodeSettingsSchema = import_zod.z.object({
|
|
|
465
465
|
]).optional(),
|
|
466
466
|
maxTurns: import_zod.z.number().int().min(1).max(100).optional(),
|
|
467
467
|
maxThinkingTokens: import_zod.z.number().int().positive().max(1e5).optional(),
|
|
468
|
+
thinking: import_zod.z.union([
|
|
469
|
+
import_zod.z.object({ type: import_zod.z.literal("adaptive") }).strict(),
|
|
470
|
+
import_zod.z.object({
|
|
471
|
+
type: import_zod.z.literal("enabled"),
|
|
472
|
+
budgetTokens: import_zod.z.number().int().positive().optional()
|
|
473
|
+
}).strict(),
|
|
474
|
+
import_zod.z.object({ type: import_zod.z.literal("disabled") }).strict()
|
|
475
|
+
]).optional(),
|
|
476
|
+
effort: import_zod.z.enum(["low", "medium", "high", "max"]).optional(),
|
|
477
|
+
promptSuggestions: import_zod.z.boolean().optional(),
|
|
468
478
|
cwd: import_zod.z.string().refine(
|
|
469
479
|
(val) => {
|
|
470
480
|
if (typeof process === "undefined" || !process.versions?.node) {
|
|
@@ -791,6 +801,22 @@ function getBaseProcessEnv() {
|
|
|
791
801
|
}
|
|
792
802
|
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).";
|
|
793
803
|
var SDK_OPTIONS_BLOCKLIST = /* @__PURE__ */ new Set(["model", "abortController", "prompt", "outputFormat"]);
|
|
804
|
+
function isContentBlock(item) {
|
|
805
|
+
return typeof item === "object" && item !== null && "type" in item;
|
|
806
|
+
}
|
|
807
|
+
function filterContentBlocks(content, type) {
|
|
808
|
+
if (!Array.isArray(content)) return [];
|
|
809
|
+
const blocks = content.filter(
|
|
810
|
+
(item) => isContentBlock(item) && item.type === type
|
|
811
|
+
);
|
|
812
|
+
const mismatch = blocks.find((b) => b.type !== type);
|
|
813
|
+
if (mismatch) {
|
|
814
|
+
throw new Error(
|
|
815
|
+
`filterContentBlocks: block type '${mismatch.type}' passed filter for '${type}'`
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
return blocks;
|
|
819
|
+
}
|
|
794
820
|
function createEmptyUsage() {
|
|
795
821
|
return {
|
|
796
822
|
inputTokens: {
|
|
@@ -1046,14 +1072,29 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1046
1072
|
getEffectiveResume(sdkOptions) {
|
|
1047
1073
|
return sdkOptions?.resume ?? this.settings.resume ?? this.sessionId;
|
|
1048
1074
|
}
|
|
1049
|
-
|
|
1050
|
-
if (!Array.isArray(content)) {
|
|
1051
|
-
|
|
1075
|
+
extractTextAndThinking(content) {
|
|
1076
|
+
if (!Array.isArray(content)) return { text: "", thinking: [] };
|
|
1077
|
+
let text = "";
|
|
1078
|
+
const thinking = [];
|
|
1079
|
+
for (const part of content) {
|
|
1080
|
+
if (!isContentBlock(part)) continue;
|
|
1081
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
1082
|
+
text += part.text;
|
|
1083
|
+
} else if (part.type === "thinking" && typeof part.thinking === "string") {
|
|
1084
|
+
thinking.push(part.thinking);
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
if (text.length > 0 && typeof text !== "string") {
|
|
1088
|
+
throw new Error("extractTextAndThinking: accumulated text must be a string");
|
|
1089
|
+
}
|
|
1090
|
+
if (thinking.some((t) => typeof t !== "string")) {
|
|
1091
|
+
throw new Error("extractTextAndThinking: all thinking entries must be strings");
|
|
1052
1092
|
}
|
|
1053
|
-
return
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1093
|
+
return { text, thinking };
|
|
1094
|
+
}
|
|
1095
|
+
extractToolUses(content) {
|
|
1096
|
+
return filterContentBlocks(content, "tool_use").map((block) => {
|
|
1097
|
+
const { id, name, input, parent_tool_use_id } = block;
|
|
1057
1098
|
return {
|
|
1058
1099
|
id: typeof id === "string" && id.length > 0 ? id : (0, import_provider_utils.generateId)(),
|
|
1059
1100
|
name: typeof name === "string" && name.length > 0 ? name : _ClaudeCodeLanguageModel.UNKNOWN_TOOL_NAME,
|
|
@@ -1063,13 +1104,8 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1063
1104
|
});
|
|
1064
1105
|
}
|
|
1065
1106
|
extractToolResults(content) {
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
}
|
|
1069
|
-
return content.filter(
|
|
1070
|
-
(item) => typeof item === "object" && item !== null && "type" in item && item.type === "tool_result"
|
|
1071
|
-
).map((item) => {
|
|
1072
|
-
const { tool_use_id, content: content2, is_error, name } = item;
|
|
1107
|
+
return filterContentBlocks(content, "tool_result").map((block) => {
|
|
1108
|
+
const { tool_use_id, content: content2, is_error, name } = block;
|
|
1073
1109
|
return {
|
|
1074
1110
|
id: typeof tool_use_id === "string" && tool_use_id.length > 0 ? tool_use_id : (0, import_provider_utils.generateId)(),
|
|
1075
1111
|
name: typeof name === "string" && name.length > 0 ? name : void 0,
|
|
@@ -1079,13 +1115,8 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1079
1115
|
});
|
|
1080
1116
|
}
|
|
1081
1117
|
extractToolErrors(content) {
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
}
|
|
1085
|
-
return content.filter(
|
|
1086
|
-
(item) => typeof item === "object" && item !== null && "type" in item && item.type === "tool_error"
|
|
1087
|
-
).map((item) => {
|
|
1088
|
-
const { tool_use_id, error, name } = item;
|
|
1118
|
+
return filterContentBlocks(content, "tool_error").map((block) => {
|
|
1119
|
+
const { tool_use_id, error, name } = block;
|
|
1089
1120
|
return {
|
|
1090
1121
|
id: typeof tool_use_id === "string" && tool_use_id.length > 0 ? tool_use_id : (0, import_provider_utils.generateId)(),
|
|
1091
1122
|
name: typeof name === "string" && name.length > 0 ? name : void 0,
|
|
@@ -1209,6 +1240,9 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1209
1240
|
pathToClaudeCodeExecutable: this.settings.pathToClaudeCodeExecutable,
|
|
1210
1241
|
maxTurns: this.settings.maxTurns,
|
|
1211
1242
|
maxThinkingTokens: this.settings.maxThinkingTokens,
|
|
1243
|
+
thinking: this.settings.thinking,
|
|
1244
|
+
effort: this.settings.effort,
|
|
1245
|
+
promptSuggestions: this.settings.promptSuggestions,
|
|
1212
1246
|
cwd: this.settings.cwd,
|
|
1213
1247
|
executable: this.settings.executable,
|
|
1214
1248
|
executableArgs: this.settings.executableArgs,
|
|
@@ -1332,6 +1366,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1332
1366
|
"auth failed",
|
|
1333
1367
|
"please login",
|
|
1334
1368
|
"claude login",
|
|
1369
|
+
"claude auth login",
|
|
1335
1370
|
"/login",
|
|
1336
1371
|
// CLI returns "Please run /login"
|
|
1337
1372
|
"invalid api key"
|
|
@@ -1425,6 +1460,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1425
1460
|
effectiveResume
|
|
1426
1461
|
);
|
|
1427
1462
|
let text = "";
|
|
1463
|
+
const thinkingTraces = [];
|
|
1428
1464
|
let structuredOutput;
|
|
1429
1465
|
let usage = createEmptyUsage();
|
|
1430
1466
|
let finishReason = { unified: "stop", raw: void 0 };
|
|
@@ -1480,7 +1516,11 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1480
1516
|
for await (const message of response) {
|
|
1481
1517
|
this.logger.debug(`[claude-code] Received message type: ${message.type}`);
|
|
1482
1518
|
if (message.type === "assistant") {
|
|
1483
|
-
|
|
1519
|
+
const { text: messageText, thinking: messageThinking } = this.extractTextAndThinking(
|
|
1520
|
+
message.message.content
|
|
1521
|
+
);
|
|
1522
|
+
text += messageText;
|
|
1523
|
+
thinkingTraces.push(...messageThinking);
|
|
1484
1524
|
} else if (message.type === "result") {
|
|
1485
1525
|
done();
|
|
1486
1526
|
this.setSessionId(message.session_id);
|
|
@@ -1547,7 +1587,13 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1547
1587
|
}
|
|
1548
1588
|
const finalText = structuredOutput !== void 0 ? JSON.stringify(structuredOutput) : text;
|
|
1549
1589
|
return {
|
|
1550
|
-
content: [
|
|
1590
|
+
content: [
|
|
1591
|
+
...thinkingTraces.map((trace) => ({
|
|
1592
|
+
type: "reasoning",
|
|
1593
|
+
text: trace
|
|
1594
|
+
})),
|
|
1595
|
+
{ type: "text", text: finalText }
|
|
1596
|
+
],
|
|
1551
1597
|
usage,
|
|
1552
1598
|
finishReason,
|
|
1553
1599
|
warnings,
|
|
@@ -1565,7 +1611,8 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1565
1611
|
...costUsd !== void 0 && { costUsd },
|
|
1566
1612
|
...durationMs !== void 0 && { durationMs },
|
|
1567
1613
|
...modelUsage !== void 0 && { modelUsage },
|
|
1568
|
-
...wasTruncated && { truncated: true }
|
|
1614
|
+
...wasTruncated && { truncated: true },
|
|
1615
|
+
...thinkingTraces.length > 0 && { thinkingTraces }
|
|
1569
1616
|
}
|
|
1570
1617
|
}
|
|
1571
1618
|
};
|
|
@@ -1784,11 +1831,18 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1784
1831
|
const toolName = typeof toolBlock.name === "string" && toolBlock.name.length > 0 ? toolBlock.name : _ClaudeCodeLanguageModel.UNKNOWN_TOOL_NAME;
|
|
1785
1832
|
hasReceivedStreamEvents = true;
|
|
1786
1833
|
if (textPartId) {
|
|
1834
|
+
const closedTextId = textPartId;
|
|
1787
1835
|
controller.enqueue({
|
|
1788
1836
|
type: "text-end",
|
|
1789
|
-
id:
|
|
1837
|
+
id: closedTextId
|
|
1790
1838
|
});
|
|
1791
1839
|
textPartId = void 0;
|
|
1840
|
+
for (const [idx, blockTextId] of textBlocksByIndex) {
|
|
1841
|
+
if (blockTextId === closedTextId) {
|
|
1842
|
+
textBlocksByIndex.delete(idx);
|
|
1843
|
+
break;
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1792
1846
|
}
|
|
1793
1847
|
toolBlocksByIndex.set(blockIndex, toolId);
|
|
1794
1848
|
toolInputAccumulators.set(toolId, "");
|
|
@@ -1847,11 +1901,18 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1847
1901
|
const blockIndex = "index" in event ? event.index : -1;
|
|
1848
1902
|
hasReceivedStreamEvents = true;
|
|
1849
1903
|
if (textPartId) {
|
|
1904
|
+
const closedTextId = textPartId;
|
|
1850
1905
|
controller.enqueue({
|
|
1851
1906
|
type: "text-end",
|
|
1852
|
-
id:
|
|
1907
|
+
id: closedTextId
|
|
1853
1908
|
});
|
|
1854
1909
|
textPartId = void 0;
|
|
1910
|
+
for (const [idx, blockTextId] of textBlocksByIndex) {
|
|
1911
|
+
if (blockTextId === closedTextId) {
|
|
1912
|
+
textBlocksByIndex.delete(idx);
|
|
1913
|
+
break;
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1855
1916
|
}
|
|
1856
1917
|
const reasoningPartId = (0, import_provider_utils.generateId)();
|
|
1857
1918
|
reasoningBlocksByIndex.set(blockIndex, reasoningPartId);
|
|
@@ -1962,11 +2023,18 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1962
2023
|
const content = message.message.content;
|
|
1963
2024
|
const tools = this.extractToolUses(content);
|
|
1964
2025
|
if (textPartId && tools.length > 0) {
|
|
2026
|
+
const closedTextId = textPartId;
|
|
1965
2027
|
controller.enqueue({
|
|
1966
2028
|
type: "text-end",
|
|
1967
|
-
id:
|
|
2029
|
+
id: closedTextId
|
|
1968
2030
|
});
|
|
1969
2031
|
textPartId = void 0;
|
|
2032
|
+
for (const [idx, blockTextId] of textBlocksByIndex) {
|
|
2033
|
+
if (blockTextId === closedTextId) {
|
|
2034
|
+
textBlocksByIndex.delete(idx);
|
|
2035
|
+
break;
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
1970
2038
|
}
|
|
1971
2039
|
for (const tool3 of tools) {
|
|
1972
2040
|
const toolId = tool3.id;
|