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.

[FEAT] add notifications for success (finished) and error state of a research

hashedviking71ce50c43dea
diff --git a/static/js/app.js b/static/js/app.js
index c462757987..5bed9b109e 100644
--- a/static/js/app.js
+++ b/static/js/app.js
@@ -9,6 +9,42 @@ document.addEventListener('DOMContentLoaded', () => {
let currentResearchId = null;
window.currentResearchId = null;
+ // Polling interval for research status
+ let pollingInterval = null;
+
+ // Sound notification variables
+ let successSound = null;
+ let errorSound = null;
+ let notificationsEnabled = true;
+
+ // Initialize notification sounds
+ function initializeSounds() {
+ successSound = new Audio('/research/static/sounds/success.mp3');
+ errorSound = new Audio('/research/static/sounds/error.mp3');
+ successSound.volume = 0.7;
+ errorSound.volume = 0.7;
+ }
+
+ // Function to play a notification sound
+ function playNotificationSound(type) {
+ console.log(`Attempting to play ${type} notification sound`);
+ if (!notificationsEnabled) {
+ console.log('Notifications are disabled');
+ return;
+ }
+
+ // Play sounds regardless of tab focus
+ if (type === 'success' && successSound) {
+ console.log('Playing success sound');
+ successSound.play().catch(err => console.error('Error playing success sound:', err));
+ } else if (type === 'error' && errorSound) {
+ console.log('Playing error sound');
+ errorSound.play().catch(err => console.error('Error playing error sound:', err));
+ } else {
+ console.warn(`Unknown sound type or sound not initialized: ${type}`);
+ }
+ }
+
// Initialize socket only when needed with a timeout for safety
function initializeSocket() {
if (socket) return socket; // Return existing socket if already initialized
@@ -96,8 +132,59 @@ document.addEventListener('DOMContentLoaded', () => {
updateProgressUI(data.progress, data.status, data.message);
// If research is complete, show the completion buttons
- if (data.status === 'completed' || data.status === 'terminated' || data.status === 'failed') {
+ if (data.status === 'completed' || data.status === 'terminated' || data.status === 'failed' || data.status === 'suspended') {
+ console.log(`Socket received research final state: ${data.status}`);
+
+ // Clear polling interval if it exists
+ if (pollingInterval) {
+ console.log('Clearing polling interval from socket event');
+ clearInterval(pollingInterval);
+ pollingInterval = null;
+ }
+
+ // Update navigation state
+ if (data.status === 'completed') {
+ isResearchInProgress = false;
+ }
+
+ // Update UI for completion
+ if (data.status === 'completed') {
+ console.log('Research completed via socket, loading results automatically');
+
+ // Hide terminate button
+ const terminateBtn = document.getElementById('terminate-research-btn');
+ if (terminateBtn) {
+ terminateBtn.style.display = 'none';
+ }
+
+ // Auto-load the results
+ loadResearch(researchId);
+ } else if (data.status === 'failed' || data.status === 'suspended') {
+ console.log(`Showing error message for status: ${data.status} from socket event`);
+ const errorMessage = document.getElementById('error-message');
+ if (errorMessage) {
+ errorMessage.style.display = 'block';
+ errorMessage.textContent = data.status === 'failed' ?
+ (data.metadata && data.metadata.error ? JSON.parse(data.metadata).error : 'Research failed') :
+ 'Research was suspended';
+ } else {
+ console.error('error-message element not found in socket handler');
+ }
+ }
+
updateNavigationBasedOnResearchStatus();
+
+ // Play notification sounds based on status
+ if (data.status === 'completed') {
+ console.log('Playing success notification sound from socket event');
+ playNotificationSound('success');
+ } else if (data.status === 'failed') {
+ console.log('Playing error notification sound from socket event');
+ playNotificationSound('error');
+ }
+
+ // Force the UI to update with a manual trigger
+ document.dispatchEvent(new CustomEvent('research_completed', { detail: data }));
}
// Update the detailed log if on details page
@@ -227,54 +314,95 @@ document.addEventListener('DOMContentLoaded', () => {
// Function to poll research status (as a backup to socket.io)
function pollResearchStatus(researchId) {
- const interval = setInterval(async () => {
- try {
- const response = await fetch(getApiUrl(`/api/research/${researchId}`));
- const data = await response.json();
-
+ console.log(`Polling research status for ID: ${researchId}`);
+ fetch(getApiUrl(`/api/research/${researchId}`))
+ .then(response => {
+ if (!response.ok) {
+ throw new Error(`Server returned ${response.status}`);
+ }
+ return response.json();
+ })
+ .then(data => {
+ console.log('Research status response:', data);
+ // Update the UI with the current progress
+ updateProgressUI(data.progress, data.status, data.message);
+
+ // If research is complete, show the completion buttons
if (data.status === 'completed' || data.status === 'failed' || data.status === 'suspended') {
- clearInterval(interval);
- isResearchInProgress = false;
- currentResearchId = null;
+ console.log(`Research is in final state: ${data.status}`);
+ // Clear the polling interval
+ if (pollingInterval) {
+ console.log('Clearing polling interval');
+ clearInterval(pollingInterval);
+ pollingInterval = null;
+ }
- // Always update the start research button state
- const startResearchBtn = document.getElementById('start-research-btn');
- if (startResearchBtn) {
- startResearchBtn.disabled = false;
- startResearchBtn.innerHTML = '<i class="fas fa-rocket"></i> Start Research';
+ // Update UI for completion
+ if (data.status === 'completed') {
+ console.log('Research completed, loading results automatically');
+ // Hide the terminate button
+ const terminateBtn = document.getElementById('terminate-research-btn');
+ if (terminateBtn) {
+ terminateBtn.style.display = 'none';
+ }
+
+ // Auto-load the results instead of showing a button
+ loadResearch(researchId);
+ } else if (data.status === 'failed' || data.status === 'suspended') {
+ console.log(`Showing error message for status: ${data.status}`);
+ const errorMessage = document.getElementById('error-message');
+ if (errorMessage) {
+ errorMessage.style.display = 'block';
+ errorMessage.textContent = data.status === 'failed' ?
+ (data.metadata && data.metadata.error ? JSON.parse(data.metadata).error : 'Research failed') :
+ 'Research was suspended';
+ } else {
+ console.error('error-message element not found');
+ }
}
- // Update navigation when research completes or is suspended
+ // Play notification sound based on status
+ if (data.status === 'completed') {
+ console.log('Playing success notification sound');
+ playNotificationSound('success');
+ } else if (data.status === 'failed') {
+ console.log('Playing error notification sound');
+ playNotificationSound('error');
+ }
+
+ // Update the navigation
+ console.log('Updating navigation based on research status');
updateNavigationBasedOnResearchStatus();
- // If history page is active, update the specific item in place
- if (document.getElementById('history').classList.contains('active')) {
- updateHistoryItemStatus(researchId, data.status);
- // Full refresh may still be needed to update duration info
- setTimeout(() => loadResearchHistory(), 500);
- }
+ // Force the UI to update with a manual trigger
+ document.dispatchEvent(new CustomEvent('research_completed', { detail: data }));
- if (data.status === 'completed') {
- loadResearch(researchId);
- } else if (data.status === 'failed' || data.status === 'suspended') {
- const statusMessage = data.status === 'suspended' ?
- 'Research was terminated by user' :
- (data.metadata ? JSON.parse(data.metadata).error : 'Research failed');
- updateProgressUI(100, data.status, statusMessage);
- }
- } else {
- updateProgressUI(data.progress || 0, data.status);
+ return;
+ }
+
+ // Continue polling if still in progress
+ if (data.status === 'in_progress') {
+ console.log('Research is still in progress, continuing polling');
+ if (!pollingInterval) {
+ console.log('Setting up polling interval');
+ pollingInterval = setInterval(() => {
+ pollResearchStatus(researchId);
+ }, 10000);
}
- } catch (error) {
- console.error('Error polling status:', error);
}
- }, 3000); // Poll every 3 seconds
+ })
+ .catch(error => {
+ console.error('Error polling research status:', error);
+ });
}
// Main initialization function
function initializeApp() {
console.log('Initializing application...');
+ // Initialize the sounds
+ initializeSounds();
+
// Get navigation elements
const navItems = document.querySelectorAll('.sidebar-nav li');
const mobileNavItems = document.querySelectorAll('.mobile-tab-bar li');
@@ -308,10 +436,10 @@ document.addEventListener('DOMContentLoaded', () => {
const pageId = this.dataset.page;
if (pageId) {
switchPage(pageId);
+ }
+ });
}
});
- }
- });
mobileNavItems.forEach(item => {
if (!item.classList.contains('external-link')) {
@@ -357,6 +485,9 @@ document.addEventListener('DOMContentLoaded', () => {
// Check for active research
checkActiveResearch();
+ // Setup notification toggle and other form elements
+ setupResearchForm();
+
console.log('Application initialized');
}
@@ -1145,6 +1276,10 @@ document.addEventListener('DOMContentLoaded', () => {
// Function to update the sidebar navigation based on research status
function updateNavigationBasedOnResearchStatus() {
+ console.log("Updating navigation based on research status");
+ console.log("isResearchInProgress:", isResearchInProgress);
+ console.log("currentResearchId:", currentResearchId);
+
// Get nav items for each update to ensure we have fresh references
const navItems = document.querySelectorAll('.sidebar-nav li');
const mobileNavItems = document.querySelectorAll('.mobile-tab-bar li');
@@ -1159,6 +1294,7 @@ document.addEventListener('DOMContentLoaded', () => {
if (newResearchNav) {
if (isResearchInProgress) {
+ console.log("Research is in progress, updating navigation");
// Change text to "Research in Progress"
newResearchNav.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Research in Progress';
@@ -1177,6 +1313,7 @@ document.addEventListener('DOMContentLoaded', () => {
updateProgressFromCurrentResearch();
}
} else {
+ console.log("Research is not in progress, resetting navigation");
// Reset to "New Research" if there's no active research
newResearchNav.innerHTML = '<i class="fas fa-search"></i> New Research';
@@ -1208,9 +1345,11 @@ document.addEventListener('DOMContentLoaded', () => {
}
});
}
-
+
// Connect to socket for updates
- window.connectToResearchSocket(currentResearchId);
+ if (currentResearchId) {
+ window.connectToResearchSocket(currentResearchId);
+ }
}
// Function to update the research progress page from nav click
@@ -1480,7 +1619,7 @@ document.addEventListener('DOMContentLoaded', () => {
if (currentY + (textLines.length * (fontSize + 4)) > pdfHeight - margin) {
pageNum++;
addPageWithHeader(pageNum);
- currentY = margin;
+ currentY = margin + 40; // Reset Y position after header
}
pdf.text(textLines, margin, currentY + fontSize);
@@ -1878,4 +2017,62 @@ document.addEventListener('DOMContentLoaded', () => {
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', generatePdf);
}
+
+ // Function to set up the research form
+ function setupResearchForm() {
+ const researchForm = document.getElementById('research-form');
+ const notificationToggle = document.getElementById('notification-toggle');
+
+ // Set notification state from toggle
+ if (notificationToggle) {
+ notificationsEnabled = notificationToggle.checked;
+
+ // Listen for changes to the toggle
+ notificationToggle.addEventListener('change', function() {
+ notificationsEnabled = this.checked;
+ // Store preference in localStorage for persistence
+ localStorage.setItem('notificationsEnabled', notificationsEnabled);
+ });
+
+ // Load saved preference from localStorage
+ const savedPref = localStorage.getItem('notificationsEnabled');
+ if (savedPref !== null) {
+ notificationsEnabled = savedPref === 'true';
+ notificationToggle.checked = notificationsEnabled;
+ }
+ }
+
+ // ... existing form setup ...
+ }
+
+ // Add event listener for view results button
+ const viewResultsBtn = document.getElementById('view-results-btn');
+ if (viewResultsBtn) {
+ viewResultsBtn.addEventListener('click', () => {
+ console.log('View results button clicked');
+ if (currentResearchId) {
+ loadResearch(currentResearchId);
+ } else {
+ console.error('No research ID available');
+ }
+ });
+ }
+
+ // Add listener for research_completed custom event
+ document.addEventListener('research_completed', (event) => {
+ console.log('Research completed event received:', event.detail);
+ const data = event.detail;
+
+ // Mark research as no longer in progress
+ isResearchInProgress = false;
+
+ // Hide terminate button
+ const terminateBtn = document.getElementById('terminate-research-btn');
+ if (terminateBtn) {
+ terminateBtn.style.display = 'none';
+ }
+
+ // Update navigation
+ updateNavigationBasedOnResearchStatus();
+ });
});
\ No newline at end of file
diff --git a/static/sounds/README.md b/static/sounds/README.md
new file mode 100644
index 0000000000..01fd553032
--- /dev/null
+++ b/static/sounds/README.md
@@ -0,0 +1,29 @@
+# Sound Files for Notifications
+
+This directory contains sound files used for notifications in the Deep Research application.
+
+## Required Files
+
+1. `success.mp3` - Played when research completes successfully
+2. `error.mp3` - Played when research fails or encounters an error
+
+## Sound Sources
+
+You can download copyright-free sound files from these sources:
+
+- [Freesound](https://freesound.org/)
+- [Free Sound Library](https://www.freesoundslibrary.com/)
+
+## Recommended Sounds
+
+### Success Sound
+- [Success Sound by grunz](https://freesound.org/people/grunz/sounds/109662/)
+- Direct download: https://freesound.org/data/previews/109/109662_945474-lq.mp3
+
+### Error Sound
+- [Error Sound by Autistic Lucario](https://freesound.org/people/Autistic%20Lucario/sounds/142608/)
+- Direct download: https://freesound.org/data/previews/142/142608_1840739-lq.mp3
+
+## Usage
+
+The application will automatically use these sounds when research tasks complete or fail, but only when the browser tab is not in focus.
\ No newline at end of file
diff --git a/static/sounds/error.mp3 b/static/sounds/error.mp3
new file mode 100644
index 0000000000..e8bdb0b911
Binary files /dev/null and b/static/sounds/error.mp3 differ
diff --git a/static/sounds/success.mp3 b/static/sounds/success.mp3
new file mode 100644
index 0000000000..ebe3e91b50
Binary files /dev/null and b/static/sounds/success.mp3 differ
diff --git a/templates/index.html b/templates/index.html
index 9e320353e9..d0f00d43de 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -61,6 +61,14 @@
</div>
</div>
</div>
+ <div class="form-options">
+ <div class="form-option">
+ <label for="notification-toggle" class="checkbox-label">
+ <input type="checkbox" id="notification-toggle" checked>
+ <span class="checkbox-text">Sound notifications when complete</span>
+ </label>
+ </div>
+ </div>
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="start-research-btn"><i class="fas fa-rocket"></i> Start Research</button>
</div>
@@ -109,6 +117,7 @@
<button id="terminate-research-btn" class="btn btn-outline terminate-btn" style="display: none;">
<i class="fas fa-stop-circle"></i> Terminate Research
</button>
+ <div id="error-message" class="error-message" style="display: none;"></div>
</div>
</div>
</div>