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 <noreply@anthropic.com>
This commit is contained in:
Wolfsblvt
2026-08-16 23:21:01 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent ed563cb7d8
commit cd2c65c9d4
2 changed files with 29 additions and 5 deletions
+18 -1
View File
@@ -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<void>}
*/
let presetApplicationPromise = Promise.resolve();
/**
* Gets a promise that resolves when the currently pending preset application completes.
* @returns {Promise<void>} 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,
+11 -4
View File
@@ -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<void>}
*/
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();
}
}