@ai-sdk/anthropic 3.0.22 → 3.0.23

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.
Files changed (45) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/index.js +1 -1
  3. package/dist/index.mjs +1 -1
  4. package/package.json +9 -5
  5. package/src/__fixtures__/anthropic-code-execution-20250825.1.chunks.txt +0 -248
  6. package/src/__fixtures__/anthropic-code-execution-20250825.1.json +0 -70
  7. package/src/__fixtures__/anthropic-code-execution-20250825.2.chunks.txt +0 -984
  8. package/src/__fixtures__/anthropic-code-execution-20250825.2.json +0 -111
  9. package/src/__fixtures__/anthropic-code-execution-20250825.pptx-skill.chunks.txt +0 -691
  10. package/src/__fixtures__/anthropic-code-execution-20250825.pptx-skill.json +0 -1801
  11. package/src/__fixtures__/anthropic-json-other-tool.1.chunks.txt +0 -13
  12. package/src/__fixtures__/anthropic-json-other-tool.1.json +0 -26
  13. package/src/__fixtures__/anthropic-json-output-format.1.chunks.txt +0 -120
  14. package/src/__fixtures__/anthropic-json-output-format.1.json +0 -25
  15. package/src/__fixtures__/anthropic-json-tool.1.chunks.txt +0 -9
  16. package/src/__fixtures__/anthropic-json-tool.1.json +0 -37
  17. package/src/__fixtures__/anthropic-json-tool.2.chunks.txt +0 -14
  18. package/src/__fixtures__/anthropic-mcp.1.chunks.txt +0 -17
  19. package/src/__fixtures__/anthropic-mcp.1.json +0 -39
  20. package/src/__fixtures__/anthropic-memory-20250818.1.json +0 -28
  21. package/src/__fixtures__/anthropic-message-delta-input-tokens.chunks.txt +0 -8
  22. package/src/__fixtures__/anthropic-programmatic-tool-calling.1.chunks.txt +0 -278
  23. package/src/__fixtures__/anthropic-programmatic-tool-calling.1.json +0 -106
  24. package/src/__fixtures__/anthropic-tool-no-args.chunks.txt +0 -13
  25. package/src/__fixtures__/anthropic-tool-no-args.json +0 -31
  26. package/src/__fixtures__/anthropic-tool-search-bm25.1.chunks.txt +0 -47
  27. package/src/__fixtures__/anthropic-tool-search-bm25.1.json +0 -67
  28. package/src/__fixtures__/anthropic-tool-search-deferred-bm25.2.json +0 -65
  29. package/src/__fixtures__/anthropic-tool-search-deferred-bm25.chunks.txt +0 -115
  30. package/src/__fixtures__/anthropic-tool-search-deferred-regex.2.json +0 -69
  31. package/src/__fixtures__/anthropic-tool-search-deferred-regex.chunks.txt +0 -119
  32. package/src/__fixtures__/anthropic-tool-search-regex.1.chunks.txt +0 -51
  33. package/src/__fixtures__/anthropic-tool-search-regex.1.json +0 -65
  34. package/src/__fixtures__/anthropic-web-fetch-tool.1.chunks.txt +0 -64
  35. package/src/__fixtures__/anthropic-web-fetch-tool.1.json +0 -54
  36. package/src/__fixtures__/anthropic-web-fetch-tool.2.json +0 -56
  37. package/src/__fixtures__/anthropic-web-fetch-tool.error.json +0 -46
  38. package/src/__fixtures__/anthropic-web-search-tool.1.chunks.txt +0 -120
  39. package/src/__fixtures__/anthropic-web-search-tool.1.json +0 -181
  40. package/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +0 -16719
  41. package/src/anthropic-error.test.ts +0 -42
  42. package/src/anthropic-messages-language-model.test.ts +0 -7368
  43. package/src/anthropic-prepare-tools.test.ts +0 -1219
  44. package/src/anthropic-provider.test.ts +0 -198
  45. package/src/convert-to-anthropic-messages-prompt.test.ts +0 -2902
@@ -1,198 +0,0 @@
1
- /* eslint-disable turbo/no-undeclared-env-vars */
2
- import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
3
- import { LanguageModelV3Prompt } from '@ai-sdk/provider';
4
- import { createAnthropic } from './anthropic-provider';
5
-
6
- vi.mock('./version', () => ({
7
- VERSION: '0.0.0-test',
8
- }));
9
-
10
- const TEST_PROMPT: LanguageModelV3Prompt = [
11
- { role: 'user', content: [{ type: 'text', text: 'Hello' }] },
12
- ];
13
-
14
- const createSuccessfulResponse = () =>
15
- new Response(
16
- JSON.stringify({
17
- type: 'message',
18
- id: 'msg_123',
19
- model: 'claude-3-haiku-20240307',
20
- content: [{ type: 'text', text: 'Hi' }],
21
- stop_reason: null,
22
- stop_sequence: null,
23
- usage: { input_tokens: 1, output_tokens: 1 },
24
- }),
25
- {
26
- status: 200,
27
- headers: { 'Content-Type': 'application/json' },
28
- },
29
- );
30
-
31
- const createFetchMock = () =>
32
- vi.fn().mockResolvedValue(createSuccessfulResponse());
33
-
34
- describe('createAnthropic', () => {
35
- describe('baseURL configuration', () => {
36
- const originalBaseUrl = process.env.ANTHROPIC_BASE_URL;
37
-
38
- beforeEach(() => {
39
- vi.restoreAllMocks();
40
- });
41
-
42
- afterEach(() => {
43
- if (originalBaseUrl === undefined) {
44
- delete process.env.ANTHROPIC_BASE_URL;
45
- } else {
46
- process.env.ANTHROPIC_BASE_URL = originalBaseUrl;
47
- }
48
- });
49
-
50
- it('uses the default Anthropic base URL when not provided', async () => {
51
- delete process.env.ANTHROPIC_BASE_URL;
52
-
53
- const fetchMock = createFetchMock();
54
- const provider = createAnthropic({
55
- apiKey: 'test-api-key',
56
- fetch: fetchMock,
57
- });
58
-
59
- await provider('claude-3-haiku-20240307').doGenerate({
60
- prompt: TEST_PROMPT,
61
- });
62
-
63
- expect(fetchMock).toHaveBeenCalledTimes(1);
64
- const [requestUrl] = fetchMock.mock.calls[0]!;
65
- expect(requestUrl).toBe('https://api.anthropic.com/v1/messages');
66
- });
67
-
68
- it('uses ANTHROPIC_BASE_URL when set', async () => {
69
- process.env.ANTHROPIC_BASE_URL = 'https://proxy.anthropic.example/v1/';
70
-
71
- const fetchMock = createFetchMock();
72
- const provider = createAnthropic({
73
- apiKey: 'test-api-key',
74
- fetch: fetchMock,
75
- });
76
-
77
- await provider('claude-3-haiku-20240307').doGenerate({
78
- prompt: TEST_PROMPT,
79
- });
80
-
81
- expect(fetchMock).toHaveBeenCalledTimes(1);
82
- const [requestUrl] = fetchMock.mock.calls[0]!;
83
- expect(requestUrl).toBe('https://proxy.anthropic.example/v1/messages');
84
- });
85
-
86
- it('prefers the baseURL option over ANTHROPIC_BASE_URL', async () => {
87
- process.env.ANTHROPIC_BASE_URL = 'https://env.anthropic.example/v1';
88
-
89
- const fetchMock = createFetchMock();
90
- const provider = createAnthropic({
91
- apiKey: 'test-api-key',
92
- baseURL: 'https://option.anthropic.example/v1/',
93
- fetch: fetchMock,
94
- });
95
-
96
- await provider('claude-3-haiku-20240307').doGenerate({
97
- prompt: TEST_PROMPT,
98
- });
99
-
100
- expect(fetchMock).toHaveBeenCalledTimes(1);
101
- const [requestUrl] = fetchMock.mock.calls[0]!;
102
- expect(requestUrl).toBe('https://option.anthropic.example/v1/messages');
103
- });
104
- });
105
- });
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
-
143
- describe('anthropic provider - custom provider name', () => {
144
- beforeEach(() => {
145
- vi.clearAllMocks();
146
- });
147
-
148
- it('should use custom provider name when specified', () => {
149
- const provider = createAnthropic({
150
- name: 'my-claude-proxy',
151
- apiKey: 'test-api-key',
152
- });
153
-
154
- const model = provider('claude-3-haiku-20240307');
155
- expect(model.provider).toBe('my-claude-proxy');
156
- });
157
-
158
- it('should default to anthropic.messages when name not specified', () => {
159
- const provider = createAnthropic({
160
- apiKey: 'test-api-key',
161
- });
162
-
163
- const model = provider('claude-3-haiku-20240307');
164
- expect(model.provider).toBe('anthropic.messages');
165
- });
166
- });
167
-
168
- describe('anthropic provider - supportedUrls', () => {
169
- it('should support image/* URLs', async () => {
170
- const provider = createAnthropic({
171
- apiKey: 'test-api-key',
172
- });
173
-
174
- const model = provider('claude-3-haiku-20240307');
175
- const supportedUrls = await model.supportedUrls;
176
-
177
- expect(supportedUrls['image/*']).toBeDefined();
178
- expect(
179
- supportedUrls['image/*']![0]!.test('https://example.com/image.png'),
180
- ).toBe(true);
181
- });
182
-
183
- it('should support application/pdf URLs', async () => {
184
- const provider = createAnthropic({
185
- apiKey: 'test-api-key',
186
- });
187
-
188
- const model = provider('claude-3-haiku-20240307');
189
- const supportedUrls = await model.supportedUrls;
190
-
191
- expect(supportedUrls['application/pdf']).toBeDefined();
192
- expect(
193
- supportedUrls['application/pdf']![0]!.test(
194
- 'https://arxiv.org/pdf/2401.00001',
195
- ),
196
- ).toBe(true);
197
- });
198
- });