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 { useState, useRef, useCallback, useEffect } from 'react';
|
|
|
|
|
import { useInput } from 'ink';
|
2025-04-18 18:08:43 -04:00
|
|
|
import {
|
2025-04-19 19:45:42 +01:00
|
|
|
GeminiClient,
|
2025-05-06 16:20:28 -07:00
|
|
|
GeminiEventType as ServerGeminiEventType,
|
2025-04-19 19:45:42 +01:00
|
|
|
getErrorMessage,
|
|
|
|
|
isNodeError,
|
2025-04-20 21:06:22 +01:00
|
|
|
Config,
|
2025-04-21 10:53:11 -04:00
|
|
|
ToolCallConfirmationDetails,
|
|
|
|
|
ToolCallResponseInfo,
|
2025-04-21 14:32:18 -04:00
|
|
|
ServerToolCallConfirmationDetails,
|
|
|
|
|
ToolConfirmationOutcome,
|
|
|
|
|
ToolResultDisplay,
|
|
|
|
|
ToolEditConfirmationDetails,
|
|
|
|
|
ToolExecuteConfirmationDetails,
|
2025-04-19 19:45:42 +01:00
|
|
|
} from '@gemini-code/server';
|
2025-04-22 11:01:09 -07:00
|
|
|
import { type Chat, type PartListUnion, type Part } from '@google/genai';
|
2025-04-19 19:45:42 +01:00
|
|
|
import {
|
2025-04-21 11:49:46 -07:00
|
|
|
StreamingState,
|
2025-04-19 19:45:42 +01:00
|
|
|
IndividualToolCallDisplay,
|
|
|
|
|
ToolCallStatus,
|
2025-05-07 12:57:19 -07:00
|
|
|
HistoryItemWithoutId,
|
2025-04-19 19:45:42 +01:00
|
|
|
} from '../types.js';
|
2025-05-02 14:39:39 -07:00
|
|
|
import { isAtCommand } from '../utils/commandUtils.js';
|
2025-04-29 13:29:57 -07:00
|
|
|
import { useSlashCommandProcessor } from './slashCommandProcessor.js';
|
2025-04-30 00:26:07 +00:00
|
|
|
import { useShellCommandProcessor } from './shellCommandProcessor.js';
|
2025-05-02 14:39:39 -07:00
|
|
|
import { handleAtCommand } from './atCommandProcessor.js';
|
2025-05-07 21:15:41 -07:00
|
|
|
import { findLastSafeSplitPoint } from '../utils/markdownUtilities.js';
|
2025-05-07 12:57:19 -07:00
|
|
|
import { useStateAndRef } from './useStateAndRef.js';
|
2025-05-06 16:20:28 -07:00
|
|
|
import { UseHistoryManagerReturn } from './useHistoryManager.js';
|
2025-04-28 12:38:07 -07:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
/**
|
|
|
|
|
* Hook to manage the Gemini stream, handle user input, process commands,
|
|
|
|
|
* and interact with the Gemini API and history manager.
|
|
|
|
|
*/
|
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
|
|
|
export const useGeminiStream = (
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem: UseHistoryManagerReturn['addItem'],
|
|
|
|
|
clearItems: UseHistoryManagerReturn['clearItems'],
|
2025-05-05 17:52:29 +00:00
|
|
|
refreshStatic: () => void,
|
2025-05-05 20:48:34 +00:00
|
|
|
setShowHelp: React.Dispatch<React.SetStateAction<boolean>>,
|
2025-04-20 21:06:22 +01:00
|
|
|
config: Config,
|
2025-04-30 22:26:28 +00:00
|
|
|
openThemeDialog: () => void,
|
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-21 12:59:31 -07:00
|
|
|
const toolRegistry = config.getToolRegistry();
|
2025-04-17 18:06:21 -04:00
|
|
|
const [streamingState, setStreamingState] = useState<StreamingState>(
|
|
|
|
|
StreamingState.Idle,
|
|
|
|
|
);
|
2025-04-20 20:20:40 +01:00
|
|
|
const [debugMessage, setDebugMessage] = useState<string>('');
|
2025-04-17 18:06:21 -04:00
|
|
|
const [initError, setInitError] = useState<string | null>(null);
|
|
|
|
|
const abortControllerRef = useRef<AbortController | null>(null);
|
|
|
|
|
const chatSessionRef = useRef<Chat | null>(null);
|
|
|
|
|
const geminiClientRef = useRef<GeminiClient | null>(null);
|
2025-05-07 12:57:19 -07:00
|
|
|
const [pendingHistoryItemRef, setPendingHistoryItem] =
|
|
|
|
|
useStateAndRef<HistoryItemWithoutId | null>(null);
|
2025-04-17 18:06:21 -04:00
|
|
|
|
2025-04-29 23:38:26 +00:00
|
|
|
const { handleSlashCommand, slashCommands } = useSlashCommandProcessor(
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem,
|
|
|
|
|
clearItems,
|
2025-05-05 17:52:29 +00:00
|
|
|
refreshStatic,
|
2025-05-05 20:48:34 +00:00
|
|
|
setShowHelp,
|
2025-04-29 13:29:57 -07:00
|
|
|
setDebugMessage,
|
2025-04-30 22:26:28 +00:00
|
|
|
openThemeDialog,
|
2025-04-29 13:29:57 -07:00
|
|
|
);
|
|
|
|
|
|
2025-04-30 00:26:07 +00:00
|
|
|
const { handleShellCommand } = useShellCommandProcessor(
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem,
|
2025-04-30 00:26:07 +00:00
|
|
|
setStreamingState,
|
|
|
|
|
setDebugMessage,
|
|
|
|
|
config,
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-17 18:06:21 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
setInitError(null);
|
|
|
|
|
if (!geminiClientRef.current) {
|
|
|
|
|
try {
|
2025-04-22 11:01:09 -07:00
|
|
|
geminiClientRef.current = new GeminiClient(config);
|
2025-04-18 17:47:49 -04:00
|
|
|
} catch (error: unknown) {
|
2025-05-06 16:20:28 -07:00
|
|
|
const errorMsg = `Failed to initialize client: ${getErrorMessage(error) || 'Unknown error'}`;
|
|
|
|
|
setInitError(errorMsg);
|
|
|
|
|
addItem({ type: 'error', text: errorMsg }, Date.now());
|
2025-04-17 18:06:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-06 16:20:28 -07:00
|
|
|
}, [config, addItem]);
|
2025-04-17 18:06:21 -04:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
useInput((_input, key) => {
|
2025-05-09 23:29:02 -07:00
|
|
|
if (streamingState !== StreamingState.Idle && key.escape) {
|
2025-04-17 18:06:21 -04:00
|
|
|
abortControllerRef.current?.abort();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const submitQuery = useCallback(
|
|
|
|
|
async (query: PartListUnion) => {
|
2025-04-19 19:45:42 +01:00
|
|
|
if (streamingState === StreamingState.Responding) return;
|
|
|
|
|
if (typeof query === 'string' && query.trim().length === 0) return;
|
2025-04-17 18:06:21 -04:00
|
|
|
|
2025-04-29 13:29:57 -07:00
|
|
|
const userMessageTimestamp = Date.now();
|
2025-04-29 15:39:36 -07:00
|
|
|
let queryToSendToGemini: PartListUnion | null = null;
|
2025-04-29 13:29:57 -07:00
|
|
|
|
2025-05-05 20:48:34 +00:00
|
|
|
setShowHelp(false);
|
|
|
|
|
|
2025-05-09 23:29:02 -07:00
|
|
|
abortControllerRef.current ??= new AbortController();
|
|
|
|
|
const signal = abortControllerRef.current.signal;
|
|
|
|
|
|
2025-04-20 20:20:40 +01:00
|
|
|
if (typeof query === 'string') {
|
2025-04-29 15:39:36 -07:00
|
|
|
const trimmedQuery = query.trim();
|
|
|
|
|
setDebugMessage(`User query: '${trimmedQuery}'`);
|
2025-04-25 00:12:20 +00:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// Handle UI-only commands first
|
|
|
|
|
if (handleSlashCommand(trimmedQuery)) return;
|
|
|
|
|
if (handleShellCommand(trimmedQuery)) return;
|
2025-04-29 13:29:57 -07:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// Handle @-commands (which might involve tool calls)
|
2025-04-29 15:39:36 -07:00
|
|
|
if (isAtCommand(trimmedQuery)) {
|
|
|
|
|
const atCommandResult = await handleAtCommand({
|
|
|
|
|
query: trimmedQuery,
|
|
|
|
|
config,
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem,
|
2025-04-29 15:39:36 -07:00
|
|
|
setDebugMessage,
|
2025-05-06 16:20:28 -07:00
|
|
|
messageId: userMessageTimestamp,
|
2025-05-09 23:29:02 -07:00
|
|
|
signal,
|
2025-04-29 15:39:36 -07:00
|
|
|
});
|
2025-05-06 16:20:28 -07:00
|
|
|
if (!atCommandResult.shouldProceed) return;
|
2025-04-29 15:39:36 -07:00
|
|
|
queryToSendToGemini = atCommandResult.processedQuery;
|
|
|
|
|
} else {
|
2025-05-06 16:20:28 -07:00
|
|
|
// Normal query for Gemini
|
|
|
|
|
addItem({ type: 'user', text: trimmedQuery }, userMessageTimestamp);
|
2025-04-29 15:39:36 -07:00
|
|
|
queryToSendToGemini = trimmedQuery;
|
|
|
|
|
}
|
2025-04-29 13:29:57 -07:00
|
|
|
} else {
|
2025-05-06 16:20:28 -07:00
|
|
|
// It's a function response (PartListUnion that isn't a string)
|
2025-04-29 15:39:36 -07:00
|
|
|
queryToSendToGemini = query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (queryToSendToGemini === null) {
|
|
|
|
|
setDebugMessage(
|
|
|
|
|
'Query processing resulted in null, not sending to Gemini.',
|
|
|
|
|
);
|
|
|
|
|
return;
|
2025-04-20 20:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-17 18:06:21 -04:00
|
|
|
const client = geminiClientRef.current;
|
|
|
|
|
if (!client) {
|
2025-05-06 16:20:28 -07:00
|
|
|
const errorMsg = 'Gemini client is not available.';
|
|
|
|
|
setInitError(errorMsg);
|
|
|
|
|
addItem({ type: 'error', text: errorMsg }, Date.now());
|
2025-04-17 18:06:21 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!chatSessionRef.current) {
|
2025-04-19 19:45:42 +01:00
|
|
|
try {
|
2025-04-22 11:01:09 -07:00
|
|
|
chatSessionRef.current = await client.startChat();
|
2025-04-19 19:45:42 +01:00
|
|
|
} catch (err: unknown) {
|
2025-05-06 16:20:28 -07:00
|
|
|
const errorMsg = `Failed to start chat: ${getErrorMessage(err)}`;
|
|
|
|
|
setInitError(errorMsg);
|
|
|
|
|
addItem({ type: 'error', text: errorMsg }, Date.now());
|
2025-04-19 19:45:42 +01:00
|
|
|
setStreamingState(StreamingState.Idle);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-04-17 18:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setStreamingState(StreamingState.Responding);
|
|
|
|
|
setInitError(null);
|
|
|
|
|
const chat = chatSessionRef.current;
|
2025-04-19 19:45:42 +01:00
|
|
|
|
2025-04-17 18:06:21 -04:00
|
|
|
try {
|
2025-04-29 15:39:36 -07:00
|
|
|
const stream = client.sendMessageStream(
|
|
|
|
|
chat,
|
|
|
|
|
queryToSendToGemini,
|
|
|
|
|
signal,
|
|
|
|
|
);
|
2025-04-19 19:45:42 +01:00
|
|
|
|
2025-05-07 21:15:41 -07:00
|
|
|
let geminiMessageBuffer = '';
|
2025-04-19 19:45:42 +01:00
|
|
|
|
|
|
|
|
for await (const event of stream) {
|
|
|
|
|
if (event.type === ServerGeminiEventType.Content) {
|
2025-05-07 22:58:41 -07:00
|
|
|
if (
|
|
|
|
|
pendingHistoryItemRef.current?.type !== 'gemini' &&
|
|
|
|
|
pendingHistoryItemRef.current?.type !== 'gemini_content'
|
|
|
|
|
) {
|
2025-05-07 12:57:19 -07:00
|
|
|
// Flush out existing pending history item.
|
|
|
|
|
if (pendingHistoryItemRef.current) {
|
|
|
|
|
addItem(pendingHistoryItemRef.current, userMessageTimestamp);
|
|
|
|
|
}
|
|
|
|
|
setPendingHistoryItem({
|
2025-05-07 22:58:41 -07:00
|
|
|
// Use the 'gemini' type for the initial history item.
|
2025-05-07 12:57:19 -07:00
|
|
|
type: 'gemini',
|
2025-05-07 21:15:41 -07:00
|
|
|
text: '',
|
2025-05-07 12:57:19 -07:00
|
|
|
});
|
2025-05-07 21:15:41 -07:00
|
|
|
geminiMessageBuffer = '';
|
2025-05-07 12:57:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-07 21:15:41 -07:00
|
|
|
geminiMessageBuffer += event.value;
|
|
|
|
|
|
|
|
|
|
// Split large messages for better rendering performance. Ideally,
|
|
|
|
|
// we should maximize the amount of output sent to <Static />.
|
|
|
|
|
const splitPoint = findLastSafeSplitPoint(geminiMessageBuffer);
|
|
|
|
|
if (splitPoint === geminiMessageBuffer.length) {
|
2025-05-07 12:57:19 -07:00
|
|
|
// Update the existing message with accumulated content
|
2025-05-07 22:58:41 -07:00
|
|
|
setPendingHistoryItem((item) => ({
|
|
|
|
|
type: item?.type as 'gemini' | 'gemini_content',
|
2025-05-07 21:15:41 -07:00
|
|
|
text: geminiMessageBuffer,
|
2025-05-07 22:58:41 -07:00
|
|
|
}));
|
2025-05-07 12:57:19 -07:00
|
|
|
} else {
|
|
|
|
|
// This indicates that we need to split up this Gemini Message.
|
|
|
|
|
// Splitting a message is primarily a performance consideration. There is a
|
|
|
|
|
// <Static> component at the root of App.tsx which takes care of rendering
|
|
|
|
|
// content statically or dynamically. Everything but the last message is
|
|
|
|
|
// treated as static in order to prevent re-rendering an entire message history
|
|
|
|
|
// multiple times per-second (as streaming occurs). Prior to this change you'd
|
|
|
|
|
// see heavy flickering of the terminal. This ensures that larger messages get
|
|
|
|
|
// broken up so that there are more "statically" rendered.
|
2025-05-07 21:15:41 -07:00
|
|
|
const beforeText = geminiMessageBuffer.substring(0, splitPoint);
|
|
|
|
|
const afterText = geminiMessageBuffer.substring(splitPoint);
|
|
|
|
|
geminiMessageBuffer = afterText; // Continue accumulating from split point
|
2025-05-07 12:57:19 -07:00
|
|
|
addItem(
|
2025-05-07 22:58:41 -07:00
|
|
|
{
|
|
|
|
|
type: pendingHistoryItemRef.current?.type as
|
|
|
|
|
| 'gemini'
|
|
|
|
|
| 'gemini_content',
|
|
|
|
|
text: beforeText,
|
|
|
|
|
},
|
2025-05-06 16:20:28 -07:00
|
|
|
userMessageTimestamp,
|
2025-04-19 11:07:39 +01:00
|
|
|
);
|
2025-05-07 12:57:19 -07:00
|
|
|
setPendingHistoryItem({
|
2025-05-07 22:58:41 -07:00
|
|
|
type: 'gemini_content',
|
2025-05-07 12:57:19 -07:00
|
|
|
text: afterText,
|
|
|
|
|
});
|
2025-04-19 19:45:42 +01:00
|
|
|
}
|
|
|
|
|
} else if (event.type === ServerGeminiEventType.ToolCallRequest) {
|
|
|
|
|
const { callId, name, args } = event.value;
|
2025-05-06 16:20:28 -07:00
|
|
|
const cliTool = toolRegistry.getTool(name);
|
2025-04-19 19:45:42 +01:00
|
|
|
if (!cliTool) {
|
|
|
|
|
console.error(`CLI Tool "${name}" not found!`);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-04-17 18:06:21 -04:00
|
|
|
|
2025-05-07 12:57:19 -07:00
|
|
|
if (pendingHistoryItemRef.current?.type !== 'tool_group') {
|
|
|
|
|
// Flush out existing pending history item.
|
|
|
|
|
if (pendingHistoryItemRef.current) {
|
|
|
|
|
addItem(pendingHistoryItemRef.current, userMessageTimestamp);
|
|
|
|
|
}
|
|
|
|
|
setPendingHistoryItem({
|
|
|
|
|
type: 'tool_group',
|
|
|
|
|
tools: [],
|
|
|
|
|
});
|
2025-04-19 19:45:42 +01:00
|
|
|
}
|
2025-04-17 18:06:21 -04:00
|
|
|
|
2025-04-21 10:53:11 -04:00
|
|
|
let description: string;
|
|
|
|
|
try {
|
|
|
|
|
description = cliTool.getDescription(args);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
description = `Error: Unable to get description: ${getErrorMessage(e)}`;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-19 19:45:42 +01:00
|
|
|
const toolCallDisplay: IndividualToolCallDisplay = {
|
|
|
|
|
callId,
|
2025-04-22 07:48:12 -04:00
|
|
|
name: cliTool.displayName,
|
2025-04-21 10:53:11 -04:00
|
|
|
description,
|
2025-04-19 19:45:42 +01:00
|
|
|
status: ToolCallStatus.Pending,
|
|
|
|
|
resultDisplay: undefined,
|
|
|
|
|
confirmationDetails: undefined,
|
|
|
|
|
};
|
2025-04-17 18:06:21 -04:00
|
|
|
|
2025-05-07 12:57:19 -07:00
|
|
|
// Add pending tool call to the UI history group
|
|
|
|
|
setPendingHistoryItem((pending) =>
|
|
|
|
|
// Should always be true.
|
|
|
|
|
pending?.type === 'tool_group'
|
|
|
|
|
? {
|
|
|
|
|
...pending,
|
|
|
|
|
tools: [...pending.tools, toolCallDisplay],
|
2025-05-06 16:20:28 -07:00
|
|
|
}
|
2025-05-07 12:57:19 -07:00
|
|
|
: null,
|
|
|
|
|
);
|
2025-04-21 10:53:11 -04:00
|
|
|
} else if (event.type === ServerGeminiEventType.ToolCallResponse) {
|
2025-04-21 14:32:18 -04:00
|
|
|
const status = event.value.error
|
|
|
|
|
? ToolCallStatus.Error
|
|
|
|
|
: ToolCallStatus.Success;
|
|
|
|
|
updateFunctionResponseUI(event.value, status);
|
2025-04-21 10:53:11 -04:00
|
|
|
} else if (
|
|
|
|
|
event.type === ServerGeminiEventType.ToolCallConfirmation
|
|
|
|
|
) {
|
2025-04-21 14:32:18 -04:00
|
|
|
const confirmationDetails = wireConfirmationSubmission(event.value);
|
|
|
|
|
updateConfirmingFunctionStatusUI(
|
|
|
|
|
event.value.request.callId,
|
|
|
|
|
confirmationDetails,
|
2025-04-19 19:45:42 +01:00
|
|
|
);
|
2025-04-21 10:53:11 -04:00
|
|
|
setStreamingState(StreamingState.WaitingForConfirmation);
|
2025-05-06 16:20:28 -07:00
|
|
|
return; // Wait for user confirmation
|
2025-05-09 22:47:18 -07:00
|
|
|
} else if (event.type === ServerGeminiEventType.UserCancelled) {
|
|
|
|
|
// Flush out existing pending history item.
|
|
|
|
|
if (pendingHistoryItemRef.current) {
|
2025-05-09 23:29:02 -07:00
|
|
|
// If the pending item is a tool_group, update statuses to Canceled
|
|
|
|
|
if (pendingHistoryItemRef.current.type === 'tool_group') {
|
|
|
|
|
const updatedTools = pendingHistoryItemRef.current.tools.map(
|
|
|
|
|
(tool) => {
|
|
|
|
|
if (
|
|
|
|
|
tool.status === ToolCallStatus.Pending ||
|
|
|
|
|
tool.status === ToolCallStatus.Confirming ||
|
|
|
|
|
tool.status === ToolCallStatus.Executing
|
|
|
|
|
) {
|
|
|
|
|
return { ...tool, status: ToolCallStatus.Canceled };
|
|
|
|
|
}
|
|
|
|
|
return tool;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const pendingHistoryItem = pendingHistoryItemRef.current;
|
|
|
|
|
pendingHistoryItem.tools = updatedTools;
|
|
|
|
|
addItem(pendingHistoryItem, userMessageTimestamp);
|
|
|
|
|
} else {
|
|
|
|
|
addItem(pendingHistoryItemRef.current, userMessageTimestamp);
|
|
|
|
|
}
|
2025-05-09 22:47:18 -07:00
|
|
|
setPendingHistoryItem(null);
|
|
|
|
|
}
|
|
|
|
|
addItem(
|
|
|
|
|
{ type: 'info', text: 'User cancelled the request.' },
|
|
|
|
|
userMessageTimestamp,
|
|
|
|
|
);
|
|
|
|
|
setStreamingState(StreamingState.Idle);
|
|
|
|
|
return; // Stop processing the stream
|
2025-04-19 19:45:42 +01:00
|
|
|
}
|
2025-05-06 16:20:28 -07:00
|
|
|
} // End stream loop
|
2025-04-21 14:32:18 -04:00
|
|
|
|
2025-05-07 12:57:19 -07:00
|
|
|
// We're waiting for user input now so all pending history can be committed.
|
|
|
|
|
if (pendingHistoryItemRef.current) {
|
|
|
|
|
addItem(pendingHistoryItemRef.current, userMessageTimestamp);
|
|
|
|
|
setPendingHistoryItem(null);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-21 14:32:18 -04:00
|
|
|
setStreamingState(StreamingState.Idle);
|
2025-04-18 17:47:49 -04:00
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!isNodeError(error) || error.name !== 'AbortError') {
|
2025-04-19 19:45:42 +01:00
|
|
|
console.error('Error processing stream or executing tool:', error);
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem(
|
2025-04-17 18:06:21 -04:00
|
|
|
{
|
|
|
|
|
type: 'error',
|
2025-05-06 16:20:28 -07:00
|
|
|
text: `[Stream Error: ${getErrorMessage(error)}]`,
|
2025-04-17 18:06:21 -04:00
|
|
|
},
|
2025-05-06 16:20:28 -07:00
|
|
|
userMessageTimestamp,
|
2025-04-17 18:06:21 -04: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
|
|
|
}
|
2025-04-21 14:32:18 -04:00
|
|
|
setStreamingState(StreamingState.Idle);
|
2025-04-17 18:06:21 -04:00
|
|
|
} finally {
|
|
|
|
|
abortControllerRef.current = null;
|
|
|
|
|
}
|
2025-04-21 10:53:11 -04:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// --- Helper functions for updating tool UI ---
|
|
|
|
|
|
2025-04-21 14:32:18 -04:00
|
|
|
function updateConfirmingFunctionStatusUI(
|
|
|
|
|
callId: string,
|
|
|
|
|
confirmationDetails: ToolCallConfirmationDetails | undefined,
|
|
|
|
|
) {
|
2025-05-07 12:57:19 -07:00
|
|
|
if (pendingHistoryItemRef.current?.type !== 'tool_group') return;
|
|
|
|
|
setPendingHistoryItem((item) =>
|
|
|
|
|
item?.type === 'tool_group'
|
|
|
|
|
? {
|
|
|
|
|
...item,
|
|
|
|
|
tools: item.tools.map((tool) =>
|
|
|
|
|
tool.callId === callId
|
|
|
|
|
? {
|
|
|
|
|
...tool,
|
|
|
|
|
status: ToolCallStatus.Confirming,
|
|
|
|
|
confirmationDetails,
|
|
|
|
|
}
|
|
|
|
|
: tool,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
: null,
|
2025-04-21 14:32:18 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateFunctionResponseUI(
|
|
|
|
|
toolResponse: ToolCallResponseInfo,
|
|
|
|
|
status: ToolCallStatus,
|
|
|
|
|
) {
|
2025-05-07 12:57:19 -07:00
|
|
|
setPendingHistoryItem((item) =>
|
|
|
|
|
item?.type === 'tool_group'
|
|
|
|
|
? {
|
|
|
|
|
...item,
|
|
|
|
|
tools: item.tools.map((tool) => {
|
|
|
|
|
if (tool.callId === toolResponse.callId) {
|
|
|
|
|
return {
|
|
|
|
|
...tool,
|
|
|
|
|
status,
|
|
|
|
|
resultDisplay: toolResponse.resultDisplay,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return tool;
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
: null,
|
2025-04-21 10:53:11 -04:00
|
|
|
);
|
|
|
|
|
}
|
2025-04-21 14:32:18 -04:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// Wires the server-side confirmation callback to UI updates and state changes
|
2025-04-21 14:32:18 -04:00
|
|
|
function wireConfirmationSubmission(
|
|
|
|
|
confirmationDetails: ServerToolCallConfirmationDetails,
|
|
|
|
|
): ToolCallConfirmationDetails {
|
|
|
|
|
const originalConfirmationDetails = confirmationDetails.details;
|
|
|
|
|
const request = confirmationDetails.request;
|
|
|
|
|
const resubmittingConfirm = async (
|
|
|
|
|
outcome: ToolConfirmationOutcome,
|
|
|
|
|
) => {
|
2025-05-06 16:20:28 -07:00
|
|
|
// Call the original server-side handler first
|
2025-04-21 14:32:18 -04:00
|
|
|
originalConfirmationDetails.onConfirm(outcome);
|
|
|
|
|
|
2025-05-06 22:26:38 -07:00
|
|
|
// Ensure UI updates before potentially long-running operations
|
2025-05-07 12:57:19 -07:00
|
|
|
if (pendingHistoryItemRef?.current?.type === 'tool_group') {
|
|
|
|
|
setPendingHistoryItem((item) =>
|
|
|
|
|
item?.type === 'tool_group'
|
|
|
|
|
? {
|
|
|
|
|
...item,
|
|
|
|
|
tools: item.tools.map((tool) =>
|
|
|
|
|
tool.callId === request.callId
|
|
|
|
|
? {
|
|
|
|
|
...tool,
|
|
|
|
|
confirmationDetails: undefined,
|
|
|
|
|
status: ToolCallStatus.Executing,
|
|
|
|
|
}
|
|
|
|
|
: tool,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
: item,
|
2025-05-06 22:26:38 -07:00
|
|
|
);
|
|
|
|
|
refreshStatic();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-21 14:32:18 -04:00
|
|
|
if (outcome === ToolConfirmationOutcome.Cancel) {
|
2025-05-09 23:29:02 -07:00
|
|
|
declineToolExecution(
|
|
|
|
|
'User rejected function call.',
|
|
|
|
|
ToolCallStatus.Error,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
const tool = toolRegistry.getTool(request.name);
|
|
|
|
|
if (!tool) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Tool "${request.name}" not found or is not registered.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
abortControllerRef.current = new AbortController();
|
|
|
|
|
const result = await tool.execute(
|
|
|
|
|
request.args,
|
|
|
|
|
abortControllerRef.current.signal,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (abortControllerRef.current.signal.aborted) {
|
|
|
|
|
declineToolExecution(
|
|
|
|
|
result.llmContent,
|
|
|
|
|
ToolCallStatus.Canceled,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const functionResponse: Part = {
|
|
|
|
|
functionResponse: {
|
|
|
|
|
name: request.name,
|
|
|
|
|
id: request.callId,
|
|
|
|
|
response: { output: result.llmContent },
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const responseInfo: ToolCallResponseInfo = {
|
|
|
|
|
callId: request.callId,
|
|
|
|
|
responsePart: functionResponse,
|
|
|
|
|
resultDisplay: result.returnDisplay,
|
|
|
|
|
error: undefined,
|
|
|
|
|
};
|
|
|
|
|
updateFunctionResponseUI(responseInfo, ToolCallStatus.Success);
|
|
|
|
|
setStreamingState(StreamingState.Idle);
|
|
|
|
|
await submitQuery(functionResponse);
|
|
|
|
|
} finally {
|
|
|
|
|
abortControllerRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function declineToolExecution(
|
|
|
|
|
declineMessage: string,
|
|
|
|
|
status: ToolCallStatus,
|
|
|
|
|
) {
|
2025-04-21 14:32:18 -04:00
|
|
|
let resultDisplay: ToolResultDisplay | undefined;
|
|
|
|
|
if ('fileDiff' in originalConfirmationDetails) {
|
|
|
|
|
resultDisplay = {
|
|
|
|
|
fileDiff: (
|
|
|
|
|
originalConfirmationDetails as ToolEditConfirmationDetails
|
|
|
|
|
).fileDiff,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
resultDisplay = `~~${(originalConfirmationDetails as ToolExecuteConfirmationDetails).command}~~`;
|
|
|
|
|
}
|
|
|
|
|
const functionResponse: Part = {
|
|
|
|
|
functionResponse: {
|
|
|
|
|
id: request.callId,
|
|
|
|
|
name: request.name,
|
2025-05-09 23:29:02 -07:00
|
|
|
response: { error: declineMessage },
|
2025-04-21 14:32:18 -04:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const responseInfo: ToolCallResponseInfo = {
|
|
|
|
|
callId: request.callId,
|
|
|
|
|
responsePart: functionResponse,
|
|
|
|
|
resultDisplay,
|
2025-05-09 23:29:02 -07:00
|
|
|
error: new Error(declineMessage),
|
2025-05-06 22:11:29 -07:00
|
|
|
};
|
|
|
|
|
|
2025-05-10 00:24:33 -07:00
|
|
|
// Update conversation history without re-issuing another request to indicate the decline.
|
|
|
|
|
const history = chatSessionRef.current?.getHistory();
|
|
|
|
|
if (history) {
|
|
|
|
|
history.push({
|
|
|
|
|
role: 'model',
|
|
|
|
|
parts: [functionResponse],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 23:29:02 -07:00
|
|
|
// Update UI to show cancellation/error
|
|
|
|
|
updateFunctionResponseUI(responseInfo, status);
|
2025-05-06 22:11:29 -07:00
|
|
|
setStreamingState(StreamingState.Idle);
|
2025-04-21 14:32:18 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...originalConfirmationDetails,
|
|
|
|
|
onConfirm: resubmittingConfirm,
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-04-17 18:06:21 -04:00
|
|
|
},
|
2025-04-19 19:45:42 +01:00
|
|
|
[
|
|
|
|
|
streamingState,
|
2025-05-07 12:57:19 -07:00
|
|
|
setShowHelp,
|
2025-04-29 13:29:57 -07:00
|
|
|
handleSlashCommand,
|
2025-05-06 14:48:49 -07:00
|
|
|
handleShellCommand,
|
2025-05-07 12:57:19 -07:00
|
|
|
config,
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem,
|
2025-05-07 12:57:19 -07:00
|
|
|
pendingHistoryItemRef,
|
|
|
|
|
setPendingHistoryItem,
|
2025-05-06 14:48:49 -07:00
|
|
|
toolRegistry,
|
2025-05-06 22:26:38 -07:00
|
|
|
refreshStatic,
|
2025-04-19 19:45:42 +01:00
|
|
|
],
|
2025-04-17 18:06:21 -04:00
|
|
|
);
|
|
|
|
|
|
2025-04-29 23:38:26 +00:00
|
|
|
return {
|
|
|
|
|
streamingState,
|
|
|
|
|
submitQuery,
|
|
|
|
|
initError,
|
|
|
|
|
debugMessage,
|
|
|
|
|
slashCommands,
|
2025-05-07 12:57:19 -07:00
|
|
|
// Normally we would be concerned that the ref would not be up-to-date, but
|
|
|
|
|
// this isn't a concern as the ref is updated whenever the corresponding
|
|
|
|
|
// state is updated.
|
|
|
|
|
pendingHistoryItem: pendingHistoryItemRef.current,
|
2025-04-29 23:38:26 +00: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
|
|
|
};
|