~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
/**
* Collections Manager JavaScript
* Handles the Collections page UI interactions and API calls
*/
// Store collections data
let collections = [];
// Serialize writes per toggle so the backend cannot commit rapid same-key
// changes out of order. Separate tails preserve concurrency across controls.
const autoIndexSaveState = { generation: 0, tail: Promise.resolve(), confirmedValue: null };
const backgroundSweepSaveState = { generation: 0, tail: Promise.resolve(), pendingCount: 0, confirmedValue: null };
function queueControlSettingSave(state, operation) {
const generation = ++state.generation;
const queued = state.tail.then(
() => operation(generation),
() => operation(generation)
);
state.tail = queued.catch(() => {});
return queued;
}
// safeFetch/safeFetchWithAuth are now provided by utils/safe-fetch.js loaded in base.html
// escapeHtml is the canonical window.escapeHtml from security/xss-protection.js
// (loaded first via base.html). Do NOT reintroduce a local fallback — it would
// be weaker (e.g. not escape "/") and risk a duplicate-const SyntaxError (#3706).
/**
* Initialize the page
*/
document.addEventListener('DOMContentLoaded', function() {
loadCollections();
// Setup auto-index toggle
const autoIndexToggle = document.getElementById('auto-index-toggle');
if (autoIndexToggle) {
loadAutoIndexSetting();
autoIndexToggle.addEventListener('change', saveAutoIndexSetting);
}
// Setup background-sweep toggle (scheduled reconciler)
const bgSweepToggle = document.getElementById('background-sweep-toggle');
if (bgSweepToggle) {
loadBackgroundSweepSetting();
bgSweepToggle.addEventListener('change', saveBackgroundSweepSetting);
}
});
/**
* Load the auto-index setting and update the toggle
*/
async function loadAutoIndexSetting() {
const hydrationGeneration = autoIndexSaveState.generation;
const isCurrent = () => hydrationGeneration === autoIndexSaveState.generation;
try {
const response = await safeFetchWithAuth('/settings/api/research_library.auto_index_enabled');
if (!isCurrent() || !response.ok) return;
const data = await response.json();
if (!isCurrent()) return;
const toggle = document.getElementById('auto-index-toggle');
toggle.checked = data.value === true || data.value === 'true';
// eslint-disable-next-line require-atomic-updates -- Generation checks after both awaits keep this hydration current.
autoIndexSaveState.confirmedValue = toggle.checked;
} catch (error) {
SafeLogger.error('Error loading auto-index setting:', error);
}
}
/**
* Save the auto-index setting when toggled
*/
async function saveAutoIndexSetting() {
const toggle = document.getElementById('auto-index-toggle');
const desired = toggle.checked;
if (autoIndexSaveState.confirmedValue === null) {
autoIndexSaveState.confirmedValue = !desired;
}
return queueControlSettingSave(autoIndexSaveState, async (generation) => {
const isLatest = () => generation === autoIndexSaveState.generation;
try {
const csrfToken = window.api ? window.api.getCsrfToken() : '';
const response = await safeFetchWithAuth('/settings/api/research_library.auto_index_enabled', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken
},
body: JSON.stringify({ value: desired })
});
const data = await response.json();
if (!response.ok || data.error) {
SafeLogger.error('Failed to save auto-index setting:', data.error);
if (isLatest()) toggle.checked = autoIndexSaveState.confirmedValue;
} else {
// A successful earlier write is still the rollback baseline
// when a newer intent is queued and its write later fails.
autoIndexSaveState.confirmedValue = desired;
}
} catch (error) {
SafeLogger.error('Error saving auto-index setting:', error);
if (isLatest()) toggle.checked = autoIndexSaveState.confirmedValue;
}
});
}
/**
* Read the legacy generate_rag arm of the reconciler gate.
*
* Returns true/false for the stored value, or null when it can't be determined
* (unregistered key or a transient network failure). Never throws. The two
* callers interpret null DIFFERENTLY by design: the disable action treats it as
* "maybe armed" and still clears (legacyArmNeedsClear), while the display load
* path treats it as "not known to be on" and shows the sweep state — so a blip
* neither leaves the reconciler silently armed nor flashes a false ON.
*/
async function readBackgroundSweepLegacyArm() {
try {
const legacy = await safeFetchWithAuth('/settings/api/document_scheduler.generate_rag');
if (!legacy.ok) {
SafeLogger.warn('Could not read legacy generate_rag setting:', legacy.status);
return null;
}
const legacyData = await legacy.json();
return legacyData.value === true || legacyData.value === 'true';
} catch (error) {
SafeLogger.error('Error reading legacy generate_rag setting:', error);
return null;
}
}
/**
* Whether the disable action must clear the legacy generate_rag arm.
*
* On OFF we clear it unless we are CERTAIN it is already false: a null (unknown
* — transient read failure or unregistered key) is treated as "might be armed"
* so a blip can never leave the toggle showing OFF while the reconciler keeps
* running. Clearing an already-false key is idempotent and harmless.
*/
function legacyArmNeedsClear(legacyOn) {
return legacyOn !== false;
}
function setBackgroundSweepToggleState(checked) {
const currentToggle = document.getElementById('background-sweep-toggle');
if (currentToggle) {
currentToggle.checked = checked;
}
}
async function readDocumentSchedulerEnabled() {
try {
const response = await safeFetchWithAuth('/settings/api/document_scheduler.enabled');
if (!response.ok) {
if (response.status === 404) return false;
SafeLogger.warn(
'Could not read document scheduler enabled setting:',
response.status,
response.statusText
);
return null;
}
const data = await response.json();
if (data.value === true || data.value === 'true') return true;
if (data.value === false || data.value === 'false') return false;
SafeLogger.warn('Document scheduler enabled setting had an invalid value');
return null;
} catch (error) {
SafeLogger.error('Error reading document scheduler enabled setting:', error);
return null;
}
}
function applyBackgroundSweepSchedulerGate(schedulerEnabled) {
const toggle = document.getElementById('background-sweep-toggle');
const row = document.getElementById('background-sweep-toggle-row');
const note = document.getElementById('background-sweep-disabled-note');
// Unknown means a transient read failure, so fail open. The backend
// scheduler remains the authoritative enforcement point.
const disabled = schedulerEnabled === false;
if (toggle) toggle.disabled = disabled;
if (row) {
row.setAttribute('aria-disabled', String(disabled));
row.style.cursor = disabled ? 'not-allowed' : 'pointer';
row.style.opacity = disabled ? '0.65' : '';
}
if (note) note.hidden = !disabled;
}
/**
* Load the background-sweep setting and update the toggle.
*
* Wired to document_scheduler.sweep_library_collections (#4627): an opt-in
* scheduled reconciler that periodically indexes any library documents that
* weren't indexed immediately.
*/
async function loadBackgroundSweepSetting() {
const row = document.getElementById('background-sweep-toggle-row');
try {
const response = await safeFetchWithAuth('/settings/api/document_scheduler.sweep_library_collections');
if (!response.ok) {
// Setting isn't registered yet (e.g. 404 before #4627 merges).
// Hide the whole row so the toggle isn't a dead control; it only
// appears once the backend setting exists.
if (row) row.style.display = 'none';
return;
}
const data = await response.json();
const toggle = document.getElementById('background-sweep-toggle');
let on = data.value === true || data.value === 'true';
// The reconciler is OR-gated on (sweep_library_collections OR the
// legacy generate_rag) — see _schedule_reconciler / the runtime gate
// in scheduler/background.py. Reflect the EFFECTIVE gate so the toggle
// can't read OFF while an upgraded generate_rag user is still being
// indexed every tick. Only the legacy arm needs a second read, and a
// failure reading it must NOT hide the control (the sweep read already
// succeeded) — degrade to the sweep-derived state instead.
if (!on) {
const legacyOn = await readBackgroundSweepLegacyArm();
// Display-only: treat ONLY a definite true as ON; null (unknown)
// degrades to the sweep state. This is deliberately the OPPOSITE of
// the disable action, which clears on null (legacyArmNeedsClear).
// Do NOT "unify" this with legacyArmNeedsClear — null-as-ON here
// would flash a false ON on every transient read blip.
if (legacyOn === true) {
on = true;
}
}
toggle.checked = on;
applyBackgroundSweepSchedulerGate(await readDocumentSchedulerEnabled());
// Setting exists — reveal the row.
if (row) row.style.display = 'flex';
} catch (error) {
SafeLogger.error('Error loading background-sweep setting:', error);
if (row) row.style.display = 'none';
}
}
/**
* Save the background-sweep setting when toggled.
*/
async function saveBackgroundSweepSetting() {
const toggle = document.getElementById('background-sweep-toggle');
const desired = toggle.checked;
const csrfToken = window.api ? window.api.getCsrfToken() : '';
// Capture the displayed baseline once per pending burst. A later click
// reverses an intent, not necessarily a write the server accepted.
if (backgroundSweepSaveState.pendingCount === 0) {
backgroundSweepSaveState.confirmedValue = !desired;
}
backgroundSweepSaveState.pendingCount++;
return queueControlSettingSave(backgroundSweepSaveState, async (generation) => {
const isLatest = () => generation === backgroundSweepSaveState.generation;
const putSetting = (key, value) => safeFetchWithAuth('/settings/api/' + key, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken
},
body: JSON.stringify({ value })
});
try {
const response = await putSetting('document_scheduler.sweep_library_collections', desired);
const preserveIndeterminateSuccess = (detail) => {
// Trust a successful enable. For disable, an unknown body cannot
// confirm the legacy gate is disarmed, so keep the honest ON state.
SafeLogger.warn(
'Background-sweep setting write succeeded but its response was indeterminate; preserving ON state:',
detail
);
backgroundSweepSaveState.confirmedValue = true;
if (isLatest()) setBackgroundSweepToggleState(true);
};
let data;
try {
data = await response.json();
} catch (error) {
if (response.ok) {
preserveIndeterminateSuccess(error);
return;
}
throw error;
}
if (response.ok && data == null) {
preserveIndeterminateSuccess('empty response body');
return;
}
if (!response.ok || data == null || data.error) {
SafeLogger.error('Failed to save background-sweep setting:', data?.error);
if (isLatest()) setBackgroundSweepToggleState(backgroundSweepSaveState.confirmedValue);
return;
}
// A successful ON is confirmed immediately. A successful primary
// OFF remains effectively ON until the legacy arm is disarmed.
backgroundSweepSaveState.confirmedValue = true;
// The reconciler runs while (sweep_library_collections OR generate_rag)
// is set, so turning the toggle OFF must also clear the legacy
// generate_rag arm — otherwise the sweep keeps running for upgraded
// users and this control would not actually disable the feature it
// governs. (Turning ON only needs sweep=true; the OR makes the legacy
// value irrelevant.)
//
// Read the legacy arm first so the common default install (generate_rag
// already false) skips a redundant PUT — avoiding a silent flip of the
// separately-displayed Settings-page control and a needless second
// reschedule. Crucially, this is the DISABLE action, so be conservative
// about "unknown": readBackgroundSweepLegacyArm returns null when it
// can't read the value (transient failure / unregistered), and we must
// NOT treat that as "already off" — otherwise a blip would leave the
// toggle showing OFF while generate_rag keeps the reconciler armed.
// Skip the clear ONLY when we are certain it is already false.
if (!desired && legacyArmNeedsClear(await readBackgroundSweepLegacyArm())) {
const legacy = await putSetting('document_scheduler.generate_rag', false);
const legacyData = await legacy.json();
if (!legacy.ok || legacyData.error) {
// sweep=false already persisted, but the legacy arm may still be
// set, so the reconciler can stay armed — the honest state is ON.
// Reflect reality rather than a false OFF.
SafeLogger.error('Failed to clear legacy generate_rag on OFF:', legacyData.error);
if (isLatest()) setBackgroundSweepToggleState(true);
return;
}
}
backgroundSweepSaveState.confirmedValue = desired;
} catch (error) {
SafeLogger.error('Error saving background-sweep setting:', error);
if (isLatest()) setBackgroundSweepToggleState(backgroundSweepSaveState.confirmedValue);
}
}).finally(() => {
backgroundSweepSaveState.pendingCount--;
});
}
/**
* Load document collections
*/
async function loadCollections() {
const container = document.getElementById('collections-container');
const noCollectionsMessage = document.getElementById('no-collections-message');
try {
const response = await safeFetchWithAuth(URLS.LIBRARY_API.COLLECTIONS);
const data = await response.json();
if (data.success) {
collections = data.collections || [];
if (collections.length === 0) {
container.style.display = 'none';
noCollectionsMessage.style.display = 'flex';
} else {
container.style.display = 'grid';
noCollectionsMessage.style.display = 'none';
renderCollections();
}
} else {
showCollectionsError('Failed to load collections: ' + data.error);
}
} catch (error) {
SafeLogger.error('Error loading collections:', error);
showCollectionsError('Failed to load collections');
}
}
/**
* Build the per-collection index-status markup.
*
* Shows "<indexed> of <total> indexed" plus a "<pending> pending indexing"
* badge when documents are still awaiting indexing. All values are coerced
* to non-negative integers before interpolation (XSS-safe: numeric only).
*/
function indexStatusMarkup(collection) {
const total = Math.max(0, Math.trunc(Number(collection.document_count) || 0));
const indexed = Math.min(
total,
Math.max(0, Math.trunc(Number(collection.indexed_document_count) || 0))
);
const pending = Math.max(0, total - indexed);
if (total === 0) {
return `
<div class="ldr-stat-item ldr-embedding-warning">
<i class="fas fa-info-circle"></i>
<span title="No documents to index yet">No documents</span>
</div>`;
}
const allIndexed = pending === 0;
return `
<div class="ldr-stat-item ${allIndexed ? 'ldr-embedding-info' : ''}">
<i class="fas ${allIndexed ? 'fa-check-circle' : 'fa-layer-group'}"></i>
<span title="${indexed} of ${total} documents indexed for search">${indexed} of ${total} indexed</span>
</div>
${pending > 0 ? `
<div class="ldr-stat-item ldr-embedding-warning">
<i class="fas fa-exclamation-triangle"></i>
<span class="ldr-pending-index-badge" title="${pending} documents are not yet indexed">${pending} pending indexing</span>
</div>
` : ''}`;
}
/**
* Render collections grid
*/
function renderCollections() {
const container = document.getElementById('collections-container');
// The Reindex <button> must be a SIBLING of the card <a>, never a child
// (interactive-in-interactive is invalid and pressing Enter on the anchor
// would still navigate). Each card is therefore a wrapper <div> holding the
// clickable <a> plus a separate actions row outside the anchor.
// eslint-disable-next-line no-unsanitized/property -- audited 2026-06-20: all interpolations use escapeHtml/esc, numeric coercion (indexStatusMarkup coerces counts to ints), or hardcoded strings
container.innerHTML = collections.map(collection => `
<div class="ldr-collection-card-wrapper" data-id="${escapeHtml(collection.id)}">
<a href="/library/collections/${encodeURIComponent(collection.id)}" class="ldr-collection-card" style="text-decoration: none; color: inherit; cursor: pointer;">
<div class="ldr-collection-header">
<h3>${escapeHtml(collection.name)}</h3>
${collection.description ? `<p class="ldr-collection-description">${escapeHtml(collection.description)}</p>` : ''}
</div>
<div class="ldr-collection-stats">
<div class="ldr-stat-item">
<i class="fas fa-file"></i>
<span>${Math.max(0, Math.trunc(Number(collection.document_count) || 0))} documents</span>
</div>
${collection.created_at ? `
<div class="ldr-stat-item">
<i class="fas fa-clock"></i>
<span>${new Date(collection.created_at).toLocaleDateString()}</span>
</div>
` : ''}
${indexStatusMarkup(collection)}
${collection.embedding ? `
<div class="ldr-stat-item ldr-embedding-info">
<i class="fas fa-microchip"></i>
<span title="Embedding: ${escapeHtml(collection.embedding.provider)}/${escapeHtml(collection.embedding.model)}">${escapeHtml(collection.embedding.model)}</span>
</div>
` : ''}
</div>
</a>
<div class="ldr-collection-card-actions">
<button type="button" class="ldr-reindex-btn" data-reindex-id="${escapeHtml(collection.id)}" title="Index any documents in this collection that aren't indexed yet">
<i class="fas fa-sync-alt"></i>
<span class="ldr-reindex-label">Reindex</span>
</button>
<a href="/library/collections/${encodeURIComponent(collection.id)}" class="ldr-collection-view-link" style="text-decoration: none;">
<span>View</span>
<i class="fas fa-arrow-right"></i>
</a>
</div>
</div>
`).join('');
bindReindexButtons(container);
}
/**
* Wire up the per-card Reindex buttons.
*
* The Reindex button is a sibling of the card <a> (not a child), so it can't
* itself trigger navigation. The click is still preventDefault()/
* stopPropagation()'d defensively so it never bubbles into any ancestor link
* handler. Uses a single delegated listener on the container.
*/
function bindReindexButtons(container) {
if (!container || container.dataset.reindexBound === 'true') return;
container.dataset.reindexBound = 'true';
container.addEventListener('click', (event) => {
const btn = event.target.closest('.ldr-reindex-btn');
if (!btn) return;
// Defensive: keep the click from bubbling into any ancestor link.
event.preventDefault();
event.stopPropagation();
triggerReindex(btn);
});
}
/**
* Build a Library API URL from the templated entry in URLS, substituting {id}.
*/
function collectionApiUrl(template, collectionId) {
return template.replace('{id}', encodeURIComponent(collectionId));
}
/**
* Start background indexing for a single collection and reflect progress on
* its Reindex button. Refreshes the card's counts when indexing finishes.
*
* Triggered from the per-card Reindex button (a sibling of the card <a>);
* the caller has already preventDefault()/stopPropagation()'d the click.
*/
async function triggerReindex(btn) {
const collectionId = btn.getAttribute('data-reindex-id');
if (!collectionId || btn.disabled) return;
const label = btn.querySelector('.ldr-reindex-label');
const icon = btn.querySelector('i');
const originalLabel = label ? label.textContent : '';
const setBusy = (busy, text) => {
btn.disabled = busy;
if (icon) icon.classList.toggle('fa-spin', busy);
if (label && text !== undefined) label.textContent = text;
};
setBusy(true, 'Indexing…');
try {
const csrfToken = window.api ? window.api.getCsrfToken() : '';
const startUrl = collectionApiUrl(URLS.LIBRARY_API.COLLECTION_INDEX_START, collectionId);
const response = await safeFetchWithAuth(startUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken
},
body: JSON.stringify({ force_reindex: false })
});
const data = await response.json();
if (!response.ok || !data.success) {
// 409 means indexing is already running for this collection — still
// poll so the button tracks the in-flight task to completion.
if (response.status !== 409) {
SafeLogger.error('Failed to start reindex:', data.error);
showCollectionsError(data.error || 'Failed to start indexing');
setBusy(false, originalLabel);
return;
}
}
const finalStatus = await pollReindexStatus(collectionId);
// Surface a failure that happened DURING polling (the poller only
// logs it) so the user isn't left with a silently-failed reindex.
if (finalStatus) {
if (finalStatus.status === 'failed' || finalStatus.status === 'error') {
const result = finalStatus.result || {};
const durable = Number.isInteger(result.durable_indexed_documents)
? ` Durable vector store: ${result.durable_indexed_documents} document(s), ${result.durable_indexed_chunks || 0} chunk(s).`
: '';
showCollectionsError((finalStatus.error_message || finalStatus.error || 'Indexing failed') + durable);
} else if (finalStatus.status === 'timeout') {
showCollectionsError('Indexing is taking longer than expected. It may still be running in the background — refresh to check.');
}
}
} catch (error) {
SafeLogger.error('Error starting reindex:', error);
showCollectionsError('Failed to start indexing');
setBusy(false, originalLabel);
return;
}
setBusy(false, originalLabel);
// Refresh counts so the "X of Y indexed" / pending status reflects the result.
await loadCollections();
}
/**
* Poll the collection index status until it reaches a terminal state.
* Resolves once indexing is no longer "processing".
*
* Bounded so a worker stuck at "processing" (or a flapping endpoint) can't
* disable the Reindex button forever: gives up after POLL_MAX_ATTEMPTS polls
* (~10 min at a 2s interval) or POLL_MAX_ERRORS consecutive fetch errors,
* resolving with a synthetic {status:'timeout'} / null so the caller can
* re-enable the button and surface a message. Mirrors the bounded poll in
* components/history_search.js.
*/
async function pollReindexStatus(collectionId) {
const statusUrl = collectionApiUrl(URLS.LIBRARY_API.COLLECTION_INDEX_STATUS, collectionId);
const TERMINAL = ['completed', 'failed', 'cancelled', 'error', 'idle'];
const POLL_INTERVAL_MS = 2000;
const POLL_MAX_ATTEMPTS = 300; // ~10 minutes at 2s/poll
const POLL_MAX_ERRORS = 5; // consecutive fetch errors before bailing
return new Promise((resolve) => {
let attempts = 0;
let errorCount = 0;
const poll = async () => {
attempts += 1;
try {
const response = await safeFetchWithAuth(statusUrl);
// safeFetchWithAuth does NOT throw on HTTP errors (it only diverts on 401), and the status
// endpoint's except path returns 500 with body {status:'error'}
// — which is in TERMINAL. Without this guard a single transient
// 500 would resolve the poll and fire a false "reindex failed"
// alert while indexing is still running. Treat an HTTP error as
// a transient failure (counts toward the error budget → retry),
// mirroring components/history_search.js.
if (!response.ok) {
throw new Error(`Status endpoint returned ${response.status}`);
}
const data = await response.json();
errorCount = 0;
if (TERMINAL.includes(data.status)) {
if (data.status === 'failed' || data.status === 'error') {
SafeLogger.error('Reindex failed:', data.error_message || data.error);
}
resolve(data);
return;
}
} catch (error) {
SafeLogger.error('Error polling reindex status:', error);
errorCount += 1;
if (errorCount >= POLL_MAX_ERRORS) {
resolve(null);
return;
}
}
if (attempts >= POLL_MAX_ATTEMPTS) {
SafeLogger.warn('Reindex status poll timed out; giving up.');
resolve({ status: 'timeout' });
return;
}
setTimeout(poll, POLL_INTERVAL_MS);
};
poll();
});
}
/**
* Show success message
*/
function showSuccess(message) {
alert('Success: ' + message);
}
/**
* Show error message
*/
function showCollectionsError(message) {
alert('Error: ' + message);
}
// Expose internals for unit tests (consumed in tests/js; harmless in prod).
if (typeof window !== 'undefined') {
window.indexStatusMarkup = indexStatusMarkup;
window.renderCollections = renderCollections;
window.bindReindexButtons = bindReindexButtons;
window.triggerReindex = triggerReindex;
window.collectionApiUrl = collectionApiUrl;
window.loadAutoIndexSetting = loadAutoIndexSetting;
window.saveAutoIndexSetting = saveAutoIndexSetting;
window.loadBackgroundSweepSetting = loadBackgroundSweepSetting;
window.saveBackgroundSweepSetting = saveBackgroundSweepSetting;
}