~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
feat: add robust error handling for SSE connections in RAG indexing
LearningCircuit7745553cbbe6
diff --git a/src/local_deep_research/web/static/js/rag_manager.js b/src/local_deep_research/web/static/js/rag_manager.jsindex ede5a4b85e..34a5f0a4c0 100644--- a/src/local_deep_research/web/static/js/rag_manager.js+++ b/src/local_deep_research/web/static/js/rag_manager.js@@ -468,67 +468,128 @@ async function indexAllDocuments(forceReindex) {// Add initial log entryaddLogEntry('Starting indexing process...', 'info');- try {- const eventSource = new EventSource(`/library/api/rag/index-all?force_reindex=${forceReindex}`);-- eventSource.onmessage = function(event) {- const data = JSON.parse(event.data);-- if (data.type === 'start') {- addLogEntry(data.message, 'info');- progressBar.style.width = '10%';- progressBar.textContent = '10%';- } else if (data.type === 'progress') {- // Update progress bar- progressBar.style.width = `${data.percent}%`;- progressBar.textContent = `${data.percent}%`;-- // Add log entry for current document- addLogEntry(`[${data.current}/${data.total}] Indexing: ${data.title}`, 'info');- } else if (data.type === 'complete') {- const results = data.results;- addLogEntry(`Indexing complete!`, 'success');- addLogEntry(`✓ Successfully indexed: ${results.successful}`, 'success');- if (results.skipped > 0) {- addLogEntry(`⊘ Skipped (already indexed): ${results.skipped}`, 'info');+ let eventSource = null;+ let reconnectAttempts = 0;+ const maxReconnectAttempts = 3;+ const reconnectDelay = 2000; // 2 seconds++ function createEventSource() {+ try {+ eventSource = new EventSource(`/library/api/rag/index-all?force_reindex=${forceReindex}`);++ eventSource.onmessage = function(event) {+ // Reset reconnect attempts on successful message+ reconnectAttempts = 0;++ try {+ const data = JSON.parse(event.data);++ if (data.type === 'start') {+ addLogEntry(data.message, 'info');+ progressBar.style.width = '10%';+ progressBar.textContent = '10%';+ } else if (data.type === 'progress') {+ // Update progress bar+ progressBar.style.width = `${data.percent}%`;+ progressBar.textContent = `${data.percent}%`;++ // Add log entry for current document+ addLogEntry(`[${data.current}/${data.total}] Indexing: ${data.title}`, 'info');+ } else if (data.type === 'complete') {+ const results = data.results;+ addLogEntry(`Indexing complete!`, 'success');+ addLogEntry(`✓ Successfully indexed: ${results.successful}`, 'success');+ if (results.skipped > 0) {+ addLogEntry(`⊘ Skipped (already indexed): ${results.skipped}`, 'info');+ }+ if (results.failed > 0) {+ addLogEntry(`✗ Failed: ${results.failed}`, 'error');+ results.errors.forEach(err => {+ addLogEntry(` - ${err.title}: ${err.error}`, 'error');+ });+ }++ progressBar.style.width = '100%';+ progressBar.textContent = '100%';+ progressBar.classList.remove('ldr-progress-bar-animated');++ // Refresh stats+ setTimeout(() => {+ refreshStats();+ }, 1000);++ closeEventSource();+ } else if (data.type === 'error') {+ addLogEntry(`Server error: ${data.error}`, 'error');+ progressBar.classList.remove('ldr-progress-bar-animated');+ progressBar.classList.add('ldr-bg-danger');+ closeEventSource();+ }+ } catch (parseError) {+ console.error('Failed to parse SSE message:', parseError, 'Raw data:', event.data);+ addLogEntry('Received invalid data from server', 'error');}- if (results.failed > 0) {- addLogEntry(`✗ Failed: ${results.failed}`, 'error');- results.errors.forEach(err => {- addLogEntry(` - ${err.title}: ${err.error}`, 'error');- });+ };++ eventSource.onerror = function(error) {+ console.error('EventSource error:', error);++ // Check if we should attempt reconnection+ if (eventSource.readyState === EventSource.CLOSED) {+ // Connection permanently closed+ addLogEntry('Connection closed by server', 'error');+ closeEventSource();+ } else if (eventSource.readyState === EventSource.CONNECTING) {+ // Attempting to reconnect automatically+ if (reconnectAttempts < maxReconnectAttempts) {+ reconnectAttempts++;+ addLogEntry(`Connection lost. Reconnecting... (${reconnectAttempts}/${maxReconnectAttempts})`, 'warn');++ // EventSource will automatically try to reconnect+ // We just need to handle the case where it fails repeatedly+ setTimeout(() => {+ if (eventSource && eventSource.readyState === EventSource.CONNECTING) {+ addLogEntry('Reconnection taking longer than expected...', 'warn');+ }+ }, reconnectDelay);+ } else {+ // Max reconnect attempts reached+ addLogEntry('Unable to reconnect after multiple attempts. Please refresh the page and try again.', 'error');+ progressBar.classList.remove('ldr-progress-bar-animated');+ progressBar.classList.add('ldr-bg-danger');+ closeEventSource();+ }+ } else {+ // Unknown error state+ addLogEntry('Connection error occurred. Check your network connection.', 'error');+ progressBar.classList.remove('ldr-progress-bar-animated');+ progressBar.classList.add('ldr-bg-danger');+ closeEventSource();}+ };- progressBar.style.width = '100%';- progressBar.textContent = '100%';- progressBar.classList.remove('ldr-progress-bar-animated');-- // Refresh stats- setTimeout(() => {- refreshStats();- }, 1000);-- eventSource.close();- } else if (data.type === 'error') {- addLogEntry(`Error: ${data.error}`, 'error');- progressBar.classList.remove('ldr-progress-bar-animated');- progressBar.classList.add('ldr-bg-danger');- eventSource.close();- }- };+ eventSource.onopen = function() {+ if (reconnectAttempts > 0) {+ addLogEntry('Connection restored! Continuing...', 'success');+ }+ };- eventSource.onerror = function(error) {- console.error('EventSource error:', error);- addLogEntry('Connection error occurred', 'error');- progressBar.classList.remove('ldr-progress-bar-animated');+ } catch (error) {+ console.error('Error creating EventSource:', error);+ addLogEntry(`Failed to start indexing: ${error.message}`, 'error');progressBar.classList.add('ldr-bg-danger');- eventSource.close();- };+ }+ }- } catch (error) {- console.error('Error indexing documents:', error);- addLogEntry(`Error: ${error.message}`, 'error');+ function closeEventSource() {+ if (eventSource) {+ eventSource.close();+ eventSource = null;+ }}++ // Create the EventSource connection+ createEventSource();}/**