mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
#1630 Add min/max depth for prompt/display regex scripts.
This commit is contained in:
@ -1563,7 +1563,16 @@ export function sendTextareaMessage() {
|
|||||||
Generate(generateType);
|
Generate(generateType);
|
||||||
}
|
}
|
||||||
|
|
||||||
function messageFormatting(mes, ch_name, isSystem, isUser) {
|
/**
|
||||||
|
* Formats the message text into an HTML string using Markdown and other formatting.
|
||||||
|
* @param {string} mes Message text
|
||||||
|
* @param {string} ch_name Character name
|
||||||
|
* @param {boolean} isSystem If the message was sent by the system
|
||||||
|
* @param {boolean} isUser If the message was sent by the user
|
||||||
|
* @param {number} messageId Message index in chat array
|
||||||
|
* @returns {string} HTML string
|
||||||
|
*/
|
||||||
|
function messageFormatting(mes, ch_name, isSystem, isUser, messageId) {
|
||||||
if (!mes) {
|
if (!mes) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@ -1595,10 +1604,14 @@ function messageFormatting(mes, ch_name, isSystem, isUser) {
|
|||||||
regexPlacement = regex_placement.AI_OUTPUT;
|
regexPlacement = regex_placement.AI_OUTPUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const usableMessages = chat.map((x, index) => ({ message: x, index: index })).filter(x => !x.message.is_system);
|
||||||
|
const depth = messageId >= 0 && usableMessages.some(x => x.index === messageId) ? (usableMessages.length - messageId - 1) : undefined;
|
||||||
|
|
||||||
// Always override the character name
|
// Always override the character name
|
||||||
mes = getRegexedString(mes, regexPlacement, {
|
mes = getRegexedString(mes, regexPlacement, {
|
||||||
characterOverride: ch_name,
|
characterOverride: ch_name,
|
||||||
isMarkdown: true,
|
isMarkdown: true,
|
||||||
|
depth: depth,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1775,7 +1788,7 @@ function getMessageFromTemplate({
|
|||||||
export function updateMessageBlock(messageId, message) {
|
export function updateMessageBlock(messageId, message) {
|
||||||
const messageElement = $(`#chat [mesid="${messageId}"]`);
|
const messageElement = $(`#chat [mesid="${messageId}"]`);
|
||||||
const text = message?.extra?.display_text ?? message.mes;
|
const text = message?.extra?.display_text ?? message.mes;
|
||||||
messageElement.find('.mes_text').html(messageFormatting(text, message.name, message.is_system, message.is_user));
|
messageElement.find('.mes_text').html(messageFormatting(text, message.name, message.is_system, message.is_user, messageId));
|
||||||
addCopyToCodeBlocks(messageElement);
|
addCopyToCodeBlocks(messageElement);
|
||||||
appendMediaToMessage(message, messageElement);
|
appendMediaToMessage(message, messageElement);
|
||||||
}
|
}
|
||||||
@ -1891,8 +1904,9 @@ function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll = true
|
|||||||
mes.name,
|
mes.name,
|
||||||
isSystem,
|
isSystem,
|
||||||
mes.is_user,
|
mes.is_user,
|
||||||
|
chat.indexOf(mes),
|
||||||
);
|
);
|
||||||
const bias = messageFormatting(mes.extra?.bias ?? '');
|
const bias = messageFormatting(mes.extra?.bias ?? '', '', false, false, -1);
|
||||||
let bookmarkLink = mes?.extra?.bookmark_link ?? '';
|
let bookmarkLink = mes?.extra?.bookmark_link ?? '';
|
||||||
// Verify bookmarked chat still exists
|
// Verify bookmarked chat still exists
|
||||||
// Cohee: Commented out for now. I'm worried of performance issues.
|
// Cohee: Commented out for now. I'm worried of performance issues.
|
||||||
@ -2545,6 +2559,7 @@ class StreamingProcessor {
|
|||||||
chat[messageId].name,
|
chat[messageId].name,
|
||||||
chat[messageId].is_system,
|
chat[messageId].is_system,
|
||||||
chat[messageId].is_user,
|
chat[messageId].is_user,
|
||||||
|
messageId,
|
||||||
);
|
);
|
||||||
const mesText = $(`#chat .mes[mesid="${messageId}"] .mes_text`);
|
const mesText = $(`#chat .mes[mesid="${messageId}"] .mes_text`);
|
||||||
mesText.html(formattedText);
|
mesText.html(formattedText);
|
||||||
@ -3015,7 +3030,7 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
|
|||||||
coreChat = await Promise.all(coreChat.map(async (chatItem, index) => {
|
coreChat = await Promise.all(coreChat.map(async (chatItem, index) => {
|
||||||
let message = chatItem.mes;
|
let message = chatItem.mes;
|
||||||
let regexType = chatItem.is_user ? regex_placement.USER_INPUT : regex_placement.AI_OUTPUT;
|
let regexType = chatItem.is_user ? regex_placement.USER_INPUT : regex_placement.AI_OUTPUT;
|
||||||
let options = { isPrompt: true };
|
let options = { isPrompt: true, depth: (coreChat.length - index - 1) };
|
||||||
|
|
||||||
let regexedMessage = getRegexedString(message, regexType, options);
|
let regexedMessage = getRegexedString(message, regexType, options);
|
||||||
regexedMessage = await appendFileContent(chatItem, regexedMessage);
|
regexedMessage = await appendFileContent(chatItem, regexedMessage);
|
||||||
@ -5803,6 +5818,7 @@ function messageEditAuto(div) {
|
|||||||
this_edit_mes_chname,
|
this_edit_mes_chname,
|
||||||
mes.is_system,
|
mes.is_system,
|
||||||
mes.is_user,
|
mes.is_user,
|
||||||
|
this_edit_mes_id,
|
||||||
));
|
));
|
||||||
saveChatDebounced();
|
saveChatDebounced();
|
||||||
}
|
}
|
||||||
@ -5822,10 +5838,11 @@ async function messageEditDone(div) {
|
|||||||
this_edit_mes_chname,
|
this_edit_mes_chname,
|
||||||
mes.is_system,
|
mes.is_system,
|
||||||
mes.is_user,
|
mes.is_user,
|
||||||
|
this_edit_mes_id,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
mesBlock.find('.mes_bias').empty();
|
mesBlock.find('.mes_bias').empty();
|
||||||
mesBlock.find('.mes_bias').append(messageFormatting(bias));
|
mesBlock.find('.mes_bias').append(messageFormatting(bias, '', false, false, -1));
|
||||||
appendMediaToMessage(mes, div.closest('.mes'));
|
appendMediaToMessage(mes, div.closest('.mes'));
|
||||||
addCopyToCodeBlocks(div.closest('.mes'));
|
addCopyToCodeBlocks(div.closest('.mes'));
|
||||||
await eventSource.emit(event_types.MESSAGE_EDITED, this_edit_mes_id);
|
await eventSource.emit(event_types.MESSAGE_EDITED, this_edit_mes_id);
|
||||||
@ -8920,6 +8937,7 @@ jQuery(async function () {
|
|||||||
this_edit_mes_chname,
|
this_edit_mes_chname,
|
||||||
chat[this_edit_mes_id].is_system,
|
chat[this_edit_mes_id].is_system,
|
||||||
chat[this_edit_mes_id].is_user,
|
chat[this_edit_mes_id].is_user,
|
||||||
|
this_edit_mes_id,
|
||||||
));
|
));
|
||||||
appendMediaToMessage(chat[this_edit_mes_id], $(this).closest('.mes'));
|
appendMediaToMessage(chat[this_edit_mes_id], $(this).closest('.mes'));
|
||||||
addCopyToCodeBlocks($(this).closest('.mes'));
|
addCopyToCodeBlocks($(this).closest('.mes'));
|
||||||
|
@ -96,6 +96,22 @@
|
|||||||
<span data-i18n="Slash Commands">Slash Commands</span>
|
<span data-i18n="Slash Commands">Slash Commands</span>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="flex-container wide100p marginTop5">
|
||||||
|
<div class="flex1 flex-container flexNoGap">
|
||||||
|
<small title="When applied to prompts or display, only affect messages that are at least N levels deep. 0 = last message, 1 = penultimate message, etc. Only counts usable messages, i.e. not hidden or system.">
|
||||||
|
<span data-i18n="Min Depth">Min Depth</span>
|
||||||
|
<span class="fa-solid fa-circle-question note-link-span"></span>
|
||||||
|
</small>
|
||||||
|
<input name="min_depth" class="text_pole textarea_compact" type="number" min="0" max="999" placeholder="Unlimited" />
|
||||||
|
</div>
|
||||||
|
<div class="flex1 flex-container flexNoGap">
|
||||||
|
<small title="When applied to prompts or display, only affect messages no more than N levels deep. 0 = last message, 1 = penultimate message, etc. Only counts usable messages, i.e. not hidden or system.">
|
||||||
|
<span data-i18n="Max Depth">Max Depth</span>
|
||||||
|
<span class="fa-solid fa-circle-question note-link-span"></span>
|
||||||
|
</small>
|
||||||
|
<input name="max_depth" class="text_pole textarea_compact" type="number" min="0" max="999" placeholder="Unlimited" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex1 wi-enter-footer-text flex-container flexFlowColumn flexNoGap alignitemsstart">
|
<div class="flex1 wi-enter-footer-text flex-container flexFlowColumn flexNoGap alignitemsstart">
|
||||||
<small>Other Options</small>
|
<small>Other Options</small>
|
||||||
@ -109,7 +125,10 @@
|
|||||||
</label>
|
</label>
|
||||||
<label class="checkbox flex-container" title="Chat history won't change, only the prompt as the request is sent (on generation)">
|
<label class="checkbox flex-container" title="Chat history won't change, only the prompt as the request is sent (on generation)">
|
||||||
<input type="checkbox" name="only_format_prompt"/>
|
<input type="checkbox" name="only_format_prompt"/>
|
||||||
<span data-i18n="Only Format Prompt (?)">Only Format Prompt (?)</span>
|
<span>
|
||||||
|
<span data-i18n="Only Format Prompt (?)">Only Format Prompt</span>
|
||||||
|
<span class="fa-solid fa-circle-question note-link-span"></span>
|
||||||
|
</span>
|
||||||
</label>
|
</label>
|
||||||
<label class="checkbox flex-container">
|
<label class="checkbox flex-container">
|
||||||
<input type="checkbox" name="run_on_edit" />
|
<input type="checkbox" name="run_on_edit" />
|
||||||
@ -117,7 +136,10 @@
|
|||||||
</label>
|
</label>
|
||||||
<label class="checkbox flex-container" title="Substitute {{macros}} in Find Regex before running it">
|
<label class="checkbox flex-container" title="Substitute {{macros}} in Find Regex before running it">
|
||||||
<input type="checkbox" name="substitute_regex" />
|
<input type="checkbox" name="substitute_regex" />
|
||||||
<span data-i18n="Substitute Regex">Substitute Regex (?)</span>
|
<span>
|
||||||
|
<span data-i18n="Substitute Regex">Substitute Regex</span>
|
||||||
|
<span class="fa-solid fa-circle-question note-link-span"></span>
|
||||||
|
</span>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -48,9 +48,9 @@ function regexFromString(input) {
|
|||||||
* @param {regex_placement} placement The placement of the string
|
* @param {regex_placement} placement The placement of the string
|
||||||
* @param {RegexParams} params The parameters to use for the regex script
|
* @param {RegexParams} params The parameters to use for the regex script
|
||||||
* @returns {string} The regexed string
|
* @returns {string} The regexed string
|
||||||
* @typedef {{characterOverride?: string, isMarkdown?: boolean, isPrompt?: boolean }} RegexParams The parameters to use for the regex script
|
* @typedef {{characterOverride?: string, isMarkdown?: boolean, isPrompt?: boolean, depth?: number }} RegexParams The parameters to use for the regex script
|
||||||
*/
|
*/
|
||||||
function getRegexedString(rawString, placement, { characterOverride, isMarkdown, isPrompt } = {}) {
|
function getRegexedString(rawString, placement, { characterOverride, isMarkdown, isPrompt, depth } = {}) {
|
||||||
// WTF have you passed me?
|
// WTF have you passed me?
|
||||||
if (typeof rawString !== 'string') {
|
if (typeof rawString !== 'string') {
|
||||||
console.warn('getRegexedString: rawString is not a string. Returning empty string.');
|
console.warn('getRegexedString: rawString is not a string. Returning empty string.');
|
||||||
@ -71,6 +71,19 @@ function getRegexedString(rawString, placement, { characterOverride, isMarkdown,
|
|||||||
// Script applies to all cases when neither "only"s are true, but there's no need to do it when `isMarkdown`, the as source (chat history) should already be changed beforehand
|
// Script applies to all cases when neither "only"s are true, but there's no need to do it when `isMarkdown`, the as source (chat history) should already be changed beforehand
|
||||||
(!script.markdownOnly && !script.promptOnly && !isMarkdown)
|
(!script.markdownOnly && !script.promptOnly && !isMarkdown)
|
||||||
) {
|
) {
|
||||||
|
// Check if the depth is within the min/max depth
|
||||||
|
if (typeof depth === 'number' && depth >= 0) {
|
||||||
|
if (!isNaN(script.minDepth) && script.minDepth >= 0 && depth < script.minDepth) {
|
||||||
|
console.debug(`getRegexedString: Skipping script ${script.scriptName} because depth ${depth} is less than minDepth ${script.minDepth}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isNaN(script.maxDepth) && script.maxDepth >= 0 && depth > script.maxDepth) {
|
||||||
|
console.debug(`getRegexedString: Skipping script ${script.scriptName} because depth ${depth} is greater than maxDepth ${script.maxDepth}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (script.placement.includes(placement)) {
|
if (script.placement.includes(placement)) {
|
||||||
finalString = runRegexScript(script, finalString, { characterOverride });
|
finalString = runRegexScript(script, finalString, { characterOverride });
|
||||||
}
|
}
|
||||||
|
@ -81,6 +81,7 @@ async function loadRegexScripts() {
|
|||||||
scriptHtml.find('.disable_regex').prop('checked', script.disabled ?? false)
|
scriptHtml.find('.disable_regex').prop('checked', script.disabled ?? false)
|
||||||
.on('input', function () {
|
.on('input', function () {
|
||||||
script.disabled = !!$(this).prop('checked');
|
script.disabled = !!$(this).prop('checked');
|
||||||
|
reloadCurrentChat();
|
||||||
saveSettingsDebounced();
|
saveSettingsDebounced();
|
||||||
});
|
});
|
||||||
scriptHtml.find('.regex-toggle-on').on('click', function () {
|
scriptHtml.find('.regex-toggle-on').on('click', function () {
|
||||||
@ -126,21 +127,13 @@ async function onRegexEditorOpenClick(existingId) {
|
|||||||
editorHtml.find('.find_regex').val(existingScript.findRegex || '');
|
editorHtml.find('.find_regex').val(existingScript.findRegex || '');
|
||||||
editorHtml.find('.regex_replace_string').val(existingScript.replaceString || '');
|
editorHtml.find('.regex_replace_string').val(existingScript.replaceString || '');
|
||||||
editorHtml.find('.regex_trim_strings').val(existingScript.trimStrings?.join('\n') || []);
|
editorHtml.find('.regex_trim_strings').val(existingScript.trimStrings?.join('\n') || []);
|
||||||
editorHtml
|
editorHtml.find('input[name="disabled"]').prop('checked', existingScript.disabled ?? false);
|
||||||
.find('input[name="disabled"]')
|
editorHtml.find('input[name="only_format_display"]').prop('checked', existingScript.markdownOnly ?? false);
|
||||||
.prop('checked', existingScript.disabled ?? false);
|
editorHtml.find('input[name="only_format_prompt"]').prop('checked', existingScript.promptOnly ?? false);
|
||||||
editorHtml
|
editorHtml.find('input[name="run_on_edit"]').prop('checked', existingScript.runOnEdit ?? false);
|
||||||
.find('input[name="only_format_display"]')
|
editorHtml.find('input[name="substitute_regex"]').prop('checked', existingScript.substituteRegex ?? false);
|
||||||
.prop('checked', existingScript.markdownOnly ?? false);
|
editorHtml.find('input[name="min_depth"]').val(existingScript.minDepth ?? '');
|
||||||
editorHtml
|
editorHtml.find('input[name="max_depth"]').val(existingScript.maxDepth ?? '');
|
||||||
.find('input[name="only_format_prompt"]')
|
|
||||||
.prop('checked', existingScript.promptOnly ?? false);
|
|
||||||
editorHtml
|
|
||||||
.find('input[name="run_on_edit"]')
|
|
||||||
.prop('checked', existingScript.runOnEdit ?? false);
|
|
||||||
editorHtml
|
|
||||||
.find('input[name="substitute_regex"]')
|
|
||||||
.prop('checked', existingScript.substituteRegex ?? false);
|
|
||||||
|
|
||||||
existingScript.placement.forEach((element) => {
|
existingScript.placement.forEach((element) => {
|
||||||
editorHtml
|
editorHtml
|
||||||
@ -200,26 +193,13 @@ async function onRegexEditorOpenClick(existingId) {
|
|||||||
.map(function () { return parseInt($(this).val()); })
|
.map(function () { return parseInt($(this).val()); })
|
||||||
.get()
|
.get()
|
||||||
.filter((e) => !isNaN(e)) || [],
|
.filter((e) => !isNaN(e)) || [],
|
||||||
disabled:
|
disabled: editorHtml.find('input[name="disabled"]').prop('checked'),
|
||||||
editorHtml
|
markdownOnly: editorHtml.find('input[name="only_format_display"]').prop('checked'),
|
||||||
.find('input[name="disabled"]')
|
promptOnly: editorHtml.find('input[name="only_format_prompt"]').prop('checked'),
|
||||||
.prop('checked'),
|
runOnEdit: editorHtml.find('input[name="run_on_edit"]').prop('checked'),
|
||||||
markdownOnly:
|
substituteRegex: editorHtml.find('input[name="substitute_regex"]').prop('checked'),
|
||||||
editorHtml
|
minDepth: parseInt(String(editorHtml.find('input[name="min_depth"]').val())),
|
||||||
.find('input[name="only_format_display"]')
|
maxDepth: parseInt(String(editorHtml.find('input[name="max_depth"]').val())),
|
||||||
.prop('checked'),
|
|
||||||
promptOnly:
|
|
||||||
editorHtml
|
|
||||||
.find('input[name="only_format_prompt"]')
|
|
||||||
.prop('checked'),
|
|
||||||
runOnEdit:
|
|
||||||
editorHtml
|
|
||||||
.find('input[name="run_on_edit"]')
|
|
||||||
.prop('checked'),
|
|
||||||
substituteRegex:
|
|
||||||
editorHtml
|
|
||||||
.find('input[name="substitute_regex"]')
|
|
||||||
.prop('checked'),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
saveRegexScript(newRegexScript, existingScriptIndex);
|
saveRegexScript(newRegexScript, existingScriptIndex);
|
||||||
|
Reference in New Issue
Block a user