@ai-sdk/anthropic 2.0.0-beta.1 → 2.0.0-beta.11

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.
@@ -11,11 +11,11 @@ import {
11
11
  postJsonToApi,
12
12
  resolve
13
13
  } from "@ai-sdk/provider-utils";
14
- import { z as z4 } from "zod";
14
+ import { z as z4 } from "zod/v4";
15
15
 
16
16
  // src/anthropic-error.ts
17
17
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
18
- import { z } from "zod";
18
+ import { z } from "zod/v4";
19
19
  var anthropicErrorDataSchema = z.object({
20
20
  type: z.literal("error"),
21
21
  error: z.object({
@@ -29,7 +29,7 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
29
29
  });
30
30
 
31
31
  // src/anthropic-messages-options.ts
32
- import { z as z2 } from "zod";
32
+ import { z as z2 } from "zod/v4";
33
33
  var anthropicFilePartProviderOptions = z2.object({
34
34
  /**
35
35
  * Citation configuration for this document.
@@ -58,7 +58,12 @@ var anthropicProviderOptions = z2.object({
58
58
  thinking: z2.object({
59
59
  type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
60
60
  budgetTokens: z2.number().optional()
61
- }).optional()
61
+ }).optional(),
62
+ /**
63
+ * Whether to disable parallel function calling during tool use. Default is false.
64
+ * When set to true, Claude will use at most one tool per response.
65
+ */
66
+ disableParallelToolUse: z2.boolean().optional()
62
67
  });
63
68
 
64
69
  // src/anthropic-prepare-tools.ts
@@ -66,6 +71,14 @@ import {
66
71
  UnsupportedFunctionalityError
67
72
  } from "@ai-sdk/provider";
68
73
 
74
+ // src/get-cache-control.ts
75
+ function getCacheControl(providerMetadata) {
76
+ var _a;
77
+ const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
78
+ const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
79
+ return cacheControlValue;
80
+ }
81
+
69
82
  // src/tool/web-search_20250305.ts
70
83
  import { createProviderDefinedToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";
71
84
  import { z as z3 } from "zod/v4";
@@ -120,7 +133,8 @@ function isWebSearchTool(tool) {
120
133
  }
121
134
  function prepareTools({
122
135
  tools,
123
- toolChoice
136
+ toolChoice,
137
+ disableParallelToolUse
124
138
  }) {
125
139
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
126
140
  const toolWarnings = [];
@@ -136,10 +150,12 @@ function prepareTools({
136
150
  }
137
151
  switch (tool.type) {
138
152
  case "function":
153
+ const cacheControl = getCacheControl(tool.providerOptions);
139
154
  anthropicTools2.push({
140
155
  name: tool.name,
141
156
  description: tool.description,
142
- input_schema: tool.inputSchema
157
+ input_schema: tool.inputSchema,
158
+ cache_control: cacheControl
143
159
  });
144
160
  break;
145
161
  case "provider-defined":
@@ -178,6 +194,13 @@ function prepareTools({
178
194
  type: "text_editor_20241022"
179
195
  });
180
196
  break;
197
+ case "anthropic.text_editor_20250429":
198
+ betas.add("computer-use-2025-01-24");
199
+ anthropicTools2.push({
200
+ name: "str_replace_based_edit_tool",
201
+ type: "text_editor_20250429"
202
+ });
203
+ break;
181
204
  case "anthropic.bash_20250124":
182
205
  betas.add("computer-use-2025-01-24");
183
206
  anthropicTools2.push({
@@ -217,7 +240,7 @@ function prepareTools({
217
240
  if (toolChoice == null) {
218
241
  return {
219
242
  tools: anthropicTools2,
220
- toolChoice: void 0,
243
+ toolChoice: disableParallelToolUse ? { type: "auto", disable_parallel_tool_use: disableParallelToolUse } : void 0,
221
244
  toolWarnings,
222
245
  betas
223
246
  };
@@ -227,14 +250,20 @@ function prepareTools({
227
250
  case "auto":
228
251
  return {
229
252
  tools: anthropicTools2,
230
- toolChoice: { type: "auto" },
253
+ toolChoice: {
254
+ type: "auto",
255
+ disable_parallel_tool_use: disableParallelToolUse
256
+ },
231
257
  toolWarnings,
232
258
  betas
233
259
  };
234
260
  case "required":
235
261
  return {
236
262
  tools: anthropicTools2,
237
- toolChoice: { type: "any" },
263
+ toolChoice: {
264
+ type: "any",
265
+ disable_parallel_tool_use: disableParallelToolUse
266
+ },
238
267
  toolWarnings,
239
268
  betas
240
269
  };
@@ -243,7 +272,11 @@ function prepareTools({
243
272
  case "tool":
244
273
  return {
245
274
  tools: anthropicTools2,
246
- toolChoice: { type: "tool", name: toolChoice.toolName },
275
+ toolChoice: {
276
+ type: "tool",
277
+ name: toolChoice.toolName,
278
+ disable_parallel_tool_use: disableParallelToolUse
279
+ },
247
280
  toolWarnings,
248
281
  betas
249
282
  };
@@ -287,12 +320,6 @@ async function convertToAnthropicMessagesPrompt({
287
320
  const blocks = groupIntoBlocks(prompt);
288
321
  let system = void 0;
289
322
  const messages = [];
290
- function getCacheControl(providerMetadata) {
291
- var _a2;
292
- const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
293
- const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
294
- return cacheControlValue;
295
- }
296
323
  async function shouldEnableCitations(providerMetadata) {
297
324
  var _a2, _b2;
298
325
  const anthropicOptions = await parseProviderOptions({
@@ -914,8 +941,13 @@ var AnthropicMessagesLanguageModel = class {
914
941
  } = prepareTools(
915
942
  jsonResponseTool != null ? {
916
943
  tools: [jsonResponseTool],
917
- toolChoice: { type: "tool", toolName: jsonResponseTool.name }
918
- } : { tools: tools != null ? tools : [], toolChoice }
944
+ toolChoice: { type: "tool", toolName: jsonResponseTool.name },
945
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
946
+ } : {
947
+ tools: tools != null ? tools : [],
948
+ toolChoice,
949
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
950
+ }
919
951
  );
920
952
  return {
921
953
  args: {
@@ -925,7 +957,7 @@ var AnthropicMessagesLanguageModel = class {
925
957
  },
926
958
  warnings: [...warnings, ...toolWarnings],
927
959
  betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
928
- jsonResponseTool
960
+ usesJsonResponseTool: jsonResponseTool != null
929
961
  };
930
962
  }
931
963
  async getHeaders({
@@ -971,7 +1003,7 @@ var AnthropicMessagesLanguageModel = class {
971
1003
  }
972
1004
  async doGenerate(options) {
973
1005
  var _a, _b, _c, _d, _e;
974
- const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
1006
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
975
1007
  const citationDocuments = this.extractCitationDocuments(options.prompt);
976
1008
  const {
977
1009
  responseHeaders,
@@ -992,7 +1024,7 @@ var AnthropicMessagesLanguageModel = class {
992
1024
  for (const part of response.content) {
993
1025
  switch (part.type) {
994
1026
  case "text": {
995
- if (jsonResponseTool == null) {
1027
+ if (!usesJsonResponseTool) {
996
1028
  content.push({ type: "text", text: part.text });
997
1029
  if (part.citations) {
998
1030
  for (const citation of part.citations) {
@@ -1034,7 +1066,7 @@ var AnthropicMessagesLanguageModel = class {
1034
1066
  case "tool_use": {
1035
1067
  content.push(
1036
1068
  // when a json response tool is used, the tool call becomes the text:
1037
- jsonResponseTool != null ? {
1069
+ usesJsonResponseTool ? {
1038
1070
  type: "text",
1039
1071
  text: JSON.stringify(part.input)
1040
1072
  } : {
@@ -1111,7 +1143,7 @@ var AnthropicMessagesLanguageModel = class {
1111
1143
  content,
1112
1144
  finishReason: mapAnthropicStopReason({
1113
1145
  finishReason: response.stop_reason,
1114
- isJsonResponseFromTool: jsonResponseTool != null
1146
+ isJsonResponseFromTool: usesJsonResponseTool
1115
1147
  }),
1116
1148
  usage: {
1117
1149
  inputTokens: response.usage.input_tokens,
@@ -1129,13 +1161,14 @@ var AnthropicMessagesLanguageModel = class {
1129
1161
  warnings,
1130
1162
  providerMetadata: {
1131
1163
  anthropic: {
1164
+ usage: response.usage,
1132
1165
  cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
1133
1166
  }
1134
1167
  }
1135
1168
  };
1136
1169
  }
1137
1170
  async doStream(options) {
1138
- const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
1171
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
1139
1172
  const citationDocuments = this.extractCitationDocuments(options.prompt);
1140
1173
  const body = { ...args, stream: true };
1141
1174
  const { responseHeaders, value: response } = await postJsonToApi({
@@ -1213,16 +1246,16 @@ var AnthropicMessagesLanguageModel = class {
1213
1246
  return;
1214
1247
  }
1215
1248
  case "tool_use": {
1216
- contentBlocks[value.index] = jsonResponseTool != null ? { type: "text" } : {
1249
+ contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
1217
1250
  type: "tool-call",
1218
1251
  toolCallId: value.content_block.id,
1219
1252
  toolName: value.content_block.name,
1220
1253
  input: ""
1221
1254
  };
1222
1255
  controller.enqueue(
1223
- jsonResponseTool != null ? { type: "text-start", id: String(value.index) } : {
1256
+ usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
1224
1257
  type: "tool-input-start",
1225
- id: String(value.index),
1258
+ id: value.content_block.id,
1226
1259
  toolName: value.content_block.name
1227
1260
  }
1228
1261
  );
@@ -1321,7 +1354,7 @@ var AnthropicMessagesLanguageModel = class {
1321
1354
  break;
1322
1355
  }
1323
1356
  case "tool-call":
1324
- if (jsonResponseTool == null) {
1357
+ if (!usesJsonResponseTool) {
1325
1358
  controller.enqueue({
1326
1359
  type: "tool-input-end",
1327
1360
  id: contentBlock.toolCallId
@@ -1339,7 +1372,7 @@ var AnthropicMessagesLanguageModel = class {
1339
1372
  const deltaType = value.delta.type;
1340
1373
  switch (deltaType) {
1341
1374
  case "text_delta": {
1342
- if (jsonResponseTool != null) {
1375
+ if (usesJsonResponseTool) {
1343
1376
  return;
1344
1377
  }
1345
1378
  controller.enqueue({
@@ -1375,16 +1408,15 @@ var AnthropicMessagesLanguageModel = class {
1375
1408
  case "input_json_delta": {
1376
1409
  const contentBlock = contentBlocks[value.index];
1377
1410
  const delta = value.delta.partial_json;
1378
- if (jsonResponseTool == null) {
1379
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
1411
+ if (usesJsonResponseTool) {
1412
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
1380
1413
  return;
1381
1414
  }
1382
1415
  controller.enqueue({
1383
- type: "tool-input-delta",
1384
- id: contentBlock.toolCallId,
1416
+ type: "text-delta",
1417
+ id: String(value.index),
1385
1418
  delta
1386
1419
  });
1387
- contentBlock.input += delta;
1388
1420
  } else {
1389
1421
  if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
1390
1422
  return;
@@ -1394,6 +1426,7 @@ var AnthropicMessagesLanguageModel = class {
1394
1426
  id: contentBlock.toolCallId,
1395
1427
  delta
1396
1428
  });
1429
+ contentBlock.input += delta;
1397
1430
  }
1398
1431
  return;
1399
1432
  }
@@ -1420,6 +1453,7 @@ var AnthropicMessagesLanguageModel = class {
1420
1453
  usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
1421
1454
  providerMetadata = {
1422
1455
  anthropic: {
1456
+ usage: value.message.usage,
1423
1457
  cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
1424
1458
  }
1425
1459
  };
@@ -1435,7 +1469,7 @@ var AnthropicMessagesLanguageModel = class {
1435
1469
  usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
1436
1470
  finishReason = mapAnthropicStopReason({
1437
1471
  finishReason: value.delta.stop_reason,
1438
- isJsonResponseFromTool: jsonResponseTool != null
1472
+ isJsonResponseFromTool: usesJsonResponseTool
1439
1473
  });
1440
1474
  return;
1441
1475
  }
@@ -1495,7 +1529,7 @@ var anthropicMessagesResponseSchema = z4.object({
1495
1529
  type: z4.literal("server_tool_use"),
1496
1530
  id: z4.string(),
1497
1531
  name: z4.string(),
1498
- input: z4.record(z4.unknown()).nullish()
1532
+ input: z4.record(z4.string(), z4.unknown()).nullish()
1499
1533
  }),
1500
1534
  z4.object({
1501
1535
  type: z4.literal("web_search_tool_result"),
@@ -1519,14 +1553,11 @@ var anthropicMessagesResponseSchema = z4.object({
1519
1553
  ])
1520
1554
  ),
1521
1555
  stop_reason: z4.string().nullish(),
1522
- usage: z4.object({
1556
+ usage: z4.looseObject({
1523
1557
  input_tokens: z4.number(),
1524
1558
  output_tokens: z4.number(),
1525
1559
  cache_creation_input_tokens: z4.number().nullish(),
1526
- cache_read_input_tokens: z4.number().nullish(),
1527
- server_tool_use: z4.object({
1528
- web_search_requests: z4.number()
1529
- }).nullish()
1560
+ cache_read_input_tokens: z4.number().nullish()
1530
1561
  })
1531
1562
  });
1532
1563
  var anthropicMessagesChunkSchema = z4.discriminatedUnion("type", [
@@ -1535,7 +1566,7 @@ var anthropicMessagesChunkSchema = z4.discriminatedUnion("type", [
1535
1566
  message: z4.object({
1536
1567
  id: z4.string().nullish(),
1537
1568
  model: z4.string().nullish(),
1538
- usage: z4.object({
1569
+ usage: z4.looseObject({
1539
1570
  input_tokens: z4.number(),
1540
1571
  output_tokens: z4.number(),
1541
1572
  cache_creation_input_tokens: z4.number().nullish(),
@@ -1568,7 +1599,7 @@ var anthropicMessagesChunkSchema = z4.discriminatedUnion("type", [
1568
1599
  type: z4.literal("server_tool_use"),
1569
1600
  id: z4.string(),
1570
1601
  name: z4.string(),
1571
- input: z4.record(z4.unknown()).nullish()
1602
+ input: z4.record(z4.string(), z4.unknown()).nullish()
1572
1603
  }),
1573
1604
  z4.object({
1574
1605
  type: z4.literal("web_search_tool_result"),
@@ -1761,6 +1792,23 @@ var textEditor_20250124 = createProviderDefinedToolFactory6({
1761
1792
  })
1762
1793
  });
1763
1794
 
1795
+ // src/tool/text-editor_20250429.ts
1796
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory7 } from "@ai-sdk/provider-utils";
1797
+ import { z as z11 } from "zod/v4";
1798
+ var textEditor_20250429 = createProviderDefinedToolFactory7({
1799
+ id: "anthropic.text_editor_20250429",
1800
+ name: "str_replace_based_edit_tool",
1801
+ inputSchema: z11.object({
1802
+ command: z11.enum(["view", "create", "str_replace", "insert"]),
1803
+ path: z11.string(),
1804
+ file_text: z11.string().optional(),
1805
+ insert_line: z11.number().int().optional(),
1806
+ new_str: z11.string().optional(),
1807
+ old_str: z11.string().optional(),
1808
+ view_range: z11.array(z11.number().int()).optional()
1809
+ })
1810
+ });
1811
+
1764
1812
  // src/anthropic-tools.ts
1765
1813
  var anthropicTools = {
1766
1814
  /**
@@ -1787,6 +1835,11 @@ var anthropicTools = {
1787
1835
  * Creates a tool for editing text. Must have name "str_replace_editor".
1788
1836
  */
1789
1837
  textEditor_20250124,
1838
+ /**
1839
+ * Creates a tool for editing text. Must have name "str_replace_based_edit_tool".
1840
+ * Note: This version does not support the "undo_edit" command.
1841
+ */
1842
+ textEditor_20250429,
1790
1843
  /**
1791
1844
  * Creates a tool for executing actions on a computer. Must have name "computer".
1792
1845
  *