#1630 Add min/max depth for prompt/display regex scripts.
This commit is contained in:
parent
55984a59bb
commit
3f3529ef89
|
@ -1563,7 +1563,16 @@ export function sendTextareaMessage() {
|
|||
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) {
|
||||
return '';
|
||||
}
|
||||
|
@ -1595,10 +1604,14 @@ function messageFormatting(mes, ch_name, isSystem, isUser) {
|
|||
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
|
||||
mes = getRegexedString(mes, regexPlacement, {
|
||||
characterOverride: ch_name,
|
||||
isMarkdown: true,
|
||||
depth: depth,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1775,7 +1788,7 @@ function getMessageFromTemplate({
|
|||
export function updateMessageBlock(messageId, message) {
|
||||
const messageElement = $(`#chat [mesid="${messageId}"]`);
|
||||
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);
|
||||
appendMediaToMessage(message, messageElement);
|
||||
}
|
||||
|
@ -1891,8 +1904,9 @@ function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll = true
|
|||
mes.name,
|
||||
isSystem,
|
||||
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 ?? '';
|
||||
// Verify bookmarked chat still exists
|
||||
// Cohee: Commented out for now. I'm worried of performance issues.
|
||||
|
@ -2545,6 +2559,7 @@ class StreamingProcessor {
|
|||
chat[messageId].name,
|
||||
chat[messageId].is_system,
|
||||
chat[messageId].is_user,
|
||||
messageId,
|
||||
);
|
||||
const mesText = $(`#chat .mes[mesid="${messageId}"] .mes_text`);
|
||||
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) => {
|
||||
let message = chatItem.mes;
|
||||
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);
|
||||
regexedMessage = await appendFileContent(chatItem, regexedMessage);
|
||||
|
@ -5803,6 +5818,7 @@ function messageEditAuto(div) {
|
|||
this_edit_mes_chname,
|
||||
mes.is_system,
|
||||
mes.is_user,
|
||||
this_edit_mes_id,
|
||||
));
|
||||
saveChatDebounced();
|
||||
}
|
||||
|
@ -5822,10 +5838,11 @@ async function messageEditDone(div) {
|
|||
this_edit_mes_chname,
|
||||
mes.is_system,
|
||||
mes.is_user,
|
||||
this_edit_mes_id,
|
||||
),
|
||||
);
|
||||
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'));
|
||||
addCopyToCodeBlocks(div.closest('.mes'));
|
||||
await eventSource.emit(event_types.MESSAGE_EDITED, this_edit_mes_id);
|
||||
|
@ -8920,6 +8937,7 @@ jQuery(async function () {
|
|||
this_edit_mes_chname,
|
||||
chat[this_edit_mes_id].is_system,
|
||||
chat[this_edit_mes_id].is_user,
|
||||
this_edit_mes_id,
|
||||
));
|
||||
appendMediaToMessage(chat[this_edit_mes_id], $(this).closest('.mes'));
|
||||
addCopyToCodeBlocks($(this).closest('.mes'));
|
||||
|
|
|
@ -96,6 +96,22 @@
|
|||
<span data-i18n="Slash Commands">Slash Commands</span>
|
||||
</label>
|
||||
</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 class="flex1 wi-enter-footer-text flex-container flexFlowColumn flexNoGap alignitemsstart">
|
||||
<small>Other Options</small>
|
||||
|
@ -109,7 +125,10 @@
|
|||
</label>
|
||||
<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"/>
|
||||
<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 class="checkbox flex-container">
|
||||
<input type="checkbox" name="run_on_edit" />
|
||||
|
@ -117,7 +136,10 @@
|
|||
</label>
|
||||
<label class="checkbox flex-container" title="Substitute {{macros}} in Find Regex before running it">
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -48,9 +48,9 @@ function regexFromString(input) {
|
|||
* @param {regex_placement} placement The placement of the string
|
||||
* @param {RegexParams} params The parameters to use for the regex script
|
||||
* @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?
|
||||
if (typeof rawString !== '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.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)) {
|
||||
finalString = runRegexScript(script, finalString, { characterOverride });
|
||||
}
|
||||
|
|
|
@ -81,6 +81,7 @@ async function loadRegexScripts() {
|
|||
scriptHtml.find('.disable_regex').prop('checked', script.disabled ?? false)
|
||||
.on('input', function () {
|
||||
script.disabled = !!$(this).prop('checked');
|
||||
reloadCurrentChat();
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
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('.regex_replace_string').val(existingScript.replaceString || '');
|
||||
editorHtml.find('.regex_trim_strings').val(existingScript.trimStrings?.join('\n') || []);
|
||||
editorHtml
|
||||
.find('input[name="disabled"]')
|
||||
.prop('checked', existingScript.disabled ?? false);
|
||||
editorHtml
|
||||
.find('input[name="only_format_display"]')
|
||||
.prop('checked', existingScript.markdownOnly ?? false);
|
||||
editorHtml
|
||||
.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);
|
||||
editorHtml.find('input[name="disabled"]').prop('checked', existingScript.disabled ?? false);
|
||||
editorHtml.find('input[name="only_format_display"]').prop('checked', existingScript.markdownOnly ?? false);
|
||||
editorHtml.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);
|
||||
editorHtml.find('input[name="min_depth"]').val(existingScript.minDepth ?? '');
|
||||
editorHtml.find('input[name="max_depth"]').val(existingScript.maxDepth ?? '');
|
||||
|
||||
existingScript.placement.forEach((element) => {
|
||||
editorHtml
|
||||
|
@ -200,26 +193,13 @@ async function onRegexEditorOpenClick(existingId) {
|
|||
.map(function () { return parseInt($(this).val()); })
|
||||
.get()
|
||||
.filter((e) => !isNaN(e)) || [],
|
||||
disabled:
|
||||
editorHtml
|
||||
.find('input[name="disabled"]')
|
||||
.prop('checked'),
|
||||
markdownOnly:
|
||||
editorHtml
|
||||
.find('input[name="only_format_display"]')
|
||||
.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'),
|
||||
disabled: editorHtml.find('input[name="disabled"]').prop('checked'),
|
||||
markdownOnly: editorHtml.find('input[name="only_format_display"]').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'),
|
||||
minDepth: parseInt(String(editorHtml.find('input[name="min_depth"]').val())),
|
||||
maxDepth: parseInt(String(editorHtml.find('input[name="max_depth"]').val())),
|
||||
};
|
||||
|
||||
saveRegexScript(newRegexScript, existingScriptIndex);
|
||||
|
|
Loading…
Reference in New Issue