@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.
@@ -45,8 +45,117 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
45
45
  errorToMessage: (data) => data.error.message
46
46
  });
47
47
 
48
- // src/convert-to-anthropic-messages-prompt.ts
48
+ // src/anthropic-prepare-tools.ts
49
49
  var import_provider = require("@ai-sdk/provider");
50
+ function prepareTools(mode) {
51
+ var _a;
52
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
53
+ const toolWarnings = [];
54
+ const betas = /* @__PURE__ */ new Set();
55
+ if (tools == null) {
56
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
57
+ }
58
+ const anthropicTools2 = [];
59
+ for (const tool of tools) {
60
+ switch (tool.type) {
61
+ case "function":
62
+ anthropicTools2.push({
63
+ name: tool.name,
64
+ description: tool.description,
65
+ input_schema: tool.parameters
66
+ });
67
+ break;
68
+ case "provider-defined":
69
+ switch (tool.id) {
70
+ case "anthropic.computer_20250124":
71
+ betas.add("computer-use-2025-01-24");
72
+ anthropicTools2.push({
73
+ name: tool.name,
74
+ type: "computer_20250124",
75
+ display_width_px: tool.args.displayWidthPx,
76
+ display_height_px: tool.args.displayHeightPx,
77
+ display_number: tool.args.displayNumber
78
+ });
79
+ break;
80
+ case "anthropic.computer_20241022":
81
+ betas.add("computer-use-2024-10-22");
82
+ anthropicTools2.push({
83
+ name: tool.name,
84
+ type: "computer_20241022",
85
+ display_width_px: tool.args.displayWidthPx,
86
+ display_height_px: tool.args.displayHeightPx,
87
+ display_number: tool.args.displayNumber
88
+ });
89
+ break;
90
+ case "anthropic.text_editor_20241022":
91
+ betas.add("computer-use-2024-10-22");
92
+ anthropicTools2.push({
93
+ name: tool.name,
94
+ type: "text_editor_20241022"
95
+ });
96
+ break;
97
+ case "anthropic.bash_20241022":
98
+ betas.add("computer-use-2024-10-22");
99
+ anthropicTools2.push({
100
+ name: tool.name,
101
+ type: "bash_20241022"
102
+ });
103
+ break;
104
+ default:
105
+ toolWarnings.push({ type: "unsupported-tool", tool });
106
+ break;
107
+ }
108
+ break;
109
+ default:
110
+ toolWarnings.push({ type: "unsupported-tool", tool });
111
+ break;
112
+ }
113
+ }
114
+ const toolChoice = mode.toolChoice;
115
+ if (toolChoice == null) {
116
+ return {
117
+ tools: anthropicTools2,
118
+ tool_choice: void 0,
119
+ toolWarnings,
120
+ betas
121
+ };
122
+ }
123
+ const type = toolChoice.type;
124
+ switch (type) {
125
+ case "auto":
126
+ return {
127
+ tools: anthropicTools2,
128
+ tool_choice: { type: "auto" },
129
+ toolWarnings,
130
+ betas
131
+ };
132
+ case "required":
133
+ return {
134
+ tools: anthropicTools2,
135
+ tool_choice: { type: "any" },
136
+ toolWarnings,
137
+ betas
138
+ };
139
+ case "none":
140
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
141
+ case "tool":
142
+ return {
143
+ tools: anthropicTools2,
144
+ tool_choice: { type: "tool", name: toolChoice.toolName },
145
+ toolWarnings,
146
+ betas
147
+ };
148
+ default: {
149
+ const _exhaustiveCheck = type;
150
+ throw new import_provider.UnsupportedFunctionalityError({
151
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
152
+ });
153
+ }
154
+ }
155
+ }
156
+
157
+ // src/convert-to-anthropic-messages-prompt.ts
158
+ var import_provider2 = require("@ai-sdk/provider");
50
159
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
51
160
  function convertToAnthropicMessagesPrompt({
52
161
  prompt
@@ -69,7 +178,7 @@ function convertToAnthropicMessagesPrompt({
69
178
  switch (type) {
70
179
  case "system": {
71
180
  if (system != null) {
72
- throw new import_provider.UnsupportedFunctionalityError({
181
+ throw new import_provider2.UnsupportedFunctionalityError({
73
182
  functionality: "Multiple system messages that are separated by user/assistant messages"
74
183
  });
75
184
  }
@@ -101,7 +210,7 @@ function convertToAnthropicMessagesPrompt({
101
210
  }
102
211
  case "image": {
103
212
  if (part.image instanceof URL) {
104
- throw new import_provider.UnsupportedFunctionalityError({
213
+ throw new import_provider2.UnsupportedFunctionalityError({
105
214
  functionality: "Image URLs in user messages"
106
215
  });
107
216
  }
@@ -118,12 +227,12 @@ function convertToAnthropicMessagesPrompt({
118
227
  }
119
228
  case "file": {
120
229
  if (part.data instanceof URL) {
121
- throw new import_provider.UnsupportedFunctionalityError({
230
+ throw new import_provider2.UnsupportedFunctionalityError({
122
231
  functionality: "Image URLs in user messages"
123
232
  });
124
233
  }
125
234
  if (part.mimeType !== "application/pdf") {
126
- throw new import_provider.UnsupportedFunctionalityError({
235
+ throw new import_provider2.UnsupportedFunctionalityError({
127
236
  functionality: "Non-PDF files in user messages"
128
237
  });
129
238
  }
@@ -212,6 +321,23 @@ function convertToAnthropicMessagesPrompt({
212
321
  });
213
322
  break;
214
323
  }
324
+ case "reasoning": {
325
+ anthropicContent.push({
326
+ type: "thinking",
327
+ thinking: part.text,
328
+ signature: part.signature,
329
+ cache_control: cacheControl
330
+ });
331
+ break;
332
+ }
333
+ case "redacted-reasoning": {
334
+ anthropicContent.push({
335
+ type: "redacted_thinking",
336
+ data: part.data,
337
+ cache_control: cacheControl
338
+ });
339
+ break;
340
+ }
215
341
  case "tool-call": {
216
342
  anthropicContent.push({
217
343
  type: "tool_use",
@@ -222,6 +348,10 @@ function convertToAnthropicMessagesPrompt({
222
348
  });
223
349
  break;
224
350
  }
351
+ default: {
352
+ const _exhaustiveCheck = part;
353
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
354
+ }
225
355
  }
226
356
  }
227
357
  }
@@ -301,103 +431,6 @@ function mapAnthropicStopReason(finishReason) {
301
431
  }
302
432
  }
303
433
 
304
- // src/anthropic-prepare-tools.ts
305
- var import_provider2 = require("@ai-sdk/provider");
306
- function prepareTools(mode) {
307
- var _a;
308
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
309
- const toolWarnings = [];
310
- const betas = /* @__PURE__ */ new Set();
311
- if (tools == null) {
312
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
313
- }
314
- const anthropicTools2 = [];
315
- for (const tool of tools) {
316
- switch (tool.type) {
317
- case "function":
318
- anthropicTools2.push({
319
- name: tool.name,
320
- description: tool.description,
321
- input_schema: tool.parameters
322
- });
323
- break;
324
- case "provider-defined":
325
- betas.add("computer-use-2024-10-22");
326
- switch (tool.id) {
327
- case "anthropic.computer_20241022":
328
- anthropicTools2.push({
329
- name: tool.name,
330
- type: "computer_20241022",
331
- display_width_px: tool.args.displayWidthPx,
332
- display_height_px: tool.args.displayHeightPx,
333
- display_number: tool.args.displayNumber
334
- });
335
- break;
336
- case "anthropic.text_editor_20241022":
337
- anthropicTools2.push({
338
- name: tool.name,
339
- type: "text_editor_20241022"
340
- });
341
- break;
342
- case "anthropic.bash_20241022":
343
- anthropicTools2.push({
344
- name: tool.name,
345
- type: "bash_20241022"
346
- });
347
- break;
348
- default:
349
- toolWarnings.push({ type: "unsupported-tool", tool });
350
- break;
351
- }
352
- break;
353
- default:
354
- toolWarnings.push({ type: "unsupported-tool", tool });
355
- break;
356
- }
357
- }
358
- const toolChoice = mode.toolChoice;
359
- if (toolChoice == null) {
360
- return {
361
- tools: anthropicTools2,
362
- tool_choice: void 0,
363
- toolWarnings,
364
- betas
365
- };
366
- }
367
- const type = toolChoice.type;
368
- switch (type) {
369
- case "auto":
370
- return {
371
- tools: anthropicTools2,
372
- tool_choice: { type: "auto" },
373
- toolWarnings,
374
- betas
375
- };
376
- case "required":
377
- return {
378
- tools: anthropicTools2,
379
- tool_choice: { type: "any" },
380
- toolWarnings,
381
- betas
382
- };
383
- case "none":
384
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
385
- case "tool":
386
- return {
387
- tools: anthropicTools2,
388
- tool_choice: { type: "tool", name: toolChoice.toolName },
389
- toolWarnings,
390
- betas
391
- };
392
- default: {
393
- const _exhaustiveCheck = type;
394
- throw new import_provider2.UnsupportedFunctionalityError({
395
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
396
- });
397
- }
398
- }
399
- }
400
-
401
434
  // src/anthropic-messages-language-model.ts
402
435
  var AnthropicMessagesLanguageModel = class {
403
436
  constructor(modelId, settings, config) {
@@ -414,7 +447,8 @@ var AnthropicMessagesLanguageModel = class {
414
447
  async getArgs({
415
448
  mode,
416
449
  prompt,
417
- maxTokens,
450
+ maxTokens = 4096,
451
+ // 4096: max model output tokens TODO update default in v5
418
452
  temperature,
419
453
  topP,
420
454
  topK,
@@ -422,8 +456,10 @@ var AnthropicMessagesLanguageModel = class {
422
456
  presencePenalty,
423
457
  stopSequences,
424
458
  responseFormat,
425
- seed
459
+ seed,
460
+ providerMetadata: providerOptions
426
461
  }) {
462
+ var _a, _b, _c;
427
463
  const type = mode.type;
428
464
  const warnings = [];
429
465
  if (frequencyPenalty != null) {
@@ -454,20 +490,67 @@ var AnthropicMessagesLanguageModel = class {
454
490
  const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
455
491
  prompt
456
492
  });
493
+ const thinkingOptions = thinkingOptionsSchema.safeParse(
494
+ (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.thinking
495
+ );
496
+ if (!thinkingOptions.success) {
497
+ throw new import_provider3.InvalidArgumentError({
498
+ argument: "providerOptions.anthropic.thinking",
499
+ message: "invalid thinking options",
500
+ cause: thinkingOptions.error
501
+ });
502
+ }
503
+ const isThinking = ((_b = thinkingOptions.data) == null ? void 0 : _b.type) === "enabled";
504
+ const thinkingBudget = (_c = thinkingOptions.data) == null ? void 0 : _c.budgetTokens;
457
505
  const baseArgs = {
458
506
  // model id:
459
507
  model: this.modelId,
460
508
  // standardized settings:
461
- max_tokens: maxTokens != null ? maxTokens : 4096,
462
- // 4096: max model output tokens TODO remove
509
+ max_tokens: maxTokens,
463
510
  temperature,
464
511
  top_k: topK,
465
512
  top_p: topP,
466
513
  stop_sequences: stopSequences,
514
+ // provider specific settings:
515
+ ...isThinking && {
516
+ thinking: { type: "enabled", budget_tokens: thinkingBudget }
517
+ },
467
518
  // prompt:
468
519
  system: messagesPrompt.system,
469
520
  messages: messagesPrompt.messages
470
521
  };
522
+ if (isThinking) {
523
+ if (thinkingBudget == null) {
524
+ throw new import_provider3.UnsupportedFunctionalityError({
525
+ functionality: "thinking requires a budget"
526
+ });
527
+ }
528
+ if (baseArgs.temperature != null) {
529
+ baseArgs.temperature = void 0;
530
+ warnings.push({
531
+ type: "unsupported-setting",
532
+ setting: "temperature",
533
+ details: "temperature is not supported when thinking is enabled"
534
+ });
535
+ }
536
+ if (topK != null) {
537
+ baseArgs.top_k = void 0;
538
+ warnings.push({
539
+ type: "unsupported-setting",
540
+ setting: "topK",
541
+ details: "topK is not supported when thinking is enabled"
542
+ });
543
+ }
544
+ if (topP != null) {
545
+ baseArgs.top_p = void 0;
546
+ warnings.push({
547
+ type: "unsupported-setting",
548
+ setting: "topP",
549
+ details: "topP is not supported when thinking is enabled"
550
+ });
551
+ }
552
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
553
+ }
471
554
  switch (type) {
472
555
  case "regular": {
473
556
  const {
@@ -558,8 +641,21 @@ var AnthropicMessagesLanguageModel = class {
558
641
  }
559
642
  }
560
643
  }
644
+ const reasoning = response.content.filter(
645
+ (content) => content.type === "redacted_thinking" || content.type === "thinking"
646
+ ).map(
647
+ (content) => content.type === "thinking" ? {
648
+ type: "text",
649
+ text: content.thinking,
650
+ signature: content.signature
651
+ } : {
652
+ type: "redacted",
653
+ data: content.data
654
+ }
655
+ );
561
656
  return {
562
657
  text,
658
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
563
659
  toolCalls,
564
660
  finishReason: mapAnthropicStopReason(response.stop_reason),
565
661
  usage: {
@@ -604,7 +700,7 @@ var AnthropicMessagesLanguageModel = class {
604
700
  };
605
701
  const toolCallContentBlocks = {};
606
702
  let providerMetadata = void 0;
607
- const self = this;
703
+ let blockType = void 0;
608
704
  return {
609
705
  stream: response.pipeThrough(
610
706
  new TransformStream({
@@ -621,8 +717,17 @@ var AnthropicMessagesLanguageModel = class {
621
717
  }
622
718
  case "content_block_start": {
623
719
  const contentBlockType = value.content_block.type;
720
+ blockType = contentBlockType;
624
721
  switch (contentBlockType) {
625
- case "text": {
722
+ case "text":
723
+ case "thinking": {
724
+ return;
725
+ }
726
+ case "redacted_thinking": {
727
+ controller.enqueue({
728
+ type: "redacted-reasoning",
729
+ data: value.content_block.data
730
+ });
626
731
  return;
627
732
  }
628
733
  case "tool_use": {
@@ -653,6 +758,7 @@ var AnthropicMessagesLanguageModel = class {
653
758
  });
654
759
  delete toolCallContentBlocks[value.index];
655
760
  }
761
+ blockType = void 0;
656
762
  return;
657
763
  }
658
764
  case "content_block_delta": {
@@ -665,6 +771,22 @@ var AnthropicMessagesLanguageModel = class {
665
771
  });
666
772
  return;
667
773
  }
774
+ case "thinking_delta": {
775
+ controller.enqueue({
776
+ type: "reasoning",
777
+ textDelta: value.delta.thinking
778
+ });
779
+ return;
780
+ }
781
+ case "signature_delta": {
782
+ if (blockType === "thinking") {
783
+ controller.enqueue({
784
+ type: "reasoning-signature",
785
+ signature: value.delta.signature
786
+ });
787
+ }
788
+ return;
789
+ }
668
790
  case "input_json_delta": {
669
791
  const contentBlock = toolCallContentBlocks[value.index];
670
792
  controller.enqueue({
@@ -744,6 +866,15 @@ var anthropicMessagesResponseSchema = import_zod2.z.object({
744
866
  type: import_zod2.z.literal("text"),
745
867
  text: import_zod2.z.string()
746
868
  }),
869
+ import_zod2.z.object({
870
+ type: import_zod2.z.literal("thinking"),
871
+ thinking: import_zod2.z.string(),
872
+ signature: import_zod2.z.string()
873
+ }),
874
+ import_zod2.z.object({
875
+ type: import_zod2.z.literal("redacted_thinking"),
876
+ data: import_zod2.z.string()
877
+ }),
747
878
  import_zod2.z.object({
748
879
  type: import_zod2.z.literal("tool_use"),
749
880
  id: import_zod2.z.string(),
@@ -782,10 +913,18 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
782
913
  type: import_zod2.z.literal("text"),
783
914
  text: import_zod2.z.string()
784
915
  }),
916
+ import_zod2.z.object({
917
+ type: import_zod2.z.literal("thinking"),
918
+ thinking: import_zod2.z.string()
919
+ }),
785
920
  import_zod2.z.object({
786
921
  type: import_zod2.z.literal("tool_use"),
787
922
  id: import_zod2.z.string(),
788
923
  name: import_zod2.z.string()
924
+ }),
925
+ import_zod2.z.object({
926
+ type: import_zod2.z.literal("redacted_thinking"),
927
+ data: import_zod2.z.string()
789
928
  })
790
929
  ])
791
930
  }),
@@ -800,6 +939,14 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
800
939
  import_zod2.z.object({
801
940
  type: import_zod2.z.literal("text_delta"),
802
941
  text: import_zod2.z.string()
942
+ }),
943
+ import_zod2.z.object({
944
+ type: import_zod2.z.literal("thinking_delta"),
945
+ thinking: import_zod2.z.string()
946
+ }),
947
+ import_zod2.z.object({
948
+ type: import_zod2.z.literal("signature_delta"),
949
+ signature: import_zod2.z.string()
803
950
  })
804
951
  ])
805
952
  }),
@@ -826,6 +973,10 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
826
973
  type: import_zod2.z.literal("ping")
827
974
  })
828
975
  ]);
976
+ var thinkingOptionsSchema = import_zod2.z.object({
977
+ type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
978
+ budgetTokens: import_zod2.z.number().optional()
979
+ }).optional();
829
980
 
830
981
  // src/anthropic-tools.ts
831
982
  var import_zod3 = require("zod");
@@ -892,10 +1043,51 @@ function computerTool_20241022(options) {
892
1043
  experimental_toToolResultContent: options.experimental_toToolResultContent
893
1044
  };
894
1045
  }
1046
+ var Computer20250124Parameters = import_zod3.z.object({
1047
+ action: import_zod3.z.enum([
1048
+ "key",
1049
+ "hold_key",
1050
+ "type",
1051
+ "cursor_position",
1052
+ "mouse_move",
1053
+ "left_mouse_down",
1054
+ "left_mouse_up",
1055
+ "left_click",
1056
+ "left_click_drag",
1057
+ "right_click",
1058
+ "middle_click",
1059
+ "double_click",
1060
+ "triple_click",
1061
+ "scroll",
1062
+ "wait",
1063
+ "screenshot"
1064
+ ]),
1065
+ coordinate: import_zod3.z.tuple([import_zod3.z.number().int(), import_zod3.z.number().int()]).optional(),
1066
+ duration: import_zod3.z.number().optional(),
1067
+ scroll_amount: import_zod3.z.number().optional(),
1068
+ scroll_direction: import_zod3.z.enum(["up", "down", "left", "right"]).optional(),
1069
+ start_coordinate: import_zod3.z.tuple([import_zod3.z.number().int(), import_zod3.z.number().int()]).optional(),
1070
+ text: import_zod3.z.string().optional()
1071
+ });
1072
+ function computerTool_20250124(options) {
1073
+ return {
1074
+ type: "provider-defined",
1075
+ id: "anthropic.computer_20250124",
1076
+ args: {
1077
+ displayWidthPx: options.displayWidthPx,
1078
+ displayHeightPx: options.displayHeightPx,
1079
+ displayNumber: options.displayNumber
1080
+ },
1081
+ parameters: Computer20250124Parameters,
1082
+ execute: options.execute,
1083
+ experimental_toToolResultContent: options.experimental_toToolResultContent
1084
+ };
1085
+ }
895
1086
  var anthropicTools = {
896
1087
  bash_20241022: bashTool_20241022,
897
1088
  textEditor_20241022: textEditorTool_20241022,
898
- computer_20241022: computerTool_20241022
1089
+ computer_20241022: computerTool_20241022,
1090
+ computer_20250124: computerTool_20250124
899
1091
  };
900
1092
  // Annotate the CommonJS export names for ESM import in node:
901
1093
  0 && (module.exports = {