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 404 error when loading research reports (#458)

Daniel Petti889837ff49b0
diff --git a/src/local_deep_research/__version__.py b/src/local_deep_research/__version__.py
index dd9b22cccc..722515271f 100644
--- a/src/local_deep_research/__version__.py
+++ b/src/local_deep_research/__version__.py
@@ -1 +1 @@
-__version__ = "0.5.1"
+__version__ = "0.5.2"
diff --git a/src/local_deep_research/web/database/migrations.py b/src/local_deep_research/web/database/migrations.py
index 2064c58be0..4c1758512a 100644
--- a/src/local_deep_research/web/database/migrations.py
+++ b/src/local_deep_research/web/database/migrations.py
@@ -2,7 +2,14 @@ from loguru import logger
from sqlalchemy import inspect
from ..services.settings_manager import SettingsManager
-from .models import Base, Journal, Setting, ResearchLog
+from .models import (
+ Base,
+ Journal,
+ Setting,
+ ResearchLog,
+ Research,
+ ResearchHistory,
+)
def import_default_settings_file(db_session):
@@ -52,6 +59,14 @@ def run_migrations(engine, db_session=None):
logger.info("Creating research logs table.")
Base.metadata.create_all(engine, tables=[ResearchLog.__table__])
+ if not inspector.has_table(Research.__tablename__):
+ logger.info("Creating research table.")
+ Base.metadata.create_all(engine, tables=[Research.__table__])
+
+ if not inspector.has_table(ResearchHistory.__tablename__):
+ logger.info("Creating research table.")
+ Base.metadata.create_all(engine, tables=[ResearchHistory.__table__])
+
# Import existing settings from files
if db_session:
import_default_settings_file(db_session)
diff --git a/src/local_deep_research/web/database/models.py b/src/local_deep_research/web/database/models.py
index 505803551d..da922796d9 100644
--- a/src/local_deep_research/web/database/models.py
+++ b/src/local_deep_research/web/database/models.py
@@ -34,6 +34,37 @@ class ResearchStatus(enum.Enum):
CANCELLED = "cancelled"
+class ResearchHistory(Base):
+ """Represents the research table."""
+
+ __tablename__ = "research_history"
+
+ # Unique identifier for each record.
+ id = Column(Integer, primary_key=True, autoincrement=True)
+ # The search query.
+ query = Column(Text, nullable=False)
+ # The mode of research (e.g., 'quick_summary', 'detailed_report').
+ mode = Column(Text, nullable=False)
+ # Current status of the research.
+ status = Column(Text, nullable=False)
+ # The timestamp when the research started.
+ created_at = Column(Text, nullable=False)
+ # The timestamp when the research was completed.
+ completed_at = Column(Text)
+ # Duration of the research in seconds.
+ duration_seconds = Column(Integer)
+ # Path to the generated report.
+ report_path = Column(Text)
+ # Additional metadata about the research.
+ research_meta = Column(JSON)
+ # Latest progress log message.
+ progress_log = Column(JSON)
+ # Current progress of the research (as a percentage).
+ progress = Column(Integer)
+ # Title of the research report.
+ title = Column(Text)
+
+
class Research(Base):
__tablename__ = "research"
@@ -54,14 +85,6 @@ class Research(Base):
end_time = Column(DateTime, nullable=True)
error_message = Column(Text, nullable=True)
- # Relationships
- report = relationship(
- "ResearchReport",
- back_populates="research",
- uselist=False,
- cascade="all, delete-orphan",
- )
-
class ResearchLog(Base):
__tablename__ = "app_logs"
@@ -88,29 +111,6 @@ class ResearchLog(Base):
)
-class ResearchReport(Base):
- __tablename__ = "research_report"
-
- id = Column(Integer, primary_key=True, index=True)
- research_id = Column(
- Integer,
- ForeignKey("research.id", ondelete="CASCADE"),
- nullable=False,
- unique=True,
- )
- content = Column(Text, nullable=True)
- created_at = Column(DateTime, server_default=func.now(), nullable=False)
- updated_at = Column(
- DateTime, server_default=func.now(), onupdate=func.now(), nullable=False
- )
- report_metadata = Column(
- JSON, nullable=True
- ) # Additional metadata about the report
-
- # Relationships
- research = relationship("Research", back_populates="report")
-
-
class SettingType(enum.Enum):
APP = "app"
LLM = "llm"
diff --git a/src/local_deep_research/web/models/database.py b/src/local_deep_research/web/models/database.py
index 59e942eece..bf1c4e7019 100644
--- a/src/local_deep_research/web/models/database.py
+++ b/src/local_deep_research/web/models/database.py
@@ -43,26 +43,6 @@ def init_db():
conn = get_db_connection()
cursor = conn.cursor()
- # Create the table if it doesn't exist
- cursor.execute(
- """
- CREATE TABLE IF NOT EXISTS research_history (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- query TEXT NOT NULL,
- mode TEXT NOT NULL,
- status TEXT NOT NULL,
- created_at TEXT NOT NULL,
- completed_at TEXT,
- duration_seconds INTEGER,
- report_path TEXT,
- metadata TEXT,
- progress_log TEXT,
- progress INTEGER,
- title TEXT
- )
- """
- )
-
# Create a dedicated table for research logs
cursor.execute(
"""
@@ -101,7 +81,7 @@ def init_db():
columns = [column[1] for column in cursor.fetchall()]
if "duration_seconds" not in columns:
- print(
+ logger.info(
"Adding missing 'duration_seconds' column to research_history table"
)
cursor.execute(
@@ -110,16 +90,26 @@ def init_db():
# Check if the progress column exists, add it if missing
if "progress" not in columns:
- print("Adding missing 'progress' column to research_history table")
+ logger.info(
+ "Adding missing 'progress' column to research_history table"
+ )
cursor.execute(
"ALTER TABLE research_history ADD COLUMN progress INTEGER"
)
# Check if the title column exists, add it if missing
if "title" not in columns:
- print("Adding missing 'title' column to research_history table")
+ logger.info("Adding missing 'title' column to research_history table")
cursor.execute("ALTER TABLE research_history ADD COLUMN title TEXT")
+ # Check if the metadata column exists, and rename it to "research_meta"
+ # if it does.
+ if "metadata" in columns:
+ logger.info("Renaming 'metadata' column to 'research_meta'")
+ cursor.execute(
+ "ALTER TABLE research_history RENAME COLUMN metadata TO research_meta"
+ )
+
# Enable foreign key support
cursor.execute("PRAGMA foreign_keys = ON")
diff --git a/src/local_deep_research/web/routes/research_routes.py b/src/local_deep_research/web/routes/research_routes.py
index 1d575565b0..90b9f82fc4 100644
--- a/src/local_deep_research/web/routes/research_routes.py
+++ b/src/local_deep_research/web/routes/research_routes.py
@@ -15,13 +15,18 @@ from flask import (
)
from loguru import logger
-from ..models.database import calculate_duration, get_db_connection
+from ..models.database import (
+ calculate_duration,
+ get_db_connection,
+)
from ..services.research_service import (
run_research_process,
start_research_process,
)
from ..utils.templates import render_template_with_defaults
from .globals import active_research, termination_flags
+from ..database.models import ResearchHistory, ResearchLog
+from ...utilities.db_utils import get_db_session
# Create a Blueprint for the research application
research_bp = Blueprint("research", __name__, url_prefix="/research")
@@ -220,20 +225,19 @@ def start_research():
"questions_per_iteration": questions_per_iteration,
}
- cursor.execute(
- "INSERT INTO research_history (query, mode, status, created_at, progress_log, metadata) VALUES (?, ?, ?, ?, ?, ?)",
- (
- query,
- mode,
- "in_progress",
- created_at,
- json.dumps([{"time": created_at, "progress": 0}]),
- json.dumps(research_settings),
- ),
- )
- research_id = cursor.lastrowid
- conn.commit()
- conn.close()
+ db_session = get_db_session()
+ with db_session:
+ research = ResearchHistory(
+ query=query,
+ mode=mode,
+ status="in_progress",
+ created_at=created_at,
+ progress_log=[{"time": created_at, "progress": 0}],
+ research_meta=research_settings,
+ )
+ db_session.add(research)
+ db_session.commit()
+ research_id = research.id
# Start the research process with the selected parameters
research_thread = start_research_process(
@@ -612,59 +616,30 @@ def get_history():
@research_bp.route("/api/research/<int:research_id>")
def get_research_details(research_id):
- """Get full details of a research"""
+ """Get full details of a research using ORM"""
try:
- conn = get_db_connection()
- cursor = conn.cursor()
- cursor.execute(
- """SELECT id, query, status, progress, mode,
- created_at, completed_at, report_path, metadata
- FROM research_history WHERE id = ?""",
- (research_id,),
+ db_session = get_db_session()
+ research = (
+ db_session.query(ResearchHistory)
+ .filter(ResearchHistory.id == research_id)
+ .first()
)
- result = cursor.fetchone()
- if result is None:
- conn.close()
+ if not research:
return jsonify({"error": "Research not found"}), 404
- # Unpack the result
- (
- research_id,
- query,
- status,
- progress,
- mode,
- created_at,
- completed_at,
- report_path,
- metadata_str,
- ) = result
-
- # Parse metadata if it exists
- metadata = {}
- if metadata_str:
- try:
- metadata = json.loads(metadata_str)
- except json.JSONDecodeError:
- logger.warning(
- f"Invalid JSON in metadata for research {research_id}"
- )
-
- conn.close()
-
return jsonify(
{
- "id": research_id,
- "query": query,
- "status": status,
- "progress": progress,
- "progress_percentage": progress or 0,
- "mode": mode,
- "created_at": created_at,
- "completed_at": completed_at,
- "report_path": report_path,
- "metadata": metadata,
+ "id": research.id,
+ "query": research.query,
+ "status": research.status,
+ "progress": research.progress,
+ "progress_percentage": research.progress or 0,
+ "mode": research.mode,
+ "created_at": research.created_at,
+ "completed_at": research.completed_at,
+ "report_path": research.report_path,
+ "metadata": research.research_meta,
}
)
except Exception as e:
@@ -676,50 +651,36 @@ def get_research_details(research_id):
def get_research_logs(research_id):
"""Get logs for a specific research"""
try:
- conn = get_db_connection()
- cursor = conn.cursor()
-
# First check if the research exists
- cursor.execute(
- "SELECT id FROM research_history WHERE id = ?", (research_id,)
- )
- if cursor.fetchone() is None:
- conn.close()
- return jsonify({"error": "Research not found"}), 404
-
- # Get logs from research_logs table
- cursor.execute(
- """SELECT id, message, timestamp, log_type, progress, metadata
- FROM research_logs
- WHERE research_id = ?
- ORDER BY timestamp ASC""",
- (research_id,),
- )
+ db_session = get_db_session()
+ with db_session:
+ research = (
+ db_session.query(ResearchHistory)
+ .filter_by(id=research_id)
+ .first()
+ )
+ if not research:
+ return jsonify({"error": "Research not found"}), 404
+
+ # Get logs from research_logs table
+ log_results = (
+ db_session.query(ResearchLog)
+ .filter_by(research_id=research_id)
+ .order_by(ResearchLog.timestamp)
+ .all()
+ )
logs = []
- for row in cursor.fetchall():
- log_id, message, timestamp, log_type, progress, metadata_str = row
-
- # Parse metadata if it exists
- metadata = {}
- if metadata_str:
- try:
- metadata = json.loads(metadata_str)
- except json.JSONDecodeError:
- pass
-
+ for row in log_results:
logs.append(
{
- "id": log_id,
- "message": message,
- "timestamp": timestamp,
- "log_type": log_type,
- "progress": progress,
- "metadata": metadata,
+ "id": row.id,
+ "message": row.message,
+ "timestamp": row.timestamp,
+ "log_type": row.level,
}
)
- conn.close()
return jsonify(logs)
except Exception as e:
@@ -730,27 +691,18 @@ def get_research_logs(research_id):
@research_bp.route("/api/report/<int:research_id>")
def get_research_report(research_id):
"""Get the research report content"""
- from ..database.models import Research
- from ...utilities.db_utils import get_db_session
-
session = get_db_session()
try:
# Query using ORM
- research = session.query(Research).filter_by(id=research_id).first()
+ research = (
+ session.query(ResearchHistory).filter_by(id=research_id).first()
+ )
if research is None:
return jsonify({"error": "Research not found"}), 404
# Parse metadata if it exists
- metadata = {}
- if research.metadata:
- try:
- metadata = json.loads(research.metadata)
- except json.JSONDecodeError:
- logger.warning(
- f"Invalid JSON in metadata for research {research_id}"
- )
-
+ metadata = research.research_meta
# Check if report file exists
if not research.report_path or not os.path.exists(research.report_path):
return jsonify({"error": "Report file not found"}), 404
@@ -771,11 +723,11 @@ def get_research_report(research_id):
"content": content,
"metadata": {
"query": research.query,
- "mode": research.mode.value if research.mode else None,
- "created_at": research.created_at.isoformat()
+ "mode": research.mode if research.mode else None,
+ "created_at": research.created_at
if research.created_at
else None,
- "completed_at": research.completed_at.isoformat()
+ "completed_at": research.completed_at
if research.completed_at
else None,
"report_path": research.report_path,
diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.py
index 762c82f52f..b7e37452b4 100644
--- a/src/local_deep_research/web/services/research_service.py
+++ b/src/local_deep_research/web/services/research_service.py
@@ -1,5 +1,4 @@
import hashlib
-import json
import threading
from datetime import datetime
from pathlib import Path
@@ -15,8 +14,8 @@ from ...utilities.log_utils import log_for_research
from ...utilities.search_utilities import extract_links_from_search_results
from ...utilities.db_utils import get_db_session
from ...utilities.threading_utils import thread_context, thread_with_app_context
-from ..database.models import ResearchStrategy
-from ..models.database import calculate_duration, get_db_connection
+from ..database.models import ResearchStrategy, ResearchHistory
+from ..models.database import calculate_duration
from .socket_service import SocketIOService
# Output directory for research results
@@ -336,36 +335,19 @@ def run_research_process(
active_research[research_id]["progress"] = adjusted_progress
# Update progress in the research_history table (for backward compatibility)
- conn = get_db_connection()
- cursor = conn.cursor()
+ db_session = get_db_session()
# Update the progress and log separately to avoid race conditions
- 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 = []
-
- cursor.execute(
- "UPDATE research_history SET progress_log = ? WHERE id = ?",
- (json.dumps(current_log), research_id),
- )
-
- conn.commit()
- conn.close()
+ with db_session:
+ if adjusted_progress is not None:
+ research = (
+ db_session.query(ResearchHistory)
+ .filter(ResearchHistory.id == research_id)
+ .first()
+ )
+ if research:
+ research.progress = adjusted_progress
+ db_session.commit()
# Emit a socket event
try:
@@ -743,32 +725,28 @@ def run_research_process(
logger.info(
"Updating database for research_id: %s", research_id
)
- # Get the start time from the database
- conn = get_db_connection()
- 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,
- str(report_path),
- json.dumps(metadata),
- research_id,
- ),
- )
- conn.commit()
- conn.close()
+
+ db_session = get_db_session()
+ with db_session:
+ research = (
+ db_session.query(ResearchHistory)
+ .filter_by(id=research_id)
+ .first()
+ )
+
+ # Use the helper function for consistent duration calculation
+ duration_seconds = calculate_duration(
+ research.created_at, research.completed_at
+ )
+
+ research.status = "completed"
+ research.completed_at = completed_at
+ research.duration_seconds = duration_seconds
+ research.report_path = str(report_path)
+ research.research_meta = metadata
+
+ db_session.commit()
+
logger.info(
f"Database updated successfully for research_id: {research_id}"
)
@@ -837,31 +815,26 @@ def run_research_process(
now = datetime.utcnow()
completed_at = now.isoformat()
- # Get the start time from the database
- conn = get_db_connection()
- 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,
- str(report_path),
- json.dumps(metadata),
- research_id,
- ),
- )
- conn.commit()
- conn.close()
+ db_session = get_db_session()
+ with db_session:
+ research = (
+ db_session.query(ResearchHistory)
+ .filter_by(id=research_id)
+ .first()
+ )
+
+ # Use the helper function for consistent duration calculation
+ duration_seconds = calculate_duration(
+ research.created_at, research.completed_at
+ )
+
+ research.status = "completed"
+ research.completed_at = completed_at
+ research.duration_seconds = duration_seconds
+ research.report_path = str(report_path)
+ research.research_meta = metadata
+
+ db_session.commit()
progress_callback(
"Research completed successfully",
@@ -914,9 +887,6 @@ def run_research_process(
if research_id in active_research:
progress_callback(user_friendly_error, None, metadata)
- conn = get_db_connection()
- cursor = conn.cursor()
-
# If termination was requested, mark as suspended instead of failed
status = (
"suspended"
@@ -938,28 +908,33 @@ def run_research_process(
# 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()
+ db_session = get_db_session()
+ with db_session:
+ research = (
+ db_session.query(ResearchHistory)
+ .filter_by(id=research_id)
+ .first()
+ )
+ assert research is not None, "Research not in database"
+
+ duration_seconds = calculate_duration(research.created_at)
+
+ db_session = get_db_session()
+ with db_session:
+ research = (
+ db_session.query(ResearchHistory)
+ .filter_by(id=research_id)
+ .first()
+ )
+ assert research is not None, "Research not in database"
+
+ # Update the ResearchHistory object with the new status and completion time
+ research.status = status
+ research.completed_at = completed_at
+ research.duration_seconds = duration_seconds
+ research.metadata = metadata
+
+ db_session.commit()
try:
SocketIOService().emit_to_subscribers(
@@ -993,15 +968,17 @@ def cleanup_research_resources(research_id, active_research, termination_flags):
# Get the current status from the database to determine the final status message
current_status = "completed" # Default
try:
- conn = get_db_connection()
- 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()
+ db_session = get_db_session()
+ with db_session:
+ research = (
+ db_session.query(ResearchHistory)
+ .filter(ResearchHistory.id == research_id)
+ .first()
+ )
+ if research:
+ current_status = research.status
+ else:
+ logger.error("Research with ID %s not found", research_id)
except Exception:
logger.exception("Error retrieving research status during cleanup")
@@ -1063,33 +1040,23 @@ def handle_termination(research_id, active_research, termination_flags):
active_research: Dictionary of active research processes
termination_flags: Dictionary of termination flags
"""
- # Explicitly set the status to suspended in the database
- conn = get_db_connection()
- 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()
+ # Fetch the start time from the database using the ORM
+ session = get_db_session()
+ research = session.query(ResearchHistory).filter_by(id=research_id).first()
- # Calculate the duration
- duration_seconds = (
- calculate_duration(result[0]) if result and result[0] else None
- )
+ if research:
+ duration_seconds = calculate_duration(research.created_at)
- # 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()
+ # Update the database with suspended status using the ORM
+ research.status = "suspended"
+ research.completed_at = completed_at
+ research.duration_seconds = duration_seconds
+ session.commit()
+ else:
+ logger.error(f"Research with ID {research_id} not found.")
# Clean up resources
cleanup_research_resources(research_id, active_research, termination_flags)
@@ -1097,7 +1064,7 @@ def handle_termination(research_id, active_research, termination_flags):
def cancel_research(research_id):
"""
- Cancel/terminate a research process
+ Cancel/terminate a research process using ORM.
Args:
research_id: The ID of the research to cancel
@@ -1122,27 +1089,14 @@ def cancel_research(research_id):
return True
else:
# Update database directly if not found in active_research
- from ..models.database import get_db_connection
-
- conn = get_db_connection()
- cursor = conn.cursor()
-
- # First check if the research exists
- cursor.execute(
- "SELECT status FROM research_history WHERE id = ?", (research_id,)
+ session = get_db_session()
+ research = (
+ session.query(ResearchHistory).filter_by(id=research_id).first()
)
- result = cursor.fetchone()
-
- if not result:
- conn.close()
+ if not research:
return False
# If it exists but isn't in active_research, still update status
- cursor.execute(
- "UPDATE research_history SET status = ? WHERE id = ?",
- ("suspended", research_id),
- )
- conn.commit()
- conn.close()
-
+ research.status = "suspended"
+ session.commit()
return True