@ai-sdk/anthropic 1.1.8 → 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.
@@ -45,8 +45,114 @@ 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
+ betas.add("computer-use-2024-10-22");
70
+ switch (tool.id) {
71
+ case "anthropic.computer_20250124":
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
+ anthropicTools2.push({
82
+ name: tool.name,
83
+ type: "computer_20241022",
84
+ display_width_px: tool.args.displayWidthPx,
85
+ display_height_px: tool.args.displayHeightPx,
86
+ display_number: tool.args.displayNumber
87
+ });
88
+ break;
89
+ case "anthropic.text_editor_20241022":
90
+ anthropicTools2.push({
91
+ name: tool.name,
92
+ type: "text_editor_20241022"
93
+ });
94
+ break;
95
+ case "anthropic.bash_20241022":
96
+ anthropicTools2.push({
97
+ name: tool.name,
98
+ type: "bash_20241022"
99
+ });
100
+ break;
101
+ default:
102
+ toolWarnings.push({ type: "unsupported-tool", tool });
103
+ break;
104
+ }
105
+ break;
106
+ default:
107
+ toolWarnings.push({ type: "unsupported-tool", tool });
108
+ break;
109
+ }
110
+ }
111
+ const toolChoice = mode.toolChoice;
112
+ if (toolChoice == null) {
113
+ return {
114
+ tools: anthropicTools2,
115
+ tool_choice: void 0,
116
+ toolWarnings,
117
+ betas
118
+ };
119
+ }
120
+ const type = toolChoice.type;
121
+ switch (type) {
122
+ case "auto":
123
+ return {
124
+ tools: anthropicTools2,
125
+ tool_choice: { type: "auto" },
126
+ toolWarnings,
127
+ betas
128
+ };
129
+ case "required":
130
+ return {
131
+ tools: anthropicTools2,
132
+ tool_choice: { type: "any" },
133
+ toolWarnings,
134
+ betas
135
+ };
136
+ case "none":
137
+ return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
138
+ case "tool":
139
+ return {
140
+ tools: anthropicTools2,
141
+ tool_choice: { type: "tool", name: toolChoice.toolName },
142
+ toolWarnings,
143
+ betas
144
+ };
145
+ default: {
146
+ const _exhaustiveCheck = type;
147
+ throw new import_provider.UnsupportedFunctionalityError({
148
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
149
+ });
150
+ }
151
+ }
152
+ }
153
+
154
+ // src/convert-to-anthropic-messages-prompt.ts
155
+ var import_provider2 = require("@ai-sdk/provider");
50
156
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
51
157
  function convertToAnthropicMessagesPrompt({
52
158
  prompt
@@ -69,7 +175,7 @@ function convertToAnthropicMessagesPrompt({
69
175
  switch (type) {
70
176
  case "system": {
71
177
  if (system != null) {
72
- throw new import_provider.UnsupportedFunctionalityError({
178
+ throw new import_provider2.UnsupportedFunctionalityError({
73
179
  functionality: "Multiple system messages that are separated by user/assistant messages"
74
180
  });
75
181
  }
@@ -101,7 +207,7 @@ function convertToAnthropicMessagesPrompt({
101
207
  }
102
208
  case "image": {
103
209
  if (part.image instanceof URL) {
104
- throw new import_provider.UnsupportedFunctionalityError({
210
+ throw new import_provider2.UnsupportedFunctionalityError({
105
211
  functionality: "Image URLs in user messages"
106
212
  });
107
213
  }
@@ -118,12 +224,12 @@ function convertToAnthropicMessagesPrompt({
118
224
  }
119
225
  case "file": {
120
226
  if (part.data instanceof URL) {
121
- throw new import_provider.UnsupportedFunctionalityError({
227
+ throw new import_provider2.UnsupportedFunctionalityError({
122
228
  functionality: "Image URLs in user messages"
123
229
  });
124
230
  }
125
231
  if (part.mimeType !== "application/pdf") {
126
- throw new import_provider.UnsupportedFunctionalityError({
232
+ throw new import_provider2.UnsupportedFunctionalityError({
127
233
  functionality: "Non-PDF files in user messages"
128
234
  });
129
235
  }
@@ -212,6 +318,23 @@ function convertToAnthropicMessagesPrompt({
212
318
  });
213
319
  break;
214
320
  }
321
+ case "reasoning": {
322
+ anthropicContent.push({
323
+ type: "thinking",
324
+ thinking: part.text,
325
+ signature: part.signature,
326
+ cache_control: cacheControl
327
+ });
328
+ break;
329
+ }
330
+ case "redacted-reasoning": {
331
+ anthropicContent.push({
332
+ type: "redacted_thinking",
333
+ data: part.data,
334
+ cache_control: cacheControl
335
+ });
336
+ break;
337
+ }
215
338
  case "tool-call": {
216
339
  anthropicContent.push({
217
340
  type: "tool_use",
@@ -222,6 +345,10 @@ function convertToAnthropicMessagesPrompt({
222
345
  });
223
346
  break;
224
347
  }
348
+ default: {
349
+ const _exhaustiveCheck = part;
350
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
351
+ }
225
352
  }
226
353
  }
227
354
  }
@@ -301,103 +428,6 @@ function mapAnthropicStopReason(finishReason) {
301
428
  }
302
429
  }
303
430
 
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
431
  // src/anthropic-messages-language-model.ts
402
432
  var AnthropicMessagesLanguageModel = class {
403
433
  constructor(modelId, settings, config) {
@@ -414,7 +444,8 @@ var AnthropicMessagesLanguageModel = class {
414
444
  async getArgs({
415
445
  mode,
416
446
  prompt,
417
- maxTokens,
447
+ maxTokens = 4096,
448
+ // 4096: max model output tokens TODO update default in v5
418
449
  temperature,
419
450
  topP,
420
451
  topK,
@@ -422,8 +453,10 @@ var AnthropicMessagesLanguageModel = class {
422
453
  presencePenalty,
423
454
  stopSequences,
424
455
  responseFormat,
425
- seed
456
+ seed,
457
+ providerMetadata: providerOptions
426
458
  }) {
459
+ var _a, _b, _c;
427
460
  const type = mode.type;
428
461
  const warnings = [];
429
462
  if (frequencyPenalty != null) {
@@ -454,20 +487,67 @@ var AnthropicMessagesLanguageModel = class {
454
487
  const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
455
488
  prompt
456
489
  });
490
+ const thinkingOptions = thinkingOptionsSchema.safeParse(
491
+ (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.thinking
492
+ );
493
+ if (!thinkingOptions.success) {
494
+ throw new import_provider3.InvalidArgumentError({
495
+ argument: "providerOptions.anthropic.thinking",
496
+ message: "invalid thinking options",
497
+ cause: thinkingOptions.error
498
+ });
499
+ }
500
+ const isThinking = ((_b = thinkingOptions.data) == null ? void 0 : _b.type) === "enabled";
501
+ const thinkingBudget = (_c = thinkingOptions.data) == null ? void 0 : _c.budgetTokens;
457
502
  const baseArgs = {
458
503
  // model id:
459
504
  model: this.modelId,
460
505
  // standardized settings:
461
- max_tokens: maxTokens != null ? maxTokens : 4096,
462
- // 4096: max model output tokens TODO remove
506
+ max_tokens: maxTokens,
463
507
  temperature,
464
508
  top_k: topK,
465
509
  top_p: topP,
466
510
  stop_sequences: stopSequences,
511
+ // provider specific settings:
512
+ ...isThinking && {
513
+ thinking: { type: "enabled", budget_tokens: thinkingBudget }
514
+ },
467
515
  // prompt:
468
516
  system: messagesPrompt.system,
469
517
  messages: messagesPrompt.messages
470
518
  };
519
+ if (isThinking) {
520
+ if (thinkingBudget == null) {
521
+ throw new import_provider3.UnsupportedFunctionalityError({
522
+ functionality: "thinking requires a budget"
523
+ });
524
+ }
525
+ if (baseArgs.temperature != null) {
526
+ baseArgs.temperature = void 0;
527
+ warnings.push({
528
+ type: "unsupported-setting",
529
+ setting: "temperature",
530
+ details: "temperature is not supported when thinking is enabled"
531
+ });
532
+ }
533
+ if (topK != null) {
534
+ baseArgs.top_k = void 0;
535
+ warnings.push({
536
+ type: "unsupported-setting",
537
+ setting: "topK",
538
+ details: "topK is not supported when thinking is enabled"
539
+ });
540
+ }
541
+ if (topP != null) {
542
+ baseArgs.top_p = void 0;
543
+ warnings.push({
544
+ type: "unsupported-setting",
545
+ setting: "topP",
546
+ details: "topP is not supported when thinking is enabled"
547
+ });
548
+ }
549
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
550
+ }
471
551
  switch (type) {
472
552
  case "regular": {
473
553
  const {
@@ -558,8 +638,21 @@ var AnthropicMessagesLanguageModel = class {
558
638
  }
559
639
  }
560
640
  }
641
+ const reasoning = response.content.filter(
642
+ (content) => content.type === "redacted_thinking" || content.type === "thinking"
643
+ ).map(
644
+ (content) => content.type === "thinking" ? {
645
+ type: "text",
646
+ text: content.thinking,
647
+ signature: content.signature
648
+ } : {
649
+ type: "redacted",
650
+ data: content.data
651
+ }
652
+ );
561
653
  return {
562
654
  text,
655
+ reasoning: reasoning.length > 0 ? reasoning : void 0,
563
656
  toolCalls,
564
657
  finishReason: mapAnthropicStopReason(response.stop_reason),
565
658
  usage: {
@@ -604,7 +697,7 @@ var AnthropicMessagesLanguageModel = class {
604
697
  };
605
698
  const toolCallContentBlocks = {};
606
699
  let providerMetadata = void 0;
607
- const self = this;
700
+ let blockType = void 0;
608
701
  return {
609
702
  stream: response.pipeThrough(
610
703
  new TransformStream({
@@ -621,8 +714,17 @@ var AnthropicMessagesLanguageModel = class {
621
714
  }
622
715
  case "content_block_start": {
623
716
  const contentBlockType = value.content_block.type;
717
+ blockType = contentBlockType;
624
718
  switch (contentBlockType) {
625
- case "text": {
719
+ case "text":
720
+ case "thinking": {
721
+ return;
722
+ }
723
+ case "redacted_thinking": {
724
+ controller.enqueue({
725
+ type: "redacted-reasoning",
726
+ data: value.content_block.data
727
+ });
626
728
  return;
627
729
  }
628
730
  case "tool_use": {
@@ -653,6 +755,7 @@ var AnthropicMessagesLanguageModel = class {
653
755
  });
654
756
  delete toolCallContentBlocks[value.index];
655
757
  }
758
+ blockType = void 0;
656
759
  return;
657
760
  }
658
761
  case "content_block_delta": {
@@ -665,6 +768,22 @@ var AnthropicMessagesLanguageModel = class {
665
768
  });
666
769
  return;
667
770
  }
771
+ case "thinking_delta": {
772
+ controller.enqueue({
773
+ type: "reasoning",
774
+ textDelta: value.delta.thinking
775
+ });
776
+ return;
777
+ }
778
+ case "signature_delta": {
779
+ if (blockType === "thinking") {
780
+ controller.enqueue({
781
+ type: "reasoning-signature",
782
+ signature: value.delta.signature
783
+ });
784
+ }
785
+ return;
786
+ }
668
787
  case "input_json_delta": {
669
788
  const contentBlock = toolCallContentBlocks[value.index];
670
789
  controller.enqueue({
@@ -744,6 +863,15 @@ var anthropicMessagesResponseSchema = import_zod2.z.object({
744
863
  type: import_zod2.z.literal("text"),
745
864
  text: import_zod2.z.string()
746
865
  }),
866
+ import_zod2.z.object({
867
+ type: import_zod2.z.literal("thinking"),
868
+ thinking: import_zod2.z.string(),
869
+ signature: import_zod2.z.string()
870
+ }),
871
+ import_zod2.z.object({
872
+ type: import_zod2.z.literal("redacted_thinking"),
873
+ data: import_zod2.z.string()
874
+ }),
747
875
  import_zod2.z.object({
748
876
  type: import_zod2.z.literal("tool_use"),
749
877
  id: import_zod2.z.string(),
@@ -782,10 +910,18 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
782
910
  type: import_zod2.z.literal("text"),
783
911
  text: import_zod2.z.string()
784
912
  }),
913
+ import_zod2.z.object({
914
+ type: import_zod2.z.literal("thinking"),
915
+ thinking: import_zod2.z.string()
916
+ }),
785
917
  import_zod2.z.object({
786
918
  type: import_zod2.z.literal("tool_use"),
787
919
  id: import_zod2.z.string(),
788
920
  name: import_zod2.z.string()
921
+ }),
922
+ import_zod2.z.object({
923
+ type: import_zod2.z.literal("redacted_thinking"),
924
+ data: import_zod2.z.string()
789
925
  })
790
926
  ])
791
927
  }),
@@ -800,6 +936,14 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
800
936
  import_zod2.z.object({
801
937
  type: import_zod2.z.literal("text_delta"),
802
938
  text: import_zod2.z.string()
939
+ }),
940
+ import_zod2.z.object({
941
+ type: import_zod2.z.literal("thinking_delta"),
942
+ thinking: import_zod2.z.string()
943
+ }),
944
+ import_zod2.z.object({
945
+ type: import_zod2.z.literal("signature_delta"),
946
+ signature: import_zod2.z.string()
803
947
  })
804
948
  ])
805
949
  }),
@@ -826,6 +970,10 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
826
970
  type: import_zod2.z.literal("ping")
827
971
  })
828
972
  ]);
973
+ var thinkingOptionsSchema = import_zod2.z.object({
974
+ type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
975
+ budgetTokens: import_zod2.z.number().optional()
976
+ }).optional();
829
977
 
830
978
  // src/anthropic-tools.ts
831
979
  var import_zod3 = require("zod");
@@ -892,10 +1040,25 @@ function computerTool_20241022(options) {
892
1040
  experimental_toToolResultContent: options.experimental_toToolResultContent
893
1041
  };
894
1042
  }
1043
+ function computerTool_20250124(options) {
1044
+ return {
1045
+ type: "provider-defined",
1046
+ id: "anthropic.computer_20250124",
1047
+ args: {
1048
+ displayWidthPx: options.displayWidthPx,
1049
+ displayHeightPx: options.displayHeightPx,
1050
+ displayNumber: options.displayNumber
1051
+ },
1052
+ parameters: Computer20241022Parameters,
1053
+ execute: options.execute,
1054
+ experimental_toToolResultContent: options.experimental_toToolResultContent
1055
+ };
1056
+ }
895
1057
  var anthropicTools = {
896
1058
  bash_20241022: bashTool_20241022,
897
1059
  textEditor_20241022: textEditorTool_20241022,
898
- computer_20241022: computerTool_20241022
1060
+ computer_20241022: computerTool_20241022,
1061
+ computer_20250124: computerTool_20250124
899
1062
  };
900
1063
  // Annotate the CommonJS export names for ESM import in node:
901
1064
  0 && (module.exports = {