Refactor hardcoded slash commands (#179)
This commit is contained in:
parent
6703b37a93
commit
dfa46df474
|
@ -30,6 +30,12 @@ import {
|
||||||
} from '../types.js';
|
} from '../types.js';
|
||||||
import { findSafeSplitPoint } from '../utils/markdownUtilities.js';
|
import { findSafeSplitPoint } from '../utils/markdownUtilities.js';
|
||||||
|
|
||||||
|
interface SlashCommand {
|
||||||
|
name: string; // slash command
|
||||||
|
description: string; // flavor text in UI
|
||||||
|
action: (value: PartListUnion) => void;
|
||||||
|
}
|
||||||
|
|
||||||
const addHistoryItem = (
|
const addHistoryItem = (
|
||||||
setHistory: React.Dispatch<React.SetStateAction<HistoryItem[]>>,
|
setHistory: React.Dispatch<React.SetStateAction<HistoryItem[]>>,
|
||||||
itemData: Omit<HistoryItem, 'id'>,
|
itemData: Omit<HistoryItem, 'id'>,
|
||||||
|
@ -58,6 +64,47 @@ export const useGeminiStream = (
|
||||||
const messageIdCounterRef = useRef(0);
|
const messageIdCounterRef = useRef(0);
|
||||||
const currentGeminiMessageIdRef = useRef<number | null>(null);
|
const currentGeminiMessageIdRef = useRef<number | null>(null);
|
||||||
|
|
||||||
|
const slashCommands: SlashCommand[] = [
|
||||||
|
{
|
||||||
|
name: 'clear',
|
||||||
|
description: 'clear the screen',
|
||||||
|
action: (_value: PartListUnion) => {
|
||||||
|
// This just clears the *UI* history, not the model history.
|
||||||
|
setDebugMessage('Clearing terminal.');
|
||||||
|
setHistory((_) => []);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'exit',
|
||||||
|
description: 'Exit gemini-code',
|
||||||
|
action: (_value: PartListUnion) => {
|
||||||
|
setDebugMessage('Exiting. Good-bye.');
|
||||||
|
const timestamp = getNextMessageId(Date.now());
|
||||||
|
addHistoryItem(
|
||||||
|
setHistory,
|
||||||
|
{ type: 'info', text: 'good-bye!' },
|
||||||
|
timestamp,
|
||||||
|
);
|
||||||
|
process.exit(0);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// TODO: dedup with exit by adding altName or cmdRegex.
|
||||||
|
name: 'quit',
|
||||||
|
description: 'Quit gemini-code',
|
||||||
|
action: (_value: PartListUnion) => {
|
||||||
|
setDebugMessage('Quitting. Good-bye.');
|
||||||
|
const timestamp = getNextMessageId(Date.now());
|
||||||
|
addHistoryItem(
|
||||||
|
setHistory,
|
||||||
|
{ type: 'info', text: 'good-bye!' },
|
||||||
|
timestamp,
|
||||||
|
);
|
||||||
|
process.exit(0);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
// Initialize Client Effect - uses props now
|
// Initialize Client Effect - uses props now
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setInitError(null);
|
setInitError(null);
|
||||||
|
@ -105,31 +152,20 @@ export const useGeminiStream = (
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = rawQuery.trim();
|
const trimmedQuery = rawQuery.trim();
|
||||||
if (query === 'clear' || query === '/clear') {
|
let query = trimmedQuery;
|
||||||
// This just clears the *UI* history, not the model history.
|
if (query.length && query.charAt(0) === '/') {
|
||||||
// TODO: add a slash command for that.
|
query = query.slice(1);
|
||||||
setDebugMessage('Clearing terminal.');
|
}
|
||||||
setHistory((_) => []);
|
|
||||||
|
for (const cmd of slashCommands) {
|
||||||
|
if (query === cmd.name) {
|
||||||
|
cmd.action(query);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
query === 'exit' ||
|
|
||||||
query === '/exit' ||
|
|
||||||
query === 'quit' ||
|
|
||||||
query === '/quit'
|
|
||||||
) {
|
|
||||||
setDebugMessage('Quitting. Good-bye.');
|
|
||||||
const timestamp = getNextMessageId(Date.now());
|
|
||||||
addHistoryItem(
|
|
||||||
setHistory,
|
|
||||||
{ type: 'info', text: 'good-bye!' },
|
|
||||||
timestamp,
|
|
||||||
);
|
|
||||||
process.exit(0);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
const maybeCommand = query.split(/\s+/)[0];
|
|
||||||
|
const maybeCommand = trimmedQuery.split(/\s+/)[0];
|
||||||
if (config.getPassthroughCommands().includes(maybeCommand)) {
|
if (config.getPassthroughCommands().includes(maybeCommand)) {
|
||||||
// Execute and capture output
|
// Execute and capture output
|
||||||
const targetDir = config.getTargetDir();
|
const targetDir = config.getTargetDir();
|
||||||
|
|
Loading…
Reference in New Issue