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.4 KB
Raw Blame
/**
 * Research Notes Component (results page)
 *
 * The reverse direction of the notes feature's research linking: from a
 * research run's results page, see the notes that comment on it, start a
 * new linked note, save the report itself as an editable note copy, clip
 * a selected passage into a quote note, or attach a Word-review-style
 * inline comment to a passage (via the shared LDRAnnotationSurface —
 * see components/annotation_surface.js).
 *
 * All rendering builds DOM nodes with textContent (never innerHTML with
 * user data). Function names are component-scoped inside this IIFE — no
 * globals to collide with the deferred shared scripts.
 */
(function() {
    // URL security: every URL this component fetches or assigns is a
    // same-origin relative path (/notes/..., built with encodeURIComponent)
    // — no external URLs are handled. External URL validation is available
    // via URLValidator.isSafeUrl if that ever changes.
    let researchId = null;
    let loadGeneration = 0;

    // Cap clipped selections: a note exists to hold a *quote*, not a
    // second copy of the report (that's what Save-as-note is for).
    const MAX_CLIP_CHARS = 4000;

    // Component-local wrappers over the shared trio (window.NotesShared) —
    // local names kept for shadowing-safety; the bodies live in one place.
    const toast = (message, type) => window.NotesShared.toast(message, type);
    const postJson = (url, body) => window.NotesShared.postJson(url, body);

    function initResearchNotes() {
        const section = document.getElementById('research-notes-section');
        if (!section) return;

        researchId = URLBuilder.extractResearchIdFromPattern('results');
        if (!researchId) {
            section.style.display = 'none';
            return;
        }

        const addBtn = document.getElementById('research-add-note-btn');
        if (addBtn) addBtn.addEventListener('click', addNoteForResearch);
        // Same "save the whole report as an editable, linked note" action is
        // offered in two places: the contextual button in the Notes card and a
        // prominent one in the results top action bar — both call the one
        // saveReportAsNote handler.
        const saveBtn = document.getElementById('research-save-as-note-btn');
        if (saveBtn) saveBtn.addEventListener('click', saveReportAsNote);
        const topSaveBtn = document.getElementById(
            'research-save-as-note-top-btn'
        );
        if (topSaveBtn) topSaveBtn.addEventListener('click', saveReportAsNote);

        // Selection toolbar + inline comments over the rendered report.
        const base = `/notes/api/research/${encodeURIComponent(researchId)}`;
        window.LDRAnnotationSurface.init({
            containerId: 'results-content',
            endpoints: {
                list: `${base}/annotations`,
                create: `${base}/annotations`,
                deleteFor: (noteId) => `${base}/annotations/${encodeURIComponent(noteId)}`
            },
            extraActions: [
                {
                    icon: 'fas fa-quote-right',
                    label: 'Clip to note',
                    onActivate: clipSelectionToNote
                }
            ],
            // Comments are notes — the panel below lists them too.
            onChanged: loadResearchNotes
        });

        loadResearchNotes();
    }

    async function loadResearchNotes() {
        const list = document.getElementById('research-notes-list');
        const empty = document.getElementById('research-notes-empty');
        if (!list) return;
        const generation = ++loadGeneration;
        try {
            const response = await safeFetchWithAuth(`/notes/api/research/${encodeURIComponent(researchId)}/notes`, {
                credentials: 'same-origin'
            });
            const data = await response.json();
            if (generation !== loadGeneration) return;
            if (!data.success) throw new Error(data.error || 'load failed');

            list.replaceChildren();
            const notes = data.notes || [];
            if (empty) empty.style.display = notes.length ? 'none' : 'block';
            for (const note of notes) {
                list.appendChild(window.NotesShared.renderNoteRow(note));
            }
        } catch (error) {
            if (generation !== loadGeneration) return;
            SafeLogger.error('Error loading research notes:', error);
            // Leave the section usable: the buttons still work, and a
            // failed list load shouldn't block reading the report.
            if (empty) {
                empty.textContent = "Couldn't load notes for this research.";
                empty.style.display = 'block';
            }
        }
    }


    async function addNoteForResearch() {
        const btn = document.getElementById('research-add-note-btn');
        if (btn) btn.disabled = true;
        try {
            const data = await postJson(`/notes/api/research/${encodeURIComponent(researchId)}/notes`, {});
            // Land the user in the editor — the starter note is theirs to write.
            window.location.href = `/notes/${encodeURIComponent(data.note_id)}`;
        } catch (error) {
            SafeLogger.error('Error creating note for research:', error);
            toast(error.message || 'Failed to create note', 'error');
            if (btn) btn.disabled = false;
        }
    }

    // The one handler is wired to TWO buttons (the Notes-card button and
    // the top action bar's). Disabling only the clicked button can't stop
    // the other — and a rapid double-click on the never-disabled top
    // button fired two POSTs, each creating its own note copy of the
    // report. Module-level in-flight flag + disable both.
    let saveAsNoteInProgress = false;

    async function saveReportAsNote() {
        if (saveAsNoteInProgress) return;
        saveAsNoteInProgress = true;
        const buttons = [
            document.getElementById('research-save-as-note-btn'),
            document.getElementById('research-save-as-note-top-btn'),
        ].filter(Boolean);
        buttons.forEach((b) => { b.disabled = true; });
        try {
            const data = await postJson(`/notes/api/research/${encodeURIComponent(researchId)}/save-as-note`, {});
            window.location.href = `/notes/${encodeURIComponent(data.note_id)}`;
        } catch (error) {
            SafeLogger.error('Error saving research as note:', error);
            toast(error.message || 'Failed to save report as note', 'error');
            buttons.forEach((b) => { b.disabled = false; });
            // eslint-disable-next-line require-atomic-updates -- single-writer guard; re-armed only on failure (success navigates away)
            saveAsNoteInProgress = false;
        }
    }

    // Escape markdown link-label metacharacters (backslash first) plus
    // newlines. Twin of the server's escape_markdown_link_label — keep in
    // sync. Component-local name so it can't collide with deferred globals.
    function escapeMarkdownLinkLabel(label) {
        return String(label == null ? '' : label)
            .replace(/\\/g, '\\\\')
            .replace(/\[/g, '\\[')
            .replace(/\]/g, '\\]')
            .replace(/\(/g, '\\(')
            .replace(/\)/g, '\\)')
            .replace(/[\r\n]/g, ' ');
    }

    async function clipSelectionToNote(hit) {
        if (!hit) return;

        let quote = hit.text;
        if (quote.length > MAX_CLIP_CHARS) {
            quote = quote.slice(0, MAX_CLIP_CHARS) + '…';
        }

        const queryEl = document.getElementById('result-query');
        const queryText = queryEl ? queryEl.textContent.trim() : '';
        const titleSeed = quote.replace(/\s+/g, ' ').slice(0, 60);
        const blockquote = quote.split('\n').map((line) => `> ${line}`).join('\n');
        // Escape the query before embedding it as a markdown link label, so
        // a crafted query can't break out of [label](url) — matches the
        // server-side escape_markdown_link_label used by the note routes.
        const label = escapeMarkdownLinkLabel(queryText || researchId);
        const content =
            `${blockquote}\n\n— clipped from the research run ` +
            `[${label}](/results/${researchId})\n\n`;

        try {
            await postJson(`/notes/api/research/${encodeURIComponent(researchId)}/notes`, {
                title: `Clip: ${titleSeed}`,
                content
            });
            // Stay on the page — clipping is part of the reading flow.
            // The new note appears in the Notes section instead.
            toast('Clipped to note', 'success');
            loadResearchNotes();
        } catch (error) {
            SafeLogger.error('Error clipping selection to note:', error);
            toast(error.message || 'Failed to clip selection', 'error');
        }
    }

    // Initialize when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initResearchNotes);
    } else {
        initResearchNotes();
    }

    if (window.__VITEST_TEST__) {
        window.__researchNotesTest = {
            initResearchNotes,
            loadResearchNotes,
            addNoteForResearch,
            saveReportAsNote,
            clipSelectionToNote,
            setResearchId: (value) => { researchId = value; },
            reset: () => {
                researchId = null;
                loadGeneration = 0;
                saveAsNoteInProgress = false;
            }
        };
    }
})();