2025-04-18 17:44:24 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
Initial commit of Gemini Code CLI
This commit introduces the initial codebase for the Gemini Code CLI, a command-line interface designed to facilitate interaction with the Gemini API for software engineering tasks.
The code was migrated from a previous git repository as a single squashed commit.
Core Features & Components:
* **Gemini Integration:** Leverages the `@google/genai` SDK to interact with the Gemini models, supporting chat history, streaming responses, and function calling (tools).
* **Terminal UI:** Built with Ink (React for CLIs) providing an interactive chat interface within the terminal, including input prompts, message display, loading indicators, and tool interaction elements.
* **Tooling Framework:** Implements a robust tool system allowing Gemini to interact with the local environment. Includes tools for:
* File system listing (`ls`)
* File reading (`read-file`)
* Content searching (`grep`)
* File globbing (`glob`)
* File editing (`edit`)
* File writing (`write-file`)
* Executing bash commands (`terminal`)
* **State Management:** Handles the streaming state of Gemini responses and manages the conversation history.
* **Configuration:** Parses command-line arguments (`yargs`) and loads environment variables (`dotenv`) for setup.
* **Project Structure:** Organized into `core`, `ui`, `tools`, `config`, and `utils` directories using TypeScript. Includes basic build (`tsc`) and start scripts.
This initial version establishes the foundation for a powerful CLI tool enabling developers to use Gemini for coding assistance directly in their terminal environment.
---
Created by yours truly: __Gemini Code__
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-19 19:45:42 +01:00
|
|
|
import { loadCliConfig } from './config/config.js';
|
2025-04-21 17:41:44 -07:00
|
|
|
import { readStdin } from './utils/readStdin.js';
|
2025-05-30 18:25:47 -07:00
|
|
|
import { GeminiClient } from '@gemini-code/core';
|
2025-04-23 22:00:40 +00:00
|
|
|
import { readPackageUp } from 'read-package-up';
|
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
|
import { dirname } from 'node:path';
|
2025-04-30 00:38:25 +00:00
|
|
|
import { sandbox_command, start_sandbox } from './utils/sandbox.js';
|
2025-05-01 10:34:07 -07:00
|
|
|
import { loadSettings } from './config/settings.js';
|
|
|
|
|
import { themeManager } from './ui/themes/theme-manager.js';
|
2025-05-14 00:12:04 +00:00
|
|
|
import { getStartupWarnings } from './utils/startupWarnings.js';
|
2025-04-23 22:00:40 +00:00
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
const __dirname = dirname(__filename);
|
2025-04-18 16:14:20 -07:00
|
|
|
|
Initial commit of Gemini Code CLI
This commit introduces the initial codebase for the Gemini Code CLI, a command-line interface designed to facilitate interaction with the Gemini API for software engineering tasks.
The code was migrated from a previous git repository as a single squashed commit.
Core Features & Components:
* **Gemini Integration:** Leverages the `@google/genai` SDK to interact with the Gemini models, supporting chat history, streaming responses, and function calling (tools).
* **Terminal UI:** Built with Ink (React for CLIs) providing an interactive chat interface within the terminal, including input prompts, message display, loading indicators, and tool interaction elements.
* **Tooling Framework:** Implements a robust tool system allowing Gemini to interact with the local environment. Includes tools for:
* File system listing (`ls`)
* File reading (`read-file`)
* Content searching (`grep`)
* File globbing (`glob`)
* File editing (`edit`)
* File writing (`write-file`)
* Executing bash commands (`terminal`)
* **State Management:** Handles the streaming state of Gemini responses and manages the conversation history.
* **Configuration:** Parses command-line arguments (`yargs`) and loads environment variables (`dotenv`) for setup.
* **Project Structure:** Organized into `core`, `ui`, `tools`, `config`, and `utils` directories using TypeScript. Includes basic build (`tsc`) and start scripts.
This initial version establishes the foundation for a powerful CLI tool enabling developers to use Gemini for coding assistance directly in their terminal environment.
---
Created by yours truly: __Gemini Code__
2025-04-15 21:41:08 -07:00
|
|
|
async function main() {
|
2025-05-02 08:15:46 -07:00
|
|
|
const settings = loadSettings(process.cwd());
|
|
|
|
|
const config = await loadCliConfig(settings.merged);
|
2025-05-17 17:28:44 -07:00
|
|
|
|
|
|
|
|
// 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 = process.env.GEMINI_CODE_SANDBOX_IMAGE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 08:15:46 -07:00
|
|
|
if (settings.merged.theme) {
|
2025-05-09 10:20:08 -07:00
|
|
|
if (!themeManager.setActiveTheme(settings.merged.theme)) {
|
2025-05-08 20:56:46 -07:00
|
|
|
// 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.
|
2025-05-09 10:20:08 -07:00
|
|
|
console.warn(`Warning: Theme "${settings.merged.theme}" not found.`);
|
2025-05-08 20:56:46 -07:00
|
|
|
}
|
2025-05-01 10:34:07 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-29 10:52:05 -07:00
|
|
|
// hop into sandbox if we are outside and sandboxing is enabled
|
|
|
|
|
if (!process.env.SANDBOX) {
|
2025-05-02 08:15:46 -07:00
|
|
|
const sandbox = sandbox_command(config.getSandbox());
|
2025-04-29 10:52:05 -07:00
|
|
|
if (sandbox) {
|
|
|
|
|
await start_sandbox(sandbox);
|
|
|
|
|
process.exit(0);
|
|
|
|
|
}
|
2025-04-24 08:58:47 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-02 08:15:46 -07:00
|
|
|
let input = config.getQuestion();
|
2025-05-14 00:12:04 +00:00
|
|
|
const startupWarnings = await getStartupWarnings();
|
2025-05-02 08:15:46 -07:00
|
|
|
|
2025-04-22 18:32:03 -07:00
|
|
|
// Render UI, passing necessary config values. Check that there is no command line question.
|
|
|
|
|
if (process.stdin.isTTY && input?.length === 0) {
|
2025-04-23 22:00:40 +00:00
|
|
|
const readUpResult = await readPackageUp({ cwd: __dirname });
|
|
|
|
|
const cliVersion =
|
2025-04-23 22:49:18 +00:00
|
|
|
process.env.CLI_VERSION || readUpResult?.packageJson.version || 'unknown';
|
2025-04-23 22:00:40 +00:00
|
|
|
|
2025-04-21 17:41:44 -07:00
|
|
|
render(
|
2025-05-27 21:40:46 +00:00
|
|
|
<React.StrictMode>
|
|
|
|
|
<App
|
|
|
|
|
config={config}
|
|
|
|
|
settings={settings}
|
|
|
|
|
cliVersion={cliVersion}
|
|
|
|
|
startupWarnings={startupWarnings}
|
|
|
|
|
/>
|
|
|
|
|
</React.StrictMode>,
|
2025-04-21 17:41:44 -07:00
|
|
|
);
|
2025-04-22 11:01:09 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2025-04-22 18:32:03 -07:00
|
|
|
// 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();
|
|
|
|
|
}
|
2025-04-22 11:01:09 -07:00
|
|
|
if (!input) {
|
|
|
|
|
console.error('No input provided via stdin.');
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2025-04-21 17:41:44 -07:00
|
|
|
|
2025-04-22 11:01:09 -07:00
|
|
|
// If not a TTY and we have initial input, process it directly
|
|
|
|
|
const geminiClient = new GeminiClient(config);
|
|
|
|
|
const chat = await geminiClient.startChat();
|
|
|
|
|
try {
|
2025-05-27 23:40:25 -07:00
|
|
|
for await (const event of geminiClient.sendMessageStream(
|
|
|
|
|
chat,
|
|
|
|
|
[{ text: input }],
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
)) {
|
2025-04-22 11:01:09 -07:00
|
|
|
if (event.type === 'content') {
|
|
|
|
|
process.stdout.write(event.value);
|
2025-04-21 17:41:44 -07:00
|
|
|
}
|
2025-04-22 11:01:09 -07:00
|
|
|
// We might need to handle other event types later, but for now, just content.
|
2025-04-21 17:41:44 -07:00
|
|
|
}
|
2025-04-22 11:01:09 -07:00
|
|
|
process.stdout.write('\n'); // Add a newline at the end
|
|
|
|
|
process.exit(0);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error processing piped input:', error);
|
2025-04-21 17:41:44 -07:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
Initial commit of Gemini Code CLI
This commit introduces the initial codebase for the Gemini Code CLI, a command-line interface designed to facilitate interaction with the Gemini API for software engineering tasks.
The code was migrated from a previous git repository as a single squashed commit.
Core Features & Components:
* **Gemini Integration:** Leverages the `@google/genai` SDK to interact with the Gemini models, supporting chat history, streaming responses, and function calling (tools).
* **Terminal UI:** Built with Ink (React for CLIs) providing an interactive chat interface within the terminal, including input prompts, message display, loading indicators, and tool interaction elements.
* **Tooling Framework:** Implements a robust tool system allowing Gemini to interact with the local environment. Includes tools for:
* File system listing (`ls`)
* File reading (`read-file`)
* Content searching (`grep`)
* File globbing (`glob`)
* File editing (`edit`)
* File writing (`write-file`)
* Executing bash commands (`terminal`)
* **State Management:** Handles the streaming state of Gemini responses and manages the conversation history.
* **Configuration:** Parses command-line arguments (`yargs`) and loads environment variables (`dotenv`) for setup.
* **Project Structure:** Organized into `core`, `ui`, `tools`, `config`, and `utils` directories using TypeScript. Includes basic build (`tsc`) and start scripts.
This initial version establishes the foundation for a powerful CLI tool enabling developers to use Gemini for coding assistance directly in their terminal environment.
---
Created by yours truly: __Gemini Code__
2025-04-15 21:41:08 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-16 16:53:56 -07:00
|
|
|
// --- Global Unhandled Rejection Handler ---
|
2025-04-18 16:38:01 -07:00
|
|
|
process.on('unhandledRejection', (reason, _promise) => {
|
2025-04-21 12:15:47 -07:00
|
|
|
// 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);
|
2025-04-16 16:53:56 -07:00
|
|
|
});
|
|
|
|
|
|
Initial commit of Gemini Code CLI
This commit introduces the initial codebase for the Gemini Code CLI, a command-line interface designed to facilitate interaction with the Gemini API for software engineering tasks.
The code was migrated from a previous git repository as a single squashed commit.
Core Features & Components:
* **Gemini Integration:** Leverages the `@google/genai` SDK to interact with the Gemini models, supporting chat history, streaming responses, and function calling (tools).
* **Terminal UI:** Built with Ink (React for CLIs) providing an interactive chat interface within the terminal, including input prompts, message display, loading indicators, and tool interaction elements.
* **Tooling Framework:** Implements a robust tool system allowing Gemini to interact with the local environment. Includes tools for:
* File system listing (`ls`)
* File reading (`read-file`)
* Content searching (`grep`)
* File globbing (`glob`)
* File editing (`edit`)
* File writing (`write-file`)
* Executing bash commands (`terminal`)
* **State Management:** Handles the streaming state of Gemini responses and manages the conversation history.
* **Configuration:** Parses command-line arguments (`yargs`) and loads environment variables (`dotenv`) for setup.
* **Project Structure:** Organized into `core`, `ui`, `tools`, `config`, and `utils` directories using TypeScript. Includes basic build (`tsc`) and start scripts.
This initial version establishes the foundation for a powerful CLI tool enabling developers to use Gemini for coding assistance directly in their terminal environment.
---
Created by yours truly: __Gemini Code__
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);
|
Initial commit of Gemini Code CLI
This commit introduces the initial codebase for the Gemini Code CLI, a command-line interface designed to facilitate interaction with the Gemini API for software engineering tasks.
The code was migrated from a previous git repository as a single squashed commit.
Core Features & Components:
* **Gemini Integration:** Leverages the `@google/genai` SDK to interact with the Gemini models, supporting chat history, streaming responses, and function calling (tools).
* **Terminal UI:** Built with Ink (React for CLIs) providing an interactive chat interface within the terminal, including input prompts, message display, loading indicators, and tool interaction elements.
* **Tooling Framework:** Implements a robust tool system allowing Gemini to interact with the local environment. Includes tools for:
* File system listing (`ls`)
* File reading (`read-file`)
* Content searching (`grep`)
* File globbing (`glob`)
* File editing (`edit`)
* File writing (`write-file`)
* Executing bash commands (`terminal`)
* **State Management:** Handles the streaming state of Gemini responses and manages the conversation history.
* **Configuration:** Parses command-line arguments (`yargs`) and loads environment variables (`dotenv`) for setup.
* **Project Structure:** Organized into `core`, `ui`, `tools`, `config`, and `utils` directories using TypeScript. Includes basic build (`tsc`) and start scripts.
This initial version establishes the foundation for a powerful CLI tool enabling developers to use Gemini for coding assistance directly in their terminal environment.
---
Created by yours truly: __Gemini Code__
2025-04-15 21:41:08 -07:00
|
|
|
});
|