add /replace command

371208c24d0964ecdeea26f0b4a60ea7c07ceeb7

Succubyss <87207237+Succubyss@users.noreply.github.com>

1 files changed, +60 -4Showing whitespace changes
public/scripts/slash-commands.js+60 -4
@@ -58,7 +58,7 @@ import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, setPersonaLockStat
5858import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js';
5959import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js';
6060import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js';
6161import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, regexFromString, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';
6262import { registerVariableCommands, resolveVariable } from './variables.js';
6363import { background_settings } from './backgrounds.js';
6464import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
@@ -1902,7 +1902,7 @@ export function initDefaultSlashCommands() {
19021902 returns: 'uppercase string',
19031903 unnamedArgumentList: [
19041904 new SlashCommandArgument(
19051905 'stringtext to affect', [ARGUMENT_TYPE.STRING], true, false,
19061906 ),
19071907 ],
19081908 helpString: 'Converts the provided string to uppercase.',
@@ -1914,7 +1914,7 @@ export function initDefaultSlashCommands() {
19141914 returns: 'lowercase string',
19151915 unnamedArgumentList: [
19161916 new SlashCommandArgument(
19171917 'stringtext to affect', [ARGUMENT_TYPE.STRING], true, false,
19181918 ),
19191919 ],
19201920 helpString: 'Converts the provided string to lowercase.',
@@ -1934,7 +1934,7 @@ export function initDefaultSlashCommands() {
19341934 ],
19351935 unnamedArgumentList: [
19361936 new SlashCommandArgument(
19371937 'stringtext to affect', [ARGUMENT_TYPE.STRING], true, false,
19381938 ),
19391939 ],
19401940 helpString: `
@@ -1998,6 +1998,62 @@ export function initDefaultSlashCommands() {
19981998 return '';
19991999 },
20002000 }));
2001+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2002+ name: 'replace',
2003+ aliases: ['re'],
2004+ callback: (async ({mode = 'literal', pattern, replacer = ''}, text) => {
2005+ if (pattern === '')
2006+ throw new Error("Argument of 'pattern=' cannot be empty");
2007+ switch (mode) {
2008+ case 'literal':
2009+ return text.replaceAll(pattern, replacer);
2010+ case 'regex':
2011+ return text.replace(regexFromString(pattern), replacer);
2012+ default:
2013+ throw new Error("Invalid '/replace mode=' argument specified!");
2014+ }
2015+ }),
2016+ returns: 'replaced text',
2017+ namedArgumentList: [
2018+ SlashCommandNamedArgument.fromProps({
2019+ name: 'mode',
2020+ description: 'Replaces occurrence(s) of a pattern',
2021+ typeList: [ARGUMENT_TYPE.STRING],
2022+ defaultValue: 'literal',
2023+ enumList: ['literal', 'regex'],
2024+ }),
2025+ new SlashCommandNamedArgument(
2026+ 'pattern', 'pattern to search with', [ARGUMENT_TYPE.STRING], true, false,
2027+ ),
2028+ new SlashCommandNamedArgument(
2029+ 'replacer', 'replacement text for matches', [ARGUMENT_TYPE.STRING], false, false, '',
2030+ ),
2031+ ],
2032+ unnamedArgumentList: [
2033+ new SlashCommandArgument(
2034+ 'text to affect', [ARGUMENT_TYPE.STRING], true, false,
2035+ ),
2036+ ],
2037+ helpString: `
2038+ <div>
2039+ Replaces text within the provided string based on the pattern.
2040+ </div>
2041+ <div>
2042+ If <code>mode</code> is <code>literal</code> (or omitted), <code>pattern</code> is a literal search string (case-sensitive).<br />
2043+ If <code>mode</code> is <code>regex</code>, <code>pattern</code> is parsed as an ECMAScript Regular Expression.<br />
2044+ The <code>replacer</code> replaces based on the <code>pattern</code> in the input text.<br />
2045+ If <code>replacer</code> is omitted, the replacement(s) will be an empty string.<br />
2046+ </div>
2047+ <div>
2048+ <strong>Example:</strong>
2049+ <pre>/let x Blue house and blue car || </pre>
2050+ <pre>/replace pattern="blue" {{var::x}} | /echo |/# Blue house and car ||</pre>
2051+ <pre>/replace pattern="blue" replacer="red" {{var::x}} | /echo |/# Blue house and red car ||</pre>
2052+ <pre>/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}} | /echo |/# red house and blue car ||</pre>
2053+ <pre>/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}} | /echo |/# red house and red car ||</pre>
2054+ </div>
2055+ `,
2056+ }));
20012057
20022058 registerVariableCommands();
20032059}