2025-04-18 17:44:24 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-18 11:12:18 -07:00
|
|
|
import yargs from 'yargs/yargs';
|
|
|
|
|
import { hideBin } from 'yargs/helpers';
|
|
|
|
|
import process from 'node:process';
|
2025-04-19 19:45:42 +01:00
|
|
|
import {
|
|
|
|
|
Config,
|
|
|
|
|
loadEnvironment,
|
|
|
|
|
createServerConfig,
|
|
|
|
|
} from '@gemini-code/server';
|
2025-05-02 08:15:46 -07:00
|
|
|
import { Settings } from './settings.js';
|
2025-05-11 14:28:21 -07:00
|
|
|
import { readPackageUp } from 'read-package-up';
|
2025-04-18 11:12:18 -07:00
|
|
|
|
2025-05-09 15:26:43 -07:00
|
|
|
const DEFAULT_GEMINI_MODEL = 'gemini-2.5-pro-preview-05-06';
|
2025-04-18 11:12:18 -07:00
|
|
|
|
2025-04-19 19:45:42 +01:00
|
|
|
// Keep CLI-specific argument parsing
|
2025-04-18 11:12:18 -07:00
|
|
|
interface CliArgs {
|
|
|
|
|
model: string | undefined;
|
2025-05-02 08:15:46 -07:00
|
|
|
sandbox: boolean | string | undefined;
|
2025-04-20 20:20:40 +01:00
|
|
|
debug_mode: boolean | undefined;
|
2025-04-22 18:32:03 -07:00
|
|
|
question: string | undefined;
|
2025-04-24 16:08:29 -07:00
|
|
|
full_context: boolean | undefined;
|
2025-04-18 11:12:18 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-01 01:00:53 +00:00
|
|
|
async function parseArguments(): Promise<CliArgs> {
|
|
|
|
|
const argv = await yargs(hideBin(process.argv))
|
2025-04-18 11:12:18 -07:00
|
|
|
.option('model', {
|
|
|
|
|
alias: 'm',
|
|
|
|
|
type: 'string',
|
|
|
|
|
description: `The Gemini model to use. Defaults to ${DEFAULT_GEMINI_MODEL}.`,
|
2025-04-21 23:25:10 -07:00
|
|
|
default: process.env.GEMINI_CODE_MODEL || DEFAULT_GEMINI_MODEL,
|
2025-04-18 11:12:18 -07:00
|
|
|
})
|
2025-05-02 08:15:46 -07:00
|
|
|
.option('sandbox', {
|
|
|
|
|
alias: 's',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
description: 'Whether to run in sandbox mode. Defaults to false.',
|
|
|
|
|
})
|
2025-04-20 20:20:40 +01:00
|
|
|
.option('debug_mode', {
|
|
|
|
|
alias: 'z',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
description: 'Whether to run in debug mode. Defaults to false.',
|
|
|
|
|
default: false,
|
|
|
|
|
})
|
2025-04-22 18:32:03 -07:00
|
|
|
.option('question', {
|
|
|
|
|
alias: 'q',
|
|
|
|
|
type: 'string',
|
|
|
|
|
description:
|
|
|
|
|
'The question to pass to the command when using piped input.',
|
|
|
|
|
})
|
2025-04-24 16:08:29 -07:00
|
|
|
.option('full_context', {
|
|
|
|
|
alias: 'f',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
description:
|
|
|
|
|
'Recursively include all files within the current directory as context.',
|
|
|
|
|
default: false,
|
|
|
|
|
})
|
2025-04-18 11:12:18 -07:00
|
|
|
.help()
|
|
|
|
|
.alias('h', 'help')
|
2025-04-19 19:45:42 +01:00
|
|
|
.strict().argv;
|
2025-05-01 01:00:53 +00:00
|
|
|
return argv;
|
2025-04-18 11:12:18 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-19 19:45:42 +01:00
|
|
|
// Renamed function for clarity
|
2025-05-02 08:15:46 -07:00
|
|
|
export async function loadCliConfig(settings: Settings): Promise<Config> {
|
2025-04-19 19:45:42 +01:00
|
|
|
// Load .env file using logic from server package
|
|
|
|
|
loadEnvironment();
|
2025-04-18 11:12:18 -07:00
|
|
|
|
2025-04-19 19:45:42 +01:00
|
|
|
// Check API key (CLI responsibility)
|
|
|
|
|
if (!process.env.GEMINI_API_KEY) {
|
|
|
|
|
console.log(
|
|
|
|
|
'GEMINI_API_KEY is not set. See https://ai.google.dev/gemini-api/docs/api-key to obtain one. ' +
|
|
|
|
|
'Please set it in your .env file or as an environment variable.',
|
|
|
|
|
);
|
|
|
|
|
process.exit(1);
|
2025-04-18 11:12:18 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-19 19:45:42 +01:00
|
|
|
// Parse CLI arguments
|
2025-05-01 01:00:53 +00:00
|
|
|
const argv = await parseArguments();
|
2025-04-19 19:45:42 +01:00
|
|
|
|
2025-05-11 14:28:21 -07:00
|
|
|
const userAgent = await createUserAgent();
|
|
|
|
|
|
2025-04-19 19:45:42 +01:00
|
|
|
// Create config using factory from server package
|
|
|
|
|
return createServerConfig(
|
|
|
|
|
process.env.GEMINI_API_KEY,
|
|
|
|
|
argv.model || DEFAULT_GEMINI_MODEL,
|
2025-05-02 08:15:46 -07:00
|
|
|
argv.sandbox ?? settings.sandbox ?? false,
|
2025-04-24 10:21:10 -07:00
|
|
|
process.cwd(),
|
2025-04-20 20:20:40 +01:00
|
|
|
argv.debug_mode || false,
|
2025-04-22 18:32:03 -07:00
|
|
|
argv.question || '',
|
2025-04-24 16:08:29 -07:00
|
|
|
argv.full_context || false,
|
2025-05-03 19:57:28 -07:00
|
|
|
settings.toolDiscoveryCommand,
|
|
|
|
|
settings.toolCallCommand,
|
2025-05-04 12:11:19 -07:00
|
|
|
settings.mcpServerCommand,
|
2025-05-11 14:28:21 -07:00
|
|
|
userAgent,
|
2025-04-19 19:45:42 +01:00
|
|
|
);
|
2025-04-18 11:12:18 -07:00
|
|
|
}
|
2025-05-11 14:28:21 -07:00
|
|
|
|
|
|
|
|
async function createUserAgent(): Promise<string> {
|
|
|
|
|
const packageJsonInfo = await readPackageUp({ cwd: import.meta.url });
|
|
|
|
|
const cliVersion = packageJsonInfo?.packageJson.version || 'unknown';
|
|
|
|
|
return `GeminiCLI/${cliVersion} Node.js/${process.version} (${process.platform}; ${process.arch})`;
|
|
|
|
|
}
|