@ai-sdk/anthropic 1.1.9 → 1.1.10

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,118 @@ 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
+ betas.add("computer-use-2024-10-22");
63
+ switch (tool.id) {
64
+ case "anthropic.computer_20250124":
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
+ anthropicTools2.push({
75
+ name: tool.name,
76
+ type: "computer_20241022",
77
+ display_width_px: tool.args.displayWidthPx,
78
+ display_height_px: tool.args.displayHeightPx,
79
+ display_number: tool.args.displayNumber
80
+ });
81
+ break;
82
+ case "anthropic.text_editor_20241022":
83
+ anthropicTools2.push({
84
+ name: tool.name,
85
+ type: "text_editor_20241022"
86
+ });
87
+ break;
88
+ case "anthropic.bash_20241022":
89
+ anthropicTools2.push({
90
+ name: tool.name,
91
+ type: "bash_20241022"
92
+ });
93
+ break;
94
+ default:
95
+ toolWarnings.push({ type: "unsupported-tool", tool });
96
+ break;
97
+ }
98
+ break;
99
+ default:
100
+ toolWarnings.push({ type: "unsupported-tool", tool });
101
+ break;
102
+ }
103
+ }
104
+ const toolChoice = mode.toolChoice;
105
+ if (toolChoice == null) {
106
+ return {
107
+ tools: anthropicTools2,
108
+ tool_choice: void 0,
109
+ toolWarnings,
110
+ betas
111
+ };
112
+ }
113
+ const type = toolChoice.type;
114
+ switch (type) {
115
+ case "auto":
116
+ return {
117
+ tools: anthropicTools2,
118
+ tool_choice: { type: "auto" },
119
+ toolWarnings,
120
+ betas
121
+ };
122
+ case "required":
123
+ return {
124
+ tools: anthropicTools2,
125
+ tool_choice: { type: "any" },
126
+ toolWarnings,
127
+ betas
128
+ };
129
+ case "none":
130
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
131
+ case "tool":
132
+ return {
133
+ tools: anthropicTools2,
134
+ tool_choice: { type: "tool", name: toolChoice.toolName },
135
+ toolWarnings,
136
+ betas
137
+ };
138
+ default: {
139
+ const _exhaustiveCheck = type;
140
+ throw new UnsupportedFunctionalityError({
141
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
142
+ });
143
+ }
144
+ }
145
+ }
146
+
147
+ // src/convert-to-anthropic-messages-prompt.ts
148
+ import {
149
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
150
+ } from "@ai-sdk/provider";
42
151
  import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
43
152
  function convertToAnthropicMessagesPrompt({
44
153
  prompt
@@ -61,7 +170,7 @@ function convertToAnthropicMessagesPrompt({
61
170
  switch (type) {
62
171
  case "system": {
63
172
  if (system != null) {
64
- throw new UnsupportedFunctionalityError({
173
+ throw new UnsupportedFunctionalityError2({
65
174
  functionality: "Multiple system messages that are separated by user/assistant messages"
66
175
  });
67
176
  }
@@ -93,7 +202,7 @@ function convertToAnthropicMessagesPrompt({
93
202
  }
94
203
  case "image": {
95
204
  if (part.image instanceof URL) {
96
- throw new UnsupportedFunctionalityError({
205
+ throw new UnsupportedFunctionalityError2({
97
206
  functionality: "Image URLs in user messages"
98
207
  });
99
208
  }
@@ -110,12 +219,12 @@ function convertToAnthropicMessagesPrompt({
110
219
  }
111
220
  case "file": {
112
221
  if (part.data instanceof URL) {
113
- throw new UnsupportedFunctionalityError({
222
+ throw new UnsupportedFunctionalityError2({
114
223
  functionality: "Image URLs in user messages"
115
224
  });
116
225
  }
117
226
  if (part.mimeType !== "application/pdf") {
118
- throw new UnsupportedFunctionalityError({
227
+ throw new UnsupportedFunctionalityError2({
119
228
  functionality: "Non-PDF files in user messages"
120
229
  });
121
230
  }
@@ -204,6 +313,23 @@ function convertToAnthropicMessagesPrompt({
204
313
  });
205
314
  break;
206
315
  }
316
+ case "reasoning": {
317
+ anthropicContent.push({
318
+ type: "thinking",
319
+ thinking: part.text,
320
+ signature: part.signature,
321
+ cache_control: cacheControl
322
+ });
323
+ break;
324
+ }
325
+ case "redacted-reasoning": {
326
+ anthropicContent.push({
327
+ type: "redacted_thinking",
328
+ data: part.data,
329
+ cache_control: cacheControl
330
+ });
331
+ break;
332
+ }
207
333
  case "tool-call": {
208
334
  anthropicContent.push({
209
335
  type: "tool_use",
@@ -214,6 +340,10 @@ function convertToAnthropicMessagesPrompt({
214
340
  });
215
341
  break;
216
342
  }
343
+ default: {
344
+ const _exhaustiveCheck = part;
345
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
346
+ }
217
347
  }
218
348
  }
219
349
  }
@@ -293,105 +423,6 @@ function mapAnthropicStopReason(finishReason) {
293
423
  }
294
424
  }
295
425
 
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
426
  // src/anthropic-messages-language-model.ts
396
427
  var AnthropicMessagesLanguageModel = class {
397
428
  constructor(modelId, settings, config) {
@@ -408,7 +439,8 @@ var AnthropicMessagesLanguageModel = class {
408
439
  async getArgs({
409
440
  mode,
410
441
  prompt,
411
- maxTokens,
442
+ maxTokens = 4096,
443
+ // 4096: max model output tokens TODO update default in v5
412
444
  temperature,
413
445
  topP,
414
446
  topK,
@@ -416,8 +448,10 @@ var AnthropicMessagesLanguageModel = class {
416
448
  presencePenalty,
417
449
  stopSequences,
418
450
  responseFormat,
419
- seed
451
+ seed,
452
+ providerMetadata: providerOptions
420
453
  }) {
454
+ var _a, _b, _c;
421
455
  const type = mode.type;
422
456
  const warnings = [];
423
457
  if (frequencyPenalty != null) {
@@ -448,20 +482,67 @@ var AnthropicMessagesLanguageModel = class {
448
482
  const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
449
483
  prompt
450
484
  });
485
+ const thinkingOptions = thinkingOptionsSchema.safeParse(
486
+ (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.thinking
487
+ );
488
+ if (!thinkingOptions.success) {
489
+ throw new InvalidArgumentError({
490
+ argument: "providerOptions.anthropic.thinking",
491
+ message: "invalid thinking options",
492
+ cause: thinkingOptions.error
493
+ });
494
+ }
495
+ const isThinking = ((_b = thinkingOptions.data) == null ? void 0 : _b.type) === "enabled";
496
+ const thinkingBudget = (_c = thinkingOptions.data) == null ? void 0 : _c.budgetTokens;
451
497
  const baseArgs = {
452
498
  // model id:
453
499
  model: this.modelId,
454
500
  // standardized settings:
455
- max_tokens: maxTokens != null ? maxTokens : 4096,
456
- // 4096: max model output tokens TODO remove
501
+ max_tokens: maxTokens,
457
502
  temperature,
458
503
  top_k: topK,
459
504
  top_p: topP,
460
505
  stop_sequences: stopSequences,
506
+ // provider specific settings:
507
+ ...isThinking && {
508
+ thinking: { type: "enabled", budget_tokens: thinkingBudget }
509
+ },
461
510
  // prompt:
462
511
  system: messagesPrompt.system,
463
512
  messages: messagesPrompt.messages
464
513
  };
514
+ if (isThinking) {
515
+ if (thinkingBudget == null) {
516
+ throw new UnsupportedFunctionalityError3({
517
+ functionality: "thinking requires a budget"
518
+ });
519
+ }
520
+ if (baseArgs.temperature != null) {
521
+ baseArgs.temperature = void 0;
522
+ warnings.push({
523
+ type: "unsupported-setting",
524
+ setting: "temperature",
525
+ details: "temperature is not supported when thinking is enabled"
526
+ });
527
+ }
528
+ if (topK != null) {
529
+ baseArgs.top_k = void 0;
530
+ warnings.push({
531
+ type: "unsupported-setting",
532
+ setting: "topK",
533
+ details: "topK is not supported when thinking is enabled"
534
+ });
535
+ }
536
+ if (topP != null) {
537
+ baseArgs.top_p = void 0;
538
+ warnings.push({
539
+ type: "unsupported-setting",
540
+ setting: "topP",
541
+ details: "topP is not supported when thinking is enabled"
542
+ });
543
+ }
544
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
545
+ }
465
546
  switch (type) {
466
547
  case "regular": {
467
548
  const {
@@ -552,8 +633,21 @@ var AnthropicMessagesLanguageModel = class {
552
633
  }
553
634
  }
554
635
  }
636
+ const reasoning = response.content.filter(
637
+ (content) => content.type === "redacted_thinking" || content.type === "thinking"
638
+ ).map(
639
+ (content) => content.type === "thinking" ? {
640
+ type: "text",
641
+ text: content.thinking,
642
+ signature: content.signature
643
+ } : {
644
+ type: "redacted",
645
+ data: content.data
646
+ }
647
+ );
555
648
  return {
556
649
  text,
650
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
557
651
  toolCalls,
558
652
  finishReason: mapAnthropicStopReason(response.stop_reason),
559
653
  usage: {
@@ -598,7 +692,7 @@ var AnthropicMessagesLanguageModel = class {
598
692
  };
599
693
  const toolCallContentBlocks = {};
600
694
  let providerMetadata = void 0;
601
- const self = this;
695
+ let blockType = void 0;
602
696
  return {
603
697
  stream: response.pipeThrough(
604
698
  new TransformStream({
@@ -615,8 +709,17 @@ var AnthropicMessagesLanguageModel = class {
615
709
  }
616
710
  case "content_block_start": {
617
711
  const contentBlockType = value.content_block.type;
712
+ blockType = contentBlockType;
618
713
  switch (contentBlockType) {
619
- case "text": {
714
+ case "text":
715
+ case "thinking": {
716
+ return;
717
+ }
718
+ case "redacted_thinking": {
719
+ controller.enqueue({
720
+ type: "redacted-reasoning",
721
+ data: value.content_block.data
722
+ });
620
723
  return;
621
724
  }
622
725
  case "tool_use": {
@@ -647,6 +750,7 @@ var AnthropicMessagesLanguageModel = class {
647
750
  });
648
751
  delete toolCallContentBlocks[value.index];
649
752
  }
753
+ blockType = void 0;
650
754
  return;
651
755
  }
652
756
  case "content_block_delta": {
@@ -659,6 +763,22 @@ var AnthropicMessagesLanguageModel = class {
659
763
  });
660
764
  return;
661
765
  }
766
+ case "thinking_delta": {
767
+ controller.enqueue({
768
+ type: "reasoning",
769
+ textDelta: value.delta.thinking
770
+ });
771
+ return;
772
+ }
773
+ case "signature_delta": {
774
+ if (blockType === "thinking") {
775
+ controller.enqueue({
776
+ type: "reasoning-signature",
777
+ signature: value.delta.signature
778
+ });
779
+ }
780
+ return;
781
+ }
662
782
  case "input_json_delta": {
663
783
  const contentBlock = toolCallContentBlocks[value.index];
664
784
  controller.enqueue({
@@ -738,6 +858,15 @@ var anthropicMessagesResponseSchema = z2.object({
738
858
  type: z2.literal("text"),
739
859
  text: z2.string()
740
860
  }),
861
+ z2.object({
862
+ type: z2.literal("thinking"),
863
+ thinking: z2.string(),
864
+ signature: z2.string()
865
+ }),
866
+ z2.object({
867
+ type: z2.literal("redacted_thinking"),
868
+ data: z2.string()
869
+ }),
741
870
  z2.object({
742
871
  type: z2.literal("tool_use"),
743
872
  id: z2.string(),
@@ -776,10 +905,18 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
776
905
  type: z2.literal("text"),
777
906
  text: z2.string()
778
907
  }),
908
+ z2.object({
909
+ type: z2.literal("thinking"),
910
+ thinking: z2.string()
911
+ }),
779
912
  z2.object({
780
913
  type: z2.literal("tool_use"),
781
914
  id: z2.string(),
782
915
  name: z2.string()
916
+ }),
917
+ z2.object({
918
+ type: z2.literal("redacted_thinking"),
919
+ data: z2.string()
783
920
  })
784
921
  ])
785
922
  }),
@@ -794,6 +931,14 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
794
931
  z2.object({
795
932
  type: z2.literal("text_delta"),
796
933
  text: z2.string()
934
+ }),
935
+ z2.object({
936
+ type: z2.literal("thinking_delta"),
937
+ thinking: z2.string()
938
+ }),
939
+ z2.object({
940
+ type: z2.literal("signature_delta"),
941
+ signature: z2.string()
797
942
  })
798
943
  ])
799
944
  }),
@@ -820,6 +965,10 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
820
965
  type: z2.literal("ping")
821
966
  })
822
967
  ]);
968
+ var thinkingOptionsSchema = z2.object({
969
+ type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
970
+ budgetTokens: z2.number().optional()
971
+ }).optional();
823
972
 
824
973
  // src/anthropic-tools.ts
825
974
  import { z as z3 } from "zod";
@@ -886,10 +1035,25 @@ function computerTool_20241022(options) {
886
1035
  experimental_toToolResultContent: options.experimental_toToolResultContent
887
1036
  };
888
1037
  }
1038
+ function computerTool_20250124(options) {
1039
+ return {
1040
+ type: "provider-defined",
1041
+ id: "anthropic.computer_20250124",
1042
+ args: {
1043
+ displayWidthPx: options.displayWidthPx,
1044
+ displayHeightPx: options.displayHeightPx,
1045
+ displayNumber: options.displayNumber
1046
+ },
1047
+ parameters: Computer20241022Parameters,
1048
+ execute: options.execute,
1049
+ experimental_toToolResultContent: options.experimental_toToolResultContent
1050
+ };
1051
+ }
889
1052
  var anthropicTools = {
890
1053
  bash_20241022: bashTool_20241022,
891
1054
  textEditor_20241022: textEditorTool_20241022,
892
- computer_20241022: computerTool_20241022
1055
+ computer_20241022: computerTool_20241022,
1056
+ computer_20250124: computerTool_20250124
893
1057
  };
894
1058
 
895
1059
  // src/anthropic-provider.ts