@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.mjs CHANGED
@@ -41,13 +41,6 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
41
41
 
42
42
  // src/anthropic-messages-options.ts
43
43
  import { z as z2 } from "zod";
44
- var webSearchLocationSchema = z2.object({
45
- type: z2.literal("approximate"),
46
- city: z2.string().optional(),
47
- region: z2.string().optional(),
48
- country: z2.string(),
49
- timezone: z2.string().optional()
50
- });
51
44
  var anthropicFilePartProviderOptions = z2.object({
52
45
  /**
53
46
  * Citation configuration for this document.
@@ -72,40 +65,10 @@ var anthropicFilePartProviderOptions = z2.object({
72
65
  context: z2.string().optional()
73
66
  });
74
67
  var anthropicProviderOptions = z2.object({
75
- /**
76
- Include reasoning content in requests sent to the model. Defaults to `true`.
77
-
78
- If you are experiencing issues with the model handling requests involving
79
- */
80
68
  sendReasoning: z2.boolean().optional(),
81
69
  thinking: z2.object({
82
70
  type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
83
71
  budgetTokens: z2.number().optional()
84
- }).optional(),
85
- /**
86
- * Web search tool configuration for Claude models that support it.
87
- * When provided, automatically adds the web search tool to the request.
88
- */
89
- webSearch: z2.object({
90
- /**
91
- * Limit the number of searches per request (optional)
92
- * Defaults to 5 if not specified
93
- */
94
- maxUses: z2.number().min(1).max(20).optional(),
95
- /**
96
- * Only include results from these domains (optional)
97
- * Cannot be used with blockedDomains
98
- */
99
- allowedDomains: z2.array(z2.string()).optional(),
100
- /**
101
- * Never include results from these domains (optional)
102
- * Cannot be used with allowedDomains
103
- */
104
- blockedDomains: z2.array(z2.string()).optional(),
105
- /**
106
- * Localize search results based on user location (optional)
107
- */
108
- userLocation: webSearchLocationSchema.optional()
109
72
  }).optional()
110
73
  });
111
74
 
@@ -137,10 +100,10 @@ function prepareTools({
137
100
  anthropicTools2.push({
138
101
  name: tool.name,
139
102
  description: tool.description,
140
- input_schema: tool.parameters
103
+ input_schema: tool.inputSchema
141
104
  });
142
105
  break;
143
- case "provider-defined":
106
+ case "provider-defined-client":
144
107
  switch (tool.id) {
145
108
  case "anthropic.computer_20250124":
146
109
  betas.add("computer-use-2025-01-24");
@@ -195,6 +158,32 @@ function prepareTools({
195
158
  break;
196
159
  }
197
160
  break;
161
+ case "provider-defined-server":
162
+ switch (tool.id) {
163
+ case "anthropic.web_search_20250305":
164
+ const webSearchTool = {
165
+ type: "web_search_20250305",
166
+ name: tool.name
167
+ };
168
+ if (tool.args.maxUses) {
169
+ webSearchTool.max_uses = tool.args.maxUses;
170
+ }
171
+ if (tool.args.allowedDomains) {
172
+ webSearchTool.allowed_domains = tool.args.allowedDomains;
173
+ }
174
+ if (tool.args.blockedDomains) {
175
+ webSearchTool.blocked_domains = tool.args.blockedDomains;
176
+ }
177
+ if (tool.args.userLocation) {
178
+ webSearchTool.user_location = tool.args.userLocation;
179
+ }
180
+ anthropicTools2.push(webSearchTool);
181
+ break;
182
+ default:
183
+ toolWarnings.push({ type: "unsupported-tool", tool });
184
+ break;
185
+ }
186
+ break;
198
187
  default:
199
188
  toolWarnings.push({ type: "unsupported-tool", tool });
200
189
  break;
@@ -247,12 +236,28 @@ import {
247
236
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
248
237
  } from "@ai-sdk/provider";
249
238
  import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
239
+ function convertToString(data) {
240
+ if (typeof data === "string") {
241
+ return Buffer.from(data, "base64").toString("utf-8");
242
+ }
243
+ if (data instanceof Uint8Array) {
244
+ return new TextDecoder().decode(data);
245
+ }
246
+ if (data instanceof URL) {
247
+ throw new UnsupportedFunctionalityError2({
248
+ functionality: "URL-based text documents are not supported for citations"
249
+ });
250
+ }
251
+ throw new UnsupportedFunctionalityError2({
252
+ functionality: `unsupported data type for text documents: ${typeof data}`
253
+ });
254
+ }
250
255
  async function convertToAnthropicMessagesPrompt({
251
256
  prompt,
252
257
  sendReasoning,
253
258
  warnings
254
259
  }) {
255
- var _a, _b, _c, _d;
260
+ var _a, _b, _c, _d, _e;
256
261
  const betas = /* @__PURE__ */ new Set();
257
262
  const blocks = groupIntoBlocks(prompt);
258
263
  let system = void 0;
@@ -359,6 +364,30 @@ async function convertToAnthropicMessagesPrompt({
359
364
  },
360
365
  cache_control: cacheControl
361
366
  });
367
+ } else if (part.mediaType === "text/plain") {
368
+ const enableCitations = await shouldEnableCitations(
369
+ part.providerOptions
370
+ );
371
+ const metadata = await getDocumentMetadata(
372
+ part.providerOptions
373
+ );
374
+ anthropicContent.push({
375
+ type: "document",
376
+ source: part.data instanceof URL ? {
377
+ type: "url",
378
+ url: part.data.toString()
379
+ } : {
380
+ type: "text",
381
+ media_type: "text/plain",
382
+ data: convertToString(part.data)
383
+ },
384
+ title: (_c = metadata.title) != null ? _c : part.filename,
385
+ ...metadata.context && { context: metadata.context },
386
+ ...enableCitations && {
387
+ citations: { enabled: true }
388
+ },
389
+ cache_control: cacheControl
390
+ });
362
391
  } else {
363
392
  throw new UnsupportedFunctionalityError2({
364
393
  functionality: `media type: ${part.mediaType}`
@@ -374,7 +403,7 @@ async function convertToAnthropicMessagesPrompt({
374
403
  for (let i2 = 0; i2 < content.length; i2++) {
375
404
  const part = content[i2];
376
405
  const isLastPart = i2 === content.length - 1;
377
- const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
406
+ const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
378
407
  const toolResultContent = part.content != null ? part.content.map((part2) => {
379
408
  var _a2;
380
409
  switch (part2.type) {
@@ -395,7 +424,7 @@ async function convertToAnthropicMessagesPrompt({
395
424
  cache_control: void 0
396
425
  };
397
426
  }
398
- }) : JSON.stringify(part.result);
427
+ }) : JSON.stringify(part.output);
399
428
  anthropicContent.push({
400
429
  type: "tool_result",
401
430
  tool_use_id: part.toolCallId,
@@ -424,7 +453,7 @@ async function convertToAnthropicMessagesPrompt({
424
453
  for (let k = 0; k < content.length; k++) {
425
454
  const part = content[k];
426
455
  const isLastContentPart = k === content.length - 1;
427
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
456
+ const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
428
457
  switch (part.type) {
429
458
  case "text": {
430
459
  anthropicContent.push({
@@ -485,7 +514,7 @@ async function convertToAnthropicMessagesPrompt({
485
514
  type: "tool_use",
486
515
  id: part.toolCallId,
487
516
  name: part.toolName,
488
- input: part.args,
517
+ input: part.input,
489
518
  cache_control: cacheControl
490
519
  });
491
520
  break;
@@ -573,8 +602,42 @@ function mapAnthropicStopReason({
573
602
  }
574
603
 
575
604
  // src/anthropic-messages-language-model.ts
576
- function processPageLocationCitation(citation, citationDocuments, generateId3, onSource) {
577
- if (citation.type === "page_location") {
605
+ var citationSchemas = {
606
+ webSearchResult: z3.object({
607
+ type: z3.literal("web_search_result_location"),
608
+ cited_text: z3.string(),
609
+ url: z3.string(),
610
+ title: z3.string(),
611
+ encrypted_index: z3.string()
612
+ }),
613
+ pageLocation: z3.object({
614
+ type: z3.literal("page_location"),
615
+ cited_text: z3.string(),
616
+ document_index: z3.number(),
617
+ document_title: z3.string().nullable(),
618
+ start_page_number: z3.number(),
619
+ end_page_number: z3.number()
620
+ }),
621
+ charLocation: z3.object({
622
+ type: z3.literal("char_location"),
623
+ cited_text: z3.string(),
624
+ document_index: z3.number(),
625
+ document_title: z3.string().nullable(),
626
+ start_char_index: z3.number(),
627
+ end_char_index: z3.number()
628
+ })
629
+ };
630
+ var citationSchema = z3.discriminatedUnion("type", [
631
+ citationSchemas.webSearchResult,
632
+ citationSchemas.pageLocation,
633
+ citationSchemas.charLocation
634
+ ]);
635
+ var documentCitationSchema = z3.discriminatedUnion("type", [
636
+ citationSchemas.pageLocation,
637
+ citationSchemas.charLocation
638
+ ]);
639
+ function processCitation(citation, citationDocuments, generateId3, onSource) {
640
+ if (citation.type === "page_location" || citation.type === "char_location") {
578
641
  const source = createCitationSource(
579
642
  citation,
580
643
  citationDocuments,
@@ -591,6 +654,15 @@ function createCitationSource(citation, citationDocuments, generateId3) {
591
654
  if (!documentInfo) {
592
655
  return null;
593
656
  }
657
+ const providerMetadata = citation.type === "page_location" ? {
658
+ citedText: citation.cited_text,
659
+ startPageNumber: citation.start_page_number,
660
+ endPageNumber: citation.end_page_number
661
+ } : {
662
+ citedText: citation.cited_text,
663
+ startCharIndex: citation.start_char_index,
664
+ endCharIndex: citation.end_char_index
665
+ };
594
666
  return {
595
667
  type: "source",
596
668
  sourceType: "document",
@@ -599,11 +671,7 @@ function createCitationSource(citation, citationDocuments, generateId3) {
599
671
  title: (_a = citation.document_title) != null ? _a : documentInfo.title,
600
672
  filename: documentInfo.filename,
601
673
  providerMetadata: {
602
- anthropic: {
603
- citedText: citation.cited_text,
604
- startPageNumber: citation.start_page_number,
605
- endPageNumber: citation.end_page_number
606
- }
674
+ anthropic: providerMetadata
607
675
  }
608
676
  };
609
677
  }
@@ -680,7 +748,7 @@ var AnthropicMessagesLanguageModel = class {
680
748
  type: "function",
681
749
  name: "json",
682
750
  description: "Respond with a JSON object.",
683
- parameters: responseFormat.schema
751
+ inputSchema: responseFormat.schema
684
752
  } : void 0;
685
753
  const anthropicOptions = await parseProviderOptions2({
686
754
  provider: "anthropic",
@@ -743,27 +811,6 @@ var AnthropicMessagesLanguageModel = class {
743
811
  }
744
812
  baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
745
813
  }
746
- let modifiedTools = tools;
747
- let modifiedToolChoice = toolChoice;
748
- if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
749
- const webSearchTool = {
750
- type: "web_search_20250305",
751
- name: "web_search",
752
- max_uses: anthropicOptions.webSearch.maxUses,
753
- allowed_domains: anthropicOptions.webSearch.allowedDomains,
754
- blocked_domains: anthropicOptions.webSearch.blockedDomains,
755
- ...anthropicOptions.webSearch.userLocation && {
756
- user_location: {
757
- type: anthropicOptions.webSearch.userLocation.type,
758
- country: anthropicOptions.webSearch.userLocation.country,
759
- city: anthropicOptions.webSearch.userLocation.city,
760
- region: anthropicOptions.webSearch.userLocation.region,
761
- timezone: anthropicOptions.webSearch.userLocation.timezone
762
- }
763
- }
764
- };
765
- modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
766
- }
767
814
  const {
768
815
  tools: anthropicTools2,
769
816
  toolChoice: anthropicToolChoice,
@@ -773,7 +820,7 @@ var AnthropicMessagesLanguageModel = class {
773
820
  jsonResponseTool != null ? {
774
821
  tools: [jsonResponseTool],
775
822
  toolChoice: { type: "tool", toolName: jsonResponseTool.name }
776
- } : { tools: modifiedTools, toolChoice: modifiedToolChoice }
823
+ } : { tools: tools != null ? tools : [], toolChoice }
777
824
  );
778
825
  return {
779
826
  args: {
@@ -805,15 +852,19 @@ var AnthropicMessagesLanguageModel = class {
805
852
  return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
806
853
  }
807
854
  extractCitationDocuments(prompt) {
808
- const isCitationEnabled = (part) => {
855
+ const isCitationPart = (part) => {
809
856
  var _a, _b;
857
+ if (part.type !== "file") {
858
+ return false;
859
+ }
860
+ if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
861
+ return false;
862
+ }
810
863
  const anthropic2 = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
811
864
  const citationsConfig = anthropic2 == null ? void 0 : anthropic2.citations;
812
865
  return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
813
866
  };
814
- return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
815
- (part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
816
- ).map((part) => {
867
+ return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
817
868
  var _a;
818
869
  const filePart = part;
819
870
  return {
@@ -850,7 +901,7 @@ var AnthropicMessagesLanguageModel = class {
850
901
  content.push({ type: "text", text: part.text });
851
902
  if (part.citations) {
852
903
  for (const citation of part.citations) {
853
- processPageLocationCitation(
904
+ processCitation(
854
905
  citation,
855
906
  citationDocuments,
856
907
  this.generateId,
@@ -896,7 +947,7 @@ var AnthropicMessagesLanguageModel = class {
896
947
  toolCallType: "function",
897
948
  toolCallId: part.id,
898
949
  toolName: part.name,
899
- args: JSON.stringify(part.input)
950
+ input: JSON.stringify(part.input)
900
951
  }
901
952
  );
902
953
  break;
@@ -1088,7 +1139,7 @@ var AnthropicMessagesLanguageModel = class {
1088
1139
  toolCallType: "function",
1089
1140
  toolCallId: contentBlock.toolCallId,
1090
1141
  toolName: contentBlock.toolName,
1091
- args: contentBlock.jsonText
1142
+ input: contentBlock.jsonText
1092
1143
  });
1093
1144
  }
1094
1145
  delete toolCallContentBlocks[value.index];
@@ -1145,7 +1196,7 @@ var AnthropicMessagesLanguageModel = class {
1145
1196
  toolCallType: "function",
1146
1197
  toolCallId: contentBlock.toolCallId,
1147
1198
  toolName: contentBlock.toolName,
1148
- argsTextDelta: value.delta.partial_json
1199
+ inputTextDelta: value.delta.partial_json
1149
1200
  }
1150
1201
  );
1151
1202
  contentBlock.jsonText += value.delta.partial_json;
@@ -1153,7 +1204,7 @@ var AnthropicMessagesLanguageModel = class {
1153
1204
  }
1154
1205
  case "citations_delta": {
1155
1206
  const citation = value.delta.citation;
1156
- processPageLocationCitation(
1207
+ processCitation(
1157
1208
  citation,
1158
1209
  citationDocuments,
1159
1210
  generateId3,
@@ -1228,25 +1279,7 @@ var anthropicMessagesResponseSchema = z3.object({
1228
1279
  z3.object({
1229
1280
  type: z3.literal("text"),
1230
1281
  text: z3.string(),
1231
- citations: z3.array(
1232
- z3.discriminatedUnion("type", [
1233
- z3.object({
1234
- type: z3.literal("web_search_result_location"),
1235
- cited_text: z3.string(),
1236
- url: z3.string(),
1237
- title: z3.string(),
1238
- encrypted_index: z3.string()
1239
- }),
1240
- z3.object({
1241
- type: z3.literal("page_location"),
1242
- cited_text: z3.string(),
1243
- document_index: z3.number(),
1244
- document_title: z3.string().nullable(),
1245
- start_page_number: z3.number(),
1246
- end_page_number: z3.number()
1247
- })
1248
- ])
1249
- ).optional()
1282
+ citations: z3.array(citationSchema).optional()
1250
1283
  }),
1251
1284
  z3.object({
1252
1285
  type: z3.literal("thinking"),
@@ -1385,23 +1418,7 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
1385
1418
  }),
1386
1419
  z3.object({
1387
1420
  type: z3.literal("citations_delta"),
1388
- citation: z3.discriminatedUnion("type", [
1389
- z3.object({
1390
- type: z3.literal("web_search_result_location"),
1391
- cited_text: z3.string(),
1392
- url: z3.string(),
1393
- title: z3.string(),
1394
- encrypted_index: z3.string()
1395
- }),
1396
- z3.object({
1397
- type: z3.literal("page_location"),
1398
- cited_text: z3.string(),
1399
- document_index: z3.number(),
1400
- document_title: z3.string().nullable(),
1401
- start_page_number: z3.number(),
1402
- end_page_number: z3.number()
1403
- })
1404
- ])
1421
+ citation: citationSchema
1405
1422
  })
1406
1423
  ])
1407
1424
  }),
@@ -1441,7 +1458,7 @@ var Bash20241022Parameters = z4.object({
1441
1458
  });
1442
1459
  function bashTool_20241022(options = {}) {
1443
1460
  return {
1444
- type: "provider-defined",
1461
+ type: "provider-defined-client",
1445
1462
  id: "anthropic.bash_20241022",
1446
1463
  args: {},
1447
1464
  parameters: Bash20241022Parameters,
@@ -1455,7 +1472,7 @@ var Bash20250124Parameters = z4.object({
1455
1472
  });
1456
1473
  function bashTool_20250124(options = {}) {
1457
1474
  return {
1458
- type: "provider-defined",
1475
+ type: "provider-defined-client",
1459
1476
  id: "anthropic.bash_20250124",
1460
1477
  args: {},
1461
1478
  parameters: Bash20250124Parameters,
@@ -1474,7 +1491,7 @@ var TextEditor20241022Parameters = z4.object({
1474
1491
  });
1475
1492
  function textEditorTool_20241022(options = {}) {
1476
1493
  return {
1477
- type: "provider-defined",
1494
+ type: "provider-defined-client",
1478
1495
  id: "anthropic.text_editor_20241022",
1479
1496
  args: {},
1480
1497
  parameters: TextEditor20241022Parameters,
@@ -1493,7 +1510,7 @@ var TextEditor20250124Parameters = z4.object({
1493
1510
  });
1494
1511
  function textEditorTool_20250124(options = {}) {
1495
1512
  return {
1496
- type: "provider-defined",
1513
+ type: "provider-defined-client",
1497
1514
  id: "anthropic.text_editor_20250124",
1498
1515
  args: {},
1499
1516
  parameters: TextEditor20250124Parameters,
@@ -1519,7 +1536,7 @@ var Computer20241022Parameters = z4.object({
1519
1536
  });
1520
1537
  function computerTool_20241022(options) {
1521
1538
  return {
1522
- type: "provider-defined",
1539
+ type: "provider-defined-client",
1523
1540
  id: "anthropic.computer_20241022",
1524
1541
  args: {
1525
1542
  displayWidthPx: options.displayWidthPx,
@@ -1559,7 +1576,7 @@ var Computer20250124Parameters = z4.object({
1559
1576
  });
1560
1577
  function computerTool_20250124(options) {
1561
1578
  return {
1562
- type: "provider-defined",
1579
+ type: "provider-defined-client",
1563
1580
  id: "anthropic.computer_20250124",
1564
1581
  args: {
1565
1582
  displayWidthPx: options.displayWidthPx,
@@ -1571,13 +1588,30 @@ function computerTool_20250124(options) {
1571
1588
  experimental_toToolResultContent: options.experimental_toToolResultContent
1572
1589
  };
1573
1590
  }
1591
+ var WebSearch20250305Parameters = z4.object({
1592
+ query: z4.string()
1593
+ });
1594
+ function webSearchTool_20250305(options = {}) {
1595
+ return {
1596
+ type: "provider-defined-server",
1597
+ id: "anthropic.web_search_20250305",
1598
+ name: "web_search",
1599
+ args: {
1600
+ ...options.maxUses && { maxUses: options.maxUses },
1601
+ ...options.allowedDomains && { allowedDomains: options.allowedDomains },
1602
+ ...options.blockedDomains && { blockedDomains: options.blockedDomains },
1603
+ ...options.userLocation && { userLocation: options.userLocation }
1604
+ }
1605
+ };
1606
+ }
1574
1607
  var anthropicTools = {
1575
1608
  bash_20241022: bashTool_20241022,
1576
1609
  bash_20250124: bashTool_20250124,
1577
1610
  textEditor_20241022: textEditorTool_20241022,
1578
1611
  textEditor_20250124: textEditorTool_20250124,
1579
1612
  computer_20241022: computerTool_20241022,
1580
- computer_20250124: computerTool_20250124
1613
+ computer_20250124: computerTool_20250124,
1614
+ webSearch_20250305: webSearchTool_20250305
1581
1615
  };
1582
1616
 
1583
1617
  // src/anthropic-provider.ts