|
| 1 | +import {LlmGenerateFilesRequestOptions, LlmRunner} from './llm-runner.js'; |
| 2 | +import {join} from 'path'; |
| 3 | +import {mkdirSync} from 'fs'; |
| 4 | +import {writeFile} from 'fs/promises'; |
| 5 | +import {BaseCliAgentRunner} from './base-cli-agent-runner.js'; |
| 6 | + |
| 7 | +const SUPPORTED_MODELS = ['gemini-2.5-pro', 'gemini-2.5-flash', 'gemini-2.5-flash-lite']; |
| 8 | + |
| 9 | +/** Runner that generates code using the Gemini CLI. */ |
| 10 | +export class GeminiCliRunner extends BaseCliAgentRunner implements LlmRunner { |
| 11 | + readonly id = 'gemini-cli'; |
| 12 | + readonly displayName = 'Gemini CLI'; |
| 13 | + readonly hasBuiltInRepairLoop = true; |
| 14 | + protected ignoredFilePatterns = ['**/GEMINI.md', '**/.geminiignore']; |
| 15 | + protected binaryName = 'gemini'; |
| 16 | + |
| 17 | + getSupportedModels(): string[] { |
| 18 | + return SUPPORTED_MODELS; |
| 19 | + } |
| 20 | + |
| 21 | + protected getCommandLineFlags(options: LlmGenerateFilesRequestOptions): string[] { |
| 22 | + return [ |
| 23 | + '--prompt', |
| 24 | + options.context.executablePrompt, |
| 25 | + '--model', |
| 26 | + options.model, |
| 27 | + // Skip all confirmations. |
| 28 | + '--approval-mode', |
| 29 | + 'yolo', |
| 30 | + ]; |
| 31 | + } |
| 32 | + |
| 33 | + protected async writeAgentFiles(options: LlmGenerateFilesRequestOptions): Promise<void> { |
| 34 | + const {context} = options; |
| 35 | + const ignoreFilePath = join(context.directory, '.geminiignore'); |
| 36 | + const instructionFilePath = join(context.directory, 'GEMINI.md'); |
| 37 | + const settingsDir = join(context.directory, '.gemini'); |
| 38 | + |
| 39 | + mkdirSync(settingsDir); |
| 40 | + |
| 41 | + const commonIgnorePatterns = super.getCommonIgnorePatterns(); |
| 42 | + const ignoreFileContent = [ |
| 43 | + ...commonIgnorePatterns.directories, |
| 44 | + ...commonIgnorePatterns.files, |
| 45 | + ].join('\n'); |
| 46 | + |
| 47 | + const promises: Promise<unknown>[] = [ |
| 48 | + writeFile(ignoreFilePath, ignoreFileContent), |
| 49 | + writeFile(instructionFilePath, super.getCommonInstructions(options)), |
| 50 | + ]; |
| 51 | + |
| 52 | + if (context.packageManager) { |
| 53 | + writeFile( |
| 54 | + join(settingsDir, 'settings.json'), |
| 55 | + this.getGeminiSettingsFile(context.packageManager, context.possiblePackageManagers), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + await Promise.all(promises); |
| 60 | + } |
| 61 | + |
| 62 | + private getGeminiSettingsFile(packageManager: string, possiblePackageManagers: string[]): string { |
| 63 | + const config = { |
| 64 | + excludeTools: [ |
| 65 | + // Prevent Gemini from using version control and package |
| 66 | + // managers since doing so via prompting doesn't always work. |
| 67 | + 'run_shell_command(git)', |
| 68 | + ...possiblePackageManagers |
| 69 | + .filter(m => m !== packageManager) |
| 70 | + .map(m => `run_shell_command(${m})`), |
| 71 | + |
| 72 | + // Note that we don't block all commands, |
| 73 | + // because the build commands also go through it. |
| 74 | + `run_shell_command(${packageManager} install)`, |
| 75 | + `run_shell_command(${packageManager} add)`, |
| 76 | + `run_shell_command(${packageManager} remove)`, |
| 77 | + `run_shell_command(${packageManager} update)`, |
| 78 | + `run_shell_command(${packageManager} list)`, |
| 79 | + ], |
| 80 | + }; |
| 81 | + |
| 82 | + return JSON.stringify(config, null, 2); |
| 83 | + } |
| 84 | +} |
0 commit comments