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-04-30 00:26:07 +00:00
|
|
|
import { getCommandFromQuery } from '../utils/commandUtils.js';
|
2025-05-06 16:20:28 -07:00
|
|
|
import { UseHistoryManagerReturn } from './useHistoryManager.js';
|
2025-05-14 12:37:17 -07:00
|
|
|
import { Config } from '@gemini-code/server'; // Import Config
|
|
|
|
|
import { Message, MessageType, HistoryItemWithoutId } from '../types.js'; // Import Message types
|
|
|
|
|
import {
|
|
|
|
|
createShowMemoryAction,
|
|
|
|
|
SHOW_MEMORY_COMMAND_NAME,
|
|
|
|
|
} from './useShowMemoryCommand.js';
|
|
|
|
|
import { REFRESH_MEMORY_COMMAND_NAME } from './useRefreshMemoryCommand.js'; // Only import name now
|
|
|
|
|
import process from 'node:process'; // For process.exit
|
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;
|
|
|
|
|
description: string;
|
2025-05-14 12:37:17 -07:00
|
|
|
action: (value: PartListUnion | string) => void; // Allow string for simpler actions
|
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-14 12:37:17 -07:00
|
|
|
config: Config | null, // Add config here
|
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-14 12:37:17 -07:00
|
|
|
performMemoryRefresh: () => Promise<void>, // Add performMemoryRefresh prop
|
2025-04-29 13:29:57 -07:00
|
|
|
) => {
|
2025-05-14 12:37:17 -07:00
|
|
|
const addMessage = useCallback(
|
|
|
|
|
(message: Message) => {
|
|
|
|
|
// Convert Message to HistoryItemWithoutId
|
|
|
|
|
const historyItemContent: HistoryItemWithoutId = {
|
|
|
|
|
type: message.type, // MessageType enum should be compatible with HistoryItemWithoutId string literal types
|
|
|
|
|
text: message.content,
|
|
|
|
|
};
|
|
|
|
|
addItem(historyItemContent, message.timestamp.getTime());
|
|
|
|
|
},
|
|
|
|
|
[addItem],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const showMemoryAction = useCallback(async () => {
|
|
|
|
|
const actionFn = createShowMemoryAction(config, addMessage);
|
|
|
|
|
await actionFn();
|
|
|
|
|
}, [config, addMessage]);
|
|
|
|
|
|
2025-05-06 14:48:49 -07:00
|
|
|
const slashCommands: SlashCommand[] = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
{
|
|
|
|
|
name: 'help',
|
|
|
|
|
altName: '?',
|
|
|
|
|
description: 'for help on gemini-code',
|
2025-05-14 12:37:17 -07:00
|
|
|
action: (_value: PartListUnion | string) => {
|
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-14 12:37:17 -07:00
|
|
|
action: (_value: PartListUnion | string) => {
|
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-14 12:37:17 -07:00
|
|
|
action: (_value) => {
|
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
|
|
|
{
|
|
|
|
|
name: REFRESH_MEMORY_COMMAND_NAME.substring(1), // Remove leading '/'
|
|
|
|
|
description: 'Reloads instructions from all GEMINI.md files.',
|
|
|
|
|
action: performMemoryRefresh, // Use the passed in function
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: SHOW_MEMORY_COMMAND_NAME.substring(1), // Remove leading '/'
|
|
|
|
|
description: 'Displays the current hierarchical memory content.',
|
|
|
|
|
action: showMemoryAction,
|
|
|
|
|
},
|
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-14 12:37:17 -07:00
|
|
|
action: (_value: PartListUnion | string) => {
|
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,
|
|
|
|
|
performMemoryRefresh, // Add to dependencies
|
|
|
|
|
showMemoryAction,
|
|
|
|
|
],
|
2025-05-06 14:48:49 -07:00
|
|
|
);
|
2025-04-29 13:29:57 -07:00
|
|
|
|
|
|
|
|
const handleSlashCommand = useCallback(
|
|
|
|
|
(rawQuery: PartListUnion): boolean => {
|
|
|
|
|
if (typeof rawQuery !== 'string') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 00:26:07 +00:00
|
|
|
const trimmed = rawQuery.trim();
|
|
|
|
|
const [symbol, test] = getCommandFromQuery(trimmed);
|
2025-04-29 13:29:57 -07:00
|
|
|
|
2025-05-05 21:16:13 +00:00
|
|
|
if (symbol !== '/' && symbol !== '?') {
|
2025-04-30 00:26:07 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2025-04-29 13:29:57 -07:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
const userMessageTimestamp = Date.now();
|
2025-05-14 12:37:17 -07:00
|
|
|
// Add user message to history only if it's not a silent command or handled internally
|
|
|
|
|
// For now, adding all slash commands to history for transparency.
|
|
|
|
|
addItem({ type: MessageType.USER, text: trimmed }, userMessageTimestamp);
|
2025-05-06 16:20:28 -07:00
|
|
|
|
2025-04-29 13:29:57 -07:00
|
|
|
for (const cmd of slashCommands) {
|
2025-05-05 21:16:13 +00:00
|
|
|
if (
|
|
|
|
|
test === cmd.name ||
|
|
|
|
|
test === cmd.altName ||
|
2025-05-14 12:37:17 -07:00
|
|
|
(symbol === '?' && cmd.altName === '?') // Special handling for ? as help
|
2025-05-05 21:16:13 +00:00
|
|
|
) {
|
2025-05-14 12:37:17 -07:00
|
|
|
cmd.action(trimmed); // Pass the full trimmed command for context if needed
|
2025-05-06 16:20:28 -07:00
|
|
|
return true;
|
2025-04-29 13:29:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem(
|
2025-05-14 12:37:17 -07:00
|
|
|
{ type: MessageType.ERROR, text: `Unknown command: ${trimmed}` },
|
|
|
|
|
userMessageTimestamp,
|
2025-05-06 16:20:28 -07:00
|
|
|
);
|
|
|
|
|
|
2025-05-14 12:37:17 -07:00
|
|
|
return true;
|
2025-04-29 13:29:57 -07:00
|
|
|
},
|
2025-05-06 16:20:28 -07:00
|
|
|
[addItem, slashCommands],
|
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
|
|
|
};
|