@ai-sdk/anthropic 2.0.0-alpha.1 → 2.0.0-alpha.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.
package/dist/index.mjs CHANGED
@@ -3,18 +3,21 @@ import {
3
3
  NoSuchModelError
4
4
  } from "@ai-sdk/provider";
5
5
  import {
6
+ generateId as generateId2,
6
7
  loadApiKey,
7
8
  withoutTrailingSlash
8
9
  } from "@ai-sdk/provider-utils";
9
10
 
10
11
  // src/anthropic-messages-language-model.ts
11
12
  import {
13
+ APICallError,
12
14
  UnsupportedFunctionalityError as UnsupportedFunctionalityError3
13
15
  } from "@ai-sdk/provider";
14
16
  import {
15
17
  combineHeaders,
16
18
  createEventSourceResponseHandler,
17
19
  createJsonResponseHandler,
20
+ generateId,
18
21
  parseProviderOptions as parseProviderOptions2,
19
22
  postJsonToApi,
20
23
  resolve
@@ -38,6 +41,36 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
38
41
 
39
42
  // src/anthropic-messages-options.ts
40
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
+ var anthropicFilePartProviderOptions = z2.object({
52
+ /**
53
+ * Citation configuration for this document.
54
+ * When enabled, this document will generate citations in the response.
55
+ */
56
+ citations: z2.object({
57
+ /**
58
+ * Enable citations for this document
59
+ */
60
+ enabled: z2.boolean()
61
+ }).optional(),
62
+ /**
63
+ * Custom title for the document.
64
+ * If not provided, the filename will be used.
65
+ */
66
+ title: z2.string().optional(),
67
+ /**
68
+ * Context about the document that will be passed to the model
69
+ * but not used towards cited content.
70
+ * Useful for storing document metadata as text or stringified JSON.
71
+ */
72
+ context: z2.string().optional()
73
+ });
41
74
  var anthropicProviderOptions = z2.object({
42
75
  /**
43
76
  Include reasoning content in requests sent to the model. Defaults to `true`.
@@ -48,6 +81,31 @@ var anthropicProviderOptions = z2.object({
48
81
  thinking: z2.object({
49
82
  type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
50
83
  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()
51
109
  }).optional()
52
110
  });
53
111
 
@@ -55,6 +113,9 @@ var anthropicProviderOptions = z2.object({
55
113
  import {
56
114
  UnsupportedFunctionalityError
57
115
  } from "@ai-sdk/provider";
116
+ function isWebSearchTool(tool) {
117
+ return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
118
+ }
58
119
  function prepareTools({
59
120
  tools,
60
121
  toolChoice
@@ -67,6 +128,10 @@ function prepareTools({
67
128
  }
68
129
  const anthropicTools2 = [];
69
130
  for (const tool of tools) {
131
+ if (isWebSearchTool(tool)) {
132
+ anthropicTools2.push(tool);
133
+ continue;
134
+ }
70
135
  switch (tool.type) {
71
136
  case "function":
72
137
  anthropicTools2.push({
@@ -187,7 +252,7 @@ async function convertToAnthropicMessagesPrompt({
187
252
  sendReasoning,
188
253
  warnings
189
254
  }) {
190
- var _a, _b, _c;
255
+ var _a, _b, _c, _d;
191
256
  const betas = /* @__PURE__ */ new Set();
192
257
  const blocks = groupIntoBlocks(prompt);
193
258
  let system = void 0;
@@ -198,6 +263,26 @@ async function convertToAnthropicMessagesPrompt({
198
263
  const cacheControlValue = (_a2 = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a2 : anthropic2 == null ? void 0 : anthropic2.cache_control;
199
264
  return cacheControlValue;
200
265
  }
266
+ async function shouldEnableCitations(providerMetadata) {
267
+ var _a2, _b2;
268
+ const anthropicOptions = await parseProviderOptions({
269
+ provider: "anthropic",
270
+ providerOptions: providerMetadata,
271
+ schema: anthropicFilePartProviderOptions
272
+ });
273
+ return (_b2 = (_a2 = anthropicOptions == null ? void 0 : anthropicOptions.citations) == null ? void 0 : _a2.enabled) != null ? _b2 : false;
274
+ }
275
+ async function getDocumentMetadata(providerMetadata) {
276
+ const anthropicOptions = await parseProviderOptions({
277
+ provider: "anthropic",
278
+ providerOptions: providerMetadata,
279
+ schema: anthropicFilePartProviderOptions
280
+ });
281
+ return {
282
+ title: anthropicOptions == null ? void 0 : anthropicOptions.title,
283
+ context: anthropicOptions == null ? void 0 : anthropicOptions.context
284
+ };
285
+ }
201
286
  for (let i = 0; i < blocks.length; i++) {
202
287
  const block = blocks[i];
203
288
  const isLastBlock = i === blocks.length - 1;
@@ -251,6 +336,12 @@ async function convertToAnthropicMessagesPrompt({
251
336
  });
252
337
  } else if (part.mediaType === "application/pdf") {
253
338
  betas.add("pdfs-2024-09-25");
339
+ const enableCitations = await shouldEnableCitations(
340
+ part.providerOptions
341
+ );
342
+ const metadata = await getDocumentMetadata(
343
+ part.providerOptions
344
+ );
254
345
  anthropicContent.push({
255
346
  type: "document",
256
347
  source: part.data instanceof URL ? {
@@ -261,6 +352,11 @@ async function convertToAnthropicMessagesPrompt({
261
352
  media_type: "application/pdf",
262
353
  data: convertToBase64(part.data)
263
354
  },
355
+ title: (_b = metadata.title) != null ? _b : part.filename,
356
+ ...metadata.context && { context: metadata.context },
357
+ ...enableCitations && {
358
+ citations: { enabled: true }
359
+ },
264
360
  cache_control: cacheControl
265
361
  });
266
362
  } else {
@@ -278,7 +374,7 @@ async function convertToAnthropicMessagesPrompt({
278
374
  for (let i2 = 0; i2 < content.length; i2++) {
279
375
  const part = content[i2];
280
376
  const isLastPart = i2 === content.length - 1;
281
- const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
377
+ const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
282
378
  const toolResultContent = part.content != null ? part.content.map((part2) => {
283
379
  var _a2;
284
380
  switch (part2.type) {
@@ -328,7 +424,7 @@ async function convertToAnthropicMessagesPrompt({
328
424
  for (let k = 0; k < content.length; k++) {
329
425
  const part = content[k];
330
426
  const isLastContentPart = k === content.length - 1;
331
- const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
427
+ const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
332
428
  switch (part.type) {
333
429
  case "text": {
334
430
  anthropicContent.push({
@@ -459,13 +555,16 @@ function groupIntoBlocks(prompt) {
459
555
  }
460
556
 
461
557
  // src/map-anthropic-stop-reason.ts
462
- function mapAnthropicStopReason(finishReason) {
558
+ function mapAnthropicStopReason({
559
+ finishReason,
560
+ isJsonResponseFromTool
561
+ }) {
463
562
  switch (finishReason) {
464
563
  case "end_turn":
465
564
  case "stop_sequence":
466
565
  return "stop";
467
566
  case "tool_use":
468
- return "tool-calls";
567
+ return isJsonResponseFromTool ? "stop" : "tool-calls";
469
568
  case "max_tokens":
470
569
  return "length";
471
570
  default:
@@ -474,11 +573,47 @@ function mapAnthropicStopReason(finishReason) {
474
573
  }
475
574
 
476
575
  // src/anthropic-messages-language-model.ts
576
+ function processPageLocationCitation(citation, citationDocuments, generateId3, onSource) {
577
+ if (citation.type === "page_location") {
578
+ const source = createCitationSource(
579
+ citation,
580
+ citationDocuments,
581
+ generateId3
582
+ );
583
+ if (source) {
584
+ onSource(source);
585
+ }
586
+ }
587
+ }
588
+ function createCitationSource(citation, citationDocuments, generateId3) {
589
+ var _a;
590
+ const documentInfo = citationDocuments[citation.document_index];
591
+ if (!documentInfo) {
592
+ return null;
593
+ }
594
+ return {
595
+ type: "source",
596
+ sourceType: "document",
597
+ id: generateId3(),
598
+ mediaType: documentInfo.mediaType,
599
+ title: (_a = citation.document_title) != null ? _a : documentInfo.title,
600
+ filename: documentInfo.filename,
601
+ providerMetadata: {
602
+ anthropic: {
603
+ citedText: citation.cited_text,
604
+ startPageNumber: citation.start_page_number,
605
+ endPageNumber: citation.end_page_number
606
+ }
607
+ }
608
+ };
609
+ }
477
610
  var AnthropicMessagesLanguageModel = class {
478
611
  constructor(modelId, config) {
479
612
  this.specificationVersion = "v2";
613
+ var _a;
480
614
  this.modelId = modelId;
481
615
  this.config = config;
616
+ this.generateId = (_a = config.generateId) != null ? _a : generateId;
482
617
  }
483
618
  supportsUrl(url) {
484
619
  return url.protocol === "https:";
@@ -526,13 +661,27 @@ var AnthropicMessagesLanguageModel = class {
526
661
  setting: "seed"
527
662
  });
528
663
  }
529
- if (responseFormat != null && responseFormat.type !== "text") {
530
- warnings.push({
531
- type: "unsupported-setting",
532
- setting: "responseFormat",
533
- details: "JSON response format is not supported."
534
- });
664
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
665
+ if (responseFormat.schema == null) {
666
+ warnings.push({
667
+ type: "unsupported-setting",
668
+ setting: "responseFormat",
669
+ details: "JSON response format requires a schema. The response format is ignored."
670
+ });
671
+ } else if (tools != null) {
672
+ warnings.push({
673
+ type: "unsupported-setting",
674
+ setting: "tools",
675
+ details: "JSON response format does not support tools. The provided tools are ignored."
676
+ });
677
+ }
535
678
  }
679
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
680
+ type: "function",
681
+ name: "json",
682
+ description: "Respond with a JSON object.",
683
+ parameters: responseFormat.schema
684
+ } : void 0;
536
685
  const anthropicOptions = await parseProviderOptions2({
537
686
  provider: "anthropic",
538
687
  providerOptions,
@@ -594,12 +743,38 @@ var AnthropicMessagesLanguageModel = class {
594
743
  }
595
744
  baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
596
745
  }
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
+ }
597
767
  const {
598
768
  tools: anthropicTools2,
599
769
  toolChoice: anthropicToolChoice,
600
770
  toolWarnings,
601
771
  betas: toolsBetas
602
- } = prepareTools({ tools, toolChoice });
772
+ } = prepareTools(
773
+ jsonResponseTool != null ? {
774
+ tools: [jsonResponseTool],
775
+ toolChoice: { type: "tool", toolName: jsonResponseTool.name }
776
+ } : { tools: modifiedTools, toolChoice: modifiedToolChoice }
777
+ );
603
778
  return {
604
779
  args: {
605
780
  ...baseArgs,
@@ -607,7 +782,8 @@ var AnthropicMessagesLanguageModel = class {
607
782
  tool_choice: anthropicToolChoice
608
783
  },
609
784
  warnings: [...warnings, ...toolWarnings],
610
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
785
+ betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
786
+ jsonResponseTool
611
787
  };
612
788
  }
613
789
  async getHeaders({
@@ -628,9 +804,29 @@ var AnthropicMessagesLanguageModel = class {
628
804
  var _a, _b, _c;
629
805
  return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
630
806
  }
807
+ extractCitationDocuments(prompt) {
808
+ const isCitationEnabled = (part) => {
809
+ var _a, _b;
810
+ const anthropic2 = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
811
+ const citationsConfig = anthropic2 == null ? void 0 : anthropic2.citations;
812
+ return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
813
+ };
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) => {
817
+ var _a;
818
+ const filePart = part;
819
+ return {
820
+ title: (_a = filePart.filename) != null ? _a : "Untitled Document",
821
+ filename: filePart.filename,
822
+ mediaType: filePart.mediaType
823
+ };
824
+ });
825
+ }
631
826
  async doGenerate(options) {
632
- var _a, _b, _c, _d;
633
- const { args, warnings, betas } = await this.getArgs(options);
827
+ var _a, _b, _c, _d, _e;
828
+ const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
829
+ const citationDocuments = this.extractCitationDocuments(options.prompt);
634
830
  const {
635
831
  responseHeaders,
636
832
  value: response,
@@ -650,7 +846,19 @@ var AnthropicMessagesLanguageModel = class {
650
846
  for (const part of response.content) {
651
847
  switch (part.type) {
652
848
  case "text": {
653
- content.push({ type: "text", text: part.text });
849
+ if (jsonResponseTool == null) {
850
+ content.push({ type: "text", text: part.text });
851
+ if (part.citations) {
852
+ for (const citation of part.citations) {
853
+ processPageLocationCitation(
854
+ citation,
855
+ citationDocuments,
856
+ this.generateId,
857
+ (source) => content.push(source)
858
+ );
859
+ }
860
+ }
861
+ }
654
862
  break;
655
863
  }
656
864
  case "thinking": {
@@ -678,43 +886,85 @@ var AnthropicMessagesLanguageModel = class {
678
886
  break;
679
887
  }
680
888
  case "tool_use": {
681
- content.push({
682
- type: "tool-call",
683
- toolCallType: "function",
684
- toolCallId: part.id,
685
- toolName: part.name,
686
- args: JSON.stringify(part.input)
687
- });
889
+ content.push(
890
+ // when a json response tool is used, the tool call becomes the text:
891
+ jsonResponseTool != null ? {
892
+ type: "text",
893
+ text: JSON.stringify(part.input)
894
+ } : {
895
+ type: "tool-call",
896
+ toolCallType: "function",
897
+ toolCallId: part.id,
898
+ toolName: part.name,
899
+ args: JSON.stringify(part.input)
900
+ }
901
+ );
902
+ break;
903
+ }
904
+ case "server_tool_use": {
905
+ continue;
906
+ }
907
+ case "web_search_tool_result": {
908
+ if (Array.isArray(part.content)) {
909
+ for (const result of part.content) {
910
+ if (result.type === "web_search_result") {
911
+ content.push({
912
+ type: "source",
913
+ sourceType: "url",
914
+ id: this.generateId(),
915
+ url: result.url,
916
+ title: result.title,
917
+ providerMetadata: {
918
+ anthropic: {
919
+ encryptedContent: result.encrypted_content,
920
+ pageAge: (_a = result.page_age) != null ? _a : null
921
+ }
922
+ }
923
+ });
924
+ }
925
+ }
926
+ } else if (part.content.type === "web_search_tool_result_error") {
927
+ throw new APICallError({
928
+ message: `Web search failed: ${part.content.error_code}`,
929
+ url: "web_search_api",
930
+ requestBodyValues: { tool_use_id: part.tool_use_id },
931
+ data: { error_code: part.content.error_code }
932
+ });
933
+ }
688
934
  break;
689
935
  }
690
936
  }
691
937
  }
692
938
  return {
693
939
  content,
694
- finishReason: mapAnthropicStopReason(response.stop_reason),
940
+ finishReason: mapAnthropicStopReason({
941
+ finishReason: response.stop_reason,
942
+ isJsonResponseFromTool: jsonResponseTool != null
943
+ }),
695
944
  usage: {
696
945
  inputTokens: response.usage.input_tokens,
697
946
  outputTokens: response.usage.output_tokens,
698
947
  totalTokens: response.usage.input_tokens + response.usage.output_tokens,
699
- cachedInputTokens: (_a = response.usage.cache_read_input_tokens) != null ? _a : void 0
948
+ cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
700
949
  },
701
950
  request: { body: args },
702
951
  response: {
703
- id: (_b = response.id) != null ? _b : void 0,
704
- modelId: (_c = response.model) != null ? _c : void 0,
952
+ id: (_c = response.id) != null ? _c : void 0,
953
+ modelId: (_d = response.model) != null ? _d : void 0,
705
954
  headers: responseHeaders,
706
955
  body: rawResponse
707
956
  },
708
957
  warnings,
709
958
  providerMetadata: {
710
959
  anthropic: {
711
- cacheCreationInputTokens: (_d = response.usage.cache_creation_input_tokens) != null ? _d : null
960
+ cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
712
961
  }
713
962
  }
714
963
  };
715
964
  }
716
965
  async doStream(options) {
717
- const { args, warnings, betas } = await this.getArgs(options);
966
+ const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
967
+ const citationDocuments = this.extractCitationDocuments(options.prompt);
718
968
  const body = { ...args, stream: true };
719
969
  const { responseHeaders, value: response } = await postJsonToApi({
720
970
  url: this.buildRequestUrl(true),
@@ -736,6 +986,8 @@ var AnthropicMessagesLanguageModel = class {
736
986
  const toolCallContentBlocks = {};
737
987
  let providerMetadata = void 0;
738
988
  let blockType = void 0;
989
+ const config = this.config;
990
+ const generateId3 = this.generateId;
739
991
  return {
740
992
  stream: response.pipeThrough(
741
993
  new TransformStream({
@@ -743,7 +995,7 @@ var AnthropicMessagesLanguageModel = class {
743
995
  controller.enqueue({ type: "stream-start", warnings });
744
996
  },
745
997
  transform(chunk, controller) {
746
- var _a, _b, _c, _d, _e, _f;
998
+ var _a, _b, _c, _d, _e, _f, _g;
747
999
  if (!chunk.success) {
748
1000
  controller.enqueue({ type: "error", error: chunk.error });
749
1001
  return;
@@ -782,6 +1034,40 @@ var AnthropicMessagesLanguageModel = class {
782
1034
  };
783
1035
  return;
784
1036
  }
1037
+ case "server_tool_use": {
1038
+ return;
1039
+ }
1040
+ case "web_search_tool_result": {
1041
+ if (Array.isArray(value.content_block.content)) {
1042
+ for (const result of value.content_block.content) {
1043
+ if (result.type === "web_search_result") {
1044
+ controller.enqueue({
1045
+ type: "source",
1046
+ sourceType: "url",
1047
+ id: generateId3(),
1048
+ url: result.url,
1049
+ title: result.title,
1050
+ providerMetadata: {
1051
+ anthropic: {
1052
+ encryptedContent: result.encrypted_content,
1053
+ pageAge: (_a = result.page_age) != null ? _a : null
1054
+ }
1055
+ }
1056
+ });
1057
+ }
1058
+ }
1059
+ } else if (value.content_block.content.type === "web_search_tool_result_error") {
1060
+ controller.enqueue({
1061
+ type: "error",
1062
+ error: {
1063
+ type: "web-search-error",
1064
+ message: `Web search failed: ${value.content_block.content.error_code}`,
1065
+ code: value.content_block.content.error_code
1066
+ }
1067
+ });
1068
+ }
1069
+ return;
1070
+ }
785
1071
  default: {
786
1072
  const _exhaustiveCheck = contentBlockType;
787
1073
  throw new Error(
@@ -793,13 +1079,15 @@ var AnthropicMessagesLanguageModel = class {
793
1079
  case "content_block_stop": {
794
1080
  if (toolCallContentBlocks[value.index] != null) {
795
1081
  const contentBlock = toolCallContentBlocks[value.index];
796
- controller.enqueue({
797
- type: "tool-call",
798
- toolCallType: "function",
799
- toolCallId: contentBlock.toolCallId,
800
- toolName: contentBlock.toolName,
801
- args: contentBlock.jsonText
802
- });
1082
+ if (jsonResponseTool == null) {
1083
+ controller.enqueue({
1084
+ type: "tool-call",
1085
+ toolCallType: "function",
1086
+ toolCallId: contentBlock.toolCallId,
1087
+ toolName: contentBlock.toolName,
1088
+ args: contentBlock.jsonText
1089
+ });
1090
+ }
803
1091
  delete toolCallContentBlocks[value.index];
804
1092
  }
805
1093
  blockType = void 0;
@@ -809,6 +1097,9 @@ var AnthropicMessagesLanguageModel = class {
809
1097
  const deltaType = value.delta.type;
810
1098
  switch (deltaType) {
811
1099
  case "text_delta": {
1100
+ if (jsonResponseTool != null) {
1101
+ return;
1102
+ }
812
1103
  controller.enqueue({
813
1104
  type: "text",
814
1105
  text: value.delta.text
@@ -839,16 +1130,34 @@ var AnthropicMessagesLanguageModel = class {
839
1130
  }
840
1131
  case "input_json_delta": {
841
1132
  const contentBlock = toolCallContentBlocks[value.index];
842
- controller.enqueue({
843
- type: "tool-call-delta",
844
- toolCallType: "function",
845
- toolCallId: contentBlock.toolCallId,
846
- toolName: contentBlock.toolName,
847
- argsTextDelta: value.delta.partial_json
848
- });
1133
+ if (!contentBlock) {
1134
+ return;
1135
+ }
1136
+ controller.enqueue(
1137
+ jsonResponseTool != null ? {
1138
+ type: "text",
1139
+ text: value.delta.partial_json
1140
+ } : {
1141
+ type: "tool-call-delta",
1142
+ toolCallType: "function",
1143
+ toolCallId: contentBlock.toolCallId,
1144
+ toolName: contentBlock.toolName,
1145
+ argsTextDelta: value.delta.partial_json
1146
+ }
1147
+ );
849
1148
  contentBlock.jsonText += value.delta.partial_json;
850
1149
  return;
851
1150
  }
1151
+ case "citations_delta": {
1152
+ const citation = value.delta.citation;
1153
+ processPageLocationCitation(
1154
+ citation,
1155
+ citationDocuments,
1156
+ generateId3,
1157
+ (source) => controller.enqueue(source)
1158
+ );
1159
+ return;
1160
+ }
852
1161
  default: {
853
1162
  const _exhaustiveCheck = deltaType;
854
1163
  throw new Error(
@@ -859,23 +1168,26 @@ var AnthropicMessagesLanguageModel = class {
859
1168
  }
860
1169
  case "message_start": {
861
1170
  usage.inputTokens = value.message.usage.input_tokens;
862
- usage.cachedInputTokens = (_a = value.message.usage.cache_read_input_tokens) != null ? _a : void 0;
1171
+ usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
863
1172
  providerMetadata = {
864
1173
  anthropic: {
865
- cacheCreationInputTokens: (_b = value.message.usage.cache_creation_input_tokens) != null ? _b : null
1174
+ cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
866
1175
  }
867
1176
  };
868
1177
  controller.enqueue({
869
1178
  type: "response-metadata",
870
- id: (_c = value.message.id) != null ? _c : void 0,
871
- modelId: (_d = value.message.model) != null ? _d : void 0
1179
+ id: (_d = value.message.id) != null ? _d : void 0,
1180
+ modelId: (_e = value.message.model) != null ? _e : void 0
872
1181
  });
873
1182
  return;
874
1183
  }
875
1184
  case "message_delta": {
876
1185
  usage.outputTokens = value.usage.output_tokens;
877
- usage.totalTokens = ((_e = usage.inputTokens) != null ? _e : 0) + ((_f = value.usage.output_tokens) != null ? _f : 0);
878
- finishReason = mapAnthropicStopReason(value.delta.stop_reason);
1186
+ usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
1187
+ finishReason = mapAnthropicStopReason({
1188
+ finishReason: value.delta.stop_reason,
1189
+ isJsonResponseFromTool: jsonResponseTool != null
1190
+ });
879
1191
  return;
880
1192
  }
881
1193
  case "message_stop": {
@@ -912,7 +1224,26 @@ var anthropicMessagesResponseSchema = z3.object({
912
1224
  z3.discriminatedUnion("type", [
913
1225
  z3.object({
914
1226
  type: z3.literal("text"),
915
- text: z3.string()
1227
+ text: z3.string(),
1228
+ citations: z3.array(
1229
+ z3.discriminatedUnion("type", [
1230
+ z3.object({
1231
+ type: z3.literal("web_search_result_location"),
1232
+ cited_text: z3.string(),
1233
+ url: z3.string(),
1234
+ title: z3.string(),
1235
+ encrypted_index: z3.string()
1236
+ }),
1237
+ z3.object({
1238
+ type: z3.literal("page_location"),
1239
+ cited_text: z3.string(),
1240
+ document_index: z3.number(),
1241
+ document_title: z3.string().nullable(),
1242
+ start_page_number: z3.number(),
1243
+ end_page_number: z3.number()
1244
+ })
1245
+ ])
1246
+ ).optional()
916
1247
  }),
917
1248
  z3.object({
918
1249
  type: z3.literal("thinking"),
@@ -928,6 +1259,31 @@ var anthropicMessagesResponseSchema = z3.object({
928
1259
  id: z3.string(),
929
1260
  name: z3.string(),
930
1261
  input: z3.unknown()
1262
+ }),
1263
+ z3.object({
1264
+ type: z3.literal("server_tool_use"),
1265
+ id: z3.string(),
1266
+ name: z3.string(),
1267
+ input: z3.record(z3.unknown()).nullish()
1268
+ }),
1269
+ z3.object({
1270
+ type: z3.literal("web_search_tool_result"),
1271
+ tool_use_id: z3.string(),
1272
+ content: z3.union([
1273
+ z3.array(
1274
+ z3.object({
1275
+ type: z3.literal("web_search_result"),
1276
+ url: z3.string(),
1277
+ title: z3.string(),
1278
+ encrypted_content: z3.string(),
1279
+ page_age: z3.string().nullish()
1280
+ })
1281
+ ),
1282
+ z3.object({
1283
+ type: z3.literal("web_search_tool_result_error"),
1284
+ error_code: z3.string()
1285
+ })
1286
+ ])
931
1287
  })
932
1288
  ])
933
1289
  ),
@@ -936,7 +1292,10 @@ var anthropicMessagesResponseSchema = z3.object({
936
1292
  input_tokens: z3.number(),
937
1293
  output_tokens: z3.number(),
938
1294
  cache_creation_input_tokens: z3.number().nullish(),
939
- cache_read_input_tokens: z3.number().nullish()
1295
+ cache_read_input_tokens: z3.number().nullish(),
1296
+ server_tool_use: z3.object({
1297
+ web_search_requests: z3.number()
1298
+ }).nullish()
940
1299
  })
941
1300
  });
942
1301
  var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
@@ -973,6 +1332,31 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
973
1332
  z3.object({
974
1333
  type: z3.literal("redacted_thinking"),
975
1334
  data: z3.string()
1335
+ }),
1336
+ z3.object({
1337
+ type: z3.literal("server_tool_use"),
1338
+ id: z3.string(),
1339
+ name: z3.string(),
1340
+ input: z3.record(z3.unknown()).nullish()
1341
+ }),
1342
+ z3.object({
1343
+ type: z3.literal("web_search_tool_result"),
1344
+ tool_use_id: z3.string(),
1345
+ content: z3.union([
1346
+ z3.array(
1347
+ z3.object({
1348
+ type: z3.literal("web_search_result"),
1349
+ url: z3.string(),
1350
+ title: z3.string(),
1351
+ encrypted_content: z3.string(),
1352
+ page_age: z3.string().nullish()
1353
+ })
1354
+ ),
1355
+ z3.object({
1356
+ type: z3.literal("web_search_tool_result_error"),
1357
+ error_code: z3.string()
1358
+ })
1359
+ ])
976
1360
  })
977
1361
  ])
978
1362
  }),
@@ -995,6 +1379,26 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
995
1379
  z3.object({
996
1380
  type: z3.literal("signature_delta"),
997
1381
  signature: z3.string()
1382
+ }),
1383
+ z3.object({
1384
+ type: z3.literal("citations_delta"),
1385
+ citation: z3.discriminatedUnion("type", [
1386
+ z3.object({
1387
+ type: z3.literal("web_search_result_location"),
1388
+ cited_text: z3.string(),
1389
+ url: z3.string(),
1390
+ title: z3.string(),
1391
+ encrypted_index: z3.string()
1392
+ }),
1393
+ z3.object({
1394
+ type: z3.literal("page_location"),
1395
+ cited_text: z3.string(),
1396
+ document_index: z3.number(),
1397
+ document_title: z3.string().nullable(),
1398
+ start_page_number: z3.number(),
1399
+ end_page_number: z3.number()
1400
+ })
1401
+ ])
998
1402
  })
999
1403
  ])
1000
1404
  }),
@@ -1186,15 +1590,19 @@ function createAnthropic(options = {}) {
1186
1590
  }),
1187
1591
  ...options.headers
1188
1592
  });
1189
- const createChatModel = (modelId) => new AnthropicMessagesLanguageModel(modelId, {
1190
- provider: "anthropic.messages",
1191
- baseURL,
1192
- headers: getHeaders,
1193
- fetch: options.fetch,
1194
- supportedUrls: () => ({
1195
- "image/*": [/^https?:\/\/.*$/]
1196
- })
1197
- });
1593
+ const createChatModel = (modelId) => {
1594
+ var _a2;
1595
+ return new AnthropicMessagesLanguageModel(modelId, {
1596
+ provider: "anthropic.messages",
1597
+ baseURL,
1598
+ headers: getHeaders,
1599
+ fetch: options.fetch,
1600
+ generateId: (_a2 = options.generateId) != null ? _a2 : generateId2,
1601
+ supportedUrls: () => ({
1602
+ "image/*": [/^https?:\/\/.*$/]
1603
+ })
1604
+ });
1605
+ };
1198
1606
  const provider = function(modelId) {
1199
1607
  if (new.target) {
1200
1608
  throw new Error(