@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.
package/dist/index.mjs CHANGED
@@ -15,11 +15,11 @@ import {
15
15
  combineHeaders,
16
16
  createEventSourceResponseHandler,
17
17
  createJsonResponseHandler,
18
- parseProviderOptions,
18
+ parseProviderOptions as parseProviderOptions2,
19
19
  postJsonToApi,
20
20
  resolve
21
21
  } from "@ai-sdk/provider-utils";
22
- import { z as z2 } from "zod";
22
+ import { z as z3 } from "zod";
23
23
 
24
24
  // src/anthropic-error.ts
25
25
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
@@ -36,6 +36,21 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
36
36
  errorToMessage: (data) => data.error.message
37
37
  });
38
38
 
39
+ // src/anthropic-messages-options.ts
40
+ import { z as z2 } from "zod";
41
+ var anthropicProviderOptions = z2.object({
42
+ /**
43
+ Include reasoning content in requests sent to the model. Defaults to `true`.
44
+
45
+ If you are experiencing issues with the model handling requests involving
46
+ */
47
+ sendReasoning: z2.boolean().optional(),
48
+ thinking: z2.object({
49
+ type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
50
+ budgetTokens: z2.number().optional()
51
+ }).optional()
52
+ });
53
+
39
54
  // src/anthropic-prepare-tools.ts
40
55
  import {
41
56
  UnsupportedFunctionalityError
@@ -166,8 +181,8 @@ function prepareTools({
166
181
  import {
167
182
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
168
183
  } from "@ai-sdk/provider";
169
- import { convertToBase64 } from "@ai-sdk/provider-utils";
170
- function convertToAnthropicMessagesPrompt({
184
+ import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
185
+ async function convertToAnthropicMessagesPrompt({
171
186
  prompt,
172
187
  sendReasoning,
173
188
  warnings
@@ -330,12 +345,37 @@ function convertToAnthropicMessagesPrompt({
330
345
  }
331
346
  case "reasoning": {
332
347
  if (sendReasoning) {
333
- anthropicContent.push({
334
- type: "thinking",
335
- thinking: part.text,
336
- signature: part.signature,
337
- cache_control: cacheControl
348
+ const reasoningMetadata = await parseProviderOptions({
349
+ provider: "anthropic",
350
+ providerOptions: part.providerOptions,
351
+ schema: anthropicReasoningMetadataSchema
338
352
  });
353
+ if (reasoningMetadata != null) {
354
+ if (reasoningMetadata.signature != null) {
355
+ anthropicContent.push({
356
+ type: "thinking",
357
+ thinking: part.text,
358
+ signature: reasoningMetadata.signature,
359
+ cache_control: cacheControl
360
+ });
361
+ } else if (reasoningMetadata.redactedData != null) {
362
+ anthropicContent.push({
363
+ type: "redacted_thinking",
364
+ data: reasoningMetadata.redactedData,
365
+ cache_control: cacheControl
366
+ });
367
+ } else {
368
+ warnings.push({
369
+ type: "other",
370
+ message: "unsupported reasoning metadata"
371
+ });
372
+ }
373
+ } else {
374
+ warnings.push({
375
+ type: "other",
376
+ message: "unsupported reasoning metadata"
377
+ });
378
+ }
339
379
  } else {
340
380
  warnings.push({
341
381
  type: "other",
@@ -344,14 +384,6 @@ function convertToAnthropicMessagesPrompt({
344
384
  }
345
385
  break;
346
386
  }
347
- case "redacted-reasoning": {
348
- anthropicContent.push({
349
- type: "redacted_thinking",
350
- data: part.data,
351
- cache_control: cacheControl
352
- });
353
- break;
354
- }
355
387
  case "tool-call": {
356
388
  anthropicContent.push({
357
389
  type: "tool_use",
@@ -443,10 +475,9 @@ function mapAnthropicStopReason(finishReason) {
443
475
 
444
476
  // src/anthropic-messages-language-model.ts
445
477
  var AnthropicMessagesLanguageModel = class {
446
- constructor(modelId, settings, config) {
478
+ constructor(modelId, config) {
447
479
  this.specificationVersion = "v2";
448
480
  this.modelId = modelId;
449
- this.settings = settings;
450
481
  this.config = config;
451
482
  }
452
483
  supportsUrl(url) {
@@ -502,15 +533,15 @@ var AnthropicMessagesLanguageModel = class {
502
533
  details: "JSON response format is not supported."
503
534
  });
504
535
  }
505
- const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
506
- prompt,
507
- sendReasoning: (_a = this.settings.sendReasoning) != null ? _a : true,
508
- warnings
509
- });
510
- const anthropicOptions = parseProviderOptions({
536
+ const anthropicOptions = await parseProviderOptions2({
511
537
  provider: "anthropic",
512
538
  providerOptions,
513
- schema: anthropicProviderOptionsSchema
539
+ schema: anthropicProviderOptions
540
+ });
541
+ const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
542
+ prompt,
543
+ sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
544
+ warnings
514
545
  });
515
546
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
516
547
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -625,21 +656,24 @@ var AnthropicMessagesLanguageModel = class {
625
656
  case "thinking": {
626
657
  content.push({
627
658
  type: "reasoning",
628
- reasoningType: "text",
629
- text: part.thinking
630
- });
631
- content.push({
632
- type: "reasoning",
633
- reasoningType: "signature",
634
- signature: part.signature
659
+ text: part.thinking,
660
+ providerMetadata: {
661
+ anthropic: {
662
+ signature: part.signature
663
+ }
664
+ }
635
665
  });
636
666
  break;
637
667
  }
638
668
  case "redacted_thinking": {
639
669
  content.push({
640
670
  type: "reasoning",
641
- reasoningType: "redacted",
642
- data: part.data
671
+ text: "",
672
+ providerMetadata: {
673
+ anthropic: {
674
+ redactedData: part.data
675
+ }
676
+ }
643
677
  });
644
678
  break;
645
679
  }
@@ -728,9 +762,14 @@ var AnthropicMessagesLanguageModel = class {
728
762
  case "redacted_thinking": {
729
763
  controller.enqueue({
730
764
  type: "reasoning",
731
- reasoningType: "redacted",
732
- data: value.content_block.data
765
+ text: "",
766
+ providerMetadata: {
767
+ anthropic: {
768
+ redactedData: value.content_block.data
769
+ }
770
+ }
733
771
  });
772
+ controller.enqueue({ type: "reasoning-part-finish" });
734
773
  return;
735
774
  }
736
775
  case "tool_use": {
@@ -777,7 +816,6 @@ var AnthropicMessagesLanguageModel = class {
777
816
  case "thinking_delta": {
778
817
  controller.enqueue({
779
818
  type: "reasoning",
780
- reasoningType: "text",
781
819
  text: value.delta.thinking
782
820
  });
783
821
  return;
@@ -786,9 +824,14 @@ var AnthropicMessagesLanguageModel = class {
786
824
  if (blockType === "thinking") {
787
825
  controller.enqueue({
788
826
  type: "reasoning",
789
- reasoningType: "signature",
790
- signature: value.delta.signature
827
+ text: "",
828
+ providerMetadata: {
829
+ anthropic: {
830
+ signature: value.delta.signature
831
+ }
832
+ }
791
833
  });
834
+ controller.enqueue({ type: "reasoning-part-finish" });
792
835
  }
793
836
  return;
794
837
  }
@@ -859,135 +902,133 @@ var AnthropicMessagesLanguageModel = class {
859
902
  };
860
903
  }
861
904
  };
862
- var anthropicMessagesResponseSchema = z2.object({
863
- type: z2.literal("message"),
864
- id: z2.string().nullish(),
865
- model: z2.string().nullish(),
866
- content: z2.array(
867
- z2.discriminatedUnion("type", [
868
- z2.object({
869
- type: z2.literal("text"),
870
- text: z2.string()
905
+ var anthropicMessagesResponseSchema = z3.object({
906
+ type: z3.literal("message"),
907
+ id: z3.string().nullish(),
908
+ model: z3.string().nullish(),
909
+ content: z3.array(
910
+ z3.discriminatedUnion("type", [
911
+ z3.object({
912
+ type: z3.literal("text"),
913
+ text: z3.string()
871
914
  }),
872
- z2.object({
873
- type: z2.literal("thinking"),
874
- thinking: z2.string(),
875
- signature: z2.string()
915
+ z3.object({
916
+ type: z3.literal("thinking"),
917
+ thinking: z3.string(),
918
+ signature: z3.string()
876
919
  }),
877
- z2.object({
878
- type: z2.literal("redacted_thinking"),
879
- data: z2.string()
920
+ z3.object({
921
+ type: z3.literal("redacted_thinking"),
922
+ data: z3.string()
880
923
  }),
881
- z2.object({
882
- type: z2.literal("tool_use"),
883
- id: z2.string(),
884
- name: z2.string(),
885
- input: z2.unknown()
924
+ z3.object({
925
+ type: z3.literal("tool_use"),
926
+ id: z3.string(),
927
+ name: z3.string(),
928
+ input: z3.unknown()
886
929
  })
887
930
  ])
888
931
  ),
889
- stop_reason: z2.string().nullish(),
890
- usage: z2.object({
891
- input_tokens: z2.number(),
892
- output_tokens: z2.number(),
893
- cache_creation_input_tokens: z2.number().nullish(),
894
- cache_read_input_tokens: z2.number().nullish()
932
+ stop_reason: z3.string().nullish(),
933
+ usage: z3.object({
934
+ input_tokens: z3.number(),
935
+ output_tokens: z3.number(),
936
+ cache_creation_input_tokens: z3.number().nullish(),
937
+ cache_read_input_tokens: z3.number().nullish()
895
938
  })
896
939
  });
897
- var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
898
- z2.object({
899
- type: z2.literal("message_start"),
900
- message: z2.object({
901
- id: z2.string().nullish(),
902
- model: z2.string().nullish(),
903
- usage: z2.object({
904
- input_tokens: z2.number(),
905
- output_tokens: z2.number(),
906
- cache_creation_input_tokens: z2.number().nullish(),
907
- cache_read_input_tokens: z2.number().nullish()
940
+ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
941
+ z3.object({
942
+ type: z3.literal("message_start"),
943
+ message: z3.object({
944
+ id: z3.string().nullish(),
945
+ model: z3.string().nullish(),
946
+ usage: z3.object({
947
+ input_tokens: z3.number(),
948
+ output_tokens: z3.number(),
949
+ cache_creation_input_tokens: z3.number().nullish(),
950
+ cache_read_input_tokens: z3.number().nullish()
908
951
  })
909
952
  })
910
953
  }),
911
- z2.object({
912
- type: z2.literal("content_block_start"),
913
- index: z2.number(),
914
- content_block: z2.discriminatedUnion("type", [
915
- z2.object({
916
- type: z2.literal("text"),
917
- text: z2.string()
954
+ z3.object({
955
+ type: z3.literal("content_block_start"),
956
+ index: z3.number(),
957
+ content_block: z3.discriminatedUnion("type", [
958
+ z3.object({
959
+ type: z3.literal("text"),
960
+ text: z3.string()
918
961
  }),
919
- z2.object({
920
- type: z2.literal("thinking"),
921
- thinking: z2.string()
962
+ z3.object({
963
+ type: z3.literal("thinking"),
964
+ thinking: z3.string()
922
965
  }),
923
- z2.object({
924
- type: z2.literal("tool_use"),
925
- id: z2.string(),
926
- name: z2.string()
966
+ z3.object({
967
+ type: z3.literal("tool_use"),
968
+ id: z3.string(),
969
+ name: z3.string()
927
970
  }),
928
- z2.object({
929
- type: z2.literal("redacted_thinking"),
930
- data: z2.string()
971
+ z3.object({
972
+ type: z3.literal("redacted_thinking"),
973
+ data: z3.string()
931
974
  })
932
975
  ])
933
976
  }),
934
- z2.object({
935
- type: z2.literal("content_block_delta"),
936
- index: z2.number(),
937
- delta: z2.discriminatedUnion("type", [
938
- z2.object({
939
- type: z2.literal("input_json_delta"),
940
- partial_json: z2.string()
977
+ z3.object({
978
+ type: z3.literal("content_block_delta"),
979
+ index: z3.number(),
980
+ delta: z3.discriminatedUnion("type", [
981
+ z3.object({
982
+ type: z3.literal("input_json_delta"),
983
+ partial_json: z3.string()
941
984
  }),
942
- z2.object({
943
- type: z2.literal("text_delta"),
944
- text: z2.string()
985
+ z3.object({
986
+ type: z3.literal("text_delta"),
987
+ text: z3.string()
945
988
  }),
946
- z2.object({
947
- type: z2.literal("thinking_delta"),
948
- thinking: z2.string()
989
+ z3.object({
990
+ type: z3.literal("thinking_delta"),
991
+ thinking: z3.string()
949
992
  }),
950
- z2.object({
951
- type: z2.literal("signature_delta"),
952
- signature: z2.string()
993
+ z3.object({
994
+ type: z3.literal("signature_delta"),
995
+ signature: z3.string()
953
996
  })
954
997
  ])
955
998
  }),
956
- z2.object({
957
- type: z2.literal("content_block_stop"),
958
- index: z2.number()
999
+ z3.object({
1000
+ type: z3.literal("content_block_stop"),
1001
+ index: z3.number()
959
1002
  }),
960
- z2.object({
961
- type: z2.literal("error"),
962
- error: z2.object({
963
- type: z2.string(),
964
- message: z2.string()
1003
+ z3.object({
1004
+ type: z3.literal("error"),
1005
+ error: z3.object({
1006
+ type: z3.string(),
1007
+ message: z3.string()
965
1008
  })
966
1009
  }),
967
- z2.object({
968
- type: z2.literal("message_delta"),
969
- delta: z2.object({ stop_reason: z2.string().nullish() }),
970
- usage: z2.object({ output_tokens: z2.number() })
1010
+ z3.object({
1011
+ type: z3.literal("message_delta"),
1012
+ delta: z3.object({ stop_reason: z3.string().nullish() }),
1013
+ usage: z3.object({ output_tokens: z3.number() })
971
1014
  }),
972
- z2.object({
973
- type: z2.literal("message_stop")
1015
+ z3.object({
1016
+ type: z3.literal("message_stop")
974
1017
  }),
975
- z2.object({
976
- type: z2.literal("ping")
1018
+ z3.object({
1019
+ type: z3.literal("ping")
977
1020
  })
978
1021
  ]);
979
- var anthropicProviderOptionsSchema = z2.object({
980
- thinking: z2.object({
981
- type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
982
- budgetTokens: z2.number().optional()
983
- }).optional()
1022
+ var anthropicReasoningMetadataSchema = z3.object({
1023
+ signature: z3.string().optional(),
1024
+ redactedData: z3.string().optional()
984
1025
  });
985
1026
 
986
1027
  // src/anthropic-tools.ts
987
- import { z as z3 } from "zod";
988
- var Bash20241022Parameters = z3.object({
989
- command: z3.string(),
990
- restart: z3.boolean().optional()
1028
+ import { z as z4 } from "zod";
1029
+ var Bash20241022Parameters = z4.object({
1030
+ command: z4.string(),
1031
+ restart: z4.boolean().optional()
991
1032
  });
992
1033
  function bashTool_20241022(options = {}) {
993
1034
  return {
@@ -999,9 +1040,9 @@ function bashTool_20241022(options = {}) {
999
1040
  experimental_toToolResultContent: options.experimental_toToolResultContent
1000
1041
  };
1001
1042
  }
1002
- var Bash20250124Parameters = z3.object({
1003
- command: z3.string(),
1004
- restart: z3.boolean().optional()
1043
+ var Bash20250124Parameters = z4.object({
1044
+ command: z4.string(),
1045
+ restart: z4.boolean().optional()
1005
1046
  });
1006
1047
  function bashTool_20250124(options = {}) {
1007
1048
  return {
@@ -1013,14 +1054,14 @@ function bashTool_20250124(options = {}) {
1013
1054
  experimental_toToolResultContent: options.experimental_toToolResultContent
1014
1055
  };
1015
1056
  }
1016
- var TextEditor20241022Parameters = z3.object({
1017
- command: z3.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1018
- path: z3.string(),
1019
- file_text: z3.string().optional(),
1020
- insert_line: z3.number().int().optional(),
1021
- new_str: z3.string().optional(),
1022
- old_str: z3.string().optional(),
1023
- view_range: z3.array(z3.number().int()).optional()
1057
+ var TextEditor20241022Parameters = z4.object({
1058
+ command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1059
+ path: z4.string(),
1060
+ file_text: z4.string().optional(),
1061
+ insert_line: z4.number().int().optional(),
1062
+ new_str: z4.string().optional(),
1063
+ old_str: z4.string().optional(),
1064
+ view_range: z4.array(z4.number().int()).optional()
1024
1065
  });
1025
1066
  function textEditorTool_20241022(options = {}) {
1026
1067
  return {
@@ -1032,14 +1073,14 @@ function textEditorTool_20241022(options = {}) {
1032
1073
  experimental_toToolResultContent: options.experimental_toToolResultContent
1033
1074
  };
1034
1075
  }
1035
- var TextEditor20250124Parameters = z3.object({
1036
- command: z3.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1037
- path: z3.string(),
1038
- file_text: z3.string().optional(),
1039
- insert_line: z3.number().int().optional(),
1040
- new_str: z3.string().optional(),
1041
- old_str: z3.string().optional(),
1042
- view_range: z3.array(z3.number().int()).optional()
1076
+ var TextEditor20250124Parameters = z4.object({
1077
+ command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1078
+ path: z4.string(),
1079
+ file_text: z4.string().optional(),
1080
+ insert_line: z4.number().int().optional(),
1081
+ new_str: z4.string().optional(),
1082
+ old_str: z4.string().optional(),
1083
+ view_range: z4.array(z4.number().int()).optional()
1043
1084
  });
1044
1085
  function textEditorTool_20250124(options = {}) {
1045
1086
  return {
@@ -1051,8 +1092,8 @@ function textEditorTool_20250124(options = {}) {
1051
1092
  experimental_toToolResultContent: options.experimental_toToolResultContent
1052
1093
  };
1053
1094
  }
1054
- var Computer20241022Parameters = z3.object({
1055
- action: z3.enum([
1095
+ var Computer20241022Parameters = z4.object({
1096
+ action: z4.enum([
1056
1097
  "key",
1057
1098
  "type",
1058
1099
  "mouse_move",
@@ -1064,8 +1105,8 @@ var Computer20241022Parameters = z3.object({
1064
1105
  "screenshot",
1065
1106
  "cursor_position"
1066
1107
  ]),
1067
- coordinate: z3.array(z3.number().int()).optional(),
1068
- text: z3.string().optional()
1108
+ coordinate: z4.array(z4.number().int()).optional(),
1109
+ text: z4.string().optional()
1069
1110
  });
1070
1111
  function computerTool_20241022(options) {
1071
1112
  return {
@@ -1081,8 +1122,8 @@ function computerTool_20241022(options) {
1081
1122
  experimental_toToolResultContent: options.experimental_toToolResultContent
1082
1123
  };
1083
1124
  }
1084
- var Computer20250124Parameters = z3.object({
1085
- action: z3.enum([
1125
+ var Computer20250124Parameters = z4.object({
1126
+ action: z4.enum([
1086
1127
  "key",
1087
1128
  "hold_key",
1088
1129
  "type",
@@ -1100,12 +1141,12 @@ var Computer20250124Parameters = z3.object({
1100
1141
  "wait",
1101
1142
  "screenshot"
1102
1143
  ]),
1103
- coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1104
- duration: z3.number().optional(),
1105
- scroll_amount: z3.number().optional(),
1106
- scroll_direction: z3.enum(["up", "down", "left", "right"]).optional(),
1107
- start_coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1108
- text: z3.string().optional()
1144
+ coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
1145
+ duration: z4.number().optional(),
1146
+ scroll_amount: z4.number().optional(),
1147
+ scroll_direction: z4.enum(["up", "down", "left", "right"]).optional(),
1148
+ start_coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
1149
+ text: z4.string().optional()
1109
1150
  });
1110
1151
  function computerTool_20250124(options) {
1111
1152
  return {
@@ -1143,7 +1184,7 @@ function createAnthropic(options = {}) {
1143
1184
  }),
1144
1185
  ...options.headers
1145
1186
  });
1146
- const createChatModel = (modelId, settings = {}) => new AnthropicMessagesLanguageModel(modelId, settings, {
1187
+ const createChatModel = (modelId) => new AnthropicMessagesLanguageModel(modelId, {
1147
1188
  provider: "anthropic.messages",
1148
1189
  baseURL,
1149
1190
  headers: getHeaders,
@@ -1152,13 +1193,13 @@ function createAnthropic(options = {}) {
1152
1193
  "image/*": [/^https?:\/\/.*$/]
1153
1194
  })
1154
1195
  });
1155
- const provider = function(modelId, settings) {
1196
+ const provider = function(modelId) {
1156
1197
  if (new.target) {
1157
1198
  throw new Error(
1158
1199
  "The Anthropic model function cannot be called with the new keyword."
1159
1200
  );
1160
1201
  }
1161
- return createChatModel(modelId, settings);
1202
+ return createChatModel(modelId);
1162
1203
  };
1163
1204
  provider.languageModel = createChatModel;
1164
1205
  provider.chat = createChatModel;