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.

20.2 KB
Raw Blame
from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse
from ..dependencies.auth import require_auth
from ..dependencies.rate_limit import limiter
from ..template_config import templates
from ..utils.pagination import OFFSET_LIMIT_ERROR, parse_bounded_offset

import json

from loguru import logger
from .notes import _log_value_preview
from sqlalchemy import func

from ...constants import (
    HISTORY_LOGS_DEFAULT_LIMIT,
    HISTORY_LOGS_HARD_CAP,
    ResearchStatus,
)
from ...database.models import ResearchHistory
from ...database.models.library import Document as Document
from ...database.session_context import get_user_db_session

from ..models.database import (
    get_logs_for_research,
    get_total_logs_for_research,
)
from ..research_state import get_active_research_snapshot
from ..services.research_service import get_research_strategy
from .research import _research_not_found

from ...security import filter_research_metadata, strip_settings_snapshot
from typing import Annotated

# Create the router for the history routes
router = APIRouter(prefix="/history", tags=["history"])

# NOTE: Routes use username (not .get()) intentionally.
# Depends(require_auth) guarantees the key exists; direct access fails
# fast if the dependency is ever removed.

# resolve_report_path removed - reports are now stored in database


@router.get("/")
def history_page(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Render the history page"""
    return templates.TemplateResponse(
        request=request, name="pages/history.html", context={"request": request}
    )


@router.get("/api")
def get_history(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Get the research history JSON data"""

    # Flask parsed these with `type=int`, which falls back to the default on
    # a non-integer instead of raising. Bare int() inside the outer try meant
    # `?limit=abc` produced a 500 rather than a 200 with defaults. The sibling
    # endpoint kept the guard — see the clamp in library.py's
    # get_research_documents, whose comment even points back here — so this
    # was the odd one out, not a deliberate change.
    try:
        limit = max(1, min(int(request.query_params.get("limit", 200)), 500))
    except (TypeError, ValueError):
        limit = 200
    try:
        offset = parse_bounded_offset(request.query_params.get("offset"))
    except ValueError:
        return JSONResponse({"error": OFFSET_LIMIT_ERROR}, status_code=400)

    try:
        with get_user_db_session(username) as db_session:
            # Single query with JOIN to get history + document counts.
            # Select only the columns the response needs — never the
            # large ``report_content`` Text body — so listing history
            # doesn't pull every report into memory (#4560).
            results = (
                db_session.query(
                    ResearchHistory.id,
                    ResearchHistory.title,
                    ResearchHistory.query,
                    ResearchHistory.mode,
                    ResearchHistory.status,
                    ResearchHistory.created_at,
                    ResearchHistory.completed_at,
                    ResearchHistory.duration_seconds,
                    ResearchHistory.research_meta,
                    ResearchHistory.chat_session_id,
                    func.count(Document.id).label("document_count"),
                )
                .outerjoin(Document, Document.research_id == ResearchHistory.id)
                .group_by(ResearchHistory.id)
                .order_by(ResearchHistory.created_at.desc())
                .limit(limit)
                .offset(offset)
                .all()
            )

            logger.debug(f"All research count: {len(results)}")

            # Convert to list of dicts
            history = []
            for research in results:
                item = {
                    "id": research.id,
                    "title": research.title,
                    "query": research.query,
                    "mode": research.mode,
                    "status": research.status,
                    "created_at": research.created_at,
                    "completed_at": research.completed_at,
                    "duration_seconds": research.duration_seconds,
                    "document_count": research.document_count,
                }

                item["metadata"] = filter_research_metadata(
                    research.research_meta
                )
                if research.chat_session_id is not None:
                    item["metadata"]["chat_session_id"] = (
                        research.chat_session_id
                    )

                # Recalculate duration if null but both timestamps exist
                if (
                    item["duration_seconds"] is None
                    and item["created_at"]
                    and item["completed_at"]
                ):
                    try:
                        from dateutil import parser  # type: ignore[import-untyped]

                        start_time = parser.parse(item["created_at"])
                        end_time = parser.parse(item["completed_at"])
                        item["duration_seconds"] = int(
                            (end_time - start_time).total_seconds()
                        )
                    except Exception:
                        logger.warning("Error recalculating duration")
                        logger.debug("Duration error details", exc_info=True)

                history.append(item)

        # Format response to match what client expects
        return {
            "status": "success",
            "items": history,  # Use 'items' key as expected by client
        }

        # CORS headers are handled by SecurityHeaders middleware
    except Exception:
        logger.exception("Error getting history")
        # 500, not a bare dict: a bare return serializes as 200, so the
        # client's `response.ok` check passes and it renders an empty
        # history as if the user had none. Matches every other error path
        # in this file.
        return JSONResponse(
            {
                "status": "error",
                "items": [],
                "message": "Failed to retrieve history",
            },
            status_code=500,
        )


def _scrub_server_paths(value):
    """Recursively drop server-path keys from a parsed progress-log entry.

    Rows written by <=v0.4.4 persisted the completion entry with
    ``metadata.report_path`` — the absolute server-side path of the
    generated report — inside the ``progress_log`` column. The raw
    column itself is never returned to the client; this scrub is
    defence in depth for the parsed entries returned as ``log``, in
    case a path-bearing key is ever nested back into the column.
    ``report_path`` is the only such key any writer has ever emitted.
    """
    if isinstance(value, dict):
        return {
            k: _scrub_server_paths(v)
            for k, v in value.items()
            if k != "report_path"
        }
    if isinstance(value, list):
        return [_scrub_server_paths(item) for item in value]
    return value


@router.get("/status/{research_id}")
@limiter.exempt
def get_research_status(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):

    with get_user_db_session(username) as db_session:
        research = (
            db_session.query(ResearchHistory).filter_by(id=research_id).first()
        )

        if not research:
            return _research_not_found(research_id)

        # Extract attributes while session is active
        # to avoid DetachedInstanceError after the with block exits
        raw_progress_log = research.progress_log
        result = {
            "id": research.id,
            "query": research.query,
            "mode": research.mode,
            "status": research.status,
            "created_at": research.created_at,
            "completed_at": research.completed_at,
            # report_path deliberately omitted: it's the absolute
            # server-side path, which leaks directory layout — same rule
            # as /library/api/check-downloads and the history LIST
            # endpoint. progress_log is omitted for the same reason:
            # rows written by <=v0.4.4 embed report_path inside the
            # completion entry's metadata, and no client reads the raw
            # field — the sanitized parsed form is returned as "log".
        }

    # Add progress information from active research (atomic snapshot)
    snapshot = get_active_research_snapshot(research_id)
    if snapshot is not None:
        result["progress"] = snapshot["progress"]
        result["log"] = snapshot["log"]
    elif result.get("status") == ResearchStatus.COMPLETED:
        result["progress"] = 100
        try:
            result["log"] = json.loads(raw_progress_log or "[]")
        except Exception:
            logger.warning(
                "Error parsing progress_log for research {}", research_id
            )
            result["log"] = []
    else:
        result["progress"] = 0
        try:
            result["log"] = json.loads(raw_progress_log or "[]")
        except Exception:
            logger.warning(
                "Error parsing progress_log for research {}", research_id
            )
            result["log"] = []

    # Scrub once at egress so the invariant holds structurally for every
    # branch above, instead of being enumerated at each assignment site.
    # This also covers the snapshot branch's "log": its only writer
    # (the termination handler in research.py) has never nested
    # report_path into an entry's metadata, so scrubbing it is a no-op
    # today — and stays defence in depth if that ever changes.
    result["log"] = _scrub_server_paths(result["log"])

    return result


@router.get("/details/{research_id}")
def get_research_details(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get detailed progress log for a specific research"""

    logger.opt(lazy=True).debug(
        "Details route accessed for research_id: {}",
        lambda: _log_value_preview(research_id),
    )

    try:
        with get_user_db_session(username) as db_session:
            research = (
                db_session.query(ResearchHistory)
                .filter_by(id=research_id)
                .first()
            )
            logger.debug(f"Research found: {research.id if research else None}")

            if not research:
                logger.error(
                    f"Research not found for id: {_log_value_preview(research_id)}"
                )
                return _research_not_found(research_id)

            # Extract all needed attributes while session is active
            # to avoid DetachedInstanceError after the with block exits
            research_data = {
                "query": research.query,
                "mode": research.mode,
                "status": research.status,
                "created_at": research.created_at,
                "completed_at": research.completed_at,
            }
    except Exception:
        logger.exception("Database error")
        return JSONResponse(
            {
                "status": "error",
                "message": "An internal database error occurred.",
            },
            status_code=500,
        )

    # Get logs from the dedicated log database
    logs = get_logs_for_research(research_id, username)

    # Get strategy information
    strategy_name = get_research_strategy(research_id, username=username)

    # Get an atomic snapshot of active research state
    snapshot = get_active_research_snapshot(research_id)

    # If this is an active research, merge with any in-memory logs
    if snapshot is not None:
        # Use the logs from memory temporarily until they're saved to the database
        memory_logs = snapshot["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"])

    progress = (
        snapshot["progress"]
        if snapshot is not None
        else (100 if research_data["status"] == ResearchStatus.COMPLETED else 0)
    )

    return {
        "research_id": research_id,
        "query": research_data["query"],
        "mode": research_data["mode"],
        "status": research_data["status"],
        "strategy": strategy_name,
        "progress": progress,
        "created_at": research_data["created_at"],
        "completed_at": research_data["completed_at"],
        "log": logs,
    }


@router.get("/report/{research_id}")
def get_report(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    with get_user_db_session(username) as db_session:
        research = (
            db_session.query(ResearchHistory).filter_by(id=research_id).first()
        )

        if not research:
            return _research_not_found(research_id, message="Report not found")

        try:
            # research.report_content holds the answer-only string; rebuild
            # the legacy display shape (answer + Sources from
            # research_resources + Metrics from research_meta) on demand.
            # Do NOT read storage.get_report*() here — those return the
            # answer-only body (notification-teaser shape) and would drop the
            # Sources / Research Metrics sections (#3665).
            from ..services.report_assembly_service import assemble_full_report

            content = assemble_full_report(research, db_session)
            # Only None means "research not found" — guarded above. An empty
            # but found row returns "" and is a valid response.
            if content is None:
                return _research_not_found(
                    research_id, message="Report content not found"
                )

            # Strip settings_snapshot (API keys, tokens, base URLs, paths)
            # before returning research_meta to the client — settings_snapshot
            # is persisted in research_meta but must never reach the API
            # response. Matches the sibling /details and /api/research/<id>
            # routes; all other metadata fields are preserved.
            stored_metadata = strip_settings_snapshot(research.research_meta)

            # Create an enhanced metadata dictionary with database fields
            enhanced_metadata = {
                "query": research.query,
                "mode": research.mode,
                "created_at": research.created_at,
                "completed_at": research.completed_at,
                "duration": research.duration_seconds,
            }

            # Merge with stored metadata
            enhanced_metadata.update(stored_metadata)

            return {
                "status": "success",
                "content": content,
                "query": research.query,
                "mode": research.mode,
                "created_at": research.created_at,
                "completed_at": research.completed_at,
                "metadata": enhanced_metadata,
            }

        except Exception:
            logger.exception(
                "Failed to retrieve report for research {}", research_id
            )
            return JSONResponse(
                {"status": "error", "message": "Failed to retrieve report"},
                status_code=500,
            )


@router.get("/markdown/{research_id}")
def get_markdown(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get markdown export for a specific research"""
    with get_user_db_session(username) as db_session:
        research = (
            db_session.query(ResearchHistory).filter_by(id=research_id).first()
        )

        if not research:
            return _research_not_found(research_id, message="Report not found")

        try:
            # Rebuild the assembled legacy shape (answer + Sources + Metrics)
            # rather than the answer-only storage.get_report() body (#3665).
            from ..services.report_assembly_service import assemble_full_report

            content = assemble_full_report(research, db_session)
            if content is None:
                return _research_not_found(
                    research_id, message="Report content not found"
                )

            return {"status": "success", "content": content}
        except Exception:
            logger.exception(
                "Failed to retrieve markdown report for research {}",
                research_id,
            )
            return JSONResponse(
                {"status": "error", "message": "Failed to retrieve report"},
                status_code=500,
            )


@router.get("/logs/{research_id}")
def get_research_logs(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get logs for a specific research ID.

    Accepts ``?limit=N`` to bound the response size; the default matches
    the frontend's ``MAX_LOG_ENTRIES`` DOM cap. Clamped to
    ``[1, HISTORY_LOGS_HARD_CAP]`` so a client cannot force an unbounded
    load (a long langgraph run can persist thousands of rows; pre-cap
    the route allocated ~150 MB transient on the server and Firefox
    parsed a ~50 MB JSON response).
    """

    # Per-request cap. HISTORY_LOGS_DEFAULT_LIMIT (500) matches
    # MAX_LOG_ENTRIES in logpanel.js; the HISTORY_LOGS_HARD_CAP (5000)
    # ceiling lets explicit log-download flows still get more rows but
    # stops accidental unbounded loads.
    # Parse defensively: a non-numeric ?limit= must not 500 (Werkzeug's
    # request.args.get(type=int) silently fell back to the default; the
    # bare int() here would raise ValueError -> 500).
    try:
        limit = int(
            request.query_params.get("limit", str(HISTORY_LOGS_DEFAULT_LIMIT))
        )
    except (TypeError, ValueError):
        limit = HISTORY_LOGS_DEFAULT_LIMIT
    limit = max(1, min(limit, HISTORY_LOGS_HARD_CAP))

    # First check if the research exists
    with get_user_db_session(username) as db_session:
        research = (
            db_session.query(ResearchHistory).filter_by(id=research_id).first()
        )

        if not research:
            return _research_not_found(research_id)

    # Retrieve logs from the database
    logs = get_logs_for_research(research_id, username, limit=limit)

    # Defensive backfill for any row missing the three frontend-required
    # fields. `get_logs_for_research` always sets these from ResearchLog
    # columns, so this backfill is belt-and-braces. It is currently
    # UNCOVERED: the test that exercised it,
    # test_logs_with_missing_fields_get_defaults, was removed with the Flask
    # suite and has no successor (0 hits in tests/). The contract it asserted
    # was: extra keys preserved, missing keys defaulted. In-place mutation is
    # safe — the formatter returned a fresh list of fresh dicts.
    for log in logs:
        log.setdefault("time", "")
        log.setdefault("message", "No message")
        log.setdefault("type", "info")

    return {"status": "success", "logs": logs}


@router.get("/log_count/{research_id}")
def get_log_count(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get the total number of logs for a specific research ID"""
    # Verify ownership before exposing the count.
    with get_user_db_session(username) as db_session:
        research = (
            db_session.query(ResearchHistory.id)
            .filter_by(id=research_id)
            .first()
        )
        if not research:
            # Use the shared helper rather than an inline 404, matching the
            # two sibling call sites in this file (lines ~165 and ~226) and
            # main's #5386 fix. It emits status/error/message together, a
            # superset of both historical 404 shapes, so every frontend
            # reader keeps working.
            return _research_not_found(research_id)

    total_logs = get_total_logs_for_research(research_id, username)

    return {"status": "success", "total_logs": total_logs}