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.

feat: add database tracking for text file extraction

LearningCircuit08f71cb04e2b
diff --git a/src/local_deep_research/database/models/library.py b/src/local_deep_research/database/models/library.py
index 1ffd25b412..3929469eb2 100644
--- a/src/local_deep_research/database/models/library.py
+++ b/src/local_deep_research/database/models/library.py
@@ -106,6 +106,114 @@ class LibraryDocument(Base):
return f"<LibraryDocument(title='{self.title[:50] if self.title else 'Untitled'}', type={self.file_type}, size={self.file_size})>"
+class LibraryTextDocument(Base):
+ """
+ Text files extracted from documents for RAG/analysis.
+ Links to LibraryDocument entries.
+ """
+
+ __tablename__ = "library_text_documents"
+
+ id = Column(Integer, primary_key=True, autoincrement=True)
+
+ # Link to the original resource
+ resource_id = Column(
+ Integer,
+ ForeignKey("research_resources.id", ondelete="CASCADE"),
+ nullable=False,
+ )
+
+ # Link to research for easy querying
+ research_id = Column(
+ String(36),
+ ForeignKey("research_history.id", ondelete="CASCADE"),
+ nullable=False,
+ index=True,
+ )
+
+ # Link to PDF document if extracted from PDF
+ document_id = Column(
+ Integer,
+ ForeignKey("library_documents.id", ondelete="SET NULL"),
+ nullable=True,
+ )
+
+ # Text document identification
+ text_hash = Column(
+ String(64), nullable=False, unique=True, index=True
+ ) # SHA256 for deduplication
+ original_url = Column(Text, nullable=False) # Source URL
+
+ # File information
+ file_path = Column(
+ Text, nullable=False
+ ) # Path relative to library root (txt/ folder)
+ file_size = Column(Integer, nullable=False) # Size in bytes
+ character_count = Column(Integer, nullable=True) # Number of characters
+ word_count = Column(Integer, nullable=True) # Number of words
+
+ # Extraction metadata
+ extraction_method = Column(
+ String(50), nullable=False
+ ) # native_api, pdf_extraction, html_extraction
+ extraction_source = Column(
+ String(50), nullable=True
+ ) # arxiv_api, pubmed_api, pdfplumber, pypdf, etc.
+
+ # Content metadata (same as LibraryDocument for consistency)
+ title = Column(Text) # Document title
+ authors = Column(JSON) # List of authors if available
+ published_date = Column(String(50)) # Publication date if known
+
+ # Academic identifiers (copied from source document)
+ doi = Column(String(255), nullable=True, index=True)
+ arxiv_id = Column(String(100), nullable=True, index=True)
+ pmid = Column(String(50), nullable=True, index=True)
+ pmcid = Column(String(50), nullable=True, index=True)
+
+ # Processing status
+ extraction_status = Column(
+ String(50), nullable=False, default="completed"
+ ) # pending, extracting, completed, failed
+ extraction_error = Column(Text, nullable=True)
+ extracted_at = Column(UtcDateTime, nullable=False, default=utcnow())
+ last_accessed = Column(UtcDateTime, nullable=True)
+
+ # Quality metrics
+ extraction_quality = Column(
+ String(20), nullable=True
+ ) # high, medium, low (based on extraction method)
+ has_formatting_issues = Column(Boolean, default=False)
+ has_encoding_issues = Column(Boolean, default=False)
+
+ # Timestamps
+ created_at = Column(UtcDateTime, default=utcnow(), nullable=False)
+ updated_at = Column(
+ UtcDateTime, default=utcnow(), onupdate=utcnow(), nullable=False
+ )
+
+ # Relationships
+ resource = relationship("ResearchResource", backref="text_document")
+ research = relationship("ResearchHistory", backref="text_documents")
+ pdf_document = relationship("LibraryDocument", backref="text_extracts")
+
+ # Ensure one text file per resource
+ __table_args__ = (
+ UniqueConstraint("resource_id", name="uix_one_text_per_resource"),
+ Index(
+ "idx_research_text_documents", "research_id", "extraction_status"
+ ),
+ Index(
+ "idx_text_extraction_method",
+ "extraction_method",
+ "extraction_status",
+ ),
+ )
+
+ def __repr__(self):
+ return f"<LibraryTextDocument(title='{self.title[:50] if self.title else 'Untitled'}', method={self.extraction_method}, size={self.file_size})>"
+
+
class LibraryCollection(Base):
"""
User-defined collections for organizing documents.
diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.py
index c0b2d3686e..9930b43f9e 100644
--- a/src/local_deep_research/research_library/services/download_service.py
+++ b/src/local_deep_research/research_library/services/download_service.py
@@ -26,7 +26,11 @@ from ...database.models.download_tracker import (
DownloadAttempt,
DownloadTracker,
)
-from ...database.models.library import LibraryDocument, LibraryDownloadQueue
+from ...database.models.library import (
+ LibraryDocument,
+ LibraryDownloadQueue,
+ LibraryTextDocument,
+)
from ...database.models.research import ResearchResource
from ...database.session_context import get_user_db_session
from ...utilities.db_utils import get_settings_manager
@@ -660,7 +664,34 @@ class DownloadService:
if not resource:
return False, "Resource not found"
- # Check if we already have the text file
+ # Check if text document already exists in database
+ existing_text = (
+ session.query(LibraryTextDocument)
+ .filter_by(resource_id=resource_id)
+ .first()
+ )
+
+ if existing_text and existing_text.extraction_status == "completed":
+ # Check if file still exists
+ text_path = Path(self.library_root) / existing_text.file_path
+ if text_path.exists():
+ logger.info(
+ f"Text file already exists (from DB): {existing_text.file_path}"
+ )
+ # Update last accessed time
+ existing_text.last_accessed = datetime.now(UTC)
+ session.commit()
+ return True, None
+ else:
+ logger.warning(
+ f"Text file in DB but missing on disk: {existing_text.file_path}"
+ )
+ # Mark as failed so it can be re-extracted
+ existing_text.extraction_status = "failed"
+ existing_text.extraction_error = "File missing on disk"
+ session.commit()
+
+ # Check if we already have the text file on disk (legacy)
txt_path = Path(self.library_root) / "txt"
existing_files = (
list(txt_path.glob(f"*_{resource_id}.txt"))
@@ -668,36 +699,56 @@ class DownloadService:
else []
)
if existing_files:
- logger.info(f"Text file already exists: {existing_files[0]}")
+ logger.info(
+ f"Text file already exists on disk: {existing_files[0]}"
+ )
+ # Add to database
+ self._create_text_document_record(
+ session,
+ resource,
+ existing_files[0],
+ extraction_method="unknown",
+ extraction_source="legacy_file",
+ )
+ session.commit()
return True, None
- # Check if we already have a PDF downloaded that we can extract from
- pdf_path = Path(self.library_root) / "pdfs"
- existing_pdfs = (
- list(pdf_path.glob(f"*_{resource_id}.pdf"))
- if pdf_path.exists()
- else []
+ # Check if we have an existing PDF document in database
+ pdf_document = (
+ session.query(LibraryDocument)
+ .filter_by(resource_id=resource_id)
+ .first()
)
- if existing_pdfs:
- logger.info(
- f"Found existing PDF, extracting text from: {existing_pdfs[0]}"
- )
- try:
- with open(existing_pdfs[0], "rb") as f:
- pdf_content = f.read()
- text = self._extract_text_from_pdf(pdf_content)
- if text:
- text_path = self._save_text(resource, text)
- if text_path:
- return True, None
- else:
- return False, "Failed to save text file"
- except Exception:
- logger.exception(
- f"Failed to extract text from existing PDF: {existing_pdfs[0]}"
+ if pdf_document and pdf_document.download_status == "completed":
+ pdf_path = Path(self.library_root) / pdf_document.file_path
+ if pdf_path.exists():
+ logger.info(
+ f"Found existing PDF, extracting text from: {pdf_path}"
)
- # Fall through to download new content
+ try:
+ with open(pdf_path, "rb") as f:
+ pdf_content = f.read()
+ text = self._extract_text_from_pdf(pdf_content)
+ if text:
+ text_path = self._save_text_with_db(
+ resource,
+ text,
+ session,
+ extraction_method="pdf_extraction",
+ extraction_source="pdfplumber",
+ pdf_document_id=pdf_document.id,
+ )
+ if text_path:
+ session.commit()
+ return True, None
+ else:
+ return False, "Failed to save text file"
+ except Exception:
+ logger.exception(
+ f"Failed to extract text from existing PDF: {pdf_path}"
+ )
+ # Fall through to download new content
# Try to get text content directly without saving PDF
logger.info(f"Extracting text from: {resource.url}")
@@ -718,9 +769,24 @@ class DownloadService:
else:
text = result.content
- # Save text file
- text_path = self._save_text(resource, text)
+ # Determine extraction method based on downloader type
+ extraction_method = "native_api"
+ extraction_source = "unknown"
+ if isinstance(downloader, ArxivDownloader):
+ extraction_source = "arxiv_api"
+ elif isinstance(downloader, PubMedDownloader):
+ extraction_source = "pubmed_api"
+
+ # Save text file with database record
+ text_path = self._save_text_with_db(
+ resource,
+ text,
+ session,
+ extraction_method=extraction_method,
+ extraction_source=extraction_source,
+ )
if text_path:
+ session.commit()
return True, None
else:
return False, "Failed to save text file"
@@ -741,15 +807,36 @@ class DownloadService:
# Extract text from PDF (without saving the PDF)
text = self._extract_text_from_pdf(result.content)
if text:
- # Save text file
- text_path = self._save_text(resource, text)
+ # Save text file with database record
+ text_path = self._save_text_with_db(
+ resource,
+ text,
+ session,
+ extraction_method="pdf_extraction",
+ extraction_source="pdfplumber_fallback",
+ )
if text_path:
+ session.commit()
return True, None
else:
return False, "Failed to save text file"
else:
+ # Record failed extraction
+ self._record_failed_text_extraction(
+ session,
+ resource,
+ error="Failed to extract text from PDF",
+ )
+ session.commit()
return False, "Failed to extract text from PDF"
else:
+ # Record failed download
+ self._record_failed_text_extraction(
+ session,
+ resource,
+ error=result.skip_reason or "Failed to download content",
+ )
+ session.commit()
return False, result.skip_reason or "Failed to download content"
def _get_downloader(self, url: str):
@@ -1096,3 +1183,183 @@ class DownloadService:
"""Download from medRxiv."""
# Same as bioRxiv
return self._download_biorxiv(url)
+
+ def _save_text_with_db(
+ self,
+ resource: ResearchResource,
+ text: str,
+ session: Session,
+ extraction_method: str,
+ extraction_source: str,
+ pdf_document_id: Optional[int] = None,
+ ) -> Optional[str]:
+ """
+ Save extracted text to disk and database.
+
+ Args:
+ resource: The research resource
+ text: Extracted text content
+ session: Database session
+ extraction_method: How the text was extracted
+ extraction_source: Specific tool/API used
+ pdf_document_id: ID of PDF document if extracted from PDF
+
+ Returns:
+ Absolute path to saved text file or None if save fails
+ """
+ try:
+ # First save the file to disk
+ text_path = self._save_text(resource, text)
+ if not text_path:
+ return None
+
+ # Calculate file metadata
+ file_size = len(text.encode("utf-8"))
+ text_hash = hashlib.sha256(text.encode("utf-8")).hexdigest()
+ word_count = len(text.split())
+ character_count = len(text)
+
+ # Get relative path from library root
+ relative_path = Path(text_path).relative_to(self.library_root)
+
+ # Check if record exists
+ text_doc = (
+ session.query(LibraryTextDocument)
+ .filter_by(resource_id=resource.id)
+ .first()
+ )
+
+ if text_doc:
+ # Update existing record
+ text_doc.text_hash = text_hash
+ text_doc.file_path = str(relative_path)
+ text_doc.file_size = file_size
+ text_doc.character_count = character_count
+ text_doc.word_count = word_count
+ text_doc.extraction_method = extraction_method
+ text_doc.extraction_source = extraction_source
+ text_doc.extraction_status = "completed"
+ text_doc.extraction_error = None
+ text_doc.extracted_at = datetime.now(UTC)
+ text_doc.document_id = pdf_document_id
+
+ # Set quality based on method
+ if extraction_method == "native_api":
+ text_doc.extraction_quality = "high"
+ elif (
+ extraction_method == "pdf_extraction"
+ and extraction_source == "pdfplumber"
+ ):
+ text_doc.extraction_quality = "medium"
+ else:
+ text_doc.extraction_quality = "low"
+ else:
+ # Create new record
+ text_doc = LibraryTextDocument(
+ resource_id=resource.id,
+ research_id=resource.research_id,
+ document_id=pdf_document_id,
+ text_hash=text_hash,
+ original_url=resource.url,
+ file_path=str(relative_path),
+ file_size=file_size,
+ character_count=character_count,
+ word_count=word_count,
+ extraction_method=extraction_method,
+ extraction_source=extraction_source,
+ extraction_status="completed",
+ title=resource.title,
+ extraction_quality="high"
+ if extraction_method == "native_api"
+ else "medium",
+ )
+ session.add(text_doc)
+
+ logger.info(
+ f"Saved text file to database and disk: {relative_path}"
+ )
+ return text_path
+
+ except Exception:
+ logger.exception("Error saving text file with DB")
+ return None
+
+ def _create_text_document_record(
+ self,
+ session: Session,
+ resource: ResearchResource,
+ file_path: Path,
+ extraction_method: str,
+ extraction_source: str,
+ ):
+ """Create a database record for an existing text file."""
+ try:
+ # Read file to get metadata
+ text = file_path.read_text(encoding="utf-8", errors="ignore")
+ file_size = len(text.encode("utf-8"))
+ text_hash = hashlib.sha256(text.encode("utf-8")).hexdigest()
+ word_count = len(text.split())
+ character_count = len(text)
+
+ # Get relative path
+ relative_path = file_path.relative_to(self.library_root)
+
+ # Create record
+ text_doc = LibraryTextDocument(
+ resource_id=resource.id,
+ research_id=resource.research_id,
+ text_hash=text_hash,
+ original_url=resource.url,
+ file_path=str(relative_path),
+ file_size=file_size,
+ character_count=character_count,
+ word_count=word_count,
+ extraction_method=extraction_method,
+ extraction_source=extraction_source,
+ extraction_status="completed",
+ title=resource.title,
+ extraction_quality="low", # Unknown quality for legacy files
+ )
+ session.add(text_doc)
+ logger.info(
+ f"Added existing text file to database: {relative_path}"
+ )
+
+ except Exception:
+ logger.exception("Error creating text document record")
+
+ def _record_failed_text_extraction(
+ self, session: Session, resource: ResearchResource, error: str
+ ):
+ """Record a failed text extraction attempt in the database."""
+ try:
+ text_doc = (
+ session.query(LibraryTextDocument)
+ .filter_by(resource_id=resource.id)
+ .first()
+ )
+
+ if not text_doc:
+ text_doc = LibraryTextDocument(
+ resource_id=resource.id,
+ research_id=resource.research_id,
+ text_hash=hashlib.sha256(b"").hexdigest(),
+ original_url=resource.url,
+ file_path="",
+ file_size=0,
+ extraction_method="failed",
+ extraction_status="failed",
+ extraction_error=error,
+ title=resource.title,
+ )
+ session.add(text_doc)
+ else:
+ text_doc.extraction_status = "failed"
+ text_doc.extraction_error = error
+
+ logger.info(
+ f"Recorded failed text extraction for resource {resource.id}: {error}"
+ )
+
+ except Exception:
+ logger.exception("Error recording failed text extraction")
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.py
index 6b79b58b3f..ff7d75e8de 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -18,7 +18,7 @@ from loguru import logger
from sqlalchemy import and_, or_, func, Integer, case
from ...database.models.download_tracker import DownloadTracker
-from ...database.models.library import LibraryDocument
+from ...database.models.library import LibraryDocument, LibraryTextDocument
from ...database.models.research import ResearchHistory, ResearchResource
from ...database.models.metrics import ResearchRating
from ...database.session_context import get_user_db_session
@@ -610,21 +610,33 @@ class LibraryService:
Statistics about the sync operation
"""
with get_user_db_session(self.username) as session:
- # Get all documents marked as downloaded
+ # Get all PDF documents marked as downloaded
documents = (
session.query(LibraryDocument)
.filter_by(download_status="completed")
.all()
)
+ # Get all text documents marked as extracted
+ text_documents = (
+ session.query(LibraryTextDocument)
+ .filter_by(extraction_status="completed")
+ .all()
+ )
+
stats = {
"total_documents": len(documents),
+ "total_text_documents": len(text_documents),
"files_found": 0,
"files_missing": 0,
+ "text_files_found": 0,
+ "text_files_missing": 0,
"trackers_updated": 0,
"missing_files": [],
+ "missing_text_files": [],
}
+ # Sync PDF documents
for doc in documents:
# Get download tracker
tracker = (
@@ -647,6 +659,7 @@ class LibraryService:
"title": doc.title,
"path": tracker.file_absolute_path,
"url": doc.original_url,
+ "type": "pdf",
}
)
@@ -663,9 +676,42 @@ class LibraryService:
stats["files_missing"] += 1
session.delete(doc)
+ # Sync text documents
+ library_root = Path(self._get_storage_path())
+ for text_doc in text_documents:
+ if text_doc.file_path:
+ # Check if text file exists
+ text_path = library_root / text_doc.file_path
+ if text_path.exists():
+ stats["text_files_found"] += 1
+ else:
+ # Text file missing - mark as failed so it can be re-extracted
+ stats["text_files_missing"] += 1
+ stats["missing_text_files"].append(
+ {
+ "id": text_doc.id,
+ "title": text_doc.title,
+ "path": str(text_path),
+ "url": text_doc.original_url,
+ "type": "txt",
+ }
+ )
+
+ # Mark as failed so it can be re-extracted
+ text_doc.extraction_status = "failed"
+ text_doc.extraction_error = (
+ "File missing on disk during sync"
+ )
+ else:
+ # No file path - mark as failed
+ stats["text_files_missing"] += 1
+ text_doc.extraction_status = "failed"
+ text_doc.extraction_error = "No file path recorded"
+
session.commit()
logger.info(
- f"Library sync completed: {stats['files_found']} found, {stats['files_missing']} missing"
+ f"Library sync completed: PDFs: {stats['files_found']} found, {stats['files_missing']} missing; "
+ f"Text: {stats['text_files_found']} found, {stats['text_files_missing']} missing"
)
return stats
diff --git a/src/local_deep_research/web/routes/research_routes.py b/src/local_deep_research/web/routes/research_routes.py
index 201a361586..f390a06d17 100644
--- a/src/local_deep_research/web/routes/research_routes.py
+++ b/src/local_deep_research/web/routes/research_routes.py
@@ -24,6 +24,7 @@ from ...database.models import (
ResearchLog,
UserActiveResearch,
)
+from ...database.models.library import LibraryDocument
from ...database.session_context import get_user_db_session
from ..auth.decorators import login_required
from ..models.database import calculate_duration
@@ -1000,9 +1001,6 @@ def get_history():
.all()
)
- # Import LibraryDocument model
- from ...database.models.library import LibraryDocument
-
history_items = []
for research in research_records:
# Calculate duration if completed