mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-08 21:44:59 +08:00
feat: close #628 add chat commands
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { useSearchParams } from "react-router-dom";
|
||||
import Locale from "./locales";
|
||||
|
||||
type Command = (param: string) => void;
|
||||
interface Commands {
|
||||
@@ -26,3 +27,45 @@ export function useCommand(commands: Commands = {}) {
|
||||
setSearchParams(searchParams);
|
||||
}
|
||||
}
|
||||
|
||||
interface ChatCommands {
|
||||
new?: Command;
|
||||
newm?: Command;
|
||||
next?: Command;
|
||||
prev?: Command;
|
||||
clear?: Command;
|
||||
del?: Command;
|
||||
}
|
||||
|
||||
export const ChatCommandPrefix = ":";
|
||||
|
||||
export function useChatCommand(commands: ChatCommands = {}) {
|
||||
function extract(userInput: string) {
|
||||
return (
|
||||
userInput.startsWith(ChatCommandPrefix) ? userInput.slice(1) : userInput
|
||||
) as keyof ChatCommands;
|
||||
}
|
||||
|
||||
function search(userInput: string) {
|
||||
const input = extract(userInput);
|
||||
const desc = Locale.Chat.Commands;
|
||||
return Object.keys(commands)
|
||||
.filter((c) => c.startsWith(input))
|
||||
.map((c) => ({
|
||||
title: desc[c as keyof ChatCommands],
|
||||
content: ChatCommandPrefix + c,
|
||||
}));
|
||||
}
|
||||
|
||||
function match(userInput: string) {
|
||||
const command = extract(userInput);
|
||||
const matched = typeof commands[command] === "function";
|
||||
|
||||
return {
|
||||
matched,
|
||||
invoke: () => matched && commands[command]!(userInput),
|
||||
};
|
||||
}
|
||||
|
||||
return { match, search };
|
||||
}
|
||||
|
Reference in New Issue
Block a user