2025-04-29 13:29:57 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-06 14:48:49 -07:00
|
|
|
import { useCallback, useMemo } from 'react';
|
2025-04-29 13:29:57 -07:00
|
|
|
import { type PartListUnion } from '@google/genai';
|
2025-05-19 16:56:32 -07:00
|
|
|
import open from 'open';
|
2025-05-30 22:18:01 +00:00
|
|
|
import process from 'node:process';
|
2025-05-06 16:20:28 -07:00
|
|
|
import { UseHistoryManagerReturn } from './useHistoryManager.js';
|
2025-05-30 18:25:47 -07:00
|
|
|
import { Config } from '@gemini-code/core';
|
2025-05-23 14:34:22 -07:00
|
|
|
import { Message, MessageType, HistoryItemWithoutId } from '../types.js';
|
2025-05-16 16:36:50 -07:00
|
|
|
import { createShowMemoryAction } from './useShowMemoryCommand.js';
|
2025-05-28 00:04:26 -07:00
|
|
|
import { GIT_COMMIT_INFO } from '../../generated/git-commit.js';
|
2025-05-30 22:18:01 +00:00
|
|
|
import { formatMemoryUsage } from '../utils/formatters.js';
|
2025-05-23 08:47:19 -07:00
|
|
|
|
|
|
|
|
export interface SlashCommandActionReturn {
|
|
|
|
|
shouldScheduleTool?: boolean;
|
|
|
|
|
toolName?: string;
|
|
|
|
|
toolArgs?: Record<string, unknown>;
|
|
|
|
|
message?: string; // For simple messages or errors
|
|
|
|
|
}
|
2025-04-29 13:29:57 -07:00
|
|
|
|
2025-04-29 23:38:26 +00:00
|
|
|
export interface SlashCommand {
|
2025-05-06 16:20:28 -07:00
|
|
|
name: string;
|
|
|
|
|
altName?: string;
|
2025-05-17 21:57:27 -07:00
|
|
|
description?: string;
|
2025-05-23 08:47:19 -07:00
|
|
|
action: (
|
|
|
|
|
mainCommand: string,
|
|
|
|
|
subCommand?: string,
|
|
|
|
|
args?: string,
|
|
|
|
|
) => void | SlashCommandActionReturn; // Action can now return this object
|
2025-04-29 13:29:57 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
/**
|
|
|
|
|
* Hook to define and process slash commands (e.g., /help, /clear).
|
|
|
|
|
*/
|
2025-04-29 13:29:57 -07:00
|
|
|
export const useSlashCommandProcessor = (
|
2025-05-21 13:31:18 -07:00
|
|
|
config: Config | null,
|
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-05-13 23:55:49 +00:00
|
|
|
onDebugMessage: (message: string) => void,
|
2025-04-30 22:26:28 +00:00
|
|
|
openThemeDialog: () => void,
|
2025-05-21 13:31:18 -07:00
|
|
|
performMemoryRefresh: () => Promise<void>,
|
2025-05-17 21:57:27 -07:00
|
|
|
toggleCorgiMode: () => void,
|
2025-05-21 13:31:18 -07:00
|
|
|
cliVersion: string,
|
2025-04-29 13:29:57 -07:00
|
|
|
) => {
|
2025-05-14 12:37:17 -07:00
|
|
|
const addMessage = useCallback(
|
|
|
|
|
(message: Message) => {
|
2025-05-23 10:34:15 -07:00
|
|
|
// Convert Message to HistoryItemWithoutId
|
|
|
|
|
let historyItemContent: HistoryItemWithoutId;
|
|
|
|
|
if (message.type === MessageType.ABOUT) {
|
|
|
|
|
historyItemContent = {
|
|
|
|
|
type: 'about',
|
|
|
|
|
cliVersion: message.cliVersion,
|
|
|
|
|
osVersion: message.osVersion,
|
|
|
|
|
sandboxEnv: message.sandboxEnv,
|
|
|
|
|
modelVersion: message.modelVersion,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
historyItemContent = {
|
|
|
|
|
type: message.type as
|
|
|
|
|
| MessageType.INFO
|
|
|
|
|
| MessageType.ERROR
|
|
|
|
|
| MessageType.USER,
|
|
|
|
|
text: message.content,
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-05-14 12:37:17 -07:00
|
|
|
addItem(historyItemContent, message.timestamp.getTime());
|
|
|
|
|
},
|
|
|
|
|
[addItem],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const showMemoryAction = useCallback(async () => {
|
|
|
|
|
const actionFn = createShowMemoryAction(config, addMessage);
|
|
|
|
|
await actionFn();
|
|
|
|
|
}, [config, addMessage]);
|
|
|
|
|
|
2025-05-16 16:36:50 -07:00
|
|
|
const addMemoryAction = useCallback(
|
2025-05-23 08:47:19 -07:00
|
|
|
(
|
|
|
|
|
_mainCommand: string,
|
|
|
|
|
_subCommand?: string,
|
|
|
|
|
args?: string,
|
|
|
|
|
): SlashCommandActionReturn | void => {
|
2025-05-16 16:36:50 -07:00
|
|
|
if (!args || args.trim() === '') {
|
|
|
|
|
addMessage({
|
|
|
|
|
type: MessageType.ERROR,
|
|
|
|
|
content: 'Usage: /memory add <text to remember>',
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-05-23 08:47:19 -07:00
|
|
|
// UI feedback for attempting to schedule
|
|
|
|
|
addMessage({
|
|
|
|
|
type: MessageType.INFO,
|
|
|
|
|
content: `Attempting to save to memory: "${args.trim()}"`,
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
});
|
|
|
|
|
// Return info for scheduling the tool call
|
|
|
|
|
return {
|
|
|
|
|
shouldScheduleTool: true,
|
|
|
|
|
toolName: 'save_memory',
|
|
|
|
|
toolArgs: { fact: args.trim() },
|
|
|
|
|
};
|
2025-05-16 16:36:50 -07:00
|
|
|
},
|
2025-05-23 08:47:19 -07:00
|
|
|
[addMessage],
|
2025-05-16 16:36:50 -07:00
|
|
|
);
|
|
|
|
|
|
2025-05-06 14:48:49 -07:00
|
|
|
const slashCommands: SlashCommand[] = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
{
|
|
|
|
|
name: 'help',
|
|
|
|
|
altName: '?',
|
|
|
|
|
description: 'for help on gemini-code',
|
2025-05-16 16:36:50 -07:00
|
|
|
action: (_mainCommand, _subCommand, _args) => {
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage('Opening help.');
|
2025-05-06 14:48:49 -07:00
|
|
|
setShowHelp(true);
|
|
|
|
|
},
|
2025-04-29 22:48:40 +00:00
|
|
|
},
|
2025-05-06 14:48:49 -07:00
|
|
|
{
|
|
|
|
|
name: 'clear',
|
|
|
|
|
description: 'clear the screen',
|
2025-05-16 16:36:50 -07:00
|
|
|
action: (_mainCommand, _subCommand, _args) => {
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage('Clearing terminal.');
|
2025-05-06 16:20:28 -07:00
|
|
|
clearItems();
|
2025-05-15 01:19:15 +00:00
|
|
|
console.clear();
|
2025-05-06 14:48:49 -07:00
|
|
|
refreshStatic();
|
|
|
|
|
},
|
2025-04-30 22:26:28 +00:00
|
|
|
},
|
2025-05-06 14:48:49 -07:00
|
|
|
{
|
|
|
|
|
name: 'theme',
|
|
|
|
|
description: 'change the theme',
|
2025-05-16 16:36:50 -07:00
|
|
|
action: (_mainCommand, _subCommand, _args) => {
|
2025-05-06 14:48:49 -07:00
|
|
|
openThemeDialog();
|
|
|
|
|
},
|
2025-04-30 22:26:28 +00:00
|
|
|
},
|
2025-05-14 12:37:17 -07:00
|
|
|
{
|
2025-05-16 16:36:50 -07:00
|
|
|
name: 'memory',
|
|
|
|
|
description:
|
2025-05-22 10:57:06 -07:00
|
|
|
'Manage memory. Usage: /memory <show|refresh|add> [text for add]',
|
2025-05-16 16:36:50 -07:00
|
|
|
action: (mainCommand, subCommand, args) => {
|
|
|
|
|
switch (subCommand) {
|
|
|
|
|
case 'show':
|
|
|
|
|
showMemoryAction();
|
2025-05-23 08:47:19 -07:00
|
|
|
return; // Explicitly return void
|
2025-05-16 16:36:50 -07:00
|
|
|
case 'refresh':
|
|
|
|
|
performMemoryRefresh();
|
2025-05-23 08:47:19 -07:00
|
|
|
return; // Explicitly return void
|
2025-05-16 16:36:50 -07:00
|
|
|
case 'add':
|
2025-05-23 08:47:19 -07:00
|
|
|
return addMemoryAction(mainCommand, subCommand, args); // Return the object
|
2025-05-16 16:36:50 -07:00
|
|
|
default:
|
|
|
|
|
addMessage({
|
|
|
|
|
type: MessageType.ERROR,
|
|
|
|
|
content: `Unknown /memory command: ${subCommand}. Available: show, refresh, add`,
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
});
|
2025-05-23 08:47:19 -07:00
|
|
|
return; // Explicitly return void
|
2025-05-16 16:36:50 -07:00
|
|
|
}
|
|
|
|
|
},
|
2025-05-14 12:37:17 -07:00
|
|
|
},
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
|
2025-05-17 21:57:27 -07:00
|
|
|
{
|
|
|
|
|
name: 'corgi',
|
|
|
|
|
action: (_mainCommand, _subCommand, _args) => {
|
|
|
|
|
toggleCorgiMode();
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-05-23 10:34:15 -07:00
|
|
|
{
|
|
|
|
|
name: 'about',
|
|
|
|
|
description: 'Show version info',
|
|
|
|
|
action: (_mainCommand, _subCommand, _args) => {
|
|
|
|
|
const osVersion = `${process.platform} ${process.version}`;
|
|
|
|
|
let sandboxEnv = 'no sandbox';
|
|
|
|
|
if (process.env.SANDBOX && process.env.SANDBOX !== 'sandbox-exec') {
|
2025-05-30 19:57:46 +00:00
|
|
|
sandboxEnv = process.env.SANDBOX;
|
2025-05-23 10:34:15 -07:00
|
|
|
} else if (process.env.SANDBOX === 'sandbox-exec') {
|
|
|
|
|
sandboxEnv = `sandbox-exec (${process.env.SEATBELT_PROFILE || 'unknown'})`;
|
|
|
|
|
}
|
|
|
|
|
const modelVersion = config?.getModel() || 'Unknown';
|
|
|
|
|
|
|
|
|
|
addMessage({
|
|
|
|
|
type: MessageType.ABOUT,
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
cliVersion,
|
|
|
|
|
osVersion,
|
|
|
|
|
sandboxEnv,
|
|
|
|
|
modelVersion,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-05-19 16:56:32 -07:00
|
|
|
{
|
|
|
|
|
name: 'bug',
|
|
|
|
|
description: 'Submit a bug report.',
|
|
|
|
|
action: (_mainCommand, _subCommand, args) => {
|
|
|
|
|
let bugDescription = _subCommand || '';
|
|
|
|
|
if (args) {
|
|
|
|
|
bugDescription += ` ${args}`;
|
|
|
|
|
}
|
|
|
|
|
bugDescription = bugDescription.trim();
|
|
|
|
|
|
|
|
|
|
const osVersion = `${process.platform} ${process.version}`;
|
|
|
|
|
let sandboxEnv = 'no sandbox';
|
|
|
|
|
if (process.env.SANDBOX && process.env.SANDBOX !== 'sandbox-exec') {
|
|
|
|
|
sandboxEnv = process.env.SANDBOX.replace(/^gemini-(?:code-)?/, '');
|
|
|
|
|
} else if (process.env.SANDBOX === 'sandbox-exec') {
|
|
|
|
|
sandboxEnv = `sandbox-exec (${process.env.SEATBELT_PROFILE || 'unknown'})`;
|
|
|
|
|
}
|
|
|
|
|
const modelVersion = config?.getModel() || 'Unknown';
|
2025-05-30 22:18:01 +00:00
|
|
|
const memoryUsage = formatMemoryUsage(process.memoryUsage().rss);
|
2025-05-19 16:56:32 -07:00
|
|
|
|
|
|
|
|
const diagnosticInfo = `
|
|
|
|
|
## Describe the bug
|
|
|
|
|
A clear and concise description of what the bug is.
|
|
|
|
|
|
|
|
|
|
## Additional context
|
|
|
|
|
Add any other context about the problem here.
|
|
|
|
|
|
|
|
|
|
## Diagnostic Information
|
|
|
|
|
* **CLI Version:** ${cliVersion}
|
2025-05-28 00:04:26 -07:00
|
|
|
* **Git Commit:** ${GIT_COMMIT_INFO}
|
2025-05-19 16:56:32 -07:00
|
|
|
* **Operating System:** ${osVersion}
|
|
|
|
|
* **Sandbox Environment:** ${sandboxEnv}
|
|
|
|
|
* **Model Version:** ${modelVersion}
|
2025-05-30 22:18:01 +00:00
|
|
|
* **Memory Usage:** ${memoryUsage}
|
2025-05-19 16:56:32 -07:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
let bugReportUrl =
|
|
|
|
|
'https://github.com/google-gemini/gemini-cli/issues/new?template=bug_report.md';
|
|
|
|
|
if (bugDescription) {
|
|
|
|
|
const encodedArgs = encodeURIComponent(bugDescription);
|
|
|
|
|
bugReportUrl += `&title=${encodedArgs}`;
|
|
|
|
|
}
|
|
|
|
|
const encodedBody = encodeURIComponent(diagnosticInfo);
|
|
|
|
|
bugReportUrl += `&body=${encodedBody}`;
|
|
|
|
|
|
|
|
|
|
addMessage({
|
|
|
|
|
type: MessageType.INFO,
|
|
|
|
|
content: `To submit your bug report, please open the following URL in your browser:\n${bugReportUrl}`,
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
});
|
|
|
|
|
(async () => {
|
|
|
|
|
try {
|
|
|
|
|
await open(bugReportUrl);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const errorMessage =
|
|
|
|
|
error instanceof Error ? error.message : String(error);
|
|
|
|
|
addMessage({
|
|
|
|
|
type: MessageType.ERROR,
|
|
|
|
|
content: `Could not open URL in browser: ${errorMessage}`,
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-05-06 14:48:49 -07:00
|
|
|
{
|
|
|
|
|
name: 'quit',
|
|
|
|
|
altName: 'exit',
|
2025-05-14 16:01:29 -07:00
|
|
|
description: 'exit the cli',
|
2025-05-16 16:36:50 -07:00
|
|
|
action: (_mainCommand, _subCommand, _args) => {
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage('Quitting. Good-bye.');
|
2025-05-06 14:48:49 -07:00
|
|
|
process.exit(0);
|
|
|
|
|
},
|
2025-04-29 13:29:57 -07:00
|
|
|
},
|
2025-05-06 14:48:49 -07:00
|
|
|
],
|
2025-05-14 12:37:17 -07:00
|
|
|
[
|
|
|
|
|
onDebugMessage,
|
|
|
|
|
setShowHelp,
|
|
|
|
|
refreshStatic,
|
|
|
|
|
openThemeDialog,
|
|
|
|
|
clearItems,
|
2025-05-16 16:36:50 -07:00
|
|
|
performMemoryRefresh,
|
2025-05-14 12:37:17 -07:00
|
|
|
showMemoryAction,
|
2025-05-16 16:36:50 -07:00
|
|
|
addMemoryAction,
|
|
|
|
|
addMessage,
|
2025-05-17 21:57:27 -07:00
|
|
|
toggleCorgiMode,
|
2025-05-23 08:47:19 -07:00
|
|
|
config,
|
2025-05-21 13:31:18 -07:00
|
|
|
cliVersion,
|
2025-05-14 12:37:17 -07:00
|
|
|
],
|
2025-05-06 14:48:49 -07:00
|
|
|
);
|
2025-04-29 13:29:57 -07:00
|
|
|
|
|
|
|
|
const handleSlashCommand = useCallback(
|
2025-05-23 08:47:19 -07:00
|
|
|
(rawQuery: PartListUnion): SlashCommandActionReturn | boolean => {
|
2025-04-29 13:29:57 -07:00
|
|
|
if (typeof rawQuery !== 'string') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-04-30 00:26:07 +00:00
|
|
|
const trimmed = rawQuery.trim();
|
2025-05-16 16:36:50 -07:00
|
|
|
if (!trimmed.startsWith('/') && !trimmed.startsWith('?')) {
|
2025-04-30 00:26:07 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-06 16:20:28 -07:00
|
|
|
const userMessageTimestamp = Date.now();
|
2025-05-14 12:37:17 -07:00
|
|
|
addItem({ type: MessageType.USER, text: trimmed }, userMessageTimestamp);
|
2025-05-06 16:20:28 -07:00
|
|
|
|
2025-05-16 16:36:50 -07:00
|
|
|
let subCommand: string | undefined;
|
|
|
|
|
let args: string | undefined;
|
|
|
|
|
|
|
|
|
|
const commandToMatch = (() => {
|
|
|
|
|
if (trimmed.startsWith('?')) {
|
2025-05-23 08:47:19 -07:00
|
|
|
return 'help';
|
2025-05-16 16:36:50 -07:00
|
|
|
}
|
|
|
|
|
const parts = trimmed.substring(1).trim().split(/\s+/);
|
|
|
|
|
if (parts.length > 1) {
|
|
|
|
|
subCommand = parts[1];
|
|
|
|
|
}
|
|
|
|
|
if (parts.length > 2) {
|
|
|
|
|
args = parts.slice(2).join(' ');
|
|
|
|
|
}
|
2025-05-23 08:47:19 -07:00
|
|
|
return parts[0];
|
2025-05-16 16:36:50 -07:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
const mainCommand = commandToMatch;
|
|
|
|
|
|
2025-04-29 13:29:57 -07:00
|
|
|
for (const cmd of slashCommands) {
|
2025-05-16 16:36:50 -07:00
|
|
|
if (mainCommand === cmd.name || mainCommand === cmd.altName) {
|
2025-05-23 08:47:19 -07:00
|
|
|
const actionResult = cmd.action(mainCommand, subCommand, args);
|
|
|
|
|
if (
|
|
|
|
|
typeof actionResult === 'object' &&
|
|
|
|
|
actionResult?.shouldScheduleTool
|
|
|
|
|
) {
|
|
|
|
|
return actionResult; // Return the object for useGeminiStream
|
|
|
|
|
}
|
|
|
|
|
return true; // Command was handled, but no tool to schedule
|
2025-04-29 13:29:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 16:36:50 -07:00
|
|
|
addMessage({
|
|
|
|
|
type: MessageType.ERROR,
|
|
|
|
|
content: `Unknown command: ${trimmed}`,
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
});
|
2025-05-23 08:47:19 -07:00
|
|
|
return true; // Indicate command was processed (even if unknown)
|
2025-04-29 13:29:57 -07:00
|
|
|
},
|
2025-05-16 16:36:50 -07:00
|
|
|
[addItem, slashCommands, addMessage],
|
2025-04-29 13:29:57 -07:00
|
|
|
);
|
|
|
|
|
|
2025-04-29 23:38:26 +00:00
|
|
|
return { handleSlashCommand, slashCommands };
|
2025-04-29 13:29:57 -07:00
|
|
|
};
|