diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 0c19dab16..e35be81f8 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -84,7 +84,7 @@ class SlashCommandParser { const key = match[1]; const value = match[2]; // Remove the quotes around the value, if any - argObj[key] = value.replace(/(^")|("$)/g, ''); + argObj[key] = substituteParams(value.replace(/(^")|("$)/g, '')); } // Match unnamed argument @@ -157,11 +157,36 @@ parser.addCommand('memberup', moveGroupMemberUpCallback, ['upmember'], '(member index or name) – moves a group member down in the group chat list', true, true); parser.addCommand('peek', peekCallback, [], '(message index or range) – shows a group member character card without switching chats', true, true); parser.addCommand('delswipe', deleteSwipeCallback, [], '(optional 1-based id) – deletes a swipe from the last chat message. If swipe id not provided - deletes the current swipe.', true, true); +parser.addCommand('echo', echoCallback, [], '(text) – echoes the text to toast message. Useful for pipes debugging.', true, true); +parser.addCommand('gen', generateCallback, [], '(prompt) – generates text using the provided prompt and passes it to the next command through the pipe.', true, true); const NARRATOR_NAME_KEY = 'narrator_name'; const NARRATOR_NAME_DEFAULT = 'System'; export const COMMENT_NAME_DEFAULT = 'Note'; +async function generateCallback(_, arg) { + if (!arg) { + console.warn('WARN: No argument provided for /gen command'); + return; + } + + // Prevent generate recursion + $('#send_textarea').val(''); + + const result = await generateQuietPrompt(arg, false, false, ''); + return result; +} + +async function echoCallback(_, arg) { + if (!arg) { + console.warn('WARN: No argument provided for /echo command'); + return; + } + + toastr.info(arg); + return arg; +} + async function deleteSwipeCallback(_, arg) { const lastMessage = chat[chat.length - 1]; @@ -176,6 +201,12 @@ async function deleteSwipeCallback(_, arg) { } const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : lastMessage.swipe_id; + + if (swipeId < 0 || swipeId >= lastMessage.swipes.length) { + toastr.warning(`Invalid swipe ID: ${swipeId + 1}`); + return; + } + lastMessage.swipes.splice(swipeId, 1); if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) { @@ -889,6 +920,7 @@ async function executeSlashCommands(text) { const linesToRemove = []; let interrupt = false; + let pipeResult = ''; for (let index = 0; index < lines.length; index++) { const trimmedLine = lines[index].trim(); @@ -908,7 +940,8 @@ async function executeSlashCommands(text) { } console.debug('Slash command executing:', result); - await result.command.callback(result.args, result.value); + const unnamedArg = result.value || pipeResult; + pipeResult = await result.command.callback(result.args, unnamedArg); if (result.command.interruptsGeneration) { interrupt = true;