@ai-sdk/google-vertex 4.0.26 → 4.0.28

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.
@@ -1,88 +0,0 @@
1
- import { resolve } from '@ai-sdk/provider-utils';
2
- import { createVertex as createVertexOriginal } from './google-vertex-provider';
3
- import { createVertex as createVertexNode } from './google-vertex-provider-node';
4
- import { generateAuthToken } from './google-vertex-auth-google-auth-library';
5
- import { describe, beforeEach, afterEach, expect, it, vi } from 'vitest';
6
-
7
- // Mock the imported modules
8
- vi.mock('./google-vertex-auth-google-auth-library', () => ({
9
- generateAuthToken: vi.fn().mockResolvedValue('mock-auth-token'),
10
- }));
11
-
12
- vi.mock('./google-vertex-provider', () => ({
13
- createVertex: vi.fn().mockImplementation(options => ({
14
- ...options,
15
- })),
16
- }));
17
-
18
- describe('google-vertex-provider-node', () => {
19
- beforeEach(() => {
20
- vi.clearAllMocks();
21
- delete process.env.GOOGLE_VERTEX_API_KEY;
22
- });
23
-
24
- afterEach(() => {
25
- delete process.env.GOOGLE_VERTEX_API_KEY;
26
- });
27
-
28
- it('default headers function should return auth token', async () => {
29
- createVertexNode({ project: 'test-project' });
30
-
31
- expect(createVertexOriginal).toHaveBeenCalledTimes(1);
32
- const passedOptions = vi.mocked(createVertexOriginal).mock.calls[0][0];
33
-
34
- expect(typeof passedOptions?.headers).toBe('function');
35
- expect(await resolve(passedOptions?.headers)).toStrictEqual({
36
- Authorization: 'Bearer mock-auth-token',
37
- });
38
- });
39
-
40
- it('should use custom headers in addition to auth token when provided', async () => {
41
- createVertexNode({
42
- project: 'test-project',
43
- headers: async () => ({
44
- 'Custom-Header': 'custom-value',
45
- }),
46
- });
47
-
48
- expect(createVertexOriginal).toHaveBeenCalledTimes(1);
49
- const passedOptions = vi.mocked(createVertexOriginal).mock.calls[0][0];
50
-
51
- expect(await resolve(passedOptions?.headers)).toEqual({
52
- Authorization: 'Bearer mock-auth-token',
53
- 'Custom-Header': 'custom-value',
54
- });
55
- });
56
-
57
- it('passes googleAuthOptions to generateAuthToken', async () => {
58
- createVertexNode({
59
- googleAuthOptions: {
60
- scopes: ['https://www.googleapis.com/auth/cloud-platform'],
61
- keyFile: 'path/to/key.json',
62
- },
63
- });
64
-
65
- expect(createVertexOriginal).toHaveBeenCalledTimes(1);
66
- const passedOptions = vi.mocked(createVertexOriginal).mock.calls[0][0];
67
-
68
- await resolve(passedOptions?.headers); // call the headers function
69
-
70
- expect(generateAuthToken).toHaveBeenCalledWith({
71
- scopes: ['https://www.googleapis.com/auth/cloud-platform'],
72
- keyFile: 'path/to/key.json',
73
- });
74
- });
75
-
76
- it('should pass options through to base provider when apiKey is provided', async () => {
77
- createVertexNode({
78
- apiKey: 'test-api-key',
79
- });
80
-
81
- expect(createVertexOriginal).toHaveBeenCalledTimes(1);
82
- const passedOptions = vi.mocked(createVertexOriginal).mock.calls[0][0];
83
-
84
- expect(passedOptions?.apiKey).toBe('test-api-key');
85
- expect(passedOptions?.headers).toBeUndefined();
86
- expect(generateAuthToken).not.toHaveBeenCalled();
87
- });
88
- });
@@ -1,318 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2
- import { createVertex } from './google-vertex-provider';
3
- import { GoogleGenerativeAILanguageModel } from '@ai-sdk/google/internal';
4
- import { GoogleVertexEmbeddingModel } from './google-vertex-embedding-model';
5
- import { GoogleVertexImageModel } from './google-vertex-image-model';
6
-
7
- // Mock the imported modules
8
- vi.mock('@ai-sdk/provider-utils', () => ({
9
- loadSetting: vi.fn().mockImplementation(({ settingValue }) => settingValue),
10
- loadOptionalSetting: vi
11
- .fn()
12
- .mockImplementation(({ settingValue, environmentVariableName }) => {
13
- if (settingValue) return settingValue;
14
- if (
15
- environmentVariableName === 'GOOGLE_VERTEX_API_KEY' &&
16
- process.env.GOOGLE_VERTEX_API_KEY
17
- ) {
18
- return process.env.GOOGLE_VERTEX_API_KEY;
19
- }
20
- return undefined;
21
- }),
22
- generateId: vi.fn().mockReturnValue('mock-id'),
23
- withoutTrailingSlash: vi.fn().mockImplementation(url => url),
24
- resolve: vi.fn().mockImplementation(async value => {
25
- if (typeof value === 'function') return value();
26
- return value;
27
- }),
28
- withUserAgentSuffix: vi.fn().mockImplementation(headers => headers),
29
- }));
30
-
31
- vi.mock('@ai-sdk/google/internal', () => ({
32
- GoogleGenerativeAILanguageModel: vi.fn(),
33
- googleTools: {
34
- googleSearch: vi.fn(),
35
- urlContext: vi.fn(),
36
- fileSearch: vi.fn(),
37
- codeExecution: vi.fn(),
38
- },
39
- }));
40
-
41
- vi.mock('./google-vertex-embedding-model', () => ({
42
- GoogleVertexEmbeddingModel: vi.fn(),
43
- }));
44
-
45
- vi.mock('./google-vertex-image-model', () => ({
46
- GoogleVertexImageModel: vi.fn(),
47
- }));
48
-
49
- describe('google-vertex-provider', () => {
50
- beforeEach(() => {
51
- vi.clearAllMocks();
52
- delete process.env.GOOGLE_VERTEX_API_KEY;
53
- });
54
-
55
- afterEach(() => {
56
- delete process.env.GOOGLE_VERTEX_API_KEY;
57
- });
58
-
59
- it('should create a language model with default settings', () => {
60
- const provider = createVertex({
61
- project: 'test-project',
62
- location: 'test-location',
63
- });
64
- provider('test-model-id');
65
-
66
- expect(GoogleGenerativeAILanguageModel).toHaveBeenCalledWith(
67
- 'test-model-id',
68
- expect.objectContaining({
69
- provider: 'google.vertex.chat',
70
- baseURL:
71
- 'https://test-location-aiplatform.googleapis.com/v1beta1/projects/test-project/locations/test-location/publishers/google',
72
- headers: expect.any(Function),
73
- generateId: expect.any(Function),
74
- }),
75
- );
76
- });
77
-
78
- it('should throw an error when using new keyword', () => {
79
- const provider = createVertex({ project: 'test-project' });
80
-
81
- expect(() => new (provider as any)('test-model-id')).toThrow(
82
- 'The Google Vertex AI model function cannot be called with the new keyword.',
83
- );
84
- });
85
-
86
- it('should create an embedding model with correct settings', () => {
87
- const provider = createVertex({
88
- project: 'test-project',
89
- location: 'test-location',
90
- });
91
- provider.embeddingModel('test-embedding-model');
92
-
93
- expect(GoogleVertexEmbeddingModel).toHaveBeenCalledWith(
94
- 'test-embedding-model',
95
- expect.objectContaining({
96
- provider: 'google.vertex.embedding',
97
- headers: expect.any(Function),
98
- baseURL:
99
- 'https://test-location-aiplatform.googleapis.com/v1beta1/projects/test-project/locations/test-location/publishers/google',
100
- }),
101
- );
102
- });
103
-
104
- it('should pass custom headers to the model constructor', () => {
105
- const customHeaders = { 'Custom-Header': 'custom-value' };
106
- const provider = createVertex({
107
- project: 'test-project',
108
- location: 'test-location',
109
- headers: customHeaders,
110
- });
111
- provider('test-model-id');
112
-
113
- expect(GoogleGenerativeAILanguageModel).toHaveBeenCalledWith(
114
- expect.anything(),
115
- expect.objectContaining({
116
- headers: expect.any(Function),
117
- }),
118
- );
119
- });
120
-
121
- it('should pass custom generateId function to the model constructor', () => {
122
- const customGenerateId = () => 'custom-id';
123
- const provider = createVertex({
124
- project: 'test-project',
125
- location: 'test-location',
126
- generateId: customGenerateId,
127
- });
128
- provider('test-model-id');
129
-
130
- expect(GoogleGenerativeAILanguageModel).toHaveBeenCalledWith(
131
- expect.anything(),
132
- expect.objectContaining({
133
- generateId: customGenerateId,
134
- }),
135
- );
136
- });
137
-
138
- it('should use languageModel method to create a model', () => {
139
- const provider = createVertex({
140
- project: 'test-project',
141
- location: 'test-location',
142
- });
143
- provider.languageModel('test-model-id');
144
-
145
- expect(GoogleGenerativeAILanguageModel).toHaveBeenCalledWith(
146
- 'test-model-id',
147
- expect.any(Object),
148
- );
149
- });
150
-
151
- it('should use custom baseURL when provided', () => {
152
- const customBaseURL = 'https://custom-endpoint.example.com';
153
- const provider = createVertex({
154
- project: 'test-project',
155
- location: 'test-location',
156
- baseURL: customBaseURL,
157
- });
158
- provider('test-model-id');
159
-
160
- expect(GoogleGenerativeAILanguageModel).toHaveBeenCalledWith(
161
- 'test-model-id',
162
- expect.objectContaining({
163
- baseURL: customBaseURL,
164
- }),
165
- );
166
- });
167
-
168
- it('should create an image model with default settings', () => {
169
- const provider = createVertex({
170
- project: 'test-project',
171
- location: 'test-location',
172
- });
173
- provider.image('imagen-3.0-generate-002');
174
-
175
- expect(GoogleVertexImageModel).toHaveBeenCalledWith(
176
- 'imagen-3.0-generate-002',
177
- expect.objectContaining({
178
- provider: 'google.vertex.image',
179
- baseURL:
180
- 'https://test-location-aiplatform.googleapis.com/v1beta1/projects/test-project/locations/test-location/publishers/google',
181
- headers: expect.any(Function),
182
- }),
183
- );
184
- });
185
-
186
- it('should use correct URL for global region', () => {
187
- const provider = createVertex({
188
- project: 'test-project',
189
- location: 'global',
190
- });
191
- provider('test-model-id');
192
-
193
- expect(GoogleGenerativeAILanguageModel).toHaveBeenCalledWith(
194
- 'test-model-id',
195
- expect.objectContaining({
196
- provider: 'google.vertex.chat',
197
- baseURL:
198
- 'https://aiplatform.googleapis.com/v1beta1/projects/test-project/locations/global/publishers/google',
199
- headers: expect.any(Function),
200
- generateId: expect.any(Function),
201
- }),
202
- );
203
- });
204
-
205
- it('should use correct URL for global region with embedding model', () => {
206
- const provider = createVertex({
207
- project: 'test-project',
208
- location: 'global',
209
- });
210
- provider.embeddingModel('test-embedding-model');
211
-
212
- expect(GoogleVertexEmbeddingModel).toHaveBeenCalledWith(
213
- 'test-embedding-model',
214
- expect.objectContaining({
215
- provider: 'google.vertex.embedding',
216
- headers: expect.any(Function),
217
- baseURL:
218
- 'https://aiplatform.googleapis.com/v1beta1/projects/test-project/locations/global/publishers/google',
219
- }),
220
- );
221
- });
222
-
223
- it('should use correct URL for global region with image model', () => {
224
- const provider = createVertex({
225
- project: 'test-project',
226
- location: 'global',
227
- });
228
- provider.image('imagen-3.0-generate-002');
229
-
230
- expect(GoogleVertexImageModel).toHaveBeenCalledWith(
231
- 'imagen-3.0-generate-002',
232
- expect.objectContaining({
233
- provider: 'google.vertex.image',
234
- baseURL:
235
- 'https://aiplatform.googleapis.com/v1beta1/projects/test-project/locations/global/publishers/google',
236
- headers: expect.any(Function),
237
- }),
238
- );
239
- });
240
-
241
- it('should expose tools', () => {
242
- const provider = createVertex({
243
- project: 'test-project',
244
- location: 'test-location',
245
- });
246
-
247
- expect(provider.tools).toBeDefined();
248
- expect(provider.tools.googleSearch).toBeDefined();
249
- expect(provider.tools.urlContext).toBeDefined();
250
- expect(provider.tools.codeExecution).toBeDefined();
251
- });
252
-
253
- it('should use region-prefixed URL for non-global regions', () => {
254
- const provider = createVertex({
255
- project: 'test-project',
256
- location: 'us-central1',
257
- });
258
- provider('test-model-id');
259
-
260
- expect(GoogleGenerativeAILanguageModel).toHaveBeenCalledWith(
261
- 'test-model-id',
262
- expect.objectContaining({
263
- provider: 'google.vertex.chat',
264
- baseURL:
265
- 'https://us-central1-aiplatform.googleapis.com/v1beta1/projects/test-project/locations/us-central1/publishers/google',
266
- headers: expect.any(Function),
267
- generateId: expect.any(Function),
268
- }),
269
- );
270
- });
271
-
272
- it('should use express mode base URL when apiKey is provided', () => {
273
- const provider = createVertex({
274
- apiKey: 'test-api-key',
275
- });
276
- provider('test-model-id');
277
-
278
- expect(GoogleGenerativeAILanguageModel).toHaveBeenCalledWith(
279
- 'test-model-id',
280
- expect.objectContaining({
281
- baseURL: 'https://aiplatform.googleapis.com/v1/publishers/google',
282
- }),
283
- );
284
- });
285
-
286
- it('should add API key as query parameter via custom fetch', async () => {
287
- const provider = createVertex({
288
- apiKey: 'test-api-key',
289
- });
290
- provider('test-model-id');
291
-
292
- const calledConfig = vi.mocked(GoogleGenerativeAILanguageModel).mock
293
- .calls[0][1];
294
- const customFetch = calledConfig.fetch;
295
-
296
- expect(customFetch).toBeDefined();
297
-
298
- const mockResponse = new Response('{}');
299
- const originalFetch = vi.fn().mockResolvedValue(mockResponse);
300
- vi.stubGlobal('fetch', originalFetch);
301
-
302
- await customFetch!(
303
- 'https://aiplatform.googleapis.com/v1/publishers/google/models/gemini-2.5-pro:streamGenerateContent',
304
- {},
305
- );
306
-
307
- expect(originalFetch).toHaveBeenCalledWith(
308
- 'https://aiplatform.googleapis.com/v1/publishers/google/models/gemini-2.5-pro:streamGenerateContent',
309
- {
310
- headers: {
311
- 'x-goog-api-key': 'test-api-key',
312
- },
313
- },
314
- );
315
-
316
- vi.unstubAllGlobals();
317
- });
318
- });