From 0d9a72becc83556c75dbd536b030ca0c8e46fc79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Jofr=C3=A9?= Date: Wed, 19 Nov 2025 17:56:15 -0300 Subject: [PATCH] Add option to keep manually selected samplers on API Type switch (#4692) * Feat - Save and load manually selected samplers per preset * Fix - Sync sampler popup with selected sampler state settings * Fix - Prevent breaking visualization after disabling keep selected samplers * Fix - ESLint errors in samplerSelect.js * Fix - ESLint errors in textgen-settings.js * Docs - Write JSDocs for new methods in textgen-settings.js * Update - Make the reset button work individually per preset * Fix - Trailing whitespace at the end of class selector * Fix - Update tooltip information for force samplers toggle * Fix - Prevent calling isSamplerManualPriorityEnabled a bajillion times * Fix - Unbrick switch from manually-forced to non-manually-forced preset This prevents samplers non controlled by [data-tg-type] from being sent to the shadow realm if they were previously hidden by [data-tg-samplers]. is_preset_switch is just to prevent breaking muscle memmory of users not using force manually selected samplers. * Fix - Load manually selected samplers on connection profile switch Changing preset does not update samplers ONLY IF force manually selection is disabled. Changing API before preset would update the samplers, but using the settings of the previous preset, due to preset being changed AFTER the API. * Fix - Switch API Type controls in the API Connections panel * Fix - ESLint errors in textgen-settings.js * Fix - Update sampler selection only with textgenerationwebui as main API selected * Styles - Change the sampler selection lock button icon to an actual lock. * Fix - Await for sampler selection reset to finish * Update - Normalize function name * Update - Preserve manual sampler selections on preset renaming * Fix - Properly reset sampler manual selections * Update - Ensure selection reset displays API specific samplers * Fix - ESLint errors * Fix - Avoid modifying non-existent data * Update - Move manual sampler control methods to samplerSelect.js * Fix - Break circular file dependency between textgen-settings.js and samplerSelect.js setting_names was unavailable during page load, throwing an error and preventing ST from loading. * Update menu layout * Remove pointless await * Update param naming to camelCase --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com> --- public/index.html | 130 ++++++------ .../extensions/connection-manager/index.js | 2 +- public/scripts/preset-manager.js | 11 + public/scripts/samplerSelect.js | 191 ++++++++++++++++-- public/scripts/templates/samplerSelector.html | 16 +- public/scripts/textgen-settings.js | 45 ++++- 6 files changed, 308 insertions(+), 87 deletions(-) diff --git a/public/index.html b/public/index.html index 793c36772..2e76dd87c 100644 --- a/public/index.html +++ b/public/index.html @@ -1246,12 +1246,12 @@
-
+
Multiple swipes per generation
-
+
Temperature
@@ -1260,7 +1260,7 @@
-
+
Top K
@@ -1268,7 +1268,7 @@
-
+
Top P
@@ -1276,7 +1276,7 @@
-
+
Typical P
@@ -1284,7 +1284,7 @@
-
+
Min P
@@ -1292,7 +1292,7 @@
-
+
Top A
@@ -1300,7 +1300,7 @@
-
+
TFS
@@ -1308,7 +1308,7 @@
-
+
Epsilon Cutoff
@@ -1316,7 +1316,7 @@
-
+
Top nsigma
@@ -1324,7 +1324,7 @@
-
+
Min Keep
@@ -1332,7 +1332,7 @@
-
+
Eta Cutoff
@@ -1340,62 +1340,62 @@
-
+
Repetition Penalty
-
+
Rep Pen Range
-
+
Rep Pen Slope
-
+
Rep Pen Decay
-
+
Encoder Penalty
-
+
Frequency Penalty
-
+
Presence Penalty
-
+
No Repeat Ngram Size
-
+
Skew
-
+
Min Length
-
+
Maximum tokens/second
-
+

@@ -1406,7 +1406,7 @@

-
+
Smoothing Curve @@ -1414,7 +1414,7 @@
-
+

@@ -1422,7 +1422,7 @@

-
+
Threshold @@ -1435,7 +1435,7 @@
-
+

@@ -1448,30 +1448,30 @@

-
+
Base
-
+
Allowed Length
-
+
Penalty Range
-
+
Sequence Breakers
-
+

@@ -1482,24 +1482,24 @@

-
+
Minimum Temp
-
+
Maximum Temp
-
+
Exponent
-
+

@@ -1510,19 +1510,19 @@

-
+
Tau
-
+
Eta
-
+

-
+
-
+
-
+

Contrastive Search
@@ -1571,64 +1571,64 @@

-
-
+
-
+

Banned Tokens/Strings
-
-
+

Logit Bias @@ -1665,7 +1665,7 @@

-
+

CFG @@ -1676,7 +1676,7 @@

-
+
Negative Prompt
@@ -1684,7 +1684,7 @@
-
+

@@ -1697,7 +1697,7 @@

-
+ -
+

Samplers Order @@ -1751,7 +1751,7 @@ Load default order
-
+

Sampler Order @@ -1775,7 +1775,7 @@ Load default order

-
+

Sampler Priority @@ -1810,7 +1810,7 @@ Load default order

-
+

Sampler Order diff --git a/public/scripts/extensions/connection-manager/index.js b/public/scripts/extensions/connection-manager/index.js index e809677ee..852ff37b9 100644 --- a/public/scripts/extensions/connection-manager/index.js +++ b/public/scripts/extensions/connection-manager/index.js @@ -47,8 +47,8 @@ const CC_COMMANDS = [ ]; const TC_COMMANDS = [ - 'api', 'preset', + 'api', 'api-url', 'model', 'sysprompt', diff --git a/public/scripts/preset-manager.js b/public/scripts/preset-manager.js index 3608e40d3..14fbfab74 100644 --- a/public/scripts/preset-manager.js +++ b/public/scripts/preset-manager.js @@ -27,6 +27,7 @@ import { oai_settings, openai_setting_names, openai_settings } from './openai.js import { POPUP_RESULT, POPUP_TYPE, Popup } from './popup.js'; import { context_presets, getContextSettings, power_user } from './power-user.js'; import { reasoning_templates } from './reasoning.js'; +import { getManualPresetSamplers, resetPresetSelectedSamplers, setPresetSamplersState } from './samplerSelect.js'; import { SlashCommand } from './slash-commands/SlashCommand.js'; import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashCommandArgument.js'; import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; @@ -1071,6 +1072,16 @@ export async function initPresetManager() { return; } + if (apiId === 'textgenerationwebui') { + const manualSamplersIterable = Object.entries(getManualPresetSamplers(oldName)); + + for (const [sampler, value] of manualSamplersIterable) { + setPresetSamplersState(sampler, value, newName); + } + + await resetPresetSelectedSamplers(oldName, true); + } + const successToast = !presetManager.isAdvancedFormatting() ? t`Preset renamed` : t`Template renamed`; toastr.success(successToast); }); diff --git a/public/scripts/samplerSelect.js b/public/scripts/samplerSelect.js index ec45c7b7c..588149665 100644 --- a/public/scripts/samplerSelect.js +++ b/public/scripts/samplerSelect.js @@ -7,18 +7,19 @@ import { power_user } from './power-user.js'; //import { getEventSourceStream } from './sse-stream.js'; //import { getSortableDelay, onlyUnique } from './utils.js'; //import { getCfgPrompt } from './cfg-scale.js'; -import { setting_names } from './textgen-settings.js'; +import { setting_names as TGsamplerNames, showTGSamplerControls, textgenerationwebui_settings } from './textgen-settings.js'; import { renderTemplateAsync } from './templates.js'; import { Popup, POPUP_TYPE } from './popup.js'; - - -const TGsamplerNames = setting_names; +import { localforage } from '../lib.js'; const forcedOnColoring = 'color: #89db35;'; const forcedOffColoring = 'color: #e84f62;'; let userDisabledSamplers, userShownSamplers; +const textGenObjectStore = localforage.createInstance({ name: 'SillyTavern_TextCompletions' }); +let selectedSamplers = {}; + // Goal 1: show popup with all samplers for active API async function showSamplerSelectPopup() { const html = $(document.createElement('div')); @@ -41,6 +42,12 @@ async function showSamplerSelectPopup() { userShownSamplers = []; power_user.selectSamplers.forceShown = []; power_user.selectSamplers.forceHidden = []; + + if (main_api === 'textgenerationwebui') { + $('#prioritizeManuallySelectedSamplers').toggleClass('toggleEnabled', false); + await resetPresetSelectedSamplers(null, true); + } + await validateDisabledSamplers(true); }); @@ -53,7 +60,23 @@ async function showSamplerSelectPopup() { await validateDisabledSamplers(); }); + if (main_api === 'textgenerationwebui') { + $('#prioritizeManuallySelectedSamplers').show(); + $('#prioritizeManuallySelectedSamplers').toggleClass('toggleEnabled', isSamplerManualPriorityEnabled()); + $('#prioritizeManuallySelectedSamplers').off('click').on('click', function () { + $(this).toggleClass('toggleEnabled'); + + const isActive = $(this).hasClass('toggleEnabled'); + + toggleSamplerManualPriority(isActive); + }); + } else { + $('#prioritizeManuallySelectedSamplers').hide(); + $('#prioritizeManuallySelectedSamplers').off('click'); + } + await showPromise; + if (main_api === 'textgenerationwebui') await savePresetSelectedSamplers(); } function setSamplerListListeners() { @@ -180,6 +203,10 @@ function setSamplerListListeners() { const shouldDisplay = $(this).prop('checked') ? targetDisplayType : 'none'; relatedDOMElement.css('display', shouldDisplay); + if (main_api === 'textgenerationwebui') { + setPresetSamplersState(samplerName, shouldDisplay !== 'none'); + } + console.log(samplerName, relatedDOMElement.data('selectsampler'), shouldDisplay); }); @@ -210,6 +237,9 @@ async function listSamplers(main_api, arrayOnly = false) { return availableSamplers; } + const samplersActivatedManually = (main_api === 'textgenerationwebui') ? getManualActivePresetSamplers() : []; + const prioritizeManualSamplerSelect = (main_api === 'textgenerationwebui') ? isSamplerManualPriorityEnabled() : false; + const samplersListHTML = availableSamplers.reduce((html, sampler) => { let customColor, displayname; let targetDOMelement = $(`#${sampler}_${main_api}`); @@ -283,8 +313,7 @@ async function listSamplers(main_api, arrayOnly = false) { displayname = 'Mirostat Block'; } - - + const isManuallyActivated = samplersActivatedManually.includes(sampler); const isInForceHiddenArray = userDisabledSamplers.includes(sampler); const isInForceShownArray = userShownSamplers.includes(sampler); let isVisibleInDOM = isElementVisibleInDOM(targetDOMelement[0]); @@ -295,7 +324,10 @@ async function listSamplers(main_api, arrayOnly = false) { }; const shouldBeChecked = () => { - if (isInForceHiddenArray) { + if (prioritizeManualSamplerSelect) { + return isManuallyActivated; + } + else if (isInForceHiddenArray) { customColor = forcedOffColoring; return false; } @@ -307,6 +339,7 @@ async function listSamplers(main_api, arrayOnly = false) { }; console.log(sampler, targetDOMelement.prop('id'), isInDefaultState(), isInForceShownArray, isInForceHiddenArray, shouldBeChecked()); if (displayname === undefined) { displayname = sampler; } + if (main_api === 'textgenerationwebui') setPresetSamplersState(sampler, shouldBeChecked()); return html + `
@@ -328,6 +361,9 @@ export async function validateDisabledSamplers(redraw = false) { return; } + const samplersActivatedManually = (main_api === 'textgenerationwebui') ? getManualActivePresetSamplers() : []; + const prioritizeManualSamplerSelect = (main_api === 'textgenerationwebui') ? isSamplerManualPriorityEnabled() : false; + for (const sampler of APISamplers) { let relatedDOMElement = $(`#${sampler}_${main_api}`).parent(); let targetDisplayType = 'flex'; @@ -400,6 +436,7 @@ export async function validateDisabledSamplers(redraw = false) { relatedDOMElement = $('#smoothingBlock'); } + if (power_user?.selectSamplers?.forceHidden.includes(sampler)) { //default handling for standard sliders relatedDOMElement.data('selectsampler', 'hidden'); @@ -417,20 +454,147 @@ export async function validateDisabledSamplers(redraw = false) { relatedDOMElement.css('display', 'none'); } } - if (redraw) { - let samplersHTML = await listSamplers(main_api); - $('#apiSamplersList').empty().append(samplersHTML.toString()); - setSamplerListListeners(); + + if (prioritizeManualSamplerSelect) { + const isManuallyActivated = samplersActivatedManually.includes(sampler); + relatedDOMElement.css('display', isManuallyActivated ? targetDisplayType : 'none'); } + } - await saveSettingsDebounced(); + if (redraw) { + if (main_api === 'textgenerationwebui') showTGSamplerControls(); + let samplersHTML = await listSamplers(main_api); + $('#apiSamplersList').empty().append(samplersHTML.toString()); + setSamplerListListeners(); + } + + await saveSettingsDebounced(); +} + +/** + * Initializes the configuration object for manually selected samplers. + */ +export async function loadPresetSelectedSamplers() { + try { + console.debug('Text Completions: loading selected samplers'); + selectedSamplers = await textGenObjectStore.getItem('selectedSamplers') || {}; + } catch (error) { + console.log('Text Completions: unable to load selected samplers, using default samplers', error); + selectedSamplers = {}; } } +/** + * Synchronizes the local forage instance with the selected samplers configuration object. + */ +export async function savePresetSelectedSamplers() { + try { + console.debug('Text Completions: saving selected samplers'); + await textGenObjectStore.setItem('selectedSamplers', selectedSamplers); + } catch (error) { + console.log('Text Completions: unable to save selected samplers', error); + } +} + +/** + * Resets the selected samplers configuration object from the local forage instance. + * @param {string?} presetName Name of the target preset - It picks the current active TC preset name by default + * @param {boolean} silent Suppresses the toastr message confirming that the data was deleted. + */ +export async function resetPresetSelectedSamplers(presetName = '', silent = false) { + try { + if (!textgenerationwebui_settings?.preset && !presetName) return; + if (!presetName) presetName = textgenerationwebui_settings.preset; + if (!selectedSamplers[presetName]) return; + + console.debug('Text Completions: resetting selected samplers'); + delete selectedSamplers[presetName]; + await savePresetSelectedSamplers(); + if (!silent) toastr.success('Selected samplers cleared.'); + } catch (error) { + console.log('Text Completions: unable to reset selected preset samplers', error); + } +} + +/** + * Saves the visibility state for selected samplers into the configuration object. + * @param {string} samplerName Target sampler key name + * @param {string|boolean} state Visibility state of the target sampler + * @param {string?} presetName Name of the target preset - It picks the current active TC preset name by default + * @returns void + */ +export function setPresetSamplersState(samplerName, state, presetName = '') { + if (!textgenerationwebui_settings?.preset && !presetName) return; + if (!presetName) presetName = textgenerationwebui_settings.preset; + if (!selectedSamplers[presetName]) selectedSamplers[presetName] = {}; + + const presetSamplers = selectedSamplers[presetName]; + presetSamplers[samplerName] = String(state) === 'true'; +} + +/** + * Returns the local forage object belonging to the active/selected TC preset + * @param {string?} presetName Name of the target preset - It picks the current active TC preset name by default + * @returns {object} Full localforage object with manual selections + */ +export function getManualPresetSamplers(presetName = '') { + if (!textgenerationwebui_settings?.preset && !presetName) return {}; + if (!presetName) presetName = textgenerationwebui_settings.preset; + if (!selectedSamplers[presetName]) selectedSamplers[presetName] = {}; + + return selectedSamplers[presetName]; +} + +/** + * Returns the key names of all the preset samplers activated manually. + * @param {string?} presetName Name of the target preset - It picks the current active TC preset name by default + * @returns {string[]} Array of sampler key names + */ +export function getManualActivePresetSamplers(presetName = '') { + if (!textgenerationwebui_settings?.preset && !presetName) return []; + if (!presetName) presetName = textgenerationwebui_settings.preset; + if (!selectedSamplers[presetName]) selectedSamplers[presetName] = {}; + + try { + const presetSamplers = Object.entries(selectedSamplers[presetName]); + + return presetSamplers + .filter(([key, val]) => val === true && key !== 'st_manual_priority') + .map(([key, val]) => key); + } catch (error) { + console.log('Text Completions: unable to fetch active preset samplers', error); + return []; + } +} + +/** + * @param {string|boolean} state Target state of the feature + * @param {string?} presetName Name of the target preset - It picks the current active TC preset name by default + * @returns void + */ +export function toggleSamplerManualPriority(state = false, presetName = '') { + if (!textgenerationwebui_settings?.preset && !presetName) return; + if (!presetName) presetName = textgenerationwebui_settings.preset; + if (!selectedSamplers[presetName]) selectedSamplers[presetName] = {}; + + const presetSamplers = selectedSamplers[presetName]; + presetSamplers.st_manual_priority = String(state) === 'true'; +} + +/** + * @param {string?} presetName Name of the target preset - It picks the current active TC preset name by default + * @returns {boolean} + */ +export function isSamplerManualPriorityEnabled(presetName = '') { + if (!textgenerationwebui_settings?.preset && !presetName) return false; + if (!presetName) presetName = textgenerationwebui_settings.preset; + if (!selectedSamplers[presetName]) selectedSamplers[presetName] = {}; + + return selectedSamplers[presetName]?.st_manual_priority ?? false; +} export async function initCustomSelectedSamplers() { - userDisabledSamplers = power_user?.selectSamplers?.forceHidden || []; userShownSamplers = power_user?.selectSamplers?.forceShown || []; power_user.selectSamplers = {}; @@ -438,7 +602,6 @@ export async function initCustomSelectedSamplers() { power_user.selectSamplers.forceShown = userShownSamplers; await saveSettingsDebounced(); $('#samplerSelectButton').off('click').on('click', showSamplerSelectPopup); - } // Goal 4: filter hidden samplers from API output diff --git a/public/scripts/templates/samplerSelector.html b/public/scripts/templates/samplerSelector.html index cecd11cec..57018bb44 100644 --- a/public/scripts/templates/samplerSelector.html +++ b/public/scripts/templates/samplerSelector.html @@ -1,11 +1,17 @@
-
+

Sampler Select

- -