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.

refactor: address PR review feedback for library improvements

LearningCircuit5b7dbd40ecfe
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 054b68cd49..38251d1f25 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -69,6 +69,40 @@ class LibraryService:
except Exception:
return False
+ def _apply_domain_filter(self, query, model_class, domain: str):
+ """Apply domain filter to query for either LibraryDocument or LibraryTextDocument."""
+ if domain == "arxiv.org":
+ return query.filter(model_class.original_url.like("%arxiv.org%"))
+ elif domain == "pubmed":
+ return query.filter(
+ or_(
+ model_class.original_url.like("%pubmed%"),
+ model_class.original_url.like("%ncbi.nlm.nih.gov%"),
+ )
+ )
+ elif domain == "other":
+ return query.filter(
+ and_(
+ ~model_class.original_url.like("%arxiv.org%"),
+ ~model_class.original_url.like("%pubmed%"),
+ ~model_class.original_url.like("%ncbi.nlm.nih.gov%"),
+ )
+ )
+ else:
+ return query.filter(model_class.original_url.like(f"%{domain}%"))
+
+ def _apply_search_filter(self, query, model_class, search_query: str):
+ """Apply search filter to query for either LibraryDocument or LibraryTextDocument."""
+ search_pattern = f"%{search_query}%"
+ return query.filter(
+ or_(
+ model_class.title.ilike(search_pattern),
+ model_class.authors.ilike(search_pattern),
+ model_class.doi.ilike(search_pattern),
+ ResearchResource.title.ilike(search_pattern),
+ )
+ )
+
def get_library_stats(self) -> Dict:
"""Get overall library statistics."""
with get_user_db_session(self.username) as session:
@@ -189,33 +223,9 @@ class LibraryService:
)
if domain:
- if domain == "arxiv.org":
- pdf_query = pdf_query.filter(
- LibraryDocument.original_url.like("%arxiv.org%")
- )
- elif domain == "pubmed":
- pdf_query = pdf_query.filter(
- or_(
- LibraryDocument.original_url.like("%pubmed%"),
- LibraryDocument.original_url.like(
- "%ncbi.nlm.nih.gov%"
- ),
- )
- )
- elif domain == "other":
- pdf_query = pdf_query.filter(
- and_(
- ~LibraryDocument.original_url.like("%arxiv.org%"),
- ~LibraryDocument.original_url.like("%pubmed%"),
- ~LibraryDocument.original_url.like(
- "%ncbi.nlm.nih.gov%"
- ),
- )
- )
- else:
- pdf_query = pdf_query.filter(
- LibraryDocument.original_url.like(f"%{domain}%")
- )
+ pdf_query = self._apply_domain_filter(
+ pdf_query, LibraryDocument, domain
+ )
if file_type:
pdf_query = pdf_query.filter(
@@ -226,16 +236,18 @@ class LibraryService:
pdf_query = pdf_query.filter(LibraryDocument.favorite.is_(True))
if search_query:
- search_pattern = f"%{search_query}%"
- pdf_query = pdf_query.filter(
- or_(
- LibraryDocument.title.ilike(search_pattern),
- LibraryDocument.authors.ilike(search_pattern),
- LibraryDocument.doi.ilike(search_pattern),
- ResearchResource.title.ilike(search_pattern),
- )
+ pdf_query = self._apply_search_filter(
+ pdf_query, LibraryDocument, search_query
)
+ # Filter to only non-downloaded documents
+ pdf_query = pdf_query.filter(
+ LibraryDocument.download_status != "completed"
+ )
+
+ # Apply safety limit to prevent memory issues
+ pdf_query = pdf_query.limit(500000)
+
# Execute PDF query
pdf_results = pdf_query.all()
@@ -360,51 +372,23 @@ class LibraryService:
)
if domain:
- if domain == "arxiv.org":
- text_query = text_query.filter(
- LibraryTextDocument.original_url.like("%arxiv.org%")
- )
- elif domain == "pubmed":
- text_query = text_query.filter(
- or_(
- LibraryTextDocument.original_url.like(
- "%pubmed%"
- ),
- LibraryTextDocument.original_url.like(
- "%ncbi.nlm.nih.gov%"
- ),
- )
- )
- elif domain == "other":
- text_query = text_query.filter(
- and_(
- ~LibraryTextDocument.original_url.like(
- "%arxiv.org%"
- ),
- ~LibraryTextDocument.original_url.like(
- "%pubmed%"
- ),
- ~LibraryTextDocument.original_url.like(
- "%ncbi.nlm.nih.gov%"
- ),
- )
- )
- else:
- text_query = text_query.filter(
- LibraryTextDocument.original_url.like(f"%{domain}%")
- )
+ text_query = self._apply_domain_filter(
+ text_query, LibraryTextDocument, domain
+ )
if search_query:
- search_pattern = f"%{search_query}%"
- text_query = text_query.filter(
- or_(
- LibraryTextDocument.title.ilike(search_pattern),
- LibraryTextDocument.authors.ilike(search_pattern),
- LibraryTextDocument.doi.ilike(search_pattern),
- ResearchResource.title.ilike(search_pattern),
- )
+ text_query = self._apply_search_filter(
+ text_query, LibraryTextDocument, search_query
)
+ # Filter to only non-extracted documents
+ text_query = text_query.filter(
+ LibraryTextDocument.extraction_status != "completed"
+ )
+
+ # Apply safety limit to prevent memory issues
+ text_query = text_query.limit(500000)
+
# Execute text query
text_results = text_query.all()
diff --git a/src/local_deep_research/web/templates/pages/library.html b/src/local_deep_research/web/templates/pages/library.html
index 7ac44e0b21..11d3a56d9f 100644
--- a/src/local_deep_research/web/templates/pages/library.html
+++ b/src/local_deep_research/web/templates/pages/library.html
@@ -193,24 +193,24 @@
<!-- Card Body -->
<div class="card-body" {% if doc.has_pdf or doc.has_text_db %}onclick="openDocument({{ doc.id }}, {{ 'true' if doc.has_pdf else 'false' }}, {{ 'true' if doc.has_text_db else 'false' }})"{% else %}style="cursor: not-allowed; opacity: 0.6;"{% endif %}>
<!-- Storage availability badges -->
- <div class="storage-badges">
+ <div class="ldr-storage-badges">
{% if doc.has_pdf %}
- <div class="storage-badge storage-badge-pdf">
+ <div class="ldr-storage-badge ldr-storage-badge-pdf">
<i class="fas fa-file-pdf"></i> PDF Available
</div>
{% endif %}
{% if doc.has_text_rag %}
- <div class="storage-badge storage-badge-rag">
+ <div class="ldr-storage-badge ldr-storage-badge-rag">
<i class="fas fa-file-alt"></i> Text for RAG
</div>
{% endif %}
{% if doc.has_text_db %}
- <div class="storage-badge storage-badge-db">
+ <div class="ldr-storage-badge ldr-storage-badge-db">
<i class="fas fa-database"></i> Text in DB
</div>
{% endif %}
{% if not doc.has_pdf and not doc.has_text_rag and not doc.has_text_db %}
- <div class="storage-badge storage-badge-none">
+ <div class="ldr-storage-badge ldr-storage-badge-none">
<i class="fas fa-exclamation-triangle"></i> Not Downloaded
</div>
{% endif %}
@@ -646,14 +646,14 @@
}
/* Storage availability badges */
-.storage-badges {
+.ldr-storage-badges {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-bottom: 12px;
}
-.storage-badge {
+.ldr-storage-badge {
padding: 4px 8px;
border-radius: 4px;
font-size: 10px;
@@ -664,22 +664,22 @@
white-space: nowrap;
}
-.storage-badge-pdf {
+.ldr-storage-badge-pdf {
background: #dc3545;
color: white;
}
-.storage-badge-rag {
+.ldr-storage-badge-rag {
background: #ffc107;
color: #212529;
}
-.storage-badge-db {
+.ldr-storage-badge-db {
background: #17a2b8;
color: white;
}
-.storage-badge-none {
+.ldr-storage-badge-none {
background: #ff9800;
color: white;
}