@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.
@@ -1,5 +1,6 @@
1
1
  // src/anthropic-messages-language-model.ts
2
2
  import {
3
+ InvalidArgumentError,
3
4
  UnsupportedFunctionalityError as UnsupportedFunctionalityError3
4
5
  } from "@ai-sdk/provider";
5
6
  import {
@@ -26,10 +27,121 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
26
27
  errorToMessage: (data) => data.error.message
27
28
  });
28
29
 
29
- // src/convert-to-anthropic-messages-prompt.ts
30
+ // src/anthropic-prepare-tools.ts
30
31
  import {
31
32
  UnsupportedFunctionalityError
32
33
  } from "@ai-sdk/provider";
34
+ function prepareTools(mode) {
35
+ var _a;
36
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
37
+ const toolWarnings = [];
38
+ const betas = /* @__PURE__ */ new Set();
39
+ if (tools == null) {
40
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
41
+ }
42
+ const anthropicTools2 = [];
43
+ for (const tool of tools) {
44
+ switch (tool.type) {
45
+ case "function":
46
+ anthropicTools2.push({
47
+ name: tool.name,
48
+ description: tool.description,
49
+ input_schema: tool.parameters
50
+ });
51
+ break;
52
+ case "provider-defined":
53
+ switch (tool.id) {
54
+ case "anthropic.computer_20250124":
55
+ betas.add("computer-use-2025-01-24");
56
+ anthropicTools2.push({
57
+ name: tool.name,
58
+ type: "computer_20250124",
59
+ display_width_px: tool.args.displayWidthPx,
60
+ display_height_px: tool.args.displayHeightPx,
61
+ display_number: tool.args.displayNumber
62
+ });
63
+ break;
64
+ case "anthropic.computer_20241022":
65
+ betas.add("computer-use-2024-10-22");
66
+ anthropicTools2.push({
67
+ name: tool.name,
68
+ type: "computer_20241022",
69
+ display_width_px: tool.args.displayWidthPx,
70
+ display_height_px: tool.args.displayHeightPx,
71
+ display_number: tool.args.displayNumber
72
+ });
73
+ break;
74
+ case "anthropic.text_editor_20241022":
75
+ betas.add("computer-use-2024-10-22");
76
+ anthropicTools2.push({
77
+ name: tool.name,
78
+ type: "text_editor_20241022"
79
+ });
80
+ break;
81
+ case "anthropic.bash_20241022":
82
+ betas.add("computer-use-2024-10-22");
83
+ anthropicTools2.push({
84
+ name: tool.name,
85
+ type: "bash_20241022"
86
+ });
87
+ break;
88
+ default:
89
+ toolWarnings.push({ type: "unsupported-tool", tool });
90
+ break;
91
+ }
92
+ break;
93
+ default:
94
+ toolWarnings.push({ type: "unsupported-tool", tool });
95
+ break;
96
+ }
97
+ }
98
+ const toolChoice = mode.toolChoice;
99
+ if (toolChoice == null) {
100
+ return {
101
+ tools: anthropicTools2,
102
+ tool_choice: void 0,
103
+ toolWarnings,
104
+ betas
105
+ };
106
+ }
107
+ const type = toolChoice.type;
108
+ switch (type) {
109
+ case "auto":
110
+ return {
111
+ tools: anthropicTools2,
112
+ tool_choice: { type: "auto" },
113
+ toolWarnings,
114
+ betas
115
+ };
116
+ case "required":
117
+ return {
118
+ tools: anthropicTools2,
119
+ tool_choice: { type: "any" },
120
+ toolWarnings,
121
+ betas
122
+ };
123
+ case "none":
124
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
125
+ case "tool":
126
+ return {
127
+ tools: anthropicTools2,
128
+ tool_choice: { type: "tool", name: toolChoice.toolName },
129
+ toolWarnings,
130
+ betas
131
+ };
132
+ default: {
133
+ const _exhaustiveCheck = type;
134
+ throw new UnsupportedFunctionalityError({
135
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
136
+ });
137
+ }
138
+ }
139
+ }
140
+
141
+ // src/convert-to-anthropic-messages-prompt.ts
142
+ import {
143
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
144
+ } from "@ai-sdk/provider";
33
145
  import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
34
146
  function convertToAnthropicMessagesPrompt({
35
147
  prompt
@@ -52,7 +164,7 @@ function convertToAnthropicMessagesPrompt({
52
164
  switch (type) {
53
165
  case "system": {
54
166
  if (system != null) {
55
- throw new UnsupportedFunctionalityError({
167
+ throw new UnsupportedFunctionalityError2({
56
168
  functionality: "Multiple system messages that are separated by user/assistant messages"
57
169
  });
58
170
  }
@@ -84,7 +196,7 @@ function convertToAnthropicMessagesPrompt({
84
196
  }
85
197
  case "image": {
86
198
  if (part.image instanceof URL) {
87
- throw new UnsupportedFunctionalityError({
199
+ throw new UnsupportedFunctionalityError2({
88
200
  functionality: "Image URLs in user messages"
89
201
  });
90
202
  }
@@ -101,12 +213,12 @@ function convertToAnthropicMessagesPrompt({
101
213
  }
102
214
  case "file": {
103
215
  if (part.data instanceof URL) {
104
- throw new UnsupportedFunctionalityError({
216
+ throw new UnsupportedFunctionalityError2({
105
217
  functionality: "Image URLs in user messages"
106
218
  });
107
219
  }
108
220
  if (part.mimeType !== "application/pdf") {
109
- throw new UnsupportedFunctionalityError({
221
+ throw new UnsupportedFunctionalityError2({
110
222
  functionality: "Non-PDF files in user messages"
111
223
  });
112
224
  }
@@ -195,6 +307,23 @@ function convertToAnthropicMessagesPrompt({
195
307
  });
196
308
  break;
197
309
  }
310
+ case "reasoning": {
311
+ anthropicContent.push({
312
+ type: "thinking",
313
+ thinking: part.text,
314
+ signature: part.signature,
315
+ cache_control: cacheControl
316
+ });
317
+ break;
318
+ }
319
+ case "redacted-reasoning": {
320
+ anthropicContent.push({
321
+ type: "redacted_thinking",
322
+ data: part.data,
323
+ cache_control: cacheControl
324
+ });
325
+ break;
326
+ }
198
327
  case "tool-call": {
199
328
  anthropicContent.push({
200
329
  type: "tool_use",
@@ -205,6 +334,10 @@ function convertToAnthropicMessagesPrompt({
205
334
  });
206
335
  break;
207
336
  }
337
+ default: {
338
+ const _exhaustiveCheck = part;
339
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
340
+ }
208
341
  }
209
342
  }
210
343
  }
@@ -284,105 +417,6 @@ function mapAnthropicStopReason(finishReason) {
284
417
  }
285
418
  }
286
419
 
287
- // src/anthropic-prepare-tools.ts
288
- import {
289
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
290
- } from "@ai-sdk/provider";
291
- function prepareTools(mode) {
292
- var _a;
293
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
294
- const toolWarnings = [];
295
- const betas = /* @__PURE__ */ new Set();
296
- if (tools == null) {
297
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
298
- }
299
- const anthropicTools2 = [];
300
- for (const tool of tools) {
301
- switch (tool.type) {
302
- case "function":
303
- anthropicTools2.push({
304
- name: tool.name,
305
- description: tool.description,
306
- input_schema: tool.parameters
307
- });
308
- break;
309
- case "provider-defined":
310
- betas.add("computer-use-2024-10-22");
311
- switch (tool.id) {
312
- case "anthropic.computer_20241022":
313
- anthropicTools2.push({
314
- name: tool.name,
315
- type: "computer_20241022",
316
- display_width_px: tool.args.displayWidthPx,
317
- display_height_px: tool.args.displayHeightPx,
318
- display_number: tool.args.displayNumber
319
- });
320
- break;
321
- case "anthropic.text_editor_20241022":
322
- anthropicTools2.push({
323
- name: tool.name,
324
- type: "text_editor_20241022"
325
- });
326
- break;
327
- case "anthropic.bash_20241022":
328
- anthropicTools2.push({
329
- name: tool.name,
330
- type: "bash_20241022"
331
- });
332
- break;
333
- default:
334
- toolWarnings.push({ type: "unsupported-tool", tool });
335
- break;
336
- }
337
- break;
338
- default:
339
- toolWarnings.push({ type: "unsupported-tool", tool });
340
- break;
341
- }
342
- }
343
- const toolChoice = mode.toolChoice;
344
- if (toolChoice == null) {
345
- return {
346
- tools: anthropicTools2,
347
- tool_choice: void 0,
348
- toolWarnings,
349
- betas
350
- };
351
- }
352
- const type = toolChoice.type;
353
- switch (type) {
354
- case "auto":
355
- return {
356
- tools: anthropicTools2,
357
- tool_choice: { type: "auto" },
358
- toolWarnings,
359
- betas
360
- };
361
- case "required":
362
- return {
363
- tools: anthropicTools2,
364
- tool_choice: { type: "any" },
365
- toolWarnings,
366
- betas
367
- };
368
- case "none":
369
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
370
- case "tool":
371
- return {
372
- tools: anthropicTools2,
373
- tool_choice: { type: "tool", name: toolChoice.toolName },
374
- toolWarnings,
375
- betas
376
- };
377
- default: {
378
- const _exhaustiveCheck = type;
379
- throw new UnsupportedFunctionalityError2({
380
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
381
- });
382
- }
383
- }
384
- }
385
-
386
420
  // src/anthropic-messages-language-model.ts
387
421
  var AnthropicMessagesLanguageModel = class {
388
422
  constructor(modelId, settings, config) {
@@ -399,7 +433,8 @@ var AnthropicMessagesLanguageModel = class {
399
433
  async getArgs({
400
434
  mode,
401
435
  prompt,
402
- maxTokens,
436
+ maxTokens = 4096,
437
+ // 4096: max model output tokens TODO update default in v5
403
438
  temperature,
404
439
  topP,
405
440
  topK,
@@ -407,8 +442,10 @@ var AnthropicMessagesLanguageModel = class {
407
442
  presencePenalty,
408
443
  stopSequences,
409
444
  responseFormat,
410
- seed
445
+ seed,
446
+ providerMetadata: providerOptions
411
447
  }) {
448
+ var _a, _b, _c;
412
449
  const type = mode.type;
413
450
  const warnings = [];
414
451
  if (frequencyPenalty != null) {
@@ -439,20 +476,67 @@ var AnthropicMessagesLanguageModel = class {
439
476
  const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
440
477
  prompt
441
478
  });
479
+ const thinkingOptions = thinkingOptionsSchema.safeParse(
480
+ (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.thinking
481
+ );
482
+ if (!thinkingOptions.success) {
483
+ throw new InvalidArgumentError({
484
+ argument: "providerOptions.anthropic.thinking",
485
+ message: "invalid thinking options",
486
+ cause: thinkingOptions.error
487
+ });
488
+ }
489
+ const isThinking = ((_b = thinkingOptions.data) == null ? void 0 : _b.type) === "enabled";
490
+ const thinkingBudget = (_c = thinkingOptions.data) == null ? void 0 : _c.budgetTokens;
442
491
  const baseArgs = {
443
492
  // model id:
444
493
  model: this.modelId,
445
494
  // standardized settings:
446
- max_tokens: maxTokens != null ? maxTokens : 4096,
447
- // 4096: max model output tokens TODO remove
495
+ max_tokens: maxTokens,
448
496
  temperature,
449
497
  top_k: topK,
450
498
  top_p: topP,
451
499
  stop_sequences: stopSequences,
500
+ // provider specific settings:
501
+ ...isThinking && {
502
+ thinking: { type: "enabled", budget_tokens: thinkingBudget }
503
+ },
452
504
  // prompt:
453
505
  system: messagesPrompt.system,
454
506
  messages: messagesPrompt.messages
455
507
  };
508
+ if (isThinking) {
509
+ if (thinkingBudget == null) {
510
+ throw new UnsupportedFunctionalityError3({
511
+ functionality: "thinking requires a budget"
512
+ });
513
+ }
514
+ if (baseArgs.temperature != null) {
515
+ baseArgs.temperature = void 0;
516
+ warnings.push({
517
+ type: "unsupported-setting",
518
+ setting: "temperature",
519
+ details: "temperature is not supported when thinking is enabled"
520
+ });
521
+ }
522
+ if (topK != null) {
523
+ baseArgs.top_k = void 0;
524
+ warnings.push({
525
+ type: "unsupported-setting",
526
+ setting: "topK",
527
+ details: "topK is not supported when thinking is enabled"
528
+ });
529
+ }
530
+ if (topP != null) {
531
+ baseArgs.top_p = void 0;
532
+ warnings.push({
533
+ type: "unsupported-setting",
534
+ setting: "topP",
535
+ details: "topP is not supported when thinking is enabled"
536
+ });
537
+ }
538
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
539
+ }
456
540
  switch (type) {
457
541
  case "regular": {
458
542
  const {
@@ -543,8 +627,21 @@ var AnthropicMessagesLanguageModel = class {
543
627
  }
544
628
  }
545
629
  }
630
+ const reasoning = response.content.filter(
631
+ (content) => content.type === "redacted_thinking" || content.type === "thinking"
632
+ ).map(
633
+ (content) => content.type === "thinking" ? {
634
+ type: "text",
635
+ text: content.thinking,
636
+ signature: content.signature
637
+ } : {
638
+ type: "redacted",
639
+ data: content.data
640
+ }
641
+ );
546
642
  return {
547
643
  text,
644
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
548
645
  toolCalls,
549
646
  finishReason: mapAnthropicStopReason(response.stop_reason),
550
647
  usage: {
@@ -589,7 +686,7 @@ var AnthropicMessagesLanguageModel = class {
589
686
  };
590
687
  const toolCallContentBlocks = {};
591
688
  let providerMetadata = void 0;
592
- const self = this;
689
+ let blockType = void 0;
593
690
  return {
594
691
  stream: response.pipeThrough(
595
692
  new TransformStream({
@@ -606,8 +703,17 @@ var AnthropicMessagesLanguageModel = class {
606
703
  }
607
704
  case "content_block_start": {
608
705
  const contentBlockType = value.content_block.type;
706
+ blockType = contentBlockType;
609
707
  switch (contentBlockType) {
610
- case "text": {
708
+ case "text":
709
+ case "thinking": {
710
+ return;
711
+ }
712
+ case "redacted_thinking": {
713
+ controller.enqueue({
714
+ type: "redacted-reasoning",
715
+ data: value.content_block.data
716
+ });
611
717
  return;
612
718
  }
613
719
  case "tool_use": {
@@ -638,6 +744,7 @@ var AnthropicMessagesLanguageModel = class {
638
744
  });
639
745
  delete toolCallContentBlocks[value.index];
640
746
  }
747
+ blockType = void 0;
641
748
  return;
642
749
  }
643
750
  case "content_block_delta": {
@@ -650,6 +757,22 @@ var AnthropicMessagesLanguageModel = class {
650
757
  });
651
758
  return;
652
759
  }
760
+ case "thinking_delta": {
761
+ controller.enqueue({
762
+ type: "reasoning",
763
+ textDelta: value.delta.thinking
764
+ });
765
+ return;
766
+ }
767
+ case "signature_delta": {
768
+ if (blockType === "thinking") {
769
+ controller.enqueue({
770
+ type: "reasoning-signature",
771
+ signature: value.delta.signature
772
+ });
773
+ }
774
+ return;
775
+ }
653
776
  case "input_json_delta": {
654
777
  const contentBlock = toolCallContentBlocks[value.index];
655
778
  controller.enqueue({
@@ -729,6 +852,15 @@ var anthropicMessagesResponseSchema = z2.object({
729
852
  type: z2.literal("text"),
730
853
  text: z2.string()
731
854
  }),
855
+ z2.object({
856
+ type: z2.literal("thinking"),
857
+ thinking: z2.string(),
858
+ signature: z2.string()
859
+ }),
860
+ z2.object({
861
+ type: z2.literal("redacted_thinking"),
862
+ data: z2.string()
863
+ }),
732
864
  z2.object({
733
865
  type: z2.literal("tool_use"),
734
866
  id: z2.string(),
@@ -767,10 +899,18 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
767
899
  type: z2.literal("text"),
768
900
  text: z2.string()
769
901
  }),
902
+ z2.object({
903
+ type: z2.literal("thinking"),
904
+ thinking: z2.string()
905
+ }),
770
906
  z2.object({
771
907
  type: z2.literal("tool_use"),
772
908
  id: z2.string(),
773
909
  name: z2.string()
910
+ }),
911
+ z2.object({
912
+ type: z2.literal("redacted_thinking"),
913
+ data: z2.string()
774
914
  })
775
915
  ])
776
916
  }),
@@ -785,6 +925,14 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
785
925
  z2.object({
786
926
  type: z2.literal("text_delta"),
787
927
  text: z2.string()
928
+ }),
929
+ z2.object({
930
+ type: z2.literal("thinking_delta"),
931
+ thinking: z2.string()
932
+ }),
933
+ z2.object({
934
+ type: z2.literal("signature_delta"),
935
+ signature: z2.string()
788
936
  })
789
937
  ])
790
938
  }),
@@ -811,6 +959,10 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
811
959
  type: z2.literal("ping")
812
960
  })
813
961
  ]);
962
+ var thinkingOptionsSchema = z2.object({
963
+ type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
964
+ budgetTokens: z2.number().optional()
965
+ }).optional();
814
966
 
815
967
  // src/anthropic-tools.ts
816
968
  import { z as z3 } from "zod";
@@ -877,10 +1029,51 @@ function computerTool_20241022(options) {
877
1029
  experimental_toToolResultContent: options.experimental_toToolResultContent
878
1030
  };
879
1031
  }
1032
+ var Computer20250124Parameters = z3.object({
1033
+ action: z3.enum([
1034
+ "key",
1035
+ "hold_key",
1036
+ "type",
1037
+ "cursor_position",
1038
+ "mouse_move",
1039
+ "left_mouse_down",
1040
+ "left_mouse_up",
1041
+ "left_click",
1042
+ "left_click_drag",
1043
+ "right_click",
1044
+ "middle_click",
1045
+ "double_click",
1046
+ "triple_click",
1047
+ "scroll",
1048
+ "wait",
1049
+ "screenshot"
1050
+ ]),
1051
+ coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1052
+ duration: z3.number().optional(),
1053
+ scroll_amount: z3.number().optional(),
1054
+ scroll_direction: z3.enum(["up", "down", "left", "right"]).optional(),
1055
+ start_coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1056
+ text: z3.string().optional()
1057
+ });
1058
+ function computerTool_20250124(options) {
1059
+ return {
1060
+ type: "provider-defined",
1061
+ id: "anthropic.computer_20250124",
1062
+ args: {
1063
+ displayWidthPx: options.displayWidthPx,
1064
+ displayHeightPx: options.displayHeightPx,
1065
+ displayNumber: options.displayNumber
1066
+ },
1067
+ parameters: Computer20250124Parameters,
1068
+ execute: options.execute,
1069
+ experimental_toToolResultContent: options.experimental_toToolResultContent
1070
+ };
1071
+ }
880
1072
  var anthropicTools = {
881
1073
  bash_20241022: bashTool_20241022,
882
1074
  textEditor_20241022: textEditorTool_20241022,
883
- computer_20241022: computerTool_20241022
1075
+ computer_20241022: computerTool_20241022,
1076
+ computer_20250124: computerTool_20250124
884
1077
  };
885
1078
  export {
886
1079
  AnthropicMessagesLanguageModel,