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: improve library document display and citations

LearningCircuit9d78c0c47f2d
diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.py
index 73c04ff7b5..35c31333f0 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -872,8 +872,8 @@ def get_collections():
"is_default": coll.is_default
if hasattr(coll, "is_default")
else False,
- "document_count": len(coll.user_documents)
- if hasattr(coll, "user_documents")
+ "document_count": len(coll.document_links)
+ if hasattr(coll, "document_links")
else 0,
"folder_count": len(coll.linked_folders)
if hasattr(coll, "linked_folders")
@@ -1315,6 +1315,12 @@ def upload_to_collection(collection_id):
file_content, file_type, filename
)
+ # Clean the extracted text to remove surrogate characters
+ if extracted_text:
+ from ...text_processing import remove_surrogates
+
+ extracted_text = remove_surrogates(extracted_text)
+
if not extracted_text:
errors.append(
{
diff --git a/src/local_deep_research/research_library/services/library_rag_service.py b/src/local_deep_research/research_library/services/library_rag_service.py
index fe938d5625..f9d3ae1e04 100644
--- a/src/local_deep_research/research_library/services/library_rag_service.py
+++ b/src/local_deep_research/research_library/services/library_rag_service.py
@@ -386,8 +386,11 @@ class LibraryRAGService:
"source": document.original_url,
"document_id": document_id, # Add document ID for source linking
"collection_id": collection_id, # Add collection ID
- "title": document.title or "Untitled",
+ "title": document.title
+ or document.filename
+ or "Untitled",
"document_title": document.title
+ or document.filename
or "Untitled", # Add for compatibility
"authors": document.authors,
"published_date": str(document.published_date)
diff --git a/src/local_deep_research/text_processing/__init__.py b/src/local_deep_research/text_processing/__init__.py
new file mode 100644
index 0000000000..befa6c60f5
--- /dev/null
+++ b/src/local_deep_research/text_processing/__init__.py
@@ -0,0 +1,11 @@
+"""
+Text Processing Utilities
+
+Provides utilities for text cleaning and normalization.
+"""
+
+from .text_cleaner import remove_surrogates
+
+__all__ = [
+ "remove_surrogates",
+]
diff --git a/src/local_deep_research/text_processing/text_cleaner.py b/src/local_deep_research/text_processing/text_cleaner.py
new file mode 100644
index 0000000000..c9d285afed
--- /dev/null
+++ b/src/local_deep_research/text_processing/text_cleaner.py
@@ -0,0 +1,39 @@
+"""
+Text Cleaning Utilities
+
+Provides functions for cleaning text with encoding issues.
+"""
+
+from loguru import logger
+
+
+def remove_surrogates(text: str) -> str:
+ """
+ Remove surrogate characters that can't be encoded in UTF-8.
+
+ PDF extraction and other text extraction methods can produce malformed Unicode
+ with surrogate characters. This function ensures the text is safe for UTF-8 encoding.
+
+ Args:
+ text: Raw text that may contain surrogate characters
+
+ Returns:
+ Cleaned text safe for UTF-8 encoding
+ """
+ if not text:
+ return text
+
+ try:
+ # Encode with surrogatepass to handle surrogates, then decode with replace
+ # to convert any remaining issues to replacement characters
+ return text.encode("utf-8", errors="surrogatepass").decode(
+ "utf-8", errors="replace"
+ )
+ except Exception as e:
+ logger.warning(
+ f"Error cleaning text with surrogatepass: {e}, using fallback"
+ )
+ # Fallback: ignore any characters that can't be encoded
+ return text.encode("utf-8", errors="ignore").decode(
+ "utf-8", errors="ignore"
+ )
diff --git a/src/local_deep_research/web/static/css/collection_details.css b/src/local_deep_research/web/static/css/collection_details.css
index 7daed15bf5..32df872dd4 100644
--- a/src/local_deep_research/web/static/css/collection_details.css
+++ b/src/local_deep_research/web/static/css/collection_details.css
@@ -691,6 +691,14 @@
border-color: #4a5568;
}
+ .ldr-document-item .ldr-document-title {
+ color: #f7fafc;
+ }
+
+ .ldr-document-item .ldr-document-meta {
+ color: #cbd5e0;
+ }
+
.ldr-progress-bar {
background: #4a5568;
}
@@ -706,6 +714,28 @@
color: #f7fafc;
border-color: #4a5568;
}
+
+ .ldr-embedding-settings-card {
+ background: rgba(45, 55, 72, 0.95) !important;
+ border-color: #4a5568 !important;
+ }
+
+ .ldr-embedding-info h4 {
+ color: #f7fafc !important;
+ }
+
+ .ldr-embedding-info .ldr-info-box {
+ background: rgba(26, 32, 44, 0.8) !important;
+ border-left-color: #667eea !important;
+ }
+
+ .ldr-embedding-info .ldr-info-label {
+ color: #cbd5e0 !important;
+ }
+
+ .ldr-embedding-info .ldr-info-value {
+ color: #f7fafc !important;
+ }
}
/* Drop Zone Styles */
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_library.py b/src/local_deep_research/web_search_engines/engines/search_engine_library.py
index cc652d39b5..8ec2f2dc44 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_library.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_library.py
@@ -12,7 +12,7 @@ from ...research_library.services.library_rag_service import LibraryRAGService
from ...research_library.services.library_service import LibraryService
from ...config.thread_settings import get_setting_from_snapshot
from ...utilities.llm_utils import get_server_url
-from ...database.models.library import RAGIndex
+from ...database.models.library import RAGIndex, Document
from ...database.session_context import get_user_db_session
@@ -228,15 +228,37 @@ class LibraryRAGSearchEngine(BaseSearchEngine):
else doc.page_content
)
- # Generate full URL using server URL
- # Use original_url if available, otherwise link to document page
- original_url = metadata.get("source")
- if original_url and original_url not in ("#", "", None):
- document_url = original_url
- elif doc_id:
- # Ensure proper URL format (server_url should not end with /)
- base_url = self.server_url.rstrip("/")
- document_url = f"{base_url}/library/document/{doc_id}"
+ # Generate URL to document content (PDF or TXT)
+ # Use relative URLs - they work in any deployment scenario
+ if doc_id:
+ try:
+ with get_user_db_session(self.username) as session:
+ document = (
+ session.query(Document)
+ .filter_by(id=doc_id)
+ .first()
+ )
+ if document:
+ # Prefer PDF if available, otherwise TXT
+ if (
+ document.file_type == "pdf"
+ and document.file_path
+ ):
+ document_url = (
+ f"/library/document/{doc_id}/pdf"
+ )
+ else:
+ document_url = (
+ f"/library/document/{doc_id}/txt"
+ )
+ else:
+ # Fallback to TXT if document not found
+ document_url = f"/library/document/{doc_id}/txt"
+ except Exception as e:
+ logger.warning(
+ f"Error querying document {doc_id}: {e}, using TXT fallback"
+ )
+ document_url = f"/library/document/{doc_id}/txt"
else:
document_url = "#"