@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.
@@ -987,8 +987,25 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
987
987
 
988
988
  // tool search tool results:
989
989
  case 'tool_search_tool_result': {
990
- const providerToolName =
991
- serverToolCalls[part.tool_use_id] ?? 'tool_search_tool_regex';
990
+ let providerToolName = serverToolCalls[part.tool_use_id];
991
+
992
+ if (providerToolName == null) {
993
+ const bm25CustomName = toolNameMapping.toCustomToolName(
994
+ 'tool_search_tool_bm25',
995
+ );
996
+ const regexCustomName = toolNameMapping.toCustomToolName(
997
+ 'tool_search_tool_regex',
998
+ );
999
+
1000
+ if (bm25CustomName !== 'tool_search_tool_bm25') {
1001
+ providerToolName = 'tool_search_tool_bm25';
1002
+ } else if (regexCustomName !== 'tool_search_tool_regex') {
1003
+ providerToolName = 'tool_search_tool_regex';
1004
+ } else {
1005
+ providerToolName = 'tool_search_tool_regex';
1006
+ }
1007
+ }
1008
+
992
1009
  if (part.content.type === 'tool_search_tool_search_result') {
993
1010
  content.push({
994
1011
  type: 'tool-result',
@@ -1480,9 +1497,25 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1480
1497
 
1481
1498
  // tool search tool results:
1482
1499
  case 'tool_search_tool_result': {
1483
- const providerToolName =
1484
- serverToolCalls[part.tool_use_id] ??
1485
- 'tool_search_tool_regex';
1500
+ let providerToolName = serverToolCalls[part.tool_use_id];
1501
+
1502
+ if (providerToolName == null) {
1503
+ const bm25CustomName = toolNameMapping.toCustomToolName(
1504
+ 'tool_search_tool_bm25',
1505
+ );
1506
+ const regexCustomName = toolNameMapping.toCustomToolName(
1507
+ 'tool_search_tool_regex',
1508
+ );
1509
+
1510
+ if (bm25CustomName !== 'tool_search_tool_bm25') {
1511
+ providerToolName = 'tool_search_tool_bm25';
1512
+ } else if (regexCustomName !== 'tool_search_tool_regex') {
1513
+ providerToolName = 'tool_search_tool_regex';
1514
+ } else {
1515
+ providerToolName = 'tool_search_tool_regex';
1516
+ }
1517
+ }
1518
+
1486
1519
  if (part.content.type === 'tool_search_tool_search_result') {
1487
1520
  controller.enqueue({
1488
1521
  type: 'tool-result',
@@ -104,6 +104,42 @@ describe('createAnthropic', () => {
104
104
  });
105
105
  });
106
106
 
107
+ describe('anthropic provider - authentication', () => {
108
+ describe('authToken option', () => {
109
+ it('sends Authorization Bearer header when authToken is provided', async () => {
110
+ const fetchMock = createFetchMock();
111
+ const provider = createAnthropic({
112
+ authToken: 'test-auth-token',
113
+ fetch: fetchMock,
114
+ });
115
+
116
+ await provider('claude-3-haiku-20240307').doGenerate({
117
+ prompt: TEST_PROMPT,
118
+ });
119
+
120
+ expect(fetchMock).toHaveBeenCalledTimes(1);
121
+ const [, requestOptions] = fetchMock.mock.calls[0]!;
122
+ expect(requestOptions.headers.authorization).toBe(
123
+ 'Bearer test-auth-token',
124
+ );
125
+ expect(requestOptions.headers['x-api-key']).toBeUndefined();
126
+ });
127
+ });
128
+
129
+ describe('apiKey and authToken conflict', () => {
130
+ it('throws error when both apiKey and authToken options are provided', () => {
131
+ expect(() =>
132
+ createAnthropic({
133
+ apiKey: 'test-api-key',
134
+ authToken: 'test-auth-token',
135
+ }),
136
+ ).toThrow(
137
+ 'Both apiKey and authToken were provided. Please use only one authentication method.',
138
+ );
139
+ });
140
+ });
141
+ });
142
+
107
143
  describe('anthropic provider - custom provider name', () => {
108
144
  beforeEach(() => {
109
145
  vi.clearAllMocks();
@@ -1,4 +1,5 @@
1
1
  import {
2
+ InvalidArgumentError,
2
3
  LanguageModelV3,
3
4
  NoSuchModelError,
4
5
  ProviderV3,
@@ -52,9 +53,17 @@ The default prefix is `https://api.anthropic.com/v1`.
52
53
  /**
53
54
  API key that is being send using the `x-api-key` header.
54
55
  It defaults to the `ANTHROPIC_API_KEY` environment variable.
56
+ Only one of `apiKey` or `authToken` is required.
55
57
  */
56
58
  apiKey?: string;
57
59
 
60
+ /**
61
+ Auth token that is being sent using the `Authorization: Bearer` header.
62
+ It defaults to the `ANTHROPIC_AUTH_TOKEN` environment variable.
63
+ Only one of `apiKey` or `authToken` is required.
64
+ */
65
+ authToken?: string;
66
+
58
67
  /**
59
68
  Custom headers to include in the requests.
60
69
  */
@@ -91,19 +100,35 @@ export function createAnthropic(
91
100
 
92
101
  const providerName = options.name ?? 'anthropic.messages';
93
102
 
94
- const getHeaders = () =>
95
- withUserAgentSuffix(
103
+ // Only error if both are explicitly provided in options
104
+ if (options.apiKey && options.authToken) {
105
+ throw new InvalidArgumentError({
106
+ argument: 'apiKey/authToken',
107
+ message:
108
+ 'Both apiKey and authToken were provided. Please use only one authentication method.',
109
+ });
110
+ }
111
+
112
+ const getHeaders = () => {
113
+ const authHeaders: Record<string, string> = options.authToken
114
+ ? { Authorization: `Bearer ${options.authToken}` }
115
+ : {
116
+ 'x-api-key': loadApiKey({
117
+ apiKey: options.apiKey,
118
+ environmentVariableName: 'ANTHROPIC_API_KEY',
119
+ description: 'Anthropic',
120
+ }),
121
+ };
122
+
123
+ return withUserAgentSuffix(
96
124
  {
97
125
  'anthropic-version': '2023-06-01',
98
- 'x-api-key': loadApiKey({
99
- apiKey: options.apiKey,
100
- environmentVariableName: 'ANTHROPIC_API_KEY',
101
- description: 'Anthropic',
102
- }),
126
+ ...authHeaders,
103
127
  ...options.headers,
104
128
  },
105
129
  `ai-sdk/anthropic/${VERSION}`,
106
130
  );
131
+ };
107
132
 
108
133
  const createChatModel = (modelId: AnthropicMessagesModelId) =>
109
134
  new AnthropicMessagesLanguageModel(modelId, {
@@ -64,6 +64,7 @@ const factory = createProviderToolFactoryWithOutputSchema<
64
64
  id: 'anthropic.tool_search_bm25_20251119',
65
65
  inputSchema: toolSearchBm25_20251119InputSchema,
66
66
  outputSchema: toolSearchBm25_20251119OutputSchema,
67
+ supportsDeferredResults: true,
67
68
  });
68
69
 
69
70
  /**
@@ -76,6 +76,7 @@ const factory = createProviderToolFactoryWithOutputSchema<
76
76
  id: 'anthropic.tool_search_regex_20251119',
77
77
  inputSchema: toolSearchRegex_20251119InputSchema,
78
78
  outputSchema: toolSearchRegex_20251119OutputSchema,
79
+ supportsDeferredResults: true,
79
80
  });
80
81
 
81
82
  /**