@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.
package/dist/index.js CHANGED
@@ -51,13 +51,6 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
51
51
 
52
52
  // src/anthropic-messages-options.ts
53
53
  var import_zod2 = require("zod");
54
- var webSearchLocationSchema = import_zod2.z.object({
55
- type: import_zod2.z.literal("approximate"),
56
- city: import_zod2.z.string().optional(),
57
- region: import_zod2.z.string().optional(),
58
- country: import_zod2.z.string(),
59
- timezone: import_zod2.z.string().optional()
60
- });
61
54
  var anthropicFilePartProviderOptions = import_zod2.z.object({
62
55
  /**
63
56
  * Citation configuration for this document.
@@ -82,40 +75,10 @@ var anthropicFilePartProviderOptions = import_zod2.z.object({
82
75
  context: import_zod2.z.string().optional()
83
76
  });
84
77
  var anthropicProviderOptions = import_zod2.z.object({
85
- /**
86
- Include reasoning content in requests sent to the model. Defaults to `true`.
87
-
88
- If you are experiencing issues with the model handling requests involving
89
- */
90
78
  sendReasoning: import_zod2.z.boolean().optional(),
91
79
  thinking: import_zod2.z.object({
92
80
  type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
93
81
  budgetTokens: import_zod2.z.number().optional()
94
- }).optional(),
95
- /**
96
- * Web search tool configuration for Claude models that support it.
97
- * When provided, automatically adds the web search tool to the request.
98
- */
99
- webSearch: import_zod2.z.object({
100
- /**
101
- * Limit the number of searches per request (optional)
102
- * Defaults to 5 if not specified
103
- */
104
- maxUses: import_zod2.z.number().min(1).max(20).optional(),
105
- /**
106
- * Only include results from these domains (optional)
107
- * Cannot be used with blockedDomains
108
- */
109
- allowedDomains: import_zod2.z.array(import_zod2.z.string()).optional(),
110
- /**
111
- * Never include results from these domains (optional)
112
- * Cannot be used with allowedDomains
113
- */
114
- blockedDomains: import_zod2.z.array(import_zod2.z.string()).optional(),
115
- /**
116
- * Localize search results based on user location (optional)
117
- */
118
- userLocation: webSearchLocationSchema.optional()
119
82
  }).optional()
120
83
  });
121
84
 
@@ -145,10 +108,10 @@ function prepareTools({
145
108
  anthropicTools2.push({
146
109
  name: tool.name,
147
110
  description: tool.description,
148
- input_schema: tool.parameters
111
+ input_schema: tool.inputSchema
149
112
  });
150
113
  break;
151
- case "provider-defined":
114
+ case "provider-defined-client":
152
115
  switch (tool.id) {
153
116
  case "anthropic.computer_20250124":
154
117
  betas.add("computer-use-2025-01-24");
@@ -203,6 +166,32 @@ function prepareTools({
203
166
  break;
204
167
  }
205
168
  break;
169
+ case "provider-defined-server":
170
+ switch (tool.id) {
171
+ case "anthropic.web_search_20250305":
172
+ const webSearchTool = {
173
+ type: "web_search_20250305",
174
+ name: tool.name
175
+ };
176
+ if (tool.args.maxUses) {
177
+ webSearchTool.max_uses = tool.args.maxUses;
178
+ }
179
+ if (tool.args.allowedDomains) {
180
+ webSearchTool.allowed_domains = tool.args.allowedDomains;
181
+ }
182
+ if (tool.args.blockedDomains) {
183
+ webSearchTool.blocked_domains = tool.args.blockedDomains;
184
+ }
185
+ if (tool.args.userLocation) {
186
+ webSearchTool.user_location = tool.args.userLocation;
187
+ }
188
+ anthropicTools2.push(webSearchTool);
189
+ break;
190
+ default:
191
+ toolWarnings.push({ type: "unsupported-tool", tool });
192
+ break;
193
+ }
194
+ break;
206
195
  default:
207
196
  toolWarnings.push({ type: "unsupported-tool", tool });
208
197
  break;
@@ -253,12 +242,28 @@ function prepareTools({
253
242
  // src/convert-to-anthropic-messages-prompt.ts
254
243
  var import_provider2 = require("@ai-sdk/provider");
255
244
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
245
+ function convertToString(data) {
246
+ if (typeof data === "string") {
247
+ return Buffer.from(data, "base64").toString("utf-8");
248
+ }
249
+ if (data instanceof Uint8Array) {
250
+ return new TextDecoder().decode(data);
251
+ }
252
+ if (data instanceof URL) {
253
+ throw new import_provider2.UnsupportedFunctionalityError({
254
+ functionality: "URL-based text documents are not supported for citations"
255
+ });
256
+ }
257
+ throw new import_provider2.UnsupportedFunctionalityError({
258
+ functionality: `unsupported data type for text documents: ${typeof data}`
259
+ });
260
+ }
256
261
  async function convertToAnthropicMessagesPrompt({
257
262
  prompt,
258
263
  sendReasoning,
259
264
  warnings
260
265
  }) {
261
- var _a, _b, _c, _d;
266
+ var _a, _b, _c, _d, _e;
262
267
  const betas = /* @__PURE__ */ new Set();
263
268
  const blocks = groupIntoBlocks(prompt);
264
269
  let system = void 0;
@@ -365,6 +370,30 @@ async function convertToAnthropicMessagesPrompt({
365
370
  },
366
371
  cache_control: cacheControl
367
372
  });
373
+ } else if (part.mediaType === "text/plain") {
374
+ const enableCitations = await shouldEnableCitations(
375
+ part.providerOptions
376
+ );
377
+ const metadata = await getDocumentMetadata(
378
+ part.providerOptions
379
+ );
380
+ anthropicContent.push({
381
+ type: "document",
382
+ source: part.data instanceof URL ? {
383
+ type: "url",
384
+ url: part.data.toString()
385
+ } : {
386
+ type: "text",
387
+ media_type: "text/plain",
388
+ data: convertToString(part.data)
389
+ },
390
+ title: (_c = metadata.title) != null ? _c : part.filename,
391
+ ...metadata.context && { context: metadata.context },
392
+ ...enableCitations && {
393
+ citations: { enabled: true }
394
+ },
395
+ cache_control: cacheControl
396
+ });
368
397
  } else {
369
398
  throw new import_provider2.UnsupportedFunctionalityError({
370
399
  functionality: `media type: ${part.mediaType}`
@@ -380,7 +409,7 @@ async function convertToAnthropicMessagesPrompt({
380
409
  for (let i2 = 0; i2 < content.length; i2++) {
381
410
  const part = content[i2];
382
411
  const isLastPart = i2 === content.length - 1;
383
- const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
412
+ const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
384
413
  const toolResultContent = part.content != null ? part.content.map((part2) => {
385
414
  var _a2;
386
415
  switch (part2.type) {
@@ -401,7 +430,7 @@ async function convertToAnthropicMessagesPrompt({
401
430
  cache_control: void 0
402
431
  };
403
432
  }
404
- }) : JSON.stringify(part.result);
433
+ }) : JSON.stringify(part.output);
405
434
  anthropicContent.push({
406
435
  type: "tool_result",
407
436
  tool_use_id: part.toolCallId,
@@ -430,7 +459,7 @@ async function convertToAnthropicMessagesPrompt({
430
459
  for (let k = 0; k < content.length; k++) {
431
460
  const part = content[k];
432
461
  const isLastContentPart = k === content.length - 1;
433
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
462
+ const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
434
463
  switch (part.type) {
435
464
  case "text": {
436
465
  anthropicContent.push({
@@ -491,7 +520,7 @@ async function convertToAnthropicMessagesPrompt({
491
520
  type: "tool_use",
492
521
  id: part.toolCallId,
493
522
  name: part.toolName,
494
- input: part.args,
523
+ input: part.input,
495
524
  cache_control: cacheControl
496
525
  });
497
526
  break;
@@ -579,8 +608,42 @@ function mapAnthropicStopReason({
579
608
  }
580
609
 
581
610
  // src/anthropic-messages-language-model.ts
582
- function processPageLocationCitation(citation, citationDocuments, generateId3, onSource) {
583
- if (citation.type === "page_location") {
611
+ var citationSchemas = {
612
+ webSearchResult: import_zod3.z.object({
613
+ type: import_zod3.z.literal("web_search_result_location"),
614
+ cited_text: import_zod3.z.string(),
615
+ url: import_zod3.z.string(),
616
+ title: import_zod3.z.string(),
617
+ encrypted_index: import_zod3.z.string()
618
+ }),
619
+ pageLocation: import_zod3.z.object({
620
+ type: import_zod3.z.literal("page_location"),
621
+ cited_text: import_zod3.z.string(),
622
+ document_index: import_zod3.z.number(),
623
+ document_title: import_zod3.z.string().nullable(),
624
+ start_page_number: import_zod3.z.number(),
625
+ end_page_number: import_zod3.z.number()
626
+ }),
627
+ charLocation: import_zod3.z.object({
628
+ type: import_zod3.z.literal("char_location"),
629
+ cited_text: import_zod3.z.string(),
630
+ document_index: import_zod3.z.number(),
631
+ document_title: import_zod3.z.string().nullable(),
632
+ start_char_index: import_zod3.z.number(),
633
+ end_char_index: import_zod3.z.number()
634
+ })
635
+ };
636
+ var citationSchema = import_zod3.z.discriminatedUnion("type", [
637
+ citationSchemas.webSearchResult,
638
+ citationSchemas.pageLocation,
639
+ citationSchemas.charLocation
640
+ ]);
641
+ var documentCitationSchema = import_zod3.z.discriminatedUnion("type", [
642
+ citationSchemas.pageLocation,
643
+ citationSchemas.charLocation
644
+ ]);
645
+ function processCitation(citation, citationDocuments, generateId3, onSource) {
646
+ if (citation.type === "page_location" || citation.type === "char_location") {
584
647
  const source = createCitationSource(
585
648
  citation,
586
649
  citationDocuments,
@@ -597,6 +660,15 @@ function createCitationSource(citation, citationDocuments, generateId3) {
597
660
  if (!documentInfo) {
598
661
  return null;
599
662
  }
663
+ const providerMetadata = citation.type === "page_location" ? {
664
+ citedText: citation.cited_text,
665
+ startPageNumber: citation.start_page_number,
666
+ endPageNumber: citation.end_page_number
667
+ } : {
668
+ citedText: citation.cited_text,
669
+ startCharIndex: citation.start_char_index,
670
+ endCharIndex: citation.end_char_index
671
+ };
600
672
  return {
601
673
  type: "source",
602
674
  sourceType: "document",
@@ -605,11 +677,7 @@ function createCitationSource(citation, citationDocuments, generateId3) {
605
677
  title: (_a = citation.document_title) != null ? _a : documentInfo.title,
606
678
  filename: documentInfo.filename,
607
679
  providerMetadata: {
608
- anthropic: {
609
- citedText: citation.cited_text,
610
- startPageNumber: citation.start_page_number,
611
- endPageNumber: citation.end_page_number
612
- }
680
+ anthropic: providerMetadata
613
681
  }
614
682
  };
615
683
  }
@@ -686,7 +754,7 @@ var AnthropicMessagesLanguageModel = class {
686
754
  type: "function",
687
755
  name: "json",
688
756
  description: "Respond with a JSON object.",
689
- parameters: responseFormat.schema
757
+ inputSchema: responseFormat.schema
690
758
  } : void 0;
691
759
  const anthropicOptions = await (0, import_provider_utils3.parseProviderOptions)({
692
760
  provider: "anthropic",
@@ -749,27 +817,6 @@ var AnthropicMessagesLanguageModel = class {
749
817
  }
750
818
  baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
751
819
  }
752
- let modifiedTools = tools;
753
- let modifiedToolChoice = toolChoice;
754
- if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
755
- const webSearchTool = {
756
- type: "web_search_20250305",
757
- name: "web_search",
758
- max_uses: anthropicOptions.webSearch.maxUses,
759
- allowed_domains: anthropicOptions.webSearch.allowedDomains,
760
- blocked_domains: anthropicOptions.webSearch.blockedDomains,
761
- ...anthropicOptions.webSearch.userLocation && {
762
- user_location: {
763
- type: anthropicOptions.webSearch.userLocation.type,
764
- country: anthropicOptions.webSearch.userLocation.country,
765
- city: anthropicOptions.webSearch.userLocation.city,
766
- region: anthropicOptions.webSearch.userLocation.region,
767
- timezone: anthropicOptions.webSearch.userLocation.timezone
768
- }
769
- }
770
- };
771
- modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
772
- }
773
820
  const {
774
821
  tools: anthropicTools2,
775
822
  toolChoice: anthropicToolChoice,
@@ -779,7 +826,7 @@ var AnthropicMessagesLanguageModel = class {
779
826
  jsonResponseTool != null ? {
780
827
  tools: [jsonResponseTool],
781
828
  toolChoice: { type: "tool", toolName: jsonResponseTool.name }
782
- } : { tools: modifiedTools, toolChoice: modifiedToolChoice }
829
+ } : { tools: tools != null ? tools : [], toolChoice }
783
830
  );
784
831
  return {
785
832
  args: {
@@ -811,15 +858,19 @@ var AnthropicMessagesLanguageModel = class {
811
858
  return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
812
859
  }
813
860
  extractCitationDocuments(prompt) {
814
- const isCitationEnabled = (part) => {
861
+ const isCitationPart = (part) => {
815
862
  var _a, _b;
863
+ if (part.type !== "file") {
864
+ return false;
865
+ }
866
+ if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
867
+ return false;
868
+ }
816
869
  const anthropic2 = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
817
870
  const citationsConfig = anthropic2 == null ? void 0 : anthropic2.citations;
818
871
  return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
819
872
  };
820
- return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
821
- (part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
822
- ).map((part) => {
873
+ return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
823
874
  var _a;
824
875
  const filePart = part;
825
876
  return {
@@ -856,7 +907,7 @@ var AnthropicMessagesLanguageModel = class {
856
907
  content.push({ type: "text", text: part.text });
857
908
  if (part.citations) {
858
909
  for (const citation of part.citations) {
859
- processPageLocationCitation(
910
+ processCitation(
860
911
  citation,
861
912
  citationDocuments,
862
913
  this.generateId,
@@ -902,7 +953,7 @@ var AnthropicMessagesLanguageModel = class {
902
953
  toolCallType: "function",
903
954
  toolCallId: part.id,
904
955
  toolName: part.name,
905
- args: JSON.stringify(part.input)
956
+ input: JSON.stringify(part.input)
906
957
  }
907
958
  );
908
959
  break;
@@ -1094,7 +1145,7 @@ var AnthropicMessagesLanguageModel = class {
1094
1145
  toolCallType: "function",
1095
1146
  toolCallId: contentBlock.toolCallId,
1096
1147
  toolName: contentBlock.toolName,
1097
- args: contentBlock.jsonText
1148
+ input: contentBlock.jsonText
1098
1149
  });
1099
1150
  }
1100
1151
  delete toolCallContentBlocks[value.index];
@@ -1151,7 +1202,7 @@ var AnthropicMessagesLanguageModel = class {
1151
1202
  toolCallType: "function",
1152
1203
  toolCallId: contentBlock.toolCallId,
1153
1204
  toolName: contentBlock.toolName,
1154
- argsTextDelta: value.delta.partial_json
1205
+ inputTextDelta: value.delta.partial_json
1155
1206
  }
1156
1207
  );
1157
1208
  contentBlock.jsonText += value.delta.partial_json;
@@ -1159,7 +1210,7 @@ var AnthropicMessagesLanguageModel = class {
1159
1210
  }
1160
1211
  case "citations_delta": {
1161
1212
  const citation = value.delta.citation;
1162
- processPageLocationCitation(
1213
+ processCitation(
1163
1214
  citation,
1164
1215
  citationDocuments,
1165
1216
  generateId3,
@@ -1234,25 +1285,7 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
1234
1285
  import_zod3.z.object({
1235
1286
  type: import_zod3.z.literal("text"),
1236
1287
  text: import_zod3.z.string(),
1237
- citations: import_zod3.z.array(
1238
- import_zod3.z.discriminatedUnion("type", [
1239
- import_zod3.z.object({
1240
- type: import_zod3.z.literal("web_search_result_location"),
1241
- cited_text: import_zod3.z.string(),
1242
- url: import_zod3.z.string(),
1243
- title: import_zod3.z.string(),
1244
- encrypted_index: import_zod3.z.string()
1245
- }),
1246
- import_zod3.z.object({
1247
- type: import_zod3.z.literal("page_location"),
1248
- cited_text: import_zod3.z.string(),
1249
- document_index: import_zod3.z.number(),
1250
- document_title: import_zod3.z.string().nullable(),
1251
- start_page_number: import_zod3.z.number(),
1252
- end_page_number: import_zod3.z.number()
1253
- })
1254
- ])
1255
- ).optional()
1288
+ citations: import_zod3.z.array(citationSchema).optional()
1256
1289
  }),
1257
1290
  import_zod3.z.object({
1258
1291
  type: import_zod3.z.literal("thinking"),
@@ -1391,23 +1424,7 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
1391
1424
  }),
1392
1425
  import_zod3.z.object({
1393
1426
  type: import_zod3.z.literal("citations_delta"),
1394
- citation: import_zod3.z.discriminatedUnion("type", [
1395
- import_zod3.z.object({
1396
- type: import_zod3.z.literal("web_search_result_location"),
1397
- cited_text: import_zod3.z.string(),
1398
- url: import_zod3.z.string(),
1399
- title: import_zod3.z.string(),
1400
- encrypted_index: import_zod3.z.string()
1401
- }),
1402
- import_zod3.z.object({
1403
- type: import_zod3.z.literal("page_location"),
1404
- cited_text: import_zod3.z.string(),
1405
- document_index: import_zod3.z.number(),
1406
- document_title: import_zod3.z.string().nullable(),
1407
- start_page_number: import_zod3.z.number(),
1408
- end_page_number: import_zod3.z.number()
1409
- })
1410
- ])
1427
+ citation: citationSchema
1411
1428
  })
1412
1429
  ])
1413
1430
  }),
@@ -1447,7 +1464,7 @@ var Bash20241022Parameters = import_zod4.z.object({
1447
1464
  });
1448
1465
  function bashTool_20241022(options = {}) {
1449
1466
  return {
1450
- type: "provider-defined",
1467
+ type: "provider-defined-client",
1451
1468
  id: "anthropic.bash_20241022",
1452
1469
  args: {},
1453
1470
  parameters: Bash20241022Parameters,
@@ -1461,7 +1478,7 @@ var Bash20250124Parameters = import_zod4.z.object({
1461
1478
  });
1462
1479
  function bashTool_20250124(options = {}) {
1463
1480
  return {
1464
- type: "provider-defined",
1481
+ type: "provider-defined-client",
1465
1482
  id: "anthropic.bash_20250124",
1466
1483
  args: {},
1467
1484
  parameters: Bash20250124Parameters,
@@ -1480,7 +1497,7 @@ var TextEditor20241022Parameters = import_zod4.z.object({
1480
1497
  });
1481
1498
  function textEditorTool_20241022(options = {}) {
1482
1499
  return {
1483
- type: "provider-defined",
1500
+ type: "provider-defined-client",
1484
1501
  id: "anthropic.text_editor_20241022",
1485
1502
  args: {},
1486
1503
  parameters: TextEditor20241022Parameters,
@@ -1499,7 +1516,7 @@ var TextEditor20250124Parameters = import_zod4.z.object({
1499
1516
  });
1500
1517
  function textEditorTool_20250124(options = {}) {
1501
1518
  return {
1502
- type: "provider-defined",
1519
+ type: "provider-defined-client",
1503
1520
  id: "anthropic.text_editor_20250124",
1504
1521
  args: {},
1505
1522
  parameters: TextEditor20250124Parameters,
@@ -1525,7 +1542,7 @@ var Computer20241022Parameters = import_zod4.z.object({
1525
1542
  });
1526
1543
  function computerTool_20241022(options) {
1527
1544
  return {
1528
- type: "provider-defined",
1545
+ type: "provider-defined-client",
1529
1546
  id: "anthropic.computer_20241022",
1530
1547
  args: {
1531
1548
  displayWidthPx: options.displayWidthPx,
@@ -1565,7 +1582,7 @@ var Computer20250124Parameters = import_zod4.z.object({
1565
1582
  });
1566
1583
  function computerTool_20250124(options) {
1567
1584
  return {
1568
- type: "provider-defined",
1585
+ type: "provider-defined-client",
1569
1586
  id: "anthropic.computer_20250124",
1570
1587
  args: {
1571
1588
  displayWidthPx: options.displayWidthPx,
@@ -1577,13 +1594,30 @@ function computerTool_20250124(options) {
1577
1594
  experimental_toToolResultContent: options.experimental_toToolResultContent
1578
1595
  };
1579
1596
  }
1597
+ var WebSearch20250305Parameters = import_zod4.z.object({
1598
+ query: import_zod4.z.string()
1599
+ });
1600
+ function webSearchTool_20250305(options = {}) {
1601
+ return {
1602
+ type: "provider-defined-server",
1603
+ id: "anthropic.web_search_20250305",
1604
+ name: "web_search",
1605
+ args: {
1606
+ ...options.maxUses && { maxUses: options.maxUses },
1607
+ ...options.allowedDomains && { allowedDomains: options.allowedDomains },
1608
+ ...options.blockedDomains && { blockedDomains: options.blockedDomains },
1609
+ ...options.userLocation && { userLocation: options.userLocation }
1610
+ }
1611
+ };
1612
+ }
1580
1613
  var anthropicTools = {
1581
1614
  bash_20241022: bashTool_20241022,
1582
1615
  bash_20250124: bashTool_20250124,
1583
1616
  textEditor_20241022: textEditorTool_20241022,
1584
1617
  textEditor_20250124: textEditorTool_20250124,
1585
1618
  computer_20241022: computerTool_20241022,
1586
- computer_20250124: computerTool_20250124
1619
+ computer_20250124: computerTool_20250124,
1620
+ webSearch_20250305: webSearchTool_20250305
1587
1621
  };
1588
1622
 
1589
1623
  // src/anthropic-provider.ts