@ai-sdk/anthropic 1.1.9 → 1.1.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.js CHANGED
@@ -49,8 +49,117 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
49
49
  errorToMessage: (data) => data.error.message
50
50
  });
51
51
 
52
- // src/convert-to-anthropic-messages-prompt.ts
52
+ // src/anthropic-prepare-tools.ts
53
53
  var import_provider = require("@ai-sdk/provider");
54
+ function prepareTools(mode) {
55
+ var _a;
56
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
57
+ const toolWarnings = [];
58
+ const betas = /* @__PURE__ */ new Set();
59
+ if (tools == null) {
60
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
61
+ }
62
+ const anthropicTools2 = [];
63
+ for (const tool of tools) {
64
+ switch (tool.type) {
65
+ case "function":
66
+ anthropicTools2.push({
67
+ name: tool.name,
68
+ description: tool.description,
69
+ input_schema: tool.parameters
70
+ });
71
+ break;
72
+ case "provider-defined":
73
+ switch (tool.id) {
74
+ case "anthropic.computer_20250124":
75
+ betas.add("computer-use-2025-01-24");
76
+ anthropicTools2.push({
77
+ name: tool.name,
78
+ type: "computer_20250124",
79
+ display_width_px: tool.args.displayWidthPx,
80
+ display_height_px: tool.args.displayHeightPx,
81
+ display_number: tool.args.displayNumber
82
+ });
83
+ break;
84
+ case "anthropic.computer_20241022":
85
+ betas.add("computer-use-2024-10-22");
86
+ anthropicTools2.push({
87
+ name: tool.name,
88
+ type: "computer_20241022",
89
+ display_width_px: tool.args.displayWidthPx,
90
+ display_height_px: tool.args.displayHeightPx,
91
+ display_number: tool.args.displayNumber
92
+ });
93
+ break;
94
+ case "anthropic.text_editor_20241022":
95
+ betas.add("computer-use-2024-10-22");
96
+ anthropicTools2.push({
97
+ name: tool.name,
98
+ type: "text_editor_20241022"
99
+ });
100
+ break;
101
+ case "anthropic.bash_20241022":
102
+ betas.add("computer-use-2024-10-22");
103
+ anthropicTools2.push({
104
+ name: tool.name,
105
+ type: "bash_20241022"
106
+ });
107
+ break;
108
+ default:
109
+ toolWarnings.push({ type: "unsupported-tool", tool });
110
+ break;
111
+ }
112
+ break;
113
+ default:
114
+ toolWarnings.push({ type: "unsupported-tool", tool });
115
+ break;
116
+ }
117
+ }
118
+ const toolChoice = mode.toolChoice;
119
+ if (toolChoice == null) {
120
+ return {
121
+ tools: anthropicTools2,
122
+ tool_choice: void 0,
123
+ toolWarnings,
124
+ betas
125
+ };
126
+ }
127
+ const type = toolChoice.type;
128
+ switch (type) {
129
+ case "auto":
130
+ return {
131
+ tools: anthropicTools2,
132
+ tool_choice: { type: "auto" },
133
+ toolWarnings,
134
+ betas
135
+ };
136
+ case "required":
137
+ return {
138
+ tools: anthropicTools2,
139
+ tool_choice: { type: "any" },
140
+ toolWarnings,
141
+ betas
142
+ };
143
+ case "none":
144
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
145
+ case "tool":
146
+ return {
147
+ tools: anthropicTools2,
148
+ tool_choice: { type: "tool", name: toolChoice.toolName },
149
+ toolWarnings,
150
+ betas
151
+ };
152
+ default: {
153
+ const _exhaustiveCheck = type;
154
+ throw new import_provider.UnsupportedFunctionalityError({
155
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
156
+ });
157
+ }
158
+ }
159
+ }
160
+
161
+ // src/convert-to-anthropic-messages-prompt.ts
162
+ var import_provider2 = require("@ai-sdk/provider");
54
163
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
55
164
  function convertToAnthropicMessagesPrompt({
56
165
  prompt
@@ -73,7 +182,7 @@ function convertToAnthropicMessagesPrompt({
73
182
  switch (type) {
74
183
  case "system": {
75
184
  if (system != null) {
76
- throw new import_provider.UnsupportedFunctionalityError({
185
+ throw new import_provider2.UnsupportedFunctionalityError({
77
186
  functionality: "Multiple system messages that are separated by user/assistant messages"
78
187
  });
79
188
  }
@@ -105,7 +214,7 @@ function convertToAnthropicMessagesPrompt({
105
214
  }
106
215
  case "image": {
107
216
  if (part.image instanceof URL) {
108
- throw new import_provider.UnsupportedFunctionalityError({
217
+ throw new import_provider2.UnsupportedFunctionalityError({
109
218
  functionality: "Image URLs in user messages"
110
219
  });
111
220
  }
@@ -122,12 +231,12 @@ function convertToAnthropicMessagesPrompt({
122
231
  }
123
232
  case "file": {
124
233
  if (part.data instanceof URL) {
125
- throw new import_provider.UnsupportedFunctionalityError({
234
+ throw new import_provider2.UnsupportedFunctionalityError({
126
235
  functionality: "Image URLs in user messages"
127
236
  });
128
237
  }
129
238
  if (part.mimeType !== "application/pdf") {
130
- throw new import_provider.UnsupportedFunctionalityError({
239
+ throw new import_provider2.UnsupportedFunctionalityError({
131
240
  functionality: "Non-PDF files in user messages"
132
241
  });
133
242
  }
@@ -216,6 +325,23 @@ function convertToAnthropicMessagesPrompt({
216
325
  });
217
326
  break;
218
327
  }
328
+ case "reasoning": {
329
+ anthropicContent.push({
330
+ type: "thinking",
331
+ thinking: part.text,
332
+ signature: part.signature,
333
+ cache_control: cacheControl
334
+ });
335
+ break;
336
+ }
337
+ case "redacted-reasoning": {
338
+ anthropicContent.push({
339
+ type: "redacted_thinking",
340
+ data: part.data,
341
+ cache_control: cacheControl
342
+ });
343
+ break;
344
+ }
219
345
  case "tool-call": {
220
346
  anthropicContent.push({
221
347
  type: "tool_use",
@@ -226,6 +352,10 @@ function convertToAnthropicMessagesPrompt({
226
352
  });
227
353
  break;
228
354
  }
355
+ default: {
356
+ const _exhaustiveCheck = part;
357
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
358
+ }
229
359
  }
230
360
  }
231
361
  }
@@ -305,103 +435,6 @@ function mapAnthropicStopReason(finishReason) {
305
435
  }
306
436
  }
307
437
 
308
- // src/anthropic-prepare-tools.ts
309
- var import_provider2 = require("@ai-sdk/provider");
310
- function prepareTools(mode) {
311
- var _a;
312
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
313
- const toolWarnings = [];
314
- const betas = /* @__PURE__ */ new Set();
315
- if (tools == null) {
316
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
317
- }
318
- const anthropicTools2 = [];
319
- for (const tool of tools) {
320
- switch (tool.type) {
321
- case "function":
322
- anthropicTools2.push({
323
- name: tool.name,
324
- description: tool.description,
325
- input_schema: tool.parameters
326
- });
327
- break;
328
- case "provider-defined":
329
- betas.add("computer-use-2024-10-22");
330
- switch (tool.id) {
331
- case "anthropic.computer_20241022":
332
- anthropicTools2.push({
333
- name: tool.name,
334
- type: "computer_20241022",
335
- display_width_px: tool.args.displayWidthPx,
336
- display_height_px: tool.args.displayHeightPx,
337
- display_number: tool.args.displayNumber
338
- });
339
- break;
340
- case "anthropic.text_editor_20241022":
341
- anthropicTools2.push({
342
- name: tool.name,
343
- type: "text_editor_20241022"
344
- });
345
- break;
346
- case "anthropic.bash_20241022":
347
- anthropicTools2.push({
348
- name: tool.name,
349
- type: "bash_20241022"
350
- });
351
- break;
352
- default:
353
- toolWarnings.push({ type: "unsupported-tool", tool });
354
- break;
355
- }
356
- break;
357
- default:
358
- toolWarnings.push({ type: "unsupported-tool", tool });
359
- break;
360
- }
361
- }
362
- const toolChoice = mode.toolChoice;
363
- if (toolChoice == null) {
364
- return {
365
- tools: anthropicTools2,
366
- tool_choice: void 0,
367
- toolWarnings,
368
- betas
369
- };
370
- }
371
- const type = toolChoice.type;
372
- switch (type) {
373
- case "auto":
374
- return {
375
- tools: anthropicTools2,
376
- tool_choice: { type: "auto" },
377
- toolWarnings,
378
- betas
379
- };
380
- case "required":
381
- return {
382
- tools: anthropicTools2,
383
- tool_choice: { type: "any" },
384
- toolWarnings,
385
- betas
386
- };
387
- case "none":
388
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
389
- case "tool":
390
- return {
391
- tools: anthropicTools2,
392
- tool_choice: { type: "tool", name: toolChoice.toolName },
393
- toolWarnings,
394
- betas
395
- };
396
- default: {
397
- const _exhaustiveCheck = type;
398
- throw new import_provider2.UnsupportedFunctionalityError({
399
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
400
- });
401
- }
402
- }
403
- }
404
-
405
438
  // src/anthropic-messages-language-model.ts
406
439
  var AnthropicMessagesLanguageModel = class {
407
440
  constructor(modelId, settings, config) {
@@ -418,7 +451,8 @@ var AnthropicMessagesLanguageModel = class {
418
451
  async getArgs({
419
452
  mode,
420
453
  prompt,
421
- maxTokens,
454
+ maxTokens = 4096,
455
+ // 4096: max model output tokens TODO update default in v5
422
456
  temperature,
423
457
  topP,
424
458
  topK,
@@ -426,8 +460,10 @@ var AnthropicMessagesLanguageModel = class {
426
460
  presencePenalty,
427
461
  stopSequences,
428
462
  responseFormat,
429
- seed
463
+ seed,
464
+ providerMetadata: providerOptions
430
465
  }) {
466
+ var _a, _b, _c;
431
467
  const type = mode.type;
432
468
  const warnings = [];
433
469
  if (frequencyPenalty != null) {
@@ -458,20 +494,67 @@ var AnthropicMessagesLanguageModel = class {
458
494
  const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
459
495
  prompt
460
496
  });
497
+ const thinkingOptions = thinkingOptionsSchema.safeParse(
498
+ (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.thinking
499
+ );
500
+ if (!thinkingOptions.success) {
501
+ throw new import_provider3.InvalidArgumentError({
502
+ argument: "providerOptions.anthropic.thinking",
503
+ message: "invalid thinking options",
504
+ cause: thinkingOptions.error
505
+ });
506
+ }
507
+ const isThinking = ((_b = thinkingOptions.data) == null ? void 0 : _b.type) === "enabled";
508
+ const thinkingBudget = (_c = thinkingOptions.data) == null ? void 0 : _c.budgetTokens;
461
509
  const baseArgs = {
462
510
  // model id:
463
511
  model: this.modelId,
464
512
  // standardized settings:
465
- max_tokens: maxTokens != null ? maxTokens : 4096,
466
- // 4096: max model output tokens TODO remove
513
+ max_tokens: maxTokens,
467
514
  temperature,
468
515
  top_k: topK,
469
516
  top_p: topP,
470
517
  stop_sequences: stopSequences,
518
+ // provider specific settings:
519
+ ...isThinking && {
520
+ thinking: { type: "enabled", budget_tokens: thinkingBudget }
521
+ },
471
522
  // prompt:
472
523
  system: messagesPrompt.system,
473
524
  messages: messagesPrompt.messages
474
525
  };
526
+ if (isThinking) {
527
+ if (thinkingBudget == null) {
528
+ throw new import_provider3.UnsupportedFunctionalityError({
529
+ functionality: "thinking requires a budget"
530
+ });
531
+ }
532
+ if (baseArgs.temperature != null) {
533
+ baseArgs.temperature = void 0;
534
+ warnings.push({
535
+ type: "unsupported-setting",
536
+ setting: "temperature",
537
+ details: "temperature is not supported when thinking is enabled"
538
+ });
539
+ }
540
+ if (topK != null) {
541
+ baseArgs.top_k = void 0;
542
+ warnings.push({
543
+ type: "unsupported-setting",
544
+ setting: "topK",
545
+ details: "topK is not supported when thinking is enabled"
546
+ });
547
+ }
548
+ if (topP != null) {
549
+ baseArgs.top_p = void 0;
550
+ warnings.push({
551
+ type: "unsupported-setting",
552
+ setting: "topP",
553
+ details: "topP is not supported when thinking is enabled"
554
+ });
555
+ }
556
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
557
+ }
475
558
  switch (type) {
476
559
  case "regular": {
477
560
  const {
@@ -562,8 +645,21 @@ var AnthropicMessagesLanguageModel = class {
562
645
  }
563
646
  }
564
647
  }
648
+ const reasoning = response.content.filter(
649
+ (content) => content.type === "redacted_thinking" || content.type === "thinking"
650
+ ).map(
651
+ (content) => content.type === "thinking" ? {
652
+ type: "text",
653
+ text: content.thinking,
654
+ signature: content.signature
655
+ } : {
656
+ type: "redacted",
657
+ data: content.data
658
+ }
659
+ );
565
660
  return {
566
661
  text,
662
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
567
663
  toolCalls,
568
664
  finishReason: mapAnthropicStopReason(response.stop_reason),
569
665
  usage: {
@@ -608,7 +704,7 @@ var AnthropicMessagesLanguageModel = class {
608
704
  };
609
705
  const toolCallContentBlocks = {};
610
706
  let providerMetadata = void 0;
611
- const self = this;
707
+ let blockType = void 0;
612
708
  return {
613
709
  stream: response.pipeThrough(
614
710
  new TransformStream({
@@ -625,8 +721,17 @@ var AnthropicMessagesLanguageModel = class {
625
721
  }
626
722
  case "content_block_start": {
627
723
  const contentBlockType = value.content_block.type;
724
+ blockType = contentBlockType;
628
725
  switch (contentBlockType) {
629
- case "text": {
726
+ case "text":
727
+ case "thinking": {
728
+ return;
729
+ }
730
+ case "redacted_thinking": {
731
+ controller.enqueue({
732
+ type: "redacted-reasoning",
733
+ data: value.content_block.data
734
+ });
630
735
  return;
631
736
  }
632
737
  case "tool_use": {
@@ -657,6 +762,7 @@ var AnthropicMessagesLanguageModel = class {
657
762
  });
658
763
  delete toolCallContentBlocks[value.index];
659
764
  }
765
+ blockType = void 0;
660
766
  return;
661
767
  }
662
768
  case "content_block_delta": {
@@ -669,6 +775,22 @@ var AnthropicMessagesLanguageModel = class {
669
775
  });
670
776
  return;
671
777
  }
778
+ case "thinking_delta": {
779
+ controller.enqueue({
780
+ type: "reasoning",
781
+ textDelta: value.delta.thinking
782
+ });
783
+ return;
784
+ }
785
+ case "signature_delta": {
786
+ if (blockType === "thinking") {
787
+ controller.enqueue({
788
+ type: "reasoning-signature",
789
+ signature: value.delta.signature
790
+ });
791
+ }
792
+ return;
793
+ }
672
794
  case "input_json_delta": {
673
795
  const contentBlock = toolCallContentBlocks[value.index];
674
796
  controller.enqueue({
@@ -748,6 +870,15 @@ var anthropicMessagesResponseSchema = import_zod2.z.object({
748
870
  type: import_zod2.z.literal("text"),
749
871
  text: import_zod2.z.string()
750
872
  }),
873
+ import_zod2.z.object({
874
+ type: import_zod2.z.literal("thinking"),
875
+ thinking: import_zod2.z.string(),
876
+ signature: import_zod2.z.string()
877
+ }),
878
+ import_zod2.z.object({
879
+ type: import_zod2.z.literal("redacted_thinking"),
880
+ data: import_zod2.z.string()
881
+ }),
751
882
  import_zod2.z.object({
752
883
  type: import_zod2.z.literal("tool_use"),
753
884
  id: import_zod2.z.string(),
@@ -786,10 +917,18 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
786
917
  type: import_zod2.z.literal("text"),
787
918
  text: import_zod2.z.string()
788
919
  }),
920
+ import_zod2.z.object({
921
+ type: import_zod2.z.literal("thinking"),
922
+ thinking: import_zod2.z.string()
923
+ }),
789
924
  import_zod2.z.object({
790
925
  type: import_zod2.z.literal("tool_use"),
791
926
  id: import_zod2.z.string(),
792
927
  name: import_zod2.z.string()
928
+ }),
929
+ import_zod2.z.object({
930
+ type: import_zod2.z.literal("redacted_thinking"),
931
+ data: import_zod2.z.string()
793
932
  })
794
933
  ])
795
934
  }),
@@ -804,6 +943,14 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
804
943
  import_zod2.z.object({
805
944
  type: import_zod2.z.literal("text_delta"),
806
945
  text: import_zod2.z.string()
946
+ }),
947
+ import_zod2.z.object({
948
+ type: import_zod2.z.literal("thinking_delta"),
949
+ thinking: import_zod2.z.string()
950
+ }),
951
+ import_zod2.z.object({
952
+ type: import_zod2.z.literal("signature_delta"),
953
+ signature: import_zod2.z.string()
807
954
  })
808
955
  ])
809
956
  }),
@@ -830,6 +977,10 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
830
977
  type: import_zod2.z.literal("ping")
831
978
  })
832
979
  ]);
980
+ var thinkingOptionsSchema = import_zod2.z.object({
981
+ type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
982
+ budgetTokens: import_zod2.z.number().optional()
983
+ }).optional();
833
984
 
834
985
  // src/anthropic-tools.ts
835
986
  var import_zod3 = require("zod");
@@ -896,10 +1047,51 @@ function computerTool_20241022(options) {
896
1047
  experimental_toToolResultContent: options.experimental_toToolResultContent
897
1048
  };
898
1049
  }
1050
+ var Computer20250124Parameters = import_zod3.z.object({
1051
+ action: import_zod3.z.enum([
1052
+ "key",
1053
+ "hold_key",
1054
+ "type",
1055
+ "cursor_position",
1056
+ "mouse_move",
1057
+ "left_mouse_down",
1058
+ "left_mouse_up",
1059
+ "left_click",
1060
+ "left_click_drag",
1061
+ "right_click",
1062
+ "middle_click",
1063
+ "double_click",
1064
+ "triple_click",
1065
+ "scroll",
1066
+ "wait",
1067
+ "screenshot"
1068
+ ]),
1069
+ coordinate: import_zod3.z.tuple([import_zod3.z.number().int(), import_zod3.z.number().int()]).optional(),
1070
+ duration: import_zod3.z.number().optional(),
1071
+ scroll_amount: import_zod3.z.number().optional(),
1072
+ scroll_direction: import_zod3.z.enum(["up", "down", "left", "right"]).optional(),
1073
+ start_coordinate: import_zod3.z.tuple([import_zod3.z.number().int(), import_zod3.z.number().int()]).optional(),
1074
+ text: import_zod3.z.string().optional()
1075
+ });
1076
+ function computerTool_20250124(options) {
1077
+ return {
1078
+ type: "provider-defined",
1079
+ id: "anthropic.computer_20250124",
1080
+ args: {
1081
+ displayWidthPx: options.displayWidthPx,
1082
+ displayHeightPx: options.displayHeightPx,
1083
+ displayNumber: options.displayNumber
1084
+ },
1085
+ parameters: Computer20250124Parameters,
1086
+ execute: options.execute,
1087
+ experimental_toToolResultContent: options.experimental_toToolResultContent
1088
+ };
1089
+ }
899
1090
  var anthropicTools = {
900
1091
  bash_20241022: bashTool_20241022,
901
1092
  textEditor_20241022: textEditorTool_20241022,
902
- computer_20241022: computerTool_20241022
1093
+ computer_20241022: computerTool_20241022,
1094
+ computer_20250124: computerTool_20250124
903
1095
  };
904
1096
 
905
1097
  // src/anthropic-provider.ts