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.

51.9 KB
Raw Blame
/**
 * Collection Details Page JavaScript
 * Handles individual collection management and document indexing
 */

let collectionData = null;
let documentsData = [];
let notesData = [];
let currentFilter = 'all';
let indexingPollInterval = null;
let activeIndexingPoll = null;
let progressHideTimer = null;
let progressUiGeneration = 0;
let collectionDetailsLoadId = 0;
let latestIndexingStatus = null;
const collectionRebuildInfo = {};
let collectionRebuildRequestId = 0;
let appliedCollectionRebuildRequestId = 0;
const collectionBooleanWriteStates = new WeakMap();
const confirmedCollectionBooleanState = new Map();
const collectionBooleanToggleIds = {
    is_public: 'collection-is-public',
    agent_enabled: 'collection-agent-enabled'
};

// safeFetch/safeFetchWithAuth (with URLValidator) are now provided by utils/safe-fetch.js loaded in base.html

/**
 * Initialize the page
 */
document.addEventListener('DOMContentLoaded', function() {
    loadCollectionDetails();

    // Setup button handlers
    document.getElementById('index-collection-btn').addEventListener('click', () => indexCollection(false));
    document.getElementById('reindex-collection-btn').addEventListener('click', () => indexCollection(true));
    document.getElementById('delete-collection-btn').addEventListener('click', deleteCollection);
    document.getElementById('cancel-indexing-btn').addEventListener('click', cancelIndexing);

    const publicToggle = document.getElementById('collection-is-public');
    if (publicToggle) {
        publicToggle.addEventListener('change', () => updateCollectionIsPublic(publicToggle.checked));
    }

    const agentToggle = document.getElementById('collection-agent-enabled');
    if (agentToggle) {
        agentToggle.addEventListener('change', () => updateCollectionAgentEnabled(agentToggle.checked));
    }

    const documentList = document.getElementById('documents-list');
    if (documentList) {
        documentList.addEventListener('click', (event) => {
            const actionButton = event.target.closest('button[data-doc-action]');
            if (!actionButton || !documentList.contains(actionButton)) return;
            const row = actionButton.closest('.ldr-document-item');
            const documentId = row?.dataset.documentId;
            if (!documentId) return;

            event.preventDefault();
            event.stopPropagation();
            if (actionButton.dataset.docAction === 'remove') {
                removeDocumentFromCollection(documentId);
            } else if (actionButton.dataset.docAction === 'delete') {
                deleteDocumentCompletely(documentId);
            }
        });
    }

    // Check if there's an active indexing task
    checkAndResumeIndexing();
});

/**
 * Serialize writes for one checkbox while keeping different checkboxes
 * independent. A later intent owns the visible state and notification, but
 * every intent still reaches the server in the order the user made it.
 */
function queueCollectionBooleanUpdate({
    toggle,
    field,
    requestedValue,
    successMessage,
    networkErrorMessage
}) {
    if (!toggle) return Promise.resolve();

    let state = collectionBooleanWriteStates.get(toggle);
    if (!state) {
        const loadedValue = collectionData && typeof collectionData[field] === 'boolean'
            ? collectionData[field]
            : !requestedValue;
        state = {
            queue: Promise.resolve(),
            latestIntentId: 0,
            confirmedValue: !!loadedValue,
            latestIntentValue: !!requestedValue,
            latestIntentPending: false
        };
        collectionBooleanWriteStates.set(toggle, state);
    }

    const intentId = ++state.latestIntentId;
    state.latestIntentValue = !!requestedValue;
    state.latestIntentPending = true;
    const run = async () => {
        try {
            const csrfToken = window.api ? window.api.getCsrfToken() : '';
            const response = await safeFetchWithAuth(URLBuilder.build(URLS.LIBRARY_API.COLLECTION_DETAILS, COLLECTION_ID), {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify({ [field]: requestedValue })
            });
            const data = await response.json();
            if (data.success) {
                const returnedValue = data.collection && typeof data.collection[field] === 'boolean'
                    ? data.collection[field]
                    : requestedValue;
                state.confirmedValue = !!returnedValue;
                const previousConfirmedState = confirmedCollectionBooleanState.get(field);
                confirmedCollectionBooleanState.set(field, {
                    generation: (previousConfirmedState?.generation || 0) + 1,
                    value: state.confirmedValue
                });
                if (collectionData) collectionData[field] = state.confirmedValue;

                if (intentId !== state.latestIntentId) return;
                state.latestIntentPending = false;
                toggle.checked = state.confirmedValue;
                showSuccess(successMessage);
            } else if (intentId === state.latestIntentId) {
                state.latestIntentPending = false;
                toggle.checked = state.confirmedValue;
                showCollectionError('Failed to update collection: ' + (data.error || 'unknown error'));
            }
        } catch {
            if (intentId !== state.latestIntentId) return;
            state.latestIntentPending = false;
            toggle.checked = state.confirmedValue;
            showCollectionError(networkErrorMessage);
        }
    };

    const queued = state.queue.then(run, run);
    state.queue = queued;
    return queued;
}

/**
 * Persist the collection's public/private (egress) flag.
 */
function updateCollectionIsPublic(isPublic) {
    return queueCollectionBooleanUpdate({
        toggle: document.getElementById('collection-is-public'),
        field: 'is_public',
        requestedValue: isPublic,
        successMessage: isPublic
            ? 'Collection marked public.'
            : 'Collection marked private (local-only).',
        networkErrorMessage: 'Failed to update collection privacy setting.'
    });
}

/**
 * Persist the collection's research-agent availability flag (usability, not
 * egress): whether the LangGraph agent offers this collection as a tool.
 */
function updateCollectionAgentEnabled(agentEnabled) {
    return queueCollectionBooleanUpdate({
        toggle: document.getElementById('collection-agent-enabled'),
        field: 'agent_enabled',
        requestedValue: agentEnabled,
        successMessage: agentEnabled
            ? 'Collection available to the research agent.'
            : 'Collection hidden from the research agent.',
        networkErrorMessage: 'Failed to update research-agent setting.'
    });
}

/**
 * Load collection details and documents
 */
async function loadCollectionDetails() {
    const currentLoadId = ++collectionDetailsLoadId;
    const rebuildRequestId = ++collectionRebuildRequestId;
    const booleanGenerationsAtRequest = new Map(
        ['is_public', 'agent_enabled'].map(field => [
            field,
            confirmedCollectionBooleanState.get(field)?.generation || 0
        ])
    );

    try {
        const response = await safeFetchWithAuth(URLBuilder.build(URLS.LIBRARY_API.COLLECTION_DOCUMENTS, COLLECTION_ID));
        if (currentLoadId !== collectionDetailsLoadId) return;
        const data = await response.json();
        if (currentLoadId !== collectionDetailsLoadId) return;

        if (data.success) {
            collectionData = data.collection;
            // A collection bootstrap/reload can begin before a checkbox PUT
            // commits and finish after it. Preserve only fields whose newer
            // server-confirmed mutation superseded this GET; the rest of the
            // payload (documents, notes, metadata, and untouched flags) still
            // hydrates normally.
            for (const field of ['is_public', 'agent_enabled']) {
                const confirmedState = confirmedCollectionBooleanState.get(field);
                const loadedValue = field === 'agent_enabled'
                    ? collectionData[field] !== false
                    : !!collectionData[field];
                let authoritativeValue = loadedValue;
                if (
                    confirmedState &&
                    confirmedState.generation !== booleanGenerationsAtRequest.get(field)
                ) {
                    authoritativeValue = confirmedState.value;
                }
                collectionData[field] = authoritativeValue;

                const toggle = document.getElementById(collectionBooleanToggleIds[field]);
                const writeState = toggle && collectionBooleanWriteStates.get(toggle);
                if (writeState) {
                    // Even while the latest intent owns the visible checkbox,
                    // retain the newest authoritative value as its rollback
                    // target if that pending write fails.
                    writeState.confirmedValue = authoritativeValue;
                }
            }
            documentsData = data.documents || [];
            notesData = data.notes || [];

            // Update header
            document.getElementById('collection-name').textContent = collectionData.name;
            document.getElementById('collection-description').textContent = collectionData.description || '';
            updateCollectionRebuildInfo(collectionData, rebuildRequestId);
            renderCollectionRebuildNotice();

            // Reflect the public/private (egress) flag
            const publicToggle = document.getElementById('collection-is-public');
            if (publicToggle) {
                const writeState = collectionBooleanWriteStates.get(publicToggle);
                publicToggle.checked = writeState?.latestIntentPending
                    ? writeState.latestIntentValue
                    : !!collectionData.is_public;
            }

            // Reflect the research-agent availability flag (default available).
            const agentToggle = document.getElementById('collection-agent-enabled');
            if (agentToggle) {
                const writeState = collectionBooleanWriteStates.get(agentToggle);
                agentToggle.checked = writeState?.latestIntentPending
                    ? writeState.latestIntentValue
                    : collectionData.agent_enabled !== false;
            }

            // System collections (Notes, Library, Research History) can't be
            // deleted — the service refuses with a 409 — so don't offer a
            // button whose only outcome is an error. The server decides
            // (is_protected mirrors PROTECTED_COLLECTION_TYPES); to clear a
            // system collection's embeddings, Re-Index remains available.
            if (collectionData.is_protected) {
                const deleteBtn = document.getElementById('delete-collection-btn');
                if (deleteBtn) deleteBtn.style.display = 'none';
            }

            // Update statistics
            updateStatistics();

            // Display collection's embedding settings
            displayCollectionEmbeddingSettings();

            // Render documents and notes
            renderDocuments();
            renderNotes();

            // Show search section if any documents are indexed
            initCollectionSearch();
        } else {
            showCollectionError('Failed to load collection details: ' + data.error);
        }
    } catch (error) {
        if (currentLoadId !== collectionDetailsLoadId) return;
        SafeLogger.error('Error loading collection details:', error);
        showCollectionError('Failed to load collection details');
    }
}

/**
 * Update statistics
 */
function updateStatistics() {
    // Notes are collection members too: indexing processes them, so the
    // displayed totals must include them to match the indexing counts.
    const allItems = documentsData.concat(notesData);
    const totalDocs = allItems.length;
    const indexedDocs = allItems.filter(item => item.indexed).length;
    const unindexedDocs = totalDocs - indexedDocs;
    const totalChunks = allItems.reduce((sum, item) => sum + (item.chunk_count || 0), 0);

    document.getElementById('stat-total-docs').textContent = totalDocs;
    document.getElementById('stat-indexed-docs').textContent = indexedDocs;
    document.getElementById('stat-unindexed-docs').textContent = unindexedDocs;
    document.getElementById('stat-total-chunks').textContent = totalChunks;
}

/**
 * Display collection's embedding settings
 */
function displayCollectionEmbeddingSettings() {
    const infoContainer = document.getElementById('collection-embedding-info');

    if (collectionData.embedding_model) {
        // Collection has stored settings - display them
        // bearer:disable javascript_lang_dangerous_insert_html
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-28: all interpolations use escapeHtml/esc, numeric coercion, or hardcoded strings
        infoContainer.innerHTML = `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Provider:</span>
                <span class="ldr-info-value">${escapeHtml(getProviderLabel(collectionData.embedding_model_type))}</span>
            </div>
            <div class="ldr-info-item">
                <span class="ldr-info-label">Model:</span>
                <span class="ldr-info-value">${escapeHtml(collectionData.embedding_model)}</span>
            </div>
            <div class="ldr-info-item">
                <span class="ldr-info-label">Chunk Size:</span>
                <span class="ldr-info-value">${escapeHtml(String(collectionData.chunk_size ?? 'Not set'))} ${collectionData.chunk_size != null ? 'characters' : ''}</span>
            </div>
            <div class="ldr-info-item">
                <span class="ldr-info-label">Chunk Overlap:</span>
                <span class="ldr-info-value">${escapeHtml(String(collectionData.chunk_overlap ?? 'Not set'))} ${collectionData.chunk_overlap != null ? 'characters' : ''}</span>
            </div>
            ${collectionData.embedding_dimension ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Embedding Dimension:</span>
                <span class="ldr-info-value">${escapeHtml(String(collectionData.embedding_dimension))}</span>
            </div>
            ` : ''}
            ${collectionData.splitter_type ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Splitter Type:</span>
                <span class="ldr-info-value">${escapeHtml(collectionData.splitter_type)}</span>
            </div>
            ` : ''}
            ${collectionData.distance_metric ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Distance Metric:</span>
                <span class="ldr-info-value">${escapeHtml(collectionData.distance_metric)}</span>
            </div>
            ` : ''}
            ${collectionData.index_type ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Index Type:</span>
                <span class="ldr-info-value">${escapeHtml(collectionData.index_type)}</span>
            </div>
            ` : ''}
            ${collectionData.normalize_vectors !== null && collectionData.normalize_vectors !== undefined ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Normalize Vectors:</span>
                <span class="ldr-info-value">${collectionData.normalize_vectors ? 'Yes' : 'No'}</span>
            </div>
            ` : ''}
            ${collectionData.index_file_size ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Index File Size:</span>
                <span class="ldr-info-value">${escapeHtml(String(collectionData.index_file_size))}</span>
            </div>
            ` : ''}
        `;
    } else {
        // Collection not yet indexed - no settings stored
        infoContainer.innerHTML = `
            <div class="ldr-alert ldr-alert-info">
                <i class="fas fa-info-circle"></i> This collection hasn't been indexed yet. Settings will be stored when you index documents.
            </div>
        `;
    }
}

/**
 * Get provider label (simplified version)
 */
function getProviderLabel(providerValue) {
    const providerMap = {
        'sentence_transformers': 'Sentence Transformers',
        'ollama': 'Ollama',
        'openai': 'OpenAI',
        'anthropic': 'Anthropic',
        'cohere': 'Cohere'
    };
    return providerMap[providerValue] || providerValue || 'Not configured';
}

/**
 * Get model label (simplified version)
 */
function getModelLabel(modelValue, provider) {
    if (!modelValue) return 'Not configured';
    if (provider === 'ollama' && modelValue.includes(':')) {
        return modelValue.split(':')[0];
    }
    return modelValue;
}

/**
 * Render documents list
 */
function renderDocuments() {
    const container = document.getElementById('documents-list');
    const noDocsMessage = document.getElementById('no-documents-message');

    // Filter documents based on current filter
    let filteredDocs = documentsData;
    if (currentFilter === 'indexed') {
        filteredDocs = documentsData.filter(doc => doc.indexed);
    } else if (currentFilter === 'unindexed') {
        filteredDocs = documentsData.filter(doc => !doc.indexed);
    }

    if (filteredDocs.length === 0) {
        container.style.display = 'none';
        // Don't show the misleading "No documents… Upload Files" empty state
        // when the collection still has (visible) notes below — that would
        // claim the collection is empty while the Notes section lists its
        // contents and would steer the user to the wrong action. Only show it
        // when nothing matches the current filter in either section.
        noDocsMessage.style.display = getFilteredNotes().length === 0 ? 'flex' : 'none';
        return;
    }

    container.style.display = 'block';
    noDocsMessage.style.display = 'none';

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-28: all interpolations use escapeHtml/esc, numeric coercion, or hardcoded strings
    container.innerHTML = filteredDocs.map(doc => `
        <div class="ldr-document-item ${doc.indexed ? 'ldr-indexed' : 'ldr-unindexed'}" data-document-id="${escapeHtml(doc.id)}">
            <a href="/library/document/${encodeURIComponent(doc.id)}" class="ldr-document-link" style="text-decoration: none; color: inherit; display: block; flex: 1;">
                <div class="ldr-document-info">
                    <div class="ldr-document-title">
                        ${escapeHtml(doc.filename)}
                        ${doc.has_pdf ? '<i class="fas fa-file-pdf" style="color: var(--error-color); margin-left: 8px;" title="PDF stored"></i>' : ''}
                        ${doc.has_text_db ? '<i class="fas fa-file-alt" style="color: var(--success-color); margin-left: 8px;" title="Text content available"></i>' : ''}
                        ${doc.in_other_collections ? `<i class="fas fa-link" style="color: var(--accent-primary); margin-left: 8px;" title="In ${escapeHtml(String(doc.other_collections_count + 1))} collections"></i>` : ''}
                    </div>
                    <div class="ldr-document-meta">
                        ${doc.file_size ? `Size: ${window.formatBytes(doc.file_size)} • ` : ''}
                        ${doc.source_type && doc.source_type !== 'unknown' ? `<span class="ldr-badge ldr-badge-info">${escapeHtml(doc.source_type.replace('_', ' '))}</span> • ` : ''}
                        ${doc.indexed ?
                            `<span class="ldr-badge ldr-badge-success">Indexed (${escapeHtml(String(doc.chunk_count))} chunks)</span>` :
                            '<span class="ldr-badge ldr-badge-warning">Not indexed</span>'
                        }
                        ${doc.last_indexed_at ? ` • Last indexed: ${escapeHtml(new Date(doc.last_indexed_at).toLocaleString())}` : ''}
                    </div>
                </div>
            </a>
            <div class="ldr-document-actions">
                <button class="ldr-btn-remove-from-collection" data-doc-action="remove"
                        title="Remove from collection. ${doc.in_other_collections ? 'Document exists in other collections.' : 'Document will be deleted (not in other collections).'}">
                    <i class="fas fa-unlink"></i>
                </button>
                <button class="ldr-btn-delete-doc" data-doc-action="delete"
                        title="Permanently delete this document, including PDF and text content. This cannot be undone.">
                    <i class="fas fa-trash"></i>
                </button>
            </div>
        </div>
    `).join('');
}

/**
 * Apply the active indexed/unindexed filter to the notes list. Notes use an
 * `indexed` flag like documents, and updateStatistics() counts them in the
 * Indexed/Not-Indexed totals, so the filter must scope the notes too or the
 * visible set would contradict the counts.
 */
function getFilteredNotes() {
    if (currentFilter === 'indexed') {
        return notesData.filter(note => note.indexed);
    } else if (currentFilter === 'unindexed') {
        return notesData.filter(note => !note.indexed);
    }
    return notesData;
}

/**
 * Render notes list (notes are collection members served in the separate
 * `notes` array of the collection-documents payload)
 */
function renderNotes() {
    const section = document.getElementById('notes-section');
    const container = document.getElementById('notes-list');
    if (!section || !container) return;

    const filteredNotes = getFilteredNotes();
    if (filteredNotes.length === 0) {
        section.style.display = 'none';
        container.innerHTML = '';
        return;
    }

    section.style.display = 'block';

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-06-06: all interpolations use escapeHtml, numeric coercion, or hardcoded strings
    container.innerHTML = filteredNotes.map(note => `
        <div class="ldr-document-item ${note.indexed ? 'ldr-indexed' : 'ldr-unindexed'}">
            <a href="/notes/${encodeURIComponent(note.id)}" class="ldr-document-link" style="text-decoration: none; color: inherit; display: block; flex: 1;">
                <div class="ldr-document-info">
                    <div class="ldr-document-title">
                        <i class="fas fa-sticky-note" style="color: var(--accent-primary); margin-right: 8px;" title="Note"></i>
                        ${escapeHtml(note.title || 'Untitled')}
                        ${note.pinned ? '<i class="fas fa-thumbtack" style="color: var(--accent-primary); margin-left: 8px;" title="Pinned"></i>' : ''}
                    </div>
                    <div class="ldr-document-meta">
                        <span class="ldr-badge ldr-badge-info">note</span> •
                        ${note.indexed ?
                            `<span class="ldr-badge ldr-badge-success">Indexed (${escapeHtml(String(note.chunk_count))} chunks)</span>` :
                            '<span class="ldr-badge ldr-badge-warning">Not indexed</span>'
                        }
                        ${note.updated_at ? ` • Updated: ${escapeHtml(new Date(note.updated_at).toLocaleString())}` : ''}
                    </div>
                </div>
            </a>
        </div>
    `).join('');
}

/**
 * Filter documents
 */
function filterDocuments(filter) {
    currentFilter = filter;

    // Update button states
    document.querySelectorAll('.ldr-filter-controls .ldr-btn-collections').forEach(btn => {
        btn.classList.remove('ldr-active');
    });
    event.target.classList.add('ldr-active');

    renderDocuments();
    // Notes are counted in the indexed/unindexed stats, so they must honor the
    // same filter — otherwise the visible notes contradict the counts.
    renderNotes();
}


/**
 * index/start signals "already running" as 409 {success: false, error} from
 * rag.py's _indexing_conflict. The CSRF layer's sign-in-changed 409 has no
 * `success` field and carries X-LDR-Auth-Changed; it is shown as an error and
 * must not resume polling.
 */
function isIndexingConflict(response, data) {
    return response.status === 409
        && data?.success === false
        && !window.api?.isAuthContextChanged?.(response);
}

/**
 * Index collection documents (background indexing)
 */
async function indexCollection(forceReindex) {
    SafeLogger.log('Index Collection button clicked, force_reindex:', forceReindex);

    const action = forceReindex ? 're-index' : 'index';
    const confirmation = forceReindex
        ? 'Rebuild the index for all documents? Keep the rebuild running until it completes. Cancelling before publication keeps the previous index, if any, and its settings. Automatic indexing does not retry a cancelled rebuild.'
        : `${action.charAt(0).toUpperCase() + action.slice(1)} all documents in this collection?`;
    if (!confirm(confirmation)) {
        return;
    }

    try {
        const csrfToken = window.api ? window.api.getCsrfToken() : '';

        // Start background indexing
        const response = await safeFetchWithAuth(`/library/api/collections/${COLLECTION_ID}/index/start`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken
            },
            body: JSON.stringify({ force_reindex: forceReindex })
        });

        const data = await response.json();

        if (!data.success) {
            if (isIndexingConflict(response, data)) {
                // Already indexing
                showCollectionError(data.error || 'Indexing is already in progress');
                latestIndexingStatus = null;
                showProgressUI();
                startPolling();
            } else {
                showCollectionError(data.error || 'Failed to start indexing');
            }
            return;
        }

        SafeLogger.log('Background indexing started, task_id:', data.task_id);

        // Show progress UI and start polling
        latestIndexingStatus = { status: 'processing', force_reindex: forceReindex };
        document.getElementById('progress-fill').style.width = '0%';
        showProgressUI();
        updateProgressFromStatus(latestIndexingStatus);
        addLogEntry(forceReindex
            ? 'Rebuilding the collection index. Cancelling before publication keeps the previous index and settings; automatic indexing does not retry this rebuild.'
            : 'Indexing started in background...', 'info');
        startPolling();

    } catch (error) {
        SafeLogger.error('Error starting indexing:', error);
        showCollectionError('Failed to start indexing');
    }
}

/**
 * Check if there's an active indexing task and resume UI
 */
async function checkAndResumeIndexing() {
    const resumeUiGeneration = progressUiGeneration;
    const rebuildRequestId = ++collectionRebuildRequestId;
    try {
        const response = await safeFetchWithAuth(`/library/api/collections/${COLLECTION_ID}/index/status`);
        if (resumeUiGeneration !== progressUiGeneration) return;
        if (!response.ok) {
            throw new Error('Indexing status request failed (HTTP ' + response.status + ')');
        }
        const data = await response.json();
        if (resumeUiGeneration !== progressUiGeneration) return;

        updateProgressFromStatus(data, rebuildRequestId);
        if (data.status === 'processing' || data.worker_active === true) {
            SafeLogger.log('Active indexing task found, resuming UI');
            showProgressUI();
            startPolling();
        } else if (ResearchStates.isFailed(data.status)) {
            showProgressUI();
            renderIndexingFailure(data);
            hideProgressUI({ keepVisible: true });
        }
    } catch (error) {
        SafeLogger.error('Error checking indexing status:', error);
    }
}

/**
 * Show the progress UI
 */
function showProgressUI() {
    progressUiGeneration += 1;
    if (progressHideTimer !== null) {
        clearTimeout(progressHideTimer);
        progressHideTimer = null;
    }

    const progressSection = document.getElementById('indexing-progress');
    const cancelBtn = document.getElementById('cancel-indexing-btn');
    const indexBtn = document.getElementById('index-collection-btn');
    const reindexBtn = document.getElementById('reindex-collection-btn');

    progressSection.style.display = 'block';
    cancelBtn.style.display = 'inline-block';
    indexBtn.disabled = true;
    reindexBtn.disabled = true;
    renderCollectionRebuildNotice();
}

/** A durable collection notice must outlive the transient progress panel. */
function updateCollectionRebuildInfo(data, requestId) {
    if (requestId < appliedCollectionRebuildRequestId) return;
    const keys = ['rebuild_pending_since', 'has_published_index', 'index_last_updated_at'];
    if (!keys.some(key => Object.hasOwn(data, key))) return;
    appliedCollectionRebuildRequestId = requestId;
    for (const key of keys) {
        if (Object.hasOwn(data, key)) collectionRebuildInfo[key] = data[key];
    }
}

function renderCollectionRebuildNotice() {
    const notice = document.getElementById('collection-rebuild-notice');
    if (!notice) return;
    const state = { ...(latestIndexingStatus || {}), ...collectionRebuildInfo };
    const rebuilding = state.force_reindex === true && (
        state.status === 'processing' || state.worker_active === true
    );
    if (!rebuilding && !state.rebuild_pending_since) {
        notice.style.display = 'none';
        notice.textContent = '';
        return;
    }
    const cancelling = rebuilding && state.status === 'cancelled';
    const operation = cancelling
        ? 'Cancellation requested; waiting for the rebuild to stop. If publication has started, it finishes and the new index becomes active.'
        : rebuilding
            ? 'Rebuilding the collection index. Keep this operation running until it completes.'
            : 'The last collection rebuild did not complete. Run Re-Index All again to replace the index.';
    const retained = state.has_published_index === false
        ? ' No index has been published for this collection.'
        : rebuilding
            ? ' Search uses the currently published index, if any. The final result will show whether the replacement was published.'
            : state.has_published_index === true
                ? ' The previous index remains in use and may be outdated.'
                : ' The published index, if any, may be outdated.';
    const date = state.index_last_updated_at && new Date(state.index_last_updated_at);
    const updated = date && !Number.isNaN(date.getTime())
        ? ` Index last updated: ${date.toLocaleString()}.`
        : '';
    const cancellation = rebuilding && !cancelling
        ? ' Cancelling before publication keeps the previous index, if any, and its settings.'
        : '';
    const message = operation + retained + updated + cancellation
        + ' Automatic indexing can add unindexed documents, but does not retry this full rebuild.';
    if (notice.textContent !== message) notice.textContent = message;
    notice.style.display = 'block';
}

/**
 * Hide the progress UI
 */
function hideProgressUI({ keepVisible = false } = {}) {
    const progressSection = document.getElementById('indexing-progress');
    const cancelBtn = document.getElementById('cancel-indexing-btn');
    const indexBtn = document.getElementById('index-collection-btn');
    const reindexBtn = document.getElementById('reindex-collection-btn');
    const spinner = document.getElementById('indexing-spinner');

    cancelBtn.style.display = 'none';
    indexBtn.disabled = false;
    reindexBtn.disabled = false;

    if (progressHideTimer !== null) {
        clearTimeout(progressHideTimer);
        progressHideTimer = null;
    }

    if (!keepVisible) {
        const hideGeneration = progressUiGeneration;
        const hideTimer = setTimeout(() => {
            if (
                progressHideTimer !== hideTimer ||
                progressUiGeneration !== hideGeneration
            ) return;

            progressHideTimer = null;
            progressSection.style.display = 'none';
        }, 5000);
        progressHideTimer = hideTimer;
    }
}

/**
 * Start polling for indexing status
 */
function startPolling() {
    // Retire both the timer and any response still awaiting I/O from the
    // previous poll owner. A restarted poll must not be repainted by that
    // older response after the new owner reaches a terminal state.
    if (activeIndexingPoll) {
        if (activeIndexingPoll.interval !== null) {
            clearInterval(activeIndexingPoll.interval);
        }
        activeIndexingPoll.interval = null;
        activeIndexingPoll = null;
        indexingPollInterval = null;
    } else if (indexingPollInterval) {
        clearInterval(indexingPollInterval);
        indexingPollInterval = null;
    }

    const poll = {
        interval: null,
        requestInFlight: false
    };
    activeIndexingPoll = poll;

    // Poll every 2 seconds
    poll.interval = setInterval(async () => {
        if (activeIndexingPoll !== poll || poll.requestInFlight) return;
        poll.requestInFlight = true;
        const rebuildRequestId = ++collectionRebuildRequestId;

        try {
            const response = await safeFetchWithAuth(`/library/api/collections/${COLLECTION_ID}/index/status`);
            if (activeIndexingPoll !== poll) return;
            if (!response.ok) {
                throw new Error('Indexing status request failed (HTTP ' + response.status + ')');
            }
            const data = await response.json();
            if (activeIndexingPoll !== poll) return;

            const isTerminal = (
                ResearchStates.isTerminal(data.status) ||
                data.status === 'idle'
            ) && data.worker_active !== true;
            if (isTerminal) {
                clearInterval(poll.interval);
                poll.interval = null;
                activeIndexingPoll = null;
                indexingPollInterval = null;
            }

            updateProgressFromStatus(data, rebuildRequestId);

            // Stop polling if indexing is done
            if (isTerminal) {
                if (ResearchStates.isCompleted(data.status)) {
                    addLogEntry(data.progress_message || 'Indexing completed!', 'success');
                    hideProgressUI();
                } else if (ResearchStates.isFailed(data.status)) {
                    // Keep visible so the user can read the failure logs
                    // rendered by renderIndexingFailure. The separate
                    // collection notice survives this progress panel.
                    renderIndexingFailure(data);
                    hideProgressUI({ keepVisible: true });
                } else if (ResearchStates.isCancelled(data.status)) {
                    addLogEntry(data.force_reindex === true
                        ? 'Rebuild cancelled. The previous index, if any, remains in use. Run Re-Index All again; automatic indexing does not retry it.'
                        : 'Indexing was cancelled', 'warning');
                    hideProgressUI();
                } else {
                    // Terminal status that is not completed/failed/cancelled
                    // (e.g. an "error" enum). Hide the progress UI without
                    // rendering a log entry — the status itself is the
                    // source of truth and the backend already wrote one.
                    hideProgressUI();
                }

                loadCollectionDetails();
            }
        } catch (error) {
            if (activeIndexingPoll !== poll) return;
            SafeLogger.error('Error polling status:', error);
        } finally {
            poll.requestInFlight = false;
        }
    }, 2000);
    indexingPollInterval = poll.interval;
}

function renderIndexingFailure(data) {
    const result = data.result || {};
    const durable = result.durable_indexed_documents;
    const chunks = result.durable_indexed_chunks;
    const summary = data.error_message || data.progress_message || 'Indexing failed';
    const durableText = Number.isInteger(durable)
        ? ` Durable vector store: ${durable} document(s), ${chunks || 0} chunk(s).`
        : '';
    // Trim a trailing period+whitespace from the summary so appending
    // ``${durableText}`` (which already starts with a space and may end
    // with its own period) doesn't render double punctuation like
    // "Indexing failed: Disk full. Durable vector store: ...".
    const trimmedSummary = summary.replace(/\.\s*$/, '');
    addLogEntry(`Indexing failed: ${trimmedSummary}${durableText}`, 'error');
    for (const item of (result.errors || []).slice(0, 20)) {
        addLogEntry(`${item.title || item.doc_id || 'Document'}: ${item.error || 'Indexing failed'}`, 'error');
    }
    // SECURITY: this writes via textContent (never innerHTML) so user-
    // controlled fields (document titles, error strings) are rendered as
    // text and cannot be parsed as HTML. addLogEntry below is the same.
    const progressText = document.getElementById('progress-text');
    if (progressText) progressText.textContent = `${trimmedSummary}${durableText}`;
}

/**
 * Update progress UI from status data
 */
function updateProgressFromStatus(data, requestId = ++collectionRebuildRequestId) {
    latestIndexingStatus = data;
    updateCollectionRebuildInfo(data, requestId);
    renderCollectionRebuildNotice();
    const progressFill = document.getElementById('progress-fill');
    const progressText = document.getElementById('progress-text');
    const title = document.getElementById('indexing-progress-title');
    if (title) title.textContent = data.force_reindex === true
        ? 'Rebuilding Collection Index' : 'Indexing Progress';

    if (data.progress_total > 0) {
        const percent = Math.round((data.progress_current / data.progress_total) * 100);
        progressFill.style.width = percent + '%';
    }

    if (data.status === 'cancelled' && data.worker_active === true) {
        progressText.textContent = data.force_reindex === true
            ? 'Cancelling rebuild; waiting for active work to finish...'
            : 'Cancelling indexing; waiting for active work to finish...';
    } else if (data.progress_message) {
        progressText.textContent = data.progress_message;
    } else if (data.status === 'processing' && data.force_reindex === true) {
        progressText.textContent = 'Rebuilding the collection index...';
    }
}

/**
 * Cancel indexing
 */
async function cancelIndexing() {
    const confirmation = latestIndexingStatus?.force_reindex === true
        ? 'Cancel this rebuild? Before publication, cancelling leaves the previous index, if any, and its settings working; it may be outdated. Automatic indexing does not retry this rebuild. If publication has already started, it finishes and the new index becomes active.'
        : 'Cancel the current indexing operation?';
    if (!confirm(confirmation)) {
        return;
    }

    try {
        const csrfToken = window.api ? window.api.getCsrfToken() : '';

        const response = await safeFetchWithAuth(`/library/api/collections/${COLLECTION_ID}/index/cancel`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': csrfToken
            }
        });

        const data = await response.json();

        if (data.success) {
            const progressText = document.getElementById('progress-text');
            progressText.textContent = 'Cancelling...';
            addLogEntry('Cancellation requested...', 'warning');
        } else {
            showCollectionError(data.error || 'Failed to cancel indexing');
        }
    } catch (error) {
        SafeLogger.error('Error cancelling indexing:', error);
        showCollectionError('Failed to cancel indexing');
    }
}

/**
 * Add a log entry to the indexing progress log.
 *
 * SECURITY: this writes ``message`` via ``entry.textContent = message``
 * (never innerHTML), so user-controlled data flowing in from
 * ``renderIndexingFailure`` (document titles, error_message, durable
 * counts) is rendered as text and cannot be parsed as HTML. The
 * companion error path in ``collection_create.js`` uses ``escapeHtml``
 * for the same reason. Do NOT change this to innerHTML without
 * revisiting every caller and adding escaping — the existing path is
 * the safe one.
 */
function addLogEntry(message, type = 'info') {
    const progressLog = document.getElementById('progress-log');
    const entry = document.createElement('div');
    entry.className = `ldr-log-entry ldr-log-${type}`;
    entry.textContent = message;
    progressLog.appendChild(entry);
    progressLog.scrollTop = progressLog.scrollHeight;
}

/**
 * Delete collection
 */
async function deleteCollection() {
    if (!confirm(`Are you sure you want to delete "${collectionData.name}"? Documents that belong only to this collection will be deleted with it. This action cannot be undone.`)) return;

    try {
        const csrfToken = window.api ? window.api.getCsrfToken() : '';
        const response = await safeFetchWithAuth(URLBuilder.build(URLS.LIBRARY_API.COLLECTION_DETAILS, COLLECTION_ID), {
            method: 'DELETE',
            headers: {
                'X-CSRFToken': csrfToken
            }
        });

        const data = await response.json();
        if (data.success) {
            showSuccess(`Collection "${collectionData.name}" deleted successfully`);
            // Redirect to collections page
            setTimeout(() => {
                window.location.href = '/library/collections';
            }, 1000);
        } else {
            showCollectionError('Failed to delete collection: ' + data.error);
        }
    } catch (error) {
        SafeLogger.error('Error deleting collection:', error);
        showCollectionError('Failed to delete collection');
    }
}

/**
 * Show success message
 */
function showSuccess(message) {
    alert('Success: ' + message);
}

/**
 * Show error message.
 *
 * The name is deliberately page-specific. This script is loaded from
 * collection_details.html's `{% block content %}`, which base.html renders
 * BEFORE its shared deferred scripts (services/ui.js and friends), so a
 * top-level `showError` here is executed first and then overwritten by
 * ui.js's `showError(container, message)` — which reads the message as a
 * container id, finds no element, and renders nothing: every error on this
 * page was silently dropped. `showSuccess` has no shared counterpart and
 * keeps its plain name. See
 * tests/web/test_notes_js_global_shadowing.py, which sweeps this file's
 * top-level names against the shared scripts.
 */
function showCollectionError(message) {
    alert('Error: ' + message);
}

/**
 * Remove document from this collection
 * If not in other collections, the document will be deleted
 */
async function removeDocumentFromCollection(documentId) {
    // Check if DeleteManager is available
    if (typeof DeleteManager !== 'undefined' && DeleteManager.removeFromCollection) {
        DeleteManager.removeFromCollection(documentId, COLLECTION_ID, {
            onSuccess: () => loadCollectionDetails()
        });
    } else {
        // Fallback to simple confirm
        if (!confirm('Remove this document from the collection? If not in other collections, it will be deleted.')) {
            return;
        }

        try {
            const csrfToken = window.api ? window.api.getCsrfToken() : '';
            const response = await safeFetchWithAuth(`/library/api/collection/${COLLECTION_ID}/document/${documentId}`, {
                method: 'DELETE',
                headers: {
                    'X-CSRFToken': csrfToken
                }
            });

            const data = await response.json();
            if (data.success) {
                const message = data.document_deleted
                    ? 'Document removed and deleted (not in other collections)'
                    : 'Document removed from collection';
                showSuccess(message);
                loadCollectionDetails();
            } else {
                showCollectionError('Failed to remove document: ' + data.error);
            }
        } catch (error) {
            SafeLogger.error('Error removing document:', error);
            showCollectionError('Failed to remove document');
        }
    }
}

/**
 * Delete document completely (from all collections)
 */
async function deleteDocumentCompletely(documentId) {
    // Check if DeleteManager is available
    if (typeof DeleteManager !== 'undefined' && DeleteManager.deleteDocument) {
        DeleteManager.deleteDocument(documentId, {
            onSuccess: () => loadCollectionDetails()
        });
    } else {
        // Fallback to simple confirm
        if (!confirm('Permanently delete this document? This cannot be undone.')) {
            return;
        }

        try {
            const csrfToken = window.api ? window.api.getCsrfToken() : '';
            const response = await safeFetchWithAuth(`/library/api/document/${documentId}`, {
                method: 'DELETE',
                headers: {
                    'X-CSRFToken': csrfToken
                }
            });

            const data = await response.json();
            if (data.success) {
                showSuccess('Document deleted successfully');
                loadCollectionDetails();
            } else {
                showCollectionError('Failed to delete document: ' + data.error);
            }
        } catch (error) {
            SafeLogger.error('Error deleting document:', error);
            showCollectionError('Failed to delete document');
        }
    }
}

// =============================================================================
// Collection Semantic Search
// =============================================================================

let searchDebounceTimer = null;
let searchListenersAttached = false;
let collectionSearchId = 0;

/**
 * Initialize search section if collection has indexed documents.
 */
function initCollectionSearch() {
    const hasIndexed = documentsData.some(function(d) { return d.indexed; })
        || notesData.some(function(n) { return n.indexed; });
    const section = document.getElementById('collection-search-section');
    if (!section) return;

    if (hasIndexed) {
        section.style.display = 'block';

        // Only attach listeners once to avoid duplicates on repeated loadCollectionDetails() calls
        if (!searchListenersAttached) {
            searchListenersAttached = true;
            const searchBtn = document.getElementById('collection-search-btn');
            const searchInput = document.getElementById('collection-search-input');

            if (searchBtn) {
                searchBtn.addEventListener('click', function() {
                    const query = searchInput ? searchInput.value.trim() : '';
                    if (query) searchCollection(query);
                });
            }
            if (searchInput) {
                searchInput.addEventListener('keydown', function(e) {
                    if (e.key === 'Enter') {
                        e.preventDefault();
                        const query = searchInput.value.trim();
                        if (query) searchCollection(query);
                    }
                });
                searchInput.addEventListener('input', function() {
                    clearTimeout(searchDebounceTimer);
                    const query = searchInput.value.trim();
                    if (!query) {
                        // Clearing the field retires any request that is still
                        // awaiting the server. Otherwise that old response can
                        // repopulate results after the user deliberately
                        // cleared them.
                        collectionSearchId += 1;
                        const container = document.getElementById('collection-search-results');
                        if (container) container.innerHTML = '';
                        return;
                    }
                    searchDebounceTimer = setTimeout(function() {
                        searchCollection(query);
                    }, 500);
                });
            }
        }
    }
}

/**
 * Search the collection semantically.
 */
async function searchCollection(query) {
    const container = document.getElementById('collection-search-results');
    if (!container) return;

    const currentSearchId = ++collectionSearchId;

    // Show loading
    // bearer:disable javascript_lang_dangerous_insert_html
    container.innerHTML = '<div class="ldr-hybrid-loading"><div class="ldr-spinner" style="width:16px;height:16px;border-width:2px;"></div> Searching...</div>';

    try {
        // Reuse LibrarySearch module for the API call
        if (!window.LibrarySearch || !window.LibrarySearch.performSemanticSearch) {
            // bearer:disable javascript_lang_dangerous_insert_html
            container.innerHTML = '<div class="ldr-empty-state"><p>Search module not loaded. Please refresh.</p></div>';
            return;
        }

        const data = await window.LibrarySearch.performSemanticSearch(COLLECTION_ID, query, 20);

        // Discard stale results if a newer search was initiated
        if (currentSearchId !== collectionSearchId) return;

        if (data.success && data.results) {
            if (data.results.length === 0) {
                // bearer:disable javascript_lang_dangerous_insert_html
                container.innerHTML = '<div class="ldr-empty-state"><i class="fas fa-search fa-2x"></i><p>No matching results found.</p></div>';
                return;
            }

            const cardConfig = window.LibrarySearch.getLibraryCardConfig
                ? window.LibrarySearch.getLibraryCardConfig()
                : undefined;
            const createCard = window.SemanticSearch && window.SemanticSearch.createSemanticResultCard;

            if (createCard) {
                const fragment = document.createDocumentFragment();
                for (let i = 0; i < data.results.length; i++) {
                    fragment.appendChild(createCard(data.results[i], cardConfig, query));
                }
                container.innerHTML = '';
                container.appendChild(fragment);
            } else {
                // bearer:disable javascript_lang_dangerous_insert_html
                container.innerHTML = '<div class="ldr-empty-state"><p>Semantic search module not loaded.</p></div>';
            }
        } else {
            // bearer:disable javascript_lang_dangerous_insert_html
            container.innerHTML = '<div class="ldr-empty-state"><i class="fas fa-exclamation-triangle fa-2x"></i><p>' + escapeHtml(data.error || 'Search failed') + '</p></div>';
        }
    } catch (error) {
        // A rejected older request must not replace a newer search result with
        // its generic error state.
        if (currentSearchId !== collectionSearchId) return;
        SafeLogger.error('Collection search error:', error);
        // bearer:disable javascript_lang_dangerous_insert_html
        container.innerHTML = '<div class="ldr-empty-state"><i class="fas fa-exclamation-triangle fa-2x"></i><p>Search failed. Please try again.</p></div>';
    }
}

// Exposed on window so vitest can exercise the checked-in runtime helpers.
window.getProviderLabel = getProviderLabel;
window.renderIndexingFailure = renderIndexingFailure;
window.checkAndResumeIndexing = checkAndResumeIndexing;
window.showProgressUI = showProgressUI;
window.hideProgressUI = hideProgressUI;
window.startPolling = startPolling;
window.displayCollectionEmbeddingSettings = displayCollectionEmbeddingSettings;
window.setCollectionDataForTesting = (data) => { collectionData = data; };
window.updateCollectionIsPublic = updateCollectionIsPublic;
window.updateCollectionAgentEnabled = updateCollectionAgentEnabled;
// These handlers are referenced by inline controls rendered by the collection
// template/document cards. Assign them explicitly so the contract survives if
// this script is evaluated as a module (as it is in the direct-runtime tests).
window.filterDocuments = filterDocuments;
window.removeDocumentFromCollection = removeDocumentFromCollection;
window.deleteDocumentCompletely = deleteDocumentCompletely;