@fairyhunter13/ai-anthropic 3.0.58-fork.11 → 3.0.58-fork.13

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.js CHANGED
@@ -2845,6 +2845,61 @@ function mapAnthropicStopReason({
2845
2845
  }
2846
2846
 
2847
2847
  // src/anthropic-messages-language-model.ts
2848
+ function isCustomReasoning(reasoning) {
2849
+ return reasoning !== void 0 && reasoning !== "provider-default";
2850
+ }
2851
+ function mapReasoningToProviderEffort({
2852
+ reasoning,
2853
+ effortMap,
2854
+ warnings
2855
+ }) {
2856
+ const mapped = effortMap[reasoning];
2857
+ if (mapped == null) {
2858
+ warnings.push({
2859
+ type: "unsupported",
2860
+ feature: "reasoning",
2861
+ details: `reasoning "${reasoning}" is not supported by this model.`
2862
+ });
2863
+ return void 0;
2864
+ }
2865
+ if (mapped !== reasoning) {
2866
+ warnings.push({
2867
+ type: "compatibility",
2868
+ feature: "reasoning",
2869
+ details: `reasoning "${reasoning}" is not directly supported by this model. mapped to effort "${mapped}".`
2870
+ });
2871
+ }
2872
+ return mapped;
2873
+ }
2874
+ var DEFAULT_REASONING_BUDGET_PERCENTAGES = {
2875
+ minimal: 0.02,
2876
+ low: 0.1,
2877
+ medium: 0.3,
2878
+ high: 0.6,
2879
+ xhigh: 0.9
2880
+ };
2881
+ function mapReasoningToProviderBudget({
2882
+ reasoning,
2883
+ maxOutputTokens,
2884
+ maxReasoningBudget,
2885
+ minReasoningBudget = 1024,
2886
+ budgetPercentages = DEFAULT_REASONING_BUDGET_PERCENTAGES,
2887
+ warnings
2888
+ }) {
2889
+ const pct = budgetPercentages[reasoning];
2890
+ if (pct == null) {
2891
+ warnings.push({
2892
+ type: "unsupported",
2893
+ feature: "reasoning",
2894
+ details: `reasoning "${reasoning}" is not supported by this model.`
2895
+ });
2896
+ return void 0;
2897
+ }
2898
+ return Math.min(
2899
+ maxReasoningBudget,
2900
+ Math.max(minReasoningBudget, Math.round(maxOutputTokens * pct))
2901
+ );
2902
+ }
2848
2903
  function createCitationSource(citation, citationDocuments, generateId3) {
2849
2904
  var _a;
2850
2905
  if (citation.type === "web_search_result_location") {
@@ -2981,7 +3036,11 @@ var AnthropicMessagesLanguageModel = class {
2981
3036
  schema: anthropicLanguageModelOptions
2982
3037
  }) : null;
2983
3038
  const usedCustomProviderKey = customProviderOptions != null;
2984
- const anthropicOptions = Object.assign({}, canonicalOptions != null ? canonicalOptions : {}, customProviderOptions != null ? customProviderOptions : {});
3039
+ const anthropicOptions = Object.assign(
3040
+ {},
3041
+ canonicalOptions != null ? canonicalOptions : {},
3042
+ customProviderOptions != null ? customProviderOptions : {}
3043
+ );
2985
3044
  const {
2986
3045
  maxOutputTokens: maxOutputTokensForModel,
2987
3046
  supportsStructuredOutput: modelSupportsStructuredOutput,
@@ -3030,7 +3089,7 @@ var AnthropicMessagesLanguageModel = class {
3030
3089
  cacheControlValidator,
3031
3090
  toolNameMapping
3032
3091
  });
3033
- if ((0, import_provider_utils15.isCustomReasoning)(reasoning) && (anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null && (anthropicOptions == null ? void 0 : anthropicOptions.effort) == null) {
3092
+ if (isCustomReasoning(reasoning) && (anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null && (anthropicOptions == null ? void 0 : anthropicOptions.effort) == null) {
3034
3093
  const reasoningConfig = resolveAnthropicReasoningConfig({
3035
3094
  reasoning,
3036
3095
  supportsAdaptiveThinking,
@@ -3047,7 +3106,6 @@ var AnthropicMessagesLanguageModel = class {
3047
3106
  if (anthropicOptions == null ? void 0 : anthropicOptions.prefill) {
3048
3107
  const lastMessage = messagesPrompt.messages[messagesPrompt.messages.length - 1];
3049
3108
  if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant") {
3050
- ;
3051
3109
  lastMessage.content.push({
3052
3110
  type: "text",
3053
3111
  text: anthropicOptions.prefill.trim(),
@@ -3323,7 +3381,12 @@ var AnthropicMessagesLanguageModel = class {
3323
3381
  // do not send when not streaming
3324
3382
  },
3325
3383
  warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
3326
- betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas, ...userSuppliedBetas, ...(_j = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _j : []]),
3384
+ betas: /* @__PURE__ */ new Set([
3385
+ ...betas,
3386
+ ...toolsBetas,
3387
+ ...userSuppliedBetas,
3388
+ ...(_j = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _j : []
3389
+ ]),
3327
3390
  usesJsonResponseTool: jsonResponseTool != null,
3328
3391
  toolNameMapping,
3329
3392
  providerOptionsName,
@@ -3346,7 +3409,10 @@ var AnthropicMessagesLanguageModel = class {
3346
3409
  const configBetaHeader = (_a = configHeaders["anthropic-beta"]) != null ? _a : "";
3347
3410
  const requestBetaHeader = (_b = requestHeaders == null ? void 0 : requestHeaders["anthropic-beta"]) != null ? _b : "";
3348
3411
  return new Set(
3349
- [...configBetaHeader.toLowerCase().split(","), ...requestBetaHeader.toLowerCase().split(",")].map((beta) => beta.trim()).filter((beta) => beta !== "")
3412
+ [
3413
+ ...configBetaHeader.toLowerCase().split(","),
3414
+ ...requestBetaHeader.toLowerCase().split(",")
3415
+ ].map((beta) => beta.trim()).filter((beta) => beta !== "")
3350
3416
  );
3351
3417
  }
3352
3418
  buildRequestUrl(isStreaming) {
@@ -3382,13 +3448,25 @@ var AnthropicMessagesLanguageModel = class {
3382
3448
  }
3383
3449
  async doGenerate(options) {
3384
3450
  var _a, _b, _c, _d, _e, _f, _g;
3385
- const { args, warnings, betas, usesJsonResponseTool, toolNameMapping, providerOptionsName, usedCustomProviderKey } = await this.getArgs({
3451
+ const {
3452
+ args,
3453
+ warnings,
3454
+ betas,
3455
+ usesJsonResponseTool,
3456
+ toolNameMapping,
3457
+ providerOptionsName,
3458
+ usedCustomProviderKey
3459
+ } = await this.getArgs({
3386
3460
  ...options,
3387
3461
  stream: false,
3388
3462
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3389
3463
  });
3390
- const citationDocuments = [...this.extractCitationDocuments(options.prompt)];
3391
- const markCodeExecutionDynamic = hasWebTool20260209WithoutCodeExecution(args.tools);
3464
+ const citationDocuments = [
3465
+ ...this.extractCitationDocuments(options.prompt)
3466
+ ];
3467
+ const markCodeExecutionDynamic = hasWebTool20260209WithoutCodeExecution(
3468
+ args.tools
3469
+ );
3392
3470
  const {
3393
3471
  responseHeaders,
3394
3472
  value: response,
@@ -3398,7 +3476,9 @@ var AnthropicMessagesLanguageModel = class {
3398
3476
  headers: await this.getHeaders({ betas, headers: options.headers }),
3399
3477
  body: this.transformRequestBody(args, betas),
3400
3478
  failedResponseHandler: anthropicFailedResponseHandler,
3401
- successfulResponseHandler: (0, import_provider_utils15.createJsonResponseHandler)(anthropicMessagesResponseSchema),
3479
+ successfulResponseHandler: (0, import_provider_utils15.createJsonResponseHandler)(
3480
+ anthropicMessagesResponseSchema
3481
+ ),
3402
3482
  abortSignal: options.abortSignal,
3403
3483
  fetch: this.config.fetch
3404
3484
  });
@@ -3413,7 +3493,11 @@ var AnthropicMessagesLanguageModel = class {
3413
3493
  content.push({ type: "text", text: part.text });
3414
3494
  if (part.citations) {
3415
3495
  for (const citation of part.citations) {
3416
- const source = createCitationSource(citation, citationDocuments, this.generateId);
3496
+ const source = createCitationSource(
3497
+ citation,
3498
+ citationDocuments,
3499
+ this.generateId
3500
+ );
3417
3501
  if (source) {
3418
3502
  content.push(source);
3419
3503
  }
@@ -3638,20 +3722,6 @@ var AnthropicMessagesLanguageModel = class {
3638
3722
  }
3639
3723
  // code execution 20250522:
3640
3724
  case "code_execution_tool_result": {
3641
- if (markCodeExecutionDynamic) {
3642
- content.push({
3643
- type: "tool-result",
3644
- toolCallId: part.tool_use_id,
3645
- toolName: toolNameMapping.toCustomToolName("code_execution"),
3646
- isError: true,
3647
- result: {
3648
- type: "code_execution_tool_result_error",
3649
- errorCode: "disabled",
3650
- message: "code_execution is not available. Use web_search and web_fetch as direct tool calls \u2014 they are NOT Python functions."
3651
- }
3652
- });
3653
- break;
3654
- }
3655
3725
  if (part.content.type === "code_execution_result") {
3656
3726
  content.push({
3657
3727
  type: "tool-result",
@@ -3695,20 +3765,6 @@ var AnthropicMessagesLanguageModel = class {
3695
3765
  // code execution 20250825:
3696
3766
  case "bash_code_execution_tool_result":
3697
3767
  case "text_editor_code_execution_tool_result": {
3698
- if (markCodeExecutionDynamic) {
3699
- content.push({
3700
- type: "tool-result",
3701
- toolCallId: part.tool_use_id,
3702
- toolName: toolNameMapping.toCustomToolName("code_execution"),
3703
- isError: true,
3704
- result: {
3705
- type: "code_execution_tool_result_error",
3706
- errorCode: "disabled",
3707
- message: "code_execution is not available. Use web_search and web_fetch as direct tool calls \u2014 they are NOT Python functions."
3708
- }
3709
- });
3710
- break;
3711
- }
3712
3768
  content.push({
3713
3769
  type: "tool-result",
3714
3770
  toolCallId: part.tool_use_id,
@@ -3721,8 +3777,12 @@ var AnthropicMessagesLanguageModel = class {
3721
3777
  case "tool_search_tool_result": {
3722
3778
  let providerToolName = serverToolCalls[part.tool_use_id];
3723
3779
  if (providerToolName == null) {
3724
- const bm25CustomName = toolNameMapping.toCustomToolName("tool_search_tool_bm25");
3725
- const regexCustomName = toolNameMapping.toCustomToolName("tool_search_tool_regex");
3780
+ const bm25CustomName = toolNameMapping.toCustomToolName(
3781
+ "tool_search_tool_bm25"
3782
+ );
3783
+ const regexCustomName = toolNameMapping.toCustomToolName(
3784
+ "tool_search_tool_regex"
3785
+ );
3726
3786
  if (bm25CustomName !== "tool_search_tool_bm25") {
3727
3787
  providerToolName = "tool_search_tool_bm25";
3728
3788
  } else if (regexCustomName !== "tool_search_tool_regex") {
@@ -3795,7 +3855,9 @@ var AnthropicMessagesLanguageModel = class {
3795
3855
  version: skill.version
3796
3856
  }))) != null ? _d2 : null
3797
3857
  } : null,
3798
- contextManagement: (_e2 = mapAnthropicResponseContextManagement(response.context_management)) != null ? _e2 : null
3858
+ contextManagement: (_e2 = mapAnthropicResponseContextManagement(
3859
+ response.context_management
3860
+ )) != null ? _e2 : null
3799
3861
  };
3800
3862
  const providerMetadata = {
3801
3863
  anthropic: anthropicMetadata
@@ -3822,15 +3884,21 @@ var AnthropicMessagesLanguageModel = class {
3822
3884
  stream: true,
3823
3885
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3824
3886
  });
3825
- const citationDocuments = [...this.extractCitationDocuments(options.prompt)];
3826
- const markCodeExecutionDynamic = hasWebTool20260209WithoutCodeExecution(body.tools);
3887
+ const citationDocuments = [
3888
+ ...this.extractCitationDocuments(options.prompt)
3889
+ ];
3890
+ const markCodeExecutionDynamic = hasWebTool20260209WithoutCodeExecution(
3891
+ body.tools
3892
+ );
3827
3893
  const url = this.buildRequestUrl(true);
3828
3894
  const { responseHeaders, value: response } = await (0, import_provider_utils15.postJsonToApi)({
3829
3895
  url,
3830
3896
  headers: await this.getHeaders({ betas, headers: options.headers }),
3831
3897
  body: this.transformRequestBody(body, betas),
3832
3898
  failedResponseHandler: anthropicFailedResponseHandler,
3833
- successfulResponseHandler: (0, import_provider_utils15.createEventSourceResponseHandler)(anthropicMessagesChunkSchema),
3899
+ successfulResponseHandler: (0, import_provider_utils15.createEventSourceResponseHandler)(
3900
+ anthropicMessagesChunkSchema
3901
+ ),
3834
3902
  abortSignal: options.abortSignal,
3835
3903
  fetch: this.config.fetch
3836
3904
  });
@@ -3991,7 +4059,9 @@ var AnthropicMessagesLanguageModel = class {
3991
4059
  });
3992
4060
  } else if (part.name === "tool_search_tool_regex" || part.name === "tool_search_tool_bm25") {
3993
4061
  serverToolCalls[part.id] = part.name;
3994
- const customToolName = toolNameMapping.toCustomToolName(part.name);
4062
+ const customToolName = toolNameMapping.toCustomToolName(
4063
+ part.name
4064
+ );
3995
4065
  contentBlocks[value.index] = {
3996
4066
  type: "tool-call",
3997
4067
  toolCallId: part.id,
@@ -4097,20 +4167,6 @@ var AnthropicMessagesLanguageModel = class {
4097
4167
  }
4098
4168
  // code execution 20250522:
4099
4169
  case "code_execution_tool_result": {
4100
- if (markCodeExecutionDynamic) {
4101
- controller.enqueue({
4102
- type: "tool-result",
4103
- toolCallId: part.tool_use_id,
4104
- toolName: toolNameMapping.toCustomToolName("code_execution"),
4105
- isError: true,
4106
- result: {
4107
- type: "code_execution_tool_result_error",
4108
- errorCode: "disabled",
4109
- message: "code_execution is not available. Use web_search and web_fetch as direct tool calls \u2014 they are NOT Python functions."
4110
- }
4111
- });
4112
- return;
4113
- }
4114
4170
  if (part.content.type === "code_execution_result") {
4115
4171
  controller.enqueue({
4116
4172
  type: "tool-result",
@@ -4154,20 +4210,6 @@ var AnthropicMessagesLanguageModel = class {
4154
4210
  // code execution 20250825:
4155
4211
  case "bash_code_execution_tool_result":
4156
4212
  case "text_editor_code_execution_tool_result": {
4157
- if (markCodeExecutionDynamic) {
4158
- controller.enqueue({
4159
- type: "tool-result",
4160
- toolCallId: part.tool_use_id,
4161
- toolName: toolNameMapping.toCustomToolName("code_execution"),
4162
- isError: true,
4163
- result: {
4164
- type: "code_execution_tool_result_error",
4165
- errorCode: "disabled",
4166
- message: "code_execution is not available. Use web_search and web_fetch as direct tool calls \u2014 they are NOT Python functions."
4167
- }
4168
- });
4169
- return;
4170
- }
4171
4213
  controller.enqueue({
4172
4214
  type: "tool-result",
4173
4215
  toolCallId: part.tool_use_id,
@@ -4180,8 +4222,12 @@ var AnthropicMessagesLanguageModel = class {
4180
4222
  case "tool_search_tool_result": {
4181
4223
  let providerToolName = serverToolCalls[part.tool_use_id];
4182
4224
  if (providerToolName == null) {
4183
- const bm25CustomName = toolNameMapping.toCustomToolName("tool_search_tool_bm25");
4184
- const regexCustomName = toolNameMapping.toCustomToolName("tool_search_tool_regex");
4225
+ const bm25CustomName = toolNameMapping.toCustomToolName(
4226
+ "tool_search_tool_bm25"
4227
+ );
4228
+ const regexCustomName = toolNameMapping.toCustomToolName(
4229
+ "tool_search_tool_regex"
4230
+ );
4185
4231
  if (bm25CustomName !== "tool_search_tool_bm25") {
4186
4232
  providerToolName = "tool_search_tool_bm25";
4187
4233
  } else if (regexCustomName !== "tool_search_tool_regex") {
@@ -4246,7 +4292,9 @@ var AnthropicMessagesLanguageModel = class {
4246
4292
  }
4247
4293
  default: {
4248
4294
  const _exhaustiveCheck = contentBlockType;
4249
- throw new Error(`Unsupported content block type: ${_exhaustiveCheck}`);
4295
+ throw new Error(
4296
+ `Unsupported content block type: ${_exhaustiveCheck}`
4297
+ );
4250
4298
  }
4251
4299
  }
4252
4300
  }
@@ -4392,7 +4440,11 @@ var AnthropicMessagesLanguageModel = class {
4392
4440
  }
4393
4441
  case "citations_delta": {
4394
4442
  const citation = value.delta.citation;
4395
- const source = createCitationSource(citation, citationDocuments, generateId3);
4443
+ const source = createCitationSource(
4444
+ citation,
4445
+ citationDocuments,
4446
+ generateId3
4447
+ );
4396
4448
  if (source) {
4397
4449
  controller.enqueue(source);
4398
4450
  }
@@ -4400,7 +4452,9 @@ var AnthropicMessagesLanguageModel = class {
4400
4452
  }
4401
4453
  default: {
4402
4454
  const _exhaustiveCheck = deltaType;
4403
- throw new Error(`Unsupported delta type: ${_exhaustiveCheck}`);
4455
+ throw new Error(
4456
+ `Unsupported delta type: ${_exhaustiveCheck}`
4457
+ );
4404
4458
  }
4405
4459
  }
4406
4460
  }
@@ -4508,7 +4562,9 @@ var AnthropicMessagesLanguageModel = class {
4508
4562
  }))) != null ? _n : null
4509
4563
  } : null;
4510
4564
  if (value.context_management) {
4511
- contextManagement = mapAnthropicResponseContextManagement(value.context_management);
4565
+ contextManagement = mapAnthropicResponseContextManagement(
4566
+ value.context_management
4567
+ );
4512
4568
  }
4513
4569
  rawUsage = {
4514
4570
  ...rawUsage,
@@ -4663,14 +4719,14 @@ function resolveAnthropicReasoningConfig({
4663
4719
  maxOutputTokensForModel,
4664
4720
  warnings
4665
4721
  }) {
4666
- if (!(0, import_provider_utils15.isCustomReasoning)(reasoning)) {
4722
+ if (!isCustomReasoning(reasoning)) {
4667
4723
  return void 0;
4668
4724
  }
4669
4725
  if (reasoning === "none") {
4670
4726
  return { thinking: { type: "disabled" } };
4671
4727
  }
4672
4728
  if (supportsAdaptiveThinking) {
4673
- const effort = (0, import_provider_utils15.mapReasoningToProviderEffort)({
4729
+ const effort = mapReasoningToProviderEffort({
4674
4730
  reasoning,
4675
4731
  effortMap: {
4676
4732
  minimal: "low",
@@ -4683,7 +4739,7 @@ function resolveAnthropicReasoningConfig({
4683
4739
  });
4684
4740
  return { thinking: { type: "adaptive" }, effort };
4685
4741
  }
4686
- const budgetTokens = (0, import_provider_utils15.mapReasoningToProviderBudget)({
4742
+ const budgetTokens = mapReasoningToProviderBudget({
4687
4743
  reasoning,
4688
4744
  maxOutputTokens: maxOutputTokensForModel,
4689
4745
  maxReasoningBudget: maxOutputTokensForModel,