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.

3.6 KB
Raw Blame
/**
 * Shared helpers for the notes feature's page scripts and components.
 *
 * The research/document notes components and the notes/note-detail pages
 * each used to define their own byte-identical toast / CSRF-token /
 * POST-and-parse helpers (three copies of the trio in the components,
 * plus a weaker CSRF helper in the pages). This is the single home for
 * that glue.
 *
 * Exposed as window.NotesShared METHODS, not bare globals: every consumer
 * keeps its own page/component-local wrapper NAME (getCSRFToken /
 * csrfToken / postJson / toast) and delegates here, so no shared global
 * function name is introduced — that would reintroduce the deferred
 * shared-script global-shadowing bug this codebase deliberately avoids.
 * Loaded via its own <script defer> tag ahead of every consumer, the same
 * way components/annotation_surface.js and services/formatting.js are.
 */
(function () {
    /** Surface a transient message via the shared ui service (no-op if absent). */
    function toast(message, type) {
        if (window.ui && window.ui.showMessage) {
            window.ui.showMessage(message, type || 'info');
        }
    }

    /**
     * Read the CSRF token: prefer the api service, fall back to the
     * meta tag so a POST still carries a token if window.api hasn't loaded
     * (the page-file helpers previously skipped this fallback and sent an
     * empty token in that case).
     */
    function csrfToken() {
        return (window.api && window.api.getCsrfToken)
            ? window.api.getCsrfToken()
            : (document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '');
    }

    /**
     * POST JSON and return the parsed body, throwing on a non-OK response
     * or a {success:false} envelope. Sends the CSRF header and same-origin
     * credentials. Callers that need the raw Response (status branching,
     * streaming) should not use this.
     */
    async function postJson(url, body) {
        const response = await safeFetchWithAuth(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken()
            },
            credentials: 'same-origin',
            body: JSON.stringify(body || {})
        });
        const data = await response.json().catch(() => ({}));
        if (!response.ok || !data.success) {
            throw new Error(
                data.error || data.detail || `Server returned ${response.status}`
            );
        }
        return data;
    }

    /**
     * Build one linked-notes row: an <a> to the note with its title and
     * (markdown-stripped) preview. Shared by the research and document
     * notes panels, which rendered byte-identical rows. Uses textContent
     * throughout, so no user data reaches innerHTML.
     */
    function renderNoteRow(note) {
        const row = document.createElement('a');
        row.className = 'ldr-research-note-row';
        row.href = `/notes/${encodeURIComponent(note.id)}`;

        const title = document.createElement('span');
        title.className = 'ldr-research-note-title';
        title.textContent = note.title || 'Untitled note';
        row.appendChild(title);

        if (note.content_preview) {
            const preview = document.createElement('span');
            preview.className = 'ldr-research-note-preview';
            preview.textContent = window.formatting.stripMarkdownToText(note.content_preview);
            row.appendChild(preview);
        }
        return row;
    }

    window.NotesShared = { toast, csrfToken, postJson, renderNoteRow };
})();