~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
upd: better panel, not folding, not filtering yet
hashedvikingfa106fca7aee
diff --git a/app.py b/app.pyindex 61f911145e..8792d7c258 100644--- a/app.py+++ b/app.py@@ -44,6 +44,9 @@ termination_flags = {}# Database setupDB_PATH = 'research_history.db'+# Output directory for research results+OUTPUT_DIR = 'research_outputs'+# Add Content Security Policy headers to allow Socket.IO to function@app.after_requestdef add_security_headers(response):@@ -99,7 +102,22 @@ def init_db():duration_seconds INTEGER,report_path TEXT,metadata TEXT,- progress_log TEXT+ progress_log TEXT,+ progress INTEGER+ )+ ''')++ # Create a dedicated table for research logs+ cursor.execute('''+ CREATE TABLE IF NOT EXISTS research_logs (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ research_id INTEGER NOT NULL,+ timestamp TEXT NOT NULL,+ message TEXT NOT NULL,+ log_type TEXT NOT NULL,+ progress INTEGER,+ metadata TEXT,+ FOREIGN KEY (research_id) REFERENCES research_history (id) ON DELETE CASCADE)''')@@ -111,6 +129,14 @@ def init_db():print("Adding missing 'duration_seconds' column to research_history table")cursor.execute('ALTER TABLE research_history ADD COLUMN duration_seconds INTEGER')+ # Check if the progress column exists, add it if missing+ if 'progress' not in columns:+ print("Adding missing 'progress' column to research_history table")+ cursor.execute('ALTER TABLE research_history ADD COLUMN progress INTEGER')++ # Enable foreign key support+ cursor.execute('PRAGMA foreign_keys = ON')+conn.commit()conn.close()@@ -154,6 +180,87 @@ def calculate_duration(created_at_str):return duration_seconds+# Add these helper functions after the calculate_duration function++def add_log_to_db(research_id, message, log_type='info', progress=None, metadata=None):+ """+ Store a log entry in the database++ Args:+ research_id: ID of the research+ message: Log message text+ log_type: Type of log (info, error, milestone)+ progress: Progress percentage (0-100)+ metadata: Additional metadata as dictionary (will be stored as JSON)+ """+ try:+ timestamp = datetime.utcnow().isoformat()+ metadata_json = json.dumps(metadata) if metadata else None++ conn = sqlite3.connect(DB_PATH)+ cursor = conn.cursor()+ cursor.execute(+ 'INSERT INTO research_logs (research_id, timestamp, message, log_type, progress, metadata) '+ 'VALUES (?, ?, ?, ?, ?, ?)',+ (research_id, timestamp, message, log_type, progress, metadata_json)+ )+ conn.commit()+ conn.close()+ return True+ except Exception as e:+ print(f"Error adding log to database: {str(e)}")+ print(traceback.format_exc())+ return False++def get_logs_for_research(research_id):+ """+ Retrieve all logs for a specific research ID++ Args:+ research_id: ID of the research++ Returns:+ List of log entries as dictionaries+ """+ try:+ conn = sqlite3.connect(DB_PATH)+ conn.row_factory = sqlite3.Row+ cursor = conn.cursor()+ cursor.execute(+ 'SELECT * FROM research_logs WHERE research_id = ? ORDER BY timestamp ASC',+ (research_id,)+ )+ results = cursor.fetchall()+ conn.close()++ logs = []+ for result in results:+ log_entry = dict(result)+ # Parse metadata JSON if it exists+ if log_entry.get('metadata'):+ try:+ log_entry['metadata'] = json.loads(log_entry['metadata'])+ except:+ log_entry['metadata'] = {}+ else:+ log_entry['metadata'] = {}++ # Convert entry for frontend consumption+ formatted_entry = {+ 'time': log_entry['timestamp'],+ 'message': log_entry['message'],+ 'progress': log_entry['progress'],+ 'metadata': log_entry['metadata'],+ 'type': log_entry['log_type']+ }+ logs.append(formatted_entry)++ return logs+ except Exception as e:+ print(f"Error retrieving logs from database: {str(e)}")+ print(traceback.format_exc())+ return []+# Initialize the database on startup# @app.before_first_request # This is deprecated in newer Flask versionsdef initialize():@@ -385,15 +492,23 @@ def get_research_details(research_id):if not result:return jsonify({'status': 'error', 'message': 'Research not found'}), 404- try:- # Get the progress log- progress_log = json.loads(result.get('progress_log', '[]'))- except:- progress_log = []-- # If this is an active research, get the latest log+ # Get logs from the dedicated log database+ logs = get_logs_for_research(research_id)++ # If this is an active research, merge with any in-memory logsif research_id in active_research:- progress_log = active_research[research_id]['log']+ # Use the logs from memory temporarily until they're saved to the database+ memory_logs = active_research[research_id]['log']++ # Filter out logs that are already in the database by timestamp+ db_timestamps = {log['time'] for log in logs}+ unique_memory_logs = [log for log in memory_logs if log['time'] not in db_timestamps]++ # Add unique memory logs to our return list+ logs.extend(unique_memory_logs)++ # Sort logs by timestamp+ logs.sort(key=lambda x: x['time'])return jsonify({'status': 'success',@@ -404,7 +519,7 @@ def get_research_details(research_id):'progress': active_research.get(research_id, {}).get('progress', 100 if result.get('status') == 'completed' else 0),'created_at': result.get('created_at'),'completed_at': result.get('completed_at'),- 'log': progress_log+ 'log': logs})@research_bp.route('/api/report/<int:research_id>')@@ -523,6 +638,19 @@ def cleanup_research_resources(research_id):"""Clean up resources for a completed research"""print(f"Cleaning up resources for research {research_id}")+ # Get the current status from the database to determine the final status message+ current_status = "completed" # Default+ try:+ conn = sqlite3.connect(DB_PATH)+ cursor = conn.cursor()+ cursor.execute('SELECT status FROM research_history WHERE id = ?', (research_id,))+ result = cursor.fetchone()+ if result and result[0]:+ current_status = result[0]+ conn.close()+ except Exception as e:+ print(f"Error retrieving research status during cleanup: {e}")+# Remove from active researchif research_id in active_research:del active_research[research_id]@@ -531,16 +659,24 @@ def cleanup_research_resources(research_id):if research_id in termination_flags:del termination_flags[research_id]- # Send a final message to any remaining subscribers with explicit completed status+ # Send a final message to any remaining subscribers with explicit statusif research_id in socket_subscriptions and socket_subscriptions[research_id]:- final_message = {- 'status': 'completed',- 'message': 'Research process has ended and resources have been cleaned up',- 'progress': 100,- }+ # Use the proper status message based on database status+ if current_status == 'suspended' or current_status == 'failed':+ final_message = {+ 'status': current_status,+ 'message': f'Research was {current_status}',+ 'progress': 0, # For suspended research, show 0% not 100%+ }+ else:+ final_message = {+ 'status': 'completed',+ 'message': 'Research process has ended and resources have been cleaned up',+ 'progress': 100,+ }try:- print(f"Sending final completion socket message for research {research_id}")+ print(f"Sending final {current_status} socket message for research {research_id}")# Use emit to all, not just subscriberssocketio.emit(f'research_progress_{research_id}', final_message)@@ -560,11 +696,52 @@ def cleanup_research_resources(research_id):# Don't immediately remove subscriptions - let clients disconnect naturallydef run_research_process(research_id, query, mode):+ """Run the research process in the background for a given research ID"""try:- system = AdvancedSearchSystem()+ # Check if this research has been terminated before we even start+ if research_id in termination_flags and termination_flags[research_id]:+ print(f"Research {research_id} was terminated before starting")+ cleanup_research_resources(research_id)+ return++ print(f"Starting research process for ID {research_id}, query: {query}")+ # Set up the AI Context Manager+ output_dir = os.path.join(OUTPUT_DIR, f"research_{research_id}")+ os.makedirs(output_dir, exist_ok=True)+# Set up progress callbackdef progress_callback(message, progress_percent, metadata):+ # FREQUENT TERMINATION CHECK: Check for termination at each callback+ if research_id in termination_flags and termination_flags[research_id]:+ # Explicitly set the status to suspended in the database+ conn = sqlite3.connect(DB_PATH)+ cursor = conn.cursor()+ # Calculate duration up to termination point - using UTC consistently+ now = datetime.utcnow()+ completed_at = now.isoformat()++ # Get the start time from the database+ cursor.execute('SELECT created_at FROM research_history WHERE id = ?', (research_id,))+ result = cursor.fetchone()++ # Calculate the duration+ duration_seconds = calculate_duration(result[0]) if result and result[0] else None++ # Update the database with suspended status+ cursor.execute(+ 'UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ? WHERE id = ?',+ ('suspended', completed_at, duration_seconds, research_id)+ )+ conn.commit()+ conn.close()++ # Clean up resources+ cleanup_research_resources(research_id)++ # Raise exception to exit the process+ raise Exception("Research was terminated by user")+timestamp = datetime.utcnow().isoformat()# Adjust progress based on research mode@@ -601,7 +778,32 @@ def run_research_process(research_id, query, mode):# Check if termination was requestedif research_id in termination_flags and termination_flags[research_id]:- # Clean up and exit+ # Explicitly set the status to suspended in the database+ conn = sqlite3.connect(DB_PATH)+ cursor = conn.cursor()+ # Calculate duration up to termination point - using UTC consistently+ now = datetime.utcnow()+ completed_at = now.isoformat()++ # Get the start time from the database+ cursor.execute('SELECT created_at FROM research_history WHERE id = ?', (research_id,))+ result = cursor.fetchone()++ # Calculate the duration+ duration_seconds = calculate_duration(result[0]) if result and result[0] else None++ # Update the database with suspended status+ cursor.execute(+ 'UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ? WHERE id = ?',+ ('suspended', completed_at, duration_seconds, research_id)+ )+ conn.commit()+ conn.close()++ # Clean up resources+ cleanup_research_resources(research_id)++ # Raise exception to exit the processraise Exception("Research was terminated by user")# Update active research record@@ -610,59 +812,111 @@ def run_research_process(research_id, query, mode):if adjusted_progress is not None:active_research[research_id]['progress'] = adjusted_progress- # Save to database (but not too frequently)- if adjusted_progress is None or adjusted_progress % 10 == 0 or metadata.get('phase') in ['complete', 'iteration_complete', 'output_generation', 'report_generation', 'report_complete']:- conn = sqlite3.connect(DB_PATH)- cursor = conn.cursor()+ # Determine log type for database storage+ log_type = 'info'+ if metadata and metadata.get('phase'):+ phase = metadata.get('phase')+ if phase in ['complete', 'iteration_complete', 'error']:+ log_type = 'milestone'+ elif phase == 'error' or 'error' in message.lower():+ log_type = 'error'++ # Always save logs to the new research_logs table+ add_log_to_db(+ research_id,+ message,+ log_type=log_type,+ progress=adjusted_progress,+ metadata=metadata+ )++ # Update progress in the research_history table (for backward compatibility)+ conn = sqlite3.connect(DB_PATH)+ cursor = conn.cursor()++ # Update the progress and log separately to avoid race conditions with reading/writing the log+ if adjusted_progress is not None:cursor.execute(- 'SELECT progress_log FROM research_history WHERE id = ?',- (research_id,)+ 'UPDATE research_history SET progress = ? WHERE id = ?',+ (adjusted_progress, research_id))- result = cursor.fetchone()- if result:- try:- current_log = json.loads(result[0])- except:- current_log = []- current_log.append(log_entry)- cursor.execute(- 'UPDATE research_history SET progress_log = ? WHERE id = ?',- (json.dumps(current_log), research_id)- )- conn.commit()- conn.close()- # Emit socket event with try/except block to handle connection issues+ # Add the log entry to the progress_log+ cursor.execute('SELECT progress_log FROM research_history WHERE id = ?', (research_id,))+ log_result = cursor.fetchone()++ if log_result:+ try:+ current_log = json.loads(log_result[0])+ except:+ current_log = []++ current_log.append(log_entry)+ cursor.execute(+ 'UPDATE research_history SET progress_log = ? WHERE id = ?',+ (json.dumps(current_log), research_id)+ )++ conn.commit()+ conn.close()++ # Emit a socket eventtry:+ # Basic event dataevent_data = {- 'progress': adjusted_progress,'message': message,- 'status': 'in_progress',- 'log_entry': log_entry+ 'progress': adjusted_progress}- # Emit to the specific research channel+ # Add log entry in full format for detailed logging on client+ if metadata:+ event_data['log_entry'] = log_entry++ # Send to all subscribers and broadcast channelsocketio.emit(f'research_progress_{research_id}', event_data)- # Also emit to specific subscribers if available- if research_id in socket_subscriptions and socket_subscriptions[research_id]:+ if research_id in socket_subscriptions:for sid in socket_subscriptions[research_id]:try:socketio.emit(f'research_progress_{research_id}',- event_data,+ event_data,room=sid)- except Exception as sub_err:- print(f"Error emitting to subscriber {sid}: {str(sub_err)}")+ except Exception as err:+ print(f"Error emitting to subscriber {sid}: {str(err)}")+ except Exception as e:+ print(f"Socket emit error (non-critical): {str(e)}")- except Exception as socket_error:- # Log socket error but continue with the research process- print(f"Socket emit error (non-critical): {str(socket_error)}")-- return not (research_id in termination_flags and termination_flags[research_id])-+ # FUNCTION TO CHECK TERMINATION DURING LONG-RUNNING OPERATIONS+ def check_termination():+ if research_id in termination_flags and termination_flags[research_id]:+ # Explicitly set the status to suspended in the database+ conn = sqlite3.connect(DB_PATH)+ cursor = conn.cursor()+ now = datetime.utcnow()+ completed_at = now.isoformat()++ cursor.execute('SELECT created_at FROM research_history WHERE id = ?', (research_id,))+ result = cursor.fetchone()+ duration_seconds = calculate_duration(result[0]) if result and result[0] else None++ cursor.execute(+ 'UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ? WHERE id = ?',+ ('suspended', completed_at, duration_seconds, research_id)+ )+ conn.commit()+ conn.close()++ # Clean up resources+ cleanup_research_resources(research_id)++ # Raise exception to exit the process+ raise Exception("Research was terminated by user during long-running operation")+ return False # Not terminated+# Set the progress callback in the system+ system = AdvancedSearchSystem()system.set_progress_callback(progress_callback)# Run the search@@ -920,7 +1174,7 @@ def run_research_process(research_id, query, mode):except Exception as inner_e:print(f"Error in error handler: {str(inner_e)}")- # Clean up resources - moved to a separate function+ # Clean up resources - moved to a separate function for reusecleanup_research_resources(research_id)@research_bp.route('/api/research/<int:research_id>/terminate', methods=['POST'])@@ -957,16 +1211,30 @@ def terminate_research(research_id):# Log the termination request - using UTC timestamptimestamp = datetime.utcnow().isoformat()+ termination_message = "Research termination requested by user"+ current_progress = active_research[research_id]['progress']++ # Create log entrylog_entry = {"time": timestamp,- "message": "Research termination requested by user",- "progress": active_research[research_id]['progress'],+ "message": termination_message,+ "progress": current_progress,"metadata": {"phase": "termination"}}+ # Add to in-memory logactive_research[research_id]['log'].append(log_entry)- # Update the log in the database+ # Add to database log+ add_log_to_db(+ research_id,+ termination_message,+ log_type='milestone',+ progress=current_progress,+ metadata={"phase": "termination"}+ )++ # Update the log in the database (old way for backward compatibility)cursor.execute('SELECT progress_log FROM research_history WHERE id = ?', (research_id,))log_result = cursor.fetchone()if log_result:@@ -980,14 +1248,16 @@ def terminate_research(research_id):(json.dumps(current_log), research_id))+ # IMMEDIATELY update the status to 'suspended' to avoid race conditions+ cursor.execute('UPDATE research_history SET status = ? WHERE id = ?', ('suspended', research_id))conn.commit()conn.close()# Emit a socket event for the termination requesttry:event_data = {- 'status': 'terminating',- 'message': 'Research termination requested by user'+ 'status': 'suspended', # Changed from 'terminating' to 'suspended'+ 'message': 'Research was suspended by user request'}socketio.emit(f'research_progress_{research_id}', event_data)@@ -1046,6 +1316,44 @@ def delete_research(research_id):return jsonify({'status': 'success'})+@research_bp.route('/api/research/<int:research_id>/logs')+def get_research_logs(research_id):+ """Get logs for a specific research ID"""+ # First check if the research exists+ conn = sqlite3.connect(DB_PATH)+ conn.row_factory = sqlite3.Row+ cursor = conn.cursor()+ cursor.execute('SELECT id FROM research_history WHERE id = ?', (research_id,))+ result = cursor.fetchone()+ conn.close()++ if not result:+ return jsonify({'status': 'error', 'message': 'Research not found'}), 404++ # Retrieve logs from the database+ logs = get_logs_for_research(research_id)++ # Add any current logs from memory if this is an active research+ if research_id in active_research and active_research[research_id].get('log'):+ # Use the logs from memory temporarily until they're saved to the database+ memory_logs = active_research[research_id]['log']++ # Filter out logs that are already in the database+ # We'll compare timestamps to avoid duplicates+ db_timestamps = {log['time'] for log in logs}+ unique_memory_logs = [log for log in memory_logs if log['time'] not in db_timestamps]++ # Add unique memory logs to our return list+ logs.extend(unique_memory_logs)++ # Sort logs by timestamp+ logs.sort(key=lambda x: x['time'])++ return jsonify({+ 'status': 'success',+ 'logs': logs+ })+# Register the blueprintapp.register_blueprint(research_bp)diff --git a/static/css/styles.css b/static/css/styles.cssindex 9e16c92cb3..ce81213df5 100644--- a/static/css/styles.css+++ b/static/css/styles.css@@ -440,7 +440,7 @@ textarea:focus, input[type="text"]:focus {.status-suspended {background-color: rgba(243, 156, 18, 0.15);- color: #f39c12;+ color: #ffc107;}.status-terminating {@@ -1020,13 +1020,9 @@ textarea:focus, input[type="text"]:focus {/* Collapsible Log Panel */.collapsible-log-panel {- position: fixed;- bottom: 0;- left: 240px; /* Same as sidebar width */- right: 0;- z-index: 100;+ padding: 1.5rem;border: 1px solid var(--border-color);- border-radius: 8px 8px 0 0;+ border-radius: 8px;background-color: var(--bg-tertiary);display: flex;flex-direction: column;@@ -1132,7 +1128,7 @@ textarea:focus, input[type="text"]:focus {}.console-log {- padding: 0.5rem 1rem;+ padding: 0.5rem;font-family: 'Consolas', 'Monaco', monospace;font-size: 0.85rem;line-height: 1.4;@@ -1141,7 +1137,7 @@ textarea:focus, input[type="text"]:focus {}.console-log-entry {- padding: 0.5rem 1rem;+ padding: 0.5rem;display: flex;align-items: flex-start;border-bottom: 1px solid rgba(255, 255, 255, 0.05);@@ -1172,7 +1168,8 @@ textarea:focus, input[type="text"]:focus {.console-log-entry .log-message {flex: 1;word-break: break-word;- padding-right: 1rem;+ padding-right: 0.5rem;+ padding-left: 0.5rem;}.console-log-entry.log-milestone .log-badge {@@ -1195,4 +1192,24 @@ textarea:focus, input[type="text"]:focus {padding: 1rem;text-align: center;font-style: italic;+}++/* Add warning message styling */+.warning-message {+ background-color: rgba(255, 193, 7, 0.1);+ color: #ffc107;+ border-color: rgba(255, 193, 7, 0.2);+}++/* Update existing error message styling if needed */+.error-message {+ display: none;+ margin: 15px 0;+ padding: 10px 15px;+ border-radius: 4px;+ border-left: 4px solid;+ background-color: rgba(220, 53, 69, 0.1);+ color: #dc3545;+ border-color: rgba(220, 53, 69, 0.2);+ font-weight: 500;}\ No newline at end of filediff --git a/static/js/app.js b/static/js/app.jsindex 743930d014..83a4efb4d0 100644--- a/static/js/app.js+++ b/static/js/app.js@@ -20,19 +20,25 @@ document.addEventListener('DOMContentLoaded', () => {// Add function to cleanup research resources globallywindow.cleanupResearchResources = function() {console.log('Cleaning up research resources');++ // Disconnect any active sockets+ disconnectAllSockets();++ // Remove any active research data+ if (window.currentResearchId) {+ console.log(`Cleaning up research ID: ${window.currentResearchId}`);+ window.currentResearchId = null;+ }++ // Reset any active timersif (pollingInterval) {clearInterval(pollingInterval);pollingInterval = null;+ console.log('Cleared polling interval during cleanup');}- isResearchInProgress = false;- currentResearchId = null;- window.currentResearchId = null;- // Hide the terminate button if it exists- const terminateBtn = document.getElementById('terminate-research-btn');- if (terminateBtn) {- terminateBtn.style.display = 'none';- }+ // Reset research state flags+ isResearchInProgress = false;};// Initialize notification sounds@@ -198,37 +204,64 @@ document.addEventListener('DOMContentLoaded', () => {}};- // Connect to the research socket for real-time updates- window.connectToResearchSocket = function(researchId) {- if (socket) {- if (!socketConnected) {- // Try to reconnect- socket.connect();- }-- // Subscribe to this research- socket.emit('subscribe_to_research', { research_id: researchId });+ // Function to connect to socket for a research+ window.connectToResearchSocket = async function(researchId) {+ if (!researchId) {+ console.error('No research ID provided for socket connection');+ return;+ }++ try {+ // Check if research is terminated/suspended before connecting+ const response = await fetch(getApiUrl(`/api/research/${researchId}`));+ const data = await response.json();- // Add socket event handlers for this research- const eventName = `research_progress_${researchId}`;+ // Don't connect to socket for terminated or suspended research+ if (data.status === 'suspended' || data.status === 'failed') {+ console.log(`Not connecting socket for ${data.status} research ${researchId}`);++ // Make sure UI reflects the suspended state+ updateTerminationUIState('suspended', `Research was ${data.status}`);+ return;+ }+ // Don't connect to completed research+ else if (data.status === 'completed') {+ console.log(`Not connecting socket for completed research ${researchId}`);+ return;+ }- // Remove any existing listeners to avoid duplicates- socket.off(eventName);+ console.log(`Connecting to socket for research ${researchId} (status: ${data.status})`);- // Add a new listener- socket.on(eventName, function(data) {- // Pass the data to our event handler- window.handleResearchProgressEvent(data);- });+ // Initialize socket if it doesn't exist+ if (!socket) {+ initializeSocket();+ }- console.log(`Connected to socket for research ${researchId}`);- } else {- console.error('Socket not initialized');- initializeSocket();- // Try again after initialization- setTimeout(() => {- window.connectToResearchSocket(researchId);- }, 1000);+ // Subscribe to the research channel+ if (socket && socket.connected) {+ socket.emit('subscribe_to_research', { research_id: researchId });+ console.log(`Subscribed to research ${researchId}`);+ } else {+ console.warn('Socket not connected, waiting for connection...');+ // Wait for socket to connect+ const maxAttempts = 5;+ let attempts = 0;++ const socketConnectInterval = setInterval(() => {+ attempts++;+ if (socket && socket.connected) {+ socket.emit('subscribe_to_research', { research_id: researchId });+ console.log(`Subscribed to research ${researchId} after ${attempts} attempts`);+ clearInterval(socketConnectInterval);+ } else if (attempts >= maxAttempts) {+ console.error(`Failed to connect to socket after ${maxAttempts} attempts`);+ clearInterval(socketConnectInterval);+ addConsoleLog('Failed to connect to real-time updates', 'error');+ }+ }, 1000);+ }+ } catch (error) {+ console.error(`Error connecting to socket for research ${researchId}:`, error);}};@@ -314,7 +347,7 @@ document.addEventListener('DOMContentLoaded', () => {}}- // Update the socket event handler to prevent duplicate log messages+ // Update the socket event handler to fix termination handlingwindow.handleResearchProgressEvent = function(data) {console.log('Research progress update:', data);@@ -333,13 +366,22 @@ document.addEventListener('DOMContentLoaded', () => {window.processedMessages.add(messageId);// Determine log type based on status or message content- if (data.status === 'failed' || data.status === 'suspended') {+ if (data.status === 'failed' || data.status === 'suspended' || data.status === 'terminating') {logType = 'error';} else if (isMilestoneLog(data.message, data.log_entry?.metadata)) {logType = 'milestone';}- addConsoleLog(data.message, logType);+ // Store meaningful messages to avoid overwriting with generic messages+ if (data.message && data.message !== 'Processing research...') {+ window.lastMeaningfulStatusMessage = data.message;+ }++ // Extract metadata for search engine information+ const metadata = data.log_entry?.metadata || null;++ // Pass metadata to addConsoleLog for potential search engine info+ addConsoleLog(data.message, logType, metadata);}}@@ -347,11 +389,15 @@ document.addEventListener('DOMContentLoaded', () => {if (data.error && !window.processedMessages.has('error:' + data.error)) {window.processedMessages.add('error:' + data.error);addConsoleLog(data.error, 'error');++ // Store error as last meaningful message+ window.lastMeaningfulStatusMessage = data.error;}// Update progress UI if progress is providedif (data.progress !== undefined) {- updateProgressUI(data.progress, data.status, data.message);+ const displayMessage = data.message || window.lastMeaningfulStatusMessage || 'Processing research...';+ updateProgressUI(data.progress, data.status, displayMessage);}// Update detail log if log_entry is provided@@ -359,68 +405,117 @@ document.addEventListener('DOMContentLoaded', () => {updateDetailLogEntry(data.log_entry);}- // Handle completion states- if (data.status === 'completed' || data.status === 'failed' || data.status === 'suspended') {- const researchId = getActiveResearchId();- if (!researchId) return;-- // Clear polling interval- if (pollingInterval) {- console.log('Clearing polling interval from socket event');- clearInterval(pollingInterval);- pollingInterval = null;+ // Handle status changes+ if (data.status) {+ // Special handling for terminating status and handling already terminated research+ if (data.status === 'terminating') {+ // Immediately mark as suspended and update UI+ isResearchInProgress = false;++ // Update UI state+ updateTerminationUIState('suspending', data.message || 'Terminating research...');}-- if (data.status === 'completed') {- // Success sound and notification- playNotificationSound('success');+ // Handle suspended research specifically+ else if (data.status === 'suspended') {+ console.log('Research was suspended, updating UI directly');- // Store the completed research ID for navigation- const completedResearchId = researchId;+ const researchId = getActiveResearchId();+ if (!researchId) return;++ // Mark research as not in progress+ isResearchInProgress = false;++ // Clear polling interval+ if (pollingInterval) {+ console.log('Clearing polling interval due to suspension');+ clearInterval(pollingInterval);+ pollingInterval = null;+ }++ // Play error notification sound+ playNotificationSound('error');++ // Update UI for suspended research+ updateTerminationUIState('suspended', data.message || 'Research was suspended');++ // Add console log+ addConsoleLog('Research suspended', 'error');++ // Reset lastMeaningfulStatusMessage for next research+ window.lastMeaningfulStatusMessage = '';// Reset current research IDcurrentResearchId = null;window.currentResearchId = null;- // Update navigation state- isResearchInProgress = false;+ // Update navigationupdateNavigationBasedOnResearchStatus();- // Navigate to results page with a slight delay to ensure all updates are processed- setTimeout(() => {- // Load the research results- loadResearch(completedResearchId);- }, 800);- } else {- // Error sound and notification- playNotificationSound('error');+ // Refresh history if on history page+ if (document.getElementById('history').classList.contains('active')) {+ loadResearchHistory();+ }+ }+ // Handle completion states+ else if (data.status === 'completed' || data.status === 'failed') {+ const researchId = getActiveResearchId();+ if (!researchId) return;- // Show error message and Try Again button for errors and terminations- const errorMessage = document.getElementById('error-message');- const tryAgainBtn = document.getElementById('try-again-btn');+ // Mark research as not in progress+ isResearchInProgress = false;- if (errorMessage) {- errorMessage.textContent = data.error || 'Research was terminated';- errorMessage.style.display = 'block';+ // Clear polling interval+ if (pollingInterval) {+ console.log('Clearing polling interval from socket event');+ clearInterval(pollingInterval);+ pollingInterval = null;}- if (tryAgainBtn) {- tryAgainBtn.style.display = 'block';+ if (data.status === 'completed') {+ // Success sound and notification+ playNotificationSound('success');++ // Store the completed research ID for navigation+ const completedResearchId = researchId;++ // Reset current research ID+ currentResearchId = null;+ window.currentResearchId = null;++ // Reset lastMeaningfulStatusMessage for next research+ window.lastMeaningfulStatusMessage = '';++ // Update navigation state+ updateNavigationBasedOnResearchStatus();++ // Navigate to results page with a slight delay to ensure all updates are processed+ setTimeout(() => {+ // Load the research results+ loadResearch(completedResearchId);+ }, 800);+ } else {+ // Error sound and notification+ playNotificationSound('error');++ // Use the updateTerminationUIState function for consistency+ updateTerminationUIState('suspended', data.error || `Research was ${data.status}`);++ // Reset lastMeaningfulStatusMessage for next research+ window.lastMeaningfulStatusMessage = '';++ // Reset current research ID+ currentResearchId = null;+ window.currentResearchId = null;++ // Update navigation - important for correctly showing/hiding various elements+ // based on the current research state+ updateNavigationBasedOnResearchStatus();}- // Update navigation - important for correctly showing/hiding various elements- // based on the current research state- isResearchInProgress = false;- updateNavigationBasedOnResearchStatus();-- // Reset current research ID- currentResearchId = null;- window.currentResearchId = null;- }-- // Refresh the history list to show the completed research- if (document.getElementById('history').classList.contains('active')) {- loadResearchHistory();+ // Refresh the history list to show the completed research+ if (document.getElementById('history').classList.contains('active')) {+ loadResearchHistory();+ }}}};@@ -505,92 +600,83 @@ document.addEventListener('DOMContentLoaded', () => {// Function to start researchasync function startResearch(query, mode) {+ // First validate that we have a query+ if (!query || query.trim() === '') {+ alert('Please enter a query');+ return;+ }+try {- // Reset any previous research states- window.cleanupResearchResources();- clearPollingInterval();-- // Clear logs when starting new research- clearConsoleLogs();-- // Add initial log entry- addConsoleLog(`Starting research: ${query}`, 'milestone');-- // Set favicon based on research mode- setFavicon(mode);-- // Show loading state+ // Update button stateconst startBtn = document.getElementById('start-research-btn');if (startBtn) {- startBtn.innerHTML = '<div class="spinner-small"></div> Starting...';+ startBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Starting...';startBtn.disabled = true;}- // Setup the form data- const data = {+ // Clear any previous research+ resetResearchState();++ // Set favicon to loading+ setFavicon('loading');++ // Get the current query element+ const currentQueryEl = document.getElementById('current-query');+ if (currentQueryEl) {+ currentQueryEl.textContent = query;+ }++ // Create payload+ const payload = {query: query,- mode: mode+ mode: mode || 'quick'};- // Make the API request+ // Call the APIconst response = await fetch(getApiUrl('/api/start_research'), {method: 'POST',headers: {'Content-Type': 'application/json'},- body: JSON.stringify(data)+ body: JSON.stringify(payload)});+ // Parse the responseconst result = await response.json();if (result.status === 'success') {- // Store the research ID for progress tracking+ // Update the current research ID+ currentResearchId = result.research_id;window.currentResearchId = result.research_id;- // Add a log entry- addConsoleLog(`Research started with ID: ${result.research_id}`, 'info');-- // Navigate to progress page- switchPage('research-progress');-- // Update the current query display- const currentQueryEl = document.getElementById('current-query');- if (currentQueryEl) {- currentQueryEl.textContent = query;- }-- // Set up progress polling- pollResearchStatus(result.research_id);-- // Also try to subscribe via socket for real-time updates- if (socket && socket.connected) {- socket.emit('subscribe_to_research', { research_id: result.research_id });- addConsoleLog('Connected to real-time updates', 'info');- }-- // Show the terminate button- const terminateBtn = document.getElementById('terminate-research-btn');- if (terminateBtn) {- terminateBtn.style.display = 'block';- }+ console.log(`Started research with ID: ${currentResearchId}`);- // Hide any previous error message- const errorMessage = document.getElementById('error-message');- if (errorMessage) {- errorMessage.style.display = 'none';- }+ // Mark as in progress+ isResearchInProgress = true;- // Hide any previous try again button+ // Hide the try again button if visibleconst tryAgainBtn = document.getElementById('try-again-btn');if (tryAgainBtn) {tryAgainBtn.style.display = 'none';}// Reset progress UI- updateProgressUI(0, 'in_progress', 'Initializing research process...');-+ updateProgressUI(0, 'in_progress', `Researching: ${query}`);++ // Store query in case we need to display it again+ window.currentResearchQuery = query;+// Update navigationupdateNavigationBasedOnResearchStatus();++ // Navigate to the progress page+ switchPage('research-progress');++ // Connect to the socket for this research+ window.connectToResearchSocket(currentResearchId);++ // Start polling for status+ pollResearchStatus(currentResearchId);} else {// Handle errorconst errorMessage = result.message || 'Failed to start research';@@ -639,7 +725,7 @@ document.addEventListener('DOMContentLoaded', () => {}}- // Update the polling function to use a more robust approach+ // Update the polling function to prevent "Processing research..." from overwriting actual status messagesfunction pollResearchStatus(researchId) {if (!researchId) {console.error('No research ID provided for polling');@@ -647,13 +733,20 @@ document.addEventListener('DOMContentLoaded', () => {}// Reset message deduplication when starting a new poll- window.processedMessages = new Set();+ window.processedMessages = window.processedMessages || new Set();- // Update UI to show loading- document.getElementById('current-query').textContent = 'Loading research...';+ // Don't set "Loading research..." here, as we'll set the actual query from the response+ // Only set loading if currentQuery is empty+ const currentQueryEl = document.getElementById('current-query');+ if (currentQueryEl && (!currentQueryEl.textContent || currentQueryEl.textContent === 'Loading research...')) {+ currentQueryEl.textContent = window.currentResearchQuery || 'Loading research...';+ }console.log(`Starting polling for research ${researchId}`);+ // Store the last real message to avoid overwriting with generic messages+ window.lastMeaningfulStatusMessage = window.lastMeaningfulStatusMessage || '';+// Ensure we have a socket connectionif (typeof window.connectToResearchSocket === 'function') {window.connectToResearchSocket(researchId);@@ -674,17 +767,28 @@ document.addEventListener('DOMContentLoaded', () => {// Function to check research statusfunction checkResearchStatus(researchId) {- fetch(getApiUrl(`/api/research/${researchId}`))+ fetch(getApiUrl(`/api/research/${researchId}/details`)).then(response => response.json()).then(data => {+ // Update the current query with the actual query from the research+ const currentQueryEl = document.getElementById('current-query');+ if (currentQueryEl && data.query) {+ currentQueryEl.textContent = data.query;+ // Store the query in case we need it later+ window.currentResearchQuery = data.query;+ }+// Process status update- if (data) {+ if (data && data.status !== 'error') {// Update UI with progressconst progress = data.progress || 0;const status = data.status || 'in_progress';// Get most recent messagelet message = '';+ let foundNewMessage = false;+ let latestMetadata = null;+if (data.log && data.log.length > 0) {// Get the latest unique log entryfor (let i = data.log.length - 1; i >= 0; i--) {@@ -694,14 +798,25 @@ document.addEventListener('DOMContentLoaded', () => {if (!window.processedMessages.has(messageId)) {window.processedMessages.add(messageId);message = latestLog.message || '';+ latestMetadata = latestLog.metadata || null;++ // Only update the lastMeaningfulStatusMessage if we have a real message+ if (message && message !== 'Processing research...') {+ window.lastMeaningfulStatusMessage = message;+ foundNewMessage = true;+ }// Add to console logsif (message) {let logType = 'info';if (isMilestoneLog(message, latestLog.metadata)) {logType = 'milestone';+ } else if (latestLog.type === 'error' || (latestLog.metadata && latestLog.metadata.phase === 'error')) {+ logType = 'error';+ } else if (latestLog.type) {+ logType = latestLog.type; // Use the type from the database if available}- addConsoleLog(message, logType);+ addConsoleLog(message, logType, latestMetadata);}break; // Only process one message per poll@@ -709,8 +824,12 @@ document.addEventListener('DOMContentLoaded', () => {}}+ // Use a meaningful message if available; otherwise, keep the last good one+ const displayMessage = foundNewMessage ? message :+ (window.lastMeaningfulStatusMessage || 'Processing research...');+// Update progress UI- updateProgressUI(progress, status, message || 'Processing research...');+ updateProgressUI(progress, status, displayMessage);// Update the UI based on research statusif (status === 'completed' || status === 'failed' || status === 'suspended') {@@ -725,6 +844,26 @@ document.addEventListener('DOMContentLoaded', () => {if (status === 'completed') {addConsoleLog('Research completed successfully', 'milestone');playNotificationSound('success');++ // Store the completed research ID for navigation+ const completedResearchId = researchId;++ // Reset current research ID+ currentResearchId = null;+ window.currentResearchId = null;++ // Update navigation state+ isResearchInProgress = false;+ updateNavigationBasedOnResearchStatus();++ // Reset lastMeaningfulStatusMessage for next research+ window.lastMeaningfulStatusMessage = '';++ // Navigate to results page with a slight delay to ensure all updates are processed+ setTimeout(() => {+ // Load the research results+ loadResearch(completedResearchId);+ }, 800);} else {addConsoleLog(`Research ${status}`, 'error');playNotificationSound('error');@@ -741,13 +880,16 @@ document.addEventListener('DOMContentLoaded', () => {if (tryAgainBtn) {tryAgainBtn.style.display = 'block';}++ // Reset lastMeaningfulStatusMessage for next research+ window.lastMeaningfulStatusMessage = '';++ // Update navigation+ isResearchInProgress = false;+ currentResearchId = null;+ window.currentResearchId = null;+ updateNavigationBasedOnResearchStatus();}-- // Update navigation- isResearchInProgress = false;- currentResearchId = null;- window.currentResearchId = null;- updateNavigationBasedOnResearchStatus();} else {// Research is still in progressisResearchInProgress = true;@@ -763,6 +905,15 @@ document.addEventListener('DOMContentLoaded', () => {}}+ // Function to reset the start research button to its default state+ function resetStartResearchButton() {+ const startBtn = document.getElementById('start-research-btn');+ if (startBtn) {+ startBtn.innerHTML = '<i class="fas fa-rocket"></i> Start Research';+ startBtn.disabled = false;+ }+ }+// Main initialization functionfunction initializeApp() {console.log('Initializing application...');@@ -770,14 +921,33 @@ document.addEventListener('DOMContentLoaded', () => {// Initialize the soundsinitializeSounds();+ // Initialize socket connection+ initializeSocket();+// Create a dynamic favicon with the lightning emoji by defaultcreateDynamicFavicon('⚡');+ // Add try again button handler+ const tryAgainBtn = document.getElementById('try-again-btn');+ if (tryAgainBtn) {+ tryAgainBtn.addEventListener('click', function() {+ // Switch back to the new research page+ switchPage('new-research');++ // Reset the research state+ resetResearchState();++ // Reset the Start Research button+ resetStartResearchButton();+ });+ }+// Get navigation elementsconst 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');+ const logo = document.getElementById('logo-link');// Handle responsive navigation based on screen sizefunction handleResponsiveNavigation() {@@ -799,6 +969,14 @@ document.addEventListener('DOMContentLoaded', () => {// Add resize listener for responsive designwindow.addEventListener('resize', handleResponsiveNavigation);+ // Handle logo click+ if (logo) {+ logo.addEventListener('click', () => {+ switchPage('new-research');+ resetStartResearchButton();+ });+ }+// Setup navigation click handlersnavItems.forEach(item => {if (!item.classList.contains('external-link')) {@@ -806,6 +984,10 @@ document.addEventListener('DOMContentLoaded', () => {const pageId = this.dataset.page;if (pageId) {switchPage(pageId);+ // Reset Start Research button when returning to the form+ if (pageId === 'new-research') {+ resetStartResearchButton();+ }}});}@@ -817,6 +999,10 @@ document.addEventListener('DOMContentLoaded', () => {const pageId = this.dataset.page;if (pageId) {switchPage(pageId);+ // Reset Start Research button when returning to the form+ if (pageId === 'new-research') {+ resetStartResearchButton();+ }}});}@@ -916,15 +1102,41 @@ document.addEventListener('DOMContentLoaded', () => {return `/research${path}`;}- // Function to terminate research - exposed to window object+ // Function to properly disconnect all socket connections+ function disconnectAllSockets() {+ if (socket) {+ try {+ console.log('Disconnecting all socket connections');++ // Get the active research ID+ const researchId = getActiveResearchId();++ // If there's an active research, unsubscribe first+ if (researchId) {+ console.log(`Unsubscribing from research ${researchId}`);+ socket.emit('unsubscribe_from_research', { research_id: researchId });+ }++ // Also attempt to disconnect the socket+ socket.disconnect();+ socket = null;++ console.log('Socket disconnected successfully');+ } catch (error) {+ console.error('Error disconnecting socket:', error);+ }+ }+ }++ // Update the terminateResearch function to handle termination more gracefullyasync function terminateResearch(researchId) {if (!researchId) {console.error('No research ID provided for termination');return;}-+// Prevent multiple termination requests- if (isTerminating) {+ if (document.getElementById('terminate-research-btn')?.disabled) {console.log('Termination already in progress');return;}@@ -934,97 +1146,220 @@ document.addEventListener('DOMContentLoaded', () => {return;}+ console.log(`Attempting to terminate research: ${researchId}`);+try {- // Set terminating flag- isTerminating = true;-- // Update UI to show we're processing+ // Get the terminate buttonconst terminateBtn = document.getElementById('terminate-research-btn');if (terminateBtn) {+ // Disable the button to prevent multiple clicksterminateBtn.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+ // Update UI to show terminating state immediately+ updateTerminationUIState('suspending', 'Terminating research...');++ // Add a log entry+ addConsoleLog('Terminating research...', 'error');++ // Immediately mark the research as terminated in our app state+ // so we don't reconnect to it+ isResearchInProgress = false;++ // Disconnect all sockets to ensure we stop receiving updates+ disconnectAllSockets();++ // Clear the polling interval immediately+ if (pollingInterval) {+ clearInterval(pollingInterval);+ pollingInterval = null;+ console.log('Cleared polling interval during termination');+ }++ // Call the API to terminateconst response = await fetch(getApiUrl(`/api/research/${researchId}/terminate`), {- method: 'POST'+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json'+ }});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');+ console.log(`Research ${researchId} termination requested successfully`);- // If we're on the history page, update the status of this item- if (document.getElementById('history').classList.contains('active')) {- updateHistoryItemStatus(researchId, 'terminating', 'Terminating...');- }+ // Add termination log+ addConsoleLog('Research termination requested. Please wait...', 'error');- // Set a timeout to reset UI if socket doesn't respond- setTimeout(() => {- if (isTerminating) {- console.log('Termination timeout - resetting UI');- isTerminating = false;- resetProgressAnimations();+ // Immediately update UI for better responsiveness+ updateTerminationUIState('suspended', 'Research was terminated');++ // Start polling for the suspended status with a more reliable approach+ let checkAttempts = 0;+ const maxAttempts = 3; // Reduced from 5+ const checkInterval = setInterval(async () => {+ checkAttempts++;+ console.log(`Checking termination status (attempt ${checkAttempts}/${maxAttempts})...`);++ try {+ const statusResponse = await fetch(getApiUrl(`/api/research/${researchId}`));+ const statusData = await statusResponse.json();- // Update the navigation- updateNavigationBasedOnResearchStatus();+ // Check for actual termination status in the response+ if (statusData.status === 'suspended' || statusData.status === 'failed') {+ console.log(`Research is now ${statusData.status}, updating UI`);+ clearInterval(checkInterval);++ // Reset research state+ currentResearchId = null;+ window.currentResearchId = null;++ // Disconnect any remaining sockets again, just to be sure+ disconnectAllSockets();++ // Update navigation+ updateNavigationBasedOnResearchStatus();++ return;+ }++ // If we reach the maximum attempts but status isn't updated yet+ if (checkAttempts >= maxAttempts) {+ console.log('Max termination check attempts reached, forcing UI update');+ clearInterval(checkInterval);++ // Force update to suspended state even if backend hasn't caught up yet+ currentResearchId = null;+ window.currentResearchId = null;++ // Disconnect any remaining sockets+ disconnectAllSockets();++ // Update database status directly with a second termination request+ try {+ console.log('Sending second termination request to ensure completion');+ await fetch(getApiUrl(`/api/research/${researchId}/terminate`), {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' }+ });+ } catch (secondError) {+ console.error('Error sending second termination request:', secondError);+ }++ updateNavigationBasedOnResearchStatus();+ }+ } catch (checkError) {+ console.error(`Error checking termination status: ${checkError}`);+ if (checkAttempts >= maxAttempts) {+ clearInterval(checkInterval);+ updateTerminationUIState('error', 'Error checking termination status');+ }}- }, 10000); // 10 second timeout- } else {- console.error('Termination request failed:', data.message);- alert(`Failed to terminate research: ${data.message}`);+ }, 300); // Check faster for more responsive feedback- // Reset the terminating flag- isTerminating = false;+ } else {+ console.error(`Error terminating research: ${data.message}`);+ addConsoleLog(`Error terminating research: ${data.message}`, 'error');- // Reset the button+ // Re-enable the buttonif (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';- }- });}} catch (error) {- console.error('Error terminating research:', error);- alert('An error occurred while trying to terminate the research.');+ console.error(`Error in terminate request: ${error}`);+ addConsoleLog(`Error in terminate request: ${error}`, 'error');- // Reset the terminating flag- isTerminating = false;-- // Reset the button+ // Re-enable the buttonconst 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';+ }+ }++ // Helper function to update UI elements during termination+ function updateTerminationUIState(state, message) {+ const terminateBtn = document.getElementById('terminate-research-btn');+ const errorMessage = document.getElementById('error-message');+ const tryAgainBtn = document.getElementById('try-again-btn');+ const progressStatus = document.getElementById('progress-status');+ const progressBar = document.getElementById('progress-bar');++ switch (state) {+ case 'suspending':+ if (progressStatus) {+ progressStatus.textContent = 'Terminating research...';+ progressStatus.className = 'progress-status status-terminating fade-in';}- });+ if (progressBar) {+ progressBar.classList.remove('suspended-status');+ }+ if (terminateBtn) {+ terminateBtn.disabled = true;+ terminateBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Terminating...';+ }+ if (errorMessage) {+ errorMessage.style.display = 'none';+ }+ if (tryAgainBtn) {+ tryAgainBtn.style.display = 'none';+ }+ break;++ case 'suspended':+ if (progressStatus) {+ progressStatus.innerHTML = '<i class="fas fa-exclamation-triangle termination-icon"></i> ' + (message || 'Research was suspended');+ progressStatus.className = 'progress-status status-suspended fade-in';+ }+ if (progressBar) {+ progressBar.classList.add('suspended-status');+ }+ if (terminateBtn) {+ terminateBtn.style.display = 'none';+ }+ if (errorMessage) {+ errorMessage.innerHTML = '<i class="fas fa-exclamation-triangle termination-icon"></i> ' + (message || 'Research was suspended');+ errorMessage.style.display = 'block';+ errorMessage.className = 'error-message warning-message fade-in';+ }+ if (tryAgainBtn) {+ tryAgainBtn.style.display = 'block';+ // Update try again button to be more attractive+ tryAgainBtn.innerHTML = '<i class="fas fa-sync-alt"></i> Try Again';+ tryAgainBtn.className = 'btn btn-primary fade-in';+ }++ // Update page title to show suspension+ document.title = '⚠️ Research Suspended - Local Deep Research';++ break;++ case 'error':+ if (progressStatus) {+ progressStatus.innerHTML = '<i class="fas fa-exclamation-circle termination-icon"></i>' + (message || 'Error terminating research');+ progressStatus.className = 'progress-status status-failed fade-in';+ }+ if (progressBar) {+ progressBar.classList.remove('suspended-status');+ }+ if (terminateBtn) {+ terminateBtn.disabled = false;+ terminateBtn.innerHTML = '<i class="fas fa-stop-circle"></i> Terminate Research';+ }+ if (errorMessage) {+ errorMessage.innerHTML = '<i class="fas fa-exclamation-circle termination-icon"></i>' + (message || 'Error terminating research');+ errorMessage.style.display = 'block';+ errorMessage.className = 'error-message fade-in';+ }+ if (tryAgainBtn) {+ tryAgainBtn.style.display = 'none';+ }+ break;}}@@ -1333,6 +1668,26 @@ document.addEventListener('DOMContentLoaded', () => {// Function to navigate to research progressfunction navigateToResearchProgress(research) {+ // First check if the research is already terminated/suspended+ if (research.status === 'suspended' || research.status === 'failed') {+ // Switch to the progress page with terminated state+ switchPage('research-progress');++ // Show the query+ document.getElementById('current-query').textContent = research.query || '';++ // Update UI for terminated state+ updateTerminationUIState('suspended', `Research was ${research.status}`);++ // Update progress percentage+ const progress = research.progress || 0;+ document.getElementById('progress-fill').style.width = `${progress}%`;+ document.getElementById('progress-percentage').textContent = `${progress}%`;++ // Don't connect to socket or start polling for terminated research+ return;+ }+document.getElementById('current-query').textContent = research.query;document.getElementById('progress-fill').style.width = `${research.progress || 0}%`;document.getElementById('progress-percentage').textContent = `${research.progress || 0}%`;@@ -1366,96 +1721,89 @@ document.addEventListener('DOMContentLoaded', () => {}}- // Function to load research data and display results+ // Update the loadResearch function to handle terminated/suspended research betterasync function loadResearch(researchId) {try {- console.log(`Loading research results for ID: ${researchId}`);+ console.log(`Loading research results for research ID: ${researchId}`);- // Show loading indicators- const resultsContent = document.getElementById('results-content');- if (resultsContent) {- resultsContent.innerHTML = '<div class="loading-spinner centered"><div class="spinner"></div></div>';- }+ // Get research data first to check status+ const researchResponse = await fetch(getApiUrl(`/api/research/${researchId}`));+ const researchData = await researchResponse.json();- // Fetch the research data- const response = await fetch(getApiUrl(`/api/research/${researchId}`));- if (!response.ok) {- throw new Error(`Server returned ${response.status}`);+ // Check if research was terminated or failed+ if (researchData.status === 'suspended' || researchData.status === 'failed') {+ console.log(`Research ${researchId} was ${researchData.status}, not loading results`);++ // Switch to research progress page if not already there+ const progressPage = document.getElementById('research-progress');+ if (!progressPage.classList.contains('active')) {+ switchPage('research-progress');+ }++ // Show error message and Try Again button+ const errorMessage = document.getElementById('error-message');+ const tryAgainBtn = document.getElementById('try-again-btn');++ if (errorMessage) {+ errorMessage.textContent = researchData.error || `Research was ${researchData.status}`;+ errorMessage.style.display = 'block';+ }++ if (tryAgainBtn) {+ tryAgainBtn.style.display = 'block';+ }++ // Update UI elements for this research+ document.getElementById('current-query').textContent = researchData.query || 'Unknown query';++ // Update progress bar+ const progressFill = document.getElementById('progress-fill');+ const progressPercentage = document.getElementById('progress-percentage');+ if (progressFill) progressFill.style.width = '0%';+ if (progressPercentage) progressPercentage.textContent = '0%';++ return; // Exit early, no need to load report for terminated research}+ // Normal flow for completed research+ const response = await fetch(getApiUrl(`/api/report/${researchId}`));const data = await response.json();- console.log('Research data:', data);- // Check if the report path exists- if (!data.report_path && !data.metadata?.report_path) {- console.error('No report path found in research data');+ if (data.status === 'error') {throw new Error('Research report not found');}- // Get report content- const reportResponse = await fetch(getApiUrl(`/api/report/${researchId}`));- if (!reportResponse.ok) {- throw new Error(`Server returned ${reportResponse.status} when fetching report`);+ if (!data.content) {+ console.error('No report content found in research data');+ throw new Error('Report content is empty');}- const reportData = await reportResponse.json();- console.log('Report data status:', reportData.status);+ // Set the report content and metadata+ document.getElementById('result-query').textContent = researchData.query || 'Unknown Query';+ document.getElementById('result-date').textContent = formatDate(researchData.completed_at);+ document.getElementById('result-mode').textContent = formatMode(researchData.mode);++ // Render the content+ const resultsContent = document.getElementById('results-content');+ resultsContent.innerHTML = ''; // Clear any previous content++ // Convert markdown to HTML+ const htmlContent = marked.parse(data.content);+ resultsContent.innerHTML = htmlContent;++ // Apply code highlighting+ document.querySelectorAll('pre code').forEach((block) => {+ hljs.highlightBlock(block);+ });++ // Switch to the results page+ switchPage('research-results');- if (reportData.status === 'success' && reportData.content) {- // Update metadata display- const queryEl = document.getElementById('result-query');- const dateEl = document.getElementById('result-date');- const modeEl = document.getElementById('result-mode');-- if (queryEl) queryEl.textContent = data.query || data.metadata?.query || 'Unknown query';- if (dateEl) dateEl.textContent = formatDate(data.completed_at || data.metadata?.completed_at || data.metadata?.generated_at);- if (modeEl) modeEl.textContent = formatMode(data.mode || data.metadata?.mode);-- // Update duration if available- if (data.metadata) {- const metadata = data.metadata;- // For duration, use created_at and completed_at- if (!metadata.started_at && data.created_at) {- metadata.started_at = data.created_at;- }- if (!metadata.completed_at && data.completed_at) {- metadata.completed_at = data.completed_at;- }- updateResearchDuration(metadata);- }-- // Render the report content- if (resultsContent) {- resultsContent.innerHTML = marked.parse(reportData.content);-- // Apply syntax highlighting- document.querySelectorAll('pre code').forEach((block) => {- try {- hljs.highlightBlock(block);- } catch (e) {- console.error('Error applying syntax highlighting:', e);- }- });- }-- // Enable the PDF download button- const pdfBtn = document.getElementById('download-pdf-btn');- if (pdfBtn) {- pdfBtn.disabled = false;- pdfBtn.onclick = function() {- generatePdf(researchId, data.query || data.metadata?.query);- };- }-- // Switch to the results page- switchPage('research-results');- console.log('Switched to results page successfully');- } else {- throw new Error(reportData.message || 'Failed to load report content');- }} catch (error) {- console.error('Error loading research:', error);- alert(`Failed to load research: ${error.message}`);+ console.error(`Error loading research: ${error}`);++ // Stay on the current page, but show an error toast or notification+ // Don't try to navigate to results for failed research}}@@ -1466,41 +1814,54 @@ document.addEventListener('DOMContentLoaded', () => {document.getElementById('research-log').innerHTML = '<div class="loading-spinner centered"><div class="spinner"></div></div>';// Fetch the research data- const response = await fetch(getApiUrl(`/api/research/${researchId}`));+ const response = await fetch(getApiUrl(`/api/research/${researchId}/details`));const data = await response.json();if (data.status === 'success') {// Update the metadata display- document.getElementById('detail-query').textContent = data.metadata.query || 'Unknown query';- document.getElementById('detail-status').textContent = formatStatus(data.metadata.status);- document.getElementById('detail-mode').textContent = formatMode(data.metadata.mode);-- // Update progress percentage+ document.getElementById('detail-query').textContent = data.query || 'Unknown query';+ document.getElementById('detail-status').textContent = formatStatus(data.status);+ document.getElementById('detail-mode').textContent = formatMode(data.mode);++ // Update progress percentageconst progressFill = document.getElementById('detail-progress-fill');const progressPercentage = document.getElementById('detail-progress-percentage');if (progressFill && progressPercentage) {- const progress = data.metadata.progress || 0;+ const progress = data.progress || 0;progressFill.style.width = `${progress}%`;progressPercentage.textContent = `${progress}%`;}// Update duration if available- updateResearchDuration(data.metadata);-- // Fetch the research log- const logResponse = await fetch(getApiUrl(`/api/research/${researchId}/log`));- const logData = await logResponse.json();+ const metadata = {+ created_at: data.created_at,+ completed_at: data.completed_at+ };+ updateResearchDuration(metadata);- if (logData.status === 'success') {- // Render the log entries- renderResearchLog(logData.log);+ // Render the log entries from the response directly+ if (data.log && Array.isArray(data.log)) {+ renderResearchLog(data.log, researchId);} else {- document.getElementById('research-log').innerHTML = '<div class="empty-state">No log entries available</div>';+ // Fallback to the dedicated logs endpoint if log data is missing+ try {+ const logResponse = await fetch(getApiUrl(`/api/research/${researchId}/logs`));+ const logData = await logResponse.json();++ if (logData.status === 'success' && logData.logs && Array.isArray(logData.logs)) {+ renderResearchLog(logData.logs, researchId);+ } else {+ document.getElementById('research-log').innerHTML = '<div class="empty-state">No log entries available</div>';+ }+ } catch (logError) {+ console.error('Error fetching logs:', logError);+ document.getElementById('research-log').innerHTML = '<div class="empty-state">Failed to load log entries</div>';+ }}// Update detail actions based on research status- updateDetailActions(data.metadata);+ updateDetailActions(data);// Switch to the details pageswitchPage('research-details');@@ -1514,7 +1875,7 @@ document.addEventListener('DOMContentLoaded', () => {}// Function to render log entries- function renderResearchLog(logEntries) {+ function renderResearchLog(logEntries, researchId) {const researchLog = document.getElementById('research-log');researchLog.innerHTML = '';@@ -1592,8 +1953,18 @@ document.addEventListener('DOMContentLoaded', () => {}// Connect to socket for updates if this is an in-progress research- if (research && research.status === 'in_progress') {- window.connectToResearchSocket(researchId);+ // Check for research ID and whether it's in progress by checking the database status+ if (researchId) {+ // Check research status in the database to determine if we should connect to socket+ fetch(getApiUrl(`/api/research/${researchId}`))+ .then(response => response.json())+ .then(data => {+ if (data && data.status === 'in_progress') {+ console.log(`Connecting to socket for research ${researchId} from log view`);+ window.connectToResearchSocket(researchId);+ }+ })+ .catch(err => console.error(`Error checking research status for socket connection: ${err}`));}}@@ -1724,30 +2095,49 @@ document.addEventListener('DOMContentLoaded', () => {// Function to update the research progress page from nav clickfunction updateProgressPage() {- if (!isResearchInProgress || !currentResearchId) {+ if (!currentResearchId && !window.currentResearchId) {return;}+ const researchId = currentResearchId || window.currentResearchId;+// Update the progress page- fetch(getApiUrl(`/api/research/${currentResearchId}`))+ fetch(getApiUrl(`/api/research/${researchId}`)).then(response => response.json()).then(data => {// Update the query displaydocument.getElementById('current-query').textContent = data.query;- // Update the progress bar- updateProgressUI(data.progress || 0, data.status);-- // Connect to socket for live updates- window.connectToResearchSocket(currentResearchId);-- // Check if we need to show the terminate button+ // Check status before updating UIif (data.status === 'in_progress') {+ // Update the progress bar+ updateProgressUI(data.progress || 0, data.status);++ // Check if we need to show the terminate buttonconst terminateBtn = document.getElementById('terminate-research-btn');if (terminateBtn) {terminateBtn.style.display = 'inline-flex';terminateBtn.disabled = false;}++ // Only connect to socket for in-progress research+ window.connectToResearchSocket(researchId);+ // Only start polling for in-progress research+ if (!pollingInterval) {+ pollResearchStatus(researchId);+ }+ }+ else if (data.status === 'suspended' || data.status === 'failed') {+ // Update UI for terminated research+ updateTerminationUIState('suspended', data.error || `Research was ${data.status}`);+ // Update progress bar+ document.getElementById('progress-fill').style.width = `${data.progress || 0}%`;+ document.getElementById('progress-percentage').textContent = `${data.progress || 0}%`;+ }+ else {+ // Just update progress for completed research+ document.getElementById('progress-fill').style.width = `${data.progress || 0}%`;+ document.getElementById('progress-percentage').textContent = `${data.progress || 0}%`;}}).catch(error => {@@ -2663,8 +3053,12 @@ document.addEventListener('DOMContentLoaded', () => {logPanelToggle.classList.add('collapsed');// Direct DOM manipulation for better toggle performance- logPanelToggle.onclick = function() {+ logPanelToggle.onclick = function(event) {console.log('Toggle button clicked');+ // Prevent default behavior and stop event propagation+ event.preventDefault();+ event.stopPropagation();+if (logPanelContent.classList.contains('collapsed')) {logPanelContent.classList.remove('collapsed');logPanelToggle.classList.remove('collapsed');@@ -2920,7 +3314,7 @@ document.addEventListener('DOMContentLoaded', () => {}// Function to add a log entry to the console with reduced deduplication time window- function addConsoleLog(message, type = 'info') {+ function addConsoleLog(message, type = 'info', metadata = null) {// Skip if identical to the last message to prevent duplicationconst currentTime = new Date().getTime();if (message === lastLogMessage && lastLogTimestamp && currentTime - lastLogTimestamp < 300) {@@ -2971,9 +3365,49 @@ document.addEventListener('DOMContentLoaded', () => {badgeEl.textContent = validType.toUpperCase();}+ // Process message to add search engine info if it's a search query+ let displayMessage = message;++ // Check if this is a search query message+ if (message && typeof message === 'string' && message.startsWith('Searching for:')) {+ // Determine search engine - add SearXNG by default since that's what we see in logs+ let searchEngine = 'SearXNG';++ // Check metadata if available for any search engine info+ if (metadata) {+ // If engine is directly specified+ if (metadata.engine) {+ searchEngine = metadata.engine;+ }+ // Try to extract engine info from phase metadata or other sources+ else if (metadata.search_engine) {+ searchEngine = metadata.search_engine;+ }+ else if (metadata.source) {+ searchEngine = metadata.source;+ }+ // Parse engine name from phase if possible+ else if (metadata.phase && metadata.phase.includes('searxng')) {+ searchEngine = 'SearXNG';+ }+ else if (metadata.phase && metadata.phase.includes('google')) {+ searchEngine = 'Google';+ }+ else if (metadata.phase && metadata.phase.includes('bing')) {+ searchEngine = 'Bing';+ }+ else if (metadata.phase && metadata.phase.includes('duckduckgo')) {+ searchEngine = 'DuckDuckGo';+ }+ }++ // Append search engine info to message+ displayMessage = `${message} [Engine: ${searchEngine}]`;+ }+// Set the messageconst messageEl = logEntry.querySelector('.log-message');- if (messageEl) messageEl.textContent = message;+ if (messageEl) messageEl.textContent = displayMessage;// Add the log entry to the containerconsoleContainer.appendChild(logEntry);@@ -2982,7 +3416,7 @@ document.addEventListener('DOMContentLoaded', () => {consoleLogEntries.push({element: logEntry,type: validType,- message: message,+ message: displayMessage,timestamp: timestamp});@@ -3007,7 +3441,7 @@ document.addEventListener('DOMContentLoaded', () => {consoleContainer.scrollTop = consoleContainer.scrollHeight;}, 0);- console.log(`Added console log (${validType}): ${message}`);+ console.log(`Added console log (${validType}): ${displayMessage}`);return logEntry;}@@ -3056,4 +3490,92 @@ document.addEventListener('DOMContentLoaded', () => {}});}++ // Function to check research status before polling+ async function checkResearchStatusBeforePolling(researchId) {+ try {+ // Get the current status from the server+ const response = await fetch(getApiUrl(`/api/research/${researchId}`));+ const data = await response.json();++ // If research is in_progress, start polling normally+ if (data.status === 'in_progress') {+ console.log(`Research ${researchId} is in progress, starting polling`);+ pollResearchStatus(researchId);+ window.connectToResearchSocket(researchId);+ return true;+ }+ // If terminated or complete, update UI but don't start polling+ else if (data.status === 'suspended' || data.status === 'failed') {+ console.log(`Research ${researchId} is ${data.status}, not starting polling`);+ updateTerminationUIState('suspended', `Research was ${data.status}`);+ return false;+ }+ // If completed, don't start polling+ else if (data.status === 'completed') {+ console.log(`Research ${researchId} is completed, not starting polling`);+ return false;+ }+ } catch (error) {+ console.error(`Error checking research status: ${error}`);+ return false;+ }++ return false;+ }++ // Function to reset research state before starting a new research+ function resetResearchState() {+ // Clean up any existing research resources+ window.cleanupResearchResources();++ // Clear any previous polling intervals+ clearPollingInterval();++ // Reset research flags+ isResearchInProgress = false;+ currentResearchId = null;+ window.currentResearchId = null;++ // Clear console logs+ clearConsoleLogs();++ // Reset any UI elements+ const errorMessage = document.getElementById('error-message');+ if (errorMessage) {+ errorMessage.style.display = 'none';+ errorMessage.textContent = '';+ }++ // Hide the try again button if visible+ const tryAgainBtn = document.getElementById('try-again-btn');+ if (tryAgainBtn) {+ tryAgainBtn.style.display = 'none';+ }++ // Reset progress bar+ const progressFill = document.getElementById('progress-fill');+ if (progressFill) {+ progressFill.style.width = '0%';+ }++ // Reset progress percentage+ const progressPercentage = document.getElementById('progress-percentage');+ if (progressPercentage) {+ progressPercentage.textContent = '0%';+ }++ // Reset progress status+ const progressStatus = document.getElementById('progress-status');+ if (progressStatus) {+ progressStatus.textContent = 'Initializing research process...';+ progressStatus.className = 'progress-status';+ }++ // Reset the Start Research button+ resetStartResearchButton();++ // Add initial log entry+ addConsoleLog('Preparing to start new research', 'info');+ }});\ No newline at end of file