Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(settings): bind the refresh buttons on first load (#6461)

Lukasd360e2a55c18
diff --git a/changelog.d/6461.bugfix.md b/changelog.d/6461.bugfix.md
new file mode 100644
index 0000000000..c5b70b83d7
--- /dev/null
+++ b/changelog.d/6461.bugfix.md
@@ -0,0 +1,8 @@
+The model and provider refresh buttons on the settings page now work on a
+fresh load (#6407). `setupRefreshButtons()` was only called from the
+tab-switch listener, so the buttons were inert until a tab was changed;
+`#llm.provider-refresh` was rendered by the template and bound nowhere; and the
+`#llm-model-refresh` handler was dead code, since the template emits
+`llm.model-refresh` with a dot. The handlers are assigned rather than added, so
+re-running the setup against still-live buttons cannot stack duplicate
+listeners and double the fetches per click.
diff --git a/src/local_deep_research/web/static/js/components/settings.js b/src/local_deep_research/web/static/js/components/settings.js
index 4d8a0ae66b..ef443837e8 100644
--- a/src/local_deep_research/web/static/js/components/settings.js
+++ b/src/local_deep_research/web/static/js/components/settings.js
@@ -361,11 +361,42 @@
}
}
+ // Handle provider refresh button. The template renders it
+ // (settings_form.html, show_refresh=True) and nothing bound it, so it
+ // was inert in every state (#6407). Same work the model button does:
+ // the provider list and the model list come from one fetch.
+ const providerRefreshBtn = document.getElementById('llm.provider-refresh');
+ if (providerRefreshBtn) {
+ // `onclick`, not addEventListener: this function runs on every
+ // render AND from the tab listener, and a second listener on one
+ // node doubles the forced fetches per click. An assignment is one
+ // handler per node by definition (#6407).
+ providerRefreshBtn.onclick = function() {
+ const icon = providerRefreshBtn.querySelector('i');
+ if (icon) icon.className = 'fas fa-spinner fa-spin';
+ providerRefreshBtn.classList.add('ldr-loading');
+ window.modelDropdownsInitialized = false;
+ fetchModelProviders(true)
+ .then(() => {
+ if (icon) icon.className = 'fas fa-sync-alt';
+ providerRefreshBtn.classList.remove('ldr-loading');
+ initializeModelDropdowns();
+ showAlert('Provider list refreshed', 'success');
+ })
+ .catch(error => {
+ SafeLogger.error('Error refreshing providers:', error);
+ if (icon) icon.className = 'fas fa-sync-alt';
+ providerRefreshBtn.classList.remove('ldr-loading');
+ showAlert('Failed to refresh providers', 'error');
+ });
+ };
+ }
+
// Handle model refresh button
const modelRefreshBtn = document.getElementById('llm.model-refresh');
if (modelRefreshBtn) {
SafeLogger.log('Found and set up model refresh button:', modelRefreshBtn.id);
- modelRefreshBtn.addEventListener('click', function() {
+ modelRefreshBtn.onclick = function() {
const icon = modelRefreshBtn.querySelector('i');
if (icon) icon.className = 'fas fa-spinner fa-spin';
modelRefreshBtn.classList.add('ldr-loading');
@@ -391,7 +422,7 @@
modelRefreshBtn.classList.remove('ldr-loading');
showAlert('Failed to refresh models', 'error');
});
- });
+ };
} else {
SafeLogger.log('Could not find model refresh button');
}
@@ -400,7 +431,7 @@
const searchEngineRefreshBtn = document.getElementById('search.tool-refresh');
if (searchEngineRefreshBtn) {
SafeLogger.log('Found and set up search engine refresh button:', searchEngineRefreshBtn.id);
- searchEngineRefreshBtn.addEventListener('click', function() {
+ searchEngineRefreshBtn.onclick = function() {
const icon = searchEngineRefreshBtn.querySelector('i');
if (icon) icon.className = 'fas fa-spinner fa-spin';
searchEngineRefreshBtn.classList.add('ldr-loading');
@@ -426,7 +457,7 @@
searchEngineRefreshBtn.classList.remove('ldr-loading');
showAlert('Failed to refresh search engines', 'error');
});
- });
+ };
} else {
SafeLogger.log('Could not find search engine refresh button');
@@ -2485,6 +2516,14 @@
// Also initialize the main setup which finds all dropdowns
setupCustomDropdowns();
+ // The refresh buttons live in the markup this render just produced, and
+ // the only other caller is the tab-switch listener — so on a fresh
+ // /settings/ load the model and provider buttons were inert until the
+ // user switched tabs at least once (#6407). Safe to call twice: every
+ // refresh button is bound by assigning `onclick`, so a later call
+ // replaces the handler instead of stacking a second one.
+ setupRefreshButtons();
+
// Reflect the scope→local-inference coupling in the checkboxes.
applyEgressScopeLock();
@@ -4463,32 +4502,13 @@
}
}
- // Set up refresh button
- const refreshBtn = document.querySelector('#llm-model-refresh');
- if (refreshBtn) {
- refreshBtn.addEventListener('click', function() {
- const icon = refreshBtn.querySelector('i');
- if (icon) icon.className = 'fas fa-spinner fa-spin';
-
- // Force refresh models
- loadModelOptions(true).then(() => {
- if (icon) icon.className = 'fas fa-sync-alt';
-
- // Re-filter for current provider
- const provider = providerHiddenInput ?
- providerHiddenInput.value :
- settingsProviderInput ? settingsProviderInput.value : 'ollama';
-
- filterModelOptionsForProvider(provider);
-
- showAlert('Model list refreshed', 'success');
- }).catch(error => {
- SafeLogger.error('Error refreshing models:', error);
- if (icon) icon.className = 'fas fa-sync-alt';
- showAlert('Failed to refresh models: ' + error.message, 'error');
- });
- });
- }
+ // The `#llm-model-refresh` handler that stood here never ran: the
+ // template emits `llm.model-refresh` with a DOT
+ // (custom_dropdown.html, `id="{{ input_id }}-refresh"`), so the
+ // hyphenated selector was always null. Correcting the id would have
+ // bound a SECOND full handler and doubled the fetches per click, so
+ // the block is removed rather than repaired — setupRefreshButtons()
+ // owns that button (#6407).
}
}).catch(err => {
@@ -5508,8 +5528,12 @@
icon.className = 'fas fa-sync-alt';
refreshBtn.appendChild(icon);
- // Add event listener to the button
- refreshBtn.addEventListener('click', function(e) {
+ // Assign, don't add: `setupRefreshButtons()` runs again on the next
+ // render and finds this button by the id set above, so a listener
+ // here would sit alongside that one and double the fetches per click
+ // (#6407). An assignment lets the later, more specific handler take
+ // the node over cleanly.
+ refreshBtn.onclick = function(e) {
e.preventDefault();
e.stopPropagation();
@@ -5540,7 +5564,7 @@
icon.className = 'fas fa-sync-alt';
showAlert('Failed to refresh options', 'error');
});
- });
+ };
// Find the input wrapper or create one
let inputWrapper = input.parentElement;
diff --git a/tests/settings/test_settings_refresh_button_wiring.py b/tests/settings/test_settings_refresh_button_wiring.py
new file mode 100644
index 0000000000..693a765232
--- /dev/null
+++ b/tests/settings/test_settings_refresh_button_wiring.py
@@ -0,0 +1,118 @@
+# allow: no-sut-import - the subject is browser JS with no module exports:
+# settings.js is a single IIFE bound to `window`, so there is nothing to
+# import and a runtime test would have to load the whole file with every
+# global it touches stubbed. These read the file as DATA and assert the
+# wiring the browser depends on. Each check carries a positive control so a
+# selector that stops matching fails loudly instead of passing empty.
+"""The Settings page's refresh buttons must be wired on first load (#6407).
+
+Three shapes, all on the same path:
+
+* ``setupRefreshButtons()`` had exactly ONE call site — the tab-switch
+ listener — so on a fresh ``/settings/`` load the model refresh button was
+ a total no-op until the user switched tabs at least once.
+* ``#llm.provider-refresh`` is rendered by the template
+ (``settings_form.html``, ``show_refresh=True``) and was bound nowhere.
+* ``initializeModelDropdowns()`` carried a handler for
+ ``#llm-model-refresh`` — with a HYPHEN — which is always ``null``: the
+ template emits ``llm.model-refresh`` with a dot. Correcting the id there
+ would have bound a second full handler and doubled the fetches per click,
+ so the block had to go rather than be repaired.
+"""
+
+from pathlib import Path
+
+import pytest
+
+SETTINGS_JS = (
+ Path(__file__).resolve().parents[2]
+ / "src/local_deep_research/web/static/js/components/settings.js"
+)
+
+
[email protected](scope="module")
+def source() -> str:
+ text = SETTINGS_JS.read_text(encoding="utf-8")
+ # Positive control: a parser reading the wrong file would pass every
+ # "is absent" assertion below for free.
+ assert "function setupRefreshButtons()" in text, (
+ "read the wrong file, or the function was renamed"
+ )
+ return text
+
+
+def test_refresh_buttons_are_bound_outside_the_tab_switch_listener(source):
+ """The initial-load render must bind them too, not only a tab switch."""
+ call_sites = source.count("setupRefreshButtons();")
+
+ assert call_sites >= 2, (
+ "setupRefreshButtons() is called from one place only; on a fresh "
+ "page load the refresh buttons are never bound"
+ )
+
+
+def test_the_binding_happens_where_the_markup_was_just_rendered(source):
+ """Anchored on the render, not on a line number: the buttons live in the
+ markup `renderSettingsByTab()` produces, so the call has to follow it."""
+ anchor = "setupCustomDropdowns();"
+ assert anchor in source
+ after_render = source[source.index(anchor) : source.index(anchor) + 800]
+
+ assert "setupRefreshButtons();" in after_render
+
+
+def test_the_provider_refresh_button_is_bound(source):
+ """The template renders it; nothing referenced it."""
+ assert "llm.provider-refresh" in source
+
+
+def _setup_refresh_buttons_body(source: str) -> str:
+ """The text of `setupRefreshButtons()`, sliced on brace depth."""
+ start = source.index("function setupRefreshButtons()")
+ depth = 0
+ for offset in range(start, len(source)):
+ char = source[offset]
+ if char == "{":
+ depth += 1
+ elif char == "}":
+ depth -= 1
+ if depth == 0:
+ return source[start : offset + 1]
+ raise AssertionError("setupRefreshButtons() is not brace-balanced")
+
+
+def test_every_refresh_binding_is_idempotent(source):
+ """`renderSettingsByTab()` rebuilds `#settings-content` via innerHTML, but
+ a tab switch re-runs this function against buttons that are still live, so
+ a second call must not double the fetches per click. `addEventListener`
+ would; an `onclick` assignment is one handler per node by definition —
+ that is the trap which kept the second call site from being added in the
+ first place."""
+ body = _setup_refresh_buttons_body(source)
+ # Control: the slice really is the function, not an empty match.
+ assert "getElementById('llm.model-refresh')" in body
+
+ for button in (
+ "providerRefreshBtn",
+ "modelRefreshBtn",
+ "searchEngineRefreshBtn",
+ ):
+ # Control: this is a button the function really handles.
+ assert f"const {button} = document.getElementById(" in body, button
+
+ assert f"{button}.onclick = function()" in body, (
+ f"{button} must be bound by assignment"
+ )
+ assert f"{button}.addEventListener('click'" not in body, (
+ f"{button} collects one more handler per call"
+ )
+
+
+def test_the_hyphenated_selector_is_gone(source):
+ """`#llm-model-refresh` never matched anything: the template emits the id
+ with a dot. It must not come back as a second handler for the same
+ button."""
+ assert "querySelector('#llm-model-refresh')" not in source
+ assert 'querySelector("#llm-model-refresh")' not in source
+ # Control: the id that DOES exist is still referenced.
+ assert "getElementById('llm.model-refresh')" in source