@ai-sdk/anthropic 2.0.0-alpha.12 → 2.0.0-alpha.14

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.
@@ -47,13 +47,6 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
47
47
 
48
48
  // src/anthropic-messages-options.ts
49
49
  var import_zod2 = require("zod");
50
- var webSearchLocationSchema = import_zod2.z.object({
51
- type: import_zod2.z.literal("approximate"),
52
- city: import_zod2.z.string().optional(),
53
- region: import_zod2.z.string().optional(),
54
- country: import_zod2.z.string(),
55
- timezone: import_zod2.z.string().optional()
56
- });
57
50
  var anthropicFilePartProviderOptions = import_zod2.z.object({
58
51
  /**
59
52
  * Citation configuration for this document.
@@ -78,40 +71,10 @@ var anthropicFilePartProviderOptions = import_zod2.z.object({
78
71
  context: import_zod2.z.string().optional()
79
72
  });
80
73
  var anthropicProviderOptions = import_zod2.z.object({
81
- /**
82
- Include reasoning content in requests sent to the model. Defaults to `true`.
83
-
84
- If you are experiencing issues with the model handling requests involving
85
- */
86
74
  sendReasoning: import_zod2.z.boolean().optional(),
87
75
  thinking: import_zod2.z.object({
88
76
  type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
89
77
  budgetTokens: import_zod2.z.number().optional()
90
- }).optional(),
91
- /**
92
- * Web search tool configuration for Claude models that support it.
93
- * When provided, automatically adds the web search tool to the request.
94
- */
95
- webSearch: import_zod2.z.object({
96
- /**
97
- * Limit the number of searches per request (optional)
98
- * Defaults to 5 if not specified
99
- */
100
- maxUses: import_zod2.z.number().min(1).max(20).optional(),
101
- /**
102
- * Only include results from these domains (optional)
103
- * Cannot be used with blockedDomains
104
- */
105
- allowedDomains: import_zod2.z.array(import_zod2.z.string()).optional(),
106
- /**
107
- * Never include results from these domains (optional)
108
- * Cannot be used with allowedDomains
109
- */
110
- blockedDomains: import_zod2.z.array(import_zod2.z.string()).optional(),
111
- /**
112
- * Localize search results based on user location (optional)
113
- */
114
- userLocation: webSearchLocationSchema.optional()
115
78
  }).optional()
116
79
  });
117
80
 
@@ -141,10 +104,10 @@ function prepareTools({
141
104
  anthropicTools2.push({
142
105
  name: tool.name,
143
106
  description: tool.description,
144
- input_schema: tool.parameters
107
+ input_schema: tool.inputSchema
145
108
  });
146
109
  break;
147
- case "provider-defined":
110
+ case "provider-defined-client":
148
111
  switch (tool.id) {
149
112
  case "anthropic.computer_20250124":
150
113
  betas.add("computer-use-2025-01-24");
@@ -199,6 +162,32 @@ function prepareTools({
199
162
  break;
200
163
  }
201
164
  break;
165
+ case "provider-defined-server":
166
+ switch (tool.id) {
167
+ case "anthropic.web_search_20250305":
168
+ const webSearchTool = {
169
+ type: "web_search_20250305",
170
+ name: tool.name
171
+ };
172
+ if (tool.args.maxUses) {
173
+ webSearchTool.max_uses = tool.args.maxUses;
174
+ }
175
+ if (tool.args.allowedDomains) {
176
+ webSearchTool.allowed_domains = tool.args.allowedDomains;
177
+ }
178
+ if (tool.args.blockedDomains) {
179
+ webSearchTool.blocked_domains = tool.args.blockedDomains;
180
+ }
181
+ if (tool.args.userLocation) {
182
+ webSearchTool.user_location = tool.args.userLocation;
183
+ }
184
+ anthropicTools2.push(webSearchTool);
185
+ break;
186
+ default:
187
+ toolWarnings.push({ type: "unsupported-tool", tool });
188
+ break;
189
+ }
190
+ break;
202
191
  default:
203
192
  toolWarnings.push({ type: "unsupported-tool", tool });
204
193
  break;
@@ -249,12 +238,28 @@ function prepareTools({
249
238
  // src/convert-to-anthropic-messages-prompt.ts
250
239
  var import_provider2 = require("@ai-sdk/provider");
251
240
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
241
+ function convertToString(data) {
242
+ if (typeof data === "string") {
243
+ return Buffer.from(data, "base64").toString("utf-8");
244
+ }
245
+ if (data instanceof Uint8Array) {
246
+ return new TextDecoder().decode(data);
247
+ }
248
+ if (data instanceof URL) {
249
+ throw new import_provider2.UnsupportedFunctionalityError({
250
+ functionality: "URL-based text documents are not supported for citations"
251
+ });
252
+ }
253
+ throw new import_provider2.UnsupportedFunctionalityError({
254
+ functionality: `unsupported data type for text documents: ${typeof data}`
255
+ });
256
+ }
252
257
  async function convertToAnthropicMessagesPrompt({
253
258
  prompt,
254
259
  sendReasoning,
255
260
  warnings
256
261
  }) {
257
- var _a, _b, _c, _d;
262
+ var _a, _b, _c, _d, _e;
258
263
  const betas = /* @__PURE__ */ new Set();
259
264
  const blocks = groupIntoBlocks(prompt);
260
265
  let system = void 0;
@@ -361,6 +366,30 @@ async function convertToAnthropicMessagesPrompt({
361
366
  },
362
367
  cache_control: cacheControl
363
368
  });
369
+ } else if (part.mediaType === "text/plain") {
370
+ const enableCitations = await shouldEnableCitations(
371
+ part.providerOptions
372
+ );
373
+ const metadata = await getDocumentMetadata(
374
+ part.providerOptions
375
+ );
376
+ anthropicContent.push({
377
+ type: "document",
378
+ source: part.data instanceof URL ? {
379
+ type: "url",
380
+ url: part.data.toString()
381
+ } : {
382
+ type: "text",
383
+ media_type: "text/plain",
384
+ data: convertToString(part.data)
385
+ },
386
+ title: (_c = metadata.title) != null ? _c : part.filename,
387
+ ...metadata.context && { context: metadata.context },
388
+ ...enableCitations && {
389
+ citations: { enabled: true }
390
+ },
391
+ cache_control: cacheControl
392
+ });
364
393
  } else {
365
394
  throw new import_provider2.UnsupportedFunctionalityError({
366
395
  functionality: `media type: ${part.mediaType}`
@@ -376,7 +405,7 @@ async function convertToAnthropicMessagesPrompt({
376
405
  for (let i2 = 0; i2 < content.length; i2++) {
377
406
  const part = content[i2];
378
407
  const isLastPart = i2 === content.length - 1;
379
- const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
408
+ const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
380
409
  const toolResultContent = part.content != null ? part.content.map((part2) => {
381
410
  var _a2;
382
411
  switch (part2.type) {
@@ -397,7 +426,7 @@ async function convertToAnthropicMessagesPrompt({
397
426
  cache_control: void 0
398
427
  };
399
428
  }
400
- }) : JSON.stringify(part.result);
429
+ }) : JSON.stringify(part.output);
401
430
  anthropicContent.push({
402
431
  type: "tool_result",
403
432
  tool_use_id: part.toolCallId,
@@ -426,7 +455,7 @@ async function convertToAnthropicMessagesPrompt({
426
455
  for (let k = 0; k < content.length; k++) {
427
456
  const part = content[k];
428
457
  const isLastContentPart = k === content.length - 1;
429
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
458
+ const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
430
459
  switch (part.type) {
431
460
  case "text": {
432
461
  anthropicContent.push({
@@ -487,7 +516,7 @@ async function convertToAnthropicMessagesPrompt({
487
516
  type: "tool_use",
488
517
  id: part.toolCallId,
489
518
  name: part.toolName,
490
- input: part.args,
519
+ input: part.input,
491
520
  cache_control: cacheControl
492
521
  });
493
522
  break;
@@ -575,8 +604,42 @@ function mapAnthropicStopReason({
575
604
  }
576
605
 
577
606
  // src/anthropic-messages-language-model.ts
578
- function processPageLocationCitation(citation, citationDocuments, generateId2, onSource) {
579
- if (citation.type === "page_location") {
607
+ var citationSchemas = {
608
+ webSearchResult: import_zod3.z.object({
609
+ type: import_zod3.z.literal("web_search_result_location"),
610
+ cited_text: import_zod3.z.string(),
611
+ url: import_zod3.z.string(),
612
+ title: import_zod3.z.string(),
613
+ encrypted_index: import_zod3.z.string()
614
+ }),
615
+ pageLocation: import_zod3.z.object({
616
+ type: import_zod3.z.literal("page_location"),
617
+ cited_text: import_zod3.z.string(),
618
+ document_index: import_zod3.z.number(),
619
+ document_title: import_zod3.z.string().nullable(),
620
+ start_page_number: import_zod3.z.number(),
621
+ end_page_number: import_zod3.z.number()
622
+ }),
623
+ charLocation: import_zod3.z.object({
624
+ type: import_zod3.z.literal("char_location"),
625
+ cited_text: import_zod3.z.string(),
626
+ document_index: import_zod3.z.number(),
627
+ document_title: import_zod3.z.string().nullable(),
628
+ start_char_index: import_zod3.z.number(),
629
+ end_char_index: import_zod3.z.number()
630
+ })
631
+ };
632
+ var citationSchema = import_zod3.z.discriminatedUnion("type", [
633
+ citationSchemas.webSearchResult,
634
+ citationSchemas.pageLocation,
635
+ citationSchemas.charLocation
636
+ ]);
637
+ var documentCitationSchema = import_zod3.z.discriminatedUnion("type", [
638
+ citationSchemas.pageLocation,
639
+ citationSchemas.charLocation
640
+ ]);
641
+ function processCitation(citation, citationDocuments, generateId2, onSource) {
642
+ if (citation.type === "page_location" || citation.type === "char_location") {
580
643
  const source = createCitationSource(
581
644
  citation,
582
645
  citationDocuments,
@@ -593,6 +656,15 @@ function createCitationSource(citation, citationDocuments, generateId2) {
593
656
  if (!documentInfo) {
594
657
  return null;
595
658
  }
659
+ const providerMetadata = citation.type === "page_location" ? {
660
+ citedText: citation.cited_text,
661
+ startPageNumber: citation.start_page_number,
662
+ endPageNumber: citation.end_page_number
663
+ } : {
664
+ citedText: citation.cited_text,
665
+ startCharIndex: citation.start_char_index,
666
+ endCharIndex: citation.end_char_index
667
+ };
596
668
  return {
597
669
  type: "source",
598
670
  sourceType: "document",
@@ -601,11 +673,7 @@ function createCitationSource(citation, citationDocuments, generateId2) {
601
673
  title: (_a = citation.document_title) != null ? _a : documentInfo.title,
602
674
  filename: documentInfo.filename,
603
675
  providerMetadata: {
604
- anthropic: {
605
- citedText: citation.cited_text,
606
- startPageNumber: citation.start_page_number,
607
- endPageNumber: citation.end_page_number
608
- }
676
+ anthropic: providerMetadata
609
677
  }
610
678
  };
611
679
  }
@@ -682,7 +750,7 @@ var AnthropicMessagesLanguageModel = class {
682
750
  type: "function",
683
751
  name: "json",
684
752
  description: "Respond with a JSON object.",
685
- parameters: responseFormat.schema
753
+ inputSchema: responseFormat.schema
686
754
  } : void 0;
687
755
  const anthropicOptions = await (0, import_provider_utils3.parseProviderOptions)({
688
756
  provider: "anthropic",
@@ -745,27 +813,6 @@ var AnthropicMessagesLanguageModel = class {
745
813
  }
746
814
  baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
747
815
  }
748
- let modifiedTools = tools;
749
- let modifiedToolChoice = toolChoice;
750
- if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
751
- const webSearchTool = {
752
- type: "web_search_20250305",
753
- name: "web_search",
754
- max_uses: anthropicOptions.webSearch.maxUses,
755
- allowed_domains: anthropicOptions.webSearch.allowedDomains,
756
- blocked_domains: anthropicOptions.webSearch.blockedDomains,
757
- ...anthropicOptions.webSearch.userLocation && {
758
- user_location: {
759
- type: anthropicOptions.webSearch.userLocation.type,
760
- country: anthropicOptions.webSearch.userLocation.country,
761
- city: anthropicOptions.webSearch.userLocation.city,
762
- region: anthropicOptions.webSearch.userLocation.region,
763
- timezone: anthropicOptions.webSearch.userLocation.timezone
764
- }
765
- }
766
- };
767
- modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
768
- }
769
816
  const {
770
817
  tools: anthropicTools2,
771
818
  toolChoice: anthropicToolChoice,
@@ -775,7 +822,7 @@ var AnthropicMessagesLanguageModel = class {
775
822
  jsonResponseTool != null ? {
776
823
  tools: [jsonResponseTool],
777
824
  toolChoice: { type: "tool", toolName: jsonResponseTool.name }
778
- } : { tools: modifiedTools, toolChoice: modifiedToolChoice }
825
+ } : { tools: tools != null ? tools : [], toolChoice }
779
826
  );
780
827
  return {
781
828
  args: {
@@ -807,15 +854,19 @@ var AnthropicMessagesLanguageModel = class {
807
854
  return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
808
855
  }
809
856
  extractCitationDocuments(prompt) {
810
- const isCitationEnabled = (part) => {
857
+ const isCitationPart = (part) => {
811
858
  var _a, _b;
859
+ if (part.type !== "file") {
860
+ return false;
861
+ }
862
+ if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
863
+ return false;
864
+ }
812
865
  const anthropic = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
813
866
  const citationsConfig = anthropic == null ? void 0 : anthropic.citations;
814
867
  return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
815
868
  };
816
- return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
817
- (part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
818
- ).map((part) => {
869
+ return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
819
870
  var _a;
820
871
  const filePart = part;
821
872
  return {
@@ -852,7 +903,7 @@ var AnthropicMessagesLanguageModel = class {
852
903
  content.push({ type: "text", text: part.text });
853
904
  if (part.citations) {
854
905
  for (const citation of part.citations) {
855
- processPageLocationCitation(
906
+ processCitation(
856
907
  citation,
857
908
  citationDocuments,
858
909
  this.generateId,
@@ -898,7 +949,7 @@ var AnthropicMessagesLanguageModel = class {
898
949
  toolCallType: "function",
899
950
  toolCallId: part.id,
900
951
  toolName: part.name,
901
- args: JSON.stringify(part.input)
952
+ input: JSON.stringify(part.input)
902
953
  }
903
954
  );
904
955
  break;
@@ -1090,7 +1141,7 @@ var AnthropicMessagesLanguageModel = class {
1090
1141
  toolCallType: "function",
1091
1142
  toolCallId: contentBlock.toolCallId,
1092
1143
  toolName: contentBlock.toolName,
1093
- args: contentBlock.jsonText
1144
+ input: contentBlock.jsonText
1094
1145
  });
1095
1146
  }
1096
1147
  delete toolCallContentBlocks[value.index];
@@ -1147,7 +1198,7 @@ var AnthropicMessagesLanguageModel = class {
1147
1198
  toolCallType: "function",
1148
1199
  toolCallId: contentBlock.toolCallId,
1149
1200
  toolName: contentBlock.toolName,
1150
- argsTextDelta: value.delta.partial_json
1201
+ inputTextDelta: value.delta.partial_json
1151
1202
  }
1152
1203
  );
1153
1204
  contentBlock.jsonText += value.delta.partial_json;
@@ -1155,7 +1206,7 @@ var AnthropicMessagesLanguageModel = class {
1155
1206
  }
1156
1207
  case "citations_delta": {
1157
1208
  const citation = value.delta.citation;
1158
- processPageLocationCitation(
1209
+ processCitation(
1159
1210
  citation,
1160
1211
  citationDocuments,
1161
1212
  generateId2,
@@ -1230,25 +1281,7 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
1230
1281
  import_zod3.z.object({
1231
1282
  type: import_zod3.z.literal("text"),
1232
1283
  text: import_zod3.z.string(),
1233
- citations: import_zod3.z.array(
1234
- import_zod3.z.discriminatedUnion("type", [
1235
- import_zod3.z.object({
1236
- type: import_zod3.z.literal("web_search_result_location"),
1237
- cited_text: import_zod3.z.string(),
1238
- url: import_zod3.z.string(),
1239
- title: import_zod3.z.string(),
1240
- encrypted_index: import_zod3.z.string()
1241
- }),
1242
- import_zod3.z.object({
1243
- type: import_zod3.z.literal("page_location"),
1244
- cited_text: import_zod3.z.string(),
1245
- document_index: import_zod3.z.number(),
1246
- document_title: import_zod3.z.string().nullable(),
1247
- start_page_number: import_zod3.z.number(),
1248
- end_page_number: import_zod3.z.number()
1249
- })
1250
- ])
1251
- ).optional()
1284
+ citations: import_zod3.z.array(citationSchema).optional()
1252
1285
  }),
1253
1286
  import_zod3.z.object({
1254
1287
  type: import_zod3.z.literal("thinking"),
@@ -1387,23 +1420,7 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
1387
1420
  }),
1388
1421
  import_zod3.z.object({
1389
1422
  type: import_zod3.z.literal("citations_delta"),
1390
- citation: import_zod3.z.discriminatedUnion("type", [
1391
- import_zod3.z.object({
1392
- type: import_zod3.z.literal("web_search_result_location"),
1393
- cited_text: import_zod3.z.string(),
1394
- url: import_zod3.z.string(),
1395
- title: import_zod3.z.string(),
1396
- encrypted_index: import_zod3.z.string()
1397
- }),
1398
- import_zod3.z.object({
1399
- type: import_zod3.z.literal("page_location"),
1400
- cited_text: import_zod3.z.string(),
1401
- document_index: import_zod3.z.number(),
1402
- document_title: import_zod3.z.string().nullable(),
1403
- start_page_number: import_zod3.z.number(),
1404
- end_page_number: import_zod3.z.number()
1405
- })
1406
- ])
1423
+ citation: citationSchema
1407
1424
  })
1408
1425
  ])
1409
1426
  }),
@@ -1443,7 +1460,7 @@ var Bash20241022Parameters = import_zod4.z.object({
1443
1460
  });
1444
1461
  function bashTool_20241022(options = {}) {
1445
1462
  return {
1446
- type: "provider-defined",
1463
+ type: "provider-defined-client",
1447
1464
  id: "anthropic.bash_20241022",
1448
1465
  args: {},
1449
1466
  parameters: Bash20241022Parameters,
@@ -1457,7 +1474,7 @@ var Bash20250124Parameters = import_zod4.z.object({
1457
1474
  });
1458
1475
  function bashTool_20250124(options = {}) {
1459
1476
  return {
1460
- type: "provider-defined",
1477
+ type: "provider-defined-client",
1461
1478
  id: "anthropic.bash_20250124",
1462
1479
  args: {},
1463
1480
  parameters: Bash20250124Parameters,
@@ -1476,7 +1493,7 @@ var TextEditor20241022Parameters = import_zod4.z.object({
1476
1493
  });
1477
1494
  function textEditorTool_20241022(options = {}) {
1478
1495
  return {
1479
- type: "provider-defined",
1496
+ type: "provider-defined-client",
1480
1497
  id: "anthropic.text_editor_20241022",
1481
1498
  args: {},
1482
1499
  parameters: TextEditor20241022Parameters,
@@ -1495,7 +1512,7 @@ var TextEditor20250124Parameters = import_zod4.z.object({
1495
1512
  });
1496
1513
  function textEditorTool_20250124(options = {}) {
1497
1514
  return {
1498
- type: "provider-defined",
1515
+ type: "provider-defined-client",
1499
1516
  id: "anthropic.text_editor_20250124",
1500
1517
  args: {},
1501
1518
  parameters: TextEditor20250124Parameters,
@@ -1521,7 +1538,7 @@ var Computer20241022Parameters = import_zod4.z.object({
1521
1538
  });
1522
1539
  function computerTool_20241022(options) {
1523
1540
  return {
1524
- type: "provider-defined",
1541
+ type: "provider-defined-client",
1525
1542
  id: "anthropic.computer_20241022",
1526
1543
  args: {
1527
1544
  displayWidthPx: options.displayWidthPx,
@@ -1561,7 +1578,7 @@ var Computer20250124Parameters = import_zod4.z.object({
1561
1578
  });
1562
1579
  function computerTool_20250124(options) {
1563
1580
  return {
1564
- type: "provider-defined",
1581
+ type: "provider-defined-client",
1565
1582
  id: "anthropic.computer_20250124",
1566
1583
  args: {
1567
1584
  displayWidthPx: options.displayWidthPx,
@@ -1573,13 +1590,30 @@ function computerTool_20250124(options) {
1573
1590
  experimental_toToolResultContent: options.experimental_toToolResultContent
1574
1591
  };
1575
1592
  }
1593
+ var WebSearch20250305Parameters = import_zod4.z.object({
1594
+ query: import_zod4.z.string()
1595
+ });
1596
+ function webSearchTool_20250305(options = {}) {
1597
+ return {
1598
+ type: "provider-defined-server",
1599
+ id: "anthropic.web_search_20250305",
1600
+ name: "web_search",
1601
+ args: {
1602
+ ...options.maxUses && { maxUses: options.maxUses },
1603
+ ...options.allowedDomains && { allowedDomains: options.allowedDomains },
1604
+ ...options.blockedDomains && { blockedDomains: options.blockedDomains },
1605
+ ...options.userLocation && { userLocation: options.userLocation }
1606
+ }
1607
+ };
1608
+ }
1576
1609
  var anthropicTools = {
1577
1610
  bash_20241022: bashTool_20241022,
1578
1611
  bash_20250124: bashTool_20250124,
1579
1612
  textEditor_20241022: textEditorTool_20241022,
1580
1613
  textEditor_20250124: textEditorTool_20250124,
1581
1614
  computer_20241022: computerTool_20241022,
1582
- computer_20250124: computerTool_20250124
1615
+ computer_20250124: computerTool_20250124,
1616
+ webSearch_20250305: webSearchTool_20250305
1583
1617
  };
1584
1618
  // Annotate the CommonJS export names for ESM import in node:
1585
1619
  0 && (module.exports = {