@ai-sdk/anthropic 2.0.0-beta.1 → 2.0.0-beta.10
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/CHANGELOG.md +71 -0
- package/dist/index.d.mts +19 -23
- package/dist/index.d.ts +19 -23
- package/dist/index.js +323 -270
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +96 -43
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +14 -1
- package/dist/internal/index.d.ts +14 -1
- package/dist/internal/index.js +319 -266
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +96 -43
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -21,11 +21,11 @@ import {
|
|
|
21
21
|
postJsonToApi,
|
|
22
22
|
resolve
|
|
23
23
|
} from "@ai-sdk/provider-utils";
|
|
24
|
-
import { z as z4 } from "zod";
|
|
24
|
+
import { z as z4 } from "zod/v4";
|
|
25
25
|
|
|
26
26
|
// src/anthropic-error.ts
|
|
27
27
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
28
|
-
import { z } from "zod";
|
|
28
|
+
import { z } from "zod/v4";
|
|
29
29
|
var anthropicErrorDataSchema = z.object({
|
|
30
30
|
type: z.literal("error"),
|
|
31
31
|
error: z.object({
|
|
@@ -39,7 +39,7 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
// src/anthropic-messages-options.ts
|
|
42
|
-
import { z as z2 } from "zod";
|
|
42
|
+
import { z as z2 } from "zod/v4";
|
|
43
43
|
var anthropicFilePartProviderOptions = z2.object({
|
|
44
44
|
/**
|
|
45
45
|
* Citation configuration for this document.
|
|
@@ -68,7 +68,12 @@ var anthropicProviderOptions = z2.object({
|
|
|
68
68
|
thinking: z2.object({
|
|
69
69
|
type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
|
|
70
70
|
budgetTokens: z2.number().optional()
|
|
71
|
-
}).optional()
|
|
71
|
+
}).optional(),
|
|
72
|
+
/**
|
|
73
|
+
* Whether to disable parallel function calling during tool use. Default is false.
|
|
74
|
+
* When set to true, Claude will use at most one tool per response.
|
|
75
|
+
*/
|
|
76
|
+
disableParallelToolUse: z2.boolean().optional()
|
|
72
77
|
});
|
|
73
78
|
|
|
74
79
|
// src/anthropic-prepare-tools.ts
|
|
@@ -76,6 +81,14 @@ import {
|
|
|
76
81
|
UnsupportedFunctionalityError
|
|
77
82
|
} from "@ai-sdk/provider";
|
|
78
83
|
|
|
84
|
+
// src/get-cache-control.ts
|
|
85
|
+
function getCacheControl(providerMetadata) {
|
|
86
|
+
var _a;
|
|
87
|
+
const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
88
|
+
const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
|
|
89
|
+
return cacheControlValue;
|
|
90
|
+
}
|
|
91
|
+
|
|
79
92
|
// src/tool/web-search_20250305.ts
|
|
80
93
|
import { createProviderDefinedToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";
|
|
81
94
|
import { z as z3 } from "zod/v4";
|
|
@@ -130,7 +143,8 @@ function isWebSearchTool(tool) {
|
|
|
130
143
|
}
|
|
131
144
|
function prepareTools({
|
|
132
145
|
tools,
|
|
133
|
-
toolChoice
|
|
146
|
+
toolChoice,
|
|
147
|
+
disableParallelToolUse
|
|
134
148
|
}) {
|
|
135
149
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
136
150
|
const toolWarnings = [];
|
|
@@ -146,10 +160,12 @@ function prepareTools({
|
|
|
146
160
|
}
|
|
147
161
|
switch (tool.type) {
|
|
148
162
|
case "function":
|
|
163
|
+
const cacheControl = getCacheControl(tool.providerOptions);
|
|
149
164
|
anthropicTools2.push({
|
|
150
165
|
name: tool.name,
|
|
151
166
|
description: tool.description,
|
|
152
|
-
input_schema: tool.inputSchema
|
|
167
|
+
input_schema: tool.inputSchema,
|
|
168
|
+
cache_control: cacheControl
|
|
153
169
|
});
|
|
154
170
|
break;
|
|
155
171
|
case "provider-defined":
|
|
@@ -188,6 +204,13 @@ function prepareTools({
|
|
|
188
204
|
type: "text_editor_20241022"
|
|
189
205
|
});
|
|
190
206
|
break;
|
|
207
|
+
case "anthropic.text_editor_20250429":
|
|
208
|
+
betas.add("computer-use-2025-01-24");
|
|
209
|
+
anthropicTools2.push({
|
|
210
|
+
name: "str_replace_based_edit_tool",
|
|
211
|
+
type: "text_editor_20250429"
|
|
212
|
+
});
|
|
213
|
+
break;
|
|
191
214
|
case "anthropic.bash_20250124":
|
|
192
215
|
betas.add("computer-use-2025-01-24");
|
|
193
216
|
anthropicTools2.push({
|
|
@@ -227,7 +250,7 @@ function prepareTools({
|
|
|
227
250
|
if (toolChoice == null) {
|
|
228
251
|
return {
|
|
229
252
|
tools: anthropicTools2,
|
|
230
|
-
toolChoice: void 0,
|
|
253
|
+
toolChoice: disableParallelToolUse ? { type: "auto", disable_parallel_tool_use: disableParallelToolUse } : void 0,
|
|
231
254
|
toolWarnings,
|
|
232
255
|
betas
|
|
233
256
|
};
|
|
@@ -237,14 +260,20 @@ function prepareTools({
|
|
|
237
260
|
case "auto":
|
|
238
261
|
return {
|
|
239
262
|
tools: anthropicTools2,
|
|
240
|
-
toolChoice: {
|
|
263
|
+
toolChoice: {
|
|
264
|
+
type: "auto",
|
|
265
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
266
|
+
},
|
|
241
267
|
toolWarnings,
|
|
242
268
|
betas
|
|
243
269
|
};
|
|
244
270
|
case "required":
|
|
245
271
|
return {
|
|
246
272
|
tools: anthropicTools2,
|
|
247
|
-
toolChoice: {
|
|
273
|
+
toolChoice: {
|
|
274
|
+
type: "any",
|
|
275
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
276
|
+
},
|
|
248
277
|
toolWarnings,
|
|
249
278
|
betas
|
|
250
279
|
};
|
|
@@ -253,7 +282,11 @@ function prepareTools({
|
|
|
253
282
|
case "tool":
|
|
254
283
|
return {
|
|
255
284
|
tools: anthropicTools2,
|
|
256
|
-
toolChoice: {
|
|
285
|
+
toolChoice: {
|
|
286
|
+
type: "tool",
|
|
287
|
+
name: toolChoice.toolName,
|
|
288
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
289
|
+
},
|
|
257
290
|
toolWarnings,
|
|
258
291
|
betas
|
|
259
292
|
};
|
|
@@ -297,12 +330,6 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
297
330
|
const blocks = groupIntoBlocks(prompt);
|
|
298
331
|
let system = void 0;
|
|
299
332
|
const messages = [];
|
|
300
|
-
function getCacheControl(providerMetadata) {
|
|
301
|
-
var _a2;
|
|
302
|
-
const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
303
|
-
const cacheControlValue = (_a2 = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a2 : anthropic2 == null ? void 0 : anthropic2.cache_control;
|
|
304
|
-
return cacheControlValue;
|
|
305
|
-
}
|
|
306
333
|
async function shouldEnableCitations(providerMetadata) {
|
|
307
334
|
var _a2, _b2;
|
|
308
335
|
const anthropicOptions = await parseProviderOptions({
|
|
@@ -924,8 +951,13 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
924
951
|
} = prepareTools(
|
|
925
952
|
jsonResponseTool != null ? {
|
|
926
953
|
tools: [jsonResponseTool],
|
|
927
|
-
toolChoice: { type: "tool", toolName: jsonResponseTool.name }
|
|
928
|
-
|
|
954
|
+
toolChoice: { type: "tool", toolName: jsonResponseTool.name },
|
|
955
|
+
disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
|
|
956
|
+
} : {
|
|
957
|
+
tools: tools != null ? tools : [],
|
|
958
|
+
toolChoice,
|
|
959
|
+
disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
|
|
960
|
+
}
|
|
929
961
|
);
|
|
930
962
|
return {
|
|
931
963
|
args: {
|
|
@@ -935,7 +967,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
935
967
|
},
|
|
936
968
|
warnings: [...warnings, ...toolWarnings],
|
|
937
969
|
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
|
|
938
|
-
jsonResponseTool
|
|
970
|
+
usesJsonResponseTool: jsonResponseTool != null
|
|
939
971
|
};
|
|
940
972
|
}
|
|
941
973
|
async getHeaders({
|
|
@@ -981,7 +1013,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
981
1013
|
}
|
|
982
1014
|
async doGenerate(options) {
|
|
983
1015
|
var _a, _b, _c, _d, _e;
|
|
984
|
-
const { args, warnings, betas,
|
|
1016
|
+
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
|
|
985
1017
|
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
986
1018
|
const {
|
|
987
1019
|
responseHeaders,
|
|
@@ -1002,7 +1034,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1002
1034
|
for (const part of response.content) {
|
|
1003
1035
|
switch (part.type) {
|
|
1004
1036
|
case "text": {
|
|
1005
|
-
if (
|
|
1037
|
+
if (!usesJsonResponseTool) {
|
|
1006
1038
|
content.push({ type: "text", text: part.text });
|
|
1007
1039
|
if (part.citations) {
|
|
1008
1040
|
for (const citation of part.citations) {
|
|
@@ -1044,7 +1076,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1044
1076
|
case "tool_use": {
|
|
1045
1077
|
content.push(
|
|
1046
1078
|
// when a json response tool is used, the tool call becomes the text:
|
|
1047
|
-
|
|
1079
|
+
usesJsonResponseTool ? {
|
|
1048
1080
|
type: "text",
|
|
1049
1081
|
text: JSON.stringify(part.input)
|
|
1050
1082
|
} : {
|
|
@@ -1121,7 +1153,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1121
1153
|
content,
|
|
1122
1154
|
finishReason: mapAnthropicStopReason({
|
|
1123
1155
|
finishReason: response.stop_reason,
|
|
1124
|
-
isJsonResponseFromTool:
|
|
1156
|
+
isJsonResponseFromTool: usesJsonResponseTool
|
|
1125
1157
|
}),
|
|
1126
1158
|
usage: {
|
|
1127
1159
|
inputTokens: response.usage.input_tokens,
|
|
@@ -1139,13 +1171,14 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1139
1171
|
warnings,
|
|
1140
1172
|
providerMetadata: {
|
|
1141
1173
|
anthropic: {
|
|
1174
|
+
usage: response.usage,
|
|
1142
1175
|
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
1143
1176
|
}
|
|
1144
1177
|
}
|
|
1145
1178
|
};
|
|
1146
1179
|
}
|
|
1147
1180
|
async doStream(options) {
|
|
1148
|
-
const { args, warnings, betas,
|
|
1181
|
+
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
|
|
1149
1182
|
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
1150
1183
|
const body = { ...args, stream: true };
|
|
1151
1184
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
@@ -1223,16 +1256,16 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1223
1256
|
return;
|
|
1224
1257
|
}
|
|
1225
1258
|
case "tool_use": {
|
|
1226
|
-
contentBlocks[value.index] =
|
|
1259
|
+
contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
|
|
1227
1260
|
type: "tool-call",
|
|
1228
1261
|
toolCallId: value.content_block.id,
|
|
1229
1262
|
toolName: value.content_block.name,
|
|
1230
1263
|
input: ""
|
|
1231
1264
|
};
|
|
1232
1265
|
controller.enqueue(
|
|
1233
|
-
|
|
1266
|
+
usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
|
|
1234
1267
|
type: "tool-input-start",
|
|
1235
|
-
id:
|
|
1268
|
+
id: value.content_block.id,
|
|
1236
1269
|
toolName: value.content_block.name
|
|
1237
1270
|
}
|
|
1238
1271
|
);
|
|
@@ -1331,7 +1364,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1331
1364
|
break;
|
|
1332
1365
|
}
|
|
1333
1366
|
case "tool-call":
|
|
1334
|
-
if (
|
|
1367
|
+
if (!usesJsonResponseTool) {
|
|
1335
1368
|
controller.enqueue({
|
|
1336
1369
|
type: "tool-input-end",
|
|
1337
1370
|
id: contentBlock.toolCallId
|
|
@@ -1349,7 +1382,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1349
1382
|
const deltaType = value.delta.type;
|
|
1350
1383
|
switch (deltaType) {
|
|
1351
1384
|
case "text_delta": {
|
|
1352
|
-
if (
|
|
1385
|
+
if (usesJsonResponseTool) {
|
|
1353
1386
|
return;
|
|
1354
1387
|
}
|
|
1355
1388
|
controller.enqueue({
|
|
@@ -1385,16 +1418,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1385
1418
|
case "input_json_delta": {
|
|
1386
1419
|
const contentBlock = contentBlocks[value.index];
|
|
1387
1420
|
const delta = value.delta.partial_json;
|
|
1388
|
-
if (
|
|
1389
|
-
if ((contentBlock == null ? void 0 : contentBlock.type) !== "
|
|
1421
|
+
if (usesJsonResponseTool) {
|
|
1422
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
|
|
1390
1423
|
return;
|
|
1391
1424
|
}
|
|
1392
1425
|
controller.enqueue({
|
|
1393
|
-
type: "
|
|
1394
|
-
id:
|
|
1426
|
+
type: "text-delta",
|
|
1427
|
+
id: String(value.index),
|
|
1395
1428
|
delta
|
|
1396
1429
|
});
|
|
1397
|
-
contentBlock.input += delta;
|
|
1398
1430
|
} else {
|
|
1399
1431
|
if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
|
|
1400
1432
|
return;
|
|
@@ -1404,6 +1436,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1404
1436
|
id: contentBlock.toolCallId,
|
|
1405
1437
|
delta
|
|
1406
1438
|
});
|
|
1439
|
+
contentBlock.input += delta;
|
|
1407
1440
|
}
|
|
1408
1441
|
return;
|
|
1409
1442
|
}
|
|
@@ -1430,6 +1463,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1430
1463
|
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
1431
1464
|
providerMetadata = {
|
|
1432
1465
|
anthropic: {
|
|
1466
|
+
usage: value.message.usage,
|
|
1433
1467
|
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
1434
1468
|
}
|
|
1435
1469
|
};
|
|
@@ -1445,7 +1479,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1445
1479
|
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
1446
1480
|
finishReason = mapAnthropicStopReason({
|
|
1447
1481
|
finishReason: value.delta.stop_reason,
|
|
1448
|
-
isJsonResponseFromTool:
|
|
1482
|
+
isJsonResponseFromTool: usesJsonResponseTool
|
|
1449
1483
|
});
|
|
1450
1484
|
return;
|
|
1451
1485
|
}
|
|
@@ -1505,7 +1539,7 @@ var anthropicMessagesResponseSchema = z4.object({
|
|
|
1505
1539
|
type: z4.literal("server_tool_use"),
|
|
1506
1540
|
id: z4.string(),
|
|
1507
1541
|
name: z4.string(),
|
|
1508
|
-
input: z4.record(z4.unknown()).nullish()
|
|
1542
|
+
input: z4.record(z4.string(), z4.unknown()).nullish()
|
|
1509
1543
|
}),
|
|
1510
1544
|
z4.object({
|
|
1511
1545
|
type: z4.literal("web_search_tool_result"),
|
|
@@ -1529,14 +1563,11 @@ var anthropicMessagesResponseSchema = z4.object({
|
|
|
1529
1563
|
])
|
|
1530
1564
|
),
|
|
1531
1565
|
stop_reason: z4.string().nullish(),
|
|
1532
|
-
usage: z4.
|
|
1566
|
+
usage: z4.looseObject({
|
|
1533
1567
|
input_tokens: z4.number(),
|
|
1534
1568
|
output_tokens: z4.number(),
|
|
1535
1569
|
cache_creation_input_tokens: z4.number().nullish(),
|
|
1536
|
-
cache_read_input_tokens: z4.number().nullish()
|
|
1537
|
-
server_tool_use: z4.object({
|
|
1538
|
-
web_search_requests: z4.number()
|
|
1539
|
-
}).nullish()
|
|
1570
|
+
cache_read_input_tokens: z4.number().nullish()
|
|
1540
1571
|
})
|
|
1541
1572
|
});
|
|
1542
1573
|
var anthropicMessagesChunkSchema = z4.discriminatedUnion("type", [
|
|
@@ -1545,7 +1576,7 @@ var anthropicMessagesChunkSchema = z4.discriminatedUnion("type", [
|
|
|
1545
1576
|
message: z4.object({
|
|
1546
1577
|
id: z4.string().nullish(),
|
|
1547
1578
|
model: z4.string().nullish(),
|
|
1548
|
-
usage: z4.
|
|
1579
|
+
usage: z4.looseObject({
|
|
1549
1580
|
input_tokens: z4.number(),
|
|
1550
1581
|
output_tokens: z4.number(),
|
|
1551
1582
|
cache_creation_input_tokens: z4.number().nullish(),
|
|
@@ -1578,7 +1609,7 @@ var anthropicMessagesChunkSchema = z4.discriminatedUnion("type", [
|
|
|
1578
1609
|
type: z4.literal("server_tool_use"),
|
|
1579
1610
|
id: z4.string(),
|
|
1580
1611
|
name: z4.string(),
|
|
1581
|
-
input: z4.record(z4.unknown()).nullish()
|
|
1612
|
+
input: z4.record(z4.string(), z4.unknown()).nullish()
|
|
1582
1613
|
}),
|
|
1583
1614
|
z4.object({
|
|
1584
1615
|
type: z4.literal("web_search_tool_result"),
|
|
@@ -1771,6 +1802,23 @@ var textEditor_20250124 = createProviderDefinedToolFactory6({
|
|
|
1771
1802
|
})
|
|
1772
1803
|
});
|
|
1773
1804
|
|
|
1805
|
+
// src/tool/text-editor_20250429.ts
|
|
1806
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory7 } from "@ai-sdk/provider-utils";
|
|
1807
|
+
import { z as z11 } from "zod/v4";
|
|
1808
|
+
var textEditor_20250429 = createProviderDefinedToolFactory7({
|
|
1809
|
+
id: "anthropic.text_editor_20250429",
|
|
1810
|
+
name: "str_replace_based_edit_tool",
|
|
1811
|
+
inputSchema: z11.object({
|
|
1812
|
+
command: z11.enum(["view", "create", "str_replace", "insert"]),
|
|
1813
|
+
path: z11.string(),
|
|
1814
|
+
file_text: z11.string().optional(),
|
|
1815
|
+
insert_line: z11.number().int().optional(),
|
|
1816
|
+
new_str: z11.string().optional(),
|
|
1817
|
+
old_str: z11.string().optional(),
|
|
1818
|
+
view_range: z11.array(z11.number().int()).optional()
|
|
1819
|
+
})
|
|
1820
|
+
});
|
|
1821
|
+
|
|
1774
1822
|
// src/anthropic-tools.ts
|
|
1775
1823
|
var anthropicTools = {
|
|
1776
1824
|
/**
|
|
@@ -1797,6 +1845,11 @@ var anthropicTools = {
|
|
|
1797
1845
|
* Creates a tool for editing text. Must have name "str_replace_editor".
|
|
1798
1846
|
*/
|
|
1799
1847
|
textEditor_20250124,
|
|
1848
|
+
/**
|
|
1849
|
+
* Creates a tool for editing text. Must have name "str_replace_based_edit_tool".
|
|
1850
|
+
* Note: This version does not support the "undo_edit" command.
|
|
1851
|
+
*/
|
|
1852
|
+
textEditor_20250429,
|
|
1800
1853
|
/**
|
|
1801
1854
|
* Creates a tool for executing actions on a computer. Must have name "computer".
|
|
1802
1855
|
*
|