Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Fix circular import issues in package structure

hashedvikingfab16286f300
diff --git a/app.py b/app.py
new file mode 100644
index 0000000000..f8320bbf2e
--- /dev/null
+++ b/app.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+"""
+Main entry point for the Local Deep Research application.
+"""
+
+import sys
+import os
+
+# Add the src directory to the Python path
+src_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src')
+if src_path not in sys.path:
+ sys.path.insert(0, src_path)
+
+from src.local_deep_research.web.app import main
+
+if __name__ == "__main__":
+ main()
\ No newline at end of file
diff --git a/src/local_deep_research/__init__.py b/src/local_deep_research/__init__.py
index 0d4e11e064..a8fb6fc4e2 100644
--- a/src/local_deep_research/__init__.py
+++ b/src/local_deep_research/__init__.py
@@ -7,33 +7,57 @@ and multiple search engines integration.
__version__ = "0.1.0"
-# Import API functions
-from .api import (
- analyze_documents,
- generate_report,
- get_available_collections,
- get_available_search_engines,
- quick_summary,
-)
-from .config.llm_config import get_llm
-from .config.search_config import get_search
-from .report_generator import IntegratedReportGenerator
-
-# Import main components
-from .search_system import AdvancedSearchSystem
-
# Initialize configuration on module import
from .utilties.setup_utils import setup_user_directories
+# Create reexport functions to avoid circular imports
+def get_llm(*args, **kwargs):
+ """Get a language model instance based on settings."""
+ from .config.llm_config import get_llm as _get_llm
+ return _get_llm(*args, **kwargs)
+
+def get_search(*args, **kwargs):
+ """Get a search engine instance based on settings."""
+ from .config.search_config import get_search as _get_search
+ return _get_search(*args, **kwargs)
+
+def get_report_generator(*args, **kwargs):
+ """Get a report generator instance."""
+ from .report_generator import IntegratedReportGenerator
+ return IntegratedReportGenerator(*args, **kwargs)
+
+# Define a function to import AdvancedSearchSystem to avoid circular imports
+def get_advanced_search_system(*args, **kwargs):
+ """Get an AdvancedSearchSystem instance."""
+ from .search_system import AdvancedSearchSystem
+ return AdvancedSearchSystem(*args, **kwargs)
+
+# Lazy import API functions to avoid circular imports
+def _import_api():
+ from .api import (
+ analyze_documents,
+ generate_report,
+ get_available_collections,
+ get_available_search_engines,
+ quick_summary,
+ )
+ return {
+ "analyze_documents": analyze_documents,
+ "generate_report": generate_report,
+ "get_available_collections": get_available_collections,
+ "get_available_search_engines": get_available_search_engines,
+ "quick_summary": quick_summary,
+ }
+
# Export it
__all__ = [
- "AdvancedSearchSystem",
- "IntegratedReportGenerator",
+ "get_advanced_search_system",
"get_llm",
"get_search",
- "quick_summary",
- "generate_report",
- "analyze_documents",
- "get_available_search_engines",
- "get_available_collections",
+ "get_report_generator",
]
+
+# Add API functions to __all__ dynamically
+for name, func in _import_api().items():
+ globals()[name] = func
+ __all__.append(name)
diff --git a/src/local_deep_research/__main__.py b/src/local_deep_research/__main__.py
new file mode 100644
index 0000000000..10520ce408
--- /dev/null
+++ b/src/local_deep_research/__main__.py
@@ -0,0 +1,13 @@
+"""
+Main entry point when running the package with `python -m local_deep_research`.
+This avoids circular imports by directly importing the main function after
+the package is fully loaded.
+"""
+
+def main():
+ # Only import main after the whole package has been initialized
+ from .main import main as main_func
+ main_func()
+
+if __name__ == "__main__":
+ main()
\ No newline at end of file
diff --git a/src/local_deep_research/api/research_functions.py b/src/local_deep_research/api/research_functions.py
index d6f53e630f..01575516d3 100644
--- a/src/local_deep_research/api/research_functions.py
+++ b/src/local_deep_research/api/research_functions.py
@@ -11,7 +11,7 @@ import toml
from ..config.llm_config import get_llm
from ..config.search_config import get_search
-from ..report_generator import IntegratedReportGenerator
+from .. import get_report_generator # Use the lazy import function
from ..search_system import AdvancedSearchSystem
from ..utilties.search_utilities import remove_think_tags
@@ -178,7 +178,7 @@ def generate_report(
initial_findings = system.analyze_topic(query)
# Generate the structured report
- report_generator = IntegratedReportGenerator(
+ report_generator = get_report_generator(
searches_per_section=searches_per_section
)
report_generator.model = llm # Ensure the model is set on the report generator too
diff --git a/src/local_deep_research/main.py b/src/local_deep_research/main.py
index 43d0184bc7..e9f1d5b998 100644
--- a/src/local_deep_research/main.py
+++ b/src/local_deep_research/main.py
@@ -1,8 +1,8 @@
from typing import Dict
from .config.config_files import settings
-from .report_generator import IntegratedReportGenerator
-from .search_system import AdvancedSearchSystem
+# from .report_generator import IntegratedReportGenerator
+from . import get_report_generator, get_advanced_search_system
def print_report(report: Dict):
@@ -28,7 +28,12 @@ def print_report(report: Dict):
print("\nReport has been saved to report.md")
-report_generator = IntegratedReportGenerator()
+# Create the report generator lazily to avoid circular imports
+def get_report_generator_instance():
+ return get_report_generator()
+
+# report_generator = IntegratedReportGenerator()
+report_generator = None # Will be initialized when needed
def main():
@@ -48,7 +53,7 @@ def main():
logger.info("Initializing configuration...")
setup_user_directories()
- system = AdvancedSearchSystem()
+ system = get_advanced_search_system()
print("Welcome to the Advanced Research System")
print("Type 'quit' to exit")
@@ -97,6 +102,11 @@ def main():
else:
# Full Report
+ # Initialize report_generator if not already done
+ global report_generator
+ if report_generator is None:
+ report_generator = get_report_generator()
+
final_report = report_generator.generate_report(results, query)
print("\n=== RESEARCH REPORT ===")
print_report(final_report)
diff --git a/src/local_deep_research/report_generator.py b/src/local_deep_research/report_generator.py
index 3847a220c1..928fd1c610 100644
--- a/src/local_deep_research/report_generator.py
+++ b/src/local_deep_research/report_generator.py
@@ -1,8 +1,11 @@
from typing import Dict, List
-from . import get_llm, utilties
-from .search_system import AdvancedSearchSystem
+# Fix circular import by importing directly from source modules
+from .config.llm_config import get_llm
+# from . import utilties
from .utilties import search_utilities
+from .search_system import AdvancedSearchSystem
+import importlib
class IntegratedReportGenerator:
@@ -185,6 +188,8 @@ class IntegratedReportGenerator:
report_parts.append("")
# Format links from search system
+ # Get utilties module dynamically to avoid circular imports
+ utilties = importlib.import_module("local_deep_research.utilties")
formatted_all_links = utilties.search_utilities.format_links(
links=self.search_system.all_links_of_system
)
diff --git a/src/local_deep_research/search_system.py b/src/local_deep_research/search_system.py
index 6885913c6a..198a9205fe 100644
--- a/src/local_deep_research/search_system.py
+++ b/src/local_deep_research/search_system.py
@@ -3,7 +3,8 @@ import os
from datetime import datetime
from typing import Callable, Dict, List
-from . import get_llm
+# Fix circular import - import directly from the config module, not from parent package
+from .config.llm_config import get_llm
from .citation_handler import CitationHandler
from .config.config_files import get_config_dir, settings
from .config.search_config import get_search
diff --git a/src/local_deep_research/web/app.py b/src/local_deep_research/web/app.py
index 11cf96b9e3..11a659a693 100644
--- a/src/local_deep_research/web/app.py
+++ b/src/local_deep_research/web/app.py
@@ -2,7 +2,6 @@ import os
import logging
from dateutil import parser
from src.local_deep_research.web.app_factory import create_app
-from src.local_deep_research.config.config_files import SEARCH_ENGINES_FILE, get_config_dir, settings
# Initialize logger
logger = logging.getLogger(__name__)
@@ -10,1164 +9,14 @@ logger = logging.getLogger(__name__)
# Create the Flask app and SocketIO instance
app, socketio = create_app()
-# ... rest of the code ...
-
-
-# Function to clean up resources for a completed research
-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 research
- if research_id in active_research:
- del active_research[research_id]
-
- # Remove from termination flags
- if research_id in termination_flags:
- del termination_flags[research_id]
-
- # Send a final message to any remaining subscribers with explicit status
- if research_id in socket_subscriptions and socket_subscriptions[research_id]:
- # 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 {current_status} socket message for research {research_id}"
- )
- # Use emit to all, not just subscribers
- socketio.emit(f"research_progress_{research_id}", final_message)
-
- # Also emit to specific subscribers
- for sid in socket_subscriptions[research_id]:
- try:
- socketio.emit(
- f"research_progress_{research_id}", final_message, room=sid
- )
- except Exception as sub_err:
- print(f"Error emitting to subscriber {sid}: {str(sub_err)}")
- except Exception as e:
- print(f"Error sending final cleanup message: {e}")
-
- # Don't immediately remove subscriptions - let clients disconnect naturally
-
-
-def run_research_process(research_id, query, mode):
- """Run the research process in the background for a given research ID"""
- try:
- # 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 callback
- def 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
- adjusted_progress = progress_percent
- if mode == "detailed" and metadata.get("phase") == "output_generation":
- # For detailed mode, we need to adjust the progress range
- # because detailed reports take longer after the search phase
- adjusted_progress = min(80, progress_percent)
- elif mode == "detailed" and metadata.get("phase") == "report_generation":
- # Scale the progress from 80% to 95% for the report generation phase
- # Map progress_percent values (0-100%) to the (80-95%) range
- if progress_percent is not None:
- normalized = progress_percent / 100
- adjusted_progress = 80 + (normalized * 15)
- elif mode == "quick" and metadata.get("phase") == "output_generation":
- # For quick mode, ensure we're at least at 85% during output generation
- adjusted_progress = max(85, progress_percent)
- # Map any further progress within output_generation to 85-95% range
- if progress_percent is not None and progress_percent > 0:
- normalized = progress_percent / 100
- adjusted_progress = 85 + (normalized * 10)
-
- # Don't let progress go backwards
- if research_id in active_research and adjusted_progress is not None:
- current_progress = active_research[research_id].get("progress", 0)
- adjusted_progress = max(current_progress, adjusted_progress)
-
- log_entry = {
- "time": timestamp,
- "message": message,
- "progress": adjusted_progress,
- "metadata": metadata,
- }
-
- # Check if termination was requested
- 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")
-
- # Update active research record
- if research_id in active_research:
- active_research[research_id]["log"].append(log_entry)
- if adjusted_progress is not None:
- active_research[research_id]["progress"] = adjusted_progress
-
- # 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"]:
- 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(
- "UPDATE research_history SET progress = ? WHERE id = ?",
- (adjusted_progress, research_id),
- )
-
- # 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 Exception:
- 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 event
- try:
- # Basic event data
- event_data = {"message": message, "progress": adjusted_progress}
-
- # 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 channel
- socketio.emit(f"research_progress_{research_id}", event_data)
-
- if research_id in socket_subscriptions:
- for sid in socket_subscriptions[research_id]:
- try:
- socketio.emit(
- f"research_progress_{research_id}",
- event_data,
- room=sid,
- )
- 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)}")
-
- # 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
- progress_callback("Starting research process", 5, {"phase": "init"})
-
- try:
- results = system.analyze_topic(query)
- if mode == "quick":
- progress_callback(
- "Search complete, preparing to generate summary...",
- 85,
- {"phase": "output_generation"},
- )
- else:
- progress_callback(
- "Search complete, generating output",
- 80,
- {"phase": "output_generation"},
- )
- except Exception as search_error:
- # Better handling of specific search errors
- error_message = str(search_error)
- error_type = "unknown"
-
- # Extract error details for common issues
- if "status code: 503" in error_message:
- error_message = "Ollama AI service is unavailable (HTTP 503). Please check that Ollama is running properly on your system."
- error_type = "ollama_unavailable"
- elif "status code: 404" in error_message:
- error_message = "Ollama model not found (HTTP 404). Please check that you have pulled the required model."
- error_type = "model_not_found"
- elif "status code:" in error_message:
- # Extract the status code for other HTTP errors
- status_code = error_message.split("status code:")[1].strip()
- error_message = f"API request failed with status code {status_code}. Please check your configuration."
- error_type = "api_error"
- elif "connection" in error_message.lower():
- error_message = "Connection error. Please check that your LLM service (Ollama/API) is running and accessible."
- error_type = "connection_error"
-
- # Raise with improved error message
- raise Exception(f"{error_message} (Error type: {error_type})")
-
- # Generate output based on mode
- if mode == "quick":
- # Quick Summary
- if results.get("findings") or results.get("formatted_findings"):
-
- raw_formatted_findings = results["formatted_findings"]
- logger.info(
- f"Found formatted_findings of length: {len(str(raw_formatted_findings))}"
- )
-
- try:
- clean_markdown = raw_formatted_findings
- # ADDED CODE: Convert debug output to clean markdown
- # clean_markdown = convert_debug_to_markdown(raw_formatted_findings, query)
- print(
- f"Successfully converted to clean markdown of length: {len(clean_markdown)}"
- )
-
- # First send a progress update for generating the summary
- progress_callback(
- "Generating clean summary from research data...",
- 90,
- {"phase": "output_generation"},
- )
-
- # Save as markdown file
- output_dir = "research_outputs"
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
-
- safe_query = "".join(
- x for x in query if x.isalnum() or x in [" ", "-", "_"]
- )[:50]
- safe_query = safe_query.replace(" ", "_").lower()
- report_path = os.path.join(
- output_dir, f"quick_summary_{safe_query}.md"
- )
-
- # Send progress update for writing to file
- progress_callback(
- "Writing research report to file...",
- 95,
- {"phase": "report_complete"},
- )
-
- print(f"Writing report to: {report_path}")
- with open(report_path, "w", encoding="utf-8") as f:
- f.write("# Quick Research Summary\n\n")
- f.write(f"Query: {query}\n\n")
- f.write(
- clean_markdown
- ) # Use clean markdown instead of raw findings
- f.write("\n\n## Research Metrics\n")
- f.write(f"- Search Iterations: {results['iterations']}\n")
- f.write(f"- Generated at: {datetime.utcnow().isoformat()}\n")
-
- # Update database
- metadata = {
- "iterations": results["iterations"],
- "generated_at": datetime.utcnow().isoformat(),
- }
-
- # Calculate duration in seconds - using UTC consistently
- now = datetime.utcnow()
- completed_at = now.isoformat()
-
- print(f"Updating database for research_id: {research_id}")
- # Get the start time from the database
- conn = sqlite3.connect(DB_PATH)
- cursor = conn.cursor()
- cursor.execute(
- "SELECT created_at FROM research_history WHERE id = ?",
- (research_id,),
- )
- result = cursor.fetchone()
-
- # Use the helper function for consistent duration calculation
- duration_seconds = calculate_duration(result[0])
-
- # Update the record
- cursor.execute(
- "UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ?, report_path = ?, metadata = ? WHERE id = ?",
- (
- "completed",
- completed_at,
- duration_seconds,
- report_path,
- json.dumps(metadata),
- research_id,
- ),
- )
- conn.commit()
- conn.close()
- print(
- f"Database updated successfully for research_id: {research_id}"
- )
-
- # Send the final completion message
- progress_callback(
- "Research completed successfully",
- 100,
- {"phase": "complete", "report_path": report_path},
- )
-
- # Clean up resources
- print(f"Cleaning up resources for research_id: {research_id}")
- cleanup_research_resources(research_id)
- print(f"Resources cleaned up for research_id: {research_id}")
- except Exception as inner_e:
- print(f"Error during quick summary generation: {str(inner_e)}")
- print(traceback.format_exc())
- raise Exception(f"Error generating quick summary: {str(inner_e)}")
- else:
- raise Exception(
- "No research findings were generated. Please try again."
- )
- else:
- # Full Report
- progress_callback(
- "Generating detailed report...", 85, {"phase": "report_generation"}
- )
- report_generator = IntegratedReportGenerator()
- final_report = report_generator.generate_report(results, query)
- progress_callback(
- "Report generation complete", 95, {"phase": "report_complete"}
- )
-
- # Save as markdown file
- output_dir = "research_outputs"
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
-
- safe_query = "".join(
- x for x in query if x.isalnum() or x in [" ", "-", "_"]
- )[:50]
- safe_query = safe_query.replace(" ", "_").lower()
- report_path = os.path.join(output_dir, f"detailed_report_{safe_query}.md")
-
- with open(report_path, "w", encoding="utf-8") as f:
- f.write(final_report["content"])
-
- # Update database
- metadata = final_report["metadata"]
- metadata["iterations"] = results["iterations"]
-
- # Calculate duration in seconds - using UTC consistently
- now = datetime.utcnow()
- completed_at = now.isoformat()
-
- # Get the start time from the database
- conn = sqlite3.connect(DB_PATH)
- cursor = conn.cursor()
- cursor.execute(
- "SELECT created_at FROM research_history WHERE id = ?", (research_id,)
- )
- result = cursor.fetchone()
-
- # Use the helper function for consistent duration calculation
- duration_seconds = calculate_duration(result[0])
-
- cursor.execute(
- "UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ?, report_path = ?, metadata = ? WHERE id = ?",
- (
- "completed",
- completed_at,
- duration_seconds,
- report_path,
- json.dumps(metadata),
- research_id,
- ),
- )
- conn.commit()
- conn.close()
-
- progress_callback(
- "Research completed successfully",
- 100,
- {"phase": "complete", "report_path": report_path},
- )
-
- # Clean up - moved to a separate function for reuse
- cleanup_research_resources(research_id)
-
- except Exception as e:
- # Handle error
- error_message = f"Research failed: {str(e)}"
- print(f"Research error: {error_message}")
- try:
- # Check for common Ollama error patterns in the exception and provide more user-friendly errors
- user_friendly_error = str(e)
- error_context = {}
-
- if "Error type: ollama_unavailable" in user_friendly_error:
- user_friendly_error = "Ollama AI service is unavailable. Please check that Ollama is running properly on your system."
- error_context = {
- "solution": "Start Ollama with 'ollama serve' or check if it's installed correctly."
- }
- elif "Error type: model_not_found" in user_friendly_error:
- user_friendly_error = (
- "Required Ollama model not found. Please pull the model first."
- )
- error_context = {
- "solution": "Run 'ollama pull mistral' to download the required model."
- }
- elif "Error type: connection_error" in user_friendly_error:
- user_friendly_error = "Connection error with LLM service. Please check that your AI service is running."
- error_context = {
- "solution": "Ensure Ollama or your API service is running and accessible."
- }
- elif "Error type: api_error" in user_friendly_error:
- # Keep the original error message as it's already improved
- error_context = {"solution": "Check API configuration and credentials."}
-
- # Update metadata with more context about the error
- metadata = {"phase": "error", "error": user_friendly_error}
- if error_context:
- metadata.update(error_context)
-
- progress_callback(user_friendly_error, None, metadata)
-
- conn = sqlite3.connect(DB_PATH)
- cursor = conn.cursor()
-
- # If termination was requested, mark as suspended instead of failed
- status = (
- "suspended"
- if (research_id in termination_flags and termination_flags[research_id])
- else "failed"
- )
- message = (
- "Research was terminated by user"
- if status == "suspended"
- else user_friendly_error
- )
-
- # Calculate duration up to termination point - using UTC consistently
- now = datetime.utcnow()
- completed_at = now.isoformat()
-
- # Get the start time from the database
- duration_seconds = None
- cursor.execute(
- "SELECT created_at FROM research_history WHERE id = ?", (research_id,)
- )
- result = cursor.fetchone()
-
- # Use the helper function for consistent duration calculation
- if result and result[0]:
- duration_seconds = calculate_duration(result[0])
-
- cursor.execute(
- "UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ?, metadata = ? WHERE id = ?",
- (
- status,
- completed_at,
- duration_seconds,
- json.dumps(metadata),
- research_id,
- ),
- )
- conn.commit()
- conn.close()
-
- try:
- socketio.emit(
- f"research_progress_{research_id}",
- {"status": status, "error": message},
- )
-
- # Also notify specific subscribers
- if (
- research_id in socket_subscriptions
- and socket_subscriptions[research_id]
- ):
- for sid in socket_subscriptions[research_id]:
- try:
- socketio.emit(
- f"research_progress_{research_id}",
- {"status": status, "error": message},
- room=sid,
- )
- except Exception as sub_err:
- print(f"Error emitting to subscriber {sid}: {str(sub_err)}")
-
- except Exception as socket_error:
- print(f"Failed to emit error via socket: {str(socket_error)}")
- except Exception as inner_e:
- print(f"Error in error handler: {str(inner_e)}")
-
- # Clean up resources - moved to a separate function for reuse
- cleanup_research_resources(research_id)
-
-
-@research_bp.route("/api/research/<int:research_id>/terminate", methods=["POST"])
-def terminate_research(research_id):
- """Terminate an in-progress research process"""
-
- # Check if the research exists and is in progress
- conn = sqlite3.connect(DB_PATH)
- cursor = conn.cursor()
- cursor.execute("SELECT status FROM research_history WHERE id = ?", (research_id,))
- result = cursor.fetchone()
-
- if not result:
- conn.close()
- return jsonify({"status": "error", "message": "Research not found"}), 404
-
- status = result[0]
-
- # If it's not in progress, return an error
- if status != "in_progress":
- conn.close()
- return (
- jsonify({"status": "error", "message": "Research is not in progress"}),
- 400,
- )
-
- # Check if it's in the active_research dict
- if research_id not in active_research:
- # Update the status in the database
- cursor.execute(
- "UPDATE research_history SET status = ? WHERE id = ?",
- ("suspended", research_id),
- )
- conn.commit()
- conn.close()
- return jsonify({"status": "success", "message": "Research terminated"})
-
- # Set the termination flag
- termination_flags[research_id] = True
-
- # Log the termination request - using UTC timestamp
- timestamp = datetime.utcnow().isoformat()
- termination_message = "Research termination requested by user"
- current_progress = active_research[research_id]["progress"]
-
- # Create log entry
- log_entry = {
- "time": timestamp,
- "message": termination_message,
- "progress": current_progress,
- "metadata": {"phase": "termination"},
- }
-
- # Add to in-memory log
- active_research[research_id]["log"].append(log_entry)
-
- # 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:
- try:
- current_log = json.loads(log_result[0])
- except Exception:
- current_log = []
- current_log.append(log_entry)
- cursor.execute(
- "UPDATE research_history SET progress_log = ? WHERE 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 request
- try:
- event_data = {
- "status": "suspended", # Changed from 'terminating' to 'suspended'
- "message": "Research was suspended by user request",
- }
-
- socketio.emit(f"research_progress_{research_id}", event_data)
-
- if research_id in socket_subscriptions and socket_subscriptions[research_id]:
- for sid in socket_subscriptions[research_id]:
- try:
- socketio.emit(
- f"research_progress_{research_id}", event_data, room=sid
- )
- except Exception as err:
- print(f"Error emitting to subscriber {sid}: {str(err)}")
-
- except Exception as socket_error:
- print(f"Socket emit error (non-critical): {str(socket_error)}")
-
- return jsonify({"status": "success", "message": "Research termination requested"})
-
-
-@research_bp.route("/api/research/<int:research_id>/delete", methods=["DELETE"])
-def delete_research(research_id):
- """Delete a research record"""
- conn = sqlite3.connect(DB_PATH)
- cursor = conn.cursor()
-
- # First check if the research exists and is not in progress
- cursor.execute(
- "SELECT status, report_path FROM research_history WHERE id = ?", (research_id,)
- )
- result = cursor.fetchone()
-
- if not result:
- conn.close()
- return jsonify({"status": "error", "message": "Research not found"}), 404
-
- status, report_path = result
-
- # Don't allow deleting research in progress
- if status == "in_progress" and research_id in active_research:
- conn.close()
- return (
- jsonify(
- {
- "status": "error",
- "message": "Cannot delete research that is in progress",
- }
- ),
- 400,
- )
-
- # Delete report file if it exists
- if report_path and os.path.exists(report_path):
- try:
- os.remove(report_path)
- except Exception as e:
- print(f"Error removing report file: {str(e)}")
-
- # Delete the database record
- cursor.execute("DELETE FROM research_history WHERE id = ?", (research_id,))
- conn.commit()
- conn.close()
-
- return jsonify({"status": "success"})
-
-
-@research_bp.route("/settings", methods=["GET"])
-def settings_page():
- """Main settings dashboard with links to specialized config pages"""
- return render_template("settings_dashboard.html")
-
-
-@research_bp.route("/settings/main", methods=["GET"])
-def main_config_page():
- """Edit main configuration with search parameters"""
- return render_template("main_config.html", main_file_path=MAIN_CONFIG_FILE)
-
-
-@research_bp.route("/settings/collections", methods=["GET"])
-def collections_config_page():
- """Edit local collections configuration using raw file editor"""
- return render_template(
- "collections_config.html", collections_file_path=LOCAL_COLLECTIONS_FILE
- )
-
-
-@research_bp.route("/settings/api_keys", methods=["GET"])
-def api_keys_config_page():
- """Edit API keys configuration"""
- # Get the secrets file path
- secrets_file = CONFIG_DIR / ".secrets.toml"
-
- return render_template("api_keys_config.html", secrets_file_path=secrets_file)
-
-
-# Add a new route for search engines configuration page
-@research_bp.route("/settings/search_engines", methods=["GET"])
-def search_engines_config_page():
- """Edit search engines configuration using raw file editor"""
- # Read the current config file
- raw_config = ""
- try:
- with open(SEARCH_ENGINES_FILE, "r") as f:
- raw_config = f.read()
- except Exception as e:
- flash(f"Error reading search engines configuration: {str(e)}", "error")
- raw_config = "# Error reading configuration file"
-
- # Get list of engine names for display
- engine_names = []
- try:
- from ..web_search_engines.search_engines_config import SEARCH_ENGINES
-
- engine_names = list(SEARCH_ENGINES.keys())
- engine_names.sort() # Alphabetical order
- except Exception as e:
- logger.error(f"Error getting engine names: {e}")
-
- return render_template(
- "search_engines_config.html",
- search_engines_file_path=SEARCH_ENGINES_FILE,
- raw_config=raw_config,
- engine_names=engine_names,
- )
-
-
-# Add a route to save search engines configuration
-@research_bp.route("/api/save_search_engines_config", methods=["POST"])
-def save_search_engines_config():
- try:
- data = request.get_json()
- raw_config = data.get("raw_config", "")
-
- # Validate TOML syntax
- try:
- toml.loads(raw_config)
- except toml.TomlDecodeError as e:
- return jsonify({"success": False, "error": f"TOML syntax error: {str(e)}"})
-
- # Ensure directory exists
- os.makedirs(os.path.dirname(SEARCH_ENGINES_FILE), exist_ok=True)
-
- # Create a backup first
- backup_path = f"{SEARCH_ENGINES_FILE}.bak"
- if os.path.exists(SEARCH_ENGINES_FILE):
- import shutil
-
- shutil.copy2(SEARCH_ENGINES_FILE, backup_path)
-
- # Write new config
- with open(SEARCH_ENGINES_FILE, "w") as f:
- f.write(raw_config)
-
- return jsonify({"success": True})
- except Exception as e:
- return jsonify({"success": False, "error": str(e)})
-
-
-# API endpoint to save raw collections config
-@research_bp.route("/api/save_collections_config", methods=["POST"])
-def save_collections_config():
- try:
- data = request.get_json()
- raw_config = data.get("raw_config", "")
-
- # Validate TOML syntax
- try:
- toml.loads(raw_config)
- except toml.TomlDecodeError as e:
- return jsonify({"success": False, "error": f"TOML syntax error: {str(e)}"})
-
- # Ensure directory exists
- os.makedirs(os.path.dirname(LOCAL_COLLECTIONS_FILE), exist_ok=True)
-
- # Create a backup first
- backup_path = f"{LOCAL_COLLECTIONS_FILE}.bak"
- if os.path.exists(LOCAL_COLLECTIONS_FILE):
- import shutil
-
- shutil.copy2(LOCAL_COLLECTIONS_FILE, backup_path)
-
- # Write new config
- with open(LOCAL_COLLECTIONS_FILE, "w") as f:
- f.write(raw_config)
-
- # Also trigger a reload in the collections system
- try:
- # TODO (djpetti) Fix collection reloading.
- load_local_collections(reload=True) # noqa: F821
- except Exception as reload_error:
- return jsonify(
- {
- "success": True,
- "warning": f"Config saved, but error reloading: {str(reload_error)}",
- }
- )
-
- return jsonify({"success": True})
- except Exception as e:
- return jsonify({"success": False, "error": str(e)})
-
-
-# API endpoint to save raw main config
-@research_bp.route("/api/save_main_config", methods=["POST"])
-def save_raw_main_config():
- try:
- data = request.get_json()
- raw_config = data.get("raw_config", "")
-
- # Validate TOML syntax
- try:
- toml.loads(raw_config)
- except toml.TomlDecodeError as e:
- return jsonify({"success": False, "error": f"TOML syntax error: {str(e)}"})
-
- # Ensure directory exists
- os.makedirs(os.path.dirname(MAIN_CONFIG_FILE), exist_ok=True)
-
- # Create a backup first
- backup_path = f"{MAIN_CONFIG_FILE}.bak"
- if os.path.exists(MAIN_CONFIG_FILE):
- import shutil
-
- shutil.copy2(MAIN_CONFIG_FILE, backup_path)
-
- # Write new config
- with open(MAIN_CONFIG_FILE, "w") as f:
- f.write(raw_config)
-
- return jsonify({"success": True})
- except Exception as e:
- return jsonify({"success": False, "error": str(e)})
-
-
-@research_bp.route("/raw_config")
-def get_raw_config():
- """Return the raw configuration file content"""
- try:
- # Determine which config file to load based on a query parameter
- config_type = request.args.get("type", "main")
-
- if config_type == "main":
- config_path = os.path.join(app.config["CONFIG_DIR"], "config.toml")
- with open(config_path, "r") as f:
- return f.read()
- elif config_type == "llm":
- config_path = os.path.join(app.config["CONFIG_DIR"], "llm_config.py")
- with open(config_path, "r") as f:
- return f.read()
- elif config_type == "collections":
- config_path = os.path.join(app.config["CONFIG_DIR"], "collections.toml")
- with open(config_path, "r") as f:
- return f.read()
- else:
- return "Unknown configuration type", 400
- except Exception as e:
- return str(e), 500
-
-
-@research_bp.route("/open_file_location", methods=["POST"])
-def open_file_location():
- file_path = request.form.get("file_path")
-
- if not file_path:
- flash("No file path provided", "error")
- return redirect(url_for("research.settings_page"))
-
- # Get the directory containing the file
- dir_path = os.path.dirname(os.path.abspath(file_path))
-
- # Open the directory in the file explorer
- try:
- if platform.system() == "Windows":
- subprocess.Popen(f'explorer "{dir_path}"')
- elif platform.system() == "Darwin": # macOS
- subprocess.Popen(["open", dir_path])
- else: # Linux
- subprocess.Popen(["xdg-open", dir_path])
-
- flash(f"Opening folder: {dir_path}", "success")
- except Exception as e:
- flash(f"Error opening folder: {str(e)}", "error")
-
- # Redirect back to the settings page
- if "llm" in file_path:
- return redirect(url_for("research.llm_config_page"))
- elif "collections" in file_path:
- return redirect(url_for("research.collections_config_page"))
- else:
- return redirect(url_for("research.main_config_page"))
-
-
-@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 blueprint
-app.register_blueprint(research_bp)
-
-
-# Also add the static route at the app level for compatibility
[email protected]("/static/<path:path>")
-def app_serve_static(path):
- return send_from_directory(app.static_folder, path)
-
-
-# Add favicon route to prevent 404 errors
[email protected]("/favicon.ico")
-def favicon():
- return send_from_directory(
- app.static_folder, "favicon.ico", mimetype="image/x-icon"
- )
-
-
-# Add this function to app.py
-def convert_debug_to_markdown(raw_text, query):
- """
- Convert the debug-formatted text to clean markdown.
-
- Args:
- raw_text: The raw formatted findings with debug symbols
- query: Original research query
-
- Returns:
- Clean markdown formatted text
- """
- try:
- print(f"Starting markdown conversion for query: {query}")
- print(f"Raw text type: {type(raw_text)}")
-
- # Handle None or empty input
- if not raw_text:
- print("WARNING: raw_text is empty or None")
- return f"No detailed findings available for '{query}'."
-
- # If there's a "DETAILED FINDINGS:" section, extract everything after it
- if "DETAILED FINDINGS:" in raw_text:
- print("Found DETAILED FINDINGS section")
- detailed_index = raw_text.index("DETAILED FINDINGS:")
- content = raw_text[detailed_index + len("DETAILED FINDINGS:") :].strip()
- else:
- print("No DETAILED FINDINGS section found, using full text")
- content = raw_text
-
- # Remove divider lines with === symbols
- lines_before = len(content.split("\n"))
- content = "\n".join(
- [
- line
- for line in content.split("\n")
- if not line.strip().startswith("===") and not line.strip() == "=" * 80
- ]
- )
- lines_after = len(content.split("\n"))
- print(f"Removed {lines_before - lines_after} divider lines")
-
- # Remove SEARCH QUESTIONS BY ITERATION section
- if "SEARCH QUESTIONS BY ITERATION:" in content:
- print("Found SEARCH QUESTIONS BY ITERATION section")
- search_index = content.index("SEARCH QUESTIONS BY ITERATION:")
- next_major_section = -1
- for marker in ["DETAILED FINDINGS:", "COMPLETE RESEARCH:"]:
- if marker in content[search_index:]:
- marker_pos = content.index(marker, search_index)
- if next_major_section == -1 or marker_pos < next_major_section:
- next_major_section = marker_pos
-
- if next_major_section != -1:
- print(
- f"Removing section from index {search_index} to {next_major_section}"
- )
- content = content[:search_index] + content[next_major_section:]
- else:
- # If no later section, just remove everything from SEARCH QUESTIONS onwards
- print(f"Removing everything after index {search_index}")
- content = content[:search_index].strip()
-
- print(f"Final markdown length: {len(content.strip())}")
- return content.strip()
- except Exception as e:
- print(f"Error in convert_debug_to_markdown: {str(e)}")
- print(traceback.format_exc())
- # Return a basic message with the original query as fallback
- return f"# Research on {query}\n\nThere was an error formatting the research results."
-
def main():
"""
Entry point for the web application when run as a command.
This function is needed for the package's entry point to work properly.
"""
+ # Import configuration inside the function to avoid circular imports
+ from src.local_deep_research.config.config_files import get_config_dir, settings
+
# Get web server settings with defaults
port = settings.web.port
host = settings.web.host
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 70d8e3b698..39f89ed490 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -63,9 +63,6 @@ def create_app():
from .services.socket_service import set_socketio
set_socketio(socketio)
- # Register socket event handlers
- register_socket_events(socketio)
-
# Apply middleware
apply_middleware(app)
@@ -75,6 +72,9 @@ def create_app():
# Register error handlers
register_error_handlers(app)
+ # Register socket event handlers
+ register_socket_events(socketio)
+
return app, socketio
def apply_middleware(app):
@@ -124,10 +124,30 @@ def register_blueprints(app):
# Import blueprints
from .routes.research_routes import research_bp
from .routes.history_routes import history_bp
+ from .routes.settings_routes import settings_bp
# Register blueprints
app.register_blueprint(research_bp)
app.register_blueprint(history_bp, url_prefix='/research/api')
+ app.register_blueprint(settings_bp)
+
+ # Configure settings paths
+ # Import config inside the function to avoid circular dependencies
+ def configure_settings_routes():
+ try:
+ from ..config.config_files import SEARCH_ENGINES_FILE, get_config_dir
+ from .routes.settings_routes import set_config_paths
+
+ CONFIG_DIR = get_config_dir() / "config"
+ MAIN_CONFIG_FILE = CONFIG_DIR / "settings.toml"
+ LOCAL_COLLECTIONS_FILE = CONFIG_DIR / "local_collections.toml"
+
+ set_config_paths(CONFIG_DIR, SEARCH_ENGINES_FILE, MAIN_CONFIG_FILE, LOCAL_COLLECTIONS_FILE)
+ except Exception as e:
+ logger.error(f"Error configuring settings routes: {e}")
+
+ # Call this after all blueprints are registered
+ configure_settings_routes()
# Add root route redirect
@app.route("/")
@@ -160,107 +180,27 @@ def register_error_handlers(app):
def register_socket_events(socketio):
"""Register Socket.IO event handlers."""
+ from .services.socket_service import handle_connect, handle_disconnect, handle_subscribe, handle_socket_error, handle_default_error
+ from .routes.research_routes import get_globals
+
@socketio.on("connect")
- def handle_connect():
- print(f"Client connected: {request.sid}")
+ def on_connect():
+ handle_connect(request)
@socketio.on("disconnect")
- def handle_disconnect(sid=None):
- try:
- # Use the sid parameter if provided, otherwise fall back to request.sid
- client_sid = sid if sid else request.sid
- print(f"Client disconnected: {client_sid}")
- # Import to avoid circular imports
- from .routes.research_routes import get_globals
- globals_dict = get_globals()
- socket_subscriptions = globals_dict['socket_subscriptions']
-
- # Clean up subscriptions for this client
- for research_id, subscribers in list(socket_subscriptions.items()):
- if client_sid in subscribers:
- subscribers.remove(client_sid)
- if not subscribers:
- socket_subscriptions.pop(research_id, None)
- print(f"Removed empty subscription for research {research_id}")
- except Exception as e:
- print(f"Error handling disconnect: {e}")
-
- @socketio.on("join")
- def handle_join(data):
- from .routes.research_routes import get_globals
- globals_dict = get_globals()
- active_research = globals_dict['active_research']
- socket_subscriptions = globals_dict['socket_subscriptions']
-
- research_id = data.get("research_id")
- if research_id:
- # First check if this research is still active
- from .models.database import get_db_connection
- conn = get_db_connection()
- cursor = conn.cursor()
- cursor.execute(
- "SELECT status FROM research_history WHERE id = ?", (research_id,)
- )
- result = cursor.fetchone()
- conn.close()
-
- # Only allow subscription to valid research
- if result:
- status = result[0]
- # Allow subscription to any research that exists
- sid = request.sid
-
- # Initialize this research key if needed
- if research_id not in socket_subscriptions:
- socket_subscriptions[research_id] = set()
-
- # Add this client to the subscribers
- socket_subscriptions[research_id].add(sid)
- print(f"Client {sid} joined research {research_id}")
-
- # Send initial data immediately (especially important for completed research)
- if research_id in active_research:
- # Send current progress
- current_data = active_research[research_id].copy()
- socketio.emit(f"progress_{research_id}", current_data, room=sid)
- else:
- # Get details from database for completed research
- conn = get_db_connection()
- conn.row_factory = lambda cursor, row: {
- column[0]: row[idx] for idx, column in enumerate(cursor.description)
- }
- cursor = conn.cursor()
- cursor.execute("SELECT * FROM research_history WHERE id = ?", (research_id,))
- result = cursor.fetchone()
- conn.close()
-
- if result:
- # Send completion data
- socketio.emit(f"progress_{research_id}", {
- "status": result["status"],
- "progress": 100 if result["status"] == "completed" else 0,
- "current_task": "Research completed" if result["status"] == "completed" else "Research not active"
- }, room=sid)
- else:
- print(f"Attempted to subscribe to invalid research: {research_id}")
- else:
- print("No research_id provided in subscribe event")
+ def on_disconnect():
+ handle_disconnect(request)
@socketio.on("subscribe_to_research")
- def handle_subscribe(data):
- """Legacy handler for backward compatibility"""
- handle_join(data)
+ def on_subscribe(data):
+ globals_dict = get_globals()
+ active_research = globals_dict.get('active_research', {})
+ handle_subscribe(data, request, active_research)
@socketio.on_error
- def handle_socket_error(e):
- print(f"Socket.IO error: {str(e)}")
- print(traceback.format_exc())
- # Don't propagate exceptions to avoid crashing the server
- return False
+ def on_error(e):
+ return handle_socket_error(e)
@socketio.on_error_default
- def handle_default_error(e):
- print(f"Unhandled Socket.IO error: {str(e)}")
- print(traceback.format_exc())
- # Don't propagate exceptions to avoid crashing the server
- return False
\ No newline at end of file
+ def on_default_error(e):
+ return handle_default_error(e)
\ No newline at end of file
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
new file mode 100644
index 0000000000..813062af62
--- /dev/null
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -0,0 +1,234 @@
+import os
+import logging
+import platform
+import subprocess
+import toml
+from flask import Blueprint, flash, jsonify, redirect, render_template, request, url_for
+
+# Initialize logger
+logger = logging.getLogger(__name__)
+
+# Create a Blueprint for settings
+settings_bp = Blueprint("settings", __name__, url_prefix="/research/settings")
+
+# Config directories and files
+SEARCH_ENGINES_FILE = None
+CONFIG_DIR = None
+MAIN_CONFIG_FILE = None
+LOCAL_COLLECTIONS_FILE = None
+
+def set_config_paths(config_dir, search_engines_file, main_config_file, local_collections_file):
+ """Set the config paths for the settings routes"""
+ global CONFIG_DIR, SEARCH_ENGINES_FILE, MAIN_CONFIG_FILE, LOCAL_COLLECTIONS_FILE
+ CONFIG_DIR = config_dir
+ SEARCH_ENGINES_FILE = search_engines_file
+ MAIN_CONFIG_FILE = main_config_file
+ LOCAL_COLLECTIONS_FILE = local_collections_file
+
+@settings_bp.route("/", methods=["GET"])
+def settings_page():
+ """Main settings dashboard with links to specialized config pages"""
+ return render_template("settings_dashboard.html")
+
+@settings_bp.route("/main", methods=["GET"])
+def main_config_page():
+ """Edit main configuration with search parameters"""
+ return render_template("main_config.html", main_file_path=MAIN_CONFIG_FILE)
+
+@settings_bp.route("/collections", methods=["GET"])
+def collections_config_page():
+ """Edit local collections configuration using raw file editor"""
+ return render_template(
+ "collections_config.html", collections_file_path=LOCAL_COLLECTIONS_FILE
+ )
+
+@settings_bp.route("/api_keys", methods=["GET"])
+def api_keys_config_page():
+ """Edit API keys configuration"""
+ # Get the secrets file path
+ secrets_file = CONFIG_DIR / ".secrets.toml"
+
+ return render_template("api_keys_config.html", secrets_file_path=secrets_file)
+
+@settings_bp.route("/search_engines", methods=["GET"])
+def search_engines_config_page():
+ """Edit search engines configuration using raw file editor"""
+ # Read the current config file
+ raw_config = ""
+ try:
+ with open(SEARCH_ENGINES_FILE, "r") as f:
+ raw_config = f.read()
+ except Exception as e:
+ flash(f"Error reading search engines configuration: {str(e)}", "error")
+ raw_config = "# Error reading configuration file"
+
+ # Get list of engine names for display
+ engine_names = []
+ try:
+ from ...web_search_engines.search_engines_config import SEARCH_ENGINES
+ engine_names = list(SEARCH_ENGINES.keys())
+ engine_names.sort() # Alphabetical order
+ except Exception as e:
+ logger.error(f"Error getting engine names: {e}")
+
+ return render_template(
+ "search_engines_config.html",
+ search_engines_file_path=SEARCH_ENGINES_FILE,
+ raw_config=raw_config,
+ engine_names=engine_names,
+ )
+
+@settings_bp.route("/api/save_search_engines_config", methods=["POST"])
+def save_search_engines_config():
+ try:
+ data = request.get_json()
+ raw_config = data.get("raw_config", "")
+
+ # Validate TOML syntax
+ try:
+ toml.loads(raw_config)
+ except toml.TomlDecodeError as e:
+ return jsonify({"success": False, "error": f"TOML syntax error: {str(e)}"})
+
+ # Ensure directory exists
+ os.makedirs(os.path.dirname(SEARCH_ENGINES_FILE), exist_ok=True)
+
+ # Create a backup first
+ backup_path = f"{SEARCH_ENGINES_FILE}.bak"
+ if os.path.exists(SEARCH_ENGINES_FILE):
+ import shutil
+ shutil.copy2(SEARCH_ENGINES_FILE, backup_path)
+
+ # Write new config
+ with open(SEARCH_ENGINES_FILE, "w") as f:
+ f.write(raw_config)
+
+ return jsonify({"success": True})
+ except Exception as e:
+ return jsonify({"success": False, "error": str(e)})
+
+@settings_bp.route("/api/save_collections_config", methods=["POST"])
+def save_collections_config():
+ try:
+ data = request.get_json()
+ raw_config = data.get("raw_config", "")
+
+ # Validate TOML syntax
+ try:
+ toml.loads(raw_config)
+ except toml.TomlDecodeError as e:
+ return jsonify({"success": False, "error": f"TOML syntax error: {str(e)}"})
+
+ # Ensure directory exists
+ os.makedirs(os.path.dirname(LOCAL_COLLECTIONS_FILE), exist_ok=True)
+
+ # Create a backup first
+ backup_path = f"{LOCAL_COLLECTIONS_FILE}.bak"
+ if os.path.exists(LOCAL_COLLECTIONS_FILE):
+ import shutil
+ shutil.copy2(LOCAL_COLLECTIONS_FILE, backup_path)
+
+ # Write new config
+ with open(LOCAL_COLLECTIONS_FILE, "w") as f:
+ f.write(raw_config)
+
+ # Also trigger a reload in the collections system
+ try:
+ # TODO (djpetti) Fix collection reloading.
+ load_local_collections(reload=True) # noqa: F821
+ except Exception as reload_error:
+ return jsonify(
+ {
+ "success": True,
+ "warning": f"Config saved, but error reloading: {str(reload_error)}",
+ }
+ )
+
+ return jsonify({"success": True})
+ except Exception as e:
+ return jsonify({"success": False, "error": str(e)})
+
+@settings_bp.route("/api/save_main_config", methods=["POST"])
+def save_raw_main_config():
+ try:
+ data = request.get_json()
+ raw_config = data.get("raw_config", "")
+
+ # Validate TOML syntax
+ try:
+ toml.loads(raw_config)
+ except toml.TomlDecodeError as e:
+ return jsonify({"success": False, "error": f"TOML syntax error: {str(e)}"})
+
+ # Ensure directory exists
+ os.makedirs(os.path.dirname(MAIN_CONFIG_FILE), exist_ok=True)
+
+ # Create a backup first
+ backup_path = f"{MAIN_CONFIG_FILE}.bak"
+ if os.path.exists(MAIN_CONFIG_FILE):
+ import shutil
+ shutil.copy2(MAIN_CONFIG_FILE, backup_path)
+
+ # Write new config
+ with open(MAIN_CONFIG_FILE, "w") as f:
+ f.write(raw_config)
+
+ return jsonify({"success": True})
+ except Exception as e:
+ return jsonify({"success": False, "error": str(e)})
+
+@settings_bp.route("/raw_config")
+def get_raw_config():
+ """Return the raw configuration file content"""
+ try:
+ # Determine which config file to load based on a query parameter
+ config_type = request.args.get("type", "main")
+
+ if config_type == "main":
+ config_path = os.path.join(CONFIG_DIR, "config.toml")
+ with open(config_path, "r") as f:
+ return f.read()
+ elif config_type == "llm":
+ config_path = os.path.join(CONFIG_DIR, "llm_config.py")
+ with open(config_path, "r") as f:
+ return f.read()
+ elif config_type == "collections":
+ config_path = os.path.join(CONFIG_DIR, "collections.toml")
+ with open(config_path, "r") as f:
+ return f.read()
+ else:
+ return "Unknown configuration type", 400
+ except Exception as e:
+ return str(e), 500
+
+@settings_bp.route("/open_file_location", methods=["POST"])
+def open_file_location():
+ file_path = request.form.get("file_path")
+
+ if not file_path:
+ flash("No file path provided", "error")
+ return redirect(url_for("settings.settings_page"))
+
+ # Get the directory containing the file
+ dir_path = os.path.dirname(os.path.abspath(file_path))
+
+ # Open the directory in the file explorer
+ try:
+ if platform.system() == "Windows":
+ subprocess.Popen(f'explorer "{dir_path}"')
+ elif platform.system() == "Darwin": # macOS
+ subprocess.Popen(["open", dir_path])
+ else: # Linux
+ subprocess.Popen(["xdg-open", dir_path])
+
+ flash(f"Opening folder: {dir_path}", "success")
+ except Exception as e:
+ flash(f"Error opening folder: {str(e)}", "error")
+
+ # Redirect back to the settings page
+ if "llm" in file_path:
+ return redirect(url_for("settings.llm_config_page"))
+ elif "collections" in file_path:
+ return redirect(url_for("settings.collections_config_page"))
+ else:
+ return redirect(url_for("settings.main_config_page"))
diff --git a/src/local_deep_research/web/services/socket_service.py b/src/local_deep_research/web/services/socket_service.py
index 96e931b159..b4a0107810 100644
--- a/src/local_deep_research/web/services/socket_service.py
+++ b/src/local_deep_research/web/services/socket_service.py
@@ -5,6 +5,8 @@ logger = logging.getLogger(__name__)
# Make this a module variable to be set by the Flask app on initialization
socketio = None
+# Socket subscription tracking
+socket_subscriptions = {}
def set_socketio(socket_instance):
"""Set the Socket.IO instance for the service."""
@@ -61,13 +63,6 @@ def emit_to_subscribers(event_base, research_id, data):
return False
try:
- # Import locally to avoid circular imports
- from ..routes.research_routes import get_globals
-
- # Get socket subscription data
- globals_dict = get_globals()
- socket_subscriptions = globals_dict.get('socket_subscriptions', {})
-
# Emit to the general channel for the research
full_event = f"{event_base}_{research_id}"
socketio.emit(full_event, data)
@@ -83,4 +78,119 @@ def emit_to_subscribers(event_base, research_id, data):
return True
except Exception as e:
logger.error(f"Error emitting to subscribers for research {research_id}: {str(e)}")
- return False
\ No newline at end of file
+ return False
+
+# Socket event handlers moved from app.py
+def handle_connect(request):
+ """Handle client connection"""
+ logger.info(f"Client connected: {request.sid}")
+
+def handle_disconnect(request):
+ """Handle client disconnection"""
+ try:
+ logger.info(f"Client disconnected: {request.sid}")
+ # Clean up subscriptions for this client
+ global socket_subscriptions
+ for research_id, subscribers in list(socket_subscriptions.items()):
+ if request.sid in subscribers:
+ subscribers.remove(request.sid)
+ if not subscribers:
+ socket_subscriptions.pop(research_id, None)
+ logger.info(f"Removed empty subscription for research {research_id}")
+ except Exception as e:
+ logger.error(f"Error handling disconnect: {e}")
+
+def handle_subscribe(data, request, active_research=None):
+ """Handle client subscription to research updates"""
+ from ..models.database import get_db_connection
+ import sqlite3
+ from datetime import datetime
+
+ research_id = data.get("research_id")
+ if research_id:
+ # First check if this research is still active
+ conn = get_db_connection()
+ cursor = conn.cursor()
+ cursor.execute(
+ "SELECT status FROM research_history WHERE id = ?", (research_id,)
+ )
+ result = cursor.fetchone()
+ conn.close()
+
+ # Only allow subscription to valid research
+ if result:
+ status = result[0]
+
+ # Initialize subscription set if needed
+ global socket_subscriptions
+ if research_id not in socket_subscriptions:
+ socket_subscriptions[research_id] = set()
+
+ # Add this client to the subscribers
+ socket_subscriptions[research_id].add(request.sid)
+ logger.info(f"Client {request.sid} subscribed to research {research_id}")
+
+ # Send current status immediately if available
+ if active_research and research_id in active_research:
+ progress = active_research[research_id]["progress"]
+ latest_log = (
+ active_research[research_id]["log"][-1]
+ if active_research[research_id]["log"]
+ else None
+ )
+
+ if latest_log:
+ emit_socket_event(
+ f"research_progress_{research_id}",
+ {
+ "progress": progress,
+ "message": latest_log.get("message", "Processing..."),
+ "status": "in_progress",
+ "log_entry": latest_log,
+ },
+ room=request.sid
+ )
+ elif status in ["completed", "failed", "suspended"]:
+ # Send final status for completed research
+ emit_socket_event(
+ f"research_progress_{research_id}",
+ {
+ "progress": 100 if status == "completed" else 0,
+ "message": (
+ "Research completed successfully"
+ if status == "completed"
+ else (
+ "Research failed"
+ if status == "failed"
+ else "Research was suspended"
+ )
+ ),
+ "status": status,
+ "log_entry": {
+ "time": datetime.utcnow().isoformat(),
+ "message": f"Research is {status}",
+ "progress": 100 if status == "completed" else 0,
+ "metadata": {
+ "phase": (
+ "complete" if status == "completed" else "error"
+ )
+ },
+ },
+ },
+ room=request.sid
+ )
+ else:
+ # Research not found
+ emit_socket_event("error", {"message": f"Research ID {research_id} not found"}, room=request.sid)
+
+def handle_socket_error(e):
+ """Handle Socket.IO errors"""
+ logger.error(f"Socket.IO error: {str(e)}")
+ # Don't propagate exceptions to avoid crashing the server
+ return False
+
+def handle_default_error(e):
+ """Handle unhandled Socket.IO errors"""
+ logger.error(f"Unhandled Socket.IO error: {str(e)}")
+ # Don't propagate exceptions to avoid crashing the server
+ return False
\ No newline at end of file
diff --git a/src/local_deep_research/web/utils/__init__.py b/src/local_deep_research/web/utils/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/src/local_deep_research/web/utils/formatters.py b/src/local_deep_research/web/utils/formatters.py
new file mode 100644
index 0000000000..9b51accd0d
--- /dev/null
+++ b/src/local_deep_research/web/utils/formatters.py
@@ -0,0 +1,75 @@
+import logging
+import traceback
+
+# Initialize logger
+logger = logging.getLogger(__name__)
+
+def convert_debug_to_markdown(raw_text, query):
+ """
+ Convert the debug-formatted text to clean markdown.
+
+ Args:
+ raw_text: The raw formatted findings with debug symbols
+ query: Original research query
+
+ Returns:
+ Clean markdown formatted text
+ """
+ try:
+ logger.info(f"Starting markdown conversion for query: {query}")
+ logger.info(f"Raw text type: {type(raw_text)}")
+
+ # Handle None or empty input
+ if not raw_text:
+ logger.warning("WARNING: raw_text is empty or None")
+ return f"No detailed findings available for '{query}'."
+
+ # If there's a "DETAILED FINDINGS:" section, extract everything after it
+ if "DETAILED FINDINGS:" in raw_text:
+ logger.info("Found DETAILED FINDINGS section")
+ detailed_index = raw_text.index("DETAILED FINDINGS:")
+ content = raw_text[detailed_index + len("DETAILED FINDINGS:") :].strip()
+ else:
+ logger.info("No DETAILED FINDINGS section found, using full text")
+ content = raw_text
+
+ # Remove divider lines with === symbols
+ lines_before = len(content.split("\n"))
+ content = "\n".join(
+ [
+ line
+ for line in content.split("\n")
+ if not line.strip().startswith("===") and not line.strip() == "=" * 80
+ ]
+ )
+ lines_after = len(content.split("\n"))
+ logger.info(f"Removed {lines_before - lines_after} divider lines")
+
+ # Remove SEARCH QUESTIONS BY ITERATION section
+ if "SEARCH QUESTIONS BY ITERATION:" in content:
+ logger.info("Found SEARCH QUESTIONS BY ITERATION section")
+ search_index = content.index("SEARCH QUESTIONS BY ITERATION:")
+ next_major_section = -1
+ for marker in ["DETAILED FINDINGS:", "COMPLETE RESEARCH:"]:
+ if marker in content[search_index:]:
+ marker_pos = content.index(marker, search_index)
+ if next_major_section == -1 or marker_pos < next_major_section:
+ next_major_section = marker_pos
+
+ if next_major_section != -1:
+ logger.info(
+ f"Removing section from index {search_index} to {next_major_section}"
+ )
+ content = content[:search_index] + content[next_major_section:]
+ else:
+ # If no later section, just remove everything from SEARCH QUESTIONS onwards
+ logger.info(f"Removing everything after index {search_index}")
+ content = content[:search_index].strip()
+
+ logger.info(f"Final markdown length: {len(content.strip())}")
+ return content.strip()
+ except Exception as e:
+ logger.error(f"Error in convert_debug_to_markdown: {str(e)}")
+ logger.error(traceback.format_exc())
+ # Return a basic message with the original query as fallback
+ return f"# Research on {query}\n\nThere was an error formatting the research results."