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 AI code review feedback

LearningCircuit0deb113ab424
diff --git a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
index 31e3fd0a0b..b22c498e11 100644
--- a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
@@ -1,3 +1,4 @@
+import random
import uuid
from typing import Dict, List, Any, Optional
@@ -168,8 +169,6 @@ class TopicOrganizationStrategy(BaseSearchStrategy):
# Shuffle sources to avoid ordering bias in topic organization
# Note: random.shuffle is used here for non-security purposes (preventing ML bias)
- import random
-
sources_to_process = (
sources_to_process.copy()
) # Create a copy to avoid modifying original
diff --git a/src/local_deep_research/research_library/routes/library_routes.py b/src/local_deep_research/research_library/routes/library_routes.py
index 415ab6df0c..862a1fbb98 100644
--- a/src/local_deep_research/research_library/routes/library_routes.py
+++ b/src/local_deep_research/research_library/routes/library_routes.py
@@ -28,7 +28,7 @@ from ...database.models.research import ResearchResource
from ...database.models.library import LibraryDocument, LibraryDownloadQueue
from ..services.download_service import DownloadService
from ..services.library_service import LibraryService
-from ..utils import open_file_location
+from ..utils import open_file_location, handle_api_error
from ...security.path_validator import PathValidator
from ...utilities.db_utils import get_settings_manager
@@ -36,33 +36,6 @@ from ...utilities.db_utils import get_settings_manager
library_bp = Blueprint("library", __name__, url_prefix="/library")
-def handle_api_error(operation: str, error: Exception, status_code: int = 500):
- """
- Handle API errors consistently - log internally, return generic message to user.
-
- This prevents information exposure by logging full error details internally
- while returning a generic message to the user.
-
- Args:
- operation: Description of the operation that failed (for logging)
- error: The exception that occurred
- status_code: HTTP status code to return (default: 500)
-
- Returns:
- Flask JSON response tuple (response, status_code)
- """
- # Log the full error internally with stack trace
- logger.exception(f"Error during {operation}")
-
- # Return generic message to user (no internal details exposed)
- return jsonify(
- {
- "success": False,
- "error": "An internal error occurred. Please try again or contact support.",
- }
- ), status_code
-
-
def is_downloadable_domain(url: str) -> bool:
"""Check if URL is from a downloadable academic domain using proper URL parsing."""
try:
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 bac961081a..22ced5fb2e 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -18,6 +18,7 @@ from flask import (
stream_with_context,
)
from loguru import logger
+import glob
import json
import uuid
from pathlib import Path
@@ -28,6 +29,7 @@ from ...web.auth.decorators import login_required
from ...utilities.db_utils import get_settings_manager
from ..services.library_rag_service import LibraryRAGService
from ...security.path_validator import PathValidator
+from ..utils import handle_api_error
rag_bp = Blueprint("rag", __name__, url_prefix="/library")
@@ -94,7 +96,7 @@ def validate_local_index_path(user_path: str) -> Path:
# CodeQL: This is intentional - local file indexing on user's own machine.
# Users are allowed to index their own filesystem. System directories are blocked below.
if Path(user_path).is_absolute():
- # lgtm[py/path-injection] - Intentional: Local filesystem indexing feature
+ # codeql[py/path-injection] - Intentional: Local filesystem indexing feature with system directory protection
validated_path = Path(user_path).resolve() # nosec B108
else:
# Use PathValidator for relative paths
@@ -131,33 +133,6 @@ def validate_local_index_path(user_path: str) -> Path:
return validated_path
-def handle_api_error(operation: str, error: Exception, status_code: int = 500):
- """
- Handle API errors consistently - log internally, return generic message to user.
-
- This prevents information exposure by logging full error details internally
- while returning a generic message to the user.
-
- Args:
- operation: Description of the operation that failed (for logging)
- error: The exception that occurred
- status_code: HTTP status code to return (default: 500)
-
- Returns:
- Flask JSON response tuple (response, status_code)
- """
- # Log the full error internally with stack trace
- logger.exception(f"Error during {operation}")
-
- # Return generic message to user (no internal details exposed)
- return jsonify(
- {
- "success": False,
- "error": "An internal error occurred. Please try again or contact support.",
- }
- ), status_code
-
-
# Page Routes
@@ -561,8 +536,6 @@ def get_documents():
@login_required
def index_local_library():
"""Index documents from a local folder with Server-Sent Events progress."""
- import glob
-
folder_path = request.args.get("path")
file_patterns = request.args.get(
"patterns", "*.pdf,*.txt,*.md,*.html"
diff --git a/src/local_deep_research/research_library/utils/__init__.py b/src/local_deep_research/research_library/utils/__init__.py
index 12f473a24d..85fcb1dc61 100644
--- a/src/local_deep_research/research_library/utils/__init__.py
+++ b/src/local_deep_research/research_library/utils/__init__.py
@@ -6,6 +6,7 @@ import subprocess
import sys
from pathlib import Path
+from flask import jsonify
from loguru import logger
@@ -156,3 +157,30 @@ def get_absolute_path_from_settings(relative_path: str) -> Path:
return library_root
return library_root / relative_path
+
+
+def handle_api_error(operation: str, error: Exception, status_code: int = 500):
+ """
+ Handle API errors consistently - log internally, return generic message to user.
+
+ This prevents information exposure by logging full error details internally
+ while returning a generic message to the user.
+
+ Args:
+ operation: Description of the operation that failed (for logging)
+ error: The exception that occurred
+ status_code: HTTP status code to return (default: 500)
+
+ Returns:
+ Flask JSON response tuple (response, status_code)
+ """
+ # Log the full error internally with stack trace
+ logger.exception(f"Error during {operation}")
+
+ # Return generic message to user (no internal details exposed)
+ return jsonify(
+ {
+ "success": False,
+ "error": "An internal error occurred. Please try again or contact support.",
+ }
+ ), status_code