@ai-sdk/anthropic 2.0.0-canary.10 → 2.0.0-canary.12

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.
@@ -6,11 +6,11 @@ import {
6
6
  combineHeaders,
7
7
  createEventSourceResponseHandler,
8
8
  createJsonResponseHandler,
9
- parseProviderOptions,
9
+ parseProviderOptions as parseProviderOptions2,
10
10
  postJsonToApi,
11
11
  resolve
12
12
  } from "@ai-sdk/provider-utils";
13
- import { z as z2 } from "zod";
13
+ import { z as z3 } from "zod";
14
14
 
15
15
  // src/anthropic-error.ts
16
16
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
@@ -27,6 +27,21 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
27
27
  errorToMessage: (data) => data.error.message
28
28
  });
29
29
 
30
+ // src/anthropic-messages-options.ts
31
+ import { z as z2 } from "zod";
32
+ var anthropicProviderOptions = z2.object({
33
+ /**
34
+ Include reasoning content in requests sent to the model. Defaults to `true`.
35
+
36
+ If you are experiencing issues with the model handling requests involving
37
+ */
38
+ sendReasoning: z2.boolean().optional(),
39
+ thinking: z2.object({
40
+ type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
41
+ budgetTokens: z2.number().optional()
42
+ }).optional()
43
+ });
44
+
30
45
  // src/anthropic-prepare-tools.ts
31
46
  import {
32
47
  UnsupportedFunctionalityError
@@ -157,8 +172,8 @@ function prepareTools({
157
172
  import {
158
173
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
159
174
  } from "@ai-sdk/provider";
160
- import { convertToBase64 } from "@ai-sdk/provider-utils";
161
- function convertToAnthropicMessagesPrompt({
175
+ import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
176
+ async function convertToAnthropicMessagesPrompt({
162
177
  prompt,
163
178
  sendReasoning,
164
179
  warnings
@@ -321,12 +336,37 @@ function convertToAnthropicMessagesPrompt({
321
336
  }
322
337
  case "reasoning": {
323
338
  if (sendReasoning) {
324
- anthropicContent.push({
325
- type: "thinking",
326
- thinking: part.text,
327
- signature: part.signature,
328
- cache_control: cacheControl
339
+ const reasoningMetadata = await parseProviderOptions({
340
+ provider: "anthropic",
341
+ providerOptions: part.providerOptions,
342
+ schema: anthropicReasoningMetadataSchema
329
343
  });
344
+ if (reasoningMetadata != null) {
345
+ if (reasoningMetadata.signature != null) {
346
+ anthropicContent.push({
347
+ type: "thinking",
348
+ thinking: part.text,
349
+ signature: reasoningMetadata.signature,
350
+ cache_control: cacheControl
351
+ });
352
+ } else if (reasoningMetadata.redactedData != null) {
353
+ anthropicContent.push({
354
+ type: "redacted_thinking",
355
+ data: reasoningMetadata.redactedData,
356
+ cache_control: cacheControl
357
+ });
358
+ } else {
359
+ warnings.push({
360
+ type: "other",
361
+ message: "unsupported reasoning metadata"
362
+ });
363
+ }
364
+ } else {
365
+ warnings.push({
366
+ type: "other",
367
+ message: "unsupported reasoning metadata"
368
+ });
369
+ }
330
370
  } else {
331
371
  warnings.push({
332
372
  type: "other",
@@ -335,14 +375,6 @@ function convertToAnthropicMessagesPrompt({
335
375
  }
336
376
  break;
337
377
  }
338
- case "redacted-reasoning": {
339
- anthropicContent.push({
340
- type: "redacted_thinking",
341
- data: part.data,
342
- cache_control: cacheControl
343
- });
344
- break;
345
- }
346
378
  case "tool-call": {
347
379
  anthropicContent.push({
348
380
  type: "tool_use",
@@ -434,10 +466,9 @@ function mapAnthropicStopReason(finishReason) {
434
466
 
435
467
  // src/anthropic-messages-language-model.ts
436
468
  var AnthropicMessagesLanguageModel = class {
437
- constructor(modelId, settings, config) {
469
+ constructor(modelId, config) {
438
470
  this.specificationVersion = "v2";
439
471
  this.modelId = modelId;
440
- this.settings = settings;
441
472
  this.config = config;
442
473
  }
443
474
  supportsUrl(url) {
@@ -493,15 +524,15 @@ var AnthropicMessagesLanguageModel = class {
493
524
  details: "JSON response format is not supported."
494
525
  });
495
526
  }
496
- const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
497
- prompt,
498
- sendReasoning: (_a = this.settings.sendReasoning) != null ? _a : true,
499
- warnings
500
- });
501
- const anthropicOptions = parseProviderOptions({
527
+ const anthropicOptions = await parseProviderOptions2({
502
528
  provider: "anthropic",
503
529
  providerOptions,
504
- schema: anthropicProviderOptionsSchema
530
+ schema: anthropicProviderOptions
531
+ });
532
+ const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
533
+ prompt,
534
+ sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
535
+ warnings
505
536
  });
506
537
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
507
538
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -616,21 +647,24 @@ var AnthropicMessagesLanguageModel = class {
616
647
  case "thinking": {
617
648
  content.push({
618
649
  type: "reasoning",
619
- reasoningType: "text",
620
- text: part.thinking
621
- });
622
- content.push({
623
- type: "reasoning",
624
- reasoningType: "signature",
625
- signature: part.signature
650
+ text: part.thinking,
651
+ providerMetadata: {
652
+ anthropic: {
653
+ signature: part.signature
654
+ }
655
+ }
626
656
  });
627
657
  break;
628
658
  }
629
659
  case "redacted_thinking": {
630
660
  content.push({
631
661
  type: "reasoning",
632
- reasoningType: "redacted",
633
- data: part.data
662
+ text: "",
663
+ providerMetadata: {
664
+ anthropic: {
665
+ redactedData: part.data
666
+ }
667
+ }
634
668
  });
635
669
  break;
636
670
  }
@@ -719,9 +753,14 @@ var AnthropicMessagesLanguageModel = class {
719
753
  case "redacted_thinking": {
720
754
  controller.enqueue({
721
755
  type: "reasoning",
722
- reasoningType: "redacted",
723
- data: value.content_block.data
756
+ text: "",
757
+ providerMetadata: {
758
+ anthropic: {
759
+ redactedData: value.content_block.data
760
+ }
761
+ }
724
762
  });
763
+ controller.enqueue({ type: "reasoning-part-finish" });
725
764
  return;
726
765
  }
727
766
  case "tool_use": {
@@ -768,7 +807,6 @@ var AnthropicMessagesLanguageModel = class {
768
807
  case "thinking_delta": {
769
808
  controller.enqueue({
770
809
  type: "reasoning",
771
- reasoningType: "text",
772
810
  text: value.delta.thinking
773
811
  });
774
812
  return;
@@ -777,9 +815,14 @@ var AnthropicMessagesLanguageModel = class {
777
815
  if (blockType === "thinking") {
778
816
  controller.enqueue({
779
817
  type: "reasoning",
780
- reasoningType: "signature",
781
- signature: value.delta.signature
818
+ text: "",
819
+ providerMetadata: {
820
+ anthropic: {
821
+ signature: value.delta.signature
822
+ }
823
+ }
782
824
  });
825
+ controller.enqueue({ type: "reasoning-part-finish" });
783
826
  }
784
827
  return;
785
828
  }
@@ -850,135 +893,133 @@ var AnthropicMessagesLanguageModel = class {
850
893
  };
851
894
  }
852
895
  };
853
- var anthropicMessagesResponseSchema = z2.object({
854
- type: z2.literal("message"),
855
- id: z2.string().nullish(),
856
- model: z2.string().nullish(),
857
- content: z2.array(
858
- z2.discriminatedUnion("type", [
859
- z2.object({
860
- type: z2.literal("text"),
861
- text: z2.string()
896
+ var anthropicMessagesResponseSchema = z3.object({
897
+ type: z3.literal("message"),
898
+ id: z3.string().nullish(),
899
+ model: z3.string().nullish(),
900
+ content: z3.array(
901
+ z3.discriminatedUnion("type", [
902
+ z3.object({
903
+ type: z3.literal("text"),
904
+ text: z3.string()
862
905
  }),
863
- z2.object({
864
- type: z2.literal("thinking"),
865
- thinking: z2.string(),
866
- signature: z2.string()
906
+ z3.object({
907
+ type: z3.literal("thinking"),
908
+ thinking: z3.string(),
909
+ signature: z3.string()
867
910
  }),
868
- z2.object({
869
- type: z2.literal("redacted_thinking"),
870
- data: z2.string()
911
+ z3.object({
912
+ type: z3.literal("redacted_thinking"),
913
+ data: z3.string()
871
914
  }),
872
- z2.object({
873
- type: z2.literal("tool_use"),
874
- id: z2.string(),
875
- name: z2.string(),
876
- input: z2.unknown()
915
+ z3.object({
916
+ type: z3.literal("tool_use"),
917
+ id: z3.string(),
918
+ name: z3.string(),
919
+ input: z3.unknown()
877
920
  })
878
921
  ])
879
922
  ),
880
- stop_reason: z2.string().nullish(),
881
- usage: z2.object({
882
- input_tokens: z2.number(),
883
- output_tokens: z2.number(),
884
- cache_creation_input_tokens: z2.number().nullish(),
885
- cache_read_input_tokens: z2.number().nullish()
923
+ stop_reason: z3.string().nullish(),
924
+ usage: z3.object({
925
+ input_tokens: z3.number(),
926
+ output_tokens: z3.number(),
927
+ cache_creation_input_tokens: z3.number().nullish(),
928
+ cache_read_input_tokens: z3.number().nullish()
886
929
  })
887
930
  });
888
- var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
889
- z2.object({
890
- type: z2.literal("message_start"),
891
- message: z2.object({
892
- id: z2.string().nullish(),
893
- model: z2.string().nullish(),
894
- usage: z2.object({
895
- input_tokens: z2.number(),
896
- output_tokens: z2.number(),
897
- cache_creation_input_tokens: z2.number().nullish(),
898
- cache_read_input_tokens: z2.number().nullish()
931
+ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
932
+ z3.object({
933
+ type: z3.literal("message_start"),
934
+ message: z3.object({
935
+ id: z3.string().nullish(),
936
+ model: z3.string().nullish(),
937
+ usage: z3.object({
938
+ input_tokens: z3.number(),
939
+ output_tokens: z3.number(),
940
+ cache_creation_input_tokens: z3.number().nullish(),
941
+ cache_read_input_tokens: z3.number().nullish()
899
942
  })
900
943
  })
901
944
  }),
902
- z2.object({
903
- type: z2.literal("content_block_start"),
904
- index: z2.number(),
905
- content_block: z2.discriminatedUnion("type", [
906
- z2.object({
907
- type: z2.literal("text"),
908
- text: z2.string()
945
+ z3.object({
946
+ type: z3.literal("content_block_start"),
947
+ index: z3.number(),
948
+ content_block: z3.discriminatedUnion("type", [
949
+ z3.object({
950
+ type: z3.literal("text"),
951
+ text: z3.string()
909
952
  }),
910
- z2.object({
911
- type: z2.literal("thinking"),
912
- thinking: z2.string()
953
+ z3.object({
954
+ type: z3.literal("thinking"),
955
+ thinking: z3.string()
913
956
  }),
914
- z2.object({
915
- type: z2.literal("tool_use"),
916
- id: z2.string(),
917
- name: z2.string()
957
+ z3.object({
958
+ type: z3.literal("tool_use"),
959
+ id: z3.string(),
960
+ name: z3.string()
918
961
  }),
919
- z2.object({
920
- type: z2.literal("redacted_thinking"),
921
- data: z2.string()
962
+ z3.object({
963
+ type: z3.literal("redacted_thinking"),
964
+ data: z3.string()
922
965
  })
923
966
  ])
924
967
  }),
925
- z2.object({
926
- type: z2.literal("content_block_delta"),
927
- index: z2.number(),
928
- delta: z2.discriminatedUnion("type", [
929
- z2.object({
930
- type: z2.literal("input_json_delta"),
931
- partial_json: z2.string()
968
+ z3.object({
969
+ type: z3.literal("content_block_delta"),
970
+ index: z3.number(),
971
+ delta: z3.discriminatedUnion("type", [
972
+ z3.object({
973
+ type: z3.literal("input_json_delta"),
974
+ partial_json: z3.string()
932
975
  }),
933
- z2.object({
934
- type: z2.literal("text_delta"),
935
- text: z2.string()
976
+ z3.object({
977
+ type: z3.literal("text_delta"),
978
+ text: z3.string()
936
979
  }),
937
- z2.object({
938
- type: z2.literal("thinking_delta"),
939
- thinking: z2.string()
980
+ z3.object({
981
+ type: z3.literal("thinking_delta"),
982
+ thinking: z3.string()
940
983
  }),
941
- z2.object({
942
- type: z2.literal("signature_delta"),
943
- signature: z2.string()
984
+ z3.object({
985
+ type: z3.literal("signature_delta"),
986
+ signature: z3.string()
944
987
  })
945
988
  ])
946
989
  }),
947
- z2.object({
948
- type: z2.literal("content_block_stop"),
949
- index: z2.number()
990
+ z3.object({
991
+ type: z3.literal("content_block_stop"),
992
+ index: z3.number()
950
993
  }),
951
- z2.object({
952
- type: z2.literal("error"),
953
- error: z2.object({
954
- type: z2.string(),
955
- message: z2.string()
994
+ z3.object({
995
+ type: z3.literal("error"),
996
+ error: z3.object({
997
+ type: z3.string(),
998
+ message: z3.string()
956
999
  })
957
1000
  }),
958
- z2.object({
959
- type: z2.literal("message_delta"),
960
- delta: z2.object({ stop_reason: z2.string().nullish() }),
961
- usage: z2.object({ output_tokens: z2.number() })
1001
+ z3.object({
1002
+ type: z3.literal("message_delta"),
1003
+ delta: z3.object({ stop_reason: z3.string().nullish() }),
1004
+ usage: z3.object({ output_tokens: z3.number() })
962
1005
  }),
963
- z2.object({
964
- type: z2.literal("message_stop")
1006
+ z3.object({
1007
+ type: z3.literal("message_stop")
965
1008
  }),
966
- z2.object({
967
- type: z2.literal("ping")
1009
+ z3.object({
1010
+ type: z3.literal("ping")
968
1011
  })
969
1012
  ]);
970
- var anthropicProviderOptionsSchema = z2.object({
971
- thinking: z2.object({
972
- type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
973
- budgetTokens: z2.number().optional()
974
- }).optional()
1013
+ var anthropicReasoningMetadataSchema = z3.object({
1014
+ signature: z3.string().optional(),
1015
+ redactedData: z3.string().optional()
975
1016
  });
976
1017
 
977
1018
  // src/anthropic-tools.ts
978
- import { z as z3 } from "zod";
979
- var Bash20241022Parameters = z3.object({
980
- command: z3.string(),
981
- restart: z3.boolean().optional()
1019
+ import { z as z4 } from "zod";
1020
+ var Bash20241022Parameters = z4.object({
1021
+ command: z4.string(),
1022
+ restart: z4.boolean().optional()
982
1023
  });
983
1024
  function bashTool_20241022(options = {}) {
984
1025
  return {
@@ -990,9 +1031,9 @@ function bashTool_20241022(options = {}) {
990
1031
  experimental_toToolResultContent: options.experimental_toToolResultContent
991
1032
  };
992
1033
  }
993
- var Bash20250124Parameters = z3.object({
994
- command: z3.string(),
995
- restart: z3.boolean().optional()
1034
+ var Bash20250124Parameters = z4.object({
1035
+ command: z4.string(),
1036
+ restart: z4.boolean().optional()
996
1037
  });
997
1038
  function bashTool_20250124(options = {}) {
998
1039
  return {
@@ -1004,14 +1045,14 @@ function bashTool_20250124(options = {}) {
1004
1045
  experimental_toToolResultContent: options.experimental_toToolResultContent
1005
1046
  };
1006
1047
  }
1007
- var TextEditor20241022Parameters = z3.object({
1008
- command: z3.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1009
- path: z3.string(),
1010
- file_text: z3.string().optional(),
1011
- insert_line: z3.number().int().optional(),
1012
- new_str: z3.string().optional(),
1013
- old_str: z3.string().optional(),
1014
- view_range: z3.array(z3.number().int()).optional()
1048
+ var TextEditor20241022Parameters = z4.object({
1049
+ command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1050
+ path: z4.string(),
1051
+ file_text: z4.string().optional(),
1052
+ insert_line: z4.number().int().optional(),
1053
+ new_str: z4.string().optional(),
1054
+ old_str: z4.string().optional(),
1055
+ view_range: z4.array(z4.number().int()).optional()
1015
1056
  });
1016
1057
  function textEditorTool_20241022(options = {}) {
1017
1058
  return {
@@ -1023,14 +1064,14 @@ function textEditorTool_20241022(options = {}) {
1023
1064
  experimental_toToolResultContent: options.experimental_toToolResultContent
1024
1065
  };
1025
1066
  }
1026
- var TextEditor20250124Parameters = z3.object({
1027
- command: z3.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1028
- path: z3.string(),
1029
- file_text: z3.string().optional(),
1030
- insert_line: z3.number().int().optional(),
1031
- new_str: z3.string().optional(),
1032
- old_str: z3.string().optional(),
1033
- view_range: z3.array(z3.number().int()).optional()
1067
+ var TextEditor20250124Parameters = z4.object({
1068
+ command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1069
+ path: z4.string(),
1070
+ file_text: z4.string().optional(),
1071
+ insert_line: z4.number().int().optional(),
1072
+ new_str: z4.string().optional(),
1073
+ old_str: z4.string().optional(),
1074
+ view_range: z4.array(z4.number().int()).optional()
1034
1075
  });
1035
1076
  function textEditorTool_20250124(options = {}) {
1036
1077
  return {
@@ -1042,8 +1083,8 @@ function textEditorTool_20250124(options = {}) {
1042
1083
  experimental_toToolResultContent: options.experimental_toToolResultContent
1043
1084
  };
1044
1085
  }
1045
- var Computer20241022Parameters = z3.object({
1046
- action: z3.enum([
1086
+ var Computer20241022Parameters = z4.object({
1087
+ action: z4.enum([
1047
1088
  "key",
1048
1089
  "type",
1049
1090
  "mouse_move",
@@ -1055,8 +1096,8 @@ var Computer20241022Parameters = z3.object({
1055
1096
  "screenshot",
1056
1097
  "cursor_position"
1057
1098
  ]),
1058
- coordinate: z3.array(z3.number().int()).optional(),
1059
- text: z3.string().optional()
1099
+ coordinate: z4.array(z4.number().int()).optional(),
1100
+ text: z4.string().optional()
1060
1101
  });
1061
1102
  function computerTool_20241022(options) {
1062
1103
  return {
@@ -1072,8 +1113,8 @@ function computerTool_20241022(options) {
1072
1113
  experimental_toToolResultContent: options.experimental_toToolResultContent
1073
1114
  };
1074
1115
  }
1075
- var Computer20250124Parameters = z3.object({
1076
- action: z3.enum([
1116
+ var Computer20250124Parameters = z4.object({
1117
+ action: z4.enum([
1077
1118
  "key",
1078
1119
  "hold_key",
1079
1120
  "type",
@@ -1091,12 +1132,12 @@ var Computer20250124Parameters = z3.object({
1091
1132
  "wait",
1092
1133
  "screenshot"
1093
1134
  ]),
1094
- coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1095
- duration: z3.number().optional(),
1096
- scroll_amount: z3.number().optional(),
1097
- scroll_direction: z3.enum(["up", "down", "left", "right"]).optional(),
1098
- start_coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1099
- text: z3.string().optional()
1135
+ coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
1136
+ duration: z4.number().optional(),
1137
+ scroll_amount: z4.number().optional(),
1138
+ scroll_direction: z4.enum(["up", "down", "left", "right"]).optional(),
1139
+ start_coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
1140
+ text: z4.string().optional()
1100
1141
  });
1101
1142
  function computerTool_20250124(options) {
1102
1143
  return {