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.

2.9 KB
Raw Blame
/**
 * Create a JSON SSE parser that preserves records split across byte chunks.
 *
 * ReadableStream chunk boundaries are unrelated to SSE line boundaries, and
 * TextDecoder must stay in streaming mode when a multi-byte character is split
 * across chunks. Call push() for each Uint8Array and finish() once at EOF.
 *
 * @param {Function} onData - Called once for each complete `data:` JSON line
 * @returns {{push: Function, finish: Function}} Stateful stream parser
 */
function createSSEJsonParser(onData) {
    const decoder = new window.TextDecoder();
    let pending = '';

    const emitLines = (flush) => {
        const lines = pending.split(/\r?\n/);
        pending = flush ? '' : lines.pop();

        for (const line of lines) {
            if (!line.startsWith('data:')) continue;

            const payload = line.slice(5).trimStart();
            if (payload) onData(JSON.parse(payload));
        }
    };

    return {
        push(chunk) {
            pending += decoder.decode(chunk, { stream: true });
            emitLines(false);
        },
        finish() {
            pending += decoder.decode();
            // EOF terminates the final SSE line even if the server omitted a
            // trailing newline.
            pending += '\n';
            emitLines(true);
        },
    };
}

/**
 * Shared SSE completion handler for download/extraction streams.
 *
 * Returns true if the stream is complete (error or success), false otherwise.
 * The caller should reset its own controller reference when true is returned.
 *
 * @param {Object} data - Parsed SSE event data
 * @param {Function} onSuccess - Called (with data) on successful completion
 * @param {Function} [isCurrent] - Re-check caller ownership before delayed UI
 * @returns {boolean} Whether the stream completed
 */
function handleSSECompletion(data, onSuccess, isCurrent = () => true) {
    if (!data.complete) return false;

    // closeProgressModal is page-specific — defined inline in
    // download_manager.html and library.html (the only callers of this
    // utility). Look it up via window so a caller from a different page
    // doesn't crash, and so eslint doesn't flag it as undefined.
    const closeModalIfDefined = () => {
        if (typeof window.closeProgressModal === 'function') {
            window.closeProgressModal();
        }
    };

    if (data.error) {
        setTimeout(() => {
            if (!isCurrent()) return;
            closeModalIfDefined();
            alert(data.error);
        }, 1000);
    } else {
        setTimeout(() => {
            if (!isCurrent()) return;
            closeModalIfDefined();
            onSuccess(data);
        }, 2000);
    }
    return true;
}

// This file is loaded as a classic script before the page-level inline
// consumers. Publish the helpers explicitly rather than relying on implicit
// top-level bindings.
window.createSSEJsonParser = createSSEJsonParser;
window.handleSSECompletion = handleSSECompletion;