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.

25.2 KB
Raw Blame
/**
 * Unified Search Page ("Search everything")
 *
 * One search box across notes, library documents, and research reports,
 * with the same Text / AI Hybrid / AI Only modes as the notes page —
 * built around a MODE REGISTRY so future modes are one registry entry.
 *
 * URL security: every URL this page fetches is a same-origin relative
 * path (/library/search/api/...), and result links come from the
 * server-computed `url` field, additionally constrained to relative
 * paths by unifiedSearchSafeHref below. External URL validation is
 * available via URLValidator.isSafeUrl if that ever changes.
 *
 * All function names are page-unique (unifiedSearch~ prefix) — deferred
 * shared scripts clobber same-named globals (see the notes
 * global-shadowing bug).
 */

// ============================================================================
// SEARCH MODE REGISTRY
// ============================================================================
// Adding a search mode = adding ONE entry here. The controller (input
// debounce, mode dropdown, render loop) consumes only this interface:
//
//   label:       string shown on the selector button and dropdown item.
//   icon:        Font Awesome class for the button/dropdown icon.
//   placeholder: search-input placeholder while the mode is active.
//   hint:        short dropdown-item description (optional).
//   run(query, {signal}) → Promise<{results, notice?}>
//     - `query` is the trimmed search text (>= 2 chars).
//     - `signal` is an AbortSignal; pass it to fetches so a newer search
//       cancels this one. Let AbortError propagate (re-throw it).
//     - Resolve with `results`: an array of
//         {id, title, content_preview, url, source_type, similarity?}
//       and an optional `notice` string the controller shows inline
//       (e.g. a degraded-but-usable search).
//     - Reject (throw) when the mode cannot produce meaningful results;
//       the controller renders the "Couldn't search" error state with a
//       retry action.
const SEARCH_MODE_REGISTRY = {
    text: { label: 'Text Only', icon: 'fa-font', placeholder: 'Search everything by keyword...', hint: 'keyword match', run: runKeywordMode },
    hybrid: { label: 'AI Hybrid', icon: 'fa-brain', placeholder: 'AI Hybrid: keywords + meaning...', hint: 'keywords + meaning', run: runHybridMode },
    semantic: { label: 'AI Only', icon: 'fa-brain', placeholder: 'Search everything by meaning...', hint: 'search by meaning', run: runSemanticMode },
};

const UNIFIED_SEARCH_DEFAULT_MODE = 'hybrid';
// Minimum query length lives in the shared window.SemanticSearch
// (MIN_QUERY_LENGTH) so this page and the notes page can't diverge; read
// at call time in runUnifiedSearch (the component is loaded before this).

// Source-type badge map. Unknown source types (user_upload,
// research_download, research_source, manual_entry, future ones) fall
// back to the generic Document badge.
const UNIFIED_SOURCE_BADGES = Object.freeze({
    note: { label: 'Note', icon: 'fa-sticky-note' },
    research_report: { label: 'Report', icon: 'fa-file-alt' },
});
const UNIFIED_SOURCE_BADGE_DEFAULT = Object.freeze({ label: 'Document', icon: 'fa-file' });

// Page state
let unifiedSearchMode = UNIFIED_SEARCH_DEFAULT_MODE;
let unifiedSearchAbortController = null;
// Monotonic id so a slow response can't overwrite a newer search.
let unifiedSearchRunId = 0;
// Coarse results-area state ('idle' | 'loading' | 'results' | 'error') so
// the per-keystroke loading call can short-circuit when the spinner is
// already on screen instead of rebuilding its DOM every input event.
let unifiedSearchUiState = 'idle';
// Debounce timer for the search input. Module-level so a mode change can
// cancel a pending re-run — it re-runs immediately itself, and the stale
// timer would otherwise fire a duplicate identical search.
let unifiedSearchDebounceTimer = null;

// DOM refs
let unifiedSearchInput;
let unifiedSearchResults;
let unifiedSearchEmpty;
let unifiedSearchNotice;
let unifiedSearchModeBtn;
let unifiedSearchModeMenu;
let unifiedSearchModeLabel;

document.addEventListener('DOMContentLoaded', function() {
    unifiedSearchInput = document.getElementById('ldr-unified-search-input');
    unifiedSearchResults = document.getElementById('ldr-unified-search-results');
    unifiedSearchEmpty = document.getElementById('ldr-unified-search-empty');
    unifiedSearchNotice = document.getElementById('ldr-unified-search-notice');
    unifiedSearchModeBtn = document.getElementById('unified-search-mode-btn');
    unifiedSearchModeMenu = document.getElementById('unified-search-mode-menu');
    unifiedSearchModeLabel = document.getElementById('unified-search-mode-label');

    renderUnifiedSearchModeMenu();
    setUnifiedSearchMode(UNIFIED_SEARCH_DEFAULT_MODE);
    setupUnifiedSearchListeners();
});

/**
 * Build the mode dropdown from the registry — a future mode appears in
 * the menu without a template change.
 */
function renderUnifiedSearchModeMenu() {
    if (!unifiedSearchModeMenu) return;
    unifiedSearchModeMenu.replaceChildren();
    for (const [mode, def] of Object.entries(SEARCH_MODE_REGISTRY)) {
        const li = document.createElement('li');
        const a = document.createElement('a');
        a.className = 'dropdown-item' + (mode === unifiedSearchMode ? ' active' : '');
        a.href = '#';
        a.dataset.mode = mode;
        const icon = document.createElement('i');
        icon.className = 'fas ' + def.icon;
        icon.setAttribute('aria-hidden', 'true');
        a.appendChild(icon);
        a.appendChild(document.createTextNode(' ' + def.label + ' '));
        if (def.hint) {
            const small = document.createElement('small');
            small.className = 'text-muted';
            small.textContent = '— ' + def.hint;
            a.appendChild(small);
        }
        li.appendChild(a);
        unifiedSearchModeMenu.appendChild(li);
    }
}

// Delegated data-action handlers (CSP-friendly — no inline onclick).
const UNIFIED_SEARCH_ACTIONS = {
    'retry-unified-search': () => runUnifiedSearch(),
};

function setupUnifiedSearchListeners() {
    // Search input with debounce. Text mode is a single server call; the
    // AI modes add an embedding round-trip, so debounce a little longer.
    if (unifiedSearchInput) {
        unifiedSearchInput.addEventListener('input', function() {
            clearTimeout(unifiedSearchDebounceTimer);

            // Invalidate immediately, not when the replacement debounce
            // expires. Otherwise a response for the previous input can render
            // stale clickable results during this 300-500 ms window.
            if (unifiedSearchAbortController) {
                unifiedSearchAbortController.abort();
                unifiedSearchAbortController = null;
            }
            unifiedSearchRunId += 1;
            clearUnifiedSearchNotice();

            const query = unifiedSearchInput.value.trim();
            if (query.length < window.SemanticSearch.MIN_QUERY_LENGTH) {
                showUnifiedSearchIdleState();
                return;
            }
            showUnifiedSearchLoading('Searching...');

            unifiedSearchDebounceTimer = setTimeout(
                () => runUnifiedSearch(),
                unifiedSearchMode === 'text' ? 300 : 500
            );
        });
    }

    // Mode selector
    if (unifiedSearchModeMenu) {
        unifiedSearchModeMenu.addEventListener('click', function(e) {
            const item = e.target.closest('[data-mode]');
            if (!item) return;
            e.preventDefault();
            const mode = item.dataset.mode;
            if (!mode || mode === unifiedSearchMode) return;
            setUnifiedSearchMode(mode);
        });
    }

    // Delegated data-action dispatcher
    document.addEventListener('click', (e) => {
        const el = e.target.closest('[data-action]');
        if (!el) return;
        const fn = UNIFIED_SEARCH_ACTIONS[el.dataset.action];
        if (fn) {
            e.preventDefault();
            fn(el, e);
        }
    });
}

/**
 * Switch the active search mode, update the selector chrome from the
 * registry, and re-run the current query in the new mode.
 */
function setUnifiedSearchMode(mode) {
    const def = SEARCH_MODE_REGISTRY[mode];
    if (!def) return;
    unifiedSearchMode = mode;

    if (unifiedSearchModeMenu) {
        unifiedSearchModeMenu.querySelectorAll('.dropdown-item').forEach((i) => {
            i.classList.toggle('active', i.dataset.mode === mode);
        });
    }
    if (unifiedSearchModeLabel) unifiedSearchModeLabel.textContent = def.label;
    if (unifiedSearchModeBtn) {
        const icon = unifiedSearchModeBtn.querySelector('i');
        if (icon) icon.className = 'fas ' + def.icon;
    }
    if (unifiedSearchInput) unifiedSearchInput.placeholder = def.placeholder;

    clearUnifiedSearchNotice();
    // Cancel any pending debounced re-run — the immediate run below covers
    // it, and the stale timer would fire a duplicate identical search.
    clearTimeout(unifiedSearchDebounceTimer);
    // Re-run so the visible results reflect the new mode immediately.
    runUnifiedSearch();
}

/** Show a small inline notice under the search box (e.g. AI unavailable). */
function showUnifiedSearchNotice(message) {
    if (!unifiedSearchNotice) return;
    unifiedSearchNotice.textContent = message;
    unifiedSearchNotice.style.display = 'block';
}

/** Hide the inline search notice. */
function clearUnifiedSearchNotice() {
    if (!unifiedSearchNotice) return;
    unifiedSearchNotice.textContent = '';
    unifiedSearchNotice.style.display = 'none';
}

/** Render the centered "searching…" spinner inside the results area. */
function showUnifiedSearchLoading(message) {
    if (!unifiedSearchResults) return;
    if (unifiedSearchUiState === 'loading') {
        // The spinner from the previous keystroke is still correct — a
        // rebuild here is pure DOM churn on the hottest path (every
        // input event above MIN_QUERY_LENGTH). Update only the label
        // when the message actually differs.
        const label = unifiedSearchResults.querySelector(
            '.ldr-semantic-search-loading > p'
        );
        const text = message || 'Searching...';
        if (label && label.textContent !== text) {
            label.textContent = text;
        }
        return;
    }
    unifiedSearchUiState = 'loading';
    unifiedSearchResults.replaceChildren();
    const wrap = document.createElement('div');
    wrap.className = 'ldr-semantic-search-loading';
    const spinner = document.createElement('div');
    spinner.className = 'ldr-spinner';
    const text = document.createElement('p');
    text.textContent = message || 'Searching...';
    wrap.append(spinner, text);
    unifiedSearchResults.appendChild(wrap);
    unifiedSearchResults.style.display = 'block';
    if (unifiedSearchEmpty) unifiedSearchEmpty.style.display = 'none';
}

/**
 * Render the "Couldn't search" error state with a retry button.
 * Replaces the results area so a failed search can't leave a spinner
 * running or masquerade as the "No matches" empty state.
 */
function showUnifiedSearchError(message) {
    if (!unifiedSearchResults || !unifiedSearchEmpty) return;
    unifiedSearchUiState = 'error';
    unifiedSearchResults.replaceChildren();
    unifiedSearchResults.style.display = 'none';
    const icon = document.createElement('i');
    icon.className = 'fas fa-exclamation-triangle ldr-notes-empty-icon';
    const heading = document.createElement('h3');
    heading.textContent = "Couldn't search";
    const text = document.createElement('p');
    text.textContent = message;
    const retryIcon = document.createElement('i');
    retryIcon.className = 'fas fa-rotate-right';
    const retryBtn = document.createElement('button');
    retryBtn.className = 'ldr-create-note-btn';
    retryBtn.dataset.action = 'retry-unified-search';
    retryBtn.append(retryIcon, ' Try again');
    // audited 2026-07-22: all children are createElement/textContent-built Nodes — replaceChildren() never parses HTML
    // bearer:disable javascript_lang_dangerous_insert_html
    unifiedSearchEmpty.replaceChildren(icon, heading, text, retryBtn);
    unifiedSearchEmpty.style.display = 'block';
}

/** Render the idle state shown before the user has typed a query. */
function showUnifiedSearchIdleState() {
    if (!unifiedSearchResults || !unifiedSearchEmpty) return;
    unifiedSearchUiState = 'idle';
    unifiedSearchResults.replaceChildren();
    unifiedSearchResults.style.display = 'none';
    unifiedSearchEmpty.innerHTML = `
        <i class="fas fa-search ldr-notes-empty-icon"></i>
        <h3>Search everything</h3>
        <p>Find notes, library documents, and research reports in one place</p>
    `;
    unifiedSearchEmpty.style.display = 'block';
}

/**
 * Run the active mode's search for the current input value. This is the
 * ONLY place a mode's `run` is invoked — everything mode-specific lives
 * behind the registry contract documented above.
 */
async function runUnifiedSearch() {
    const query = ((unifiedSearchInput && unifiedSearchInput.value) || '').trim();

    if (unifiedSearchAbortController) {
        unifiedSearchAbortController.abort();
    }
    const controller = new AbortController();
    unifiedSearchAbortController = controller;
    const myId = ++unifiedSearchRunId;

    if (query.length < window.SemanticSearch.MIN_QUERY_LENGTH) {
        // This return bypasses the try/finally below — the only other
        // cleanup site for the controller reference we just installed.
        // Nothing is in flight, so drop it here (mirroring the input
        // handler's null-out) or the reference lingers until some later
        // search aborts the stale controller as a no-op (#5580).
        if (unifiedSearchAbortController === controller) {
            unifiedSearchAbortController = null;
        }
        clearUnifiedSearchNotice();
        showUnifiedSearchIdleState();
        return;
    }

    const def = SEARCH_MODE_REGISTRY[unifiedSearchMode];
    if (!def) return;

    clearUnifiedSearchNotice();
    showUnifiedSearchLoading('Searching...');

    try {
        const { results, notice } = await def.run(query, { signal: controller.signal });
        // A newer search (typing or a mode change) superseded this one.
        if (myId !== unifiedSearchRunId) return;
        if (notice) showUnifiedSearchNotice(notice);
        renderUnifiedSearchResults(results || []);
    } catch (error) {
        if (error && error.name === 'AbortError') return;
        if (myId !== unifiedSearchRunId) return;
        SafeLogger.error('Unified search failed:', error);
        // Surface the server's own error message when a mode flagged it
        // (e.g. a 400 validation detail) — blaming "your connection" for
        // a server-reported problem sends the user debugging the wrong
        // thing. Network/parse failures keep the generic copy.
        showUnifiedSearchError(
            error && error.isServerError && error.message
                ? error.message
                : 'Search failed — check your connection and try again.'
        );
    } finally {
        // Only clear the module-level controller if it is still ours —
        // a newer run already installed its own.
        if (unifiedSearchAbortController === controller) {
            unifiedSearchAbortController = null;
        }
    }
}

// ============================================================================
// Mode implementations (registry `run` functions)
// ============================================================================

/** Text Only: the keyword endpoint (ILIKE over the listed name + content). */
async function runKeywordMode(query, { signal } = {}) {
    const params = new URLSearchParams();
    params.append('q', query);
    params.append('limit', '20');
    const response = await safeFetchWithAuth(`/library/search/api/keyword?${params.toString()}`, {
        credentials: 'same-origin',
        signal,
    });
    const data = await response.json();
    if (!data.success) {
        const err = new Error(data.error || 'Keyword search failed');
        // Server-reported failure — the controller may show this message.
        err.isServerError = true;
        throw err;
    }
    return { results: data.results || [] };
}

/** AI Only: the semantic endpoint (FAISS over the system collections). */
async function runSemanticMode(query, { signal } = {}) {
    const params = new URLSearchParams();
    params.append('q', query);
    params.append('limit', '20');
    params.append('min_similarity', String(window.SemanticSearch.MIN_SIMILARITY));
    const response = await safeFetchWithAuth(`/library/search/api/semantic?${params.toString()}`, {
        credentials: 'same-origin',
        signal,
    });
    const data = await response.json();
    if (!data.success) {
        const err = new Error(data.error || 'Semantic search failed');
        // Server-reported failure — the controller may show this message.
        err.isServerError = true;
        throw err;
    }
    return { results: data.results || [] };
}

/**
 * AI Hybrid: run both legs in parallel and merge them via the shared
 * window.SemanticSearch.buildTieredResults — the exact tiering the
 * notes/library/history/news pages use. Tier 1 = matched by both (top,
 * ranked by similarity), Tier 2 = keyword-only (recency), Tier 3 =
 * semantic-only (ranked by similarity). Each leg is best-effort: a
 * single-leg failure degrades to the other leg plus an inline notice
 * (by design, like the notes page — not a bug-masking fallback); both
 * legs failing rejects, which the controller renders as the error
 * state with retry.
 */
async function runHybridMode(query, { signal } = {}) {
    let kwUnavailable = false;
    let aiUnavailable = false;

    const kwPromise = runKeywordMode(query, { signal })
        .then((r) => r.results)
        .catch((err) => {
            if (err && err.name === 'AbortError') throw err;
            SafeLogger.error('Hybrid keyword leg failed:', err);
            kwUnavailable = true;
            return [];
        });

    const semPromise = runSemanticMode(query, { signal })
        .then((r) => r.results)
        .catch((err) => {
            if (err && err.name === 'AbortError') throw err;
            SafeLogger.error('Hybrid semantic leg failed:', err);
            aiUnavailable = true;
            return [];
        });

    const [textResults, semanticResults] = await Promise.all([kwPromise, semPromise]);

    // Both legs down: nothing meaningful to render — reject so the
    // controller shows the explicit error state (with retry) instead of
    // a fake empty result.
    if (kwUnavailable && aiUnavailable) {
        throw new Error('Both search legs failed');
    }

    const merge = window.SemanticSearch && window.SemanticSearch.buildTieredResults;
    if (!merge) {
        // Shared component missing (load-order/dependency issue) — show
        // the keyword base rather than nothing, and log it loudly. Keep
        // the degradation notice: without it, a failed AI leg on this
        // path was indistinguishable from healthy keyword-only results.
        SafeLogger.error('window.SemanticSearch.buildTieredResults unavailable; showing keyword results only');
        return {
            results: textResults,
            notice: aiUnavailable
                ? (textResults.length > 0
                    ? 'AI search unavailable — showing keyword matches only.'
                    : 'AI search unavailable — no keyword matches found.')
                : 'AI ranking unavailable — showing keyword matches only.',
        };
    }

    // The semantic endpoint returns its snippet as `content_preview`;
    // buildTieredResults reads `.snippet`, so alias it for Tier-1 enrichment.
    const normalizedSemantic = semanticResults.map(
        (r) => ({ ...r, snippet: r.content_preview || '' }),
    );

    const tiered = merge(textResults, normalizedSemantic, {
        textIdKey: 'id',
        semanticIdKey: 'id',
    });
    const results = mergeUnifiedSearchTiers(tiered);

    let notice = null;
    if (aiUnavailable) {
        notice = textResults.length > 0
            ? 'AI search unavailable — showing keyword matches only.'
            : 'AI search unavailable — no keyword matches found.';
    } else if (kwUnavailable) {
        notice = results.length > 0
            ? 'Text search unavailable — showing AI matches only.'
            : 'Text search unavailable — no AI matches found.';
    }
    return { results, notice };
}

/**
 * Flatten tiered hybrid results into a single ordered results array.
 * Thin page-local wrapper over the shared
 * window.SemanticSearch.flattenTieredResults (the notes and
 * unified-search copies of this logic had drifted apart); the caller
 * already sits behind the buildTieredResults availability guard, which
 * ships in the same component file.
 */
function mergeUnifiedSearchTiers(tiered) {
    return window.SemanticSearch.flattenTieredResults(tiered);
}

// ============================================================================
// Rendering
// ============================================================================

/**
 * Constrain a result link to a same-origin relative path. The server
 * only ever emits /notes/…, /results/… or /library/document/… here, so
 * anything else indicates tampering/corruption — link to '#' instead.
 */
function unifiedSearchSafeHref(url) {
    // Reject '//' (protocol-relative) AND '/\' — browsers normalize the
    // backslash to '/', so '/\evil.com' navigates cross-origin too.
    // Reject ASCII control chars first: the URL parser strips embedded
    // tab/newline/CR, so '/\t/evil.com' would slip past the '//' check yet
    // resolve to a protocol-relative cross-origin URL once parsed.
    return typeof url === 'string'
        // eslint-disable-next-line no-control-regex -- intentionally detecting embedded control chars the URL parser would strip
        && !/[\u0000-\u001f\u007f]/.test(url)
        && url.startsWith('/')
        && !url.startsWith('//')
        && !url.startsWith('/\\')
        ? url
        : '#';
}

/** Render one result row as an <a> card (badge, title, preview). */
function createUnifiedSearchResultCard(result) {
    const badge = UNIFIED_SOURCE_BADGES[result.source_type] || UNIFIED_SOURCE_BADGE_DEFAULT;
    const similarityHtml = window.formatting.similarityBadge(result.similarity);
    const preview = result.content_preview || '';

    return `
        <a class="ldr-unified-result-card" href="${escapeHtml(unifiedSearchSafeHref(result.url))}">
            <div class="ldr-unified-result-header">
                <span class="ldr-unified-source-badge">
                    <i class="fas ${escapeHtml(badge.icon)}" aria-hidden="true"></i> ${escapeHtml(badge.label)}
                </span>
                <h3 class="ldr-unified-result-title">${escapeHtml(result.title || 'Untitled')}</h3>
                ${similarityHtml}
            </div>
            <p class="ldr-unified-result-preview">${escapeHtml(preview)}</p>
        </a>
    `;
}

/** Render the result list (or the "No matches" empty state). */
function renderUnifiedSearchResults(results) {
    if (!unifiedSearchResults || !unifiedSearchEmpty) return;
    unifiedSearchUiState = 'results';

    if (!results.length) {
        unifiedSearchResults.replaceChildren();
        unifiedSearchResults.style.display = 'none';
        const hint = unifiedSearchMode === 'text'
            ? 'Try a different search or switch to AI Hybrid mode'
            : 'Try a different search or switch to Text mode';
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-07-04: hint is one of two hardcoded strings, no user data
        unifiedSearchEmpty.innerHTML = `
            <i class="fas fa-search ldr-notes-empty-icon"></i>
            <h3>No matches found</h3>
            <p>${hint}</p>
        `;
        unifiedSearchEmpty.style.display = 'block';
        return;
    }

    unifiedSearchEmpty.style.display = 'none';
    unifiedSearchResults.style.display = 'block';
    const html = results.map((r) => createUnifiedSearchResultCard(r)).join('');
    // bearer:disable javascript_lang_dangerous_insert_html
    // eslint-disable-next-line no-unsanitized/property -- audited 2026-07-04: all interpolations use escapeHtml/numeric
    unifiedSearchResults.innerHTML = html;
}

// Production-inert test hook (mirrors notes.js's __notesTest). Exposed
// only when a vitest harness sets window.__VITEST_TEST__ before import,
// so it never ships to the browser.
if (typeof window !== 'undefined' && window.__VITEST_TEST__) {
    window.__unifiedSearchTest = {
        SEARCH_MODE_REGISTRY,
        UNIFIED_SEARCH_ACTIONS,
        setupUnifiedSearchListeners,
        runUnifiedSearch,
        runKeywordMode,
        runSemanticMode,
        runHybridMode,
        mergeUnifiedSearchTiers,
        renderUnifiedSearchResults,
        renderUnifiedSearchModeMenu,
        setUnifiedSearchMode,
        unifiedSearchSafeHref,
        getMode: () => unifiedSearchMode,
        setDomRefs: ({ input, results, empty, notice, modeBtn, modeMenu, modeLabel } = {}) => {
            unifiedSearchInput = input;
            unifiedSearchResults = results;
            unifiedSearchEmpty = empty;
            unifiedSearchNotice = notice;
            unifiedSearchModeBtn = modeBtn;
            unifiedSearchModeMenu = modeMenu;
            unifiedSearchModeLabel = modeLabel;
            unifiedSearchRunId = 0;
            unifiedSearchUiState = 'idle';
        },
    };
}