mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Regex: Add character name override
Sendas uses a different character's name for messages, so allow the use of a character name override in the regex match function. This overrides substituteParams to use a different value. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
@@ -4888,7 +4888,9 @@ function updateMessage(div) {
|
||||
let text = mesBlock.find(".edit_textarea").val();
|
||||
const mes = chat[this_edit_mes_id];
|
||||
let regexPlacement;
|
||||
if (mes.is_name && !mes.is_user) {
|
||||
if (mes.is_name && !mes.is_user && mes.name !== name2) {
|
||||
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;
|
||||
@@ -4896,7 +4898,13 @@ function updateMessage(div) {
|
||||
regexPlacement = regex_placement.SYSTEM;
|
||||
}
|
||||
|
||||
const regexResult = getRegexedString(text, regexPlacement);
|
||||
const regexResult = getRegexedString(
|
||||
text,
|
||||
regexPlacement,
|
||||
{
|
||||
characterOverride: regexPlacement === regex_placement.SENDAS ? mes.name : undefined
|
||||
}
|
||||
);
|
||||
if (regexResult) {
|
||||
text = regexResult;
|
||||
}
|
||||
|
@@ -28,7 +28,7 @@ function regexFromString(input) {
|
||||
return new RegExp(m[2], m[3]);
|
||||
}
|
||||
|
||||
function getRegexedString(rawString, placement) {
|
||||
function getRegexedString(rawString, placement, { characterOverride } = {}) {
|
||||
if (extension_settings.disabledExtensions.includes("regex") || !rawString || placement === undefined) {
|
||||
return;
|
||||
}
|
||||
@@ -36,7 +36,7 @@ function getRegexedString(rawString, placement) {
|
||||
let finalString;
|
||||
extension_settings.regex.forEach((script) => {
|
||||
if (script.placement.includes(placement)) {
|
||||
finalString = runRegexScript(script, rawString);
|
||||
finalString = runRegexScript(script, rawString, { characterOverride });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -44,14 +44,13 @@ function getRegexedString(rawString, placement) {
|
||||
}
|
||||
|
||||
// Runs the provided regex script on the given string
|
||||
function runRegexScript(regexScript, rawString) {
|
||||
function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
|
||||
if (!regexScript || !!(regexScript.disabled) || !regexScript?.findRegex || !rawString) {
|
||||
return;
|
||||
}
|
||||
|
||||
let match;
|
||||
let newString;
|
||||
console.log(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex)
|
||||
const findRegex = regexFromString(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex);
|
||||
while ((match = findRegex.exec(rawString)) !== null) {
|
||||
const fencedMatch = match[0];
|
||||
@@ -60,16 +59,16 @@ function runRegexScript(regexScript, rawString) {
|
||||
let trimCapturedMatch;
|
||||
let trimFencedMatch;
|
||||
if (capturedMatch) {
|
||||
const tempTrimCapture = filterString(capturedMatch, regexScript.trimStrings);
|
||||
const tempTrimCapture = filterString(capturedMatch, regexScript.trimStrings, { characterOverride });
|
||||
trimFencedMatch = fencedMatch.replaceAll(capturedMatch, tempTrimCapture);
|
||||
trimCapturedMatch = tempTrimCapture;
|
||||
} else {
|
||||
trimFencedMatch = filterString(fencedMatch, regexScript.trimStrings);
|
||||
trimFencedMatch = filterString(fencedMatch, regexScript.trimStrings, { characterOverride });
|
||||
}
|
||||
|
||||
// TODO: Use substrings for replacement. But not necessary at this time.
|
||||
// A substring is from match.index to match.index + match[0].length or fencedMatch.length
|
||||
const subReplaceString = substituteRegexParams(regexScript.replaceString, trimCapturedMatch ?? trimFencedMatch);
|
||||
const subReplaceString = substituteRegexParams(regexScript.replaceString, trimCapturedMatch ?? trimFencedMatch, { characterOverride });
|
||||
if (!newString) {
|
||||
newString = rawString.replace(fencedMatch, subReplaceString);
|
||||
} else {
|
||||
@@ -86,10 +85,10 @@ function runRegexScript(regexScript, rawString) {
|
||||
}
|
||||
|
||||
// Filters anything to trim from the regex match
|
||||
function filterString(rawString, trimStrings) {
|
||||
function filterString(rawString, trimStrings, { characterOverride } = {}) {
|
||||
let finalString = rawString;
|
||||
trimStrings.forEach((trimString) => {
|
||||
const subTrimString = substituteParams(trimString);
|
||||
const subTrimString = substituteParams(trimString, undefined, characterOverride);
|
||||
finalString = finalString.replaceAll(subTrimString, "");
|
||||
});
|
||||
|
||||
@@ -97,10 +96,10 @@ function filterString(rawString, trimStrings) {
|
||||
}
|
||||
|
||||
// Substitutes regex-specific and normal parameters
|
||||
function substituteRegexParams(rawString, regexMatch) {
|
||||
function substituteRegexParams(rawString, regexMatch, { characterOverride } = {}) {
|
||||
let finalString = rawString;
|
||||
finalString = finalString.replace("{{match}}", regexMatch);
|
||||
finalString = substituteParams(finalString);
|
||||
finalString = substituteParams(finalString, undefined, characterOverride);
|
||||
|
||||
return finalString;
|
||||
}
|
||||
|
@@ -22,7 +22,6 @@ import {
|
||||
} from "../script.js";
|
||||
import { humanizedDateTime } from "./RossAscends-mods.js";
|
||||
import { resetSelectedGroup } from "./group-chats.js";
|
||||
import { extension_settings } from "./extensions.js";
|
||||
import { getRegexedString, regex_placement } from "./extensions/regex/engine.js";
|
||||
import { chat_styles, power_user } from "./power-user.js";
|
||||
export {
|
||||
@@ -227,7 +226,7 @@ async function sendMessageAs(_, text) {
|
||||
|
||||
const name = parts.shift().trim();
|
||||
let mesText = parts.join('\n').trim();
|
||||
const regexResult = getRegexedString(mesText, regex_placement.SENDAS);
|
||||
const regexResult = getRegexedString(mesText, regex_placement.SENDAS, { characterOverride: name });
|
||||
if (regexResult) {
|
||||
mesText = regexResult;
|
||||
}
|
||||
|
Reference in New Issue
Block a user