From c911265dbdf98340326bc22a732ced06a3f311b3 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 5 Jun 2024 21:20:55 +0300 Subject: [PATCH] Add name argument to /flushinjects command --- public/scripts/slash-commands.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 5baa11435..c672b5e8c 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -1129,9 +1129,15 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({ helpString: 'Lists all script injections for the current chat.', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ - name: 'flushinjects', + name: 'flushinject', + aliases: ['flushinjects'], + unnamedArgumentList: [ + new SlashCommandArgument( + 'injection ID or a variable name pointing to ID', [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.VARIABLE_NAME], false, false, '', + ), + ], callback: flushInjectsCallback, - helpString: 'Removes all script injections for the current chat.', + helpString: 'Removes a script injection for the current chat. If no ID is provided, removes all script injections.', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'tokens', @@ -1231,7 +1237,7 @@ function injectCallback(args, value) { } function listInjectsCallback() { - if (!chat_metadata.script_injects) { + if (!chat_metadata.script_injects || !Object.keys(chat_metadata.script_injects).length) { toastr.info('No script injections for the current chat'); return ''; } @@ -1251,17 +1257,29 @@ function listInjectsCallback() { sendSystemMessage(system_message_types.GENERIC, htmlMessage); } -function flushInjectsCallback() { +/** + * Flushes script injections for the current chat. + * @param {import('./slash-commands/SlashCommand.js').NamedArguments} args Named arguments + * @param {string} value Unnamed argument + * @returns {string} Empty string + */ +function flushInjectsCallback(args, value) { if (!chat_metadata.script_injects) { return ''; } + const idArgument = resolveVariable(value, args._scope); + for (const [id, inject] of Object.entries(chat_metadata.script_injects)) { + if (idArgument && id !== idArgument) { + continue; + } + const prefixedId = `${SCRIPT_PROMPT_KEY}${id}`; setExtensionPrompt(prefixedId, '', inject.position, inject.depth, inject.scan, inject.role); + delete chat_metadata.script_injects[id]; } - chat_metadata.script_injects = {}; saveMetadataDebounced(); return ''; }