diff --git a/packages/core/src/core/contentGenerator.test.ts b/packages/core/src/core/contentGenerator.test.ts index 60cb4c563e..3fef648d87 100644 --- a/packages/core/src/core/contentGenerator.test.ts +++ b/packages/core/src/core/contentGenerator.test.ts @@ -508,13 +508,13 @@ describe('createContentGenerator', () => { expect(GoogleGenAI).toHaveBeenCalledWith( expect.objectContaining({ - googleAuthOptions: { - clientOptions: { - transporterOptions: { + googleAuthOptions: expect.objectContaining({ + clientOptions: expect.objectContaining({ + transporterOptions: expect.objectContaining({ agent: expect.any(HttpsProxyAgent), - }, - }, - }, + }), + }), + }), }), ); }); @@ -544,13 +544,13 @@ describe('createContentGenerator', () => { expect(GoogleGenAI).toHaveBeenCalledWith( expect.objectContaining({ - googleAuthOptions: { - clientOptions: { - transporterOptions: { + googleAuthOptions: expect.objectContaining({ + clientOptions: expect.objectContaining({ + transporterOptions: expect.objectContaining({ agent: expect.any(HttpsProxyAgent), - }, - }, - }, + }), + }), + }), }), ); }); @@ -582,13 +582,13 @@ describe('createContentGenerator', () => { expect(GoogleGenAI).toHaveBeenCalledWith( expect.objectContaining({ - googleAuthOptions: { - clientOptions: { - transporterOptions: { + googleAuthOptions: expect.objectContaining({ + clientOptions: expect.objectContaining({ + transporterOptions: expect.objectContaining({ agent: expect.any(HttpProxyAgent), - }, - }, - }, + }), + }), + }), }), ); }); @@ -618,13 +618,13 @@ describe('createContentGenerator', () => { expect(GoogleGenAI).toHaveBeenCalledWith( expect.objectContaining({ - googleAuthOptions: { - clientOptions: { - transporterOptions: { + googleAuthOptions: expect.objectContaining({ + clientOptions: expect.objectContaining({ + transporterOptions: expect.objectContaining({ agent: expect.any(HttpsProxyAgent), - }, - }, - }, + }), + }), + }), }), ); }); @@ -1003,6 +1003,38 @@ describe('createContentGenerator', () => { ); }); + it('should inject apiEndpoint into googleAuthOptions.clientOptions when GOOGLE_VERTEX_BASE_URL is set', async () => { + const mockConfig = { + getModel: vi.fn().mockReturnValue('gemini-pro'), + getProxy: vi.fn().mockReturnValue(undefined), + getUsageStatisticsEnabled: () => false, + getClientName: vi.fn().mockReturnValue(undefined), + } as unknown as Config; + + const mockGenerator = { + models: {}, + } as unknown as GoogleGenAI; + vi.mocked(GoogleGenAI).mockImplementation(() => mockGenerator as never); + vi.stubEnv('GOOGLE_VERTEX_BASE_URL', 'https://vertex.test.local'); + + await createContentGenerator( + { + authType: AuthType.USE_VERTEX_AI, + }, + mockConfig, + ); + + expect(GoogleGenAI).toHaveBeenCalledWith( + expect.objectContaining({ + googleAuthOptions: expect.objectContaining({ + clientOptions: expect.objectContaining({ + apiEndpoint: 'https://vertex.test.local', + }), + }), + }), + ); + }); + it('should prefer an explicit baseUrl over GOOGLE_GEMINI_BASE_URL', async () => { const mockConfig = { getModel: vi.fn().mockReturnValue('gemini-pro'), diff --git a/packages/core/src/core/contentGenerator.ts b/packages/core/src/core/contentGenerator.ts index c893860d4c..cf7cdcd37f 100644 --- a/packages/core/src/core/contentGenerator.ts +++ b/packages/core/src/core/contentGenerator.ts @@ -361,7 +361,8 @@ export async function createContentGenerator( ? new HttpProxyAgent(proxyUrl) : new HttpsProxyAgent(proxyUrl) : undefined; - + const useVertex = + config.vertexai ?? config.authType === AuthType.USE_VERTEX_AI; const googleGenAI = new GoogleGenAI({ apiKey: config.authType === AuthType.GATEWAY @@ -372,10 +373,17 @@ export async function createContentGenerator( vertexai: config.vertexai ?? config.authType === AuthType.USE_VERTEX_AI, httpOptions, ...(apiVersionEnv && { apiVersion: apiVersionEnv }), - ...(proxyAgent && { + // Merge proxy and GDCH endpoint into googleAuthOptions if either exists + ...((proxyAgent || (useVertex && baseUrl)) && { googleAuthOptions: { clientOptions: { - transporterOptions: { agent: proxyAgent }, + ...(proxyAgent && { + transporterOptions: { agent: proxyAgent }, + }), + ...(useVertex && + baseUrl && { + apiEndpoint: baseUrl, + }), }, }, }),