~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Storage that always uses database as primary storage with optional file backup."""
from typing import Dict, Any, Optional
from loguru import logger
from sqlalchemy.orm import Session
from .base import ReportStorage
from .database import DatabaseReportStorage
from .file import FileReportStorage
from ..config.paths import (
REPORT_BACKUP_REMEDY,
describe_private_directory_refusal,
)
class DatabaseWithFileBackupStorage(ReportStorage):
"""
Storage that always saves to database and optionally backs up to file system.
Database is the primary storage and is always used.
File storage is optional for external access/backup purposes.
"""
def __init__(self, session: Session, enable_file_storage: bool = False):
"""
Initialize combined storage.
Args:
session: SQLAlchemy database session
enable_file_storage: Whether to also save reports to file system.
If FileReportStorage refuses its directory with an OSError
(for example one that is not private), ``file_storage``
stays None, a warning names the cause, and reports are saved
to the database only.
"""
self.db_storage = DatabaseReportStorage(session)
self.file_storage = None
if enable_file_storage:
try:
self.file_storage = FileReportStorage()
except OSError as exc:
cause = describe_private_directory_refusal(exc)
logger.warning(
"Report file backup is disabled because the research "
f"outputs directory was refused: {cause}. Reports are "
f"still saved in the database. {REPORT_BACKUP_REMEDY}."
)
self.enable_file_storage = enable_file_storage
def save_report(
self,
research_id: str,
content: str,
metadata: Optional[Dict[str, Any]] = None,
username: Optional[str] = None,
) -> bool:
"""Save report to database and optionally to file."""
# Always save to database first (primary storage)
success = self.db_storage.save_report(
research_id, content, metadata, username
)
if not success:
logger.error(f"Failed to save report {research_id} to database")
return False
# Optionally save to file system
if self.file_storage:
try:
file_success = self.file_storage.save_report(
research_id, content, metadata, username
)
if not file_success:
logger.warning(
f"Failed to save report {research_id} to file, "
"but database save was successful"
)
except Exception:
logger.warning(
f"Error saving report {research_id} to file"
"but database save was successful"
)
return success
def get_report(
self, research_id: str, username: Optional[str] = None
) -> Optional[str]:
"""Get report from database (never from file)."""
# Always read from database for consistency
return self.db_storage.get_report(research_id, username)
def delete_report(
self, research_id: str, username: Optional[str] = None
) -> bool:
"""Delete report from database and file if it exists."""
# Delete from database
db_success = self.db_storage.delete_report(research_id, username)
# Also delete from file if enabled
if self.file_storage and db_success:
try:
self.file_storage.delete_report(research_id, username)
except Exception:
logger.warning(f"Error deleting report file {research_id}")
return db_success
def list_reports(self, username: Optional[str] = None) -> list:
"""List reports from database only."""
# Always list from database for consistency
return self.db_storage.list_reports(username)
def get_report_with_metadata(
self, research_id: str, username: Optional[str] = None
) -> Optional[Dict[str, Any]]:
"""Get report with metadata from database."""
# Always read from database for consistency
return self.db_storage.get_report_with_metadata(research_id, username)
def report_exists(
self, research_id: str, username: Optional[str] = None
) -> bool:
"""Check if report exists in database."""
# Always check database for consistency
return self.db_storage.report_exists(research_id, username)