1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import React from 'react';
import { render } from 'ink';
import { AppWrapper } from './ui/App.js';
import { loadCliConfig } from './config/config.js';
import { readStdin } from './utils/readStdin.js';
import { fileURLToPath } from 'node:url';
import { dirname, basename } from 'node:path';
import { sandbox_command, start_sandbox } from './utils/sandbox.js';
import { LoadedSettings, loadSettings } from './config/settings.js';
import { themeManager } from './ui/themes/theme-manager.js';
import { getStartupWarnings } from './utils/startupWarnings.js';
import { runNonInteractive } from './nonInteractiveCli.js';
import { loadGeminiIgnorePatterns } from './utils/loadIgnorePatterns.js';
import { loadExtensions, ExtensionConfig } from './config/extension.js';
import {
ApprovalMode,
Config,
EditTool,
GlobTool,
GrepTool,
LSTool,
MemoryTool,
ReadFileTool,
ReadManyFilesTool,
ShellTool,
WebFetchTool,
WebSearchTool,
WriteFileTool,
} from '@gemini-cli/core';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export async function main() {
const workspaceRoot = process.cwd();
const settings = loadSettings(workspaceRoot);
setWindowTitle(basename(workspaceRoot), settings);
// warn about deprecated environment variables
if (process.env.GEMINI_CODE_MODEL) {
console.warn('GEMINI_CODE_MODEL is deprecated. Use GEMINI_MODEL instead.');
process.env.GEMINI_MODEL = process.env.GEMINI_CODE_MODEL;
}
if (process.env.GEMINI_CODE_SANDBOX) {
console.warn(
'GEMINI_CODE_SANDBOX is deprecated. Use GEMINI_SANDBOX instead.',
);
process.env.GEMINI_SANDBOX = process.env.GEMINI_CODE_SANDBOX;
}
if (process.env.GEMINI_CODE_SANDBOX_IMAGE) {
console.warn(
'GEMINI_CODE_SANDBOX_IMAGE is deprecated. Use GEMINI_SANDBOX_IMAGE_NAME instead.',
);
process.env.GEMINI_SANDBOX_IMAGE_NAME =
process.env.GEMINI_CODE_SANDBOX_IMAGE; // Corrected to GEMINI_SANDBOX_IMAGE_NAME
}
const geminiIgnorePatterns = loadGeminiIgnorePatterns(workspaceRoot);
if (settings.errors.length > 0) {
for (const error of settings.errors) {
let errorMessage = `Error in ${error.path}: ${error.message}`;
if (!process.env.NO_COLOR) {
errorMessage = `\x1b[31m${errorMessage}\x1b[0m`;
}
console.error(errorMessage);
console.error(`Please fix ${error.path} and try again.`);
}
process.exit(1);
}
const extensions = loadExtensions(workspaceRoot);
const config = await loadCliConfig(
settings.merged,
extensions,
geminiIgnorePatterns,
);
// Initialize centralized FileDiscoveryService
await config.getFileService();
if (settings.merged.theme) {
if (!themeManager.setActiveTheme(settings.merged.theme)) {
// If the theme is not found during initial load, log a warning and continue.
// The useThemeCommand hook in App.tsx will handle opening the dialog.
console.warn(`Warning: Theme "${settings.merged.theme}" not found.`);
}
}
// hop into sandbox if we are outside and sandboxing is enabled
if (!process.env.SANDBOX) {
const sandbox = sandbox_command(config.getSandbox());
if (sandbox) {
await start_sandbox(sandbox);
process.exit(0);
}
}
let input = config.getQuestion();
const startupWarnings = await getStartupWarnings();
// Render UI, passing necessary config values. Check that there is no command line question.
if (process.stdin.isTTY && input?.length === 0) {
render(
<React.StrictMode>
<AppWrapper
config={config}
settings={settings}
startupWarnings={startupWarnings}
/>
</React.StrictMode>,
{ exitOnCtrlC: false },
);
return;
}
// If not a TTY, read from stdin
// This is for cases where the user pipes input directly into the command
if (!process.stdin.isTTY) {
input += await readStdin();
}
if (!input) {
console.error('No input provided via stdin.');
process.exit(1);
}
// Non-interactive mode handled by runNonInteractive
const nonInteractiveConfig = await loadNonInteractiveConfig(
config,
extensions,
settings,
);
await runNonInteractive(nonInteractiveConfig, input);
process.exit(0);
}
function setWindowTitle(title: string, settings: LoadedSettings) {
if (!settings.merged.hideWindowTitle) {
process.stdout.write(`\x1b]2; Gemini - ${title} \x07`);
process.on('exit', () => {
process.stdout.write(`\x1b]2;\x07`);
});
}
}
// --- Global Unhandled Rejection Handler ---
process.on('unhandledRejection', (reason, _promise) => {
// Log other unexpected unhandled rejections as critical errors
console.error('=========================================');
console.error('CRITICAL: Unhandled Promise Rejection!');
console.error('=========================================');
console.error('Reason:', reason);
console.error('Stack trace may follow:');
if (!(reason instanceof Error)) {
console.error(reason);
}
// Exit for genuinely unhandled errors
process.exit(1);
});
async function loadNonInteractiveConfig(
config: Config,
extensions: ExtensionConfig[],
settings: LoadedSettings,
) {
if (config.getApprovalMode() === ApprovalMode.YOLO) {
// Since everything is being allowed we can use normal yolo behavior.
return config;
}
// Everything is not allowed, ensure that only read-only tools are configured.
let existingCoreTools = config.getCoreTools();
existingCoreTools = existingCoreTools || [
ReadFileTool.Name,
LSTool.Name,
GrepTool.Name,
GlobTool.Name,
EditTool.Name,
WriteFileTool.Name,
WebFetchTool.Name,
WebSearchTool.Name,
ReadManyFilesTool.Name,
ShellTool.Name,
MemoryTool.Name,
];
const interactiveTools = [ShellTool.Name, EditTool.Name, WriteFileTool.Name];
const nonInteractiveTools = existingCoreTools.filter(
(tool) => !interactiveTools.includes(tool),
);
const nonInteractiveSettings = {
...settings.merged,
coreTools: nonInteractiveTools,
};
return await loadCliConfig(
nonInteractiveSettings,
extensions,
config.getGeminiIgnorePatterns(),
);
}
|