From cd2c65c9d45bc774b5a2fd4bfc1858bc885feb21 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sun, 16 Aug 2026 23:21:01 +0200 Subject: [PATCH] fix: await chat completion preset application when switching presets (#5930) Applying a CC preset deferred the actual settings update behind an unawaitable event continuation, so /preset resolved before the preset was applied. A connection profile switch runs /api, /preset, /api in sequence, and the preset's stored chat_completion_source would land after the final /api check, reverting the source to the previous provider. Requests then silently went to the wrong endpoint. The preset application chain is now captured in a promise that selectPreset awaits, so /preset resolves only when the preset is fully applied and follow-up commands sequence correctly. The UI dropdown path is unchanged, and callers that do not await selectPreset keep the exact previous behavior. Fixes #5926 Fixes #5147 Co-authored-by: Claude Fable 5 --- public/scripts/openai.js | 19 ++++++++++++++++++- public/scripts/preset-manager.js | 15 +++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/public/scripts/openai.js b/public/scripts/openai.js index ad92a37d9..a6600c0c9 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -4939,6 +4939,23 @@ async function onLogitBiasPresetDeleteClick() { saveSettingsDebounced(); } +/** + * Promise that resolves when the most recently started preset application has fully completed. + * The change handler defers the actual apply behind an event emission, so programmatic preset + * switches (e.g. /preset, connection profiles) must await this to sequence follow-up commands + * such as /api and /model correctly. + * @type {Promise} + */ +let presetApplicationPromise = Promise.resolve(); + +/** + * Gets a promise that resolves when the currently pending preset application completes. + * @returns {Promise} Promise that resolves when the preset is fully applied. + */ +export function getPresetApplicationPromise() { + return presetApplicationPromise; +} + // Load OpenAI preset settings function onSettingsPresetChange() { const presetNameBefore = oai_settings.preset_settings_openai; @@ -4954,7 +4971,7 @@ function onSettingsPresetChange() { const updateCheckbox = (selector, value) => $(selector).prop('checked', value).trigger('input', { source: 'preset' }); // Allow subscribers to alter the preset before applying deltas - eventSource.emit(event_types.OAI_PRESET_CHANGED_BEFORE, { + presetApplicationPromise = eventSource.emit(event_types.OAI_PRESET_CHANGED_BEFORE, { preset: preset, presetName: presetName, settingsToUpdate: settingsToUpdate, diff --git a/public/scripts/preset-manager.js b/public/scripts/preset-manager.js index 3812670c5..18196ad4e 100644 --- a/public/scripts/preset-manager.js +++ b/public/scripts/preset-manager.js @@ -23,7 +23,7 @@ import { t } from './i18n.js'; import { instruct_presets } from './instruct-mode.js'; import { kai_settings } from './kai-settings.js'; import { convertNovelPreset } from './nai-settings.js'; -import { oai_settings, openai_setting_names, openai_settings } from './openai.js'; +import { getPresetApplicationPromise, 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'; @@ -406,14 +406,21 @@ class PresetManager { /** * Selects a preset by option value. + * The returned promise resolves when the preset is fully applied. Chat Completion presets + * apply asynchronously after the change event, so callers that run follow-up commands + * (e.g. /preset followed by /api) must await it to avoid overriding their own changes. * @param {string} value Preset option value + * @returns {Promise} */ - selectPreset(value) { + async selectPreset(value) { const option = $(this.select).filter(function () { return $(this).val() === value; }); option.prop('selected', true); $(this.select).val(value).trigger('change'); + if (this.apiId === 'openai') { + await getPresetApplicationPromise(); + } } /** @@ -938,7 +945,7 @@ async function presetCommandCallback(_, name) { const presetValue = presetManager.findPreset(exactMatch); if (presetValue) { - presetManager.selectPreset(presetValue); + await presetManager.selectPreset(presetValue); shouldReconnect && await waitForConnection(); } } @@ -961,7 +968,7 @@ async function presetCommandCallback(_, name) { console.log('Found fuzzy preset match', fuzzyPresetName); if (currentPreset !== fuzzyPresetName) { - presetManager.selectPreset(fuzzyPresetValue); + await presetManager.selectPreset(fuzzyPresetValue); shouldReconnect && await waitForConnection(); } }