@ai-sdk/anthropic 3.0.20 → 3.0.22

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.22
4
+
5
+ ### Patch Changes
6
+
7
+ - 8ccf04b: Add `authToken` option to support `Authorization: Bearer` authentication as an alternative to `x-api-key` header authentication.
8
+
9
+ ## 3.0.21
10
+
11
+ ### Patch Changes
12
+
13
+ - 662d359: feat(anthropic): deferred results for tool search tool
14
+
3
15
  ## 3.0.20
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -715,9 +715,16 @@ interface AnthropicProviderSettings {
715
715
  /**
716
716
  API key that is being send using the `x-api-key` header.
717
717
  It defaults to the `ANTHROPIC_API_KEY` environment variable.
718
+ Only one of `apiKey` or `authToken` is required.
718
719
  */
719
720
  apiKey?: string;
720
721
  /**
722
+ Auth token that is being sent using the `Authorization: Bearer` header.
723
+ It defaults to the `ANTHROPIC_AUTH_TOKEN` environment variable.
724
+ Only one of `apiKey` or `authToken` is required.
725
+ */
726
+ authToken?: string;
727
+ /**
721
728
  Custom headers to include in the requests.
722
729
  */
723
730
  headers?: Record<string, string>;
package/dist/index.d.ts CHANGED
@@ -715,9 +715,16 @@ interface AnthropicProviderSettings {
715
715
  /**
716
716
  API key that is being send using the `x-api-key` header.
717
717
  It defaults to the `ANTHROPIC_API_KEY` environment variable.
718
+ Only one of `apiKey` or `authToken` is required.
718
719
  */
719
720
  apiKey?: string;
720
721
  /**
722
+ Auth token that is being sent using the `Authorization: Bearer` header.
723
+ It defaults to the `ANTHROPIC_AUTH_TOKEN` environment variable.
724
+ Only one of `apiKey` or `authToken` is required.
725
+ */
726
+ authToken?: string;
727
+ /**
721
728
  Custom headers to include in the requests.
722
729
  */
723
730
  headers?: Record<string, string>;
package/dist/index.js CHANGED
@@ -32,7 +32,7 @@ var import_provider4 = require("@ai-sdk/provider");
32
32
  var import_provider_utils22 = require("@ai-sdk/provider-utils");
33
33
 
34
34
  // src/version.ts
35
- var VERSION = true ? "3.0.20" : "0.0.0-test";
35
+ var VERSION = true ? "3.0.22" : "0.0.0-test";
36
36
 
37
37
  // src/anthropic-messages-language-model.ts
38
38
  var import_provider3 = require("@ai-sdk/provider");
@@ -1527,7 +1527,8 @@ var toolSearchRegex_20251119InputSchema = (0, import_provider_utils10.lazySchema
1527
1527
  var factory6 = (0, import_provider_utils10.createProviderToolFactoryWithOutputSchema)({
1528
1528
  id: "anthropic.tool_search_regex_20251119",
1529
1529
  inputSchema: toolSearchRegex_20251119InputSchema,
1530
- outputSchema: toolSearchRegex_20251119OutputSchema
1530
+ outputSchema: toolSearchRegex_20251119OutputSchema,
1531
+ supportsDeferredResults: true
1531
1532
  });
1532
1533
  var toolSearchRegex_20251119 = (args = {}) => {
1533
1534
  return factory6(args);
@@ -2759,7 +2760,7 @@ var AnthropicMessagesLanguageModel = class {
2759
2760
  });
2760
2761
  }
2761
2762
  async doGenerate(options) {
2762
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2763
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2763
2764
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2764
2765
  ...options,
2765
2766
  stream: false,
@@ -3048,7 +3049,22 @@ var AnthropicMessagesLanguageModel = class {
3048
3049
  }
3049
3050
  // tool search tool results:
3050
3051
  case "tool_search_tool_result": {
3051
- const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3052
+ let providerToolName = serverToolCalls[part.tool_use_id];
3053
+ if (providerToolName == null) {
3054
+ const bm25CustomName = toolNameMapping.toCustomToolName(
3055
+ "tool_search_tool_bm25"
3056
+ );
3057
+ const regexCustomName = toolNameMapping.toCustomToolName(
3058
+ "tool_search_tool_regex"
3059
+ );
3060
+ if (bm25CustomName !== "tool_search_tool_bm25") {
3061
+ providerToolName = "tool_search_tool_bm25";
3062
+ } else if (regexCustomName !== "tool_search_tool_regex") {
3063
+ providerToolName = "tool_search_tool_regex";
3064
+ } else {
3065
+ providerToolName = "tool_search_tool_regex";
3066
+ }
3067
+ }
3052
3068
  if (part.content.type === "tool_search_tool_search_result") {
3053
3069
  content.push({
3054
3070
  type: "tool-result",
@@ -3082,13 +3098,13 @@ var AnthropicMessagesLanguageModel = class {
3082
3098
  finishReason: response.stop_reason,
3083
3099
  isJsonResponseFromTool
3084
3100
  }),
3085
- raw: (_e = response.stop_reason) != null ? _e : void 0
3101
+ raw: (_d = response.stop_reason) != null ? _d : void 0
3086
3102
  },
3087
3103
  usage: convertAnthropicMessagesUsage(response.usage),
3088
3104
  request: { body: args },
3089
3105
  response: {
3090
- id: (_f = response.id) != null ? _f : void 0,
3091
- modelId: (_g = response.model) != null ? _g : void 0,
3106
+ id: (_e = response.id) != null ? _e : void 0,
3107
+ modelId: (_f = response.model) != null ? _f : void 0,
3092
3108
  headers: responseHeaders,
3093
3109
  body: rawResponse
3094
3110
  },
@@ -3096,20 +3112,20 @@ var AnthropicMessagesLanguageModel = class {
3096
3112
  providerMetadata: {
3097
3113
  anthropic: {
3098
3114
  usage: response.usage,
3099
- cacheCreationInputTokens: (_h = response.usage.cache_creation_input_tokens) != null ? _h : null,
3100
- stopSequence: (_i = response.stop_sequence) != null ? _i : null,
3115
+ cacheCreationInputTokens: (_g = response.usage.cache_creation_input_tokens) != null ? _g : null,
3116
+ stopSequence: (_h = response.stop_sequence) != null ? _h : null,
3101
3117
  container: response.container ? {
3102
3118
  expiresAt: response.container.expires_at,
3103
3119
  id: response.container.id,
3104
- skills: (_k = (_j = response.container.skills) == null ? void 0 : _j.map((skill) => ({
3120
+ skills: (_j = (_i = response.container.skills) == null ? void 0 : _i.map((skill) => ({
3105
3121
  type: skill.type,
3106
3122
  skillId: skill.skill_id,
3107
3123
  version: skill.version
3108
- }))) != null ? _k : null
3124
+ }))) != null ? _j : null
3109
3125
  } : null,
3110
- contextManagement: (_l = mapAnthropicResponseContextManagement(
3126
+ contextManagement: (_k = mapAnthropicResponseContextManagement(
3111
3127
  response.context_management
3112
- )) != null ? _l : null
3128
+ )) != null ? _k : null
3113
3129
  }
3114
3130
  }
3115
3131
  };
@@ -3169,7 +3185,7 @@ var AnthropicMessagesLanguageModel = class {
3169
3185
  controller.enqueue({ type: "stream-start", warnings });
3170
3186
  },
3171
3187
  transform(chunk, controller) {
3172
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
3188
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
3173
3189
  if (options.includeRawChunks) {
3174
3190
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3175
3191
  }
@@ -3430,7 +3446,22 @@ var AnthropicMessagesLanguageModel = class {
3430
3446
  }
3431
3447
  // tool search tool results:
3432
3448
  case "tool_search_tool_result": {
3433
- const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3449
+ let providerToolName = serverToolCalls[part.tool_use_id];
3450
+ if (providerToolName == null) {
3451
+ const bm25CustomName = toolNameMapping.toCustomToolName(
3452
+ "tool_search_tool_bm25"
3453
+ );
3454
+ const regexCustomName = toolNameMapping.toCustomToolName(
3455
+ "tool_search_tool_regex"
3456
+ );
3457
+ if (bm25CustomName !== "tool_search_tool_bm25") {
3458
+ providerToolName = "tool_search_tool_bm25";
3459
+ } else if (regexCustomName !== "tool_search_tool_regex") {
3460
+ providerToolName = "tool_search_tool_regex";
3461
+ } else {
3462
+ providerToolName = "tool_search_tool_regex";
3463
+ }
3464
+ }
3434
3465
  if (part.content.type === "tool_search_tool_search_result") {
3435
3466
  controller.enqueue({
3436
3467
  type: "tool-result",
@@ -3644,12 +3675,12 @@ var AnthropicMessagesLanguageModel = class {
3644
3675
  }
3645
3676
  case "message_start": {
3646
3677
  usage.input_tokens = value.message.usage.input_tokens;
3647
- usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
3648
- usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
3678
+ usage.cache_read_input_tokens = (_d = value.message.usage.cache_read_input_tokens) != null ? _d : 0;
3679
+ usage.cache_creation_input_tokens = (_e = value.message.usage.cache_creation_input_tokens) != null ? _e : 0;
3649
3680
  rawUsage = {
3650
3681
  ...value.message.usage
3651
3682
  };
3652
- cacheCreationInputTokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : null;
3683
+ cacheCreationInputTokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : null;
3653
3684
  if (value.message.container != null) {
3654
3685
  container = {
3655
3686
  expiresAt: value.message.container.expires_at,
@@ -3668,8 +3699,8 @@ var AnthropicMessagesLanguageModel = class {
3668
3699
  }
3669
3700
  controller.enqueue({
3670
3701
  type: "response-metadata",
3671
- id: (_h = value.message.id) != null ? _h : void 0,
3672
- modelId: (_i = value.message.model) != null ? _i : void 0
3702
+ id: (_g = value.message.id) != null ? _g : void 0,
3703
+ modelId: (_h = value.message.model) != null ? _h : void 0
3673
3704
  });
3674
3705
  if (value.message.content != null) {
3675
3706
  for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
@@ -3685,7 +3716,7 @@ var AnthropicMessagesLanguageModel = class {
3685
3716
  id: part.id,
3686
3717
  toolName: part.name
3687
3718
  });
3688
- const inputStr = JSON.stringify((_j = part.input) != null ? _j : {});
3719
+ const inputStr = JSON.stringify((_i = part.input) != null ? _i : {});
3689
3720
  controller.enqueue({
3690
3721
  type: "tool-input-delta",
3691
3722
  id: part.id,
@@ -3723,17 +3754,17 @@ var AnthropicMessagesLanguageModel = class {
3723
3754
  finishReason: value.delta.stop_reason,
3724
3755
  isJsonResponseFromTool
3725
3756
  }),
3726
- raw: (_k = value.delta.stop_reason) != null ? _k : void 0
3757
+ raw: (_j = value.delta.stop_reason) != null ? _j : void 0
3727
3758
  };
3728
- stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
3759
+ stopSequence = (_k = value.delta.stop_sequence) != null ? _k : null;
3729
3760
  container = value.delta.container != null ? {
3730
3761
  expiresAt: value.delta.container.expires_at,
3731
3762
  id: value.delta.container.id,
3732
- skills: (_n = (_m = value.delta.container.skills) == null ? void 0 : _m.map((skill) => ({
3763
+ skills: (_m = (_l = value.delta.container.skills) == null ? void 0 : _l.map((skill) => ({
3733
3764
  type: skill.type,
3734
3765
  skillId: skill.skill_id,
3735
3766
  version: skill.version
3736
- }))) != null ? _n : null
3767
+ }))) != null ? _m : null
3737
3768
  } : null;
3738
3769
  if (value.delta.context_management) {
3739
3770
  contextManagement = mapAnthropicResponseContextManagement(
@@ -4111,7 +4142,8 @@ var toolSearchBm25_20251119InputSchema = (0, import_provider_utils21.lazySchema)
4111
4142
  var factory7 = (0, import_provider_utils21.createProviderToolFactoryWithOutputSchema)({
4112
4143
  id: "anthropic.tool_search_bm25_20251119",
4113
4144
  inputSchema: toolSearchBm25_20251119InputSchema,
4114
- outputSchema: toolSearchBm25_20251119OutputSchema
4145
+ outputSchema: toolSearchBm25_20251119OutputSchema,
4146
+ supportsDeferredResults: true
4115
4147
  });
4116
4148
  var toolSearchBm25_20251119 = (args = {}) => {
4117
4149
  return factory7(args);
@@ -4281,18 +4313,29 @@ function createAnthropic(options = {}) {
4281
4313
  })
4282
4314
  )) != null ? _a : "https://api.anthropic.com/v1";
4283
4315
  const providerName = (_b = options.name) != null ? _b : "anthropic.messages";
4284
- const getHeaders = () => (0, import_provider_utils22.withUserAgentSuffix)(
4285
- {
4286
- "anthropic-version": "2023-06-01",
4316
+ if (options.apiKey && options.authToken) {
4317
+ throw new import_provider4.InvalidArgumentError({
4318
+ argument: "apiKey/authToken",
4319
+ message: "Both apiKey and authToken were provided. Please use only one authentication method."
4320
+ });
4321
+ }
4322
+ const getHeaders = () => {
4323
+ const authHeaders = options.authToken ? { Authorization: `Bearer ${options.authToken}` } : {
4287
4324
  "x-api-key": (0, import_provider_utils22.loadApiKey)({
4288
4325
  apiKey: options.apiKey,
4289
4326
  environmentVariableName: "ANTHROPIC_API_KEY",
4290
4327
  description: "Anthropic"
4291
- }),
4292
- ...options.headers
4293
- },
4294
- `ai-sdk/anthropic/${VERSION}`
4295
- );
4328
+ })
4329
+ };
4330
+ return (0, import_provider_utils22.withUserAgentSuffix)(
4331
+ {
4332
+ "anthropic-version": "2023-06-01",
4333
+ ...authHeaders,
4334
+ ...options.headers
4335
+ },
4336
+ `ai-sdk/anthropic/${VERSION}`
4337
+ );
4338
+ };
4296
4339
  const createChatModel = (modelId) => {
4297
4340
  var _a2;
4298
4341
  return new AnthropicMessagesLanguageModel(modelId, {