diff --git a/public/index.html b/public/index.html index a8969f3a9..34e19668c 100644 --- a/public/index.html +++ b/public/index.html @@ -115,7 +115,7 @@ Click slider numbers to input manually.
-
+

Kobold Presets @@ -178,7 +178,7 @@
-
+
select
@@ -194,7 +194,7 @@
-
+
select
@@ -535,7 +535,7 @@
-
+
select
@@ -3624,4 +3624,4 @@ - \ No newline at end of file + diff --git a/public/script.js b/public/script.js index b3274285d..3143e0c35 100644 --- a/public/script.js +++ b/public/script.js @@ -433,6 +433,7 @@ const system_messages = {
  • {​{user}​} - your current Persona username
  • {​{char}​} - the Character's name
  • +
  • {​{input}​} - the user input
  • {​{time}​} - the current time
  • {​{date}​} - the current date
  • {{idle_duration}} - the time since the last user message was sent
  • @@ -445,6 +446,7 @@ const system_messages = { name: systemUserName, force_avatar: system_avatar, is_user: false, + is_system: true, is_name: true, mes: [ '

    SillyTavern

    ', @@ -1138,7 +1140,20 @@ function messageFormatting(mes, ch_name, isSystem, isUser) { mes = mes.replaceAll(substituteParams(power_user.user_prompt_bias), ""); } - mes = getRegexedString(mes, regex_placement.MD_DISPLAY); + if (!isSystem) { + let regexPlacement; + if (isUser) { + regexPlacement = regex_placement.USER_INPUT; + } else if (ch_name === "System") { + regexPlacement = regex_placement.SYSTEM; + } else if (ch_name !== name2) { + regexPlacement = regex_placement.SENDAS; + } else { + regexPlacement = regex_placement.AI_OUTPUT; + } + + mes = getRegexedString(mes, regexPlacement, { isMarkdown: true }); + } if (power_user.auto_fix_generated_markdown) { mes = fixMarkdown(mes); @@ -1513,6 +1528,7 @@ function substituteParams(content, _name1, _name2, _original) { content = content.replace(/{{original}}/i, _original); } + content = content.replace(/{{input}}/gi, $('#send_textarea').val()); content = content.replace(/{{user}}/gi, _name1); content = content.replace(/{{char}}/gi, _name2); content = content.replace(//gi, _name1); diff --git a/public/scripts/extensions.js b/public/scripts/extensions.js index 1c7f2052c..3e15c4540 100644 --- a/public/scripts/extensions.js +++ b/public/scripts/extensions.js @@ -63,6 +63,11 @@ const extension_settings = { translate: {}, objective: {}, quickReply: {}, + randomizer: { + controls: [], + fluctuation: 0.1, + enabled: false, + }, }; let modules = []; diff --git a/public/scripts/extensions/randomize/index.js b/public/scripts/extensions/randomize/index.js new file mode 100644 index 000000000..c0950788f --- /dev/null +++ b/public/scripts/extensions/randomize/index.js @@ -0,0 +1,152 @@ +import { saveSettingsDebounced } from "../../../script.js"; +import { extension_settings } from "../../extensions.js"; + +function toggleRandomizedSetting(buttonRef, forId) { + if (extension_settings.randomizer.controls.indexOf(forId) === -1) { + extension_settings.randomizer.controls.push(forId); + } else { + extension_settings.randomizer.controls = extension_settings.randomizer.controls.filter(x => x !== forId); + } + + buttonRef.toggleClass('active'); + console.debug('Randomizer controls:', extension_settings.randomizer.controls); + saveSettingsDebounced(); +} + +function addRandomizeButton() { + const counterRef = $(this); + const labelRef = $(this).find('div[data-for]'); + const isDisabled = counterRef.data('randomization-disabled'); + + if (labelRef.length === 0 || isDisabled == true) { + return; + } + + const forId = labelRef.data('for'); + const buttonRef = $(''); + buttonRef.toggleClass('active', extension_settings.randomizer.controls.indexOf(forId) !== -1); + buttonRef.hide(); + buttonRef.on('click', () => toggleRandomizedSetting(buttonRef, forId)); + counterRef.append(buttonRef); +} + +function onRandomizerEnabled() { + extension_settings.randomizer.enabled = $(this).prop('checked'); + $('.randomize_button').toggle(extension_settings.randomizer.enabled); + console.debug('Randomizer enabled:', extension_settings.randomizer.enabled); +} + +window['randomizerInterceptor'] = (function () { + if (extension_settings.randomizer.enabled === false) { + console.debug('Randomizer skipped: disabled.'); + return; + } + + if (extension_settings.randomizer.fluctuation === 0 || extension_settings.randomizer.controls.length === 0) { + console.debug('Randomizer skipped: nothing to do.'); + return; + } + + for (const control of extension_settings.randomizer.controls) { + const controlRef = $('#' + control); + + if (controlRef.length === 0) { + console.debug(`Randomizer skipped: control ${control} not found.`); + continue; + } + + if (!controlRef.is(':visible')) { + console.debug(`Randomizer skipped: control ${control} is not visible.`); + continue; + } + + let previousValue = parseFloat(controlRef.data('previous-value')); + let originalValue = parseFloat(controlRef.data('original-value')); + let currentValue = parseFloat(controlRef.val()); + + let value; + + // Initialize originalValue and previousValue if they are NaN + if (isNaN(originalValue)) { + originalValue = currentValue; + controlRef.data('original-value', originalValue); + } + if (isNaN(previousValue)) { + previousValue = currentValue; + controlRef.data('previous-value', previousValue); + } + + // If the current value hasn't changed compared to the previous value, use the original value as a base for the calculation + if (currentValue === previousValue) { + console.debug(`Randomizer for ${control} reusing original value: ${originalValue}`); + value = originalValue; + } else { + console.debug(`Randomizer for ${control} using current value: ${currentValue}`); + value = currentValue; + controlRef.data('previous-value', currentValue); // Update the previous value when using the current value + controlRef.data('original-value', currentValue); // Update the original value when using the current value + } + + if (isNaN(value)) { + console.debug('Randomizer skipped: NaN.'); + continue; + } + + const fluctuation = extension_settings.randomizer.fluctuation; + const min = parseFloat(controlRef.attr('min')); + const max = parseFloat(controlRef.attr('max')); + const delta = (Math.random() * fluctuation * 2 - fluctuation) * value; + const newValue = Math.min(Math.max(value + delta, min), max); + console.debug(`Randomizer for ${control}: ${value} -> ${newValue} (delta: ${delta}, min: ${min}, max: ${max})`); + controlRef.val(newValue).trigger('input'); + controlRef.data('previous-value', parseFloat(controlRef.val())); + } +}); + +jQuery(() => { + const html = ` +
    +
    +
    + Parameter Randomizer +
    +
    +
    + +
    +
    + Fluctuation (0-1) +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    `; + + $('#extensions_settings').append(html); + $('#ai_response_configuration .range-block-counter').each(addRandomizeButton); + $('#randomizer_enabled').on('input', onRandomizerEnabled); + $('#randomizer_enabled').prop('checked', extension_settings.randomizer.enabled).trigger('input'); + $('#randomizer_fluctuation').val(extension_settings.randomizer.fluctuation).trigger('input'); + $('#randomizer_fluctuation_counter').text(extension_settings.randomizer.fluctuation); + $('#randomizer_fluctuation').on('input', function () { + const value = parseFloat($(this).val()); + $('#randomizer_fluctuation_counter').text(value); + extension_settings.randomizer.fluctuation = value; + console.debug('Randomizer fluctuation:', extension_settings.randomizer.fluctuation); + saveSettingsDebounced(); + }); +}); diff --git a/public/scripts/extensions/randomize/manifest.json b/public/scripts/extensions/randomize/manifest.json new file mode 100644 index 000000000..11ea04500 --- /dev/null +++ b/public/scripts/extensions/randomize/manifest.json @@ -0,0 +1,12 @@ +{ + "display_name": "Parameter Randomizer", + "loading_order": 15, + "requires": [], + "optional": [], + "js": "index.js", + "css": "style.css", + "author": "Cohee#1207", + "version": "1.0.0", + "generate_interceptor": "randomizerInterceptor", + "homePage": "https://github.com/SillyTavern/SillyTavern" +} diff --git a/public/scripts/extensions/randomize/style.css b/public/scripts/extensions/randomize/style.css new file mode 100644 index 000000000..e69de29bb diff --git a/public/scripts/extensions/regex/editor.html b/public/scripts/extensions/regex/editor.html index 7448e474e..f692cf06c 100644 --- a/public/scripts/extensions/regex/editor.html +++ b/public/scripts/extensions/regex/editor.html @@ -56,13 +56,7 @@