mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Regex: Add slash command input hook
Slash command output for sys and sendas commands were being formatted, but add the ability for user placement to also apply to slash command invocations. Some slash commands will require an output hook, so add exclusions inside the code itself. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
@@ -1146,15 +1146,17 @@ function messageFormatting(mes, ch_name, isSystem, isUser) {
|
|||||||
let regexPlacement;
|
let regexPlacement;
|
||||||
if (isUser) {
|
if (isUser) {
|
||||||
regexPlacement = regex_placement.USER_INPUT;
|
regexPlacement = regex_placement.USER_INPUT;
|
||||||
} else if (ch_name === "System") {
|
|
||||||
regexPlacement = regex_placement.SYSTEM;
|
|
||||||
} else if (ch_name !== name2) {
|
} else if (ch_name !== name2) {
|
||||||
regexPlacement = regex_placement.SENDAS;
|
regexPlacement = regex_placement.SLASH_COMMAND;
|
||||||
} else {
|
} else {
|
||||||
regexPlacement = regex_placement.AI_OUTPUT;
|
regexPlacement = regex_placement.AI_OUTPUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
mes = getRegexedString(mes, regexPlacement, { isMarkdown: true });
|
// Always override the character name
|
||||||
|
mes = getRegexedString(mes, regexPlacement, {
|
||||||
|
characterOverride: ch_name,
|
||||||
|
isMarkdown: true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (power_user.auto_fix_generated_markdown) {
|
if (power_user.auto_fix_generated_markdown) {
|
||||||
@@ -5047,25 +5049,24 @@ function updateMessage(div) {
|
|||||||
const mesBlock = div.closest(".mes_block");
|
const mesBlock = div.closest(".mes_block");
|
||||||
let text = mesBlock.find(".edit_textarea").val();
|
let text = mesBlock.find(".edit_textarea").val();
|
||||||
const mes = chat[this_edit_mes_id];
|
const mes = chat[this_edit_mes_id];
|
||||||
|
|
||||||
let regexPlacement;
|
let regexPlacement;
|
||||||
if (mes.is_name && !mes.is_user && mes.name !== name2) {
|
if (mes.is_name && mes.is_user) {
|
||||||
regexPlacement = regex_placement.SENDAS;
|
|
||||||
} else if (mes.is_name && !mes.is_user) {
|
|
||||||
regexPlacement = regex_placement.AI_OUTPUT;
|
|
||||||
} else if (mes.is_name && mes.is_user) {
|
|
||||||
regexPlacement = regex_placement.USER_INPUT;
|
regexPlacement = regex_placement.USER_INPUT;
|
||||||
} else if (mes.extra?.type === "narrator") {
|
} else if (mes.is_name && mes.name === name2) {
|
||||||
regexPlacement = regex_placement.SYSTEM;
|
regexPlacement = regex_placement.AI_OUTPUT;
|
||||||
|
} else if (mes.is_name && mes.name !== name2 || mes.extra?.type === "narrator") {
|
||||||
|
regexPlacement = regex_placement.SLASH_COMMAND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ignore character override if sent as system
|
||||||
text = getRegexedString(
|
text = getRegexedString(
|
||||||
text,
|
text,
|
||||||
regexPlacement,
|
regexPlacement,
|
||||||
{
|
{ characterOverride: mes.extra?.type === "narrator" ? undefined : mes.name }
|
||||||
characterOverride: regexPlacement === regex_placement.SENDAS ? mes.name : undefined
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
if (power_user.trim_spaces) {
|
if (power_user.trim_spaces) {
|
||||||
text = text.trim();
|
text = text.trim();
|
||||||
}
|
}
|
||||||
|
@@ -72,13 +72,7 @@
|
|||||||
<div>
|
<div>
|
||||||
<label class="checkbox flex-container">
|
<label class="checkbox flex-container">
|
||||||
<input type="checkbox" name="replace_position" value="3">
|
<input type="checkbox" name="replace_position" value="3">
|
||||||
<span data-i18n="Author's Note">/sys Command</span>
|
<span data-i18n="Slash Commands">Slash Commands</span>
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label class="checkbox flex-container">
|
|
||||||
<input type="checkbox" name="replace_position" value="4">
|
|
||||||
<span data-i18n="Author's Note">/sendas Command</span>
|
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -11,8 +11,7 @@ const regex_placement = {
|
|||||||
MD_DISPLAY: 0,
|
MD_DISPLAY: 0,
|
||||||
USER_INPUT: 1,
|
USER_INPUT: 1,
|
||||||
AI_OUTPUT: 2,
|
AI_OUTPUT: 2,
|
||||||
SYSTEM: 3,
|
SLASH_COMMAND: 3
|
||||||
SENDAS: 4
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const regex_replace_strategy = {
|
const regex_replace_strategy = {
|
||||||
|
@@ -201,6 +201,16 @@ function migrateSettings() {
|
|||||||
|
|
||||||
performSave = true;
|
performSave = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Old system and sendas placement migration
|
||||||
|
// 4 - sendAs
|
||||||
|
if (script.placement.includes(4)) {
|
||||||
|
script.placement = script.placement.length === 1 ?
|
||||||
|
[regex_placement.SLASH_COMMAND] :
|
||||||
|
script.placement = script.placement.filter((e) => e !== 4);
|
||||||
|
|
||||||
|
performSave = true;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (performSave) {
|
if (performSave) {
|
||||||
|
@@ -61,6 +61,7 @@ class SlashCommandParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parse(text) {
|
parse(text) {
|
||||||
|
const excludedFromRegex = ["sendas"]
|
||||||
const firstSpace = text.indexOf(' ');
|
const firstSpace = text.indexOf(' ');
|
||||||
const command = firstSpace !== -1 ? text.substring(1, firstSpace) : text.substring(1);
|
const command = firstSpace !== -1 ? text.substring(1, firstSpace) : text.substring(1);
|
||||||
const args = firstSpace !== -1 ? text.substring(firstSpace + 1) : '';
|
const args = firstSpace !== -1 ? text.substring(firstSpace + 1) : '';
|
||||||
@@ -82,6 +83,14 @@ class SlashCommandParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unnamedArg = argsArray.slice(Object.keys(argObj).length).join(' ');
|
unnamedArg = argsArray.slice(Object.keys(argObj).length).join(' ');
|
||||||
|
|
||||||
|
// Excluded commands format in their own function
|
||||||
|
if (!excludedFromRegex.includes(command)) {
|
||||||
|
unnamedArg = getRegexedString(
|
||||||
|
unnamedArg,
|
||||||
|
regex_placement.SLASH_COMMAND
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.commands[command]) {
|
if (this.commands[command]) {
|
||||||
@@ -177,8 +186,11 @@ async function generateSystemMessage(_, prompt) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate and regex the output if applicable
|
||||||
toastr.info('Please wait', 'Generating...');
|
toastr.info('Please wait', 'Generating...');
|
||||||
const message = await generateQuietPrompt(prompt);
|
let message = await generateQuietPrompt(prompt);
|
||||||
|
message = getRegexedString(message, regex_placement.SLASH_COMMAND);
|
||||||
|
|
||||||
sendNarratorMessage(_, message);
|
sendNarratorMessage(_, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,7 +254,9 @@ async function sendMessageAs(_, text) {
|
|||||||
|
|
||||||
const name = parts.shift().trim();
|
const name = parts.shift().trim();
|
||||||
let mesText = parts.join('\n').trim();
|
let mesText = parts.join('\n').trim();
|
||||||
mesText = getRegexedString(mesText, regex_placement.SENDAS, { characterOverride: name });
|
|
||||||
|
// Requires a regex check after the slash command is pushed to output
|
||||||
|
mesText = getRegexedString(mesText, regex_placement.SLASH_COMMAND, { characterOverride: name });
|
||||||
|
|
||||||
// Messages that do nothing but set bias will be hidden from the context
|
// Messages that do nothing but set bias will be hidden from the context
|
||||||
const bias = extractMessageBias(mesText);
|
const bias = extractMessageBias(mesText);
|
||||||
@@ -286,8 +300,6 @@ async function sendNarratorMessage(_, text) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
text = getRegexedString(text, regex_placement.SYSTEM);
|
|
||||||
|
|
||||||
const name = chat_metadata[NARRATOR_NAME_KEY] || NARRATOR_NAME_DEFAULT;
|
const name = chat_metadata[NARRATOR_NAME_KEY] || NARRATOR_NAME_DEFAULT;
|
||||||
// Messages that do nothing but set bias will be hidden from the context
|
// Messages that do nothing but set bias will be hidden from the context
|
||||||
const bias = extractMessageBias(text);
|
const bias = extractMessageBias(text);
|
||||||
|
Reference in New Issue
Block a user