@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.mjs CHANGED
@@ -9,6 +9,7 @@ import {
9
9
 
10
10
  // src/anthropic-messages-language-model.ts
11
11
  import {
12
+ InvalidArgumentError,
12
13
  UnsupportedFunctionalityError as UnsupportedFunctionalityError3
13
14
  } from "@ai-sdk/provider";
14
15
  import {
@@ -35,10 +36,121 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
35
36
  errorToMessage: (data) => data.error.message
36
37
  });
37
38
 
38
- // src/convert-to-anthropic-messages-prompt.ts
39
+ // src/anthropic-prepare-tools.ts
39
40
  import {
40
41
  UnsupportedFunctionalityError
41
42
  } from "@ai-sdk/provider";
43
+ function prepareTools(mode) {
44
+ var _a;
45
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
46
+ const toolWarnings = [];
47
+ const betas = /* @__PURE__ */ new Set();
48
+ if (tools == null) {
49
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
50
+ }
51
+ const anthropicTools2 = [];
52
+ for (const tool of tools) {
53
+ switch (tool.type) {
54
+ case "function":
55
+ anthropicTools2.push({
56
+ name: tool.name,
57
+ description: tool.description,
58
+ input_schema: tool.parameters
59
+ });
60
+ break;
61
+ case "provider-defined":
62
+ switch (tool.id) {
63
+ case "anthropic.computer_20250124":
64
+ betas.add("computer-use-2025-01-24");
65
+ anthropicTools2.push({
66
+ name: tool.name,
67
+ type: "computer_20250124",
68
+ display_width_px: tool.args.displayWidthPx,
69
+ display_height_px: tool.args.displayHeightPx,
70
+ display_number: tool.args.displayNumber
71
+ });
72
+ break;
73
+ case "anthropic.computer_20241022":
74
+ betas.add("computer-use-2024-10-22");
75
+ anthropicTools2.push({
76
+ name: tool.name,
77
+ type: "computer_20241022",
78
+ display_width_px: tool.args.displayWidthPx,
79
+ display_height_px: tool.args.displayHeightPx,
80
+ display_number: tool.args.displayNumber
81
+ });
82
+ break;
83
+ case "anthropic.text_editor_20241022":
84
+ betas.add("computer-use-2024-10-22");
85
+ anthropicTools2.push({
86
+ name: tool.name,
87
+ type: "text_editor_20241022"
88
+ });
89
+ break;
90
+ case "anthropic.bash_20241022":
91
+ betas.add("computer-use-2024-10-22");
92
+ anthropicTools2.push({
93
+ name: tool.name,
94
+ type: "bash_20241022"
95
+ });
96
+ break;
97
+ default:
98
+ toolWarnings.push({ type: "unsupported-tool", tool });
99
+ break;
100
+ }
101
+ break;
102
+ default:
103
+ toolWarnings.push({ type: "unsupported-tool", tool });
104
+ break;
105
+ }
106
+ }
107
+ const toolChoice = mode.toolChoice;
108
+ if (toolChoice == null) {
109
+ return {
110
+ tools: anthropicTools2,
111
+ tool_choice: void 0,
112
+ toolWarnings,
113
+ betas
114
+ };
115
+ }
116
+ const type = toolChoice.type;
117
+ switch (type) {
118
+ case "auto":
119
+ return {
120
+ tools: anthropicTools2,
121
+ tool_choice: { type: "auto" },
122
+ toolWarnings,
123
+ betas
124
+ };
125
+ case "required":
126
+ return {
127
+ tools: anthropicTools2,
128
+ tool_choice: { type: "any" },
129
+ toolWarnings,
130
+ betas
131
+ };
132
+ case "none":
133
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
134
+ case "tool":
135
+ return {
136
+ tools: anthropicTools2,
137
+ tool_choice: { type: "tool", name: toolChoice.toolName },
138
+ toolWarnings,
139
+ betas
140
+ };
141
+ default: {
142
+ const _exhaustiveCheck = type;
143
+ throw new UnsupportedFunctionalityError({
144
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
145
+ });
146
+ }
147
+ }
148
+ }
149
+
150
+ // src/convert-to-anthropic-messages-prompt.ts
151
+ import {
152
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
153
+ } from "@ai-sdk/provider";
42
154
  import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
43
155
  function convertToAnthropicMessagesPrompt({
44
156
  prompt
@@ -61,7 +173,7 @@ function convertToAnthropicMessagesPrompt({
61
173
  switch (type) {
62
174
  case "system": {
63
175
  if (system != null) {
64
- throw new UnsupportedFunctionalityError({
176
+ throw new UnsupportedFunctionalityError2({
65
177
  functionality: "Multiple system messages that are separated by user/assistant messages"
66
178
  });
67
179
  }
@@ -93,7 +205,7 @@ function convertToAnthropicMessagesPrompt({
93
205
  }
94
206
  case "image": {
95
207
  if (part.image instanceof URL) {
96
- throw new UnsupportedFunctionalityError({
208
+ throw new UnsupportedFunctionalityError2({
97
209
  functionality: "Image URLs in user messages"
98
210
  });
99
211
  }
@@ -110,12 +222,12 @@ function convertToAnthropicMessagesPrompt({
110
222
  }
111
223
  case "file": {
112
224
  if (part.data instanceof URL) {
113
- throw new UnsupportedFunctionalityError({
225
+ throw new UnsupportedFunctionalityError2({
114
226
  functionality: "Image URLs in user messages"
115
227
  });
116
228
  }
117
229
  if (part.mimeType !== "application/pdf") {
118
- throw new UnsupportedFunctionalityError({
230
+ throw new UnsupportedFunctionalityError2({
119
231
  functionality: "Non-PDF files in user messages"
120
232
  });
121
233
  }
@@ -204,6 +316,23 @@ function convertToAnthropicMessagesPrompt({
204
316
  });
205
317
  break;
206
318
  }
319
+ case "reasoning": {
320
+ anthropicContent.push({
321
+ type: "thinking",
322
+ thinking: part.text,
323
+ signature: part.signature,
324
+ cache_control: cacheControl
325
+ });
326
+ break;
327
+ }
328
+ case "redacted-reasoning": {
329
+ anthropicContent.push({
330
+ type: "redacted_thinking",
331
+ data: part.data,
332
+ cache_control: cacheControl
333
+ });
334
+ break;
335
+ }
207
336
  case "tool-call": {
208
337
  anthropicContent.push({
209
338
  type: "tool_use",
@@ -214,6 +343,10 @@ function convertToAnthropicMessagesPrompt({
214
343
  });
215
344
  break;
216
345
  }
346
+ default: {
347
+ const _exhaustiveCheck = part;
348
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
349
+ }
217
350
  }
218
351
  }
219
352
  }
@@ -293,105 +426,6 @@ function mapAnthropicStopReason(finishReason) {
293
426
  }
294
427
  }
295
428
 
296
- // src/anthropic-prepare-tools.ts
297
- import {
298
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
299
- } from "@ai-sdk/provider";
300
- function prepareTools(mode) {
301
- var _a;
302
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
303
- const toolWarnings = [];
304
- const betas = /* @__PURE__ */ new Set();
305
- if (tools == null) {
306
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
307
- }
308
- const anthropicTools2 = [];
309
- for (const tool of tools) {
310
- switch (tool.type) {
311
- case "function":
312
- anthropicTools2.push({
313
- name: tool.name,
314
- description: tool.description,
315
- input_schema: tool.parameters
316
- });
317
- break;
318
- case "provider-defined":
319
- betas.add("computer-use-2024-10-22");
320
- switch (tool.id) {
321
- case "anthropic.computer_20241022":
322
- anthropicTools2.push({
323
- name: tool.name,
324
- type: "computer_20241022",
325
- display_width_px: tool.args.displayWidthPx,
326
- display_height_px: tool.args.displayHeightPx,
327
- display_number: tool.args.displayNumber
328
- });
329
- break;
330
- case "anthropic.text_editor_20241022":
331
- anthropicTools2.push({
332
- name: tool.name,
333
- type: "text_editor_20241022"
334
- });
335
- break;
336
- case "anthropic.bash_20241022":
337
- anthropicTools2.push({
338
- name: tool.name,
339
- type: "bash_20241022"
340
- });
341
- break;
342
- default:
343
- toolWarnings.push({ type: "unsupported-tool", tool });
344
- break;
345
- }
346
- break;
347
- default:
348
- toolWarnings.push({ type: "unsupported-tool", tool });
349
- break;
350
- }
351
- }
352
- const toolChoice = mode.toolChoice;
353
- if (toolChoice == null) {
354
- return {
355
- tools: anthropicTools2,
356
- tool_choice: void 0,
357
- toolWarnings,
358
- betas
359
- };
360
- }
361
- const type = toolChoice.type;
362
- switch (type) {
363
- case "auto":
364
- return {
365
- tools: anthropicTools2,
366
- tool_choice: { type: "auto" },
367
- toolWarnings,
368
- betas
369
- };
370
- case "required":
371
- return {
372
- tools: anthropicTools2,
373
- tool_choice: { type: "any" },
374
- toolWarnings,
375
- betas
376
- };
377
- case "none":
378
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
379
- case "tool":
380
- return {
381
- tools: anthropicTools2,
382
- tool_choice: { type: "tool", name: toolChoice.toolName },
383
- toolWarnings,
384
- betas
385
- };
386
- default: {
387
- const _exhaustiveCheck = type;
388
- throw new UnsupportedFunctionalityError2({
389
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
390
- });
391
- }
392
- }
393
- }
394
-
395
429
  // src/anthropic-messages-language-model.ts
396
430
  var AnthropicMessagesLanguageModel = class {
397
431
  constructor(modelId, settings, config) {
@@ -408,7 +442,8 @@ var AnthropicMessagesLanguageModel = class {
408
442
  async getArgs({
409
443
  mode,
410
444
  prompt,
411
- maxTokens,
445
+ maxTokens = 4096,
446
+ // 4096: max model output tokens TODO update default in v5
412
447
  temperature,
413
448
  topP,
414
449
  topK,
@@ -416,8 +451,10 @@ var AnthropicMessagesLanguageModel = class {
416
451
  presencePenalty,
417
452
  stopSequences,
418
453
  responseFormat,
419
- seed
454
+ seed,
455
+ providerMetadata: providerOptions
420
456
  }) {
457
+ var _a, _b, _c;
421
458
  const type = mode.type;
422
459
  const warnings = [];
423
460
  if (frequencyPenalty != null) {
@@ -448,20 +485,67 @@ var AnthropicMessagesLanguageModel = class {
448
485
  const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
449
486
  prompt
450
487
  });
488
+ const thinkingOptions = thinkingOptionsSchema.safeParse(
489
+ (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.thinking
490
+ );
491
+ if (!thinkingOptions.success) {
492
+ throw new InvalidArgumentError({
493
+ argument: "providerOptions.anthropic.thinking",
494
+ message: "invalid thinking options",
495
+ cause: thinkingOptions.error
496
+ });
497
+ }
498
+ const isThinking = ((_b = thinkingOptions.data) == null ? void 0 : _b.type) === "enabled";
499
+ const thinkingBudget = (_c = thinkingOptions.data) == null ? void 0 : _c.budgetTokens;
451
500
  const baseArgs = {
452
501
  // model id:
453
502
  model: this.modelId,
454
503
  // standardized settings:
455
- max_tokens: maxTokens != null ? maxTokens : 4096,
456
- // 4096: max model output tokens TODO remove
504
+ max_tokens: maxTokens,
457
505
  temperature,
458
506
  top_k: topK,
459
507
  top_p: topP,
460
508
  stop_sequences: stopSequences,
509
+ // provider specific settings:
510
+ ...isThinking && {
511
+ thinking: { type: "enabled", budget_tokens: thinkingBudget }
512
+ },
461
513
  // prompt:
462
514
  system: messagesPrompt.system,
463
515
  messages: messagesPrompt.messages
464
516
  };
517
+ if (isThinking) {
518
+ if (thinkingBudget == null) {
519
+ throw new UnsupportedFunctionalityError3({
520
+ functionality: "thinking requires a budget"
521
+ });
522
+ }
523
+ if (baseArgs.temperature != null) {
524
+ baseArgs.temperature = void 0;
525
+ warnings.push({
526
+ type: "unsupported-setting",
527
+ setting: "temperature",
528
+ details: "temperature is not supported when thinking is enabled"
529
+ });
530
+ }
531
+ if (topK != null) {
532
+ baseArgs.top_k = void 0;
533
+ warnings.push({
534
+ type: "unsupported-setting",
535
+ setting: "topK",
536
+ details: "topK is not supported when thinking is enabled"
537
+ });
538
+ }
539
+ if (topP != null) {
540
+ baseArgs.top_p = void 0;
541
+ warnings.push({
542
+ type: "unsupported-setting",
543
+ setting: "topP",
544
+ details: "topP is not supported when thinking is enabled"
545
+ });
546
+ }
547
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
548
+ }
465
549
  switch (type) {
466
550
  case "regular": {
467
551
  const {
@@ -552,8 +636,21 @@ var AnthropicMessagesLanguageModel = class {
552
636
  }
553
637
  }
554
638
  }
639
+ const reasoning = response.content.filter(
640
+ (content) => content.type === "redacted_thinking" || content.type === "thinking"
641
+ ).map(
642
+ (content) => content.type === "thinking" ? {
643
+ type: "text",
644
+ text: content.thinking,
645
+ signature: content.signature
646
+ } : {
647
+ type: "redacted",
648
+ data: content.data
649
+ }
650
+ );
555
651
  return {
556
652
  text,
653
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
557
654
  toolCalls,
558
655
  finishReason: mapAnthropicStopReason(response.stop_reason),
559
656
  usage: {
@@ -598,7 +695,7 @@ var AnthropicMessagesLanguageModel = class {
598
695
  };
599
696
  const toolCallContentBlocks = {};
600
697
  let providerMetadata = void 0;
601
- const self = this;
698
+ let blockType = void 0;
602
699
  return {
603
700
  stream: response.pipeThrough(
604
701
  new TransformStream({
@@ -615,8 +712,17 @@ var AnthropicMessagesLanguageModel = class {
615
712
  }
616
713
  case "content_block_start": {
617
714
  const contentBlockType = value.content_block.type;
715
+ blockType = contentBlockType;
618
716
  switch (contentBlockType) {
619
- case "text": {
717
+ case "text":
718
+ case "thinking": {
719
+ return;
720
+ }
721
+ case "redacted_thinking": {
722
+ controller.enqueue({
723
+ type: "redacted-reasoning",
724
+ data: value.content_block.data
725
+ });
620
726
  return;
621
727
  }
622
728
  case "tool_use": {
@@ -647,6 +753,7 @@ var AnthropicMessagesLanguageModel = class {
647
753
  });
648
754
  delete toolCallContentBlocks[value.index];
649
755
  }
756
+ blockType = void 0;
650
757
  return;
651
758
  }
652
759
  case "content_block_delta": {
@@ -659,6 +766,22 @@ var AnthropicMessagesLanguageModel = class {
659
766
  });
660
767
  return;
661
768
  }
769
+ case "thinking_delta": {
770
+ controller.enqueue({
771
+ type: "reasoning",
772
+ textDelta: value.delta.thinking
773
+ });
774
+ return;
775
+ }
776
+ case "signature_delta": {
777
+ if (blockType === "thinking") {
778
+ controller.enqueue({
779
+ type: "reasoning-signature",
780
+ signature: value.delta.signature
781
+ });
782
+ }
783
+ return;
784
+ }
662
785
  case "input_json_delta": {
663
786
  const contentBlock = toolCallContentBlocks[value.index];
664
787
  controller.enqueue({
@@ -738,6 +861,15 @@ var anthropicMessagesResponseSchema = z2.object({
738
861
  type: z2.literal("text"),
739
862
  text: z2.string()
740
863
  }),
864
+ z2.object({
865
+ type: z2.literal("thinking"),
866
+ thinking: z2.string(),
867
+ signature: z2.string()
868
+ }),
869
+ z2.object({
870
+ type: z2.literal("redacted_thinking"),
871
+ data: z2.string()
872
+ }),
741
873
  z2.object({
742
874
  type: z2.literal("tool_use"),
743
875
  id: z2.string(),
@@ -776,10 +908,18 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
776
908
  type: z2.literal("text"),
777
909
  text: z2.string()
778
910
  }),
911
+ z2.object({
912
+ type: z2.literal("thinking"),
913
+ thinking: z2.string()
914
+ }),
779
915
  z2.object({
780
916
  type: z2.literal("tool_use"),
781
917
  id: z2.string(),
782
918
  name: z2.string()
919
+ }),
920
+ z2.object({
921
+ type: z2.literal("redacted_thinking"),
922
+ data: z2.string()
783
923
  })
784
924
  ])
785
925
  }),
@@ -794,6 +934,14 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
794
934
  z2.object({
795
935
  type: z2.literal("text_delta"),
796
936
  text: z2.string()
937
+ }),
938
+ z2.object({
939
+ type: z2.literal("thinking_delta"),
940
+ thinking: z2.string()
941
+ }),
942
+ z2.object({
943
+ type: z2.literal("signature_delta"),
944
+ signature: z2.string()
797
945
  })
798
946
  ])
799
947
  }),
@@ -820,6 +968,10 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
820
968
  type: z2.literal("ping")
821
969
  })
822
970
  ]);
971
+ var thinkingOptionsSchema = z2.object({
972
+ type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
973
+ budgetTokens: z2.number().optional()
974
+ }).optional();
823
975
 
824
976
  // src/anthropic-tools.ts
825
977
  import { z as z3 } from "zod";
@@ -886,10 +1038,51 @@ function computerTool_20241022(options) {
886
1038
  experimental_toToolResultContent: options.experimental_toToolResultContent
887
1039
  };
888
1040
  }
1041
+ var Computer20250124Parameters = z3.object({
1042
+ action: z3.enum([
1043
+ "key",
1044
+ "hold_key",
1045
+ "type",
1046
+ "cursor_position",
1047
+ "mouse_move",
1048
+ "left_mouse_down",
1049
+ "left_mouse_up",
1050
+ "left_click",
1051
+ "left_click_drag",
1052
+ "right_click",
1053
+ "middle_click",
1054
+ "double_click",
1055
+ "triple_click",
1056
+ "scroll",
1057
+ "wait",
1058
+ "screenshot"
1059
+ ]),
1060
+ coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1061
+ duration: z3.number().optional(),
1062
+ scroll_amount: z3.number().optional(),
1063
+ scroll_direction: z3.enum(["up", "down", "left", "right"]).optional(),
1064
+ start_coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1065
+ text: z3.string().optional()
1066
+ });
1067
+ function computerTool_20250124(options) {
1068
+ return {
1069
+ type: "provider-defined",
1070
+ id: "anthropic.computer_20250124",
1071
+ args: {
1072
+ displayWidthPx: options.displayWidthPx,
1073
+ displayHeightPx: options.displayHeightPx,
1074
+ displayNumber: options.displayNumber
1075
+ },
1076
+ parameters: Computer20250124Parameters,
1077
+ execute: options.execute,
1078
+ experimental_toToolResultContent: options.experimental_toToolResultContent
1079
+ };
1080
+ }
889
1081
  var anthropicTools = {
890
1082
  bash_20241022: bashTool_20241022,
891
1083
  textEditor_20241022: textEditorTool_20241022,
892
- computer_20241022: computerTool_20241022
1084
+ computer_20241022: computerTool_20241022,
1085
+ computer_20250124: computerTool_20250124
893
1086
  };
894
1087
 
895
1088
  // src/anthropic-provider.ts