diff --git a/packages/cli/src/validateNonInterActiveAuth.test.ts b/packages/cli/src/validateNonInterActiveAuth.test.ts index 801f4cce..307b20d4 100644 --- a/packages/cli/src/validateNonInterActiveAuth.test.ts +++ b/packages/cli/src/validateNonInterActiveAuth.test.ts @@ -15,6 +15,7 @@ describe('validateNonInterActiveAuth', () => { let originalEnvGeminiApiKey: string | undefined; let originalEnvVertexAi: string | undefined; let originalEnvGcp: string | undefined; + let originalEnvOpenAiApiKey: string | undefined; let consoleErrorSpy: ReturnType; let processExitSpy: ReturnType; let refreshAuthMock: jest.MockedFunction< @@ -25,9 +26,11 @@ describe('validateNonInterActiveAuth', () => { originalEnvGeminiApiKey = process.env.GEMINI_API_KEY; originalEnvVertexAi = process.env.GOOGLE_GENAI_USE_VERTEXAI; originalEnvGcp = process.env.GOOGLE_GENAI_USE_GCA; + originalEnvOpenAiApiKey = process.env.OPENAI_API_KEY; delete process.env.GEMINI_API_KEY; delete process.env.GOOGLE_GENAI_USE_VERTEXAI; delete process.env.GOOGLE_GENAI_USE_GCA; + delete process.env.OPENAI_API_KEY; consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); processExitSpy = vi.spyOn(process, 'exit').mockImplementation((code) => { throw new Error(`process.exit(${code}) called`); @@ -51,6 +54,11 @@ describe('validateNonInterActiveAuth', () => { } else { delete process.env.GOOGLE_GENAI_USE_GCA; } + if (originalEnvOpenAiApiKey !== undefined) { + process.env.OPENAI_API_KEY = originalEnvOpenAiApiKey; + } else { + delete process.env.OPENAI_API_KEY; + } vi.restoreAllMocks(); }); @@ -88,6 +96,15 @@ describe('validateNonInterActiveAuth', () => { expect(refreshAuthMock).toHaveBeenCalledWith(AuthType.USE_GEMINI); }); + it('uses USE_OPENAI if OPENAI_API_KEY is set', async () => { + process.env.OPENAI_API_KEY = 'fake-openai-key'; + const nonInteractiveConfig: NonInteractiveConfig = { + refreshAuth: refreshAuthMock, + }; + await validateNonInteractiveAuth(undefined, nonInteractiveConfig); + expect(refreshAuthMock).toHaveBeenCalledWith(AuthType.USE_OPENAI); + }); + it('uses USE_VERTEX_AI if GOOGLE_GENAI_USE_VERTEXAI is true (with GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION)', async () => { process.env.GOOGLE_GENAI_USE_VERTEXAI = 'true'; process.env.GOOGLE_CLOUD_PROJECT = 'test-project'; diff --git a/packages/cli/src/validateNonInterActiveAuth.ts b/packages/cli/src/validateNonInterActiveAuth.ts index 21a0573d..0a974b0e 100644 --- a/packages/cli/src/validateNonInterActiveAuth.ts +++ b/packages/cli/src/validateNonInterActiveAuth.ts @@ -18,6 +18,9 @@ function getAuthTypeFromEnv(): AuthType | undefined { if (process.env.GEMINI_API_KEY) { return AuthType.USE_GEMINI; } + if (process.env.OPENAI_API_KEY) { + return AuthType.USE_OPENAI; + } return undefined; } @@ -29,7 +32,7 @@ export async function validateNonInteractiveAuth( if (!effectiveAuthType) { console.error( - `Please set an Auth method in your ${USER_SETTINGS_PATH} or specify one of the following environment variables before running: GEMINI_API_KEY, GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_GENAI_USE_GCA`, + `Please set an Auth method in your ${USER_SETTINGS_PATH} or specify one of the following environment variables before running: GEMINI_API_KEY, OPENAI_API_KEY, GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_GENAI_USE_GCA`, ); process.exit(1); }