@ai-sdk/anthropic 4.0.0-beta.2 → 4.0.0-beta.21

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.
@@ -8,9 +8,13 @@ import {
8
8
  createJsonResponseHandler,
9
9
  createToolNameMapping,
10
10
  generateId,
11
+ isCustomReasoning,
12
+ mapReasoningToProviderBudget,
13
+ mapReasoningToProviderEffort,
11
14
  parseProviderOptions as parseProviderOptions2,
12
15
  postJsonToApi,
13
- resolve
16
+ resolve,
17
+ resolveProviderReference as resolveProviderReference2
14
18
  } from "@ai-sdk/provider-utils";
15
19
 
16
20
  // src/anthropic-error.ts
@@ -848,6 +852,20 @@ var anthropicLanguageModelOptions = z3.object({
848
852
  type: z3.literal("ephemeral"),
849
853
  ttl: z3.union([z3.literal("5m"), z3.literal("1h")]).optional()
850
854
  }).optional(),
855
+ /**
856
+ * Metadata to include with the request.
857
+ *
858
+ * See https://platform.claude.com/docs/en/api/messages/create for details.
859
+ */
860
+ metadata: z3.object({
861
+ /**
862
+ * An external identifier for the user associated with the request.
863
+ *
864
+ * Should be a UUID, hash value, or other opaque identifier.
865
+ * Must not contain PII (name, email, phone number, etc.).
866
+ */
867
+ userId: z3.string().optional()
868
+ }).optional(),
851
869
  /**
852
870
  * MCP servers to be utilized in this request.
853
871
  */
@@ -871,11 +889,18 @@ var anthropicLanguageModelOptions = z3.object({
871
889
  container: z3.object({
872
890
  id: z3.string().optional(),
873
891
  skills: z3.array(
874
- z3.object({
875
- type: z3.union([z3.literal("anthropic"), z3.literal("custom")]),
876
- skillId: z3.string(),
877
- version: z3.string().optional()
878
- })
892
+ z3.discriminatedUnion("type", [
893
+ z3.object({
894
+ type: z3.literal("anthropic"),
895
+ skillId: z3.string(),
896
+ version: z3.string().optional()
897
+ }),
898
+ z3.object({
899
+ type: z3.literal("custom"),
900
+ providerReference: z3.record(z3.string(), z3.string()),
901
+ version: z3.string().optional()
902
+ })
903
+ ])
879
904
  ).optional()
880
905
  }).optional(),
881
906
  /**
@@ -1266,7 +1291,8 @@ async function prepareTools({
1266
1291
  toolChoice,
1267
1292
  disableParallelToolUse,
1268
1293
  cacheControlValidator,
1269
- supportsStructuredOutput
1294
+ supportsStructuredOutput,
1295
+ supportsStrictTools
1270
1296
  }) {
1271
1297
  var _a;
1272
1298
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
@@ -1288,13 +1314,20 @@ async function prepareTools({
1288
1314
  const eagerInputStreaming = anthropicOptions == null ? void 0 : anthropicOptions.eagerInputStreaming;
1289
1315
  const deferLoading = anthropicOptions == null ? void 0 : anthropicOptions.deferLoading;
1290
1316
  const allowedCallers = anthropicOptions == null ? void 0 : anthropicOptions.allowedCallers;
1317
+ if (!supportsStrictTools && tool.strict != null) {
1318
+ toolWarnings.push({
1319
+ type: "unsupported",
1320
+ feature: "strict",
1321
+ details: `Tool '${tool.name}' has strict: ${tool.strict}, but strict mode is not supported by this provider. The strict property will be ignored.`
1322
+ });
1323
+ }
1291
1324
  anthropicTools2.push({
1292
1325
  name: tool.name,
1293
1326
  description: tool.description,
1294
1327
  input_schema: tool.inputSchema,
1295
1328
  cache_control: cacheControl,
1296
1329
  ...eagerInputStreaming ? { eager_input_streaming: true } : {},
1297
- ...supportsStructuredOutput === true && tool.strict != null ? { strict: tool.strict } : {},
1330
+ ...supportsStrictTools === true && tool.strict != null ? { strict: tool.strict } : {},
1298
1331
  ...deferLoading != null ? { defer_loading: deferLoading } : {},
1299
1332
  ...allowedCallers != null ? { allowed_callers: allowedCallers } : {},
1300
1333
  ...tool.inputExamples != null ? {
@@ -1510,7 +1543,6 @@ async function prepareTools({
1510
1543
  break;
1511
1544
  }
1512
1545
  case "anthropic.tool_search_regex_20251119": {
1513
- betas.add("advanced-tool-use-2025-11-20");
1514
1546
  anthropicTools2.push({
1515
1547
  type: "tool_search_tool_regex_20251119",
1516
1548
  name: "tool_search_tool_regex"
@@ -1518,7 +1550,6 @@ async function prepareTools({
1518
1550
  break;
1519
1551
  }
1520
1552
  case "anthropic.tool_search_bm25_20251119": {
1521
- betas.add("advanced-tool-use-2025-11-20");
1522
1553
  anthropicTools2.push({
1523
1554
  type: "tool_search_tool_bm25_20251119",
1524
1555
  name: "tool_search_tool_bm25"
@@ -1643,7 +1674,9 @@ import {
1643
1674
  import {
1644
1675
  convertBase64ToUint8Array,
1645
1676
  convertToBase64,
1677
+ isProviderReference,
1646
1678
  parseProviderOptions,
1679
+ resolveProviderReference,
1647
1680
  validateTypes as validateTypes2,
1648
1681
  isNonNullable
1649
1682
  } from "@ai-sdk/provider-utils";
@@ -2075,7 +2108,26 @@ async function convertToAnthropicMessagesPrompt({
2075
2108
  break;
2076
2109
  }
2077
2110
  case "file": {
2078
- if (part.mediaType.startsWith("image/")) {
2111
+ if (isProviderReference(part.data)) {
2112
+ const fileId = resolveProviderReference({
2113
+ reference: part.data,
2114
+ provider: "anthropic"
2115
+ });
2116
+ betas.add("files-api-2025-04-14");
2117
+ if (part.mediaType.startsWith("image/")) {
2118
+ anthropicContent.push({
2119
+ type: "image",
2120
+ source: { type: "file", file_id: fileId },
2121
+ cache_control: cacheControl
2122
+ });
2123
+ } else {
2124
+ anthropicContent.push({
2125
+ type: "document",
2126
+ source: { type: "file", file_id: fileId },
2127
+ cache_control: cacheControl
2128
+ });
2129
+ }
2130
+ } else if (part.mediaType.startsWith("image/")) {
2079
2131
  anthropicContent.push({
2080
2132
  type: "image",
2081
2133
  source: isUrlData(part.data) ? {
@@ -2851,7 +2903,7 @@ function createCitationSource(citation, citationDocuments, generateId2) {
2851
2903
  }
2852
2904
  var AnthropicMessagesLanguageModel = class {
2853
2905
  constructor(modelId, config) {
2854
- this.specificationVersion = "v3";
2906
+ this.specificationVersion = "v4";
2855
2907
  var _a;
2856
2908
  this.modelId = modelId;
2857
2909
  this.config = config;
@@ -2890,10 +2942,11 @@ var AnthropicMessagesLanguageModel = class {
2890
2942
  seed,
2891
2943
  tools,
2892
2944
  toolChoice,
2945
+ reasoning,
2893
2946
  providerOptions,
2894
2947
  stream
2895
2948
  }) {
2896
- var _a, _b, _c, _d, _e, _f, _g;
2949
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2897
2950
  const warnings = [];
2898
2951
  if (frequencyPenalty != null) {
2899
2952
  warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
@@ -2948,10 +3001,13 @@ var AnthropicMessagesLanguageModel = class {
2948
3001
  const {
2949
3002
  maxOutputTokens: maxOutputTokensForModel,
2950
3003
  supportsStructuredOutput: modelSupportsStructuredOutput,
3004
+ supportsAdaptiveThinking,
2951
3005
  isKnownModel
2952
3006
  } = getModelCapabilities(this.modelId);
3007
+ const isAnthropicModel = isKnownModel || this.modelId.startsWith("claude-");
2953
3008
  const supportsStructuredOutput = ((_a = this.config.supportsNativeStructuredOutput) != null ? _a : true) && modelSupportsStructuredOutput;
2954
- const structureOutputMode = (_b = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _b : "auto";
3009
+ const supportsStrictTools = ((_b = this.config.supportsStrictTools) != null ? _b : true) && modelSupportsStructuredOutput;
3010
+ const structureOutputMode = (_c = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _c : "auto";
2955
3011
  const useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
2956
3012
  const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useStructuredOutput ? {
2957
3013
  type: "function",
@@ -2986,14 +3042,28 @@ var AnthropicMessagesLanguageModel = class {
2986
3042
  });
2987
3043
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
2988
3044
  prompt,
2989
- sendReasoning: (_c = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _c : true,
3045
+ sendReasoning: (_d = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _d : true,
2990
3046
  warnings,
2991
3047
  cacheControlValidator,
2992
3048
  toolNameMapping
2993
3049
  });
2994
- const thinkingType = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.type;
3050
+ if (isCustomReasoning(reasoning) && (anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null && (anthropicOptions == null ? void 0 : anthropicOptions.effort) == null) {
3051
+ const reasoningConfig = resolveAnthropicReasoningConfig({
3052
+ reasoning,
3053
+ supportsAdaptiveThinking,
3054
+ maxOutputTokensForModel,
3055
+ warnings
3056
+ });
3057
+ if (reasoningConfig != null) {
3058
+ anthropicOptions.thinking = reasoningConfig.thinking;
3059
+ if (reasoningConfig.effort != null) {
3060
+ anthropicOptions.effort = reasoningConfig.effort;
3061
+ }
3062
+ }
3063
+ }
3064
+ const thinkingType = (_e = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _e.type;
2995
3065
  const isThinking = thinkingType === "enabled" || thinkingType === "adaptive";
2996
- let thinkingBudget = thinkingType === "enabled" ? (_e = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _e.budgetTokens : void 0;
3066
+ let thinkingBudget = thinkingType === "enabled" ? (_f = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _f.budgetTokens : void 0;
2997
3067
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
2998
3068
  const baseArgs = {
2999
3069
  // model id:
@@ -3030,6 +3100,9 @@ var AnthropicMessagesLanguageModel = class {
3030
3100
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
3031
3101
  cache_control: anthropicOptions.cacheControl
3032
3102
  },
3103
+ ...((_g = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _g.userId) != null && {
3104
+ metadata: { user_id: anthropicOptions.metadata.userId }
3105
+ },
3033
3106
  // mcp servers:
3034
3107
  ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
3035
3108
  mcp_servers: anthropicOptions.mcpServers.map((server) => ({
@@ -3051,7 +3124,10 @@ var AnthropicMessagesLanguageModel = class {
3051
3124
  id: anthropicOptions.container.id,
3052
3125
  skills: anthropicOptions.container.skills.map((skill) => ({
3053
3126
  type: skill.type,
3054
- skill_id: skill.skillId,
3127
+ skill_id: skill.type === "custom" ? resolveProviderReference2({
3128
+ reference: skill.providerReference,
3129
+ provider: "anthropic"
3130
+ }) : skill.skillId,
3055
3131
  version: skill.version
3056
3132
  }))
3057
3133
  }
@@ -3153,7 +3229,7 @@ var AnthropicMessagesLanguageModel = class {
3153
3229
  }
3154
3230
  baseArgs.max_tokens = maxTokens + (thinkingBudget != null ? thinkingBudget : 0);
3155
3231
  } else {
3156
- if (topP != null && temperature != null) {
3232
+ if (isAnthropicModel && topP != null && temperature != null) {
3157
3233
  warnings.push({
3158
3234
  type: "unsupported",
3159
3235
  feature: "topP",
@@ -3200,7 +3276,7 @@ var AnthropicMessagesLanguageModel = class {
3200
3276
  if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
3201
3277
  betas.add("fast-mode-2026-02-01");
3202
3278
  }
3203
- if (stream && ((_f = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _f : true)) {
3279
+ if (stream && ((_h = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _h : true)) {
3204
3280
  betas.add("fine-grained-tool-streaming-2025-05-14");
3205
3281
  }
3206
3282
  const {
@@ -3214,13 +3290,15 @@ var AnthropicMessagesLanguageModel = class {
3214
3290
  toolChoice: { type: "required" },
3215
3291
  disableParallelToolUse: true,
3216
3292
  cacheControlValidator,
3217
- supportsStructuredOutput: false
3293
+ supportsStructuredOutput: false,
3294
+ supportsStrictTools
3218
3295
  } : {
3219
3296
  tools: tools != null ? tools : [],
3220
3297
  toolChoice,
3221
3298
  disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
3222
3299
  cacheControlValidator,
3223
- supportsStructuredOutput
3300
+ supportsStructuredOutput,
3301
+ supportsStrictTools
3224
3302
  }
3225
3303
  );
3226
3304
  const cacheWarnings = cacheControlValidator.getWarnings();
@@ -3237,7 +3315,7 @@ var AnthropicMessagesLanguageModel = class {
3237
3315
  ...betas,
3238
3316
  ...toolsBetas,
3239
3317
  ...userSuppliedBetas,
3240
- ...(_g = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _g : []
3318
+ ...(_i = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _i : []
3241
3319
  ]),
3242
3320
  usesJsonResponseTool: jsonResponseTool != null,
3243
3321
  toolNameMapping,
@@ -4500,42 +4578,49 @@ function getModelCapabilities(modelId) {
4500
4578
  return {
4501
4579
  maxOutputTokens: 128e3,
4502
4580
  supportsStructuredOutput: true,
4581
+ supportsAdaptiveThinking: true,
4503
4582
  isKnownModel: true
4504
4583
  };
4505
4584
  } else if (modelId.includes("claude-sonnet-4-5") || modelId.includes("claude-opus-4-5") || modelId.includes("claude-haiku-4-5")) {
4506
4585
  return {
4507
4586
  maxOutputTokens: 64e3,
4508
4587
  supportsStructuredOutput: true,
4588
+ supportsAdaptiveThinking: false,
4509
4589
  isKnownModel: true
4510
4590
  };
4511
4591
  } else if (modelId.includes("claude-opus-4-1")) {
4512
4592
  return {
4513
4593
  maxOutputTokens: 32e3,
4514
4594
  supportsStructuredOutput: true,
4595
+ supportsAdaptiveThinking: false,
4515
4596
  isKnownModel: true
4516
4597
  };
4517
4598
  } else if (modelId.includes("claude-sonnet-4-")) {
4518
4599
  return {
4519
4600
  maxOutputTokens: 64e3,
4520
4601
  supportsStructuredOutput: false,
4602
+ supportsAdaptiveThinking: false,
4521
4603
  isKnownModel: true
4522
4604
  };
4523
4605
  } else if (modelId.includes("claude-opus-4-")) {
4524
4606
  return {
4525
4607
  maxOutputTokens: 32e3,
4526
4608
  supportsStructuredOutput: false,
4609
+ supportsAdaptiveThinking: false,
4527
4610
  isKnownModel: true
4528
4611
  };
4529
4612
  } else if (modelId.includes("claude-3-haiku")) {
4530
4613
  return {
4531
4614
  maxOutputTokens: 4096,
4532
4615
  supportsStructuredOutput: false,
4616
+ supportsAdaptiveThinking: false,
4533
4617
  isKnownModel: true
4534
4618
  };
4535
4619
  } else {
4536
4620
  return {
4537
4621
  maxOutputTokens: 4096,
4538
4622
  supportsStructuredOutput: false,
4623
+ supportsAdaptiveThinking: false,
4539
4624
  isKnownModel: false
4540
4625
  };
4541
4626
  }
@@ -4558,6 +4643,43 @@ function hasWebTool20260209WithoutCodeExecution(tools) {
4558
4643
  }
4559
4644
  return hasWebTool20260209 && !hasCodeExecutionTool;
4560
4645
  }
4646
+ function resolveAnthropicReasoningConfig({
4647
+ reasoning,
4648
+ supportsAdaptiveThinking,
4649
+ maxOutputTokensForModel,
4650
+ warnings
4651
+ }) {
4652
+ if (!isCustomReasoning(reasoning)) {
4653
+ return void 0;
4654
+ }
4655
+ if (reasoning === "none") {
4656
+ return { thinking: { type: "disabled" } };
4657
+ }
4658
+ if (supportsAdaptiveThinking) {
4659
+ const effort = mapReasoningToProviderEffort({
4660
+ reasoning,
4661
+ effortMap: {
4662
+ minimal: "low",
4663
+ low: "low",
4664
+ medium: "medium",
4665
+ high: "high",
4666
+ xhigh: "max"
4667
+ },
4668
+ warnings
4669
+ });
4670
+ return { thinking: { type: "adaptive" }, effort };
4671
+ }
4672
+ const budgetTokens = mapReasoningToProviderBudget({
4673
+ reasoning,
4674
+ maxOutputTokens: maxOutputTokensForModel,
4675
+ maxReasoningBudget: maxOutputTokensForModel,
4676
+ warnings
4677
+ });
4678
+ if (budgetTokens == null) {
4679
+ return void 0;
4680
+ }
4681
+ return { thinking: { type: "enabled", budgetTokens } };
4682
+ }
4561
4683
  function mapAnthropicResponseContextManagement(contextManagement) {
4562
4684
  return contextManagement ? {
4563
4685
  appliedEdits: contextManagement.applied_edits.map((edit) => {
@@ -5120,6 +5242,7 @@ var anthropicTools = {
5120
5242
  export {
5121
5243
  AnthropicMessagesLanguageModel,
5122
5244
  anthropicTools,
5245
+ getModelCapabilities,
5123
5246
  prepareTools
5124
5247
  };
5125
5248
  //# sourceMappingURL=index.mjs.map