@ai-sdk/anthropic 3.0.21 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "3.0.21",
3
+ "version": "3.0.22",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -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, {