fix(web): model selection race + stale custom id override

- live catalog no longer re-applies the old/default model after the operator
  changed it while the fetch was in flight
- custom provider:model is cleared when it conflicts with the selected provider
- model card, selects and run body stay in sync
This commit is contained in:
CyberSecurityUP committed 2026-12-03 11:16:20 -03:00
1 parent 48fe6182b7
commit 7e8110350d
1 file changed
+35 -6
+35 -6
View File
@@ -680,6 +680,9 @@ async function loadProviders() {
onProviderChange(parts.slice(1).join(':'));
$('#fieldModelSelect').value = parts.slice(1).join(':');
sanitizeModelSelect();
// A stale custom id must not override the saved default provider/model.
const cc = $('#fieldModelCustom');
if (cc) cc.value = '';
return;
}
}
@@ -705,12 +708,25 @@ function onProviderChange(preferredModel) {
// The seeded list is only a fallback — pull the provider's full live catalog
// (OpenRouter, OpenAI, DeepSeek, Groq, Gemini, local runtimes, …).
const provKey = p?.key;
// If the operator typed a full `provider:model` for a DIFFERENT provider in
// the custom field, it would silently win over the provider just selected.
// Clear the override so the dropdown choice is what the run uses.
const customEl = $('#fieldModelCustom');
if (customEl && customEl.value.trim()) {
const cv = customEl.value.trim();
if (cv.includes(':') && cv.split(':')[0] !== provKey) customEl.value = '';
}
if (provKey) {
// Capture the selection before the async fetch: if the operator changes the
// model while the catalog is loading, the saved "prefer" must not overwrite
// that newer choice (the model card and the select would disagree).
const beforeFetch = modelSel.value;
api(`/api/providers/${encodeURIComponent(provKey)}/models`).then((r) => {
if ($('#fieldProvider')?.value !== provKey) return;
const list = r.models || [];
if (!list.length) return;
const keep = (prefer && list.includes(prefer)) ? prefer : modelSel.value;
const untouched = modelSel.value === beforeFetch;
const keep = (untouched && prefer && list.includes(prefer)) ? prefer : modelSel.value;
modelSel.innerHTML = list.map((m) => `<option value="${esc(m)}">${esc(m)}</option>`).join('');
if (list.includes(keep)) modelSel.value = keep;
sanitizeModelSelect();
@@ -7686,14 +7702,15 @@ loadSettings = async function loadSettingsV5() {
};
const loadCatalog = async (key, prefer) => {
if (!key || !modelSel) return;
const beforeFetch = modelSel.value;
try {
const r = await api(`/api/providers/${encodeURIComponent(key)}/models`);
const list = r.models || [];
if (!list.length || $('#settingProvider')?.value !== key) return;
// The saved model may only exist in the live catalog, not the seed
// list; apply it after the catalog replaces the options or the
// Settings page would show (and then save) the first model.
const keep = (prefer && list.includes(prefer)) ? prefer : modelSel.value;
// Apply the saved model only if the operator did not change the
// selection while the catalog was loading.
const untouched = modelSel.value === beforeFetch;
const keep = (untouched && prefer && list.includes(prefer)) ? prefer : modelSel.value;
modelSel.innerHTML = list.map((m) => `<option value="${esc(m)}">${esc(m)}</option>`).join('');
if (list.includes(keep)) modelSel.value = keep;
} catch { /* keep the seed list */ }
@@ -7876,7 +7893,12 @@ function syncAiSummary() {
$('#btnAiAddKey')?.addEventListener('click', () => openProviderKeysModal());
$('#fieldProvider')?.addEventListener('change', syncAiSummary);
$('#fieldModelSelect')?.addEventListener('change', syncAiSummary);
$('#fieldModelSelect')?.addEventListener('change', () => {
// Picking a model from the dropdown must win over a custom id typed earlier.
const c = $('#fieldModelCustom');
if (c) c.value = '';
syncAiSummary();
});
$('#fieldModelCustom')?.addEventListener('input', syncAiSummary);
$$('#authModeToggle button').forEach((b) => b.addEventListener('click', () => setTimeout(syncAiSummary, 0)));
@@ -9792,6 +9814,13 @@ function restoreDraft(d) {
if (!el) continue;
if (el.type === 'checkbox') el.checked = !!v; else el.value = v;
}
// A draft custom id that names another provider must not override the
// provider the draft selected (e.g. openrouter:… with provider OrcaRouter).
const draftCustom = $('#fieldModelCustom')?.value.trim() || '';
const draftProvider = $('#fieldProvider')?.value || '';
if (draftCustom.includes(':') && draftCustom.split(':')[0] !== draftProvider) {
$('#fieldModelCustom').value = '';
}
Object.assign(state.authz, d.authz || {});
state.credsPath = d.credsPath || ''; state.collectionPath = d.collectionPath || '';
state.selected = new Set(d.selected || []);