diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 39b3db71e..cc1ac3aea 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -58,7 +58,7 @@ import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, setPersonaLockStat import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js'; import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js'; import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js'; -import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js'; +import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, regexFromString, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js'; import { registerVariableCommands, resolveVariable } from './variables.js'; import { background_settings } from './backgrounds.js'; import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; @@ -1902,7 +1902,7 @@ export function initDefaultSlashCommands() { returns: 'uppercase string', unnamedArgumentList: [ new SlashCommandArgument( - 'string', [ARGUMENT_TYPE.STRING], true, false, + 'text to affect', [ARGUMENT_TYPE.STRING], true, false, ), ], helpString: 'Converts the provided string to uppercase.', @@ -1914,7 +1914,7 @@ export function initDefaultSlashCommands() { returns: 'lowercase string', unnamedArgumentList: [ new SlashCommandArgument( - 'string', [ARGUMENT_TYPE.STRING], true, false, + 'text to affect', [ARGUMENT_TYPE.STRING], true, false, ), ], helpString: 'Converts the provided string to lowercase.', @@ -1934,7 +1934,7 @@ export function initDefaultSlashCommands() { ], unnamedArgumentList: [ new SlashCommandArgument( - 'string', [ARGUMENT_TYPE.STRING], true, false, + 'text to affect', [ARGUMENT_TYPE.STRING], true, false, ), ], helpString: ` @@ -1998,6 +1998,62 @@ export function initDefaultSlashCommands() { return ''; }, })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'replace', + aliases: ['re'], + callback: (async ({mode = 'literal', pattern, replacer = ''}, text) => { + if (pattern === '') + throw new Error("Argument of 'pattern=' cannot be empty"); + switch (mode) { + case 'literal': + return text.replaceAll(pattern, replacer); + case 'regex': + return text.replace(regexFromString(pattern), replacer); + default: + throw new Error("Invalid '/replace mode=' argument specified!"); + } + }), + returns: 'replaced text', + namedArgumentList: [ + SlashCommandNamedArgument.fromProps({ + name: 'mode', + description: 'Replaces occurrence(s) of a pattern', + typeList: [ARGUMENT_TYPE.STRING], + defaultValue: 'literal', + enumList: ['literal', 'regex'], + }), + new SlashCommandNamedArgument( + 'pattern', 'pattern to search with', [ARGUMENT_TYPE.STRING], true, false, + ), + new SlashCommandNamedArgument( + 'replacer', 'replacement text for matches', [ARGUMENT_TYPE.STRING], false, false, '', + ), + ], + unnamedArgumentList: [ + new SlashCommandArgument( + 'text to affect', [ARGUMENT_TYPE.STRING], true, false, + ), + ], + helpString: ` +
+ Replaces text within the provided string based on the pattern. +
+
+ If mode is literal (or omitted), pattern is a literal search string (case-sensitive).
+ If mode is regex, pattern is parsed as an ECMAScript Regular Expression.
+ The replacer replaces based on the pattern in the input text.
+ If replacer is omitted, the replacement(s) will be an empty string.
+
+
+ Example: +
/let x Blue house and blue car ||                                                                        
+
/replace pattern="blue" {{var::x}}                                | /echo  |/# Blue house and  car     ||
+
/replace pattern="blue" replacer="red" {{var::x}}                 | /echo  |/# Blue house and red car  ||
+
/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}}   | /echo  |/# red house and blue car  ||
+
/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}}  | /echo  |/# red house and red car   ||
+
+ `, + })); registerVariableCommands(); }