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.

9.6 KB
Raw Blame
// Note: URLValidator is available globally via /static/js/security/url-validator.js
// Keep writes to the same setting in user-event order. The FastAPI handler
// runs each request in an independent worker and has no client sequence token,
// so overlapping PUTs could otherwise commit in the opposite order. Different
// setting keys remain independent and can still save concurrently.
const pendingMenuSettingSaves = new Map();

// Perform one save using the individual settings manager API.
function performMenuSettingSave(settingKey, settingValue) {
    SafeLogger.log('Saving setting:', settingKey, '=', settingValue);

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

    // Use the individual settings API endpoint that uses the settings manager
    return fetch(`/settings/api/${settingKey}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': csrfToken
        },
        body: JSON.stringify({ value: settingValue })
    })
    .then(async response => {
        SafeLogger.log('Response status:', response.status, response.statusText);
        let data;
        try {
            data = await response.json();
        } catch {
            data = {};
        }
        if (!response.ok) {
            const message = data.error || data.detail || data.message
                || `HTTP ${response.status}`;
            SafeLogger.error('Error response body:', message);
            throw new Error(message);
        }
        return data;
    })
    .then(data => {
        SafeLogger.log(`Setting ${settingKey} saved via settings manager:`, data);

        // If the response includes warnings, display them directly
        if (data.warnings && typeof window.displayWarnings === 'function') {
            window.displayWarnings(data.warnings);
        }

        // Also trigger client-side warning recalculation for search settings
        if (settingKey.startsWith('search.') || settingKey === 'llm.provider') {
            if (typeof window.refetchSettingsAndUpdateWarnings === 'function') {
                window.refetchSettingsAndUpdateWarnings();
            }
        }

        // Show success notification if UI module is available
        if (window.ui && window.ui.showMessage) {
            window.ui.showMessage(`${settingKey.split('.').pop()} updated successfully`, 'success');
        } else {
            SafeLogger.log('Setting saved successfully:', data);
        }

        return true;
    })
    .catch(error => {
        SafeLogger.error(`Error saving setting ${settingKey}:`, error);
        if (window.ui && window.ui.showMessage) {
            window.ui.showMessage(`Error updating ${settingKey}: ${error.message}`, 'error');
        }
        return false;
    });
}

function saveMenuSettings(settingKey, settingValue) {
    const priorSave = pendingMenuSettingSaves.get(settingKey)
        || Promise.resolve();
    const startSave = () => performMenuSettingSave(settingKey, settingValue);
    const request = priorSave.then(startSave, startSave);
    const trackedRequest = request.finally(() => {
        if (pendingMenuSettingSaves.get(settingKey) === trackedRequest) {
            pendingMenuSettingSaves.delete(settingKey);
        }
    });

    pendingMenuSettingSaves.set(settingKey, trackedRequest);
    return trackedRequest;
}

/**
 * Save a URL input once for the browser's change -> blur event sequence.
 *
 * Text controls fire `change` immediately before `blur` when an edited value
 * loses focus. Mark the value synchronously so the blur handler cannot issue
 * an identical PUT while the change request is still awaiting the server.
 * A rejected write releases the marker so a later blur can retry it.
 */
function saveUrlInputSetting(input, settingKey) {
    const value = input.value;
    input.setAttribute('data-last-saved', value);

    return saveMenuSettings(settingKey, value).then(saved => {
        if (!saved && input.getAttribute('data-last-saved') === value) {
            input.removeAttribute('data-last-saved');
        }
        return saved;
    });
}

/**
 * Connects the menu settings to use the same save method as the settings page.
 */
function connectMenuSettings() {
    SafeLogger.log('Initializing menu settings handler');
    // research.js owns these controls on the research page, including their
    // optimistic UI and error handling. Binding the generic base-page bridge
    // as well made one dropdown selection issue duplicate (and, for search,
    // sometimes triple) PUTs to the same FastAPI setting route.
    const researchOwnsCoreSettings = !!document.getElementById('research-form');

    // Handle model dropdown changes
    const modelHidden = document.getElementById('model_hidden');

    if (modelHidden && !researchOwnsCoreSettings) {
        modelHidden.addEventListener('change', function() {
            SafeLogger.log('Model changed to:', this.value);
            saveMenuSettings('llm.model', this.value);
        });
    }

    // Handle provider dropdown changes
    const providerSelect = document.getElementById('model_provider');
    if (providerSelect && !researchOwnsCoreSettings) {
        providerSelect.addEventListener('change', function() {
            SafeLogger.log('Provider changed to:', this.value);
            saveMenuSettings('llm.provider', this.value);
        });
    }

    // Handle search engine dropdown changes
    const searchEngineHidden = document.getElementById('search_engine_hidden');
    if (searchEngineHidden && !researchOwnsCoreSettings) {
        searchEngineHidden.addEventListener('change', function() {
            SafeLogger.log('Search engine changed to:', this.value);
            saveMenuSettings('search.tool', this.value);
        });
    }

    // Handle iterations and questions per iteration
    const iterationsInput = document.getElementById('iterations');
    if (iterationsInput && !researchOwnsCoreSettings) {
        iterationsInput.addEventListener('change', function() {
            SafeLogger.log('Iterations changed to:', this.value);
            saveMenuSettings('search.iterations', this.value);
        });
    }

    const questionsInput = document.getElementById('questions_per_iteration');
    if (questionsInput && !researchOwnsCoreSettings) {
        questionsInput.addEventListener('change', function() {
            SafeLogger.log('Questions per iteration changed to:', this.value);
            saveMenuSettings('search.questions_per_iteration', this.value);
        });
    }

    // Handle search strategy dropdown changes
    const strategySelect = document.getElementById('strategy');
    if (strategySelect) {
        strategySelect.addEventListener('change', function() {
            SafeLogger.log('Search strategy changed to:', this.value);
            saveMenuSettings('search.search_strategy', this.value);
        });
    }

    // Handle Ollama URL input changes
    const ollamaUrlInput = document.getElementById('ollama_url');
    if (ollamaUrlInput && !researchOwnsCoreSettings) {
        ollamaUrlInput.addEventListener('change', function() {
            SafeLogger.log('Ollama URL changed to:', this.value);
            saveUrlInputSetting(this, 'llm.ollama.url');
        });
        // Also save on blur (when user clicks away)
        ollamaUrlInput.addEventListener('blur', function() {
            if (this.value && this.value !== this.getAttribute('data-last-saved')) {
                SafeLogger.log('Ollama URL changed (on blur) to:', this.value);
                saveUrlInputSetting(this, 'llm.ollama.url');
            }
        });
    }

    // Handle custom endpoint URL input changes (for OpenAI endpoint)
    const customEndpointInput = document.getElementById('custom_endpoint');
    if (customEndpointInput) {
        customEndpointInput.addEventListener('change', function() {
            SafeLogger.log('Custom endpoint URL changed to:', this.value);
            saveUrlInputSetting(this, 'llm.openai_endpoint.url');
        });
        // Also save on blur
        customEndpointInput.addEventListener('blur', function() {
            if (this.value && this.value !== this.getAttribute('data-last-saved')) {
                SafeLogger.log('Custom endpoint URL changed (on blur) to:', this.value);
                saveUrlInputSetting(this, 'llm.openai_endpoint.url');
            }
        });
    }

    // Handle theme dropdown changes
    // Try multiple selectors to find the theme select element
    const themeSelect = document.querySelector('select[data-key="app.theme"], select[name="app.theme"], #theme-select');
    if (themeSelect) {
        themeSelect.addEventListener('change', function() {
            SafeLogger.log('Theme changed to:', this.value);
            // Use themeService if available (handles both UI update and server sync)
            if (window.themeService && typeof window.themeService.setTheme === 'function') {
                window.themeService.setTheme(this.value, true);
            } else {
                // Fallback: just save to server
                saveMenuSettings('app.theme', this.value);
            }
        });
    }

    SafeLogger.log('Menu settings handlers initialized');
}

// Call this function after the page and other scripts are loaded
document.addEventListener('DOMContentLoaded', function() {
    // Use requestIdleCallback for better performance, fallback to requestAnimationFrame
    if (typeof requestIdleCallback === 'function') {
        requestIdleCallback(connectMenuSettings, { timeout: 500 });
    } else if (typeof requestAnimationFrame === 'function') {
        requestAnimationFrame(connectMenuSettings);
    } else {
        // Fallback for older browsers
        setTimeout(connectMenuSettings, 100);
    }
});