Chat refactoring
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { useChatSession } from './useChatSession';
|
||||
|
||||
export type SlashCommandResult = { handled: true; feedback: string } | { handled: false };
|
||||
|
||||
type UseSlashCommandsParams = {
|
||||
sessionId: string | null;
|
||||
};
|
||||
|
||||
export function useSlashCommands({ sessionId }: UseSlashCommandsParams) {
|
||||
const { rename } = useChatSession({ sessionId });
|
||||
|
||||
const execute = async (input: string): Promise<SlashCommandResult> => {
|
||||
const trimmed = input.trim();
|
||||
if (!trimmed.startsWith('/')) return { handled: false };
|
||||
if (!sessionId) return { handled: false };
|
||||
|
||||
const spaceIndex = trimmed.indexOf(' ');
|
||||
const commandName = spaceIndex === -1 ? trimmed.slice(1) : trimmed.slice(1, spaceIndex);
|
||||
const args = spaceIndex === -1 ? '' : trimmed.slice(spaceIndex + 1).trim();
|
||||
|
||||
try {
|
||||
switch (commandName) {
|
||||
case 'rename': {
|
||||
if (!args.trim()) {
|
||||
return { handled: true, feedback: 'Usage: /rename <new name>' };
|
||||
}
|
||||
await rename(args);
|
||||
return { handled: true, feedback: `Session renamed to "${args}"` };
|
||||
}
|
||||
|
||||
case 'help': {
|
||||
const helpText = [
|
||||
'Available commands:',
|
||||
' /rename <name> - Rename the current session',
|
||||
' /help - Show this help message',
|
||||
].join('\n');
|
||||
return { handled: true, feedback: helpText };
|
||||
}
|
||||
|
||||
default:
|
||||
return { handled: false };
|
||||
}
|
||||
} catch (error) {
|
||||
return { handled: true, feedback: `Error: ${error instanceof Error ? error.message : 'Unknown error'}` };
|
||||
}
|
||||
};
|
||||
|
||||
return { execute };
|
||||
}
|
||||
Reference in New Issue
Block a user