@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.
@@ -31,13 +31,6 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
31
31
 
32
32
  // src/anthropic-messages-options.ts
33
33
  import { z as z2 } from "zod";
34
- var webSearchLocationSchema = z2.object({
35
- type: z2.literal("approximate"),
36
- city: z2.string().optional(),
37
- region: z2.string().optional(),
38
- country: z2.string(),
39
- timezone: z2.string().optional()
40
- });
41
34
  var anthropicFilePartProviderOptions = z2.object({
42
35
  /**
43
36
  * Citation configuration for this document.
@@ -62,40 +55,10 @@ var anthropicFilePartProviderOptions = z2.object({
62
55
  context: z2.string().optional()
63
56
  });
64
57
  var anthropicProviderOptions = z2.object({
65
- /**
66
- Include reasoning content in requests sent to the model. Defaults to `true`.
67
-
68
- If you are experiencing issues with the model handling requests involving
69
- */
70
58
  sendReasoning: z2.boolean().optional(),
71
59
  thinking: z2.object({
72
60
  type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
73
61
  budgetTokens: z2.number().optional()
74
- }).optional(),
75
- /**
76
- * Web search tool configuration for Claude models that support it.
77
- * When provided, automatically adds the web search tool to the request.
78
- */
79
- webSearch: z2.object({
80
- /**
81
- * Limit the number of searches per request (optional)
82
- * Defaults to 5 if not specified
83
- */
84
- maxUses: z2.number().min(1).max(20).optional(),
85
- /**
86
- * Only include results from these domains (optional)
87
- * Cannot be used with blockedDomains
88
- */
89
- allowedDomains: z2.array(z2.string()).optional(),
90
- /**
91
- * Never include results from these domains (optional)
92
- * Cannot be used with allowedDomains
93
- */
94
- blockedDomains: z2.array(z2.string()).optional(),
95
- /**
96
- * Localize search results based on user location (optional)
97
- */
98
- userLocation: webSearchLocationSchema.optional()
99
62
  }).optional()
100
63
  });
101
64
 
@@ -127,10 +90,10 @@ function prepareTools({
127
90
  anthropicTools2.push({
128
91
  name: tool.name,
129
92
  description: tool.description,
130
- input_schema: tool.parameters
93
+ input_schema: tool.inputSchema
131
94
  });
132
95
  break;
133
- case "provider-defined":
96
+ case "provider-defined-client":
134
97
  switch (tool.id) {
135
98
  case "anthropic.computer_20250124":
136
99
  betas.add("computer-use-2025-01-24");
@@ -185,6 +148,32 @@ function prepareTools({
185
148
  break;
186
149
  }
187
150
  break;
151
+ case "provider-defined-server":
152
+ switch (tool.id) {
153
+ case "anthropic.web_search_20250305":
154
+ const webSearchTool = {
155
+ type: "web_search_20250305",
156
+ name: tool.name
157
+ };
158
+ if (tool.args.maxUses) {
159
+ webSearchTool.max_uses = tool.args.maxUses;
160
+ }
161
+ if (tool.args.allowedDomains) {
162
+ webSearchTool.allowed_domains = tool.args.allowedDomains;
163
+ }
164
+ if (tool.args.blockedDomains) {
165
+ webSearchTool.blocked_domains = tool.args.blockedDomains;
166
+ }
167
+ if (tool.args.userLocation) {
168
+ webSearchTool.user_location = tool.args.userLocation;
169
+ }
170
+ anthropicTools2.push(webSearchTool);
171
+ break;
172
+ default:
173
+ toolWarnings.push({ type: "unsupported-tool", tool });
174
+ break;
175
+ }
176
+ break;
188
177
  default:
189
178
  toolWarnings.push({ type: "unsupported-tool", tool });
190
179
  break;
@@ -237,12 +226,28 @@ import {
237
226
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
238
227
  } from "@ai-sdk/provider";
239
228
  import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
229
+ function convertToString(data) {
230
+ if (typeof data === "string") {
231
+ return Buffer.from(data, "base64").toString("utf-8");
232
+ }
233
+ if (data instanceof Uint8Array) {
234
+ return new TextDecoder().decode(data);
235
+ }
236
+ if (data instanceof URL) {
237
+ throw new UnsupportedFunctionalityError2({
238
+ functionality: "URL-based text documents are not supported for citations"
239
+ });
240
+ }
241
+ throw new UnsupportedFunctionalityError2({
242
+ functionality: `unsupported data type for text documents: ${typeof data}`
243
+ });
244
+ }
240
245
  async function convertToAnthropicMessagesPrompt({
241
246
  prompt,
242
247
  sendReasoning,
243
248
  warnings
244
249
  }) {
245
- var _a, _b, _c, _d;
250
+ var _a, _b, _c, _d, _e;
246
251
  const betas = /* @__PURE__ */ new Set();
247
252
  const blocks = groupIntoBlocks(prompt);
248
253
  let system = void 0;
@@ -349,6 +354,30 @@ async function convertToAnthropicMessagesPrompt({
349
354
  },
350
355
  cache_control: cacheControl
351
356
  });
357
+ } else if (part.mediaType === "text/plain") {
358
+ const enableCitations = await shouldEnableCitations(
359
+ part.providerOptions
360
+ );
361
+ const metadata = await getDocumentMetadata(
362
+ part.providerOptions
363
+ );
364
+ anthropicContent.push({
365
+ type: "document",
366
+ source: part.data instanceof URL ? {
367
+ type: "url",
368
+ url: part.data.toString()
369
+ } : {
370
+ type: "text",
371
+ media_type: "text/plain",
372
+ data: convertToString(part.data)
373
+ },
374
+ title: (_c = metadata.title) != null ? _c : part.filename,
375
+ ...metadata.context && { context: metadata.context },
376
+ ...enableCitations && {
377
+ citations: { enabled: true }
378
+ },
379
+ cache_control: cacheControl
380
+ });
352
381
  } else {
353
382
  throw new UnsupportedFunctionalityError2({
354
383
  functionality: `media type: ${part.mediaType}`
@@ -364,7 +393,7 @@ async function convertToAnthropicMessagesPrompt({
364
393
  for (let i2 = 0; i2 < content.length; i2++) {
365
394
  const part = content[i2];
366
395
  const isLastPart = i2 === content.length - 1;
367
- const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
396
+ const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
368
397
  const toolResultContent = part.content != null ? part.content.map((part2) => {
369
398
  var _a2;
370
399
  switch (part2.type) {
@@ -385,7 +414,7 @@ async function convertToAnthropicMessagesPrompt({
385
414
  cache_control: void 0
386
415
  };
387
416
  }
388
- }) : JSON.stringify(part.result);
417
+ }) : JSON.stringify(part.output);
389
418
  anthropicContent.push({
390
419
  type: "tool_result",
391
420
  tool_use_id: part.toolCallId,
@@ -414,7 +443,7 @@ async function convertToAnthropicMessagesPrompt({
414
443
  for (let k = 0; k < content.length; k++) {
415
444
  const part = content[k];
416
445
  const isLastContentPart = k === content.length - 1;
417
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
446
+ const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
418
447
  switch (part.type) {
419
448
  case "text": {
420
449
  anthropicContent.push({
@@ -475,7 +504,7 @@ async function convertToAnthropicMessagesPrompt({
475
504
  type: "tool_use",
476
505
  id: part.toolCallId,
477
506
  name: part.toolName,
478
- input: part.args,
507
+ input: part.input,
479
508
  cache_control: cacheControl
480
509
  });
481
510
  break;
@@ -563,8 +592,42 @@ function mapAnthropicStopReason({
563
592
  }
564
593
 
565
594
  // src/anthropic-messages-language-model.ts
566
- function processPageLocationCitation(citation, citationDocuments, generateId2, onSource) {
567
- if (citation.type === "page_location") {
595
+ var citationSchemas = {
596
+ webSearchResult: z3.object({
597
+ type: z3.literal("web_search_result_location"),
598
+ cited_text: z3.string(),
599
+ url: z3.string(),
600
+ title: z3.string(),
601
+ encrypted_index: z3.string()
602
+ }),
603
+ pageLocation: z3.object({
604
+ type: z3.literal("page_location"),
605
+ cited_text: z3.string(),
606
+ document_index: z3.number(),
607
+ document_title: z3.string().nullable(),
608
+ start_page_number: z3.number(),
609
+ end_page_number: z3.number()
610
+ }),
611
+ charLocation: z3.object({
612
+ type: z3.literal("char_location"),
613
+ cited_text: z3.string(),
614
+ document_index: z3.number(),
615
+ document_title: z3.string().nullable(),
616
+ start_char_index: z3.number(),
617
+ end_char_index: z3.number()
618
+ })
619
+ };
620
+ var citationSchema = z3.discriminatedUnion("type", [
621
+ citationSchemas.webSearchResult,
622
+ citationSchemas.pageLocation,
623
+ citationSchemas.charLocation
624
+ ]);
625
+ var documentCitationSchema = z3.discriminatedUnion("type", [
626
+ citationSchemas.pageLocation,
627
+ citationSchemas.charLocation
628
+ ]);
629
+ function processCitation(citation, citationDocuments, generateId2, onSource) {
630
+ if (citation.type === "page_location" || citation.type === "char_location") {
568
631
  const source = createCitationSource(
569
632
  citation,
570
633
  citationDocuments,
@@ -581,6 +644,15 @@ function createCitationSource(citation, citationDocuments, generateId2) {
581
644
  if (!documentInfo) {
582
645
  return null;
583
646
  }
647
+ const providerMetadata = citation.type === "page_location" ? {
648
+ citedText: citation.cited_text,
649
+ startPageNumber: citation.start_page_number,
650
+ endPageNumber: citation.end_page_number
651
+ } : {
652
+ citedText: citation.cited_text,
653
+ startCharIndex: citation.start_char_index,
654
+ endCharIndex: citation.end_char_index
655
+ };
584
656
  return {
585
657
  type: "source",
586
658
  sourceType: "document",
@@ -589,11 +661,7 @@ function createCitationSource(citation, citationDocuments, generateId2) {
589
661
  title: (_a = citation.document_title) != null ? _a : documentInfo.title,
590
662
  filename: documentInfo.filename,
591
663
  providerMetadata: {
592
- anthropic: {
593
- citedText: citation.cited_text,
594
- startPageNumber: citation.start_page_number,
595
- endPageNumber: citation.end_page_number
596
- }
664
+ anthropic: providerMetadata
597
665
  }
598
666
  };
599
667
  }
@@ -670,7 +738,7 @@ var AnthropicMessagesLanguageModel = class {
670
738
  type: "function",
671
739
  name: "json",
672
740
  description: "Respond with a JSON object.",
673
- parameters: responseFormat.schema
741
+ inputSchema: responseFormat.schema
674
742
  } : void 0;
675
743
  const anthropicOptions = await parseProviderOptions2({
676
744
  provider: "anthropic",
@@ -733,27 +801,6 @@ var AnthropicMessagesLanguageModel = class {
733
801
  }
734
802
  baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
735
803
  }
736
- let modifiedTools = tools;
737
- let modifiedToolChoice = toolChoice;
738
- if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
739
- const webSearchTool = {
740
- type: "web_search_20250305",
741
- name: "web_search",
742
- max_uses: anthropicOptions.webSearch.maxUses,
743
- allowed_domains: anthropicOptions.webSearch.allowedDomains,
744
- blocked_domains: anthropicOptions.webSearch.blockedDomains,
745
- ...anthropicOptions.webSearch.userLocation && {
746
- user_location: {
747
- type: anthropicOptions.webSearch.userLocation.type,
748
- country: anthropicOptions.webSearch.userLocation.country,
749
- city: anthropicOptions.webSearch.userLocation.city,
750
- region: anthropicOptions.webSearch.userLocation.region,
751
- timezone: anthropicOptions.webSearch.userLocation.timezone
752
- }
753
- }
754
- };
755
- modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
756
- }
757
804
  const {
758
805
  tools: anthropicTools2,
759
806
  toolChoice: anthropicToolChoice,
@@ -763,7 +810,7 @@ var AnthropicMessagesLanguageModel = class {
763
810
  jsonResponseTool != null ? {
764
811
  tools: [jsonResponseTool],
765
812
  toolChoice: { type: "tool", toolName: jsonResponseTool.name }
766
- } : { tools: modifiedTools, toolChoice: modifiedToolChoice }
813
+ } : { tools: tools != null ? tools : [], toolChoice }
767
814
  );
768
815
  return {
769
816
  args: {
@@ -795,15 +842,19 @@ var AnthropicMessagesLanguageModel = class {
795
842
  return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
796
843
  }
797
844
  extractCitationDocuments(prompt) {
798
- const isCitationEnabled = (part) => {
845
+ const isCitationPart = (part) => {
799
846
  var _a, _b;
847
+ if (part.type !== "file") {
848
+ return false;
849
+ }
850
+ if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
851
+ return false;
852
+ }
800
853
  const anthropic = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
801
854
  const citationsConfig = anthropic == null ? void 0 : anthropic.citations;
802
855
  return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
803
856
  };
804
- return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
805
- (part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
806
- ).map((part) => {
857
+ return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
807
858
  var _a;
808
859
  const filePart = part;
809
860
  return {
@@ -840,7 +891,7 @@ var AnthropicMessagesLanguageModel = class {
840
891
  content.push({ type: "text", text: part.text });
841
892
  if (part.citations) {
842
893
  for (const citation of part.citations) {
843
- processPageLocationCitation(
894
+ processCitation(
844
895
  citation,
845
896
  citationDocuments,
846
897
  this.generateId,
@@ -886,7 +937,7 @@ var AnthropicMessagesLanguageModel = class {
886
937
  toolCallType: "function",
887
938
  toolCallId: part.id,
888
939
  toolName: part.name,
889
- args: JSON.stringify(part.input)
940
+ input: JSON.stringify(part.input)
890
941
  }
891
942
  );
892
943
  break;
@@ -1078,7 +1129,7 @@ var AnthropicMessagesLanguageModel = class {
1078
1129
  toolCallType: "function",
1079
1130
  toolCallId: contentBlock.toolCallId,
1080
1131
  toolName: contentBlock.toolName,
1081
- args: contentBlock.jsonText
1132
+ input: contentBlock.jsonText
1082
1133
  });
1083
1134
  }
1084
1135
  delete toolCallContentBlocks[value.index];
@@ -1135,7 +1186,7 @@ var AnthropicMessagesLanguageModel = class {
1135
1186
  toolCallType: "function",
1136
1187
  toolCallId: contentBlock.toolCallId,
1137
1188
  toolName: contentBlock.toolName,
1138
- argsTextDelta: value.delta.partial_json
1189
+ inputTextDelta: value.delta.partial_json
1139
1190
  }
1140
1191
  );
1141
1192
  contentBlock.jsonText += value.delta.partial_json;
@@ -1143,7 +1194,7 @@ var AnthropicMessagesLanguageModel = class {
1143
1194
  }
1144
1195
  case "citations_delta": {
1145
1196
  const citation = value.delta.citation;
1146
- processPageLocationCitation(
1197
+ processCitation(
1147
1198
  citation,
1148
1199
  citationDocuments,
1149
1200
  generateId2,
@@ -1218,25 +1269,7 @@ var anthropicMessagesResponseSchema = z3.object({
1218
1269
  z3.object({
1219
1270
  type: z3.literal("text"),
1220
1271
  text: z3.string(),
1221
- citations: z3.array(
1222
- z3.discriminatedUnion("type", [
1223
- z3.object({
1224
- type: z3.literal("web_search_result_location"),
1225
- cited_text: z3.string(),
1226
- url: z3.string(),
1227
- title: z3.string(),
1228
- encrypted_index: z3.string()
1229
- }),
1230
- z3.object({
1231
- type: z3.literal("page_location"),
1232
- cited_text: z3.string(),
1233
- document_index: z3.number(),
1234
- document_title: z3.string().nullable(),
1235
- start_page_number: z3.number(),
1236
- end_page_number: z3.number()
1237
- })
1238
- ])
1239
- ).optional()
1272
+ citations: z3.array(citationSchema).optional()
1240
1273
  }),
1241
1274
  z3.object({
1242
1275
  type: z3.literal("thinking"),
@@ -1375,23 +1408,7 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
1375
1408
  }),
1376
1409
  z3.object({
1377
1410
  type: z3.literal("citations_delta"),
1378
- citation: z3.discriminatedUnion("type", [
1379
- z3.object({
1380
- type: z3.literal("web_search_result_location"),
1381
- cited_text: z3.string(),
1382
- url: z3.string(),
1383
- title: z3.string(),
1384
- encrypted_index: z3.string()
1385
- }),
1386
- z3.object({
1387
- type: z3.literal("page_location"),
1388
- cited_text: z3.string(),
1389
- document_index: z3.number(),
1390
- document_title: z3.string().nullable(),
1391
- start_page_number: z3.number(),
1392
- end_page_number: z3.number()
1393
- })
1394
- ])
1411
+ citation: citationSchema
1395
1412
  })
1396
1413
  ])
1397
1414
  }),
@@ -1431,7 +1448,7 @@ var Bash20241022Parameters = z4.object({
1431
1448
  });
1432
1449
  function bashTool_20241022(options = {}) {
1433
1450
  return {
1434
- type: "provider-defined",
1451
+ type: "provider-defined-client",
1435
1452
  id: "anthropic.bash_20241022",
1436
1453
  args: {},
1437
1454
  parameters: Bash20241022Parameters,
@@ -1445,7 +1462,7 @@ var Bash20250124Parameters = z4.object({
1445
1462
  });
1446
1463
  function bashTool_20250124(options = {}) {
1447
1464
  return {
1448
- type: "provider-defined",
1465
+ type: "provider-defined-client",
1449
1466
  id: "anthropic.bash_20250124",
1450
1467
  args: {},
1451
1468
  parameters: Bash20250124Parameters,
@@ -1464,7 +1481,7 @@ var TextEditor20241022Parameters = z4.object({
1464
1481
  });
1465
1482
  function textEditorTool_20241022(options = {}) {
1466
1483
  return {
1467
- type: "provider-defined",
1484
+ type: "provider-defined-client",
1468
1485
  id: "anthropic.text_editor_20241022",
1469
1486
  args: {},
1470
1487
  parameters: TextEditor20241022Parameters,
@@ -1483,7 +1500,7 @@ var TextEditor20250124Parameters = z4.object({
1483
1500
  });
1484
1501
  function textEditorTool_20250124(options = {}) {
1485
1502
  return {
1486
- type: "provider-defined",
1503
+ type: "provider-defined-client",
1487
1504
  id: "anthropic.text_editor_20250124",
1488
1505
  args: {},
1489
1506
  parameters: TextEditor20250124Parameters,
@@ -1509,7 +1526,7 @@ var Computer20241022Parameters = z4.object({
1509
1526
  });
1510
1527
  function computerTool_20241022(options) {
1511
1528
  return {
1512
- type: "provider-defined",
1529
+ type: "provider-defined-client",
1513
1530
  id: "anthropic.computer_20241022",
1514
1531
  args: {
1515
1532
  displayWidthPx: options.displayWidthPx,
@@ -1549,7 +1566,7 @@ var Computer20250124Parameters = z4.object({
1549
1566
  });
1550
1567
  function computerTool_20250124(options) {
1551
1568
  return {
1552
- type: "provider-defined",
1569
+ type: "provider-defined-client",
1553
1570
  id: "anthropic.computer_20250124",
1554
1571
  args: {
1555
1572
  displayWidthPx: options.displayWidthPx,
@@ -1561,13 +1578,30 @@ function computerTool_20250124(options) {
1561
1578
  experimental_toToolResultContent: options.experimental_toToolResultContent
1562
1579
  };
1563
1580
  }
1581
+ var WebSearch20250305Parameters = z4.object({
1582
+ query: z4.string()
1583
+ });
1584
+ function webSearchTool_20250305(options = {}) {
1585
+ return {
1586
+ type: "provider-defined-server",
1587
+ id: "anthropic.web_search_20250305",
1588
+ name: "web_search",
1589
+ args: {
1590
+ ...options.maxUses && { maxUses: options.maxUses },
1591
+ ...options.allowedDomains && { allowedDomains: options.allowedDomains },
1592
+ ...options.blockedDomains && { blockedDomains: options.blockedDomains },
1593
+ ...options.userLocation && { userLocation: options.userLocation }
1594
+ }
1595
+ };
1596
+ }
1564
1597
  var anthropicTools = {
1565
1598
  bash_20241022: bashTool_20241022,
1566
1599
  bash_20250124: bashTool_20250124,
1567
1600
  textEditor_20241022: textEditorTool_20241022,
1568
1601
  textEditor_20250124: textEditorTool_20250124,
1569
1602
  computer_20241022: computerTool_20241022,
1570
- computer_20250124: computerTool_20250124
1603
+ computer_20250124: computerTool_20250124,
1604
+ webSearch_20250305: webSearchTool_20250305
1571
1605
  };
1572
1606
  export {
1573
1607
  AnthropicMessagesLanguageModel,