Use GOOGLE_API_KEY as default if both GEMINI and GOOGLE set (#777)

This commit is contained in:
Seth Troisi 2025-06-09 13:54:11 -07:00 committed by GitHub
parent 6e5332f716
commit f11414a424
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 4 deletions

1
.eslintignore Normal file
View File

@ -0,0 +1 @@
packages/server/dist

View File

@ -36,8 +36,8 @@ vi.mock('@gemini-cli/core', async () => {
loadEnvironment: vi.fn(),
Config: vi.fn((params) => ({
// Mock the config object and its methods
getApiKey: () => params.apiKey,
getModel: () => params.model,
getApiKey: () => params.contentGeneratorConfig.apiKey,
getModel: () => params.contentGeneratorConfig.model,
getSandbox: () => params.sandbox,
getTargetDir: () => params.targetDir,
getDebugMode: () => params.debugMode,
@ -51,7 +51,7 @@ vi.mock('@gemini-cli/core', async () => {
getUserAgent: () => params.userAgent,
getUserMemory: () => params.userMemory,
getGeminiMdFileCount: () => params.geminiMdFileCount,
getVertexAI: () => params.vertexai,
getVertexAI: () => params.contentGeneratorConfig.vertexai,
getShowMemoryUsage: () => params.showMemoryUsage, // Added for the test
getTelemetry: () => params.telemetry,
// Add any other methods that are called on the config object
@ -175,6 +175,50 @@ describe('loadCliConfig telemetry', () => {
});
});
describe('API Key Handling', () => {
const originalEnv = { ...process.env };
const originalArgv = process.argv;
beforeEach(() => {
vi.resetAllMocks();
process.argv = ['node', 'script.js'];
});
afterEach(() => {
process.env = originalEnv;
process.argv = originalArgv;
});
it('should use GEMINI_API_KEY from env', async () => {
process.env.GEMINI_API_KEY = 'gemini-key';
delete process.env.GOOGLE_API_KEY;
const settings: Settings = {};
const result = await loadCliConfig(settings, []);
expect(result.getApiKey()).toBe('gemini-key');
});
it('should use GOOGLE_API_KEY and warn when both GOOGLE_API_KEY and GEMINI_API_KEY are set', async () => {
const consoleWarnSpy = vi
.spyOn(console, 'warn')
.mockImplementation(() => {});
process.env.GEMINI_API_KEY = 'gemini-key';
process.env.GOOGLE_API_KEY = 'google-key';
const settings: Settings = {};
const result = await loadCliConfig(settings, []);
expect(consoleWarnSpy).toHaveBeenCalledWith(
'[WARN]',
'Both GEMINI_API_KEY and GOOGLE_API_KEY are set. Using GOOGLE_API_KEY.',
);
expect(result.getApiKey()).toBe('google-key');
consoleWarnSpy.mockRestore();
});
});
describe('Hierarchical Memory Loading (config.ts) - Placeholder Suite', () => {
beforeEach(() => {
vi.resetAllMocks();

View File

@ -188,6 +188,11 @@ async function createContentGeneratorConfig(
const hasVertexProjectLocationConfig =
!!googleCloudProject && !!googleCloudLocation;
if (hasGeminiApiKey && hasGoogleApiKey) {
logger.warn(
'Both GEMINI_API_KEY and GOOGLE_API_KEY are set. Using GOOGLE_API_KEY.',
);
}
if (!hasGeminiApiKey && !hasGoogleApiKey && !hasVertexProjectLocationConfig) {
logger.error(
'No valid API authentication configuration found. Please set ONE of the following combinations in your environment variables or .env file:\n' +
@ -203,7 +208,7 @@ async function createContentGeneratorConfig(
const config: ContentGeneratorConfig = {
model: argv.model || DEFAULT_GEMINI_MODEL,
apiKey: geminiApiKey || googleApiKey || '',
apiKey: googleApiKey || geminiApiKey || '',
vertexai: hasGeminiApiKey ? false : undefined,
};