2025-04-18 17:44:24 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-30 08:31:32 -07:00
|
|
|
import React, { useCallback } from 'react';
|
2025-05-13 11:24:04 -07:00
|
|
|
import { Text, Box, Key } from 'ink';
|
2025-04-19 12:38:09 -04:00
|
|
|
import { Colors } from '../colors.js';
|
2025-05-01 18:02:04 -07:00
|
|
|
import { Suggestion } from './SuggestionsDisplay.js';
|
2025-05-13 11:24:04 -07:00
|
|
|
import { MultilineTextEditor } from './shared/multiline-editor.js';
|
2025-05-13 16:23:14 -07:00
|
|
|
import { useInputHistory } from '../hooks/useInputHistory.js';
|
2025-04-18 11:12:18 -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
|
|
|
interface InputPromptProps {
|
2025-04-30 08:31:32 -07:00
|
|
|
query: string;
|
2025-05-13 11:24:04 -07:00
|
|
|
onChange: (value: string) => void;
|
|
|
|
|
onChangeAndMoveCursor: (value: string) => void;
|
|
|
|
|
editorState: EditorState;
|
2025-04-17 18:06:21 -04:00
|
|
|
onSubmit: (value: string) => void;
|
2025-04-30 08:31:32 -07:00
|
|
|
showSuggestions: boolean;
|
2025-05-02 14:39:39 -07:00
|
|
|
suggestions: Suggestion[];
|
2025-04-30 08:31:32 -07:00
|
|
|
activeSuggestionIndex: number;
|
|
|
|
|
resetCompletion: () => void;
|
2025-05-13 16:23:14 -07:00
|
|
|
userMessages: readonly string[];
|
2025-05-13 11:24:04 -07:00
|
|
|
navigateSuggestionUp: () => void;
|
|
|
|
|
navigateSuggestionDown: () => void;
|
2025-05-14 17:33:37 -07:00
|
|
|
onClearScreen: () => void;
|
2025-05-18 01:18:32 -07:00
|
|
|
shellModeActive: boolean;
|
|
|
|
|
setShellModeActive: (value: boolean) => void;
|
2025-05-13 11:24:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface EditorState {
|
|
|
|
|
key: number;
|
|
|
|
|
initialCursorOffset?: number;
|
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-30 08:31:32 -07:00
|
|
|
export const InputPrompt: React.FC<InputPromptProps> = ({
|
|
|
|
|
query,
|
2025-05-13 11:24:04 -07:00
|
|
|
onChange,
|
|
|
|
|
onChangeAndMoveCursor,
|
|
|
|
|
editorState,
|
2025-04-30 08:31:32 -07:00
|
|
|
onSubmit,
|
|
|
|
|
showSuggestions,
|
|
|
|
|
suggestions,
|
|
|
|
|
activeSuggestionIndex,
|
2025-05-13 16:23:14 -07:00
|
|
|
userMessages,
|
2025-05-13 11:24:04 -07:00
|
|
|
navigateSuggestionUp,
|
|
|
|
|
navigateSuggestionDown,
|
2025-04-30 08:31:32 -07:00
|
|
|
resetCompletion,
|
2025-05-14 17:33:37 -07:00
|
|
|
onClearScreen,
|
2025-05-18 01:18:32 -07:00
|
|
|
shellModeActive,
|
|
|
|
|
setShellModeActive,
|
2025-04-30 08:31:32 -07:00
|
|
|
}) => {
|
2025-05-13 16:23:14 -07:00
|
|
|
const handleSubmit = useCallback(
|
|
|
|
|
(submittedValue: string) => {
|
|
|
|
|
onSubmit(submittedValue);
|
|
|
|
|
onChangeAndMoveCursor(''); // Clear query after submit
|
|
|
|
|
},
|
|
|
|
|
[onSubmit, onChangeAndMoveCursor],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const inputHistory = useInputHistory({
|
|
|
|
|
userMessages,
|
|
|
|
|
onSubmit: handleSubmit,
|
|
|
|
|
isActive: !showSuggestions, // Input history is active when suggestions are not shown
|
|
|
|
|
currentQuery: query,
|
|
|
|
|
onChangeAndMoveCursor,
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
const handleAutocomplete = useCallback(
|
|
|
|
|
(indexToUse: number) => {
|
|
|
|
|
if (indexToUse < 0 || indexToUse >= suggestions.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const selectedSuggestion = suggestions[indexToUse];
|
|
|
|
|
const trimmedQuery = query.trimStart();
|
2025-04-30 08:31:32 -07:00
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
if (trimmedQuery.startsWith('/')) {
|
|
|
|
|
// Handle / command completion
|
|
|
|
|
const slashIndex = query.indexOf('/');
|
|
|
|
|
const base = query.substring(0, slashIndex + 1);
|
|
|
|
|
const newValue = base + selectedSuggestion.value;
|
2025-05-13 11:24:04 -07:00
|
|
|
onChangeAndMoveCursor(newValue);
|
2025-05-13 16:08:12 -07:00
|
|
|
onSubmit(newValue); // Execute the command
|
2025-05-14 16:01:29 -07:00
|
|
|
onChangeAndMoveCursor(''); // Clear query after submit
|
2025-05-07 12:30:32 -07:00
|
|
|
} else {
|
|
|
|
|
// Handle @ command completion
|
|
|
|
|
const atIndex = query.lastIndexOf('@');
|
|
|
|
|
if (atIndex === -1) return;
|
2025-05-01 00:52:01 +00:00
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
// Find the part of the query after the '@'
|
|
|
|
|
const pathPart = query.substring(atIndex + 1);
|
|
|
|
|
// Find the last slash within that part
|
|
|
|
|
const lastSlashIndexInPath = pathPart.lastIndexOf('/');
|
2025-05-01 00:52:01 +00:00
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
let base = '';
|
|
|
|
|
if (lastSlashIndexInPath === -1) {
|
|
|
|
|
// No slash after '@', replace everything after '@'
|
|
|
|
|
base = query.substring(0, atIndex + 1);
|
|
|
|
|
} else {
|
|
|
|
|
// Slash found, keep everything up to and including the last slash
|
|
|
|
|
base = query.substring(0, atIndex + 1 + lastSlashIndexInPath + 1);
|
|
|
|
|
}
|
2025-05-01 00:52:01 +00:00
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
const newValue = base + selectedSuggestion.value;
|
2025-05-13 11:24:04 -07:00
|
|
|
onChangeAndMoveCursor(newValue);
|
2025-05-07 12:30:32 -07:00
|
|
|
}
|
2025-04-30 08:31:32 -07:00
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
resetCompletion(); // Hide suggestions after selection
|
|
|
|
|
},
|
2025-05-13 16:08:12 -07:00
|
|
|
[query, suggestions, resetCompletion, onChangeAndMoveCursor, onSubmit],
|
2025-05-07 12:30:32 -07:00
|
|
|
);
|
2025-04-30 08:31:32 -07:00
|
|
|
|
2025-05-13 11:24:04 -07:00
|
|
|
const inputPreprocessor = useCallback(
|
2025-05-20 10:12:07 -07:00
|
|
|
(input: string, key: Key) => {
|
2025-05-18 01:18:32 -07:00
|
|
|
if (input === '!' && query === '' && !showSuggestions) {
|
|
|
|
|
setShellModeActive(!shellModeActive);
|
|
|
|
|
onChangeAndMoveCursor(''); // Clear the '!' from input
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-04-30 08:31:32 -07:00
|
|
|
if (showSuggestions) {
|
|
|
|
|
if (key.upArrow) {
|
2025-05-13 11:24:04 -07:00
|
|
|
navigateSuggestionUp();
|
|
|
|
|
return true;
|
2025-04-30 08:31:32 -07:00
|
|
|
} else if (key.downArrow) {
|
2025-05-13 11:24:04 -07:00
|
|
|
navigateSuggestionDown();
|
|
|
|
|
return true;
|
2025-05-07 12:30:32 -07:00
|
|
|
} else if (key.tab) {
|
|
|
|
|
if (suggestions.length > 0) {
|
|
|
|
|
const targetIndex =
|
|
|
|
|
activeSuggestionIndex === -1 ? 0 : activeSuggestionIndex;
|
|
|
|
|
if (targetIndex < suggestions.length) {
|
|
|
|
|
handleAutocomplete(targetIndex);
|
2025-05-13 11:24:04 -07:00
|
|
|
return true;
|
2025-05-07 12:30:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (key.return) {
|
|
|
|
|
if (activeSuggestionIndex >= 0) {
|
|
|
|
|
handleAutocomplete(activeSuggestionIndex);
|
|
|
|
|
} else {
|
|
|
|
|
if (query.trim()) {
|
2025-05-13 16:23:14 -07:00
|
|
|
handleSubmit(query);
|
2025-05-07 12:30:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-13 11:24:04 -07:00
|
|
|
return true;
|
2025-04-30 08:31:32 -07:00
|
|
|
} else if (key.escape) {
|
|
|
|
|
resetCompletion();
|
2025-05-13 11:24:04 -07:00
|
|
|
return true;
|
2025-04-19 19:45:42 +01:00
|
|
|
}
|
2025-05-14 17:33:37 -07:00
|
|
|
} else {
|
|
|
|
|
// Keybindings when suggestions are not shown
|
|
|
|
|
if (key.ctrl && input === 'l') {
|
|
|
|
|
onClearScreen();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (key.ctrl && input === 'p') {
|
|
|
|
|
inputHistory.navigateUp();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (key.ctrl && input === 'n') {
|
|
|
|
|
inputHistory.navigateDown();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-04-19 19:45:42 +01:00
|
|
|
}
|
2025-05-13 11:24:04 -07:00
|
|
|
return false;
|
2025-04-19 19:45:42 +01:00
|
|
|
},
|
2025-05-13 11:24:04 -07:00
|
|
|
[
|
|
|
|
|
handleAutocomplete,
|
|
|
|
|
navigateSuggestionDown,
|
|
|
|
|
navigateSuggestionUp,
|
|
|
|
|
query,
|
|
|
|
|
suggestions,
|
|
|
|
|
showSuggestions,
|
|
|
|
|
resetCompletion,
|
|
|
|
|
activeSuggestionIndex,
|
2025-05-13 16:23:14 -07:00
|
|
|
handleSubmit,
|
2025-05-14 17:33:37 -07:00
|
|
|
inputHistory,
|
|
|
|
|
onClearScreen,
|
2025-05-18 01:18:32 -07:00
|
|
|
shellModeActive,
|
|
|
|
|
setShellModeActive,
|
|
|
|
|
onChangeAndMoveCursor,
|
2025-05-13 11:24:04 -07:00
|
|
|
],
|
2025-04-19 19:45:42 +01:00
|
|
|
);
|
2025-04-18 17:06:16 +01:00
|
|
|
|
2025-04-17 18:06:21 -04:00
|
|
|
return (
|
2025-05-18 01:18:32 -07:00
|
|
|
<Box
|
|
|
|
|
borderStyle="round"
|
|
|
|
|
borderColor={shellModeActive ? Colors.AccentYellow : Colors.AccentBlue}
|
|
|
|
|
paddingX={1}
|
|
|
|
|
>
|
|
|
|
|
<Text color={shellModeActive ? Colors.AccentYellow : Colors.AccentPurple}>
|
|
|
|
|
{shellModeActive ? '! ' : '> '}
|
|
|
|
|
</Text>
|
2025-04-19 12:38:09 -04:00
|
|
|
<Box flexGrow={1}>
|
2025-05-13 11:24:04 -07:00
|
|
|
<MultilineTextEditor
|
|
|
|
|
key={editorState.key.toString()}
|
|
|
|
|
initialCursorOffset={editorState.initialCursorOffset}
|
|
|
|
|
initialText={query}
|
|
|
|
|
onChange={onChange}
|
2025-05-19 16:58:57 -07:00
|
|
|
placeholder="Type your message or @path/to/file"
|
2025-05-13 11:24:04 -07:00
|
|
|
/* Account for width used by the box and > */
|
2025-05-13 16:23:14 -07:00
|
|
|
navigateUp={inputHistory.navigateUp}
|
|
|
|
|
navigateDown={inputHistory.navigateDown}
|
2025-05-13 11:24:04 -07:00
|
|
|
inputPreprocessor={inputPreprocessor}
|
|
|
|
|
widthUsedByParent={3}
|
|
|
|
|
widthFraction={0.9}
|
2025-04-19 12:38:09 -04:00
|
|
|
onSubmit={() => {
|
2025-05-07 12:30:32 -07:00
|
|
|
// This onSubmit is for the TextInput component itself.
|
|
|
|
|
// It should only fire if suggestions are NOT showing,
|
2025-05-13 11:24:04 -07:00
|
|
|
// as inputPreprocessor handles Enter when suggestions are visible.
|
2025-05-07 12:30:32 -07:00
|
|
|
const trimmedQuery = query.trim();
|
|
|
|
|
if (!showSuggestions && trimmedQuery) {
|
2025-05-13 16:23:14 -07:00
|
|
|
handleSubmit(trimmedQuery);
|
2025-05-07 12:30:32 -07:00
|
|
|
}
|
2025-04-19 12:38:09 -04:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
2025-04-17 18:06:21 -04:00
|
|
|
</Box>
|
|
|
|
|
);
|
2025-04-18 18:08:43 -04:00
|
|
|
};
|