~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
fix: Update client-side WebSocket handling for improved reliability
hashedviking5d860b615484
diff --git a/static/js/app.js b/static/js/app.jsindex d3da39082a..cd94f38d12 100644--- a/static/js/app.js+++ b/static/js/app.js@@ -1,187 +1,398 @@// Main application functionalitydocument.addEventListener('DOMContentLoaded', () => {- // Initialize socket.io connection with proper error handling and reconnection options- const socket = io({- path: '/research/socket.io',- transports: ['websocket', 'polling']- });-- // Socket connection event handlers- socket.on('connect', () => {- console.log('Socket.IO connected successfully');- });+ // Global socket variable - initialize as null+ let socket = null;+ let socketConnected = false;- socket.on('connect_error', (error) => {- console.error('Socket.IO connection error:', error);- // The connection will automatically try to reconnect due to our config- });+ // Global state variables+ let isResearchInProgress = false;+ let currentResearchId = null;+ window.currentResearchId = null;- socket.on('reconnect', (attemptNumber) => {- console.log(`Socket.IO reconnected after ${attemptNumber} attempts`);- });+ // Initialize socket only when needed with a timeout for safety+ function initializeSocket() {+ if (socket) return socket; // Return existing socket if already initialized++ console.log('Initializing socket connection...');+ // Create new socket connection with optimized settings for threading mode+ socket = io({+ path: '/research/socket.io',+ transports: ['websocket', 'polling'],+ reconnection: true,+ reconnectionAttempts: 3,+ reconnectionDelay: 1000,+ timeout: 5000,+ autoConnect: true,+ forceNew: true+ });++ // Add event handlers+ socket.on('connect', () => {+ console.log('Socket connected');+ socketConnected = true;+ });++ socket.on('disconnect', () => {+ console.log('Socket disconnected');+ socketConnected = false;+ });++ socket.on('connect_error', (error) => {+ console.error('Socket connection error:', error);+ socketConnected = false;+ });++ // Set a timeout to detect hanging connections+ setTimeout(() => {+ if (!socketConnected) {+ console.log('Socket connection timeout - forcing reconnect');+ try {+ socket.disconnect();+ socket.connect();+ } catch (e) {+ console.error('Error during forced reconnect:', e);+ }+ }+ }, 5000);++ return socket;+ }- socket.on('reconnect_error', (error) => {- console.error('Socket.IO reconnection error:', error);- });-- // Navigation- const navItems = document.querySelectorAll('.sidebar-nav li');- const mobileNavItems = document.querySelectorAll('.mobile-tab-bar li');- const pages = document.querySelectorAll('.page');- const mobileTabBar = document.querySelector('.mobile-tab-bar');+ // Function to safely disconnect socket+ window.disconnectSocket = function() {+ try {+ if (socket) {+ console.log('Manually disconnecting socket');+ socket.removeAllListeners();+ socket.disconnect();+ socket = null;+ socketConnected = false;+ }+ } catch (e) {+ console.error('Error disconnecting socket:', e);+ }+ };- // Handle responsive navigation based on screen size- function handleResponsiveNavigation() {- // Mobile tab bar should only be visible on small screens- if (window.innerWidth <= 767) {- if (mobileTabBar) {- mobileTabBar.style.display = 'flex';+ // Helper function to connect to research updates+ window.connectToResearchSocket = function(researchId) {+ try {+ // Initialize socket if needed+ if (!socket) {+ socket = initializeSocket();}- } else {- if (mobileTabBar) {- mobileTabBar.style.display = 'none';++ // Subscribe to research updates+ socket.emit('subscribe_to_research', { research_id: researchId });++ // Set up event listener for research progress+ const progressEventName = `research_progress_${researchId}`;++ // Remove existing listeners to prevent duplicates+ socket.off(progressEventName);++ // Add new listener+ socket.on(progressEventName, (data) => {+ console.log('Received research progress update:', data);+ 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') {+ updateNavigationBasedOnResearchStatus();+ }++ // Update the detailed log if on details page+ if (data.log_entry && document.getElementById('research-log')) {+ updateDetailLogEntry(data.log_entry);+ }+ });++ return true;+ } catch (e) {+ console.error('Error connecting to research socket:', e);+ return false;+ }+ };++ // Check for active research on page load+ async function checkActiveResearch() {+ try {+ const response = await fetch(getApiUrl('/api/history'));+ const history = await response.json();++ // Find in-progress research+ const activeResearch = history.find(item => item.status === 'in_progress');++ if (activeResearch) {+ isResearchInProgress = true;+ currentResearchId = activeResearch.id;+ window.currentResearchId = currentResearchId;++ // Check if we're on the new research page and redirect to progress+ const currentPage = document.querySelector('.page.active');++ if (currentPage && currentPage.id === 'new-research') {+ // Navigate to progress page+ switchPage('research-progress');++ // Connect to socket for this research+ window.connectToResearchSocket(currentResearchId);++ // Start polling for updates+ pollResearchStatus(currentResearchId);+ }}+ } catch (error) {+ console.error('Error checking for active research:', error);}}- // Call on initial load- handleResponsiveNavigation();-- // Set up window resize listener- window.addEventListener('resize', handleResponsiveNavigation);-- // Initialize modal research id- let currentResearchId = null;- let isResearchInProgress = false;-- // Expose the currentResearchId and termination function to the window object for the inline script- window.currentResearchId = null;-- // Track termination status- let isTerminating = false;+ // Add unload event listener+ window.addEventListener('beforeunload', function() {+ window.disconnectSocket();+ });- // Check if we're on the history page and load history if needed- const historyPage = document.getElementById('history');- if (historyPage && historyPage.classList.contains('active')) {- // Use setTimeout to ensure the DOM is fully loaded- setTimeout(() => loadResearchHistory(), 100);- }+ // Navigation event listeners with socket cleanup+ document.getElementById('home-link').addEventListener('click', function(e) {+ e.preventDefault();+ window.disconnectSocket();+ setTimeout(() => {+ window.location.href = 'https://warplink.dev';+ }, 100);+ });- // Add a prefix helper function at the top of the file- function getApiUrl(path) {- // This function adds the /research prefix to all API URLs- return `/research${path}`;- }+ document.getElementById('mobile-home-link').addEventListener('click', function(e) {+ e.preventDefault();+ window.disconnectSocket();+ setTimeout(() => {+ window.location.href = 'https://warplink.dev';+ }, 100);+ });- // Function to terminate research - exposed to window object- async function terminateResearch(researchId) {- if (!researchId) {- console.error('No research ID provided for termination');- return;- }-- // Prevent multiple termination requests- if (isTerminating) {- console.log('Termination already in progress');+ // Function to start research+ async function startResearch(query, mode) {+ // Check if research is already in progress+ if (isResearchInProgress) {+ alert('Another research is already in progress. Please wait for it to complete or check its status in the history tab.');return;}- // Confirm with the user- if (!confirm('Are you sure you want to terminate this research? This action cannot be undone.')) {- return;- }+ // Get the start button+ const startResearchBtn = document.getElementById('start-research-btn');+ if (!startResearchBtn) return;++ // Disable the start button while we attempt to start the research+ startResearchBtn.disabled = true;+ startResearchBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Starting...';try {- // Set terminating flag- isTerminating = true;-- // Update UI to show we're processing- const terminateBtn = document.getElementById('terminate-research-btn');- if (terminateBtn) {- terminateBtn.disabled = true;- terminateBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Terminating...';- }-- // Find all terminate buttons in history items and disable them- const allTerminateBtns = document.querySelectorAll('.terminate-btn');- allTerminateBtns.forEach(btn => {- if (btn !== terminateBtn) {- btn.disabled = true;- btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Terminating...';- }- });-- // Call the terminate API- const response = await fetch(getApiUrl(`/api/research/${researchId}/terminate`), {- method: 'POST'+ const response = await fetch(getApiUrl('/api/start_research'), {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json'+ },+ body: JSON.stringify({+ query: query,+ mode: mode+ })});const data = await response.json();if (data.status === 'success') {- // The UI will be updated via socket events or polling- console.log('Termination request sent successfully');+ isResearchInProgress = true;+ currentResearchId = data.research_id;- // If we're on the history page, update the status of this item- if (document.getElementById('history').classList.contains('active')) {- updateHistoryItemStatus(researchId, 'terminating', 'Terminating...');- }- } else {- console.error('Termination request failed:', data.message);- alert(`Failed to terminate research: ${data.message}`);+ // Also update the window object+ window.currentResearchId = data.research_id;- // Reset the terminating flag- isTerminating = false;+ // Update the navigation to show Research in Progress+ updateNavigationBasedOnResearchStatus();- // Reset the button+ // Update progress page+ document.getElementById('current-query').textContent = query;+ document.getElementById('progress-fill').style.width = '0%';+ document.getElementById('progress-percentage').textContent = '0%';+ document.getElementById('progress-status').textContent = 'Initializing research process...';++ // Navigate to progress page+ switchPage('research-progress');++ // Connect to socket for this research+ window.connectToResearchSocket(data.research_id);++ // Start polling for status+ pollResearchStatus(data.research_id);++ // Show the terminate button+ const terminateBtn = document.getElementById('terminate-research-btn');if (terminateBtn) {+ terminateBtn.style.display = 'inline-flex';terminateBtn.disabled = false;- terminateBtn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate Research';}+ } else {+ alert('Error starting research: ' + (data.message || 'Unknown error'));+ startResearchBtn.disabled = false;+ startResearchBtn.innerHTML = '<i class="fas fa-rocket"></i> Start Research';+ }+ } catch (error) {+ console.error('Error:', error);+ alert('An error occurred while starting the research. Please try again.');+ startResearchBtn.disabled = false;+ startResearchBtn.innerHTML = '<i class="fas fa-rocket"></i> Start Research';+ }+ }++ // 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();- // Reset history button states- const allTerminateBtns = document.querySelectorAll('.terminate-btn');- allTerminateBtns.forEach(btn => {- if (btn !== terminateBtn) {- btn.disabled = false;- btn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate';+ if (data.status === 'completed' || data.status === 'failed' || data.status === 'suspended') {+ clearInterval(interval);+ isResearchInProgress = false;+ currentResearchId = 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 navigation when research completes or is suspended+ 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);+ }++ 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);+ }+ } catch (error) {+ console.error('Error polling status:', error);+ }+ }, 3000); // Poll every 3 seconds+ }++ // Main initialization function+ function initializeApp() {+ console.log('Initializing application...');++ // Get navigation elements+ const navItems = document.querySelectorAll('.sidebar-nav li');+ const mobileNavItems = document.querySelectorAll('.mobile-tab-bar li');+ const pages = document.querySelectorAll('.page');+ const mobileTabBar = document.querySelector('.mobile-tab-bar');++ // Handle responsive navigation based on screen size+ function handleResponsiveNavigation() {+ // Mobile tab bar should only be visible on small screens+ if (window.innerWidth <= 767) {+ if (mobileTabBar) {+ mobileTabBar.style.display = 'flex';+ }+ } else {+ if (mobileTabBar) {+ mobileTabBar.style.display = 'none';+ }+ }+ }++ // Call on initial load+ handleResponsiveNavigation();++ // Add resize listener for responsive design+ window.addEventListener('resize', handleResponsiveNavigation);++ // Setup navigation click handlers+ navItems.forEach(item => {+ if (!item.classList.contains('external-link')) {+ item.addEventListener('click', function() {+ const pageId = this.dataset.page;+ if (pageId) {+ switchPage(pageId);+ }+ });+ }+ });++ mobileNavItems.forEach(item => {+ if (!item.classList.contains('external-link')) {+ item.addEventListener('click', function() {+ const pageId = this.dataset.page;+ if (pageId) {+ switchPage(pageId);}});}- } catch (error) {- console.error('Error terminating research:', error);- alert('An error occurred while trying to terminate the research.');-- // Reset the terminating flag- isTerminating = false;-- // Reset the button- const terminateBtn = document.getElementById('terminate-research-btn');- if (terminateBtn) {- terminateBtn.disabled = false;- terminateBtn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate Research';- }-- // Reset history button states- const allTerminateBtns = document.querySelectorAll('.terminate-btn');- allTerminateBtns.forEach(btn => {- if (btn !== terminateBtn) {- btn.disabled = false;- btn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate';+ });++ // Setup form submission+ const researchForm = document.getElementById('research-form');+ if (researchForm) {+ researchForm.addEventListener('submit', function(e) {+ e.preventDefault();+ const query = document.getElementById('query').value.trim();+ if (!query) {+ alert('Please enter a research query');+ return;}++ const mode = document.querySelector('.mode-option.active')?.dataset.mode || 'quick';+ startResearch(query, mode);});}++ // Initialize research mode selection+ const modeOptions = document.querySelectorAll('.mode-option');+ modeOptions.forEach(option => {+ option.addEventListener('click', function() {+ modeOptions.forEach(opt => opt.classList.remove('active'));+ this.classList.add('active');+ });+ });++ // Load research history initially+ if (document.getElementById('history-list')) {+ loadResearchHistory();+ }++ // Check for active research+ checkActiveResearch();++ console.log('Application initialized');}- // Expose the terminate function to the window object- window.terminateResearch = terminateResearch;+ // Initialize the app+ initializeApp();- // Function to switch pages+ // Function to switch between pagesfunction switchPage(pageId) {+ // Get elements directly from the DOM+ const pages = document.querySelectorAll('.page');+ const navItems = document.querySelectorAll('.sidebar-nav li');+ const mobileNavItems = document.querySelectorAll('.mobile-tab-bar li');++ // Remove active class from all pagespages.forEach(page => {page.classList.remove('active');});+ // Add active class to target pageconst targetPage = document.getElementById(pageId);if (targetPage) {targetPage.classList.add('active');@@ -204,239 +415,131 @@ document.addEventListener('DOMContentLoaded', () => {item.classList.remove('active');}});++ // Special handling for history page+ if (pageId === 'history') {+ loadResearchHistory();+ }}- // Setup navigation click handlers- navItems.forEach(item => {- item.addEventListener('click', () => {- const pageId = item.getAttribute('data-page');- switchPage(pageId);-- // Load history data when navigating to history page- if (pageId === 'history') {- loadResearchHistory();- }- });- });-- // Setup mobile navigation click handlers- mobileNavItems.forEach(item => {- item.addEventListener('click', () => {- const pageId = item.getAttribute('data-page');- switchPage(pageId);-- // Load history data when navigating to history page- if (pageId === 'history') {- loadResearchHistory();- }- });- });+ // Track termination status+ let isTerminating = false;- // Handle logo click to return to home page- const logoLink = document.getElementById('logo-link');- if (logoLink) {- logoLink.addEventListener('click', () => {- switchPage('new-research');- });+ // Check if we're on the history page and load history if needed+ const historyPage = document.getElementById('history');+ if (historyPage && historyPage.classList.contains('active')) {+ // Use setTimeout to ensure the DOM is fully loaded+ setTimeout(() => loadResearchHistory(), 100);}- // Mode selection- const modeOptions = document.querySelectorAll('.mode-option');- let selectedMode = 'quick';+ // Add a prefix helper function at the top of the file+ function getApiUrl(path) {+ // This function adds the /research prefix to all API URLs+ return `/research${path}`;+ }- modeOptions.forEach(option => {- option.addEventListener('click', () => {- modeOptions.forEach(opt => opt.classList.remove('active'));- option.classList.add('active');- selectedMode = option.getAttribute('data-mode');- });- });+ // Function to terminate research - exposed to window object+ async function terminateResearch(researchId) {+ if (!researchId) {+ console.error('No research ID provided for termination');+ return;+ }- // Form submission- const researchForm = document.getElementById('research-form');- const startResearchBtn = document.getElementById('start-research-btn');-- researchForm.addEventListener('submit', async (e) => {- e.preventDefault();-- if (isResearchInProgress) {- alert('Another research is already in progress. Please wait for it to complete or check its status in the history tab.');+ // Prevent multiple termination requests+ if (isTerminating) {+ console.log('Termination already in progress');return;}- const queryInput = document.getElementById('query');- const query = queryInput.value.trim();-- if (!query) {- alert('Please enter a research query');+ // Confirm with the user+ if (!confirm('Are you sure you want to terminate this research? This action cannot be undone.')) {return;}- // Disable the start button while we attempt to start the research- startResearchBtn.disabled = true;- startResearchBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Starting...';-try {- const response = await fetch(getApiUrl('/api/start_research'), {- method: 'POST',- headers: {- 'Content-Type': 'application/json'- },- body: JSON.stringify({- query: query,- mode: selectedMode- })+ // Set terminating flag+ isTerminating = true;++ // Update UI to show we're processing+ const terminateBtn = document.getElementById('terminate-research-btn');+ if (terminateBtn) {+ terminateBtn.disabled = true;+ terminateBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Terminating...';+ }++ // Find all terminate buttons in history items and disable them+ const allTerminateBtns = document.querySelectorAll('.terminate-btn');+ allTerminateBtns.forEach(btn => {+ if (btn !== terminateBtn) {+ btn.disabled = true;+ btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Terminating...';+ }+ });++ // Call the terminate API+ const response = await fetch(getApiUrl(`/api/research/${researchId}/terminate`), {+ method: 'POST'});const data = await response.json();if (data.status === 'success') {- isResearchInProgress = true;- currentResearchId = data.research_id;-- // Also update the window object- window.currentResearchId = data.research_id;-- // Update the navigation to show Research in Progress- updateNavigationBasedOnResearchStatus();-- // Update progress page- document.getElementById('current-query').textContent = query;- document.getElementById('progress-fill').style.width = '0%';- document.getElementById('progress-percentage').textContent = '0%';- document.getElementById('progress-status').textContent = 'Initializing research process...';-- // Navigate to progress page- switchPage('research-progress');+ // The UI will be updated via socket events or polling+ console.log('Termination request sent successfully');- // Connect to socket for this research- connectToResearchSocket(data.research_id);+ // If we're on the history page, update the status of this item+ if (document.getElementById('history').classList.contains('active')) {+ updateHistoryItemStatus(researchId, 'terminating', 'Terminating...');+ }+ } else {+ console.error('Termination request failed:', data.message);+ alert(`Failed to terminate research: ${data.message}`);- // Start polling for status- pollResearchStatus(data.research_id);+ // Reset the terminating flag+ isTerminating = false;- // Show the terminate button- const terminateBtn = document.getElementById('terminate-research-btn');+ // Reset the buttonif (terminateBtn) {- terminateBtn.style.display = 'inline-flex';terminateBtn.disabled = false;+ terminateBtn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate Research';}- } else {- alert('Error starting research: ' + (data.message || 'Unknown error'));- startResearchBtn.disabled = false;- startResearchBtn.innerHTML = '<i class="fas fa-rocket"></i> Start Research';- }- } catch (error) {- console.error('Error:', error);- alert('An error occurred while starting the research. Please try again.');- startResearchBtn.disabled = false;- startResearchBtn.innerHTML = '<i class="fas fa-rocket"></i> Start Research';- }- });-- // Socket event handlers for research progress- function connectToResearchSocket(researchId) {- // Subscribe to the specific channel for this research- socket.on(`research_progress_${researchId}`, (data) => {- console.log('Research progress update:', data);-- // Update the progress UI- if (data.progress) {- updateProgressUI(data.progress, data.status, data.message);- }++ // Reset history button states+ const allTerminateBtns = document.querySelectorAll('.terminate-btn');+ allTerminateBtns.forEach(btn => {+ if (btn !== terminateBtn) {+ btn.disabled = false;+ btn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate';+ }+ });+ }+ } catch (error) {+ console.error('Error terminating research:', error);+ alert('An error occurred while trying to terminate the research.');- // Update the log if we're on the details page- if (data.log_entry && document.getElementById('research-details').classList.contains('active')) {- updateDetailLogEntry(data.log_entry);- }+ // Reset the terminating flag+ isTerminating = false;- // If status is terminating, update the history item if visible- if (data.status === 'terminating' && document.getElementById('history').classList.contains('active')) {- updateHistoryItemStatus(researchId, 'terminating', 'Terminating...');+ // Reset the button+ const terminateBtn = document.getElementById('terminate-research-btn');+ if (terminateBtn) {+ terminateBtn.disabled = false;+ terminateBtn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate Research';}- // Handle completion- if (data.status === 'completed' || data.status === 'failed' || data.status === 'suspended') {- isResearchInProgress = false;- currentResearchId = null;-- // Always update the start research button state- startResearchBtn.disabled = false;- startResearchBtn.innerHTML = '<i class="fas fa-rocket"></i> Start Research';-- // Reset terminating flag if it was suspended- if (data.status === 'suspended') {- isTerminating = false;- }-- // Update navigation when research completes or is suspended- 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);- }-- if (data.status === 'completed' && data.report_path) {- // Navigate to results page if we're on the progress page- if (document.getElementById('research-progress').classList.contains('active')) {- loadResearch(researchId);- }- } else if (data.status === 'failed' || data.status === 'suspended') {- // Update the progress UI with the error message- const statusMessage = data.status === 'suspended' ?- 'Research was terminated by user' :- (data.error || 'Research failed');- updateProgressUI(100, data.status, statusMessage);+ // Reset history button states+ const allTerminateBtns = document.querySelectorAll('.terminate-btn');+ allTerminateBtns.forEach(btn => {+ if (btn !== terminateBtn) {+ btn.disabled = false;+ btn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate';}- }- });+ });+ }}- // 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();-- if (data.status === 'completed' || data.status === 'failed' || data.status === 'suspended') {- clearInterval(interval);- isResearchInProgress = false;- currentResearchId = null;-- // Always update the start research button state- startResearchBtn.disabled = false;- startResearchBtn.innerHTML = '<i class="fas fa-rocket"></i> Start Research';-- // Update navigation when research completes or is suspended- 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);- }-- 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);- }- } catch (error) {- console.error('Error polling status:', error);- }- }, 3000); // Poll every 3 seconds- }+ // Expose the terminate function to the window object+ window.terminateResearch = terminateResearch;// Function to update the progress UIfunction updateProgressUI(progress, status, message) {@@ -489,11 +592,10 @@ document.addEventListener('DOMContentLoaded', () => {historyList.innerHTML = '<div class="loading-spinner centered"><div class="spinner"></div></div>';try {- // Basic fetch without any extrasconst response = await fetch(getApiUrl('/api/history'));if (!response.ok) {- throw new Error(`API returned status ${response.status}: ${response.statusText}`);+ throw new Error(`Server returned ${response.status}`);}const data = await response.json();@@ -507,6 +609,25 @@ document.addEventListener('DOMContentLoaded', () => {return;}+ // Check if any research is in progress+ const inProgressResearch = data.find(item => item.status === 'in_progress');++ // Get the start research button+ const startResearchBtn = document.getElementById('start-research-btn');++ if (inProgressResearch) {+ isResearchInProgress = true;+ currentResearchId = inProgressResearch.id;+ if (startResearchBtn) {+ startResearchBtn.disabled = true;+ }+ } else {+ isResearchInProgress = false;+ if (startResearchBtn) {+ startResearchBtn.disabled = false;+ }+ }+// Display each history itemdata.forEach(item => {try {@@ -632,21 +753,6 @@ document.addEventListener('DOMContentLoaded', () => {}});- // Check if any research is in progress and update the start button state- const inProgressResearch = data.find(item => item.status === 'in_progress');- if (inProgressResearch) {- isResearchInProgress = true;- currentResearchId = inProgressResearch.id;- if (startResearchBtn) {- startResearchBtn.disabled = true;- }- } else {- isResearchInProgress = false;- if (startResearchBtn) {- startResearchBtn.disabled = false;- }- }-} catch (error) {console.error('Error loading history:', error);historyList.innerHTML = `@@ -701,7 +807,7 @@ document.addEventListener('DOMContentLoaded', () => {switchPage('research-progress');// Connect to socket for this research- connectToResearchSocket(research.id);+ window.connectToResearchSocket(research.id);// Start polling for statuspollResearchStatus(research.id);@@ -827,10 +933,8 @@ document.addEventListener('DOMContentLoaded', () => {// Render log entriesrenderLogEntries(data.log || []);- // Set up socket connection for live updates if research is in progress- if (data.status === 'in_progress') {- connectToResearchSocket(researchId);- }+ // Connect to socket for real-time updates+ window.connectToResearchSocket(researchId);// Add appropriate actions based on research statusconst detailActions = document.getElementById('detail-actions');@@ -860,7 +964,7 @@ document.addEventListener('DOMContentLoaded', () => {switchPage('research-progress');// Connect to socket- connectToResearchSocket(researchId);+ window.connectToResearchSocket(researchId);});detailActions.appendChild(viewProgressBtn);}@@ -947,6 +1051,11 @@ document.addEventListener('DOMContentLoaded', () => {console.error('Error rendering log entries:', error);researchLog.innerHTML = '<div class="error-message">Error rendering log entries. Please try again later.</div>';}++ // Connect to socket for updates if this is an in-progress research+ if (research && research.status === 'in_progress') {+ window.connectToResearchSocket(researchId);+ }}// Function to update detail log with a new entry@@ -1030,39 +1139,6 @@ document.addEventListener('DOMContentLoaded', () => {return `${month} ${day}, ${dateYear}, ${hours}:${minutes}`;}}-- // Check for active research on page load- (async function checkActiveResearch() {- try {- const response = await fetch(getApiUrl('/api/history'));- const history = await response.json();-- // Find in-progress research- const activeResearch = history.find(item => item.status === 'in_progress');-- if (activeResearch) {- isResearchInProgress = true;- currentResearchId = activeResearch.id;- window.currentResearchId = currentResearchId;-- // Check if we're on the new research page and redirect to progress- const currentPage = Array.from(pages).find(page => page.classList.contains('active'));-- if (currentPage && currentPage.id === 'new-research') {- // Navigate to progress page- switchPage('research-progress');-- // Connect to socket for this research- connectToResearchSocket(currentResearchId);-- // Start polling for updates- pollResearchStatus(currentResearchId);- }- }- } catch (error) {- console.error('Error checking for active research:', error);- }- })();// Function to update progress UI from current researchfunction updateProgressFromCurrentResearch() {@@ -1077,7 +1153,7 @@ document.addEventListener('DOMContentLoaded', () => {document.getElementById('progress-percentage').textContent = `${data.progress || 0}%`;// Connect to socket for this research- connectToResearchSocket(currentResearchId);+ window.connectToResearchSocket(currentResearchId);}).catch(error => {console.error('Error fetching research status:', error);@@ -1143,6 +1219,9 @@ document.addEventListener('DOMContentLoaded', () => {}});}++ // Connect to socket for updates+ window.connectToResearchSocket(currentResearchId);}// Function to update the research progress page from nav click@@ -1162,7 +1241,7 @@ document.addEventListener('DOMContentLoaded', () => {updateProgressUI(data.progress || 0, data.status);// Connect to socket for live updates- connectToResearchSocket(currentResearchId);+ window.connectToResearchSocket(currentResearchId);// Check if we need to show the terminate buttonif (data.status === 'in_progress') {@@ -1551,11 +1630,21 @@ document.addEventListener('DOMContentLoaded', () => {}// Home navigation - redirect to warplink.dev- document.getElementById('home-link').addEventListener('click', function() {- window.location.href = 'https://warplink.dev';+ document.getElementById('home-link').addEventListener('click', function(e) {+ e.preventDefault();+ window.disconnectSocket();+ // Use a short timeout to ensure socket disconnects properly+ setTimeout(() => {+ window.location.href = 'https://warplink.dev';+ }, 100);});- document.getElementById('mobile-home-link').addEventListener('click', function() {- window.location.href = 'https://warplink.dev';+ document.getElementById('mobile-home-link').addEventListener('click', function(e) {+ e.preventDefault();+ window.disconnectSocket();+ // Use a short timeout to ensure socket disconnects properly+ setTimeout(() => {+ window.location.href = 'https://warplink.dev';+ }, 100);});});\ No newline at end of file