@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.
@@ -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,118 @@ 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
+ betas.add("computer-use-2024-10-22");
54
+ switch (tool.id) {
55
+ case "anthropic.computer_20250124":
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
+ anthropicTools2.push({
66
+ name: tool.name,
67
+ type: "computer_20241022",
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.text_editor_20241022":
74
+ anthropicTools2.push({
75
+ name: tool.name,
76
+ type: "text_editor_20241022"
77
+ });
78
+ break;
79
+ case "anthropic.bash_20241022":
80
+ anthropicTools2.push({
81
+ name: tool.name,
82
+ type: "bash_20241022"
83
+ });
84
+ break;
85
+ default:
86
+ toolWarnings.push({ type: "unsupported-tool", tool });
87
+ break;
88
+ }
89
+ break;
90
+ default:
91
+ toolWarnings.push({ type: "unsupported-tool", tool });
92
+ break;
93
+ }
94
+ }
95
+ const toolChoice = mode.toolChoice;
96
+ if (toolChoice == null) {
97
+ return {
98
+ tools: anthropicTools2,
99
+ tool_choice: void 0,
100
+ toolWarnings,
101
+ betas
102
+ };
103
+ }
104
+ const type = toolChoice.type;
105
+ switch (type) {
106
+ case "auto":
107
+ return {
108
+ tools: anthropicTools2,
109
+ tool_choice: { type: "auto" },
110
+ toolWarnings,
111
+ betas
112
+ };
113
+ case "required":
114
+ return {
115
+ tools: anthropicTools2,
116
+ tool_choice: { type: "any" },
117
+ toolWarnings,
118
+ betas
119
+ };
120
+ case "none":
121
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
122
+ case "tool":
123
+ return {
124
+ tools: anthropicTools2,
125
+ tool_choice: { type: "tool", name: toolChoice.toolName },
126
+ toolWarnings,
127
+ betas
128
+ };
129
+ default: {
130
+ const _exhaustiveCheck = type;
131
+ throw new UnsupportedFunctionalityError({
132
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
133
+ });
134
+ }
135
+ }
136
+ }
137
+
138
+ // src/convert-to-anthropic-messages-prompt.ts
139
+ import {
140
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
141
+ } from "@ai-sdk/provider";
33
142
  import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
34
143
  function convertToAnthropicMessagesPrompt({
35
144
  prompt
@@ -52,7 +161,7 @@ function convertToAnthropicMessagesPrompt({
52
161
  switch (type) {
53
162
  case "system": {
54
163
  if (system != null) {
55
- throw new UnsupportedFunctionalityError({
164
+ throw new UnsupportedFunctionalityError2({
56
165
  functionality: "Multiple system messages that are separated by user/assistant messages"
57
166
  });
58
167
  }
@@ -84,7 +193,7 @@ function convertToAnthropicMessagesPrompt({
84
193
  }
85
194
  case "image": {
86
195
  if (part.image instanceof URL) {
87
- throw new UnsupportedFunctionalityError({
196
+ throw new UnsupportedFunctionalityError2({
88
197
  functionality: "Image URLs in user messages"
89
198
  });
90
199
  }
@@ -101,12 +210,12 @@ function convertToAnthropicMessagesPrompt({
101
210
  }
102
211
  case "file": {
103
212
  if (part.data instanceof URL) {
104
- throw new UnsupportedFunctionalityError({
213
+ throw new UnsupportedFunctionalityError2({
105
214
  functionality: "Image URLs in user messages"
106
215
  });
107
216
  }
108
217
  if (part.mimeType !== "application/pdf") {
109
- throw new UnsupportedFunctionalityError({
218
+ throw new UnsupportedFunctionalityError2({
110
219
  functionality: "Non-PDF files in user messages"
111
220
  });
112
221
  }
@@ -195,6 +304,23 @@ function convertToAnthropicMessagesPrompt({
195
304
  });
196
305
  break;
197
306
  }
307
+ case "reasoning": {
308
+ anthropicContent.push({
309
+ type: "thinking",
310
+ thinking: part.text,
311
+ signature: part.signature,
312
+ cache_control: cacheControl
313
+ });
314
+ break;
315
+ }
316
+ case "redacted-reasoning": {
317
+ anthropicContent.push({
318
+ type: "redacted_thinking",
319
+ data: part.data,
320
+ cache_control: cacheControl
321
+ });
322
+ break;
323
+ }
198
324
  case "tool-call": {
199
325
  anthropicContent.push({
200
326
  type: "tool_use",
@@ -205,6 +331,10 @@ function convertToAnthropicMessagesPrompt({
205
331
  });
206
332
  break;
207
333
  }
334
+ default: {
335
+ const _exhaustiveCheck = part;
336
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
337
+ }
208
338
  }
209
339
  }
210
340
  }
@@ -284,105 +414,6 @@ function mapAnthropicStopReason(finishReason) {
284
414
  }
285
415
  }
286
416
 
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
417
  // src/anthropic-messages-language-model.ts
387
418
  var AnthropicMessagesLanguageModel = class {
388
419
  constructor(modelId, settings, config) {
@@ -399,7 +430,8 @@ var AnthropicMessagesLanguageModel = class {
399
430
  async getArgs({
400
431
  mode,
401
432
  prompt,
402
- maxTokens,
433
+ maxTokens = 4096,
434
+ // 4096: max model output tokens TODO update default in v5
403
435
  temperature,
404
436
  topP,
405
437
  topK,
@@ -407,8 +439,10 @@ var AnthropicMessagesLanguageModel = class {
407
439
  presencePenalty,
408
440
  stopSequences,
409
441
  responseFormat,
410
- seed
442
+ seed,
443
+ providerMetadata: providerOptions
411
444
  }) {
445
+ var _a, _b, _c;
412
446
  const type = mode.type;
413
447
  const warnings = [];
414
448
  if (frequencyPenalty != null) {
@@ -439,20 +473,67 @@ var AnthropicMessagesLanguageModel = class {
439
473
  const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
440
474
  prompt
441
475
  });
476
+ const thinkingOptions = thinkingOptionsSchema.safeParse(
477
+ (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.thinking
478
+ );
479
+ if (!thinkingOptions.success) {
480
+ throw new InvalidArgumentError({
481
+ argument: "providerOptions.anthropic.thinking",
482
+ message: "invalid thinking options",
483
+ cause: thinkingOptions.error
484
+ });
485
+ }
486
+ const isThinking = ((_b = thinkingOptions.data) == null ? void 0 : _b.type) === "enabled";
487
+ const thinkingBudget = (_c = thinkingOptions.data) == null ? void 0 : _c.budgetTokens;
442
488
  const baseArgs = {
443
489
  // model id:
444
490
  model: this.modelId,
445
491
  // standardized settings:
446
- max_tokens: maxTokens != null ? maxTokens : 4096,
447
- // 4096: max model output tokens TODO remove
492
+ max_tokens: maxTokens,
448
493
  temperature,
449
494
  top_k: topK,
450
495
  top_p: topP,
451
496
  stop_sequences: stopSequences,
497
+ // provider specific settings:
498
+ ...isThinking && {
499
+ thinking: { type: "enabled", budget_tokens: thinkingBudget }
500
+ },
452
501
  // prompt:
453
502
  system: messagesPrompt.system,
454
503
  messages: messagesPrompt.messages
455
504
  };
505
+ if (isThinking) {
506
+ if (thinkingBudget == null) {
507
+ throw new UnsupportedFunctionalityError3({
508
+ functionality: "thinking requires a budget"
509
+ });
510
+ }
511
+ if (baseArgs.temperature != null) {
512
+ baseArgs.temperature = void 0;
513
+ warnings.push({
514
+ type: "unsupported-setting",
515
+ setting: "temperature",
516
+ details: "temperature is not supported when thinking is enabled"
517
+ });
518
+ }
519
+ if (topK != null) {
520
+ baseArgs.top_k = void 0;
521
+ warnings.push({
522
+ type: "unsupported-setting",
523
+ setting: "topK",
524
+ details: "topK is not supported when thinking is enabled"
525
+ });
526
+ }
527
+ if (topP != null) {
528
+ baseArgs.top_p = void 0;
529
+ warnings.push({
530
+ type: "unsupported-setting",
531
+ setting: "topP",
532
+ details: "topP is not supported when thinking is enabled"
533
+ });
534
+ }
535
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
536
+ }
456
537
  switch (type) {
457
538
  case "regular": {
458
539
  const {
@@ -543,8 +624,21 @@ var AnthropicMessagesLanguageModel = class {
543
624
  }
544
625
  }
545
626
  }
627
+ const reasoning = response.content.filter(
628
+ (content) => content.type === "redacted_thinking" || content.type === "thinking"
629
+ ).map(
630
+ (content) => content.type === "thinking" ? {
631
+ type: "text",
632
+ text: content.thinking,
633
+ signature: content.signature
634
+ } : {
635
+ type: "redacted",
636
+ data: content.data
637
+ }
638
+ );
546
639
  return {
547
640
  text,
641
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
548
642
  toolCalls,
549
643
  finishReason: mapAnthropicStopReason(response.stop_reason),
550
644
  usage: {
@@ -589,7 +683,7 @@ var AnthropicMessagesLanguageModel = class {
589
683
  };
590
684
  const toolCallContentBlocks = {};
591
685
  let providerMetadata = void 0;
592
- const self = this;
686
+ let blockType = void 0;
593
687
  return {
594
688
  stream: response.pipeThrough(
595
689
  new TransformStream({
@@ -606,8 +700,17 @@ var AnthropicMessagesLanguageModel = class {
606
700
  }
607
701
  case "content_block_start": {
608
702
  const contentBlockType = value.content_block.type;
703
+ blockType = contentBlockType;
609
704
  switch (contentBlockType) {
610
- case "text": {
705
+ case "text":
706
+ case "thinking": {
707
+ return;
708
+ }
709
+ case "redacted_thinking": {
710
+ controller.enqueue({
711
+ type: "redacted-reasoning",
712
+ data: value.content_block.data
713
+ });
611
714
  return;
612
715
  }
613
716
  case "tool_use": {
@@ -638,6 +741,7 @@ var AnthropicMessagesLanguageModel = class {
638
741
  });
639
742
  delete toolCallContentBlocks[value.index];
640
743
  }
744
+ blockType = void 0;
641
745
  return;
642
746
  }
643
747
  case "content_block_delta": {
@@ -650,6 +754,22 @@ var AnthropicMessagesLanguageModel = class {
650
754
  });
651
755
  return;
652
756
  }
757
+ case "thinking_delta": {
758
+ controller.enqueue({
759
+ type: "reasoning",
760
+ textDelta: value.delta.thinking
761
+ });
762
+ return;
763
+ }
764
+ case "signature_delta": {
765
+ if (blockType === "thinking") {
766
+ controller.enqueue({
767
+ type: "reasoning-signature",
768
+ signature: value.delta.signature
769
+ });
770
+ }
771
+ return;
772
+ }
653
773
  case "input_json_delta": {
654
774
  const contentBlock = toolCallContentBlocks[value.index];
655
775
  controller.enqueue({
@@ -729,6 +849,15 @@ var anthropicMessagesResponseSchema = z2.object({
729
849
  type: z2.literal("text"),
730
850
  text: z2.string()
731
851
  }),
852
+ z2.object({
853
+ type: z2.literal("thinking"),
854
+ thinking: z2.string(),
855
+ signature: z2.string()
856
+ }),
857
+ z2.object({
858
+ type: z2.literal("redacted_thinking"),
859
+ data: z2.string()
860
+ }),
732
861
  z2.object({
733
862
  type: z2.literal("tool_use"),
734
863
  id: z2.string(),
@@ -767,10 +896,18 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
767
896
  type: z2.literal("text"),
768
897
  text: z2.string()
769
898
  }),
899
+ z2.object({
900
+ type: z2.literal("thinking"),
901
+ thinking: z2.string()
902
+ }),
770
903
  z2.object({
771
904
  type: z2.literal("tool_use"),
772
905
  id: z2.string(),
773
906
  name: z2.string()
907
+ }),
908
+ z2.object({
909
+ type: z2.literal("redacted_thinking"),
910
+ data: z2.string()
774
911
  })
775
912
  ])
776
913
  }),
@@ -785,6 +922,14 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
785
922
  z2.object({
786
923
  type: z2.literal("text_delta"),
787
924
  text: z2.string()
925
+ }),
926
+ z2.object({
927
+ type: z2.literal("thinking_delta"),
928
+ thinking: z2.string()
929
+ }),
930
+ z2.object({
931
+ type: z2.literal("signature_delta"),
932
+ signature: z2.string()
788
933
  })
789
934
  ])
790
935
  }),
@@ -811,6 +956,10 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
811
956
  type: z2.literal("ping")
812
957
  })
813
958
  ]);
959
+ var thinkingOptionsSchema = z2.object({
960
+ type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
961
+ budgetTokens: z2.number().optional()
962
+ }).optional();
814
963
 
815
964
  // src/anthropic-tools.ts
816
965
  import { z as z3 } from "zod";
@@ -877,10 +1026,25 @@ function computerTool_20241022(options) {
877
1026
  experimental_toToolResultContent: options.experimental_toToolResultContent
878
1027
  };
879
1028
  }
1029
+ function computerTool_20250124(options) {
1030
+ return {
1031
+ type: "provider-defined",
1032
+ id: "anthropic.computer_20250124",
1033
+ args: {
1034
+ displayWidthPx: options.displayWidthPx,
1035
+ displayHeightPx: options.displayHeightPx,
1036
+ displayNumber: options.displayNumber
1037
+ },
1038
+ parameters: Computer20241022Parameters,
1039
+ execute: options.execute,
1040
+ experimental_toToolResultContent: options.experimental_toToolResultContent
1041
+ };
1042
+ }
880
1043
  var anthropicTools = {
881
1044
  bash_20241022: bashTool_20241022,
882
1045
  textEditor_20241022: textEditorTool_20241022,
883
- computer_20241022: computerTool_20241022
1046
+ computer_20241022: computerTool_20241022,
1047
+ computer_20250124: computerTool_20250124
884
1048
  };
885
1049
  export {
886
1050
  AnthropicMessagesLanguageModel,