@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.
@@ -18,13 +18,14 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/internal/index.ts
21
- var internal_exports = {};
22
- __export(internal_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  AnthropicMessagesLanguageModel: () => AnthropicMessagesLanguageModel,
24
24
  anthropicTools: () => anthropicTools,
25
+ getModelCapabilities: () => getModelCapabilities,
25
26
  prepareTools: () => prepareTools
26
27
  });
27
- module.exports = __toCommonJS(internal_exports);
28
+ module.exports = __toCommonJS(index_exports);
28
29
 
29
30
  // src/anthropic-messages-language-model.ts
30
31
  var import_provider3 = require("@ai-sdk/provider");
@@ -861,6 +862,20 @@ var anthropicLanguageModelOptions = import_v43.z.object({
861
862
  type: import_v43.z.literal("ephemeral"),
862
863
  ttl: import_v43.z.union([import_v43.z.literal("5m"), import_v43.z.literal("1h")]).optional()
863
864
  }).optional(),
865
+ /**
866
+ * Metadata to include with the request.
867
+ *
868
+ * See https://platform.claude.com/docs/en/api/messages/create for details.
869
+ */
870
+ metadata: import_v43.z.object({
871
+ /**
872
+ * An external identifier for the user associated with the request.
873
+ *
874
+ * Should be a UUID, hash value, or other opaque identifier.
875
+ * Must not contain PII (name, email, phone number, etc.).
876
+ */
877
+ userId: import_v43.z.string().optional()
878
+ }).optional(),
864
879
  /**
865
880
  * MCP servers to be utilized in this request.
866
881
  */
@@ -884,11 +899,18 @@ var anthropicLanguageModelOptions = import_v43.z.object({
884
899
  container: import_v43.z.object({
885
900
  id: import_v43.z.string().optional(),
886
901
  skills: import_v43.z.array(
887
- import_v43.z.object({
888
- type: import_v43.z.union([import_v43.z.literal("anthropic"), import_v43.z.literal("custom")]),
889
- skillId: import_v43.z.string(),
890
- version: import_v43.z.string().optional()
891
- })
902
+ import_v43.z.discriminatedUnion("type", [
903
+ import_v43.z.object({
904
+ type: import_v43.z.literal("anthropic"),
905
+ skillId: import_v43.z.string(),
906
+ version: import_v43.z.string().optional()
907
+ }),
908
+ import_v43.z.object({
909
+ type: import_v43.z.literal("custom"),
910
+ providerReference: import_v43.z.record(import_v43.z.string(), import_v43.z.string()),
911
+ version: import_v43.z.string().optional()
912
+ })
913
+ ])
892
914
  ).optional()
893
915
  }).optional(),
894
916
  /**
@@ -1261,7 +1283,8 @@ async function prepareTools({
1261
1283
  toolChoice,
1262
1284
  disableParallelToolUse,
1263
1285
  cacheControlValidator,
1264
- supportsStructuredOutput
1286
+ supportsStructuredOutput,
1287
+ supportsStrictTools
1265
1288
  }) {
1266
1289
  var _a;
1267
1290
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
@@ -1283,13 +1306,20 @@ async function prepareTools({
1283
1306
  const eagerInputStreaming = anthropicOptions == null ? void 0 : anthropicOptions.eagerInputStreaming;
1284
1307
  const deferLoading = anthropicOptions == null ? void 0 : anthropicOptions.deferLoading;
1285
1308
  const allowedCallers = anthropicOptions == null ? void 0 : anthropicOptions.allowedCallers;
1309
+ if (!supportsStrictTools && tool.strict != null) {
1310
+ toolWarnings.push({
1311
+ type: "unsupported",
1312
+ feature: "strict",
1313
+ details: `Tool '${tool.name}' has strict: ${tool.strict}, but strict mode is not supported by this provider. The strict property will be ignored.`
1314
+ });
1315
+ }
1286
1316
  anthropicTools2.push({
1287
1317
  name: tool.name,
1288
1318
  description: tool.description,
1289
1319
  input_schema: tool.inputSchema,
1290
1320
  cache_control: cacheControl,
1291
1321
  ...eagerInputStreaming ? { eager_input_streaming: true } : {},
1292
- ...supportsStructuredOutput === true && tool.strict != null ? { strict: tool.strict } : {},
1322
+ ...supportsStrictTools === true && tool.strict != null ? { strict: tool.strict } : {},
1293
1323
  ...deferLoading != null ? { defer_loading: deferLoading } : {},
1294
1324
  ...allowedCallers != null ? { allowed_callers: allowedCallers } : {},
1295
1325
  ...tool.inputExamples != null ? {
@@ -1505,7 +1535,6 @@ async function prepareTools({
1505
1535
  break;
1506
1536
  }
1507
1537
  case "anthropic.tool_search_regex_20251119": {
1508
- betas.add("advanced-tool-use-2025-11-20");
1509
1538
  anthropicTools2.push({
1510
1539
  type: "tool_search_tool_regex_20251119",
1511
1540
  name: "tool_search_tool_regex"
@@ -1513,7 +1542,6 @@ async function prepareTools({
1513
1542
  break;
1514
1543
  }
1515
1544
  case "anthropic.tool_search_bm25_20251119": {
1516
- betas.add("advanced-tool-use-2025-11-20");
1517
1545
  anthropicTools2.push({
1518
1546
  type: "tool_search_tool_bm25_20251119",
1519
1547
  name: "tool_search_tool_bm25"
@@ -2046,7 +2074,26 @@ async function convertToAnthropicMessagesPrompt({
2046
2074
  break;
2047
2075
  }
2048
2076
  case "file": {
2049
- if (part.mediaType.startsWith("image/")) {
2077
+ if ((0, import_provider_utils14.isProviderReference)(part.data)) {
2078
+ const fileId = (0, import_provider_utils14.resolveProviderReference)({
2079
+ reference: part.data,
2080
+ provider: "anthropic"
2081
+ });
2082
+ betas.add("files-api-2025-04-14");
2083
+ if (part.mediaType.startsWith("image/")) {
2084
+ anthropicContent.push({
2085
+ type: "image",
2086
+ source: { type: "file", file_id: fileId },
2087
+ cache_control: cacheControl
2088
+ });
2089
+ } else {
2090
+ anthropicContent.push({
2091
+ type: "document",
2092
+ source: { type: "file", file_id: fileId },
2093
+ cache_control: cacheControl
2094
+ });
2095
+ }
2096
+ } else if (part.mediaType.startsWith("image/")) {
2050
2097
  anthropicContent.push({
2051
2098
  type: "image",
2052
2099
  source: isUrlData(part.data) ? {
@@ -2822,7 +2869,7 @@ function createCitationSource(citation, citationDocuments, generateId2) {
2822
2869
  }
2823
2870
  var AnthropicMessagesLanguageModel = class {
2824
2871
  constructor(modelId, config) {
2825
- this.specificationVersion = "v3";
2872
+ this.specificationVersion = "v4";
2826
2873
  var _a;
2827
2874
  this.modelId = modelId;
2828
2875
  this.config = config;
@@ -2861,10 +2908,11 @@ var AnthropicMessagesLanguageModel = class {
2861
2908
  seed,
2862
2909
  tools,
2863
2910
  toolChoice,
2911
+ reasoning,
2864
2912
  providerOptions,
2865
2913
  stream
2866
2914
  }) {
2867
- var _a, _b, _c, _d, _e, _f, _g;
2915
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2868
2916
  const warnings = [];
2869
2917
  if (frequencyPenalty != null) {
2870
2918
  warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
@@ -2919,10 +2967,13 @@ var AnthropicMessagesLanguageModel = class {
2919
2967
  const {
2920
2968
  maxOutputTokens: maxOutputTokensForModel,
2921
2969
  supportsStructuredOutput: modelSupportsStructuredOutput,
2970
+ supportsAdaptiveThinking,
2922
2971
  isKnownModel
2923
2972
  } = getModelCapabilities(this.modelId);
2973
+ const isAnthropicModel = isKnownModel || this.modelId.startsWith("claude-");
2924
2974
  const supportsStructuredOutput = ((_a = this.config.supportsNativeStructuredOutput) != null ? _a : true) && modelSupportsStructuredOutput;
2925
- const structureOutputMode = (_b = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _b : "auto";
2975
+ const supportsStrictTools = ((_b = this.config.supportsStrictTools) != null ? _b : true) && modelSupportsStructuredOutput;
2976
+ const structureOutputMode = (_c = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _c : "auto";
2926
2977
  const useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
2927
2978
  const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useStructuredOutput ? {
2928
2979
  type: "function",
@@ -2957,14 +3008,28 @@ var AnthropicMessagesLanguageModel = class {
2957
3008
  });
2958
3009
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
2959
3010
  prompt,
2960
- sendReasoning: (_c = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _c : true,
3011
+ sendReasoning: (_d = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _d : true,
2961
3012
  warnings,
2962
3013
  cacheControlValidator,
2963
3014
  toolNameMapping
2964
3015
  });
2965
- const thinkingType = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.type;
3016
+ if ((0, import_provider_utils15.isCustomReasoning)(reasoning) && (anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null && (anthropicOptions == null ? void 0 : anthropicOptions.effort) == null) {
3017
+ const reasoningConfig = resolveAnthropicReasoningConfig({
3018
+ reasoning,
3019
+ supportsAdaptiveThinking,
3020
+ maxOutputTokensForModel,
3021
+ warnings
3022
+ });
3023
+ if (reasoningConfig != null) {
3024
+ anthropicOptions.thinking = reasoningConfig.thinking;
3025
+ if (reasoningConfig.effort != null) {
3026
+ anthropicOptions.effort = reasoningConfig.effort;
3027
+ }
3028
+ }
3029
+ }
3030
+ const thinkingType = (_e = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _e.type;
2966
3031
  const isThinking = thinkingType === "enabled" || thinkingType === "adaptive";
2967
- let thinkingBudget = thinkingType === "enabled" ? (_e = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _e.budgetTokens : void 0;
3032
+ let thinkingBudget = thinkingType === "enabled" ? (_f = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _f.budgetTokens : void 0;
2968
3033
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
2969
3034
  const baseArgs = {
2970
3035
  // model id:
@@ -3001,6 +3066,9 @@ var AnthropicMessagesLanguageModel = class {
3001
3066
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
3002
3067
  cache_control: anthropicOptions.cacheControl
3003
3068
  },
3069
+ ...((_g = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _g.userId) != null && {
3070
+ metadata: { user_id: anthropicOptions.metadata.userId }
3071
+ },
3004
3072
  // mcp servers:
3005
3073
  ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
3006
3074
  mcp_servers: anthropicOptions.mcpServers.map((server) => ({
@@ -3022,7 +3090,10 @@ var AnthropicMessagesLanguageModel = class {
3022
3090
  id: anthropicOptions.container.id,
3023
3091
  skills: anthropicOptions.container.skills.map((skill) => ({
3024
3092
  type: skill.type,
3025
- skill_id: skill.skillId,
3093
+ skill_id: skill.type === "custom" ? (0, import_provider_utils15.resolveProviderReference)({
3094
+ reference: skill.providerReference,
3095
+ provider: "anthropic"
3096
+ }) : skill.skillId,
3026
3097
  version: skill.version
3027
3098
  }))
3028
3099
  }
@@ -3124,7 +3195,7 @@ var AnthropicMessagesLanguageModel = class {
3124
3195
  }
3125
3196
  baseArgs.max_tokens = maxTokens + (thinkingBudget != null ? thinkingBudget : 0);
3126
3197
  } else {
3127
- if (topP != null && temperature != null) {
3198
+ if (isAnthropicModel && topP != null && temperature != null) {
3128
3199
  warnings.push({
3129
3200
  type: "unsupported",
3130
3201
  feature: "topP",
@@ -3171,7 +3242,7 @@ var AnthropicMessagesLanguageModel = class {
3171
3242
  if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
3172
3243
  betas.add("fast-mode-2026-02-01");
3173
3244
  }
3174
- if (stream && ((_f = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _f : true)) {
3245
+ if (stream && ((_h = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _h : true)) {
3175
3246
  betas.add("fine-grained-tool-streaming-2025-05-14");
3176
3247
  }
3177
3248
  const {
@@ -3185,13 +3256,15 @@ var AnthropicMessagesLanguageModel = class {
3185
3256
  toolChoice: { type: "required" },
3186
3257
  disableParallelToolUse: true,
3187
3258
  cacheControlValidator,
3188
- supportsStructuredOutput: false
3259
+ supportsStructuredOutput: false,
3260
+ supportsStrictTools
3189
3261
  } : {
3190
3262
  tools: tools != null ? tools : [],
3191
3263
  toolChoice,
3192
3264
  disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
3193
3265
  cacheControlValidator,
3194
- supportsStructuredOutput
3266
+ supportsStructuredOutput,
3267
+ supportsStrictTools
3195
3268
  }
3196
3269
  );
3197
3270
  const cacheWarnings = cacheControlValidator.getWarnings();
@@ -3208,7 +3281,7 @@ var AnthropicMessagesLanguageModel = class {
3208
3281
  ...betas,
3209
3282
  ...toolsBetas,
3210
3283
  ...userSuppliedBetas,
3211
- ...(_g = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _g : []
3284
+ ...(_i = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _i : []
3212
3285
  ]),
3213
3286
  usesJsonResponseTool: jsonResponseTool != null,
3214
3287
  toolNameMapping,
@@ -4471,42 +4544,49 @@ function getModelCapabilities(modelId) {
4471
4544
  return {
4472
4545
  maxOutputTokens: 128e3,
4473
4546
  supportsStructuredOutput: true,
4547
+ supportsAdaptiveThinking: true,
4474
4548
  isKnownModel: true
4475
4549
  };
4476
4550
  } else if (modelId.includes("claude-sonnet-4-5") || modelId.includes("claude-opus-4-5") || modelId.includes("claude-haiku-4-5")) {
4477
4551
  return {
4478
4552
  maxOutputTokens: 64e3,
4479
4553
  supportsStructuredOutput: true,
4554
+ supportsAdaptiveThinking: false,
4480
4555
  isKnownModel: true
4481
4556
  };
4482
4557
  } else if (modelId.includes("claude-opus-4-1")) {
4483
4558
  return {
4484
4559
  maxOutputTokens: 32e3,
4485
4560
  supportsStructuredOutput: true,
4561
+ supportsAdaptiveThinking: false,
4486
4562
  isKnownModel: true
4487
4563
  };
4488
4564
  } else if (modelId.includes("claude-sonnet-4-")) {
4489
4565
  return {
4490
4566
  maxOutputTokens: 64e3,
4491
4567
  supportsStructuredOutput: false,
4568
+ supportsAdaptiveThinking: false,
4492
4569
  isKnownModel: true
4493
4570
  };
4494
4571
  } else if (modelId.includes("claude-opus-4-")) {
4495
4572
  return {
4496
4573
  maxOutputTokens: 32e3,
4497
4574
  supportsStructuredOutput: false,
4575
+ supportsAdaptiveThinking: false,
4498
4576
  isKnownModel: true
4499
4577
  };
4500
4578
  } else if (modelId.includes("claude-3-haiku")) {
4501
4579
  return {
4502
4580
  maxOutputTokens: 4096,
4503
4581
  supportsStructuredOutput: false,
4582
+ supportsAdaptiveThinking: false,
4504
4583
  isKnownModel: true
4505
4584
  };
4506
4585
  } else {
4507
4586
  return {
4508
4587
  maxOutputTokens: 4096,
4509
4588
  supportsStructuredOutput: false,
4589
+ supportsAdaptiveThinking: false,
4510
4590
  isKnownModel: false
4511
4591
  };
4512
4592
  }
@@ -4529,6 +4609,43 @@ function hasWebTool20260209WithoutCodeExecution(tools) {
4529
4609
  }
4530
4610
  return hasWebTool20260209 && !hasCodeExecutionTool;
4531
4611
  }
4612
+ function resolveAnthropicReasoningConfig({
4613
+ reasoning,
4614
+ supportsAdaptiveThinking,
4615
+ maxOutputTokensForModel,
4616
+ warnings
4617
+ }) {
4618
+ if (!(0, import_provider_utils15.isCustomReasoning)(reasoning)) {
4619
+ return void 0;
4620
+ }
4621
+ if (reasoning === "none") {
4622
+ return { thinking: { type: "disabled" } };
4623
+ }
4624
+ if (supportsAdaptiveThinking) {
4625
+ const effort = (0, import_provider_utils15.mapReasoningToProviderEffort)({
4626
+ reasoning,
4627
+ effortMap: {
4628
+ minimal: "low",
4629
+ low: "low",
4630
+ medium: "medium",
4631
+ high: "high",
4632
+ xhigh: "max"
4633
+ },
4634
+ warnings
4635
+ });
4636
+ return { thinking: { type: "adaptive" }, effort };
4637
+ }
4638
+ const budgetTokens = (0, import_provider_utils15.mapReasoningToProviderBudget)({
4639
+ reasoning,
4640
+ maxOutputTokens: maxOutputTokensForModel,
4641
+ maxReasoningBudget: maxOutputTokensForModel,
4642
+ warnings
4643
+ });
4644
+ if (budgetTokens == null) {
4645
+ return void 0;
4646
+ }
4647
+ return { thinking: { type: "enabled", budgetTokens } };
4648
+ }
4532
4649
  function mapAnthropicResponseContextManagement(contextManagement) {
4533
4650
  return contextManagement ? {
4534
4651
  appliedEdits: contextManagement.applied_edits.map((edit) => {
@@ -5052,6 +5169,7 @@ var anthropicTools = {
5052
5169
  0 && (module.exports = {
5053
5170
  AnthropicMessagesLanguageModel,
5054
5171
  anthropicTools,
5172
+ getModelCapabilities,
5055
5173
  prepareTools
5056
5174
  });
5057
5175
  //# sourceMappingURL=index.js.map