#1630 Add min/max depth for prompt/display regex scripts.

This commit is contained in:
Cohee
2024-01-24 22:48:58 +02:00
parent 55984a59bb
commit 3f3529ef89
4 changed files with 77 additions and 44 deletions

View File

@@ -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>

View File

@@ -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 });
}

View File

@@ -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);