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.

184 KB
Raw Blame
/**
 * Note: URLValidator is available globally via /static/js/security/url-validator.js
 * Research Component
 * Manages the research form and handles submissions
 */
(function() {
    /**
     * Treat blank / whitespace-only strings as absent. Used by the
     * submit handler when extracting ``data.message`` / ``data.field``
     * so a malformed response ({message: ""} or {message: "   "}) is
     * never passed through as-is — that would render an empty alert
     * that is invisible to the user and recreates the original
     * "click submit and nothing happens" symptom.
     * @param {*} value - Value extracted from the response body.
     * @returns {boolean} - True when value is a non-empty string of
     *     non-whitespace characters.
     */
    function nonBlankString(value) {
        return typeof value === 'string' && value.trim().length > 0;
    }

    // Form validator instance
    let researchValidator = null;
    // The active submit owns the button and overlay until its request settles.
    // Keyboard shortcuts call the handler directly (and textarea key events
    // also bubble to the document shortcut), so disabling the button alone
    // cannot prevent duplicate requests.
    let activeResearchSubmit = null;

    // Preserve user-event order for writes to the same setting. Different
    // settings keep independent promise tails, so they can still save in
    // parallel. The tracked tail always settles so one failed request cannot
    // prevent the next value from being written.
    const pendingResearchSettingSaves = new Map();

    function enqueueResearchSettingSave(settingKey, startSave) {
        const priorSave = pendingResearchSettingSaves.get(settingKey)
            || Promise.resolve();
        const request = priorSave.then(startSave, startSave);
        const trackedRequest = request.catch(() => undefined).finally(() => {
            if (pendingResearchSettingSaves.get(settingKey) === trackedRequest) {
                pendingResearchSettingSaves.delete(settingKey);
            }
        });

        pendingResearchSettingSaves.set(settingKey, trackedRequest);
        return request;
    }

    // DOM Elements
    let form = null;
    let queryInput = null;
    let modeOptions = null;
    let notificationToggle = null;
    let startBtn = null;
    let modelProviderSelect = null;
    let customEndpointInput = null;
    let endpointContainer = null;
    let anthropicEndpointUrlInput = null;
    let anthropicEndpointContainer = null;
    let ollamaUrlInput = null;
    let ollamaUrlContainer = null;
    let lmstudioUrlInput = null;
    let lmstudioUrlContainer = null;
    let contextWindowInput = null;
    let contextWindowContainer = null;
    // API Key inputs and containers
    let openaiApiKeyInput = null;
    let openaiApiKeyContainer = null;
    let anthropicApiKeyInput = null;
    let anthropicApiKeyContainer = null;
    let googleApiKeyInput = null;
    let googleApiKeyContainer = null;
    let openrouterApiKeyInput = null;
    let openrouterApiKeyContainer = null;
    let orcarouterApiKeyInput = null;
    let orcarouterApiKeyContainer = null;
    let atlascloudApiKeyInput = null;
    let atlascloudApiKeyContainer = null;
    let qwenApiKeyInput = null;
    let qwenApiKeyContainer = null;
    let xaiApiKeyInput = null;
    let xaiApiKeyContainer = null;
    let ionosApiKeyInput = null;
    let ionosApiKeyContainer = null;
    let openaiEndpointApiKeyInput = null;
    let openaiEndpointApiKeyContainer = null;
    let anthropicEndpointApiKeyInput = null;
    let anthropicEndpointApiKeyContainer = null;
    let ollamaApiKeyInput = null;
    let ollamaApiKeyContainer = null;
    let lmstudioApiKeyInput = null;
    let lmstudioApiKeyContainer = null;
    let modelInput = null;
    let modelDropdown = null;
    let modelDropdownList = null;
    let modelRefreshBtn = null;
    let searchEngineInput = null;
    let searchEngineDropdown = null;
    let searchEngineDropdownList = null;
    let searchEngineRefreshBtn = null;
    let advancedToggle = null;
    let advancedPanel = null;

    // Cache keys for in-memory cache (5-minute expiration, clears on page reload)
    const CACHE_KEYS = {
        MODELS: 'deepResearch.availableModels',
        SEARCH_ENGINES: 'deepResearch.searchEngines'
    };

    // Cache expiration time (24 hours in milliseconds)
    const CACHE_EXPIRATION = 24 * 60 * 60 * 1000;

    // State variables for dropdowns
    let modelOptions = [];
    let selectedModelValue = '';
    const modelSelectedIndex = -1;
    let searchEngineOptions = [];
    let selectedSearchEngineValue = '';
    let unavailableSearchEngineValue = '';
    let hasExplicitSearchEngineSelection = false;
    let initialSearchEngineOptionsPending = true;
    const searchEngineSelectedIndex = -1;

    // Generation token + AbortController for the egress-scope
    // reapplier. Rapid scope/primary changes would otherwise race:
    // every response unconditionally overwrites searchEngineOptions,
    // so an older response completing last could render
    // classifications for a scope/primary that is no longer current.
    // Issue #5204 follow-up review.
    let applyEgressScopeSeq = 0;
    let applyEgressScopeController = null;

    // Track initialization to prevent unwanted saves during initial setup
    let isInitializing = true;

    // Store pending rerun config to apply after initialization completes
    let pendingRerunConfig = null;

    /**
     * Select a research mode (both visual and radio button)
     * @param {HTMLElement} modeElement - The mode option element that was selected
     */
    function selectMode(modeElement) {
        // Update visual appearance
        modeOptions.forEach(m => {
            m.classList.remove('active');
            m.setAttribute('tabindex', '-1');
        });

        modeElement.classList.add('active');
        modeElement.setAttribute('tabindex', '0');

        // Update the corresponding radio button
        const modeValue = modeElement.getAttribute('data-mode');
        const radioButton = document.getElementById(`mode-${modeValue}`);
        if (radioButton) {
            radioButton.checked = true;
        }
    }

    // Model provider options - will be populated dynamically from API
    let MODEL_PROVIDERS = [];

    // Store available models by provider - will be populated dynamically from API
    let availableModels = {};

    /**
     * Check if a provider is a local provider (not cloud-based)
     * Uses the is_cloud attribute from provider metadata
     * @param {string} providerKey - The provider key (e.g., 'OLLAMA', 'LMSTUDIO')
     * @returns {boolean} - True if the provider is local (is_cloud === false)
     */
    function isLocalProvider(providerKey) {
        if (!providerKey) return false;
        const provider = MODEL_PROVIDERS.find(
            p => p.value && p.value.toUpperCase() === providerKey.toUpperCase()
        );
        // Use is_cloud from provider metadata; return false if provider not found yet
        return provider ? provider.is_cloud === false : false;
    }

    /**
     * Get saved advanced menu state from localStorage.
     * @returns {boolean} true if panel should be open (defaults to true for first visit)
     */
    function getAdvancedMenuState() {
        const saved = localStorage.getItem('advancedMenuOpen');
        // Default to open for new users so they discover the available options
        return saved === null ? true : saved === 'true';
    }

    /**
     * Apply advanced options panel state to the DOM.
     * Syncs toggle button classes, ARIA attributes, icon, and panel visibility.
     * @param {boolean} isOpen - Whether the panel should be open
     */
    function applyAdvancedOptionsState(isOpen) {
        if (!advancedToggle || !advancedPanel) return;

        advancedToggle.classList.toggle('ldr-open', isOpen);
        advancedPanel.classList.toggle('ldr-expanded', isOpen);
        advancedToggle.setAttribute('aria-expanded', String(isOpen));

        const srText = advancedToggle.querySelector('.sr-only');
        if (srText) {
            srText.textContent = isOpen
                ? 'Click to collapse advanced options'
                : 'Click to expand advanced options';
        }

        const icon = advancedToggle.querySelector('i');
        if (icon) {
            icon.className = isOpen ? 'fas fa-chevron-up' : 'fas fa-chevron-down';
        }
    }

    /**
     * Check for rerun configuration from sessionStorage.
     * Stores it for later application after initialization completes.
     */
    function checkAndApplyRerunConfig() {
        let rerunConfigStr;
        try {
            rerunConfigStr = sessionStorage.getItem('rerunConfig');
        } catch (e) {
            SafeLogger.warn('Could not read rerun config from sessionStorage:', e);
            return;
        }
        if (!rerunConfigStr) return;

        try {
            pendingRerunConfig = JSON.parse(rerunConfigStr);
            sessionStorage.removeItem('rerunConfig'); // Clear immediately
            SafeLogger.log('Stored pending rerun config:', pendingRerunConfig);
        } catch (e) {
            SafeLogger.error('Error parsing rerun config:', e);
            sessionStorage.removeItem('rerunConfig');
            pendingRerunConfig = null;
        }
    }

    /**
     * Apply pending rerun configuration after initialization is complete.
     * Only pre-fills query and mode; all other settings come from the
     * settings manager / database (the current user defaults).
     */
    function applyPendingRerunConfig() {
        if (!pendingRerunConfig) return;

        const config = pendingRerunConfig;
        pendingRerunConfig = null; // Clear to prevent re-application
        SafeLogger.log('Applying rerun config:', config);

        // Set query
        const queryEl = document.getElementById('query');
        if (queryEl && config.query) {
            queryEl.value = config.query;
        }

        // Set mode via the visible label element so both the radio and
        // the visual highlight update correctly. Match the stored mode
        // against the rendered options by attribute comparison instead of
        // building a selector from the stored value; unknown or hostile
        // values match no option and leave the default mode selected.
        if (config.mode) {
            const modeOption = Array.from(modeOptions).find(
                option => option.getAttribute('data-mode') === config.mode
            );
            if (modeOption) {
                selectMode(modeOption);
            }
        }

        // Show notification
        showRerunNotification();
    }

    /**
     * Called when initialization completes. Applies any pending rerun config.
     */
    function onInitializationComplete() {
        SafeLogger.log('Initialization complete, checking for pending rerun config');
        applyPendingRerunConfig();
    }

    /**
     * Show notification that form has been pre-filled for re-run
     */
    function showRerunNotification() {
        const alertContainer = document.getElementById('research-alert');
        if (alertContainer) {
            alertContainer.style.display = 'block';
            alertContainer.innerHTML = `
                <div class="alert alert-info alert-dismissible fade show" role="alert">
                    <i class="fas fa-redo me-2"></i>
                    Re-running previous research. Review settings and click "Start Research" when ready.
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            `;
        }
    }

    /**
     * Initialize the research component
     */
    function initializeResearch() {
        // Set initializing flag
        isInitializing = true;
        SafeLogger.log('=== Starting research page initialization. isInitializing:', isInitializing);

        // Check for rerun config from history page
        checkAndApplyRerunConfig();

        // Get DOM elements
        form = document.getElementById('research-form');
        queryInput = document.getElementById('query');
        modeOptions = document.querySelectorAll('.ldr-mode-option');
        notificationToggle = document.getElementById('notification-toggle');
        startBtn = document.getElementById('start-research-btn');
        modelProviderSelect = document.getElementById('model_provider');
        customEndpointInput = document.getElementById('custom_endpoint');
        endpointContainer = document.getElementById('endpoint_container');
        anthropicEndpointUrlInput = document.getElementById('anthropic_endpoint_url');
        anthropicEndpointContainer = document.getElementById('anthropic_endpoint_container');
        ollamaUrlInput = document.getElementById('ollama_url');
        ollamaUrlContainer = document.getElementById('ollama_url_container');
        lmstudioUrlInput = document.getElementById('lmstudio_url');
        lmstudioUrlContainer = document.getElementById('lmstudio_url_container');
        contextWindowInput = document.getElementById('context_window');
        contextWindowContainer = document.getElementById('context_window_container');

        // API Key elements
        openaiApiKeyInput = document.getElementById('openai_api_key');
        openaiApiKeyContainer = document.getElementById('openai_api_key_container');
        anthropicApiKeyInput = document.getElementById('anthropic_api_key');
        anthropicApiKeyContainer = document.getElementById('anthropic_api_key_container');
        googleApiKeyInput = document.getElementById('google_api_key');
        googleApiKeyContainer = document.getElementById('google_api_key_container');
        openrouterApiKeyInput = document.getElementById('openrouter_api_key');
        openrouterApiKeyContainer = document.getElementById('openrouter_api_key_container');
        orcarouterApiKeyInput = document.getElementById('orcarouter_api_key');
        orcarouterApiKeyContainer = document.getElementById('orcarouter_api_key_container');
        atlascloudApiKeyInput = document.getElementById('atlascloud_api_key');
        atlascloudApiKeyContainer = document.getElementById('atlascloud_api_key_container');
        qwenApiKeyInput = document.getElementById('qwen_api_key');
        qwenApiKeyContainer = document.getElementById('qwen_api_key_container');
        xaiApiKeyInput = document.getElementById('xai_api_key');
        xaiApiKeyContainer = document.getElementById('xai_api_key_container');
        ionosApiKeyInput = document.getElementById('ionos_api_key');
        ionosApiKeyContainer = document.getElementById('ionos_api_key_container');
        openaiEndpointApiKeyInput = document.getElementById('openai_endpoint_api_key');
        openaiEndpointApiKeyContainer = document.getElementById('openai_endpoint_api_key_container');
        anthropicEndpointApiKeyInput = document.getElementById('anthropic_endpoint_api_key');
        anthropicEndpointApiKeyContainer = document.getElementById('anthropic_endpoint_api_key_container');
        ollamaApiKeyInput = document.getElementById('ollama_api_key');
        ollamaApiKeyContainer = document.getElementById('ollama_api_key_container');
        lmstudioApiKeyInput = document.getElementById('lmstudio_api_key');
        lmstudioApiKeyContainer = document.getElementById('lmstudio_api_key_container');

        // Custom dropdown elements
        modelInput = document.getElementById('model');
        modelDropdown = document.getElementById('model-dropdown');
        modelDropdownList = document.getElementById('model-dropdown-list');
        modelRefreshBtn = document.getElementById('model-refresh');

        searchEngineInput = document.getElementById('search_engine');
        searchEngineDropdown = document.getElementById('search-engine-dropdown');
        searchEngineDropdownList = document.getElementById('search-engine-dropdown-list');
        searchEngineRefreshBtn = document.getElementById('search_engine-refresh');

        // Other form elements
        advancedToggle = document.querySelector('.ldr-advanced-options-toggle');
        advancedPanel = document.querySelector('.ldr-advanced-options-panel');

        // Note: Settings are now loaded from the database via the template
        // The form values are already set by the server-side rendering
        // We just need to initialize the UI components

        // Initialize the UI first (immediate operations)
        setupEventListeners();
        // Don't populate providers yet - wait for API data
        initializeDropdowns();

        // Don't set initial values yet - wait for model options to load first
        // setInitialFormValues() will be called after loadSettings() completes

        // Auto-focus the query input
        if (queryInput) {
            queryInput.focus();
            // Move cursor to end if there's existing text
            if (queryInput.value) {
                queryInput.setSelectionRange(queryInput.value.length, queryInput.value.length);
            }
        }

        // Then load data asynchronously (don't block UI)
        Promise.all([
            loadModelOptions(false),
            loadSearchEngineOptions(false).then(engines => {
                // Reconcile even cached/error-fallback results before a
                // research submit can use the server-rendered hidden value.
                // Model/settings loading may still be pending at this point.
                reconcileSearchEngineSelection(engines);
                initialSearchEngineOptionsPending = false;
                const loadingAlert = document.querySelector('#research-alert [data-search-engine-loading]');
                if (loadingAlert) {
                    const container = loadingAlert.parentNode;
                    loadingAlert.remove();
                    if (!container.firstChild) container.style.display = 'none';
                }
                return engines;
            })
        ]).then(([_modelData, searchEngineData]) => {
            // After loading model data, update the UI with the loaded data
            const currentProvider = modelProviderSelect ? modelProviderSelect.value : 'OLLAMA';
            updateModelOptionsForProvider(currentProvider, false);

            // Update search engine options
            if (searchEngineData && Array.isArray(searchEngineData)) {
                searchEngineOptions = searchEngineData;

                // Force search engine dropdown to update with new data
                if (searchEngineDropdownList && window.setupCustomDropdown) {
                    // Recreate the dropdown with the new data
                    const searchDropdownInstance = window.setupCustomDropdown(
                        searchEngineInput,
                        searchEngineDropdownList,
                        () => (searchEngineOptions.length > 0 ? searchEngineOptions : [{ value: '', label: 'No search engines available' }]),
                        (value, item) => {
                            if (searchEngineOptions.some(engine => engine.value === value && !engine.disabled)) {
                                hasExplicitSearchEngineSelection = true;
                            }
                            selectedSearchEngineValue = value;

                            // Update the input field
                            if (item) {
                                searchEngineInput.value = item.label;
                            } else {
                                searchEngineInput.value = value;
                            }
                            reconcileSearchEngineSelection(searchEngineOptions);

                            // custom_dropdown dispatches the hidden input's
                            // change event immediately after this callback;
                            // that listener below is the single owner of the
                            // settings write. Only fall back to a direct save
                            // for an incomplete embedding without the hidden
                            // form control.
                            if (!isInitializing && !document.getElementById('search_engine_hidden')) {
                                saveSearchEngineSettings(value);
                            }
                        },
                        false,
                        'No search engines available.',
                        handleSearchEngineFavoriteToggle
                    );

                    // If we have a last selected search engine, try to select it if allowed
                    const lastSearchEngine = searchEngineInput?.getAttribute('data-initial-value') ||
                                           localStorage.getItem('selected_search_engine');
                    reconcileSearchEngineSelection(searchEngineOptions, lastSearchEngine || '');
                }
            }

            // Set initial form values from data attributes
            setInitialFormValues();

            // Finally, load settings after data is available
            loadSettings();
        }).catch(error => {
            SafeLogger.error('Failed to load options:', error);

            // Set initial form values even if data loading fails
            setInitialFormValues();

            // Still load settings even if data loading fails
            loadSettings();

            if (window.ui && window.ui.showAlert) {
                window.ui.showAlert('Some options could not be loaded. Using defaults instead.', 'warning');
            }
        });
    }

    /**
     * Initialize custom dropdowns for model and search engine
     */
    function initializeDropdowns() {
        // Check if the custom dropdown script is loaded
        if (typeof window.setupCustomDropdown !== 'function') {
            SafeLogger.error('Custom dropdown script is not loaded');
            // Display an error message
            if (window.ui && window.ui.showAlert) {
                window.ui.showAlert('Failed to initialize dropdowns. Please reload the page.', 'error');
            }
            return;
        }

        SafeLogger.log('Initializing dropdowns with setupCustomDropdown');

        // Set up model dropdown
        if (modelInput && modelDropdownList) {
            // Clear any existing dropdown setup
            modelDropdownList.innerHTML = '';
            const modelDropdownInstance = window.setupCustomDropdown(
                modelInput,
                modelDropdownList,
                () => {
                    SafeLogger.log('Getting model options from dropdown:', modelOptions);
                    return modelOptions.length > 0 ? modelOptions : [{ value: '', label: 'No models available' }];
                },
                (value, item) => {
                    SafeLogger.log('Model selected:', value, item);
                    SafeLogger.log('isInitializing flag:', isInitializing);
                    selectedModelValue = value;

                    // Update the input field with the selected model's label or value
                    if (item) {
                        modelInput.value = item.label;
                    } else {
                        modelInput.value = value;
                    }

                    const isCustomValue = !item;
                    showCustomModelWarning(isCustomValue);

                    // Save selected model to settings - only if not initializing
                    if (!isInitializing) {
                        SafeLogger.log('Saving model to database:', value);
                        saveModelSettings(value);
                    } else {
                        SafeLogger.log('Skipping save - still initializing');
                    }
                },
                true, // Allow custom values
                'No models available. Type to enter a custom model name.'
            );

            // Initialize model refresh button
            if (modelRefreshBtn) {
                modelRefreshBtn.addEventListener('click', function() {
                    const icon = modelRefreshBtn.querySelector('i');

                    // Add loading class to button
                    modelRefreshBtn.classList.add('ldr-loading');

                    // Force refresh of model options
                    loadModelOptions(true).then(() => {
                        // Remove loading class
                        modelRefreshBtn.classList.remove('ldr-loading');

                        // Ensure the current provider's models are loaded
                        const currentProvider = modelProviderSelect ? modelProviderSelect.value : 'OLLAMA';
                        updateModelOptionsForProvider(currentProvider, false);

                        // Force dropdown update
                        const event = new Event('click', { bubbles: true });
                        modelInput.dispatchEvent(event);
                    }).catch(error => {
                        SafeLogger.error('Error refreshing models:', error);

                        // Remove loading class
                        modelRefreshBtn.classList.remove('ldr-loading');

                        if (window.ui && window.ui.showAlert) {
                            window.ui.showAlert('Failed to refresh models: ' + error.message, 'error');
                        }
                    });
                });
            }
        }

        // Set up search engine dropdown
        if (searchEngineInput && searchEngineDropdownList) {
            // Clear any existing dropdown setup
            searchEngineDropdownList.innerHTML = '';

            // Add loading state to search engine input
            if (searchEngineInput.parentNode) {
                searchEngineInput.parentNode.classList.add('ldr-loading');
            }
            const searchDropdownInstance = window.setupCustomDropdown(
                searchEngineInput,
                searchEngineDropdownList,
                () => {
                    // Log available search engines for debugging
                    SafeLogger.log('Getting search engine options:', searchEngineOptions);
                    return searchEngineOptions.length > 0 ? searchEngineOptions : [{ value: '', label: 'No search engines available' }];
                },
                (value, item) => {
                    SafeLogger.log('Search engine selected:', value, item);
                    if (searchEngineOptions.some(engine => engine.value === value && !engine.disabled)) {
                        hasExplicitSearchEngineSelection = true;
                    }
                    selectedSearchEngineValue = value;

                    // Update the input field with the selected search engine's label or value
                    if (item) {
                        searchEngineInput.value = item.label;
                    } else {
                        searchEngineInput.value = value;
                    }

                    // Keep the hidden input in sync so the STRICT-scope
                    // availability check (and form submit) see the new value.
                    const seHidden = document.getElementById('search_engine_hidden');
                    if (seHidden) {
                        seHidden.value = value;
                    }
                    reconcileSearchEngineSelection(searchEngineOptions);

                    // The dropdown dispatches change on the hidden input after
                    // this callback; let that listener own the write so a
                    // single selection cannot race duplicate PUTs.
                    if (!isInitializing && !seHidden) {
                        saveSearchEngineSettings(value);
                    }
                },
                false, // Don't allow custom values
                'No search engines available.',
                handleSearchEngineFavoriteToggle
            );

            // Initialize search engine refresh button
            if (searchEngineRefreshBtn) {
                searchEngineRefreshBtn.addEventListener('click', function() {
                    const icon = searchEngineRefreshBtn.querySelector('i');

                    // Add loading class to button
                    searchEngineRefreshBtn.classList.add('ldr-loading');

                    // Force refresh of search engine options
                    loadSearchEngineOptions(true).then(() => {
                        // Remove loading class
                        searchEngineRefreshBtn.classList.remove('ldr-loading');

                        // Force dropdown update
                        const event = new Event('click', { bubbles: true });
                        searchEngineInput.dispatchEvent(event);
                    }).catch(error => {
                        SafeLogger.error('Error refreshing search engines:', error);

                        // Remove loading class
                        searchEngineRefreshBtn.classList.remove('ldr-loading');

                        if (window.ui && window.ui.showAlert) {
                            window.ui.showAlert('Failed to refresh search engines: ' + error.message, 'error');
                        }
                    });
                });
            }
        }
    }

    /**
     * Set initial form values from data attributes
     */
    function setInitialFormValues() {
        SafeLogger.log('Setting initial form values...');

        // Set initial model value if available
        if (modelInput) {
            const initialModel = modelInput.getAttribute('data-initial-value');
            SafeLogger.log('Initial model value from data attribute:', initialModel);
            if (initialModel) {
                // Find the matching model in the options
                const matchingModel = modelOptions.find(m =>
                    m.value === initialModel || m.id === initialModel
                );

                if (matchingModel) {
                    modelInput.value = matchingModel.label;
                    selectedModelValue = matchingModel.value;
                } else {
                    // If not found in options, set it as custom value
                    modelInput.value = initialModel;
                    selectedModelValue = initialModel;
                }

                // Update hidden input
                const hiddenInput = document.getElementById('model_hidden');
                if (hiddenInput) {
                    hiddenInput.value = selectedModelValue;
                }
            }
        }

        // Reconcile the saved value even when it is absent from the options.
        if (searchEngineInput) {
            const initialSearchEngine = searchEngineInput.getAttribute('data-initial-value');
            reconcileSearchEngineSelection(searchEngineOptions, initialSearchEngine || '');
        }
    }

    /**
     * Setup event listeners
     */
    function setupEventListeners() {
        if (!form || !startBtn) return;

        // Setup inline form validation for query field
        if (window.FormValidator && queryInput) {
            researchValidator = new window.FormValidator();
            researchValidator.addValidation(
                queryInput,
                window.formValidators.required('Please enter a research query.'),
                { validateOnBlur: false, validateOnInput: false }
            );

            // The selected model is stored in the hidden #model_hidden input
            // (kept in sync by the custom dropdown), so validate against that
            // value while showing the error on the visible #model input.
            if (modelInput) {
                researchValidator.addValidation(
                    modelInput,
                    () => {
                        const modelHidden = document.getElementById('model_hidden');
                        const modelValue = modelHidden ? modelHidden.value.trim() : '';
                        return modelValue ? null : 'Please select or enter a model.';
                    },
                    { validateOnBlur: false, validateOnInput: false }
                );
            }

            // Register the egress scope dropdown so a `.ldr-field-error` element
            // exists next to it. There's no client-side rule here — the
            // server is the source of truth — but pre-creating the slot means
            // showFormError() can attach an inline message without having to
            // insert DOM at error time (which is more error-prone around
            // timing and scroll-into-view).
            const egressScopeInput = document.getElementById('policy_egress_scope');
            if (egressScopeInput) {
                researchValidator.addValidation(
                    egressScopeInput,
                    () => null,
                    { validateOnBlur: false, validateOnInput: false }
                );
            }

            // Clear any prior egress-scope error as soon as the user
            // changes the dropdown — keeps the red border / message
            // from sticking around once the user has acknowledged the
            // issue. Only this field's inline error is wiped: an
            // unrelated query-required / model-required error must
            // still be visible to the user. Submit-owned alert
            // toasts are also dismissed so a stale message stops
            // being announced on the next interaction.
            if (egressScopeInput) {
                egressScopeInput.addEventListener('change', () => {
                    if (researchValidator) {
                        try {
                            researchValidator.clearFieldError(egressScopeInput);
                        } catch (_e) { /* defensive */ }
                    }
                    clearSubmitOwnedAlerts();
                });
            }
        }

        // INITIALIZE ADVANCED OPTIONS FIRST - before any async operations
        // Advanced options toggle - make immediately responsive
        if (advancedToggle && advancedPanel) {
            // Set initial state based on localStorage (default to open)
            applyAdvancedOptionsState(getAdvancedMenuState());

            // Add the click listener
            advancedToggle.addEventListener('click', function() {
                const isOpen = !advancedToggle.classList.contains('ldr-open');
                applyAdvancedOptionsState(isOpen);
                localStorage.setItem('advancedMenuOpen', isOpen.toString());
            });

            // Add keyboard support for the advanced options toggle
            advancedToggle.addEventListener('keydown', function(event) {
                if (event.key === 'Enter' || event.key === ' ') {
                    event.preventDefault();
                    this.click(); // Trigger the click handler
                }
            });
        }

        // Global keyboard shortcuts for this page
        document.addEventListener('keydown', function(event) {
            // Escape key: return focus to search field (override global Esc behavior when on search page)
            if (event.key === 'Escape') {
                if (queryInput && document.activeElement !== queryInput) {
                    event.preventDefault();
                    event.stopPropagation(); // Prevent global keyboard service from handling this
                    queryInput.focus();
                    queryInput.select(); // Select all text for easy replacement
                }
            }

            // Ctrl/Cmd + Enter: submit form from anywhere on the page
            if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {
                if (form) {
                    event.preventDefault();
                    handleResearchSubmit(new Event('submit'));
                }
            }
        });

        // Form submission
        form.addEventListener('submit', handleResearchSubmit);

        // Mode selection - updated for accessibility
        modeOptions.forEach(mode => {
            mode.addEventListener('click', function() {
                selectMode(this);
            });

            mode.addEventListener('keydown', function(event) {
                if (event.key === 'Enter' || event.key === ' ') {
                    event.preventDefault();
                    selectMode(this);
                } else if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') {
                    event.preventDefault();
                    // Find the previous mode option, skipping hidden inputs
                    const allModeOptions = Array.from(document.querySelectorAll('.ldr-mode-option'));
                    const currentIndex = allModeOptions.indexOf(this);
                    const previousMode = allModeOptions[currentIndex - 1];
                    if (previousMode) {
                        selectMode(previousMode);
                        previousMode.focus();
                    }
                } else if (event.key === 'ArrowRight' || event.key === 'ArrowDown') {
                    event.preventDefault();
                    // Find the next mode option, skipping hidden inputs
                    const allModeOptions = Array.from(document.querySelectorAll('.ldr-mode-option'));
                    const currentIndex = allModeOptions.indexOf(this);
                    const nextMode = allModeOptions[currentIndex + 1];
                    if (nextMode) {
                        selectMode(nextMode);
                        nextMode.focus();
                    }
                }
            });
        });

        // Add keyboard shortcuts for textarea
        if (queryInput) {
            queryInput.addEventListener('keydown', function(event) {
                if (event.key === 'Enter') {
                    if (event.shiftKey) {
                        // Allow default behavior (new line) — fall through
                    } else if (event.ctrlKey || event.metaKey) {
                        // Ctrl+Enter or Cmd+Enter = Submit form (common pattern)
                        event.preventDefault();
                        handleResearchSubmit(new Event('submit'));
                    } else {
                        // Just Enter = Submit form (keeping existing behavior)
                        event.preventDefault();
                        handleResearchSubmit(new Event('submit'));
                    }
                }
            });
        }

        // Model provider change
        if (modelProviderSelect) {
            modelProviderSelect.addEventListener('change', function() {
                const provider = this.value;
                SafeLogger.log('Model provider changed to:', provider);

                // Show custom endpoint input if OpenAI endpoint is selected
                if (endpointContainer) {
                    endpointContainer.style.display = provider === 'OPENAI_ENDPOINT' ? 'block' : 'none';
                }

                // Show Anthropic endpoint input if Anthropic endpoint is selected
                if (anthropicEndpointContainer) {
                    anthropicEndpointContainer.style.display = provider === 'ANTHROPIC_ENDPOINT' ? 'block' : 'none';
                }

                // Show Ollama URL input if Ollama is selected
                if (ollamaUrlContainer) {
                    ollamaUrlContainer.style.display = provider === 'OLLAMA' ? 'block' : 'none';
                }

                // Show LM Studio URL input if LMSTUDIO is selected
                if (lmstudioUrlContainer) {
                    lmstudioUrlContainer.style.display = provider === 'LMSTUDIO' ? 'block' : 'none';
                }

                // Show context window for local providers
                if (contextWindowContainer) {
                    contextWindowContainer.style.display = isLocalProvider(provider) ? 'block' : 'none';
                }

                // Show API key input for cloud providers
                if (openaiApiKeyContainer) {
                    openaiApiKeyContainer.style.display = provider === 'OPENAI' ? 'block' : 'none';
                }
                if (anthropicApiKeyContainer) {
                    anthropicApiKeyContainer.style.display = provider === 'ANTHROPIC' ? 'block' : 'none';
                }
                if (googleApiKeyContainer) {
                    googleApiKeyContainer.style.display = provider === 'GOOGLE' ? 'block' : 'none';
                }
                if (openrouterApiKeyContainer) {
                    openrouterApiKeyContainer.style.display = provider === 'OPENROUTER' ? 'block' : 'none';
                }
                if (orcarouterApiKeyContainer) {
                    orcarouterApiKeyContainer.style.display = provider === 'ORCAROUTER' ? 'block' : 'none';
                }
                if (atlascloudApiKeyContainer) {
                    atlascloudApiKeyContainer.style.display = provider === 'ATLASCLOUD' ? 'block' : 'none';
                }
                if (qwenApiKeyContainer) {
                    qwenApiKeyContainer.style.display = provider === 'QWEN' ? 'block' : 'none';
                }
                if (xaiApiKeyContainer) {
                    xaiApiKeyContainer.style.display = provider === 'XAI' ? 'block' : 'none';
                }
                if (ionosApiKeyContainer) {
                    ionosApiKeyContainer.style.display = provider === 'IONOS' ? 'block' : 'none';
                }
                if (openaiEndpointApiKeyContainer) {
                    openaiEndpointApiKeyContainer.style.display = provider === 'OPENAI_ENDPOINT' ? 'block' : 'none';
                }
                if (anthropicEndpointApiKeyContainer) {
                    anthropicEndpointApiKeyContainer.style.display = provider === 'ANTHROPIC_ENDPOINT' ? 'block' : 'none';
                }
                if (ollamaApiKeyContainer) {
                    ollamaApiKeyContainer.style.display = provider === 'OLLAMA' ? 'block' : 'none';
                }
                if (lmstudioApiKeyContainer) {
                    lmstudioApiKeyContainer.style.display = provider === 'LMSTUDIO' ? 'block' : 'none';
                }

                // Update model options based on provider
                // Don't reset model selection - preserve it if valid for new provider
                updateModelOptionsForProvider(provider, false);

                // Save provider change to database
                saveProviderSetting(provider);

                // Also update any settings form with the same provider
                const settingsProviderInputs = document.querySelectorAll('input[data-key="llm.provider"]');
                settingsProviderInputs.forEach(input => {
                    if (input !== modelProviderSelect) {
                        input.value = provider;
                        const hiddenInput = document.getElementById('llm.provider_hidden');
                        if (hiddenInput) {
                            hiddenInput.value = provider;
                            // Trigger change event
                            const event = new Event('change', { bubbles: true });
                            hiddenInput.dispatchEvent(event);
                        }
                    }
                });
            });
        }

        // Search engine change - save to settings manager
        // Note: Listen to the hidden input to get the value (config key) not the label
        const searchEngineHiddenInput = document.getElementById('search_engine_hidden');
        if (searchEngineHiddenInput) {
            searchEngineHiddenInput.addEventListener('change', function() {
                const searchEngine = this.value;
                SafeLogger.log('Search engine changed to:', searchEngine);
                saveSearchSetting('search.tool', searchEngine);
                // Issue #5204: re-apply the egress-scope filter now
                // that the selected primary changed.
                if (typeof applyEgressScopeToEngines === 'function') {
                    applyEgressScopeToEngines();
                }
            });
        }

        // Iterations change - save to settings manager
        const iterationsInput = document.getElementById('iterations');
        if (iterationsInput) {
            iterationsInput.addEventListener('change', function() {
                const iterations = parseInt(this.value, 10);
                SafeLogger.log('Iterations changed to:', iterations);
                saveSearchSetting('search.iterations', iterations);
            });
        }

        // Questions per iteration change - save to settings manager
        const questionsInput = document.getElementById('questions_per_iteration');
        if (questionsInput) {
            questionsInput.addEventListener('change', function() {
                const questions = parseInt(this.value, 10);
                SafeLogger.log('Questions per iteration changed to:', questions);
                saveSearchSetting('search.questions_per_iteration', questions);
            });
        }

        // Privacy & Egress controls — persist to settings DB on change so
        // the picked scope/local-inference toggles survive the next page load
        // and the egress-policy warning banner refreshes immediately.
        let policyScopeSaveQueue = Promise.resolve();
        let policyScopeSaveGeneration = 0;

        // Both the egress-scope select and the "Require Local LLM Endpoint"
        // checkbox reshape which providers the backend reports as disabled,
        // and both have to re-fetch AFTER their save lands, because the
        // backend rebuilds provider_options from the DB on every request.
        //
        // Call this synchronously inside the change handler to claim a
        // generation, then run the returned thunk on policyScopeSaveQueue.
        // The shared queue keeps a scope save and a checkbox save ordered
        // against each other; the generation guard drops every refresh but
        // the newest one, so two quick toggles can't land their responses
        // out of order and leave the previous policy's disabled set
        // rendered. A cached (non-force_refresh) fetch is enough here — the
        // ~1s force_refresh path re-discovers every provider's models and
        // buys nothing, since provider_options is policy-derived per
        // request.
        let policyModelRefreshGeneration = 0;
        function claimPolicyModelRefresh() {
            const generation = ++policyModelRefreshGeneration;
            return () => {
                if (generation !== policyModelRefreshGeneration) {
                    return undefined;
                }
                invalidateCacheKey(CACHE_KEYS.MODELS);
                return loadModelOptions(false).catch(() => undefined);
            };
        }
        const policyScopeSelect = document.getElementById('policy_egress_scope');
        if (policyScopeSelect) {
            policyScopeSelect.dataset.savedValue = policyScopeSelect.value;
            policyScopeSelect.addEventListener("change", function() {
                const selectedValue = this.value;
                const generation = ++policyScopeSaveGeneration;
                applyPrivacyPanelScope(selectedValue);
                // Issue #5204: re-fetch the search-engine list under the
                // new scope so the dropdown's disabled set reflects
                // what would actually be accepted at submit time. The
                // backend's precheck stays as the security guarantee;
                // this is the UX layer that makes the form impossible
                // to misconfigure by default. Fired on change rather
                // than on save success because submit reads the scope
                // from the form, not from the saved setting; if the
                // save is rejected, refreshPolicyScopeFromServer
                // reverts the select and re-mirrors the dropdown.
                if (typeof applyEgressScopeToEngines === 'function') {
                    applyEgressScopeToEngines();
                }
                // private_only forces require_local_llm at the backend,
                // which reshapes the LLM provider dropdown. The refresh is
                // chained onto the save queue so it sees the freshly-saved
                // scope rather than the policy it replaced.
                const refreshProviders = claimPolicyModelRefresh();
                policyScopeSaveQueue = policyScopeSaveQueue
                    .catch(() => undefined)
                    .then(() => saveSearchSetting(
                        "policy.egress_scope",
                        selectedValue,
                        () => {
                            if (generation === policyScopeSaveGeneration) {
                                refreshPolicyScopeFromServer(this);
                            }
                        },
                        () => {
                            if (generation === policyScopeSaveGeneration) {
                                this.dataset.savedValue = selectedValue;
                            }
                        }
                    ))
                    .then(refreshProviders)
                    // The queue is left dangling until the next toggle
                    // chains onto it, so it has to be self-contained: an
                    // escaping rejection here would surface as an
                    // unhandledRejection with no one to catch it.
                    .catch(() => undefined);
            });
            // Apply the initial cue on page load (the data-scope attribute is
            // already set server-side from settings; this just keeps the icon
            // in sync without requiring a roundtrip).
            applyPrivacyPanelScope(policyScopeSelect.value);
        }

        const llmRequireLocalInput = document.getElementById('llm_require_local_endpoint');
        if (llmRequireLocalInput && llmRequireLocalInput.dataset.envLocked !== "true") {
            llmRequireLocalInput.addEventListener('change', function() {
                // The toggle reshapes which cloud providers are blocked in
                // the Model Provider dropdown, so the refresh has to fire
                // after the save lands. Runs on the same queue as the
                // egress-scope save: private_only forces this same flag on
                // at the backend, so letting the two race would mean the
                // dropdown reflects whichever response happened to arrive
                // last rather than the policy actually in effect.
                const refreshProviders = claimPolicyModelRefresh();
                const checked = this.checked;
                policyScopeSaveQueue = policyScopeSaveQueue
                    .catch(() => undefined)
                    .then(() => saveSearchSetting(
                        'llm.require_local_endpoint', checked
                    ))
                    .then(refreshProviders)
                    .catch(() => undefined);
            });
        }
        const embRequireLocalInput = document.getElementById('embeddings_require_local');
        if (embRequireLocalInput && embRequireLocalInput.dataset.envLocked !== "true") {
            embRequireLocalInput.addEventListener('change', function() {
                saveSearchSetting('embeddings.require_local', this.checked);
            });
        }

        // LM Studio URL change - save to settings manager
        if (lmstudioUrlInput) {
            lmstudioUrlInput.addEventListener('change', function() {
                const url = this.value;
                saveSearchSetting('llm.lmstudio.url', url);
            });
        }

        // Context window size change - save to settings manager
        if (contextWindowInput) {
            contextWindowInput.addEventListener('change', function() {
                const size = parseInt(this.value, 10);
                saveSearchSetting('llm.local_context_window_size', size);
            });
        }

        // Ollama URL change - save to settings manager
        if (ollamaUrlInput) {
            ollamaUrlInput.addEventListener('change', function() {
                const url = this.value;
                SafeLogger.log('Ollama URL changed to:', url);
                saveSearchSetting('llm.ollama.url', url);
            });
        }

        // API Key change handlers - save to settings manager
        if (openaiApiKeyInput) {
            openaiApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.openai.api_key', this.value);
            });
        }
        if (anthropicApiKeyInput) {
            anthropicApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.anthropic.api_key', this.value);
            });
        }
        if (googleApiKeyInput) {
            googleApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.google.api_key', this.value);
            });
        }
        if (openrouterApiKeyInput) {
            openrouterApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.openrouter.api_key', this.value);
            });
        }
        if (orcarouterApiKeyInput) {
            orcarouterApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.orcarouter.api_key', this.value);
            });
        }
        if (atlascloudApiKeyInput) {
            atlascloudApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.atlascloud.api_key', this.value);
            });
        }
        if (qwenApiKeyInput) {
            qwenApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.qwen.api_key', this.value);
            });
        }
        if (xaiApiKeyInput) {
            xaiApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.xai.api_key', this.value);
            });
        }
        if (ionosApiKeyInput) {
            ionosApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.ionos.api_key', this.value);
            });
        }
        if (openaiEndpointApiKeyInput) {
            openaiEndpointApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.openai_endpoint.api_key', this.value);
            });
        }
        if (anthropicEndpointUrlInput) {
            anthropicEndpointUrlInput.addEventListener('change', function() {
                const url = this.value;
                SafeLogger.log('Anthropic endpoint URL changed to:', url);
                saveSearchSetting('llm.anthropic_endpoint.url', url);
            });
        }
        if (anthropicEndpointApiKeyInput) {
            anthropicEndpointApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.anthropic_endpoint.api_key', this.value);
            });
        }
        if (ollamaApiKeyInput) {
            ollamaApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.ollama.api_key', this.value);
            });
        }
        if (lmstudioApiKeyInput) {
            lmstudioApiKeyInput.addEventListener('change', function() {
                saveSearchSetting('llm.lmstudio.api_key', this.value);
            });
        }

        // NOTE: model/search-engine options are loaded once by initializeResearch()
        // right after this function returns (its Promise.all also runs
        // setInitialFormValues() + loadSettings() once the data is in). A second
        // load was previously kicked off here too, firing two concurrent
        // /available-models requests on every page load — that self-inflicted
        // contention against a cold Ollama (2s list timeout) is a big reason the
        // model dropdown came up empty. Deliberately not reloading here.
    }

    /**
     * Show or hide warning about custom model entries
     * @param {boolean} show - Whether to show the warning
     */
    function showCustomModelWarning(show) {
        let warningEl = document.getElementById('custom-model-warning');

        if (!warningEl && show) {
            warningEl = document.createElement('div');
            warningEl.id = 'custom-model-warning';
            warningEl.className = 'ldr-model-warning';
            warningEl.textContent = 'Custom model name entered. Make sure it exists in your provider.';
            // The research page wraps the dropdown in `.ldr-form-group`; the
            // unprefixed class only exists on older/settings markup. Fall back
            // to the immediate parent so the warning is never silently dropped.
            const parent =
                modelDropdown &&
                (modelDropdown.closest('.ldr-form-group, .form-group') ||
                    modelDropdown.parentNode);
            if (parent) {
                parent.appendChild(warningEl);
            }
        }

        if (warningEl) {
            warningEl.style.display = show ? 'block' : 'none';
        }
    }

    /**
     * Populate model provider dropdown
     */
    function populateModelProviders() {
        if (!modelProviderSelect) return;

        // Don't populate if we don't have providers yet
        if (MODEL_PROVIDERS.length === 0) {
            SafeLogger.log('No providers loaded yet, skipping populate');
            return;
        }

        // Remember the latest selection across a policy-driven clear, so
        // re-enabling it restores that provider instead of the page-load one.
        const currentValue = modelProviderSelect.value;
        if (currentValue) {
            modelProviderSelect.setAttribute('data-initial-value', currentValue);
        }

        // Clear existing options
        modelProviderSelect.innerHTML = '';

        // Add options. Cloud providers that the egress policy blocks are kept in
        // the dropdown but rendered as <option disabled> with the policy
        // reason appended to the label — so the user sees that the
        // provider exists, that their API key was saved, and exactly why
        // it can't be selected right now.
        MODEL_PROVIDERS.forEach(provider => {
            const option = document.createElement('option');
            option.value = provider.value;
            const isDisabled = provider.disabled === true;
            option.disabled = isDisabled;
            let label = provider.label || provider.value;
            if (isDisabled && provider.disabled_reason) {
                label += ' — ' + provider.disabled_reason;
            }
            option.textContent = label;
            modelProviderSelect.appendChild(option);
        });

        // Reconcile the selection against the new option list, mirroring
        // reconcileSearchEngineSelection() below.
        //
        // Rules:
        //   1. If the current selection is still present AND enabled, keep
        //      it (the user's last pick wins).
        //   2. Otherwise restore the most recently selected provider from
        //      data-initial-value, when that one is enabled.
        //   3. If neither is selectable, CLEAR the selection instead of
        //      substituting some other enabled provider.
        //
        // Rule 3 is deliberate. Substituting looks friendlier but submits a
        // provider the user never chose: the assignment is programmatic, so
        // the change listener never fires and llm.provider is never saved,
        // and the model input is left holding a model that belongs to the
        // provider we just navigated away from. The form would post that
        // mismatched pair while the settings DB still said something else.
        // Submission keeps the remembered provider even when selection is
        // empty, so the backend rejects the intended provider regardless
        // of whether its settings save has finished. The blocked provider
        // stays visible in the list with
        // its reason attached, so nothing is hidden from the user.
        const optionsList = Array.from(modelProviderSelect.options);
        const currentOpt = currentValue
            ? optionsList.find(opt => opt.value === currentValue)
            : null;
        const initialProvider = (
            modelProviderSelect.getAttribute('data-initial-value') || 'OLLAMA'
        ).toUpperCase();
        const initialOpt = optionsList.find(
            opt => opt.value.toUpperCase() === initialProvider
        );

        if (currentOpt && !currentOpt.disabled) {
            modelProviderSelect.value = currentOpt.value;
        } else if (initialOpt && !initialOpt.disabled) {
            SafeLogger.log('Initial provider from data attribute:', initialProvider);
            modelProviderSelect.value = initialOpt.value;
        } else {
            SafeLogger.log(
                'No selectable model provider (configured provider blocked or missing); clearing selection'
            );
            modelProviderSelect.value = '';
        }

        const selectedProvider = modelProviderSelect.value || initialProvider;

        // Show custom endpoint input if OpenAI endpoint is selected
        if (endpointContainer) {
            SafeLogger.log('Setting endpoint container display for provider:', selectedProvider);
            endpointContainer.style.display = selectedProvider === 'OPENAI_ENDPOINT' ? 'block' : 'none';
        } else {
            SafeLogger.warn('Endpoint container not found');
        }

        // Show Anthropic endpoint input if Anthropic endpoint is selected
        if (anthropicEndpointContainer) {
            anthropicEndpointContainer.style.display = selectedProvider === 'ANTHROPIC_ENDPOINT' ? 'block' : 'none';
        }

        // Show Ollama URL input if Ollama is selected
        if (ollamaUrlContainer) {
            ollamaUrlContainer.style.display = selectedProvider === 'OLLAMA' ? 'block' : 'none';
        }

        // Show LM Studio URL input if LMSTUDIO is selected
        if (lmstudioUrlContainer) {
            lmstudioUrlContainer.style.display = selectedProvider === 'LMSTUDIO' ? 'block' : 'none';
        }

        // Show context window for local providers
        if (contextWindowContainer) {
            contextWindowContainer.style.display = isLocalProvider(selectedProvider) ? 'block' : 'none';
        }

        // Show API key containers based on active/reconciled provider
        if (openaiApiKeyContainer) {
            openaiApiKeyContainer.style.display = selectedProvider === 'OPENAI' ? 'block' : 'none';
        }
        if (anthropicApiKeyContainer) {
            anthropicApiKeyContainer.style.display = selectedProvider === 'ANTHROPIC' ? 'block' : 'none';
        }
        if (googleApiKeyContainer) {
            googleApiKeyContainer.style.display = selectedProvider === 'GOOGLE' ? 'block' : 'none';
        }
        if (openrouterApiKeyContainer) {
            openrouterApiKeyContainer.style.display = selectedProvider === 'OPENROUTER' ? 'block' : 'none';
        }
        if (orcarouterApiKeyContainer) {
            orcarouterApiKeyContainer.style.display = selectedProvider === 'ORCAROUTER' ? 'block' : 'none';
        }
        if (atlascloudApiKeyContainer) {
            atlascloudApiKeyContainer.style.display = selectedProvider === 'ATLASCLOUD' ? 'block' : 'none';
        }
        if (qwenApiKeyContainer) {
            qwenApiKeyContainer.style.display = selectedProvider === 'QWEN' ? 'block' : 'none';
        }
        if (xaiApiKeyContainer) {
            xaiApiKeyContainer.style.display = selectedProvider === 'XAI' ? 'block' : 'none';
        }
        if (ionosApiKeyContainer) {
            ionosApiKeyContainer.style.display = selectedProvider === 'IONOS' ? 'block' : 'none';
        }
        if (openaiEndpointApiKeyContainer) {
            openaiEndpointApiKeyContainer.style.display = selectedProvider === 'OPENAI_ENDPOINT' ? 'block' : 'none';
        }
        if (anthropicEndpointApiKeyContainer) {
            anthropicEndpointApiKeyContainer.style.display = selectedProvider === 'ANTHROPIC_ENDPOINT' ? 'block' : 'none';
        }
        if (ollamaApiKeyContainer) {
            ollamaApiKeyContainer.style.display = selectedProvider === 'OLLAMA' ? 'block' : 'none';
        }
        if (lmstudioApiKeyContainer) {
            lmstudioApiKeyContainer.style.display = selectedProvider === 'LMSTUDIO' ? 'block' : 'none';
        }

        // Initial update of model options
        updateModelOptionsForProvider(selectedProvider);
    }

    /**
     * Update model options based on selected provider
     * @param {string} provider - The selected provider
     * @param {boolean} resetSelectedModel - Whether to reset the selected model
     * @returns {Promise} - A promise that resolves when the model options are updated
     */
    function updateModelOptionsForProvider(provider, resetSelectedModel = false) {
        return new Promise((resolve) => {
            // Convert provider to uppercase for consistent comparison
            const providerUpper = provider.toUpperCase();
            SafeLogger.log('Filtering models for provider:', providerUpper, 'resetSelectedModel:', resetSelectedModel);

        // If models aren't loaded yet, return early - they'll be loaded when available
        const allModels = getCachedData(CACHE_KEYS.MODELS);
        if (!allModels || !Array.isArray(allModels)) {
            SafeLogger.log('No model data loaded yet, will populate when available');
            // Load models then try again
            loadModelOptions(false).then(() => {
                    updateModelOptionsForProvider(provider, resetSelectedModel)
                        .then(resolve)
                        .catch(() => resolve([]));
                }).catch(() => resolve([]));
            return;
        }

            SafeLogger.log('Filtering models for provider:', providerUpper, 'from', allModels.length, 'models');

            // Filter models based on provider
            // Simple filtering: only show models from the selected provider
            const models = allModels.filter(model => {
                if (!model || typeof model !== 'object') return false;
                // Skip provider options (they have value but no id)
                if (model.value && !model.id && !model.name) return false;
                const modelProvider = (model.provider || '').toUpperCase();
                return modelProvider === providerUpper;
            });

            SafeLogger.log('Filtered models for provider', provider, ':', models.length, 'models');

        // Format models for dropdown
        modelOptions = models.map(model => {
                const label = model.name || model.label || model.id || model.value || 'Unknown model';
                const value = model.id || model.value || '';
            return { value, label, provider: model.provider };
        });

            SafeLogger.log(`Updated model options for provider ${provider}: ${modelOptions.length} models`);

            // The saved model is restored separately by the main settings load
            // (loadSettings applies data.settings["llm.model"] to the dropdown).
            // No extra per-provider DB lookup is needed here — the previous
            // fetch read data.setting.value, a shape the flat settings endpoint
            // never returns, so it was a no-op. Select from the provider's
            // available options and resolve.
            selectModelBasedOnProvider(resetSelectedModel, null);
            resolve(modelOptions);
        });
    }

    /**
     * Select a model based on the current provider and saved preferences
     * @param {boolean} resetSelectedModel - Whether to reset the selected model
     * @param {string} lastSelectedModel - The last selected model from localStorage or database
     */
    function selectModelBasedOnProvider(resetSelectedModel, lastSelectedModel) {
        if (modelInput && modelInput.disabled) {
            // Don't change the model automatically if we've disabled model
            // selection. Then the user won't be able to change it back.
            return;
        }

        if (resetSelectedModel) {
            if (modelInput) {
                // Try to select last used model first if it's available
                if (lastSelectedModel) {
                    const matchingModel = modelOptions.find(model => model.value === lastSelectedModel);
                    if (matchingModel) {
                        modelInput.value = matchingModel.label;
                        selectedModelValue = matchingModel.value;
                        SafeLogger.log('Selected previously used model:', selectedModelValue);

                        // Update any hidden input if it exists
                        const hiddenInput = document.getElementById('model_hidden');
                        if (hiddenInput) {
                            hiddenInput.value = selectedModelValue;
                        }

                        // Only save to settings if we're not initializing
                        if (!isInitializing) {
                            saveModelSettings(selectedModelValue);
                        }
                        return;
                    }
                }

                // If no matching model, clear and select first available
                modelInput.value = '';
                selectedModelValue = '';
            }
        }

        // Select model from database if available
        if ((!selectedModelValue || selectedModelValue === '') && modelOptions.length > 0 && modelInput) {
            // Try to find last used model from database
            if (lastSelectedModel) {
                const matchingModel = modelOptions.find(model => model.value === lastSelectedModel);
                if (matchingModel) {
                    modelInput.value = matchingModel.label;
                    selectedModelValue = matchingModel.value;
                    SafeLogger.log('Selected previously used model:', selectedModelValue);

                    // Update any hidden input if it exists
                    const hiddenInput = document.getElementById('model_hidden');
                    if (hiddenInput) {
                        hiddenInput.value = selectedModelValue;
                    }

                    // Only save to settings if we're not initializing
                    if (!isInitializing) {
                        saveModelSettings(selectedModelValue);
                    }
                    return;
                }
            }

            // Don't auto-select first model - wait for database settings to load
            // or let user manually select a model
            SafeLogger.log('No saved model found, waiting for user selection');
        }
    }

    /**
     * Check if Ollama is running and available
     * @returns {Promise<boolean>} True if Ollama is running
     */
    async function isOllamaRunning() {
        try {
            // Use the API endpoint with proper timeout handling
            const controller = new AbortController();
            const timeoutId = setTimeout(() => controller.abort(), 5000); // 5 second timeout

            const response = await fetch(URLS.SETTINGS_API.OLLAMA_STATUS, {
                signal: controller.signal
            });

            clearTimeout(timeoutId);

            if (response.ok) {
                const data = await response.json();
                return data.running === true;
            }
            return false;
            } catch (error) {
            SafeLogger.error('Ollama check failed:', error.name === 'AbortError' ? 'Request timed out' : error);
            return false;
        }
    }

    /**
     * Get the currently selected model value
     * @returns {string} The selected model value
     */
    function getSelectedModel() {
        SafeLogger.log('Getting selected model...');
        SafeLogger.log('- selectedModelValue:', selectedModelValue);
        SafeLogger.log('- modelInput value:', modelInput ? modelInput.value : 'modelInput not found');
        SafeLogger.log('- modelInput exists:', !!modelInput);

        // First try the stored selected value from dropdown
        if (selectedModelValue) {
            SafeLogger.log('Using selectedModelValue:', selectedModelValue);
            return selectedModelValue;
        }

        // Then try the input field value
        if (modelInput && modelInput.value.trim()) {
            SafeLogger.log('Using modelInput value:', modelInput.value.trim());
            return modelInput.value.trim();
        }

        // Finally, check if there's a hidden input with the model value
        const hiddenModelInput = document.getElementById('model_hidden');
        if (hiddenModelInput && hiddenModelInput.value) {
            SafeLogger.log('Using hidden input value:', hiddenModelInput.value);
            return hiddenModelInput.value;
        }

        SafeLogger.log('No model value found, returning empty string');
        return "";
    }

    /**
     * Check if Ollama is running and the selected model is available
     * @returns {Promise<{success: boolean, error: string, solution: string}>} Result of the check
     */
    async function checkOllamaModel() {
        const isRunning = await isOllamaRunning();

        if (!isRunning) {
            return {
                success: false,
                error: "Ollama service is not running.",
                solution: "Please start Ollama and try again. If you've recently updated, you may need to run database migration with 'python -m src.local_deep_research.migrate_db'."
            };
        }

        // Get the currently selected model
        const model = getSelectedModel();

        if (!model) {
            return {
                success: false,
                error: "No model selected.",
                solution: "Please select or enter a valid model name."
            };
        }

        // Check if the model is available in Ollama
        try {
            const controller = new AbortController();
            const timeoutId = setTimeout(() => controller.abort(), 5000);

            const response = await fetch(`/research/api/check/ollama_model?model=${encodeURIComponent(model)}`, {
                signal: controller.signal
            });

            clearTimeout(timeoutId);

            if (!response.ok) {
                return {
                    success: false,
                    error: "Error checking model availability.",
                    solution: "Please check your Ollama installation and try again."
                };
            }

            const data = await response.json();

            if (data.available) {
                return {
                    success: true
                };
            }
            return {
                    success: false,
                    error: data.message || "The selected model is not available in Ollama.",
                    solution: "Please pull the model first using 'ollama pull " + model + "' or select a different model."
                };

        } catch (error) {
            SafeLogger.error("Error checking Ollama model:", error);
            return {
                success: false,
                error: "Error checking model availability: " + error.message,
                solution: "Please check your Ollama installation and try again."
            };
        }
    }

    // Load settings from the database
    function loadSettings() {
        SafeLogger.log('Loading settings from database...');
        // A single async settings fetch gates initialization completion.
        // The strategy dropdown is rendered and pre-selected server-side
        // (research.html / get_available_strategies), so it needs no separate
        // client fetch here.
        let numApiCallsPending = 1;

        // Fetch the current settings from the settings API
        fetch(URLS.SETTINGS_API.BASE, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => {
            if (!response.ok) {
                throw new Error(`API error: ${response.status}`);
            }
            return response.json();
        })
        .then(data => {
            SafeLogger.log('Loaded settings from database:', data);

            // If we have a settings object in the response
            if (data && data.settings) {
                // Find the provider and model settings
                const providerSetting = data.settings["llm.provider"];
                const modelSetting = data.settings["llm.model"];
                const customEndpointUrlSetting = data.settings["llm.openai_endpoint.url"];
                const anthropicEndpointUrlSetting = data.settings["llm.anthropic_endpoint.url"];

                // Update provider dropdown if we have a valid provider
                if (providerSetting && modelProviderSelect) {
                    const providerValue = providerSetting.value.toUpperCase();
                    modelProviderSelect.setAttribute('data-initial-value', providerValue);
                    SafeLogger.log('Setting provider to:', providerValue);

                    // Find the matching option in the dropdown
                    const matchingOption = Array.from(modelProviderSelect.options).find(
                        option => option.value.toUpperCase() === providerValue
                    );

                    if (matchingOption && !matchingOption.disabled) {
                        SafeLogger.log('Found matching provider option:', matchingOption.value);
                        modelProviderSelect.value = matchingOption.value;
                        // Also save to localStorage
                        // Provider saved to DB: matchingOption.value);
                    } else if (!matchingOption) {
                        modelProviderSelect.value = '';
                        // No option matches the saved provider at all (e.g. it
                        // was removed from the provider list). Leave the
                        // selection cleared rather than fuzzy-matching to a
                        // different enabled provider by substring — that's the
                        // same silent-substitution pattern populateModelProviders()'s
                        // Rule 3 and the disabled-provider branch below both
                        // reject: the assignment is programmatic, so the change
                        // listener never fires and nothing gets persisted, and
                        // the model input is left holding a model for a
                        // provider the user never chose.
                        SafeLogger.warn(`No matching provider option found for '${providerValue}'`);
                    } else {
                        // Settings may have changed since the page was rendered.
                        modelProviderSelect.value = '';
                        // The saved provider is present but policy-blocked.
                        // Clear any older selection. activeProvider below still
                        // resolves to the configured provider, so its API
                        // key / URL fields stay visible and the user can
                        // fix the setting that caused the block.
                        SafeLogger.log('Configured provider is disabled by egress policy; leaving selection cleared:', providerValue);
                    }
                    modelProviderSelect.disabled = !providerSetting.editable;

                    const activeProvider = modelProviderSelect.value || providerValue;

                    // Display endpoint container if using custom endpoint
                    if (endpointContainer) {
                        endpointContainer.style.display =
                            activeProvider === 'OPENAI_ENDPOINT' ? 'block' : 'none';
                    }

                    // Display Anthropic endpoint container if using Anthropic endpoint
                    if (anthropicEndpointContainer) {
                        anthropicEndpointContainer.style.display =
                            activeProvider === 'ANTHROPIC_ENDPOINT' ? 'block' : 'none';
                    }

                    // Display Ollama URL container if using Ollama
                    if (ollamaUrlContainer) {
                        ollamaUrlContainer.style.display =
                            activeProvider === 'OLLAMA' ? 'block' : 'none';
                    }

                    // Display LM Studio URL container if using LMSTUDIO
                    if (lmstudioUrlContainer) {
                        lmstudioUrlContainer.style.display =
                            activeProvider === 'LMSTUDIO' ? 'block' : 'none';
                    }

                    // Display context window container for local providers
                    if (contextWindowContainer) {
                        contextWindowContainer.style.display = isLocalProvider(activeProvider) ? 'block' : 'none';
                    }

                    // Display API key containers based on provider
                    if (openaiApiKeyContainer) {
                        openaiApiKeyContainer.style.display = activeProvider === 'OPENAI' ? 'block' : 'none';
                    }
                    if (anthropicApiKeyContainer) {
                        anthropicApiKeyContainer.style.display = activeProvider === 'ANTHROPIC' ? 'block' : 'none';
                    }
                    if (googleApiKeyContainer) {
                        googleApiKeyContainer.style.display = activeProvider === 'GOOGLE' ? 'block' : 'none';
                    }
                    if (openrouterApiKeyContainer) {
                        openrouterApiKeyContainer.style.display = activeProvider === 'OPENROUTER' ? 'block' : 'none';
                    }
                    if (orcarouterApiKeyContainer) {
                        orcarouterApiKeyContainer.style.display = activeProvider === 'ORCAROUTER' ? 'block' : 'none';
                    }
                    if (atlascloudApiKeyContainer) {
                        atlascloudApiKeyContainer.style.display = activeProvider === 'ATLASCLOUD' ? 'block' : 'none';
                    }
                    if (qwenApiKeyContainer) {
                        qwenApiKeyContainer.style.display = activeProvider === 'QWEN' ? 'block' : 'none';
                    }
                    if (xaiApiKeyContainer) {
                        xaiApiKeyContainer.style.display = activeProvider === 'XAI' ? 'block' : 'none';
                    }
                    if (ionosApiKeyContainer) {
                        ionosApiKeyContainer.style.display = activeProvider === 'IONOS' ? 'block' : 'none';
                    }
                    if (openaiEndpointApiKeyContainer) {
                        openaiEndpointApiKeyContainer.style.display = activeProvider === 'OPENAI_ENDPOINT' ? 'block' : 'none';
                    }
                    if (anthropicEndpointApiKeyContainer) {
                        anthropicEndpointApiKeyContainer.style.display = activeProvider === 'ANTHROPIC_ENDPOINT' ? 'block' : 'none';
                    }
                    if (ollamaApiKeyContainer) {
                        ollamaApiKeyContainer.style.display = activeProvider === 'OLLAMA' ? 'block' : 'none';
                    }
                    if (lmstudioApiKeyContainer) {
                        lmstudioApiKeyContainer.style.display = activeProvider === 'LMSTUDIO' ? 'block' : 'none';
                    }
                }

                // Update the custom endpoint URl if we have one.
                if (customEndpointUrlSetting && customEndpointInput) {
                    const customEndpointUrlValue = customEndpointUrlSetting.value;
                    SafeLogger.log('Current endpoint URL:', customEndpointUrlValue);
                    customEndpointInput.value = customEndpointUrlValue;
                    customEndpointInput.disabled = !customEndpointUrlSetting.editable;
                }

                // Update the Anthropic endpoint URL if we have one.
                if (anthropicEndpointUrlSetting && anthropicEndpointUrlInput) {
                    const anthropicEndpointUrlValue = anthropicEndpointUrlSetting.value;
                    SafeLogger.log('Current Anthropic endpoint URL:', anthropicEndpointUrlValue);
                    anthropicEndpointUrlInput.value = anthropicEndpointUrlValue;
                    anthropicEndpointUrlInput.disabled = !anthropicEndpointUrlSetting.editable;
                }

                // Update the Ollama URL if we have one
                const ollamaUrlSetting = data.settings['llm.ollama.url'];
                if (ollamaUrlSetting && ollamaUrlInput) {
                    const ollamaUrlValue = ollamaUrlSetting.value;
                    SafeLogger.log('Current Ollama URL:', ollamaUrlValue);
                    ollamaUrlInput.value = ollamaUrlValue;
                    ollamaUrlInput.disabled = !ollamaUrlSetting.editable;
                }

                // Update the LM Studio URL if we have one
                const lmstudioUrlSetting = data.settings['llm.lmstudio.url'];
                if (lmstudioUrlSetting && lmstudioUrlInput) {
                    const lmstudioUrlValue = lmstudioUrlSetting.value;
                    lmstudioUrlInput.value = lmstudioUrlValue;
                    lmstudioUrlInput.disabled = !lmstudioUrlSetting.editable;
                }

                // Update the context window size if we have one
                const contextWindowSetting = data.settings['llm.local_context_window_size'];
                if (contextWindowSetting && contextWindowInput) {
                    const contextWindowValue = contextWindowSetting.value;
                    contextWindowInput.value = contextWindowValue;
                    contextWindowInput.disabled = !contextWindowSetting.editable;
                }

                // The FastAPI settings endpoint deliberately returns
                // "[REDACTED]" for stored secrets. Keep password controls
                // blank instead of rendering that transport sentinel (or any
                // accidentally unredacted value); the editable flag still
                // tells the user whether a replacement may be submitted.
                const openaiApiKeySetting = data.settings['llm.openai.api_key'];
                if (openaiApiKeySetting && openaiApiKeyInput) {
                    openaiApiKeyInput.value = '';
                    openaiApiKeyInput.disabled = !openaiApiKeySetting.editable;
                }

                const anthropicApiKeySetting = data.settings['llm.anthropic.api_key'];
                if (anthropicApiKeySetting && anthropicApiKeyInput) {
                    anthropicApiKeyInput.value = '';
                    anthropicApiKeyInput.disabled = !anthropicApiKeySetting.editable;
                }

                const googleApiKeySetting = data.settings['llm.google.api_key'];
                if (googleApiKeySetting && googleApiKeyInput) {
                    googleApiKeyInput.value = '';
                    googleApiKeyInput.disabled = !googleApiKeySetting.editable;
                }

                const openrouterApiKeySetting = data.settings['llm.openrouter.api_key'];
                if (openrouterApiKeySetting && openrouterApiKeyInput) {
                    openrouterApiKeyInput.value = '';
                    openrouterApiKeyInput.disabled = !openrouterApiKeySetting.editable;
                }

                const orcarouterApiKeySetting = data.settings['llm.orcarouter.api_key'];
                if (orcarouterApiKeySetting && orcarouterApiKeyInput) {
                    orcarouterApiKeyInput.value = '';
                    orcarouterApiKeyInput.disabled = !orcarouterApiKeySetting.editable;
                }

                const atlascloudApiKeySetting = data.settings['llm.atlascloud.api_key'];
                if (atlascloudApiKeySetting && atlascloudApiKeyInput) {
                    atlascloudApiKeyInput.value = '';
                    atlascloudApiKeyInput.disabled = !atlascloudApiKeySetting.editable;
                }

                const qwenApiKeySetting = data.settings['llm.qwen.api_key'];
                if (qwenApiKeySetting && qwenApiKeyInput) {
                    qwenApiKeyInput.value = '';
                    qwenApiKeyInput.disabled = !qwenApiKeySetting.editable;
                }

                const xaiApiKeySetting = data.settings['llm.xai.api_key'];
                if (xaiApiKeySetting && xaiApiKeyInput) {
                    xaiApiKeyInput.value = '';
                    xaiApiKeyInput.disabled = !xaiApiKeySetting.editable;
                }

                const ionosApiKeySetting = data.settings['llm.ionos.api_key'];
                if (ionosApiKeySetting && ionosApiKeyInput) {
                    ionosApiKeyInput.value = '';
                    ionosApiKeyInput.disabled = !ionosApiKeySetting.editable;
                }

                const openaiEndpointApiKeySetting = data.settings['llm.openai_endpoint.api_key'];
                if (openaiEndpointApiKeySetting && openaiEndpointApiKeyInput) {
                    openaiEndpointApiKeyInput.value = '';
                    openaiEndpointApiKeyInput.disabled = !openaiEndpointApiKeySetting.editable;
                }

                const anthropicEndpointApiKeySetting = data.settings['llm.anthropic_endpoint.api_key'];
                if (anthropicEndpointApiKeySetting && anthropicEndpointApiKeyInput) {
                    anthropicEndpointApiKeyInput.value = '';
                    anthropicEndpointApiKeyInput.disabled = !anthropicEndpointApiKeySetting.editable;
                }

                const ollamaApiKeySetting = data.settings['llm.ollama.api_key'];
                if (ollamaApiKeySetting && ollamaApiKeyInput) {
                    ollamaApiKeyInput.value = '';
                    ollamaApiKeyInput.disabled = !ollamaApiKeySetting.editable;
                }

                const lmstudioApiKeySetting = data.settings['llm.lmstudio.api_key'];
                if (lmstudioApiKeySetting && lmstudioApiKeyInput) {
                    lmstudioApiKeyInput.value = '';
                    lmstudioApiKeyInput.disabled = !lmstudioApiKeySetting.editable;
                }

                // Load model options based on the current provider
                const currentProvider = modelProviderSelect ? modelProviderSelect.value : 'OLLAMA';
                updateModelOptionsForProvider(currentProvider, false).then(() => {
                    // Update model selection if we have a valid model
                    if (modelSetting && modelInput) {
                        const modelValue = modelSetting.value;
                        SafeLogger.log('Setting model to:', modelValue);

                        // Save to localStorage
                        // Model saved to DB

                        // Find the model in our loaded options
                        const matchingModel = modelOptions.find(m =>
                            m.value === modelValue || m.id === modelValue
                        );

                        if (matchingModel) {
                            SafeLogger.log('Found matching model in options:', matchingModel);

                            // Set the input field value
                            modelInput.value = matchingModel.label || modelValue;
                            selectedModelValue = modelValue;

                            // Also update hidden input if it exists
                            const hiddenInput = document.getElementById('model_hidden');
                            if (hiddenInput) {
                                hiddenInput.value = modelValue;
                            }
                        } else {
                            // If no matching model found, just set the raw value
                            SafeLogger.warn(`No matching model found for '${modelValue}'`);
                            modelInput.value = modelValue;
                            selectedModelValue = modelValue;

                            // Also update hidden input if it exists
                            const hiddenInput = document.getElementById('model_hidden');
                            if (hiddenInput) {
                                hiddenInput.value = modelValue;
                            }
                        }
                        modelInput.disabled = !modelSetting.editable;
                    }
                });

                // Settings are authoritative, including a saved engine that
                // is no longer offered for topic searches (such as Wayback).
                const searchEngineSetting = data.settings["search.tool"];
                if (searchEngineSetting && searchEngineSetting.value && searchEngineInput) {
                    const engineValue = searchEngineSetting.value;
                    SafeLogger.log('Setting search engine to:', engineValue);
                    reconcileSearchEngineSelection(searchEngineOptions, engineValue);
                    searchEngineInput.disabled = !searchEngineSetting.editable;
                }


            }

            // Population done; the shared settle step runs in .finally below.
        })
        .catch(error => {
            SafeLogger.error('Error loading settings:', error);

            // Fallback to localStorage if database fetch fails
            fallbackToLocalStorageSettings();
        })
        .finally(() => {
            // Settle exactly once, whether the load succeeded or threw. Keeping
            // this here (instead of duplicated in .then and .catch) means a
            // throw inside onInitializationComplete() can't re-enter the chain
            // and decrement the counter a second time, which would leave
            // isInitializing stuck truthy and silently suppress settings
            // auto-save.
            numApiCallsPending--;
            isInitializing = (numApiCallsPending !== 0);
            SafeLogger.log('Settings load settled. isInitializing now:', isInitializing, 'pending calls:', numApiCallsPending);
            if (!isInitializing) onInitializationComplete();
        });
    }

    // Add a fallback function to use localStorage settings
    function fallbackToLocalStorageSettings() {
        // Settings are loaded from database, not localStorage
        const provider = null;
        const model = null;
        const searchEngine = null;

        SafeLogger.log('Falling back to localStorage settings:', { provider, model, searchEngine });

        if (provider && modelProviderSelect) {
            modelProviderSelect.value = provider;
            // Show/hide custom endpoint input if needed
            if (endpointContainer) {
                endpointContainer.style.display =
                    provider === 'OPENAI_ENDPOINT' ? 'block' : 'none';
            }
            // Show/hide Anthropic endpoint input if needed
            if (anthropicEndpointContainer) {
                anthropicEndpointContainer.style.display =
                    provider === 'ANTHROPIC_ENDPOINT' ? 'block' : 'none';
            }
            // Show/hide Ollama URL input if needed
            if (ollamaUrlContainer) {
                ollamaUrlContainer.style.display =
                    provider === 'OLLAMA' ? 'block' : 'none';
            }
            // Show/hide LM Studio URL input if needed
            if (lmstudioUrlContainer) {
                lmstudioUrlContainer.style.display =
                    provider === 'LMSTUDIO' ? 'block' : 'none';
            }
            // Show/hide context window for local providers
            if (contextWindowContainer) {
                contextWindowContainer.style.display = isLocalProvider(provider) ? 'block' : 'none';
            }
            // Show/hide API key containers based on provider
            if (openaiApiKeyContainer) {
                openaiApiKeyContainer.style.display = provider === 'OPENAI' ? 'block' : 'none';
            }
            if (anthropicApiKeyContainer) {
                anthropicApiKeyContainer.style.display = provider === 'ANTHROPIC' ? 'block' : 'none';
            }
            if (googleApiKeyContainer) {
                googleApiKeyContainer.style.display = provider === 'GOOGLE' ? 'block' : 'none';
            }
            if (openrouterApiKeyContainer) {
                openrouterApiKeyContainer.style.display = provider === 'OPENROUTER' ? 'block' : 'none';
            }
            if (atlascloudApiKeyContainer) {
                atlascloudApiKeyContainer.style.display = provider === 'ATLASCLOUD' ? 'block' : 'none';
            }
            if (qwenApiKeyContainer) {
                qwenApiKeyContainer.style.display = provider === 'QWEN' ? 'block' : 'none';
            }
            if (xaiApiKeyContainer) {
                xaiApiKeyContainer.style.display = provider === 'XAI' ? 'block' : 'none';
            }
            if (ionosApiKeyContainer) {
                ionosApiKeyContainer.style.display = provider === 'IONOS' ? 'block' : 'none';
            }
            if (openaiEndpointApiKeyContainer) {
                openaiEndpointApiKeyContainer.style.display = provider === 'OPENAI_ENDPOINT' ? 'block' : 'none';
            }
            if (anthropicEndpointApiKeyContainer) {
                anthropicEndpointApiKeyContainer.style.display = provider === 'ANTHROPIC_ENDPOINT' ? 'block' : 'none';
            }
            if (ollamaApiKeyContainer) {
                ollamaApiKeyContainer.style.display = provider === 'OLLAMA' ? 'block' : 'none';
            }
        }

        const currentProvider = modelProviderSelect ? modelProviderSelect.value : 'OLLAMA';
        updateModelOptionsForProvider(currentProvider, !model);

        if (model && modelInput) {
            const matchingModel = modelOptions.find(m => m.value === model);
            if (matchingModel) {
                modelInput.value = matchingModel.label;
            } else {
                modelInput.value = model;
            }
            selectedModelValue = model;

            // Update hidden input if it exists
            const hiddenInput = document.getElementById('model_hidden');
            if (hiddenInput) {
                hiddenInput.value = model;
            }
        }

        if (searchEngine && searchEngineInput) {
            const matchingEngine = searchEngineOptions.find(e => e.value === searchEngine);
            if (matchingEngine) {
                searchEngineInput.value = matchingEngine.label;
            } else {
                searchEngineInput.value = searchEngine;
            }
            selectedSearchEngineValue = searchEngine;

            // Update hidden input if it exists
            const hiddenInput = document.getElementById('search_engine_hidden');
            if (hiddenInput) {
                hiddenInput.value = searchEngine;
            }
        }
    }

    /**
     * Load model options from API or cache
     */
    let modelOptionsRequestGeneration = 0;
    let fullModelOptionsRequestGeneration = 0;
    let modelProviderOptionsGeneration = 0;
    const providerModelOptionsRequestGenerations = new Map();

    function loadModelOptions(forceRefresh = false, refreshProvider = null) {
        return new Promise((resolve) => {
            // Check in-memory cache first for plain loads (5-minute
            // expiration). A targeted refresh explicitly wants fresh
            // server data, so it bypasses the client cache like a full
            // force refresh does.
            if (!forceRefresh && !refreshProvider) {
                const cachedData = getCachedData(CACHE_KEYS.MODELS);
                if (cachedData) {
                    SafeLogger.log('Using cached model data');
                    resolve(cachedData);
                    return;
                }
            }

            // Reading the cache above deliberately does NOT advance the
            // generation: a plain cache hit must not cancel an in-flight
            // targeted/forced refresh (which owns the loading spinner
            // until it settles).
            const requestGeneration = ++modelOptionsRequestGeneration;
            const targetedProvider = !forceRefresh && refreshProvider
                ? refreshProvider.toUpperCase() : null;
            if (targetedProvider) {
                providerModelOptionsRequestGenerations.set(targetedProvider, requestGeneration);
            } else {
                fullModelOptionsRequestGeneration = requestGeneration;
            }
            // Independent targeted refreshes may both contribute models.
            // A newer request for the same provider, or a full/policy reload,
            // supersedes this provider's response instead.
            const isSuperseded = () => {
                if (targetedProvider) {
                    return providerModelOptionsRequestGenerations.get(targetedProvider) !== requestGeneration
                        || fullModelOptionsRequestGeneration > requestGeneration;
                }
                return requestGeneration !== fullModelOptionsRequestGeneration;
            };

            // Add loading class to parent
            if (modelInput && modelInput.parentNode) {
                modelInput.parentNode.classList.add('ldr-loading');
            }

            // Unwind a response that a newer request has superseded. Such a
            // response must not touch MODEL_PROVIDERS or the dropdown - that is
            // what the generation check is for - but it still has to settle its
            // caller without leaving the cache unset, or two concurrent chains
            // deadlock:
            //   * leave the cache holding an array. updateModelOptionsForProvider()
            //     re-enters loadModelOptions() whenever the cache is *null*
            //     (see the EMPTY_CACHE_DURATION note further down), and every
            //     entry bumps modelOptionsRequestGeneration - so two chains
            //     that both bail out with the cache still null invalidate each
            //     other forever: an unbounded request storm, a spinner that
            //     never clears and callers whose promises never settle.
            //     Caching [] briefly is the same guard the empty-result and
            //     API-failure paths below use. This write is the half that
            //     terminates the storm.
            //   * drop the 'ldr-loading' this call added. classList is a set,
            //     not a counter, so this clears the spinner outright rather
            //     than balancing an add: the indicator can go out slightly
            //     early while the newer request is still in flight. That is
            //     the deliberate trade - the newer chain may never render
            //     anything (a policy refresh that is itself superseded, a
            //     response that never arrives), and an unconditional remove
            //     is what keeps the spinner from being stranded then.
            //     loadSearchEngineOptions takes the opposite side of the same
            //     trade and keeps its class on a stale response.
            const resolveSupersededRequest = () => {
                if (modelInput && modelInput.parentNode) {
                    modelInput.parentNode.classList.remove('ldr-loading');
                }
                const cachedData = getCachedData(CACHE_KEYS.MODELS);
                if (cachedData) {
                    resolve(cachedData);
                    return;
                }
                SafeLogger.log('Superseded model response and no cache - caching empty model list briefly');
                cacheData(CACHE_KEYS.MODELS, []);
                resolve([]);
            };

            // Fetch from API if cache is invalid or refresh is forced.
            // Discovery calls providers with the user's stored keys and
            // rewrites the cached rows, so every load is a CSRF-protected
            // POST: a GET only reads the server's model cache (empty when
            // cold) and answers 405 with refresh_provider or
            // force_refresh=true. A plain POST reuses the server's fresh
            // cache and discovers only on a miss; forceRefresh re-discovers
            // every provider; refreshProvider targets a single provider's
            // catalog (used after its API key was saved).
            const params = new URLSearchParams();
            if (forceRefresh) {
                params.set('force_refresh', 'true');
            } else if (refreshProvider) {
                params.set('refresh_provider', refreshProvider);
            }
            const url = params.toString()
                ? `${URLS.SETTINGS_API.AVAILABLE_MODELS}?${params.toString()}`
                : URLS.SETTINGS_API.AVAILABLE_MODELS;
            const csrfToken = window.api?.getCsrfToken?.()
                || document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';
            const modelRequest = fetch(url, {
                method: 'POST', headers: { 'X-CSRFToken': csrfToken }
            });

            modelRequest
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`API error: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => {
                    if (isSuperseded()) {
                        resolveSupersededRequest();
                        return;
                    }
                    // Remove loading class
                    if (modelInput && modelInput.parentNode) {
                        modelInput.parentNode.classList.remove('ldr-loading');
                    }

                    if (data && data.providers) {
                        SafeLogger.log('Got model data from API:', data);

                        // Update MODEL_PROVIDERS from API if available
                        const updatesProviderOptions = data.provider_options
                            && requestGeneration > modelProviderOptionsGeneration;
                        if (updatesProviderOptions) {
                            MODEL_PROVIDERS = data.provider_options;
                            modelProviderOptionsGeneration = requestGeneration;
                            SafeLogger.log('Updated MODEL_PROVIDERS from API:', MODEL_PROVIDERS);
                        }

                        // Format the data for our dropdown and cache BEFORE
                        // populateModelProviders() runs. populateModelProviders
                        // calls updateModelOptionsForProvider() at the end of
                        // its body, and that helper falls back to a
                        // loadModelOptions() round-trip when the cache is
                        // empty — which, on a same-page refresh after the
                        // user toggled a policy, would be the very call we
                        // just made (waste) or, worse, a fresh force-refresh
                        // for a cloud provider that just got re-enabled.
                        const formattedModels = formatModelRefresh(
                            data, targetedProvider, requestGeneration
                        );
                        cacheData(CACHE_KEYS.MODELS, formattedModels);

                        // Re-populate the provider dropdown with new options
                        if (updatesProviderOptions) {
                            populateModelProviders();
                        } else if (modelProviderSelect) {
                            updateModelOptionsForProvider(modelProviderSelect.value, false);
                        }

                        resolve(formattedModels);
                    } else {
                        throw new Error('Invalid model data format');
                    }
                })
                .catch(error => {
                    if (isSuperseded()) {
                        resolveSupersededRequest();
                        return;
                    }
                    SafeLogger.error('Error loading models:', error.message || error);

                    // Remove loading class on error
                    if (modelInput && modelInput.parentNode) {
                        modelInput.parentNode.classList.remove('ldr-loading');
                    }

                    // Use cached data if available, even if expired
                    const cachedData = getCachedData(CACHE_KEYS.MODELS);
                    if (cachedData) {
                        SafeLogger.log('Using expired cached model data due to API error');
                        resolve(cachedData);
                    } else {
                        // No cache and API failed. Cache an empty list (short TTL,
                        // see getCachedData) rather than leaving the cache null:
                        // updateModelOptionsForProvider() treats a null cache as
                        // "not loaded yet" and reloads, so on a *persistent*
                        // failure (HTTP error, expired session, malformed
                        // response) it would otherwise recurse into an unbounded
                        // request loop. Caching [] bounds the retry to once per
                        // EMPTY_CACHE_DURATION — same guard as the empty-success
                        // path above.
                        SafeLogger.log('API failed and no cache available - caching empty model list briefly');
                        cacheData(CACHE_KEYS.MODELS, []);
                        resolve([]);
                    }
                });
        });
    }

    function formatModelRefresh(data, provider, requestGeneration) {
        const cachedModels = getCachedData(CACHE_KEYS.MODELS);
        const cachedProviders = {};
        if (cachedModels && cachedModels.length > 0) {
            // The server takes its other-provider snapshot before fetching the
            // target. Keep our other providers' models: that snapshot can be
            // older than a targeted response we have already applied.
            cachedModels.forEach(model => {
                if (model.isProvider || !model.provider) return;
                const key = `${model.provider.toLowerCase()}_models`;
                if (!cachedProviders[key]) cachedProviders[key] = [];
                cachedProviders[key].push(model);
            });
        }
        const providers = provider && cachedModels && cachedModels.length > 0
            ? cachedProviders : {...data.providers};
        if (provider) {
            const targetKey = `${provider.toLowerCase()}_models`;
            providers[targetKey] = data.providers[targetKey] || [];
        } else {
            // A full response can still contribute other providers after a
            // newer targeted request. Preserve that target's current models,
            // including an empty list, instead of replacing them with the
            // full request's older credential/configuration snapshot.
            providerModelOptionsRequestGenerations.forEach((generation, name) => {
                if (generation > requestGeneration) {
                    const key = `${name.toLowerCase()}_models`;
                    providers[key] = cachedProviders[key] || [];
                }
            });
        }

        // Only the newest response may update provider policy/options. An
        // older targeted response must never restore a now-disabled provider.
        const allowedProviders = new Set(MODEL_PROVIDERS
            .filter(option => !option.disabled)
            .map(option => option.value.toUpperCase()));
        Object.keys(providers).forEach(key => {
            if (!allowedProviders.has(key.replace(/_models$/, '').toUpperCase())) {
                delete providers[key];
            }
        });
        return formatModelsFromAPI({provider_options: MODEL_PROVIDERS, providers});
    }

    // Format models from API response
    function formatModelsFromAPI(data) {
        const formatted = [];

        // Process provider options
        if (data.provider_options) {
            data.provider_options.forEach(provider => {
                formatted.push({
                    ...provider,
                    isProvider: true // Flag to identify provider options
                });
            });
        }

        // Process all provider models dynamically
        if (data.providers) {
            // Create a new object to avoid race conditions
            const newAvailableModels = {};

            // Iterate through all providers in the response
            Object.keys(data.providers).forEach(providerKey => {
                // Extract provider name from key (e.g., 'ollama_models' -> 'OLLAMA')
                const providerName = providerKey.replace('_models', '').toUpperCase();

                // Initialize array for this provider
                if (!newAvailableModels[providerName]) {
                    newAvailableModels[providerName] = [];
                }

                // Process each model for this provider
                const models = data.providers[providerKey];
                if (Array.isArray(models)) {
                    models.forEach(model => {
                        const formattedModel = {
                            ...model,
                            id: model.value,
                            provider: model.provider || providerName
                        };
                        formatted.push(formattedModel);
                        newAvailableModels[providerName].push(formattedModel);
                    });
                }
            });

            // Atomically update the global variable
            availableModels = newAvailableModels;
            SafeLogger.log('Dynamically populated availableModels:', availableModels);
        }

        return formatted;
    }

    // In-memory cache to avoid excessive API calls within a session
    const memoryCache = {};
    const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
    // An empty list almost always means a failed/timed-out provider fetch, not a
    // real "zero results", so it should not linger for the full window. We give
    // empties a much shorter TTL rather than skipping the cache write entirely,
    // on purpose: updateModelOptionsForProvider() treats a *null* cache as "not
    // loaded yet" and reloads, so never caching an empty result would turn that
    // into an unbounded reload loop whenever a provider genuinely returns none.
    // Caching the [] (just briefly) keeps getCachedData returning an array, which
    // stops the loop, while still expiring fast so a transient empty self-heals.
    const EMPTY_CACHE_DURATION = 15 * 1000; // 15 seconds

    function cacheData(key, data) {
        memoryCache[key] = {
            data,
            timestamp: Date.now()
        };
    }

    function getCachedData(key) {
        const cached = memoryCache[key];
        if (!cached) {
            return null;
        }
        const isEmpty = Array.isArray(cached.data) && cached.data.length === 0;
        const maxAge = isEmpty ? EMPTY_CACHE_DURATION : CACHE_DURATION;
        if (Date.now() - cached.timestamp < maxAge) {
            return cached.data;
        }
        return null;
    }

    function invalidateCacheKey(key) {
        if (memoryCache[key]) {
            delete memoryCache[key];
            SafeLogger.log(`Cache invalidated for key: ${key}`);
        }
    }

    // Read the current egress scope for the dropdown's egress-aware
    // API call. Returns null when the form is in "no filtering"
    // mode (no scope set, or Unprotected — the escape hatch where
    // every engine is allowed). Mirrors the backend's
    // ``apply_egress_filter`` branch in
    // ``api_get_available_search_engines`` (issue #5204).
    function getCurrentEgressScopeForDropdown() {
        const sel = document.getElementById('policy_egress_scope');
        if (!sel) return null;
        const val = (sel.value || '').toLowerCase().trim();
        if (val === 'private_only' || val === 'public_only') return val;
        return null;
    }

    // The user's currently-selected primary engine. The hidden input is the
    // value that form submission actually sends, so it is authoritative if a
    // hydration/reconciliation path ever leaves it out of sync with the
    // in-memory dropdown selection.
    function getCurrentPrimaryForDropdown() {
        const hidden = document.getElementById('search_engine_hidden');
        if (hidden && hidden.value) return hidden.value;
        return selectedSearchEngineValue || '';
    }

    // Build the egress-aware URL for GET /api/available-search-engines.
    // Centralised so the initial load, the refresh button, and the
    // scope-change reapplier all ask the same question.
    function buildEgressAwareEnginesURL(scope, primary) {
        const base = URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES;
        try {
            const u = new URL(base, window.location.origin);
            u.searchParams.set('egress_scope', scope);
            if (primary) u.searchParams.set('primary', primary);
            return u.toString();
        } catch (_e) {
            // window.location.origin is missing in some test envs
            // (jsdom, happy-dom). Fall back to a relative URL with
            // manual query-string construction.
            const sep = base.indexOf('?') >= 0 ? '&' : '?';
            const parts = [`egress_scope=${encodeURIComponent(scope)}`];
            if (primary) parts.push(`primary=${encodeURIComponent(primary)}`);
            return base + sep + parts.join('&');
        }
    }

    // Re-fetch the search engine list under the current egress scope
    // and re-render the dropdown so disabled markers reflect the new
    // scope. The backend's precheck (web/routers/research.py::
    // _precheck_engine_policy) is the security guarantee; this is the
    // UX guarantee that keeps the user from having to submit to
    // discover the mismatch (issue #5204).
    //
    // Wire points:
    //   1. Initial load — called once after loadSearchEngineOptions
    //      resolves in initializeResearch / initializeDropdowns (the
    //      load already uses the right scope via the egress-aware URL,
    //      so this is a no-op for the initial pass; the function
    //      exists for the post-scope-change reapplier).
    //   2. Egress Scope change — called from the policy_egress_scope
    //      change listener (re-fetches with the new scope).
    //   3. search.tool change — called when the user picks a different
    //      engine.
    //
    // If the active scope renders the current selection disallowed (e.g.
    // switching to private_only with a public engine selected),
    // reconcileSearchEngineSelection updates the selection to an allowed
    // engine (searxng preferred / library fallback).
    function applyEgressScopeToEngines() {
        // The dropdown instance is registered globally by
        // setupCustomDropdown; refresh its options source + nudge a
        // re-render if the dropdown is currently open so disabled
        // markers appear without the user having to close + reopen.
        const apiURL = (() => {
            const scope = getCurrentEgressScopeForDropdown();
            const primary = getCurrentPrimaryForDropdown();
            return scope
                ? buildEgressAwareEnginesURL(scope, primary)
                : URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES;
        })();

        // Issue #5204 follow-up review: a rapid burst of scope/primary
        // changes fires overlapping fetches. Without cancellation, the
        // last response to arrive wins — and it may not match the
        // scope/primary currently selected. Track a per-instance
        // generation token + AbortController so superseded requests
        // are dropped before they mutate the dropdown's options.
        const token = ++applyEgressScopeSeq;
        if (applyEgressScopeController) {
            try {
                applyEgressScopeController.abort();
            } catch (_e) {
                // Defensive: abort() should never throw, but if it does
                // (some test envs stub AbortController) we still want
                // the new fetch to proceed.
            }
        }
        const controller = new AbortController();
        applyEgressScopeController = controller;

        if (searchEngineInput && searchEngineInput.parentNode) {
            searchEngineInput.parentNode.classList.add('ldr-loading');
        }

        return fetch(apiURL, { signal: controller.signal })
            .then((r) => (r.ok ? r.json() : null))
            .then((data) => {
                // If a newer call has started since this fetch began,
                // drop the response on the floor — the newer fetch's
                // URL represents the current scope/primary.
                if (token !== applyEgressScopeSeq) return;
                if (searchEngineInput && searchEngineInput.parentNode) {
                    searchEngineInput.parentNode.classList.remove('ldr-loading');
                }
                if (!data || !Array.isArray(data.engine_options)) return;
                // Reuse the shared mapEngineOption helper so the
                // disabled/disabled_reason mapping stays in one place.
                searchEngineOptions = data.engine_options.map(mapEngineOption);

                // If the dropdown is currently open, push the new
                // options through updateDropdownOptions so the user
                // sees the change without a close + reopen cycle. If
                // it's closed, the next open reads searchEngineOptions
                // via the registered getOptions() closure — no extra
                // work needed.
                if (searchEngineInput && window.updateDropdownOptions) {
                    try {
                        window.updateDropdownOptions(
                            searchEngineInput,
                            searchEngineOptions
                        );
                    } catch (_e) {
                        // Defensive: never let the scope reapplier
                        // break the dropdown. The next open will pick
                        // up the updated searchEngineOptions either
                        // way.
                    }
                }

                // A scope change can re-disable the previously selected
                // engine (e.g. a public primary now under
                // private_only). Switch the visible selection to a
                // pre-configured favorite (SearXNG, then "Search All
                // Collections") so the form can't submit a hidden
                // engine by default. UI-only — the saved search.tool
                // is not changed.
                reconcileSearchEngineSelection(searchEngineOptions);
            })
            .catch((err) => {
                // AbortError is expected when a newer call supersedes
                // this one — log it as debug, not as a failure.
                if (err && err.name === 'AbortError') {
                    SafeLogger.log(
                        'applyEgressScopeToEngines: superseded by newer request'
                    );
                    return;
                }
                // If this fetch was already superseded, ignore the
                // failure too — the newer fetch will report (or not)
                // for itself.
                if (token !== applyEgressScopeSeq) return;
                if (searchEngineInput && searchEngineInput.parentNode) {
                    searchEngineInput.parentNode.classList.remove('ldr-loading');
                }
                SafeLogger.log(
                    'applyEgressScopeToEngines: fetch failed; keeping existing list',
                    err && err.message
                );
            });
    }

    // Map a single engine option object from backend format to dropdown format
    function mapEngineOption(engine) {
        const egress = engine.egress;
        const isEgressDenied = !!egress && egress.allowed === false;
        // Tool-list availability controls automatic discovery, not an explicit
        // primary-engine choice. Only policy and required credentials block it.
        const isApiKeyMissing = engine.requires_api_key === true &&
            engine.has_api_key === false;
        const disabled = isEgressDenied || isApiKeyMissing;
        const disabledReason = isEgressDenied
            ? egressToDisabledReason(egress.reason, engine)
            : isApiKeyMissing
              ? 'API key required — configure it in Settings.'
              : null;
        return {
            value: engine.value || engine.id || '',
            label: engine.label || engine.name || engine.value || '',
            type: engine.type || 'search',
            is_favorite: engine.is_favorite || false,
            group_label: engine.group_label,
            group_order: engine.group_order,
            base_group_label: engine.base_group_label,
            base_group_order: engine.base_group_order,
            // Preserve credential and policy decisions across scope refreshes.
            requires_api_key: engine.requires_api_key,
            has_api_key: engine.has_api_key,
            egress,
            disabled,
            disabled_reason: disabledReason,
            egress_reason: egress ? egress.reason : null,
        };
    }

    // Map a backend ``egress: {allowed, reason}`` decision onto the
    // ``disabled`` / ``disabled_reason`` contract the custom dropdown
    // renders. Reason text mirrors the human language the backend's
    // ``denial_guidance`` uses (PR #5126) so the dropdown's inline
    // reason and the precheck's 400 message stay in sync.
    function egressToDisabledReason(reason, engine) {
        if (!reason) return null;
        const display = (engine && (engine.display_name || engine.label)) ||
                        (engine && engine.value) || 'engine';
        switch (reason) {
            case 'scope_mismatch_private_only':
                return `Blocked: not a local source under Private only`;
            case 'scope_mismatch_public_only':
                return `Blocked: local source under Public only`;
            case 'strict_not_primary':
                return `Blocked: only the primary search engine is allowed under Strict`;
            case 'unclassified':
            case 'engine_unknown':
                return `Blocked: ${display} is not recognized by the egress policy`;
            case 'engine_denied':
                return `Blocked: ${display} is denied by the egress policy`;
            case 'private_url_unapproved':
                // Deliberately not "see the warning banner": that banner is
                // suppressed once dismissed, and it is emitted from the same
                // DNS-free check as this overlay, so it stays silent on exactly
                // the URLs the overlay misses. Name the operator-side remedy.
                return `Disabled: ${display}'s private instance URL needs server-operator approval (env-only: allow private IPs, add the origin to search.private_engine_url_allowlist, or env-lock the engine's URL setting)`;
            default:
                return `Blocked by egress policy (${reason})`;
        }
    }

    // Pre-configured favorites used as the dropdown's default selection
    // whenever the current selection becomes invalid (e.g. a public
    // engine under a scope that excludes it). SearXNG is the preferred
    // default because it is the recommended general-purpose engine and
    // it has the broadest coverage; "Search All Collections" (the
    // library engine) is the local-only fallback so a run never starts
    // with an empty selection.
    const PREFERRED_DEFAULT_ENGINES = ['searxng', 'library'];

    // Pick the preferred default from a list of mapped engine options.
    // Returns the first option whose value matches a PREFERRED_DEFAULT
    // entry AND is not disabled, or null if no preferred default is
    // available. Order matters: SearXNG wins over the library engine
    // when both are eligible.
    function pickPreferredSearchEngine(options) {
        if (!Array.isArray(options)) return null;
        for (const value of PREFERRED_DEFAULT_ENGINES) {
            const match = options.find(
                (o) => o && o.value === value && !o.disabled
            );
            if (match) return match;
        }
        return null;
    }

    // Reuse the custom-model notice style for a saved engine that is no
    // longer configured. Keep this separate from transient submit alerts so
    // typing, an unsuccessful submit, or refreshing cannot dismiss it.
    function showSavedSearchEngineWarning(value = '') {
        unavailableSearchEngineValue = value;
        const input = searchEngineInput || document.getElementById('search_engine');
        let warning = document.getElementById('saved-search-engine-warning');
        if (!warning && value && input) {
            warning = document.createElement('div');
            warning.id = 'saved-search-engine-warning';
            warning.className = 'ldr-model-warning';
            warning.setAttribute('role', 'status');
            warning.textContent = 'The saved search engine is unavailable. Choose a configured search provider before starting research.';
            const parent = input.closest('.ldr-form-group, .form-group') || input.parentNode;
            if (parent) parent.appendChild(warning);
        }
        if (warning) {
            warning.style.display = value ? 'block' : 'none';
            if (input) {
                const descriptions = (input.getAttribute('aria-describedby') || '')
                    .split(/\s+/).filter(id => id && id !== warning.id);
                if (value) descriptions.push(warning.id);
                if (descriptions.length) {
                    input.setAttribute('aria-describedby', descriptions.join(' '));
                } else {
                    input.removeAttribute('aria-describedby');
                }
            }
        }
        if (value && advancedPanel && !advancedPanel.classList.contains('ldr-expanded')) {
            applyAdvancedOptionsState(true);
        }
    }

    // Preserve enabled selections and keep unlisted saved choices visible
    // with a notice until the user chooses a configured provider. A choice
    // removed or disabled by a policy/strategy refresh still uses the existing
    // preferred-default / first-enabled fallback. No settings are saved here.
    function reconcileSearchEngineSelection(options, savedValue = '') {
        const list = Array.isArray(options) ? options : searchEngineOptions;
        if (!Array.isArray(list)) return;

        const input = searchEngineInput || document.getElementById('search_engine');
        const hidden = document.getElementById('search_engine_hidden');
        // Initial options and settings can finish after the user has picked
        // a configured engine. Neither a stale template nor a late settings
        // GET may restore the old saved value over that explicit choice.
        const initial = !hasExplicitSearchEngineSelection && input
            ? input.getAttribute('data-initial-value') : '';
        const saved = hasExplicitSearchEngineSelection ? '' : savedValue;
        const current = saved || selectedSearchEngineValue || (hidden ? hidden.value : '') || initial;
        const currentOpt = current
            ? list.find((o) => o && (o.value === current || o.id === current))
            : null;

        const preserveSaved = saved || unavailableSearchEngineValue === current ||
            (!selectedSearchEngineValue && initial === current);
        if (current && !currentOpt && preserveSaved) {
            selectedSearchEngineValue = current;
            if (input) {
                input.value = current === 'wayback'
                    ? 'Wayback (saved; choose a configured search provider for topics)'
                    : `${current} (saved; unavailable)`;
            }
            if (hidden) hidden.value = current;
            showSavedSearchEngineWarning(current);
            return;
        }

        showSavedSearchEngineWarning();
        if (currentOpt && !currentOpt.disabled) {
            // The previously selected value is still visible/enabled;
            // keep it (rule 1). Keep inputs in sync.
            selectedSearchEngineValue = currentOpt.value;
            if (input) input.value = currentOpt.label || currentOpt.value;
            if (hidden) hidden.value = currentOpt.value;
            return;
        }

        const preferred = pickPreferredSearchEngine(list);
        const next = preferred || list.find((o) => o && !o.disabled) || null;

        if (!next) {
            // No selectable options at all — clear the selection so the
            // form can't submit a hidden/disabled engine. The form
            // submit precheck will surface the empty-engine error.
            SafeLogger.log(
                'reconcileSearchEngineSelection: no selectable options; clearing selection'
            );
            selectedSearchEngineValue = '';
            if (input) input.value = '';
            if (hidden) hidden.value = '';
            return;
        }

        const previous = current || '(unset)';
        SafeLogger.log(
            `reconcileSearchEngineSelection: ${previous} -> ${next.value} (preferred=${preferred ? 'yes' : 'fallback'})`
        );
        selectedSearchEngineValue = next.value;
        if (input) input.value = next.label || next.value;
        if (hidden) hidden.value = next.value;
    }

    // Load search engine options
    function loadSearchEngineOptions(forceRefresh = false) {
        return new Promise((resolve) => {
            // Issue #5204: when the active egress scope is set, ask the
            // endpoint to stamp a per-option egress decision onto each
            // entry so the dropdown can disable the ones that would be
            // refused at submit time. The unfiltered path (no scope
            // set) is unchanged — existing callers (settings page,
            // news-subscription form) keep using the same cached list.
            const egressScope = getCurrentEgressScopeForDropdown();
            const primary = getCurrentPrimaryForDropdown();
            const egressFiltered = !!egressScope;

            // Check in-memory cache first if not forcing refresh (5-minute expiration).
            // The cache only ever holds the UNFILTERED list: a scope-tagged
            // response is small and changes on every scope switch, so caching
            // it would just give us stale data on the next toggle.
            if (!egressFiltered && !forceRefresh) {
                const cachedData = getCachedData(CACHE_KEYS.SEARCH_ENGINES);
                if (cachedData) {
                    SafeLogger.log('Using cached search engine data');
                    searchEngineOptions = cachedData; // Ensure the global variable is updated
                    resolve(cachedData);
                    return;
                }
            }

            // Add loading class to parent
            if (searchEngineInput && searchEngineInput.parentNode) {
                searchEngineInput.parentNode.classList.add('ldr-loading');
            }

            SafeLogger.log('Fetching search engines from API...');

            // Build the egress-aware URL. Kept in one place so every
            // code path (initial load, refresh, scope-change reapplier)
            // asks for the same shape and the cache slot above is
            // consistent.
            const apiURL = egressFiltered
                ? buildEgressAwareEnginesURL(egressScope, primary)
                : URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES;

            // Thread the shared sequence token + AbortController so rapid interactions
            // don't race and a late load response cannot overwrite a newer scope-aware fetch.
            const token = ++applyEgressScopeSeq;
            if (applyEgressScopeController) {
                try {
                    applyEgressScopeController.abort();
                } catch (_e) {
                    // Defensive: abort() should never throw
                }
            }
            const controller = new AbortController();
            applyEgressScopeController = controller;

            // Fetch from API
            fetch(apiURL, { signal: controller.signal })
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`API error: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => {
                    if (token !== applyEgressScopeSeq) {
                        resolve(searchEngineOptions);
                        return;
                    }
                    // Remove loading class
                    if (searchEngineInput && searchEngineInput.parentNode) {
                        searchEngineInput.parentNode.classList.remove('ldr-loading');
                    }

                    // Log the entire response to debug
                    SafeLogger.log('Search engine API response:', data);

                    // Extract engines from the data based on the actual response format
                    let formattedEngines = [];

                    // Handle the case where API returns {engine_options, engines}
                    if (data && data.engine_options) {
                        SafeLogger.log('Processing engine_options:', data.engine_options.length + ' options');

                        // Map the engine options to our dropdown format. The
                        // group_* fields drive the band headers/ordering; the
                        // base_group_* fields let a favorite toggle move an
                        // engine back to its category band without re-fetching.
                        // An option may also carry an
                        // ``egress: {allowed, reason}`` field; we surface
                        // that as the disabled / disabled_reason contract
                        // the custom dropdown already understands. Full
                        // per-scope classification requires the
                        // egress_scope= opt-in (issue #5204), but the
                        // guarded engine-URL overlay is stamped on EVERY
                        // response — that guard ignores the egress scope,
                        // so an engine whose private instance URL the
                        // runtime will refuse must read as disabled here
                        // under the default ``adaptive`` scope too, where
                        // getCurrentEgressScopeForDropdown sends no scope.
                        formattedEngines = data.engine_options.map(mapEngineOption);
                    }
                    // Also try adding engines from engines object if it exists
                    if (data && data.engines) {
                        SafeLogger.log('Processing engines object:', Object.keys(data.engines).length + ' engine types');

                        // Handle each type of engine in the engines object
                        Object.keys(data.engines).forEach(engineType => {
                            const enginesOfType = data.engines[engineType];
                            if (Array.isArray(enginesOfType)) {
                                SafeLogger.log(`Processing ${engineType} engines:`, enginesOfType.length + ' engines');

                                // Map each engine to our dropdown format
                                const typeEngines = enginesOfType.map(engine => ({
                                    value: engine.value || engine.id || '',
                                    label: engine.label || engine.name || engine.value || '',
                                    type: engineType
                                }));

                                // Add to our formatted engines array
                                formattedEngines = [...formattedEngines, ...typeEngines];
                            }
                        });
                    }
                    // Handle classic format with search_engines array
                    else if (data && data.search_engines) {
                        SafeLogger.log('Processing search_engines array:', data.search_engines.length + ' engines');
                        formattedEngines = data.search_engines.map(engine => ({
                            value: engine.id || engine.value || '',
                            label: engine.name || engine.label || '',
                            type: engine.type || 'search'
                        }));
                    }
                    // Handle direct array format
                    else if (data && Array.isArray(data)) {
                        SafeLogger.log('Processing direct array:', data.length + ' engines');
                        formattedEngines = data.map(engine => ({
                            value: engine.id || engine.value || '',
                            label: engine.name || engine.label || '',
                            type: engine.type || 'search'
                        }));
                    }

                    SafeLogger.log('Final formatted search engines:', formattedEngines);

                    if (formattedEngines.length > 0) {
                        // Cache the data
                        cacheData(CACHE_KEYS.SEARCH_ENGINES, formattedEngines);

                        // Update global searchEngineOptions
                        searchEngineOptions = formattedEngines;

                        // Reconcile the visible selection against the
                        // freshly loaded options. On the very first
                        // load ``selectedSearchEngineValue`` is still
                        // empty, so this picks a pre-configured
                        // favorite (SearXNG, then library) as the
                        // visible default; setInitialFormValues will
                        // then overwrite with the saved primary if
                        // it is still in the list. The precheck
                        // reconciliation runs again in
                        // applyEgressScopeToEngines for the
                        // egress-aware shape on the first user-driven
                        // scope change.
                        reconcileSearchEngineSelection(searchEngineOptions);

                        resolve(formattedEngines);
                    } else {
                        throw new Error('No valid search engines found in API response');
                    }
                })
                .catch(error => {
                    if (error && error.name === 'AbortError') {
                        SafeLogger.log('loadSearchEngineOptions: superseded by newer request');
                        resolve(searchEngineOptions);
                        return;
                    }
                    if (token !== applyEgressScopeSeq) {
                        resolve(searchEngineOptions);
                        return;
                    }
                    SafeLogger.error('Error loading search engines:', error.message || error);

                    // Remove loading class on error
                    if (searchEngineInput && searchEngineInput.parentNode) {
                        searchEngineInput.parentNode.classList.remove('ldr-loading');
                    }

                    // Use cached data if available, even if expired
                    const cachedData = getCachedData(CACHE_KEYS.SEARCH_ENGINES);
                    if (cachedData) {
                        SafeLogger.log('Using expired cached search engine data due to API error');
                        searchEngineOptions = cachedData;
                        resolve(cachedData);
                    } else {
                        // No cache and API failed - return empty array
                        SafeLogger.log('API failed and no cache available - returning empty search engine list');
                        resolve([]);
                    }
                });
        });
    }

    // Save model settings to database
    function saveModelSettings(modelValue) {
        // Only save to database, not localStorage

        // Update any hidden input with the same settings key that might exist in other forms
        const hiddenInputs = document.querySelectorAll('input[id$="_hidden"][name="llm.model"]');
        hiddenInputs.forEach(input => {
            input.value = modelValue;
        });

        // Save to the database using the settings API
        return enqueueResearchSettingSave('llm.model', () => fetch(URLBuilder.updateSetting('llm.model'), {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': window.api ? window.api.getCsrfToken() : ''
            },
            body: JSON.stringify({ value: modelValue })
        })
        .then(async response => {
            const data = await response.json();
            if (!response.ok) {
                throw new Error(
                    data.error || data.detail || data.message || `HTTP ${response.status}`
                );
            }
            return data;
        })
        .then(data => {
            SafeLogger.log('Model setting saved to database:', data);

            // Optionally show a notification if there's UI notification support
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Model updated to: ${modelValue}`, 'success', 2000);
            }
        }))
        .catch(error => {
            SafeLogger.error('Error saving model setting to database:', error);

            // Show error notification if available
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Error updating model: ${error.message}`, 'error', 3000);
            }
        });
    }

    // Save search engine settings to database
    function saveSearchEngineSettings(engineValue) {
        // Only save to database, not localStorage

        // Update any hidden input with the same settings key that might exist in other forms
        const hiddenInputs = document.querySelectorAll('input[id$="_hidden"][name="search.tool"]');
        hiddenInputs.forEach(input => {
            input.value = engineValue;
        });

        // Save to the database using the settings API
        return enqueueResearchSettingSave('search.tool', () => fetch(URLS.SETTINGS_API.SEARCH_TOOL, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': window.api ? window.api.getCsrfToken() : ''
            },
            body: JSON.stringify({ value: engineValue })
        })
        .then(async response => {
            const data = await response.json();
            if (!response.ok) {
                throw new Error(
                    data.error || data.detail || data.message || `HTTP ${response.status}`
                );
            }
            return data;
        })
        .then(data => {
            SafeLogger.log('Search engine setting saved to database:', data);

            // Optionally show a notification
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Search engine updated to: ${engineValue}`, 'success', 2000);
            }
        }))
        .catch(error => {
            SafeLogger.error('Error saving search engine setting to database:', error);

            // Show error notification if available
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Error updating search engine: ${error.message}`, 'error', 3000);
            }
        });
    }

    // Handle toggling a search engine as favorite
    function handleSearchEngineFavoriteToggle(engineId, item, isFavorite) {
        SafeLogger.log(`Toggling favorite for ${engineId}: ${isFavorite}`);

        // Make API call to toggle favorite
        fetch(URLS.SETTINGS_API.SEARCH_FAVORITES_TOGGLE, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': window.api ? window.api.getCsrfToken() : ''
            },
            body: JSON.stringify({ engine_id: engineId })
        })
        .then(async response => {
            const data = await response.json();
            if (!response.ok) {
                throw new Error(
                    data.error || data.detail || data.message || `HTTP ${response.status}`
                );
            }
            return data;
        })
        .then(data => {
            SafeLogger.log('Favorite toggled:', data);

            if (data.error) {
                SafeLogger.error('Error toggling favorite:', data.error);
                if (window.ui && window.ui.showMessage) {
                    window.ui.showMessage(`Error: ${data.error}`, 'error', 3000);
                }
                return;
            }

            // Update the local options with new favorite status. A starred
            // engine moves to the Favorites band; un-starring returns it to its
            // category band (base_group_*). Favorites is the top band, mirroring
            // engine_groups.SEARCH_ENGINE_GROUPS[0] on the server.
            const updatedFavorites = data.favorites || [];
            const FAVORITES_BAND = { label: 'Favorites', order: 0 };
            searchEngineOptions = searchEngineOptions.map(engine => {
                const isFav = updatedFavorites.includes(engine.value);
                return {
                    ...engine,
                    is_favorite: isFav,
                    group_label: isFav ? FAVORITES_BAND.label : engine.base_group_label,
                    group_order: isFav ? FAVORITES_BAND.order : engine.base_group_order
                };
            });

            // Re-sort by band order (favorites first), then alphabetically
            // within each band. group_order can be 0, so don't treat it as falsy.
            const bandOrder = (engine) => {
                return typeof engine.group_order === 'number' ? engine.group_order : 999;
            };
            searchEngineOptions.sort((a, b) =>
                (bandOrder(a) - bandOrder(b)) ||
                (a.label || '').localeCompare(b.label || '')
            );

            // Invalidate cache so next dropdown open gets fresh data
            invalidateCacheKey(CACHE_KEYS.SEARCH_ENGINES);

            // Show success message
            const action = data.is_favorite ? 'added to' : 'removed from';
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Search engine ${action} favorites`, 'success', 2000);
            }
        })
        .catch(error => {
            SafeLogger.error('Error toggling search engine favorite:', error);
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Error updating favorites: ${error.message}`, 'error', 3000);
            }
        });
    }

    // Save provider setting to database
    function saveProviderSetting(providerValue) {
        // Only save to database, not localStorage

        // Update any hidden input with the same settings key that might exist in other forms
        const hiddenInputs = document.querySelectorAll('input[id$="_hidden"][name="llm.provider"]');
        hiddenInputs.forEach(input => {
            input.value = providerValue;
        });

        // Save to the database using the settings API
        return enqueueResearchSettingSave('llm.provider', () => fetch(URLS.SETTINGS_API.LLM_PROVIDER, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': window.api ? window.api.getCsrfToken() : ''
            },
            body: JSON.stringify({ value: providerValue.toLowerCase() })
        })
        .then(async response => {
            const data = await response.json();
            if (!response.ok) {
                throw new Error(
                    data.error || data.detail || data.message || `HTTP ${response.status}`
                );
            }
            return data;
        })
        .then(data => {
            SafeLogger.log('Provider setting saved to database:', data);

            // If the response includes warnings, display them directly
            if (data.warnings && typeof window.displayWarnings === 'function') {
                window.displayWarnings(data.warnings);
            } else if (typeof window.refetchSettingsAndUpdateWarnings === 'function') {
                // Fallback: trigger warning system update
                window.refetchSettingsAndUpdateWarnings();
            }

            // Optionally show a notification
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Provider updated to: ${providerValue}`, 'success', 2000);
            }
        }))
        .catch(error => {
            SafeLogger.error('Error saving provider setting to database:', error);

            // Show error notification if available
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Error updating provider: ${error.message}`, 'error', 3000);
            }
        });
    }

    // Save search setting to database
    /**
     * Reflect the chosen egress scope visually: swap the panel's data-scope
     * (CSS handles the border/background/header color) and the header icon.
     * Icon mapping mirrors the dropdown semantics — globe = "anywhere",
     * cloud = "data going out", house = "stays home", target = "one tool only".
     */
    function applyPrivacyPanelScope(scope) {
        const normalized = scope || 'adaptive';
        const panel = document.querySelector('.ldr-privacy-panel');
        if (panel) panel.setAttribute('data-scope', normalized);
        // Also stamp the body so CSS can escalate the cue to the research card
        // and query textarea (see <style> block in research.html).
        if (document.body) document.body.dataset.scope = normalized;

        // Auto-check + lock the local-inference toggles under "Private only",
        // so the form matches what the backend actually enforces
        // (context_from_snapshot forces require_local_llm/embeddings under
        // PRIVATE_ONLY) and matches docs/egress-modes.md. Visual only — the
        // saved scope is what the backend couples; we restore the user's stored
        // preference when they leave Private only. (Mirrors applyEgressScopeLock
        // on the settings page.)
        const LOCK_TITLE = 'Forced on by the Private-only egress scope — local inference is required so data stays on this machine.';
        const scopeLocked = normalized === 'private_only';
        ["llm_require_local_endpoint", "embeddings_require_local"].forEach((id) => {
            const cb = document.getElementById(id);
            if (!cb) return;
            const envLocked = cb.dataset.envLocked === "true";
            const envValue = cb.dataset.envValue === "true";
            const envTitle = cb.dataset.envTitle || "Locked by the server operator";
            if (scopeLocked) {
                if (!envLocked && !cb.dataset.userCheckedSaved) {
                    cb.dataset.userChecked = cb.checked ? "1" : "0";
                    cb.dataset.userCheckedSaved = "1";
                }
                cb.checked = true;
                cb.disabled = true;
                cb.title = LOCK_TITLE;
            } else if (envLocked) {
                cb.checked = envValue;
                cb.disabled = true;
                cb.title = envTitle;
            } else {
                if (cb.dataset.userCheckedSaved) {
                    cb.checked = cb.dataset.userChecked === "1";
                    delete cb.dataset.userChecked;
                    delete cb.dataset.userCheckedSaved;
                }
                cb.disabled = false;
                cb.title = "";
            }
        });

        const icon = document.getElementById('ldr-privacy-panel-icon');
        if (!icon) return;
        const iconClassByScope = {
            adaptive: 'fas fa-shield-alt',
            public_only: 'fas fa-cloud',
            private_only: 'fas fa-home',
            strict: 'fas fa-bullseye',
            unprotected: 'fas fa-unlock',
        };
        icon.className = iconClassByScope[normalized] || iconClassByScope.adaptive;
    }

    function refreshPolicyScopeFromServer(select) {
        select.disabled = true;
        const settingKey = "policy.egress_scope";
        fetch(`/settings/api/${settingKey}`)
            .then(async response => {
                const data = await response.json();
                if (!response.ok) throw new Error(data.error || "Refresh failed");
                return data;
            })
            .then(data => {
                const effectiveValue = data.value || "adaptive";
                if (select.querySelector(`option[value="${effectiveValue}"]`)) {
                    select.value = effectiveValue;
                } else {
                    select.value = "adaptive";
                }
                select.dataset.savedValue = select.value;
                select.disabled = data.editable === false;
                applyPrivacyPanelScope(select.value);
                // Issue #5204: the revert changed the form's effective
                // scope without firing a change event, so re-mirror
                // the search-engine dropdown's disabled set too.
                if (typeof applyEgressScopeToEngines === 'function') {
                    applyEgressScopeToEngines();
                }
            })
            .catch(error => {
                SafeLogger.error("Unable to refresh effective egress scope:", error);
                window.location.reload();
            });
    }

    // Setting keys whose values are secrets and must never be echoed in a
    // toast (case-insensitive; matches the last key segment's suffix).
    const SECRET_SETTING_KEY_RE = /(?:api_key|token|secret|password)$/i;
    // Endpoint URL settings can embed credentials (user:pw@host or
    // ?api_key=...), so their toasts omit the value too.
    const URL_SETTING_KEY_RE = /\.url$/i;

    function saveSearchSetting(settingKey, value, onFailure = null, onSuccess = null) {
        return enqueueResearchSettingSave(settingKey, () => fetch(`/settings/api/${settingKey}`, {
            method: "PUT",
            headers: {
                "Content-Type": "application/json",
                "X-CSRFToken": window.api ? window.api.getCsrfToken() : ""
            },
            body: JSON.stringify({ value })
        })
        .then(async response => {
            const data = await response.json();
            if (!response.ok) {
                throw new Error(
                    data.error || data.detail || data.message ||
                    `Request failed (${response.status})`
                );
            }
            return data;
        })
        .then(data => {
            SafeLogger.log(`Search setting ${settingKey} saved to database:`, data);
            if (data.warnings && typeof window.displayWarnings === "function") {
                window.displayWarnings(data.warnings);
            }
            if (typeof onSuccess === "function") onSuccess(data);
            const parts = settingKey.split(".");
            const isApiKey = settingKey.endsWith("api_key");
            // The PUT response carries no ui_element, so classify secrets
            // by key name: any *api_key / *token / *secret / *password
            // setting (the password-typed inputs this page saves).
            const isSecret = SECRET_SETTING_KEY_RE.test(settingKey);
            const hideValue = isSecret || URL_SETTING_KEY_RE.test(settingKey);
            if (window.ui && window.ui.showMessage) {
                // Never echo secret or endpoint URL values on screen —
                // show a neutral confirmation instead of the raw value,
                // keeping the provider segment for identifiability
                // ("qwen api_key updated", "ollama url updated").
                const label = hideValue
                    ? `${parts.length > 2 ? `${parts[parts.length - 2]} ` : ""}${parts[parts.length - 1]}`
                    : parts[parts.length - 1];
                const detail = hideValue ? "updated" : `updated to: ${value}`;
                window.ui.showMessage(`${label} ${detail}`, "success", 2000);
            }
            // A saved provider API key changes what that provider's model
            // dropdown can offer: a missing or wrong key previously left
            // it undiscovered, and the server caches model lists for up
            // to 24h. Refresh just that provider's catalog so its models
            // appear at once (single-provider fetch, keeps other
            // providers' caches intact).
            if (isApiKey && parts.length > 2) {
                loadModelOptions(false, parts[parts.length - 2]);
            }
        }))
        .catch(error => {
            SafeLogger.error(`Error saving search setting ${settingKey} to database:`, error);
            if (typeof onFailure === "function") onFailure(error);
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage(`Error updating ${settingKey}: ${error.message}`, "error", 3000);
            }
        });
    }

    // Research form submission handler
    function handleResearchSubmit(event) {
        event.preventDefault();

        if (activeResearchSubmit !== null) {
            return;
        }

        SafeLogger.log('Research form submitted');

        // Clear any alerts this submit handler previously produced — both
        // the egress-specific error UX and any generic submission error
        // (e.g. a network failure from the last attempt). Clearing at the
        // very start of submit means early validation returns (missing
        // query, missing model) also wipe the stale message instead of
        // letting it survive and appear to describe the new attempt.
        clearSubmitAlerts();

        // Determine the research mode up front so validation can be mode-aware.
        const selectedModeRadio = document.querySelector('input[name="research_mode"]:checked');
        const mode = selectedModeRadio ? selectedModeRadio.value : 'quick';

        // Validate the query BEFORE any UI changes
        const query = queryInput.value.trim();
        if (!query) {
            if (researchValidator) {
                researchValidator.validateAll();
                queryInput.focus();
            } else {
                showAlert('Please enter a research query.', 'error');
            }
            return;
        }

        // Validate that a model has been selected/entered BEFORE any UI changes.
        // Chat mode creates its session server-side and never sends a model from
        // this form (only the query), so the model field is required for the
        // research modes only.
        if (mode !== 'chat') {
            const modelHidden = document.querySelector('#model_hidden');
            const modelValue = modelHidden ? modelHidden.value.trim() : '';
            if (!modelValue) {
                // The model field lives inside the Advanced Options panel, which
                // may be collapsed. Expand it first so the inline error and the
                // focus below are actually visible instead of being hidden in a
                // display:none subtree (the #query field is always visible, so
                // its guard doesn't need this).
                if (advancedPanel && !advancedPanel.classList.contains('ldr-expanded')) {
                    applyAdvancedOptionsState(true);
                }
                if (researchValidator && modelInput) {
                    researchValidator.validateField(modelInput);
                    modelInput.focus();
                } else {
                    showAlert('Please select or enter a model.', 'error');
                }
                return;
            }

            if (initialSearchEngineOptionsPending) {
                showAlert('Search providers are still loading. Please wait before starting research.', 'info');
                const loadingAlert = document.querySelector('#research-alert .alert[data-submit-alert="true"]');
                if (loadingAlert) loadingAlert.setAttribute('data-search-engine-loading', 'true');
                return;
            }

            // A saved-but-unlisted engine must never be silently replaced or
            // submitted. Filtering the dropdown is not a provider selection.
            if (unavailableSearchEngineValue) {
                showSavedSearchEngineWarning(unavailableSearchEngineValue);
                if (searchEngineInput) searchEngineInput.focus();
                return;
            }
        }

        // Clear any previous validation errors
        if (researchValidator) {
            researchValidator.clearErrors();
        }

        const submitOwnership = {};
        activeResearchSubmit = submitOwnership;

        // Disable the submit button to prevent multiple submissions
        startBtn.disabled = true;

      // Use centralized security utilities for button update
        window.safeUpdateButton(startBtn, 'fa-spinner', ' Starting...', true);

        // Show loading overlay for better feedback using centralized utility
        const loadingOverlay = window.createSafeLoadingOverlay({
            title: 'Preparing your research...',
            description: 'Securing settings and initializing search engines'
        });
        loadingOverlay.style.cssText = `
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.85);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 9999;
            color: white;
        `;
        document.body.appendChild(loadingOverlay);

        const finishResearchSubmit = () => {
            if (activeResearchSubmit !== submitOwnership) {
                return;
            }

            activeResearchSubmit = null;
            startBtn.disabled = false;
            window.safeUpdateButton(startBtn, 'fa-rocket', ' Start Research');
            loadingOverlay.remove();
        };

        // Handle Chat Mode - submit the user's message before opening its chat.
        // (mode was resolved up front, before the validation guards.)
        if (mode === 'chat') {
            if (!query) {
                showAlert('Please enter a research query.', 'error');
                finishResearchSubmit();
                return;
            }

            // Keep both writes bound to the login that rendered this page.
            // This helper only repeats a middleware-rejected CSRF request;
            // it never retries a message whose handler may have run.
            const sendChatRequest = window.api?.fetchWithCsrfRecovery;
            if (typeof sendChatRequest !== 'function') {
                showAlert('Failed to start chat. Reload this page and try again.', 'error');
                finishResearchSubmit();
                return;
            }
            let createdChatId = null;
            let messageAttempted = false;
            const postChat = async (url, body, fallbackMessage) => {
                const response = await sendChatRequest(url, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': window.api.getCsrfToken(),
                    },
                    body: JSON.stringify(body),
                });
                const data = await response.json().catch(() => null);
                if (!response.ok || !data?.success) {
                    const message = nonBlankString(data?.detail)
                        ? data.detail
                        : nonBlankString(data?.error)
                          ? data.error
                          : nonBlankString(data?.message)
                            ? data.message
                            : fallbackMessage;
                    const error = new Error(message);
                    error.status = response.status;
                    throw error;
                }
                return data;
            };
            postChat('/api/chat/sessions', { initial_query: query }, 'Failed to create chat session')
            .then(async data => {
                if (typeof data.session_id !== 'string' || !data.session_id) {
                    throw new Error('Failed to create chat session');
                }
                const id = encodeURIComponent(data.session_id);
                createdChatId = data.session_id;
                messageAttempted = true;
                await postChat(`/api/chat/sessions/${id}/messages`, {
                    content: query,
                    trigger_research: true,
                }, 'Failed to send chat message');
                // The message and any running research now live in encrypted
                // storage. chat.js restores them without sending again. No
                // query is staged in browser storage or the navigation URL.
                window.location.href = `/chat/${id}`;
            })
            .catch(error => {
                SafeLogger.error('Error starting chat:', error);
                showAlert('Failed to start chat: ' + error.message, 'error');
                if (messageAttempted && createdChatId
                    && (!error.status || error.status < 400 || error.status >= 500)) {
                    // A lost response can hide a committed message. Offer a
                    // read-only recovery instead of automatically posting again.
                    const recovery = document.createElement('p');
                    recovery.append('Your chat may have started. ');
                    const link = document.createElement('a');
                    link.href = `/chat/${encodeURIComponent(createdChatId)}`;
                    link.textContent = 'Open chat';
                    recovery.append(link, ' to check before trying again.');
                    document.getElementById('research-alert')?.appendChild(recovery);
                }
            })
            .finally(finishResearchSubmit);
            return;
        }

        // Get values from form fields (query already read above)
        // A policy refresh can clear the visible selection while its
        // provider save is still pending. Send the remembered choice so
        // the backend checks that provider instead of an older DB default.
        const modelProvider = modelProviderSelect
            ? (modelProviderSelect.value || modelProviderSelect.getAttribute('data-initial-value') || '')
            : '';

        // Get values from hidden inputs for custom dropdowns
        const model = document.querySelector('#model_hidden') ?
                     document.querySelector('#model_hidden').value : '';
        const searchEngine = document.querySelector('#search_engine_hidden') ?
                           document.querySelector('#search_engine_hidden').value : '';

        // Get other form values
        const customEndpoint = customEndpointInput ? customEndpointInput.value : '';
        const ollamaUrl = ollamaUrlInput ? ollamaUrlInput.value : '';
        const enableNotifications = notificationToggle ? notificationToggle.checked : true;

        // Get strategy value
        const strategySelect = document.getElementById('strategy');
        const strategy = strategySelect ? strategySelect.value : 'source-based';

        // Get iterations and questions per iteration
        const iterationsInput = document.getElementById('iterations');
        const iterations = iterationsInput ? parseInt(iterationsInput.value, 10) : 2;
        const questionsInput = document.getElementById('questions_per_iteration');
        const questionsPerIteration = questionsInput ? parseInt(questionsInput.value, 10) : 3;

        // Egress policy form fields (per-research override, not saved
        // to settings). Omit disabled controls: the server derives operator
        // locks and Private-only locality from the effective scope/environment.
        const policyScopeEl = document.getElementById("policy_egress_scope");
        const llmLocalEl = document.getElementById("llm_require_local_endpoint");
        const embLocalEl = document.getElementById("embeddings_require_local");
        const policyEgressScope = policyScopeEl && !policyScopeEl.disabled ? policyScopeEl.value : null;
        const llmRequireLocalEndpoint = llmLocalEl && !llmLocalEl.disabled ? !!llmLocalEl.checked : null;
        const embeddingsRequireLocal = embLocalEl && !embLocalEl.disabled ? !!embLocalEl.checked : null;

        // Prepare the data for submission
        const formData = {
            query,
            mode,
            model_provider: modelProvider,
            model,
            custom_endpoint: customEndpoint,
            ollama_url: ollamaUrl,
            search_engine: searchEngine,
            strategy,
            iterations,
            questions_per_iteration: questionsPerIteration
        };
        if (policyEgressScope !== null && policyEgressScope !== '') {
            formData.policy_egress_scope = policyEgressScope;
        }
        if (llmRequireLocalEndpoint !== null) {
            formData.llm_require_local_endpoint = llmRequireLocalEndpoint;
        }
        if (embeddingsRequireLocal !== null) {
            formData.embeddings_require_local = embeddingsRequireLocal;
        }

        SafeLogger.log('Submitting research with data:', formData);

        // Get CSRF token
        const csrfToken = window.api ? window.api.getCsrfToken() : '';

        // Submit the form data to the backend
        fetch(URLS.API.START_RESEARCH, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken
            },
            body: JSON.stringify(formData)
        })
        .then(async response => ({
            response,
            data: await response.json()
        }))
        .then(({ response, data }) => {
            try {
                const researchId = nonBlankString(data && data.research_id)
                    ? data.research_id.trim()
                    : null;
                const hasSuccessEnvelope = (
                    response.ok &&
                    data &&
                    (data.status === 'success' || data.status === window.RESEARCH_STATUS.QUEUED) &&
                    researchId !== null
                );

                if (hasSuccessEnvelope) {
                    SafeLogger.log('Research started:', data);

                    if (data.status === window.RESEARCH_STATUS.QUEUED) {
                        showAlert(data.message || 'Your research has been queued.', 'info');
                    }

                    // Store research preferences in localStorage
                    // Settings are saved to database via the API, not localStorage

                    // Redirect to the progress page
                    // URLBuilder produces /progress/{uuid}
                    // bearer:disable javascript_lang_open_redirect
                    window.location.href = URLBuilder.progressPage(researchId);
                } else {
                    // Show error message — anchor near submit + flag the
                    // offending field (if the server told us which one).
                    // showFormError is the belt-and-suspenders version:
                    // bottom-of-form alert that won't scroll off-screen
                    // + inline .ldr-field-error on the named field.
                    // We ALSO call showAlert for the top-of-form alert (now
                    // scrollIntoView'd), since the user's symptom was
                    // "nothing happens" — redundancy is the point.
                    //
                    // Treat blank / whitespace-only strings as absent so a
                    // malformed ``{}``-shaped body can't render an empty
                    // alert (which would be invisible to the user and
                    // look like the original "click submit and nothing
                    // happens" symptom).
                    const message = nonBlankString(data && data.message)
                        ? data.message
                        : nonBlankString(data && data.detail)
                          ? data.detail
                          : 'Failed to start research.';
                    showAlert(message, 'error');
                    if (typeof showFormError === 'function') {
                        showFormError(message, nonBlankString(data && data.field)
                            ? data.field : null);
                    }
                }
            } finally {
                // Always re-enable the button and tear down the loading
                // overlay, even if a malformed response (non-object data,
                // missing status, etc.) would otherwise throw partway
                // through. The user must never be left with a permanently
                // disabled submit button and a stuck overlay.
                finishResearchSubmit();
            }
        })
        .catch(error => {
            SafeLogger.error('Error starting research:', error);

            // Network/parse failure — no field hint available, so just
            // surface the generic message in both alert slots.
            const message = 'An error occurred while starting research. Please try again.';
            showAlert(message, 'error');
            if (typeof showFormError === 'function') {
                showFormError(message, null);
            }

            finishResearchSubmit();
        });
    }

    /**
     * Show an alert message
     * @param {string} message - The message to show
     * @param {string} type - The alert type (success, error, warning, info)
     */
    function showAlert(message, type = 'info') {
        // Use centralized security utility for alerts with auto-hide functionality
        // Keep the live parent in the accessibility tree before its
        // passive child is appended so assistive technology observes the
        // content change inside an already-exposed alert region.
        const alertContainer = document.getElementById('research-alert');
        if (alertContainer) {
            alertContainer.style.display = 'block';
        }
        window.showSafeAlert(
            'research-alert',
            message,
            type,
            { announce: false }
        );
        // Mark the alert as submit-owned so clearSubmitAlerts() can
        // remove it on the next attempt or when the user changes the
        // implicated setting. Unrelated danger alerts (e.g. a settings
        // page warning) are not tagged and therefore survive.
        if (alertContainer) {
            const alertEl = alertContainer.querySelector('.alert');
            if (alertEl) {
                alertEl.setAttribute('data-submit-alert', 'true');
            }
            try {
                alertContainer.scrollIntoView({ block: 'center', behavior: 'smooth' });
            } catch (_e) { /* old browsers */ }
        }

        // Errors don't auto-hide — they're actionable and the user may
        // need time to read them, scroll up to context, or change a
        // setting. info/success/warning still auto-hide (e.g. the queued
        // notification, the rerun hint).
        if (type === 'error') return;

        if (alertContainer && alertContainer.firstChild) {
            const alert = alertContainer.firstChild;
            setTimeout(() => {
                if (alertContainer.contains(alert)) {
                    alert.remove();
                    if (alertContainer.children.length === 0) {
                        alertContainer.style.display = 'none';
                    }
                }
            }, 5000);
        }
    }

    /**
     * Show a form-submission error that's impossible to miss:
     *  1. Anchors a copy of the message directly above the submit button
     *     (the user's natural focal point — see the "click Start Research
     *     and nothing happens" bug where the top-of-form alert was
     *     off-screen and got auto-dismissed).
     *  2. Scrolls that copy into view, so it's visible even if the user
     *     has scrolled the long form somewhere unusual.
     *  3. If a field name is supplied, mirrors the message inline next to
     *     that field using the existing FormValidator/.ldr-field-error
     *     convention so the offending input gets a red border too.
     *  4. Never auto-hides — errors need to stick around until the user
     *     acts or re-submits.
     *
     * The anchored copy is a visual duplicate of the top-of-form
     * #research-alert (which is the live ``role="alert"`` announcement).
     * The child .alert element has its ``role``/``aria-atomic``
     * attributes stripped, and the parent #research-error-alert has no
     * ``role``/``aria-live`` either. Field-specific inline copies are also
     * rendered without a live-region attribute because the top alert has
     * already announced the same message.
     *
     * @param {string} message - The user-facing error text.
     * @param {string|null} fieldName - Optional form field id to flag
     *     inline (e.g. "policy_egress_scope").
     */
    function showFormError(message, fieldName) {
        // Anchor near the submit button (always visible — it's the last
        // thing in the form, so scrolling can't push it off-screen
        // before the user clicks submit again).
        const errorContainer = document.getElementById('research-error-alert');
        if (errorContainer) {
            window.showSafeAlert(
                'research-error-alert',
                message,
                'error',
                { announce: false }
            );
            const alertEl = errorContainer.querySelector('.alert');
            if (alertEl) {
                // Mark as submit-owned so clearSubmitAlerts() can
                // remove it on the next attempt or when the user
                // changes the implicated setting. Unrelated alerts
                // (none today, but a safe default) are not tagged.
                alertEl.setAttribute('data-submit-alert', 'true');
            }
            // Only scroll if the user wouldn't already see it. With a
            // reasonably-sized viewport, scrolling on every error is
            // disorienting, so prefer `nearest` and only nudge when the
            // container is fully out of view.
            try {
                const r = errorContainer.getBoundingClientRect();
                const fullyOff = r.bottom < 0 || r.top > window.innerHeight;
                if (fullyOff) {
                    errorContainer.scrollIntoView({ block: 'center', behavior: 'smooth' });
                } else {
                    // Nudge into the safe middle band even if partially visible.
                    errorContainer.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
                }
            } catch (_e) { /* old browsers */ }
        }

        // Inline field error (matches the FormValidator convention used by
        // the query/model guards). Falls back gracefully if the field
        // wasn't registered — showError is a no-op in that case.
        if (fieldName && researchValidator) {
            const fieldEl = document.getElementById(fieldName);
            if (fieldEl) {
                try {
                    researchValidator.showError(
                        fieldEl,
                        message,
                        { announce: false }
                    );
                } catch (_e) { /* defensive: never let error UX break the form */ }
                // If the field is in the (collapsed) Advanced Options
                // panel, reveal it so the user can see both the alert and
                // the highlighted field.
                if (advancedPanel && !advancedPanel.classList.contains('ldr-expanded')) {
                    applyAdvancedOptionsState(true);
                }
            }
        }
    }

    /**
     * Remove the submit-owned alert toasts (the top-of-form
     * #research-alert and the anchored #research-error-alert that this
     * submit handler tagged with ``data-submit-alert="true"``). Untagged
     * alerts are preserved — only the submit handler's own messages are
     * removed. Used by the change listener for the egress-scope
     * dropdown, which should NOT wipe unrelated field errors via
     * FormValidator.clearErrors().
     */
    function clearSubmitOwnedAlerts() {
        for (const id of ['research-error-alert', 'research-alert']) {
            const container = document.getElementById(id);
            if (!container) continue;
            const tagged = container.querySelector('.alert[data-submit-alert="true"]');
            if (tagged) {
                tagged.remove();
                if (!container.firstChild) {
                    container.style.display = 'none';
                }
            }
        }
    }

    /**
     * Clear the submission-error UX produced by this submit handler:
     * the top-of-form #research-alert, the anchored #research-error-alert
     * above the submit button, and the inline field errors set via
     * FormValidator. Called at the very start of a new submission (so a
     * stale message can't survive an early validation return).
     *
     * Both the egress-specific error UX AND generic submission errors
     * (network failures, 5xx, malformed responses) are wiped. The submit
     * handler tags every alert it creates with ``data-submit-alert="true"``;
     * this function removes only those tags, so unrelated page alerts
     * (e.g. a settings-page danger warning that happens to live in
     * #research-alert) survive untouched.
     */
    function clearSubmitAlerts() {
        clearSubmitOwnedAlerts();
        // Wipe inline field errors so the red borders / messages from a
        // prior attempt don't survive. Each registered field's rule is
        // re-evaluated by the validation guards that follow; if the
        // field is now valid the error is cleared, if it's still
        // invalid the error is re-shown (e.g. a missing model is
        // re-flagged so the user can see it after the next attempt's
        // missing-model guard runs).
        if (researchValidator) {
            try {
                researchValidator.clearErrors();
            } catch (_e) { /* defensive */ }
        }
    }

    // Initialize research component when DOM is loaded
    document.addEventListener('DOMContentLoaded', initializeResearch);
})();