@fairyhunter13/ai-anthropic 3.0.58-fork.10 → 3.0.58-fork.12

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,58 @@ 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(maxReasoningBudget, Math.max(minReasoningBudget, Math.round(maxOutputTokens * pct)));
2899
+ }
2848
2900
  function createCitationSource(citation, citationDocuments, generateId3) {
2849
2901
  var _a;
2850
2902
  if (citation.type === "web_search_result_location") {
@@ -2981,11 +3033,7 @@ var AnthropicMessagesLanguageModel = class {
2981
3033
  schema: anthropicLanguageModelOptions
2982
3034
  }) : null;
2983
3035
  const usedCustomProviderKey = customProviderOptions != null;
2984
- const anthropicOptions = Object.assign(
2985
- {},
2986
- canonicalOptions != null ? canonicalOptions : {},
2987
- customProviderOptions != null ? customProviderOptions : {}
2988
- );
3036
+ const anthropicOptions = Object.assign({}, canonicalOptions != null ? canonicalOptions : {}, customProviderOptions != null ? customProviderOptions : {});
2989
3037
  const {
2990
3038
  maxOutputTokens: maxOutputTokensForModel,
2991
3039
  supportsStructuredOutput: modelSupportsStructuredOutput,
@@ -3034,7 +3082,7 @@ var AnthropicMessagesLanguageModel = class {
3034
3082
  cacheControlValidator,
3035
3083
  toolNameMapping
3036
3084
  });
3037
- if ((0, import_provider_utils15.isCustomReasoning)(reasoning) && (anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null && (anthropicOptions == null ? void 0 : anthropicOptions.effort) == null) {
3085
+ if (isCustomReasoning(reasoning) && (anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null && (anthropicOptions == null ? void 0 : anthropicOptions.effort) == null) {
3038
3086
  const reasoningConfig = resolveAnthropicReasoningConfig({
3039
3087
  reasoning,
3040
3088
  supportsAdaptiveThinking,
@@ -3051,6 +3099,7 @@ var AnthropicMessagesLanguageModel = class {
3051
3099
  if (anthropicOptions == null ? void 0 : anthropicOptions.prefill) {
3052
3100
  const lastMessage = messagesPrompt.messages[messagesPrompt.messages.length - 1];
3053
3101
  if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant") {
3102
+ ;
3054
3103
  lastMessage.content.push({
3055
3104
  type: "text",
3056
3105
  text: anthropicOptions.prefill.trim(),
@@ -3326,12 +3375,7 @@ var AnthropicMessagesLanguageModel = class {
3326
3375
  // do not send when not streaming
3327
3376
  },
3328
3377
  warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
3329
- betas: /* @__PURE__ */ new Set([
3330
- ...betas,
3331
- ...toolsBetas,
3332
- ...userSuppliedBetas,
3333
- ...(_j = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _j : []
3334
- ]),
3378
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas, ...userSuppliedBetas, ...(_j = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _j : []]),
3335
3379
  usesJsonResponseTool: jsonResponseTool != null,
3336
3380
  toolNameMapping,
3337
3381
  providerOptionsName,
@@ -3354,10 +3398,7 @@ var AnthropicMessagesLanguageModel = class {
3354
3398
  const configBetaHeader = (_a = configHeaders["anthropic-beta"]) != null ? _a : "";
3355
3399
  const requestBetaHeader = (_b = requestHeaders == null ? void 0 : requestHeaders["anthropic-beta"]) != null ? _b : "";
3356
3400
  return new Set(
3357
- [
3358
- ...configBetaHeader.toLowerCase().split(","),
3359
- ...requestBetaHeader.toLowerCase().split(",")
3360
- ].map((beta) => beta.trim()).filter((beta) => beta !== "")
3401
+ [...configBetaHeader.toLowerCase().split(","), ...requestBetaHeader.toLowerCase().split(",")].map((beta) => beta.trim()).filter((beta) => beta !== "")
3361
3402
  );
3362
3403
  }
3363
3404
  buildRequestUrl(isStreaming) {
@@ -3393,25 +3434,13 @@ var AnthropicMessagesLanguageModel = class {
3393
3434
  }
3394
3435
  async doGenerate(options) {
3395
3436
  var _a, _b, _c, _d, _e, _f, _g;
3396
- const {
3397
- args,
3398
- warnings,
3399
- betas,
3400
- usesJsonResponseTool,
3401
- toolNameMapping,
3402
- providerOptionsName,
3403
- usedCustomProviderKey
3404
- } = await this.getArgs({
3437
+ const { args, warnings, betas, usesJsonResponseTool, toolNameMapping, providerOptionsName, usedCustomProviderKey } = await this.getArgs({
3405
3438
  ...options,
3406
3439
  stream: false,
3407
3440
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3408
3441
  });
3409
- const citationDocuments = [
3410
- ...this.extractCitationDocuments(options.prompt)
3411
- ];
3412
- const markCodeExecutionDynamic = hasWebTool20260209WithoutCodeExecution(
3413
- args.tools
3414
- );
3442
+ const citationDocuments = [...this.extractCitationDocuments(options.prompt)];
3443
+ const markCodeExecutionDynamic = hasWebTool20260209WithoutCodeExecution(args.tools);
3415
3444
  const {
3416
3445
  responseHeaders,
3417
3446
  value: response,
@@ -3421,9 +3450,7 @@ var AnthropicMessagesLanguageModel = class {
3421
3450
  headers: await this.getHeaders({ betas, headers: options.headers }),
3422
3451
  body: this.transformRequestBody(args, betas),
3423
3452
  failedResponseHandler: anthropicFailedResponseHandler,
3424
- successfulResponseHandler: (0, import_provider_utils15.createJsonResponseHandler)(
3425
- anthropicMessagesResponseSchema
3426
- ),
3453
+ successfulResponseHandler: (0, import_provider_utils15.createJsonResponseHandler)(anthropicMessagesResponseSchema),
3427
3454
  abortSignal: options.abortSignal,
3428
3455
  fetch: this.config.fetch
3429
3456
  });
@@ -3438,11 +3465,7 @@ var AnthropicMessagesLanguageModel = class {
3438
3465
  content.push({ type: "text", text: part.text });
3439
3466
  if (part.citations) {
3440
3467
  for (const citation of part.citations) {
3441
- const source = createCitationSource(
3442
- citation,
3443
- citationDocuments,
3444
- this.generateId
3445
- );
3468
+ const source = createCitationSource(citation, citationDocuments, this.generateId);
3446
3469
  if (source) {
3447
3470
  content.push(source);
3448
3471
  }
@@ -3667,6 +3690,20 @@ var AnthropicMessagesLanguageModel = class {
3667
3690
  }
3668
3691
  // code execution 20250522:
3669
3692
  case "code_execution_tool_result": {
3693
+ if (markCodeExecutionDynamic) {
3694
+ content.push({
3695
+ type: "tool-result",
3696
+ toolCallId: part.tool_use_id,
3697
+ toolName: toolNameMapping.toCustomToolName("code_execution"),
3698
+ isError: true,
3699
+ result: {
3700
+ type: "code_execution_tool_result_error",
3701
+ errorCode: "disabled",
3702
+ message: "code_execution is not available. Use web_search and web_fetch as direct tool calls \u2014 they are NOT Python functions."
3703
+ }
3704
+ });
3705
+ break;
3706
+ }
3670
3707
  if (part.content.type === "code_execution_result") {
3671
3708
  content.push({
3672
3709
  type: "tool-result",
@@ -3710,6 +3747,20 @@ var AnthropicMessagesLanguageModel = class {
3710
3747
  // code execution 20250825:
3711
3748
  case "bash_code_execution_tool_result":
3712
3749
  case "text_editor_code_execution_tool_result": {
3750
+ if (markCodeExecutionDynamic) {
3751
+ content.push({
3752
+ type: "tool-result",
3753
+ toolCallId: part.tool_use_id,
3754
+ toolName: toolNameMapping.toCustomToolName("code_execution"),
3755
+ isError: true,
3756
+ result: {
3757
+ type: "code_execution_tool_result_error",
3758
+ errorCode: "disabled",
3759
+ message: "code_execution is not available. Use web_search and web_fetch as direct tool calls \u2014 they are NOT Python functions."
3760
+ }
3761
+ });
3762
+ break;
3763
+ }
3713
3764
  content.push({
3714
3765
  type: "tool-result",
3715
3766
  toolCallId: part.tool_use_id,
@@ -3722,12 +3773,8 @@ var AnthropicMessagesLanguageModel = class {
3722
3773
  case "tool_search_tool_result": {
3723
3774
  let providerToolName = serverToolCalls[part.tool_use_id];
3724
3775
  if (providerToolName == null) {
3725
- const bm25CustomName = toolNameMapping.toCustomToolName(
3726
- "tool_search_tool_bm25"
3727
- );
3728
- const regexCustomName = toolNameMapping.toCustomToolName(
3729
- "tool_search_tool_regex"
3730
- );
3776
+ const bm25CustomName = toolNameMapping.toCustomToolName("tool_search_tool_bm25");
3777
+ const regexCustomName = toolNameMapping.toCustomToolName("tool_search_tool_regex");
3731
3778
  if (bm25CustomName !== "tool_search_tool_bm25") {
3732
3779
  providerToolName = "tool_search_tool_bm25";
3733
3780
  } else if (regexCustomName !== "tool_search_tool_regex") {
@@ -3800,9 +3847,7 @@ var AnthropicMessagesLanguageModel = class {
3800
3847
  version: skill.version
3801
3848
  }))) != null ? _d2 : null
3802
3849
  } : null,
3803
- contextManagement: (_e2 = mapAnthropicResponseContextManagement(
3804
- response.context_management
3805
- )) != null ? _e2 : null
3850
+ contextManagement: (_e2 = mapAnthropicResponseContextManagement(response.context_management)) != null ? _e2 : null
3806
3851
  };
3807
3852
  const providerMetadata = {
3808
3853
  anthropic: anthropicMetadata
@@ -3829,21 +3874,15 @@ var AnthropicMessagesLanguageModel = class {
3829
3874
  stream: true,
3830
3875
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3831
3876
  });
3832
- const citationDocuments = [
3833
- ...this.extractCitationDocuments(options.prompt)
3834
- ];
3835
- const markCodeExecutionDynamic = hasWebTool20260209WithoutCodeExecution(
3836
- body.tools
3837
- );
3877
+ const citationDocuments = [...this.extractCitationDocuments(options.prompt)];
3878
+ const markCodeExecutionDynamic = hasWebTool20260209WithoutCodeExecution(body.tools);
3838
3879
  const url = this.buildRequestUrl(true);
3839
3880
  const { responseHeaders, value: response } = await (0, import_provider_utils15.postJsonToApi)({
3840
3881
  url,
3841
3882
  headers: await this.getHeaders({ betas, headers: options.headers }),
3842
3883
  body: this.transformRequestBody(body, betas),
3843
3884
  failedResponseHandler: anthropicFailedResponseHandler,
3844
- successfulResponseHandler: (0, import_provider_utils15.createEventSourceResponseHandler)(
3845
- anthropicMessagesChunkSchema
3846
- ),
3885
+ successfulResponseHandler: (0, import_provider_utils15.createEventSourceResponseHandler)(anthropicMessagesChunkSchema),
3847
3886
  abortSignal: options.abortSignal,
3848
3887
  fetch: this.config.fetch
3849
3888
  });
@@ -4004,9 +4043,7 @@ var AnthropicMessagesLanguageModel = class {
4004
4043
  });
4005
4044
  } else if (part.name === "tool_search_tool_regex" || part.name === "tool_search_tool_bm25") {
4006
4045
  serverToolCalls[part.id] = part.name;
4007
- const customToolName = toolNameMapping.toCustomToolName(
4008
- part.name
4009
- );
4046
+ const customToolName = toolNameMapping.toCustomToolName(part.name);
4010
4047
  contentBlocks[value.index] = {
4011
4048
  type: "tool-call",
4012
4049
  toolCallId: part.id,
@@ -4112,6 +4149,20 @@ var AnthropicMessagesLanguageModel = class {
4112
4149
  }
4113
4150
  // code execution 20250522:
4114
4151
  case "code_execution_tool_result": {
4152
+ if (markCodeExecutionDynamic) {
4153
+ controller.enqueue({
4154
+ type: "tool-result",
4155
+ toolCallId: part.tool_use_id,
4156
+ toolName: toolNameMapping.toCustomToolName("code_execution"),
4157
+ isError: true,
4158
+ result: {
4159
+ type: "code_execution_tool_result_error",
4160
+ errorCode: "disabled",
4161
+ message: "code_execution is not available. Use web_search and web_fetch as direct tool calls \u2014 they are NOT Python functions."
4162
+ }
4163
+ });
4164
+ return;
4165
+ }
4115
4166
  if (part.content.type === "code_execution_result") {
4116
4167
  controller.enqueue({
4117
4168
  type: "tool-result",
@@ -4155,6 +4206,20 @@ var AnthropicMessagesLanguageModel = class {
4155
4206
  // code execution 20250825:
4156
4207
  case "bash_code_execution_tool_result":
4157
4208
  case "text_editor_code_execution_tool_result": {
4209
+ if (markCodeExecutionDynamic) {
4210
+ controller.enqueue({
4211
+ type: "tool-result",
4212
+ toolCallId: part.tool_use_id,
4213
+ toolName: toolNameMapping.toCustomToolName("code_execution"),
4214
+ isError: true,
4215
+ result: {
4216
+ type: "code_execution_tool_result_error",
4217
+ errorCode: "disabled",
4218
+ message: "code_execution is not available. Use web_search and web_fetch as direct tool calls \u2014 they are NOT Python functions."
4219
+ }
4220
+ });
4221
+ return;
4222
+ }
4158
4223
  controller.enqueue({
4159
4224
  type: "tool-result",
4160
4225
  toolCallId: part.tool_use_id,
@@ -4167,12 +4232,8 @@ var AnthropicMessagesLanguageModel = class {
4167
4232
  case "tool_search_tool_result": {
4168
4233
  let providerToolName = serverToolCalls[part.tool_use_id];
4169
4234
  if (providerToolName == null) {
4170
- const bm25CustomName = toolNameMapping.toCustomToolName(
4171
- "tool_search_tool_bm25"
4172
- );
4173
- const regexCustomName = toolNameMapping.toCustomToolName(
4174
- "tool_search_tool_regex"
4175
- );
4235
+ const bm25CustomName = toolNameMapping.toCustomToolName("tool_search_tool_bm25");
4236
+ const regexCustomName = toolNameMapping.toCustomToolName("tool_search_tool_regex");
4176
4237
  if (bm25CustomName !== "tool_search_tool_bm25") {
4177
4238
  providerToolName = "tool_search_tool_bm25";
4178
4239
  } else if (regexCustomName !== "tool_search_tool_regex") {
@@ -4237,9 +4298,7 @@ var AnthropicMessagesLanguageModel = class {
4237
4298
  }
4238
4299
  default: {
4239
4300
  const _exhaustiveCheck = contentBlockType;
4240
- throw new Error(
4241
- `Unsupported content block type: ${_exhaustiveCheck}`
4242
- );
4301
+ throw new Error(`Unsupported content block type: ${_exhaustiveCheck}`);
4243
4302
  }
4244
4303
  }
4245
4304
  }
@@ -4385,11 +4444,7 @@ var AnthropicMessagesLanguageModel = class {
4385
4444
  }
4386
4445
  case "citations_delta": {
4387
4446
  const citation = value.delta.citation;
4388
- const source = createCitationSource(
4389
- citation,
4390
- citationDocuments,
4391
- generateId3
4392
- );
4447
+ const source = createCitationSource(citation, citationDocuments, generateId3);
4393
4448
  if (source) {
4394
4449
  controller.enqueue(source);
4395
4450
  }
@@ -4397,9 +4452,7 @@ var AnthropicMessagesLanguageModel = class {
4397
4452
  }
4398
4453
  default: {
4399
4454
  const _exhaustiveCheck = deltaType;
4400
- throw new Error(
4401
- `Unsupported delta type: ${_exhaustiveCheck}`
4402
- );
4455
+ throw new Error(`Unsupported delta type: ${_exhaustiveCheck}`);
4403
4456
  }
4404
4457
  }
4405
4458
  }
@@ -4507,9 +4560,7 @@ var AnthropicMessagesLanguageModel = class {
4507
4560
  }))) != null ? _n : null
4508
4561
  } : null;
4509
4562
  if (value.context_management) {
4510
- contextManagement = mapAnthropicResponseContextManagement(
4511
- value.context_management
4512
- );
4563
+ contextManagement = mapAnthropicResponseContextManagement(value.context_management);
4513
4564
  }
4514
4565
  rawUsage = {
4515
4566
  ...rawUsage,
@@ -4664,14 +4715,14 @@ function resolveAnthropicReasoningConfig({
4664
4715
  maxOutputTokensForModel,
4665
4716
  warnings
4666
4717
  }) {
4667
- if (!(0, import_provider_utils15.isCustomReasoning)(reasoning)) {
4718
+ if (!isCustomReasoning(reasoning)) {
4668
4719
  return void 0;
4669
4720
  }
4670
4721
  if (reasoning === "none") {
4671
4722
  return { thinking: { type: "disabled" } };
4672
4723
  }
4673
4724
  if (supportsAdaptiveThinking) {
4674
- const effort = (0, import_provider_utils15.mapReasoningToProviderEffort)({
4725
+ const effort = mapReasoningToProviderEffort({
4675
4726
  reasoning,
4676
4727
  effortMap: {
4677
4728
  minimal: "low",
@@ -4684,7 +4735,7 @@ function resolveAnthropicReasoningConfig({
4684
4735
  });
4685
4736
  return { thinking: { type: "adaptive" }, effort };
4686
4737
  }
4687
- const budgetTokens = (0, import_provider_utils15.mapReasoningToProviderBudget)({
4738
+ const budgetTokens = mapReasoningToProviderBudget({
4688
4739
  reasoning,
4689
4740
  maxOutputTokens: maxOutputTokensForModel,
4690
4741
  maxReasoningBudget: maxOutputTokensForModel,