Files
gemini-cli/packages/cli/src/gemini.ts
T

102 lines
3.4 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
2025-04-15 21:41:08 -07:00
import React from 'react';
import { render } from 'ink';
2025-04-18 19:09:41 -04:00
import { App } from './ui/App.js';
2025-04-15 21:41:08 -07:00
import { toolRegistry } from './tools/tool-registry.js';
import { LSTool } from './tools/ls.tool.js';
import { ReadFileTool } from './tools/read-file.tool.js';
import { GrepTool } from './tools/grep.tool.js';
import { GlobTool } from './tools/glob.tool.js';
import { EditTool } from './tools/edit.tool.js';
import { TerminalTool } from './tools/terminal.tool.js';
import { WriteFileTool } from './tools/write-file.tool.js';
2025-04-18 13:20:39 -07:00
import { WebFetchTool } from './tools/web-fetch.tool.js';
import { globalConfig } from './config/config.js';
2025-04-15 21:41:08 -07:00
2025-04-18 16:14:20 -07:00
// TODO(b/411707095): remove. left here as an example of how to pull in inter-package deps
import { helloServer } from '@gemini-code/server';
helloServer();
2025-04-15 21:41:08 -07:00
async function main() {
// Configure tools
registerTools(globalConfig.getTargetDir());
2025-04-15 21:41:08 -07:00
// Render UI
render(
React.createElement(App, {
directory: globalConfig.getTargetDir(),
}),
);
2025-04-15 21:41:08 -07:00
}
// --- Global Unhandled Rejection Handler ---
2025-04-18 16:38:01 -07:00
process.on('unhandledRejection', (reason, _promise) => {
2025-04-17 18:06:21 -04:00
// Check if this is the known 429 ClientError that sometimes escapes
// this is a workaround for a specific issue with the way we are calling gemini
// where a 429 error is thrown but not caught, causing an unhandled rejection
// TODO(adh): Remove this when the race condition is fixed
const isKnownEscaped429 =
reason instanceof Error &&
reason.name === 'ClientError' &&
reason.message.includes('got status: 429');
2025-04-17 18:06:21 -04:00
if (isKnownEscaped429) {
// Log it differently and DON'T exit, as it's likely already handled visually
console.warn('-----------------------------------------');
console.warn(
'WORKAROUND: Suppressed known escaped 429 Unhandled Rejection.',
);
console.warn('-----------------------------------------');
console.warn('Reason:', reason);
// No process.exit(1);
} else {
// 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);
}
2025-04-17 18:06:21 -04:00
// Exit for genuinely unhandled errors
process.exit(1);
}
});
2025-04-15 21:41:08 -07:00
// --- Global Entry Point ---
main().catch((error) => {
2025-04-17 18:06:21 -04:00
console.error('An unexpected critical error occurred:');
if (error instanceof Error) {
console.error(error.message);
} else {
console.error(String(error));
}
process.exit(1);
2025-04-15 21:41:08 -07:00
});
function registerTools(targetDir: string) {
2025-04-17 18:06:21 -04:00
const lsTool = new LSTool(targetDir);
const readFileTool = new ReadFileTool(targetDir);
const grepTool = new GrepTool(targetDir);
const globTool = new GlobTool(targetDir);
const editTool = new EditTool(targetDir);
const terminalTool = new TerminalTool(targetDir);
const writeFileTool = new WriteFileTool(targetDir);
2025-04-18 13:20:39 -07:00
const webFetchTool = new WebFetchTool();
2025-04-15 21:41:08 -07:00
2025-04-17 18:06:21 -04:00
toolRegistry.registerTool(lsTool);
toolRegistry.registerTool(readFileTool);
toolRegistry.registerTool(grepTool);
toolRegistry.registerTool(globTool);
toolRegistry.registerTool(editTool);
toolRegistry.registerTool(terminalTool);
toolRegistry.registerTool(writeFileTool);
2025-04-18 13:20:39 -07:00
toolRegistry.registerTool(webFetchTool);
2025-04-15 21:41:08 -07:00
}