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: background indexing with cancel button and progress tracking

LearningCircuit9b36518db8ab
diff --git a/.gitleaks.toml b/.gitleaks.toml
index 5ca97aa6b8..9031ca7c5e 100644
--- a/.gitleaks.toml
+++ b/.gitleaks.toml
@@ -110,7 +110,10 @@ paths = [
# JavaScript files use csrfToken variable names (not actual secrets)
'''src/local_deep_research/web/static/js/.*\.js$''',
# Database session context uses password variable names (not actual secrets)
- '''src/local_deep_research/database/session_context\.py$'''
+ '''src/local_deep_research/database/session_context\.py$''',
+ # RAG routes and services use db_password variable names (not actual secrets)
+ '''src/local_deep_research/research_library/routes/rag_routes\.py$''',
+ '''src/local_deep_research/research_library/services/library_rag_service\.py$'''
]
files = [
"\\.env\\.example$",
@@ -200,7 +203,15 @@ regexes = [
# Thread context password retrieval (session_context.py)
'''thread_context\.get\("user_password"\)''',
'''thread_context\["user_password"\]''',
- '''if not password:'''
+ '''if not password:''',
+ # Database password variable patterns (not actual secrets)
+ '''db_password\s*=\s*session_password_store\.get_session_password''',
+ '''db_password\s*=\s*db_password''',
+ '''db_password=db_password''',
+ '''db_password:\s*Optional\[str\]''',
+ '''self\._db_password\s*=\s*db_password''',
+ '''embedding_manager\.db_password\s*=\s*db_password''',
+ '''rag_service\.db_password\s*=\s*db_password'''
]
# Target specific files for scanning
diff --git a/.pre-commit-hooks/custom-checks.py b/.pre-commit-hooks/custom-checks.py
index d8c78d7150..bb50ea3c5b 100755
--- a/.pre-commit-hooks/custom-checks.py
+++ b/.pre-commit-hooks/custom-checks.py
@@ -266,6 +266,7 @@ def check_raw_sql(content: str, filename: str) -> List[Tuple[int, str]]:
or "socket_service.py" in filename
or "thread_local_session.py" in filename
or "encrypted_db.py" in filename
+ or "initialize.py" in filename
)
# Allow raw SQL in migrations, db utils, and all test files
@@ -295,6 +296,7 @@ def check_raw_sql(content: str, filename: str) -> List[Tuple[int, str]]:
or "socket_service.py" in filename
or "thread_local_session.py" in filename
or "encrypted_db.py" in filename
+ or "initialize.py" in filename
)
# Allow raw SQL in migrations, db utils, and all test files
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index 53eddd51f7..ce21db34b9 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -405,10 +405,13 @@ class DatabaseManager:
# Store connection
self.connections[username] = engine
- # Note: Since v0.x databases are not migratable, we don't attempt
- # to add new tables to existing databases. Users will need to
- # recreate their database to get new features.
- # Future versions will use Alembic for proper migrations.
+ # Run database initialization (creates missing tables and runs migrations)
+ from .initialize import initialize_database
+
+ try:
+ initialize_database(engine)
+ except Exception as e:
+ logger.warning(f"Could not run migrations for {username}: {e}")
logger.info(f"Opened encrypted database for user {username}")
return engine
diff --git a/src/local_deep_research/database/initialize.py b/src/local_deep_research/database/initialize.py
index 172c6ff038..00ff239fc5 100644
--- a/src/local_deep_research/database/initialize.py
+++ b/src/local_deep_research/database/initialize.py
@@ -45,6 +45,9 @@ def initialize_database(
logger.info("Creating database tables")
Base.metadata.create_all(engine, checkfirst=True)
+ # Run migrations for existing tables
+ _run_migrations(engine)
+
# Check what was created (need new inspector to avoid caching)
new_inspector = inspect(engine)
new_tables = new_inspector.get_table_names()
@@ -125,3 +128,91 @@ def check_database_schema(engine: Engine) -> dict:
break
return schema_info
+
+
+def _add_column_if_not_exists(
+ engine: Engine,
+ table_name: str,
+ column_name: str,
+ column_type: str,
+ default: str = None,
+) -> bool:
+ """
+ Add a column to a table if it doesn't already exist.
+
+ Uses SQLAlchemy's DDL capabilities for dialect-aware column addition.
+
+ Args:
+ engine: SQLAlchemy engine
+ table_name: Name of the table to modify
+ column_name: Name of the column to add
+ column_type: SQLAlchemy-compatible type string (e.g., 'INTEGER', 'TEXT')
+ default: Optional default value clause
+
+ Returns:
+ True if column was added, False if it already existed
+ """
+ from sqlalchemy.schema import CreateColumn, Column
+ from sqlalchemy import Integer, String
+
+ inspector = inspect(engine)
+ existing_columns = {
+ col["name"] for col in inspector.get_columns(table_name)
+ }
+
+ if column_name in existing_columns:
+ return False
+
+ # Build column definition using SQLAlchemy types
+ type_map = {
+ "INTEGER": Integer(),
+ "TEXT": String(),
+ }
+ col_type = type_map.get(column_type.upper(), String())
+ column = Column(column_name, col_type)
+
+ # Use CreateColumn to get dialect-aware DDL
+ compiled = CreateColumn(column).compile(dialect=engine.dialect)
+ column_def = str(compiled).strip()
+
+ # Add default clause if specified
+ if default is not None:
+ column_def = f"{column_def} DEFAULT {default}"
+
+ try:
+ with engine.begin() as conn:
+ # Use DDL class for proper execution
+ from sqlalchemy import DDL
+
+ ddl = DDL(f"ALTER TABLE {table_name} ADD {column_def}")
+ conn.execute(ddl)
+ logger.info(f"Added column {column_name} to {table_name} table")
+ return True
+ except Exception as e:
+ logger.debug(f"Migration for {column_name} skipped: {e}")
+ return False
+
+
+def _run_migrations(engine: Engine) -> None:
+ """
+ Run database migrations to add missing columns to existing tables.
+
+ This is a simple migration system for adding new columns.
+ For more complex migrations, consider using Alembic.
+ """
+ inspector = inspect(engine)
+
+ # Migration: Add progress tracking columns to task_metadata
+ if inspector.has_table("task_metadata"):
+ _add_column_if_not_exists(
+ engine, "task_metadata", "progress_current", "INTEGER", "0"
+ )
+ _add_column_if_not_exists(
+ engine, "task_metadata", "progress_total", "INTEGER", "0"
+ )
+ _add_column_if_not_exists(
+ engine, "task_metadata", "progress_message", "TEXT"
+ )
+ _add_column_if_not_exists(
+ engine, "task_metadata", "metadata_json", "TEXT"
+ )
diff --git a/src/local_deep_research/database/models/queue.py b/src/local_deep_research/database/models/queue.py
index e9f79399f5..81ea526af4 100644
--- a/src/local_deep_research/database/models/queue.py
+++ b/src/local_deep_research/database/models/queue.py
@@ -2,7 +2,7 @@
Queue-related database models stored in each user's encrypted database.
"""
-from sqlalchemy import Column, Integer, String
+from sqlalchemy import Column, Integer, String, JSON
from sqlalchemy_utc import UtcDateTime, utcnow
from .base import Base
@@ -40,3 +40,11 @@ class TaskMetadata(Base):
priority = Column(Integer, default=0) # Higher number = higher priority
retry_count = Column(Integer, default=0)
max_retries = Column(Integer, default=3)
+
+ # Progress tracking for long-running tasks
+ progress_current = Column(Integer, default=0)
+ progress_total = Column(Integer, default=0)
+ progress_message = Column(String, nullable=True)
+ metadata_json = Column(
+ JSON, nullable=True
+ ) # Store task-specific data (e.g., collection_id)
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 ee2c5b6f60..177419308f 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -21,6 +21,10 @@ from loguru import logger
import glob
import json
import uuid
+import time
+import threading
+import queue
+from datetime import datetime, UTC
from pathlib import Path
from typing import Optional
@@ -37,6 +41,8 @@ from ...database.models.library import (
SourceType,
EmbeddingProvider,
)
+from ...database.models.queue import TaskMetadata
+from ...web.utils.rate_limiter import limiter
rag_bp = Blueprint("rag", __name__, url_prefix="/library")
@@ -93,9 +99,16 @@ def get_rag_service(collection_id: Optional[str] = None) -> LibraryRAGService:
default_distance_metric = settings.get_setting(
"local_search_distance_metric", "cosine"
)
- default_normalize_vectors = settings.get_setting(
- "local_search_normalize_vectors", True
- )
+ # Ensure normalize_vectors is a proper boolean (settings may return string)
+ raw_normalize = settings.get_setting("local_search_normalize_vectors", True)
+ if isinstance(raw_normalize, str):
+ default_normalize_vectors = raw_normalize.lower() in (
+ "true",
+ "1",
+ "yes",
+ )
+ else:
+ default_normalize_vectors = bool(raw_normalize)
default_index_type = settings.get_setting("local_search_index_type", "flat")
# If collection_id provided, check for stored settings
@@ -111,6 +124,20 @@ def get_rag_service(collection_id: Optional[str] = None) -> LibraryRAGService:
f"Using stored settings for collection {collection_id}: "
f"{collection.embedding_model_type.value}/{collection.embedding_model}"
)
+ # Handle normalize_vectors - may be stored as string in some cases
+ coll_normalize = collection.normalize_vectors
+ if coll_normalize is not None:
+ if isinstance(coll_normalize, str):
+ coll_normalize = coll_normalize.lower() in (
+ "true",
+ "1",
+ "yes",
+ )
+ else:
+ coll_normalize = bool(coll_normalize)
+ else:
+ coll_normalize = default_normalize_vectors
+
return LibraryRAGService(
username=username,
embedding_model=collection.embedding_model,
@@ -124,9 +151,7 @@ def get_rag_service(collection_id: Optional[str] = None) -> LibraryRAGService:
or default_text_separators,
distance_metric=collection.distance_metric
or default_distance_metric,
- normalize_vectors=collection.normalize_vectors
- if collection.normalize_vectors is not None
- else default_normalize_vectors,
+ normalize_vectors=coll_normalize,
index_type=collection.index_type or default_index_type,
)
elif collection:
@@ -367,7 +392,6 @@ def get_current_settings():
@login_required
def test_embedding():
"""Test an embedding configuration by generating a test embedding."""
- import time
try:
data = request.json
@@ -1906,17 +1930,34 @@ def get_collection_documents(collection_id):
def index_collection(collection_id):
"""Index all documents in a collection with Server-Sent Events progress."""
from ...database.session_context import get_user_db_session
+ from ...database.session_passwords import session_password_store
force_reindex = request.args.get("force_reindex", "false").lower() == "true"
username = session["username"]
+ session_id = session.get("session_id")
+
+ logger.info(f"Starting index_collection, force_reindex={force_reindex}")
+
+ # Get password for thread access to encrypted database
+ db_password = None
+ if session_id:
+ db_password = session_password_store.get_session_password(
+ username, session_id
+ )
# Create RAG service
rag_service = get_rag_service(collection_id)
+ # Set password for thread access
+ rag_service.db_password = db_password
+ logger.info(
+ f"RAG service created: provider={rag_service.embedding_provider}"
+ )
def generate():
"""Generator for SSE progress updates."""
+ logger.info("SSE generator started")
try:
- with get_user_db_session(username) as db_session:
+ with get_user_db_session(username, db_password) as db_session:
# Verify collection exists in this user's database
collection = (
db_session.query(Collection)
@@ -1955,11 +1996,14 @@ def index_collection(collection_id):
collection.splitter_type = rag_service.splitter_type
collection.text_separators = rag_service.text_separators
collection.distance_metric = rag_service.distance_metric
- collection.normalize_vectors = rag_service.normalize_vectors
+ # Ensure normalize_vectors is a proper boolean for database
+ collection.normalize_vectors = bool(
+ rag_service.normalize_vectors
+ )
collection.index_type = rag_service.index_type
db_session.commit()
logger.info(
- f"Stored embedding metadata for collection {collection.id}: {rag_service.embedding_provider}/{rag_service.embedding_model}"
+ f"Stored embedding metadata for collection: provider={rag_service.embedding_provider}"
)
# Get documents to index
@@ -1981,10 +2025,12 @@ def index_collection(collection_id):
doc_links = query.all()
if not doc_links:
+ logger.info("No documents to index in collection")
yield f"data: {json.dumps({'type': 'complete', 'results': {'successful': 0, 'skipped': 0, 'failed': 0, 'message': 'No documents to index'}})}\n\n"
return
total = len(doc_links)
+ logger.info(f"Found {total} documents to index")
results = {
"successful": 0,
"skipped": 0,
@@ -1999,17 +2045,41 @@ def index_collection(collection_id):
yield f"data: {json.dumps({'type': 'progress', 'current': idx, 'total': total, 'filename': filename, 'percent': int((idx / total) * 100)})}\n\n"
try:
- logger.info(
- f"Indexing document {idx}/{total}: {filename}"
- )
- # Index the document using collection-based method
- result = rag_service.index_document(
- document_id=doc.id,
- collection_id=collection_id,
- force_reindex=force_reindex,
- )
- logger.info(
- f"Indexed document {idx}/{total}: {filename} - status={result.get('status')}"
+ logger.debug(f"Indexing document {idx}/{total}")
+
+ # Use a queue to run index_document in a thread while sending heartbeats
+ result_queue = queue.Queue()
+ error_queue = queue.Queue()
+
+ def index_in_thread():
+ try:
+ r = rag_service.index_document(
+ document_id=doc.id,
+ collection_id=collection_id,
+ force_reindex=force_reindex,
+ )
+ result_queue.put(r)
+ except Exception as ex:
+ error_queue.put(ex)
+
+ thread = threading.Thread(target=index_in_thread)
+ thread.start()
+
+ # Send heartbeats while waiting for the thread to complete
+ heartbeat_interval = 5 # seconds
+ while thread.is_alive():
+ thread.join(timeout=heartbeat_interval)
+ if thread.is_alive():
+ # Send SSE comment as heartbeat (keeps connection alive)
+ yield f": heartbeat {idx}/{total}\n\n"
+
+ # Check for errors from thread
+ if not error_queue.empty():
+ raise error_queue.get()
+
+ result = result_queue.get()
+ logger.debug(
+ f"Indexed document {idx}/{total}, status={result.get('status')}"
)
if result.get("status") == "success":
@@ -2028,7 +2098,7 @@ def index_collection(collection_id):
}
)
logger.warning(
- f"Failed to index {filename}: {error_msg}"
+ f"Failed to index document {idx}/{total}: {error_msg}"
)
except Exception as e:
results["failed"] += 1
@@ -2040,7 +2110,7 @@ def index_collection(collection_id):
}
)
logger.exception(
- f"Exception indexing document {filename}"
+ f"Exception indexing document {idx}/{total}"
)
# Send error update to client so they know indexing is continuing
yield f"data: {json.dumps({'type': 'doc_error', 'filename': filename, 'error': error_msg})}\n\n"
@@ -2049,12 +2119,625 @@ def index_collection(collection_id):
# Ensure all changes are written to disk
db_session.flush()
+ logger.info(
+ f"Indexing complete: {results['successful']} successful, {results['failed']} failed, {results['skipped']} skipped"
+ )
yield f"data: {json.dumps({'type': 'complete', 'results': results})}\n\n"
+ logger.info("SSE generator finished successfully")
except Exception:
logger.exception("Error in collection indexing")
yield f"data: {json.dumps({'type': 'error', 'error': 'An internal error occurred during indexing'})}\n\n"
- return Response(
+ response = Response(
stream_with_context(generate()), mimetype="text/event-stream"
)
+ # Prevent buffering for proper SSE streaming
+ response.headers["Cache-Control"] = "no-cache, no-transform"
+ response.headers["Connection"] = "keep-alive"
+ response.headers["X-Accel-Buffering"] = "no"
+ return response
+
+
+# =============================================================================
+# Background Indexing Endpoints
+# =============================================================================
+
+
+def _get_rag_service_for_thread(
+ collection_id: str,
+ username: str,
+ db_password: str,
+) -> LibraryRAGService:
+ """
+ Create RAG service for use in background threads (no Flask context).
+ """
+ from ...database.session_context import get_user_db_session
+ from ...settings.manager import SettingsManager
+ from ...web_search_engines.engines.search_engine_local import (
+ LocalEmbeddingManager,
+ )
+ import json
+
+ with get_user_db_session(username, db_password) as db_session:
+ settings_manager = SettingsManager(db_session)
+
+ # Get default settings
+ default_embedding_model = settings_manager.get_setting(
+ "local_search_embedding_model", "all-MiniLM-L6-v2"
+ )
+ default_embedding_provider = settings_manager.get_setting(
+ "local_search_embedding_provider", "sentence_transformers"
+ )
+ default_chunk_size = int(
+ settings_manager.get_setting("local_search_chunk_size", 1000)
+ )
+ default_chunk_overlap = int(
+ settings_manager.get_setting("local_search_chunk_overlap", 200)
+ )
+ default_splitter_type = settings_manager.get_setting(
+ "local_search_splitter_type", "recursive"
+ )
+ default_text_separators = settings_manager.get_setting(
+ "local_search_text_separators", '["\n\n", "\n", ". ", " ", ""]'
+ )
+ if isinstance(default_text_separators, str):
+ try:
+ default_text_separators = json.loads(default_text_separators)
+ except json.JSONDecodeError:
+ default_text_separators = ["\n\n", "\n", ". ", " ", ""]
+ default_distance_metric = settings_manager.get_setting(
+ "local_search_distance_metric", "cosine"
+ )
+ raw_normalize = settings_manager.get_setting(
+ "local_search_normalize_vectors", True
+ )
+ if isinstance(raw_normalize, str):
+ default_normalize_vectors = raw_normalize.lower() in (
+ "true",
+ "1",
+ "yes",
+ )
+ else:
+ default_normalize_vectors = bool(raw_normalize)
+ default_index_type = settings_manager.get_setting(
+ "local_search_index_type", "flat"
+ )
+
+ # Get settings snapshot for embedding manager
+ settings_snapshot = settings_manager.get_settings_snapshot()
+ settings_snapshot["_username"] = username
+
+ # Check for collection's stored settings
+ collection = (
+ db_session.query(Collection).filter_by(id=collection_id).first()
+ )
+
+ if collection and collection.embedding_model:
+ # Use collection's stored settings
+ embedding_model = collection.embedding_model
+ embedding_provider = collection.embedding_model_type.value
+ chunk_size = collection.chunk_size or default_chunk_size
+ chunk_overlap = collection.chunk_overlap or default_chunk_overlap
+ splitter_type = collection.splitter_type or default_splitter_type
+ text_separators = (
+ collection.text_separators or default_text_separators
+ )
+ distance_metric = (
+ collection.distance_metric or default_distance_metric
+ )
+ index_type = collection.index_type or default_index_type
+
+ coll_normalize = collection.normalize_vectors
+ if coll_normalize is not None:
+ if isinstance(coll_normalize, str):
+ coll_normalize = coll_normalize.lower() in (
+ "true",
+ "1",
+ "yes",
+ )
+ else:
+ coll_normalize = bool(coll_normalize)
+ else:
+ coll_normalize = default_normalize_vectors
+ normalize_vectors = coll_normalize
+ else:
+ # Use default settings
+ embedding_model = default_embedding_model
+ embedding_provider = default_embedding_provider
+ chunk_size = default_chunk_size
+ chunk_overlap = default_chunk_overlap
+ splitter_type = default_splitter_type
+ text_separators = default_text_separators
+ distance_metric = default_distance_metric
+ normalize_vectors = default_normalize_vectors
+ index_type = default_index_type
+
+ # Update settings snapshot with embedding config
+ settings_snapshot.update(
+ {
+ "embeddings.provider": embedding_provider,
+ f"embeddings.{embedding_provider}.model": embedding_model,
+ "local_search_chunk_size": chunk_size,
+ "local_search_chunk_overlap": chunk_overlap,
+ }
+ )
+
+ # Create embedding manager (to avoid database access in LibraryRAGService.__init__)
+ embedding_manager = LocalEmbeddingManager(
+ embedding_model=embedding_model,
+ embedding_model_type=embedding_provider,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ settings_snapshot=settings_snapshot,
+ )
+ embedding_manager.db_password = db_password
+
+ # Create RAG service with pre-built embedding manager and db_password
+ rag_service = LibraryRAGService(
+ username=username,
+ embedding_model=embedding_model,
+ embedding_provider=embedding_provider,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ splitter_type=splitter_type,
+ text_separators=text_separators,
+ distance_metric=distance_metric,
+ normalize_vectors=normalize_vectors,
+ index_type=index_type,
+ embedding_manager=embedding_manager,
+ db_password=db_password,
+ )
+
+ return rag_service
+
+
+def _background_index_worker(
+ task_id: str,
+ collection_id: str,
+ username: str,
+ db_password: str,
+ force_reindex: bool,
+):
+ """
+ Background worker thread for indexing documents.
+ Updates TaskMetadata with progress and checks for cancellation.
+ """
+ from ...database.session_context import get_user_db_session
+
+ try:
+ # Create RAG service (thread-safe, no Flask context needed)
+ rag_service = _get_rag_service_for_thread(
+ collection_id, username, db_password
+ )
+
+ with get_user_db_session(username, db_password) as db_session:
+ # Get collection
+ collection = (
+ db_session.query(Collection).filter_by(id=collection_id).first()
+ )
+
+ if not collection:
+ _update_task_status(
+ username,
+ db_password,
+ task_id,
+ status="failed",
+ error_message="Collection not found",
+ )
+ return
+
+ # Store embedding metadata if first time
+ if collection.embedding_model is None:
+ collection.embedding_model = rag_service.embedding_model
+ collection.embedding_model_type = EmbeddingProvider(
+ rag_service.embedding_provider
+ )
+ collection.chunk_size = rag_service.chunk_size
+ collection.chunk_overlap = rag_service.chunk_overlap
+ collection.splitter_type = rag_service.splitter_type
+ collection.text_separators = rag_service.text_separators
+ collection.distance_metric = rag_service.distance_metric
+ collection.normalize_vectors = bool(
+ rag_service.normalize_vectors
+ )
+ collection.index_type = rag_service.index_type
+ db_session.commit()
+
+ # Get documents to index
+ query = (
+ db_session.query(DocumentCollection, Document)
+ .join(Document)
+ .filter(DocumentCollection.collection_id == collection_id)
+ )
+
+ if not force_reindex:
+ query = query.filter(DocumentCollection.indexed == False) # noqa: E712
+
+ doc_links = query.all()
+
+ if not doc_links:
+ _update_task_status(
+ username,
+ db_password,
+ task_id,
+ status="completed",
+ progress_message="No documents to index",
+ )
+ return
+
+ total = len(doc_links)
+ results = {"successful": 0, "skipped": 0, "failed": 0}
+
+ # Update task with total count
+ _update_task_status(
+ username,
+ db_password,
+ task_id,
+ progress_total=total,
+ progress_message=f"Indexing {total} documents",
+ )
+
+ for idx, (link, doc) in enumerate(doc_links, 1):
+ # Check if cancelled
+ if _is_task_cancelled(username, db_password, task_id):
+ _update_task_status(
+ username,
+ db_password,
+ task_id,
+ status="cancelled",
+ progress_message=f"Cancelled after {idx - 1}/{total} documents",
+ )
+ logger.info(f"Indexing task {task_id} was cancelled")
+ return
+
+ filename = doc.filename or doc.title or "Unknown"
+
+ # Update progress with filename
+ _update_task_status(
+ username,
+ db_password,
+ task_id,
+ progress_current=idx,
+ progress_message=f"Indexing {idx}/{total}: {filename}",
+ )
+
+ try:
+ result = rag_service.index_document(
+ document_id=doc.id,
+ collection_id=collection_id,
+ force_reindex=force_reindex,
+ )
+
+ if result.get("status") == "success":
+ results["successful"] += 1
+ elif result.get("status") == "skipped":
+ results["skipped"] += 1
+ else:
+ results["failed"] += 1
+
+ except Exception:
+ results["failed"] += 1
+ logger.exception(f"Error indexing document {idx}/{total}")
+
+ db_session.commit()
+
+ # Mark as completed
+ _update_task_status(
+ username,
+ db_password,
+ task_id,
+ status="completed",
+ progress_current=total,
+ progress_message=f"Completed: {results['successful']} indexed, {results['failed']} failed, {results['skipped']} skipped",
+ )
+ logger.info(f"Background indexing task {task_id} completed: {results}")
+
+ except Exception as e:
+ logger.exception(f"Background indexing task {task_id} failed")
+ _update_task_status(
+ username,
+ db_password,
+ task_id,
+ status="failed",
+ error_message=str(e),
+ )
+
+
+def _update_task_status(
+ username: str,
+ db_password: str,
+ task_id: str,
+ status: str = None,
+ progress_current: int = None,
+ progress_total: int = None,
+ progress_message: str = None,
+ error_message: str = None,
+):
+ """Update task metadata in the database."""
+ from ...database.session_context import get_user_db_session
+
+ try:
+ with get_user_db_session(username, db_password) as db_session:
+ task = (
+ db_session.query(TaskMetadata)
+ .filter_by(task_id=task_id)
+ .first()
+ )
+ if task:
+ if status is not None:
+ task.status = status
+ if status == "completed":
+ task.completed_at = datetime.now(UTC)
+ if progress_current is not None:
+ task.progress_current = progress_current
+ if progress_total is not None:
+ task.progress_total = progress_total
+ if progress_message is not None:
+ task.progress_message = progress_message
+ if error_message is not None:
+ task.error_message = error_message
+ db_session.commit()
+ except Exception:
+ logger.exception(f"Failed to update task status for {task_id}")
+
+
+def _is_task_cancelled(username: str, db_password: str, task_id: str) -> bool:
+ """Check if a task has been cancelled."""
+ from ...database.session_context import get_user_db_session
+
+ try:
+ with get_user_db_session(username, db_password) as db_session:
+ task = (
+ db_session.query(TaskMetadata)
+ .filter_by(task_id=task_id)
+ .first()
+ )
+ return task and task.status == "cancelled"
+ except Exception:
+ return False
+
+
+@rag_bp.route(
+ "/api/collections/<string:collection_id>/index/start", methods=["POST"]
+)
+@login_required
+def start_background_index(collection_id):
+ """Start background indexing for a collection."""
+ from ...database.session_context import get_user_db_session
+ from ...database.session_passwords import session_password_store
+
+ username = session["username"]
+ session_id = session.get("session_id")
+
+ # Get password for thread access
+ db_password = None
+ if session_id:
+ db_password = session_password_store.get_session_password(
+ username, session_id
+ )
+
+ # Parse request body
+ data = request.get_json() or {}
+ force_reindex = data.get("force_reindex", False)
+
+ try:
+ with get_user_db_session(username, db_password) as db_session:
+ # Check if there's already an active indexing task for this collection
+ existing_task = (
+ db_session.query(TaskMetadata)
+ .filter(
+ TaskMetadata.task_type == "indexing",
+ TaskMetadata.status == "processing",
+ )
+ .first()
+ )
+
+ if existing_task:
+ # Check if it's for this collection
+ metadata = existing_task.metadata_json or {}
+ if metadata.get("collection_id") == collection_id:
+ return jsonify(
+ {
+ "success": False,
+ "error": "Indexing is already in progress for this collection",
+ "task_id": existing_task.task_id,
+ }
+ ), 409
+
+ # Create new task
+ task_id = str(uuid.uuid4())
+ task = TaskMetadata(
+ task_id=task_id,
+ status="processing",
+ task_type="indexing",
+ created_at=datetime.now(UTC),
+ started_at=datetime.now(UTC),
+ progress_current=0,
+ progress_total=0,
+ progress_message="Starting indexing...",
+ metadata_json={
+ "collection_id": collection_id,
+ "force_reindex": force_reindex,
+ },
+ )
+ db_session.add(task)
+ db_session.commit()
+
+ # Start background thread
+ thread = threading.Thread(
+ target=_background_index_worker,
+ args=(task_id, collection_id, username, db_password, force_reindex),
+ daemon=True,
+ )
+ thread.start()
+
+ logger.info(
+ f"Started background indexing task {task_id} for collection {collection_id}"
+ )
+
+ return jsonify(
+ {
+ "success": True,
+ "task_id": task_id,
+ "message": "Indexing started in background",
+ }
+ )
+
+ except Exception as e:
+ logger.exception("Failed to start background indexing")
+ return jsonify(
+ {
+ "success": False,
+ "error": str(e),
+ }
+ ), 500
+
+
+@rag_bp.route(
+ "/api/collections/<string:collection_id>/index/status", methods=["GET"]
+)
+@login_required
+def get_index_status(collection_id):
+ """Get the current indexing status for a collection."""
+ from ...database.session_context import get_user_db_session
+ from ...database.session_passwords import session_password_store
+
+ username = session["username"]
+ session_id = session.get("session_id")
+
+ db_password = None
+ if session_id:
+ db_password = session_password_store.get_session_password(
+ username, session_id
+ )
+
+ try:
+ with get_user_db_session(username, db_password) as db_session:
+ # Find the most recent indexing task for this collection
+ task = (
+ db_session.query(TaskMetadata)
+ .filter(TaskMetadata.task_type == "indexing")
+ .order_by(TaskMetadata.created_at.desc())
+ .first()
+ )
+
+ if not task:
+ return jsonify(
+ {
+ "status": "idle",
+ "message": "No indexing task found",
+ }
+ )
+
+ # Check if it's for this collection
+ metadata = task.metadata_json or {}
+ if metadata.get("collection_id") != collection_id:
+ return jsonify(
+ {
+ "status": "idle",
+ "message": "No indexing task for this collection",
+ }
+ )
+
+ return jsonify(
+ {
+ "task_id": task.task_id,
+ "status": task.status,
+ "progress_current": task.progress_current or 0,
+ "progress_total": task.progress_total or 0,
+ "progress_message": task.progress_message,
+ "error_message": task.error_message,
+ "created_at": task.created_at.isoformat()
+ if task.created_at
+ else None,
+ "completed_at": task.completed_at.isoformat()
+ if task.completed_at
+ else None,
+ }
+ )
+
+ except Exception as e:
+ logger.exception("Failed to get index status")
+ return jsonify(
+ {
+ "status": "error",
+ "error": str(e),
+ }
+ ), 500
+
+
+@rag_bp.route(
+ "/api/collections/<string:collection_id>/index/cancel", methods=["POST"]
+)
+@login_required
+def cancel_indexing(collection_id):
+ """Cancel an active indexing task for a collection."""
+ from ...database.session_context import get_user_db_session
+ from ...database.session_passwords import session_password_store
+
+ username = session["username"]
+ session_id = session.get("session_id")
+
+ db_password = None
+ if session_id:
+ db_password = session_password_store.get_session_password(
+ username, session_id
+ )
+
+ try:
+ with get_user_db_session(username, db_password) as db_session:
+ # Find active indexing task for this collection
+ task = (
+ db_session.query(TaskMetadata)
+ .filter(
+ TaskMetadata.task_type == "indexing",
+ TaskMetadata.status == "processing",
+ )
+ .first()
+ )
+
+ if not task:
+ return jsonify(
+ {
+ "success": False,
+ "error": "No active indexing task found",
+ }
+ ), 404
+
+ # Check if it's for this collection
+ metadata = task.metadata_json or {}
+ if metadata.get("collection_id") != collection_id:
+ return jsonify(
+ {
+ "success": False,
+ "error": "No active indexing task for this collection",
+ }
+ ), 404
+
+ # Mark as cancelled - the worker thread will check this
+ task.status = "cancelled"
+ task.progress_message = "Cancellation requested..."
+ db_session.commit()
+
+ logger.info(
+ f"Cancelled indexing task {task.task_id} for collection {collection_id}"
+ )
+
+ return jsonify(
+ {
+ "success": True,
+ "message": "Cancellation requested",
+ "task_id": task.task_id,
+ }
+ )
+
+ except Exception as e:
+ logger.exception("Failed to cancel indexing")
+ return jsonify(
+ {
+ "success": False,
+ "error": str(e),
+ }
+ ), 500
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 19d6af8eda..46cdfc920b 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
@@ -53,6 +53,7 @@ class LibraryRAGService:
normalize_vectors: bool = True,
index_type: str = "flat",
embedding_manager: Optional["LocalEmbeddingManager"] = None,
+ db_password: Optional[str] = None,
):
"""
Initialize library RAG service for a user.
@@ -69,8 +70,10 @@ class LibraryRAGService:
normalize_vectors: Whether to normalize vectors with L2
index_type: FAISS index type ('flat', 'hnsw', or 'ivf')
embedding_manager: Optional pre-constructed LocalEmbeddingManager for testing/flexibility
+ db_password: Optional database password for background thread access
"""
self.username = username
+ self._db_password = db_password # Can be used for thread access
self.embedding_model = embedding_model
self.embedding_provider = embedding_provider
self.chunk_size = chunk_size
@@ -82,7 +85,15 @@ class LibraryRAGService:
else ["\n\n", "\n", ". ", " ", ""]
)
self.distance_metric = distance_metric
- self.normalize_vectors = normalize_vectors
+ # Ensure normalize_vectors is always a proper boolean
+ if isinstance(normalize_vectors, str):
+ self.normalize_vectors = normalize_vectors.lower() in (
+ "true",
+ "1",
+ "yes",
+ )
+ else:
+ self.normalize_vectors = bool(normalize_vectors)
self.index_type = index_type
# Use provided embedding manager or create a new one
@@ -135,9 +146,25 @@ class LibraryRAGService:
self.rag_index_record = None
# Initialize file integrity manager for FAISS indexes
- self.integrity_manager = FileIntegrityManager(username)
+ self.integrity_manager = FileIntegrityManager(
+ username, password=self._db_password
+ )
self.integrity_manager.register_verifier(FAISSIndexVerifier())
+ @property
+ def db_password(self):
+ """Get database password."""
+ return self._db_password
+
+ @db_password.setter
+ def db_password(self, value):
+ """Set database password and propagate to embedding manager and integrity manager."""
+ self._db_password = value
+ if hasattr(self, "embedding_manager") and self.embedding_manager:
+ self.embedding_manager.db_password = value
+ if hasattr(self, "integrity_manager") and self.integrity_manager:
+ self.integrity_manager.password = value
+
def _get_index_hash(
self,
collection_name: str,
@@ -161,7 +188,7 @@ class LibraryRAGService:
def _get_or_create_rag_index(self, collection_id: str) -> RAGIndex:
"""Get or create RAGIndex record for the current configuration."""
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(self.username, self.db_password) as session:
# Use collection_<uuid> format
collection_name = f"collection_{collection_id}"
index_hash = self._get_index_hash(
@@ -242,18 +269,77 @@ class LibraryRAGService:
logger.exception("Failed to remove corrupted index")
else:
try:
- # Load existing FAISS index (checksum verified)
- faiss_index = FAISS.load_local(
- str(index_path.parent),
- self.embedding_manager.embeddings,
- index_name=index_path.stem,
- allow_dangerous_deserialization=True,
- normalize_L2=True,
- )
- logger.info(
- f"Loaded existing FAISS index from {index_path}"
+ # Check for embedding dimension mismatch before loading
+ current_dim = len(
+ self.embedding_manager.embeddings.embed_query(
+ "dimension_check"
+ )
)
- return faiss_index
+ stored_dim = rag_index.embedding_dimension
+
+ if stored_dim and current_dim != stored_dim:
+ logger.warning(
+ f"Embedding dimension mismatch detected! "
+ f"Index created with dim={stored_dim}, "
+ f"current model returns dim={current_dim}. "
+ f"Deleting old index and rebuilding."
+ )
+ # Delete old index files
+ try:
+ index_path.unlink()
+ pkl_path = index_path.with_suffix(".pkl")
+ if pkl_path.exists():
+ pkl_path.unlink()
+ logger.info(
+ f"Deleted old FAISS index files at {index_path}"
+ )
+ except Exception:
+ logger.exception("Failed to delete old index files")
+
+ # Update RAGIndex with new dimension and reset counts
+ with get_user_db_session(
+ self.username, self.db_password
+ ) as session:
+ idx = (
+ session.query(RAGIndex)
+ .filter_by(id=rag_index.id)
+ .first()
+ )
+ if idx:
+ idx.embedding_dimension = current_dim
+ idx.chunk_count = 0
+ idx.total_documents = 0
+ session.commit()
+ logger.info(
+ f"Updated RAGIndex dimension to {current_dim}"
+ )
+
+ # Clear rag_document_status for this index
+ session.query(RagDocumentStatus).filter_by(
+ rag_index_id=rag_index.id
+ ).delete()
+ session.commit()
+ logger.info(
+ "Cleared indexed status for documents in this "
+ "collection"
+ )
+
+ # Update local reference for index creation below
+ rag_index.embedding_dimension = current_dim
+ # Fall through to create new index below
+ else:
+ # Dimensions match (or no stored dimension), load index
+ faiss_index = FAISS.load_local(
+ str(index_path.parent),
+ self.embedding_manager.embeddings,
+ index_name=index_path.stem,
+ allow_dangerous_deserialization=True,
+ normalize_L2=True,
+ )
+ logger.info(
+ f"Loaded existing FAISS index from {index_path}"
+ )
+ return faiss_index
except Exception as e:
logger.warning(
f"Failed to load FAISS index: {e}, creating new one"
@@ -313,7 +399,7 @@ class LibraryRAGService:
Args:
collection_id: UUID of collection (defaults to Library if None)
"""
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(self.username, self.db_password) as session:
# Get collection name in the format stored in RAGIndex (collection_<uuid>)
if collection_id:
collection = (
@@ -389,7 +475,7 @@ class LibraryRAGService:
Returns:
Dict with status, chunk_count, and any errors
"""
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(self.username, self.db_password) as session:
# Get the document
document = session.query(Document).filter_by(id=document_id).first()
@@ -669,7 +755,7 @@ class LibraryRAGService:
Returns:
Dict with counts of successful, skipped, and failed documents
"""
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(self.username, self.db_password) as session:
# Get all DocumentCollection entries for this collection
query = session.query(DocumentCollection).filter_by(
collection_id=collection_id
@@ -746,7 +832,7 @@ class LibraryRAGService:
Returns:
Dict with status and count of removed chunks
"""
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(self.username, self.db_password) as session:
# Get the DocumentCollection entry
doc_collection = (
session.query(DocumentCollection)
@@ -820,7 +906,7 @@ class LibraryRAGService:
doc_ids = [doc_id for doc_id, _ in doc_info]
# Use single database session for querying
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(self.username, self.db_password) as session:
# Pre-load all documents for this batch
documents = (
session.query(Document).filter(Document.id.in_(doc_ids)).all()
@@ -904,7 +990,7 @@ class LibraryRAGService:
Returns:
Dict with counts and metadata about indexed documents
"""
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(self.username, self.db_password) as session:
# Get collection ID (default to Library)
if not collection_id:
from ...database.library_init import get_default_library_id
@@ -1268,7 +1354,9 @@ class LibraryRAGService:
from ...database.session_context import get_user_db_session
try:
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(
+ self.username, self.db_password
+ ) as session:
# Get all chunk IDs for this collection
chunks = (
session.query(DocumentChunk)
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 fe1668d6eb..b4abe900b2 100644
--- a/src/local_deep_research/web/static/css/collection_details.css
+++ b/src/local_deep_research/web/static/css/collection_details.css
@@ -391,8 +391,10 @@
.ldr-progress-text {
text-align: center;
- color: #4a5568;
- font-weight: 500;
+ color: #2b6cb0;
+ font-weight: 600;
+ font-size: 1.1rem;
+ margin-top: 10px;
}
.ldr-progress-log {
@@ -536,17 +538,18 @@
/* Log Entry Styles */
.ldr-log-entry {
- padding: 4px 8px;
- margin: 2px 0;
+ padding: 8px 12px;
+ margin: 4px 0;
border-radius: 4px;
font-family: 'Courier New', monospace;
- font-size: 0.85rem;
+ font-size: 0.95rem;
+ font-weight: 500;
}
.ldr-log-info {
- background: rgba(66, 153, 225, 0.1);
- color: #2c5282;
- border-left: 3px solid #4299e1;
+ background: rgba(66, 153, 225, 0.15);
+ color: #1a4971;
+ border-left: 4px solid #3182ce;
}
.ldr-log-success {
@@ -788,28 +791,98 @@
.ldr-embedding-info .ldr-info-value {
color: #f7fafc !important;
}
+
+ .ldr-progress-text {
+ color: #63b3ed;
+ }
+
+ .ldr-log-info {
+ background: rgba(99, 179, 237, 0.2);
+ color: #90cdf4;
+ border-left-color: #4299e1;
+ }
+
+ .ldr-log-entry {
+ color: #e2e8f0;
+ }
}
/* Drop Zone Styles */
.ldr-drop-zone {
- border: 2px dashed #ccc;
- border-radius: 8px;
+ border: 3px dashed #667eea;
+ border-radius: 12px;
padding: 40px 20px;
text-align: center;
- background-color: rgba(248, 249, 250, 0.5);
- transition: all 0.3s ease;
+ background-color: rgba(102, 126, 234, 0.05);
cursor: pointer;
+ position: relative;
+ /* No transition - instant feedback */
}
.ldr-drop-zone:hover {
- border-color: #007bff;
- background-color: #e9ecef;
+ border-color: #764ba2;
+ background-color: rgba(118, 75, 162, 0.1);
}
+/* Drag-over state - INSTANT, very visible */
.ldr-drop-zone.ldr-drag-over {
border-color: #28a745;
- background-color: #d4edda;
border-style: solid;
+ border-width: 4px;
+ background: linear-gradient(135deg, rgba(40, 167, 69, 0.15) 0%, rgba(32, 201, 151, 0.15) 100%);
+ box-shadow: 0 0 20px rgba(40, 167, 69, 0.3), inset 0 0 30px rgba(40, 167, 69, 0.1);
+}
+
+/* Spinner appears when dragging */
+.ldr-drop-zone.ldr-drag-over::before {
+ content: '';
+ position: absolute;
+ top: 50%;
+ left: 50%;
+ width: 50px;
+ height: 50px;
+ margin: -25px 0 0 -25px;
+ border: 4px solid rgba(40, 167, 69, 0.2);
+ border-top-color: #28a745;
+ border-radius: 50%;
+ animation: drop-spinner 0.6s linear infinite;
+ z-index: 10;
+}
+
+/* Pulsing glow effect */
+.ldr-drop-zone.ldr-drag-over::after {
+ content: 'Drop files here!';
+ position: absolute;
+ bottom: 15px;
+ left: 0;
+ right: 0;
+ font-size: 1.1rem;
+ font-weight: 600;
+ color: #28a745;
+ animation: pulse-text 0.8s ease-in-out infinite alternate;
+}
+
+@keyframes drop-spinner {
+ to { transform: rotate(360deg); }
+}
+
+@keyframes pulse-text {
+ from { opacity: 0.7; }
+ to { opacity: 1; }
+}
+
+/* Hide normal content when dragging */
+.ldr-drop-zone.ldr-drag-over .ldr-drop-zone-icon,
+.ldr-drop-zone.ldr-drag-over .ldr-drop-zone-text,
+.ldr-drop-zone.ldr-drag-over .ldr-drop-zone-subtext,
+.ldr-drop-zone.ldr-drag-over .ldr-btn-collections {
+ opacity: 0.3;
+}
+
+.ldr-drop-zone-icon {
+ font-size: 3rem;
+ color: #667eea;
+ margin-bottom: 10px;
}
.ldr-drop-zone-text {
diff --git a/src/local_deep_research/web/static/js/collection_details.js b/src/local_deep_research/web/static/js/collection_details.js
index f1b2264930..6deedecdca 100644
--- a/src/local_deep_research/web/static/js/collection_details.js
+++ b/src/local_deep_research/web/static/js/collection_details.js
@@ -6,6 +6,7 @@
let collectionData = null;
let documentsData = [];
let currentFilter = 'all';
+let indexingPollInterval = null;
/**
* Safe fetch wrapper with URL validation
@@ -30,6 +31,10 @@ document.addEventListener('DOMContentLoaded', function() {
document.getElementById('index-collection-btn').addEventListener('click', () => indexCollection(false));
document.getElementById('reindex-collection-btn').addEventListener('click', () => indexCollection(true));
document.getElementById('delete-collection-btn').addEventListener('click', deleteCollection);
+ document.getElementById('cancel-indexing-btn').addEventListener('click', cancelIndexing);
+
+ // Check if there's an active indexing task
+ checkAndResumeIndexing();
});
/**
@@ -253,95 +258,196 @@ function filterDocuments(filter) {
/**
- * Index collection documents
+ * Index collection documents (background indexing)
*/
async function indexCollection(forceReindex) {
- console.log('🚀 Index Collection button clicked!');
- console.log('📋 Force reindex:', forceReindex);
- console.log('🆔 Collection ID:', COLLECTION_ID);
+ console.log('Index Collection button clicked, force_reindex:', forceReindex);
const action = forceReindex ? 're-index' : 'index';
if (!confirm(`${action.charAt(0).toUpperCase() + action.slice(1)} all documents in this collection?`)) {
- console.log('❌ User cancelled indexing');
return;
}
- console.log('✅ User confirmed indexing');
+ try {
+ const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';
+
+ // Start background indexing
+ const response = await safeFetch(`/library/api/collections/${COLLECTION_ID}/index/start`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-CSRFToken': csrfToken
+ },
+ body: JSON.stringify({ force_reindex: forceReindex })
+ });
+
+ const data = await response.json();
+
+ if (!data.success) {
+ if (response.status === 409) {
+ // Already indexing
+ showError(data.error || 'Indexing is already in progress');
+ showProgressUI();
+ startPolling();
+ } else {
+ showError(data.error || 'Failed to start indexing');
+ }
+ return;
+ }
+
+ console.log('Background indexing started, task_id:', data.task_id);
+
+ // Show progress UI and start polling
+ showProgressUI();
+ addLogEntry('Indexing started in background...', 'info');
+ startPolling();
+ } catch (error) {
+ console.error('Error starting indexing:', error);
+ showError('Failed to start indexing');
+ }
+}
+
+/**
+ * Check if there's an active indexing task and resume UI
+ */
+async function checkAndResumeIndexing() {
+ try {
+ const response = await safeFetch(`/library/api/collections/${COLLECTION_ID}/index/status`);
+ const data = await response.json();
+
+ if (data.status === 'processing') {
+ console.log('Active indexing task found, resuming UI');
+ showProgressUI();
+ updateProgressFromStatus(data);
+ startPolling();
+ }
+ } catch (error) {
+ console.error('Error checking indexing status:', error);
+ }
+}
+
+/**
+ * Show the progress UI
+ */
+function showProgressUI() {
const progressSection = document.getElementById('indexing-progress');
+ const cancelBtn = document.getElementById('cancel-indexing-btn');
+ const indexBtn = document.getElementById('index-collection-btn');
+ const reindexBtn = document.getElementById('reindex-collection-btn');
+
+ progressSection.style.display = 'block';
+ cancelBtn.style.display = 'inline-block';
+ indexBtn.disabled = true;
+ reindexBtn.disabled = true;
+}
+
+/**
+ * Hide the progress UI
+ */
+function hideProgressUI() {
+ const progressSection = document.getElementById('indexing-progress');
+ const cancelBtn = document.getElementById('cancel-indexing-btn');
+ const indexBtn = document.getElementById('index-collection-btn');
+ const reindexBtn = document.getElementById('reindex-collection-btn');
+ const spinner = document.getElementById('indexing-spinner');
+
+ cancelBtn.style.display = 'none';
+ indexBtn.disabled = false;
+ reindexBtn.disabled = false;
+
+ // Keep progress visible for a few seconds before hiding
+ setTimeout(() => {
+ progressSection.style.display = 'none';
+ }, 5000);
+}
+
+/**
+ * Start polling for indexing status
+ */
+function startPolling() {
+ // Clear any existing interval
+ if (indexingPollInterval) {
+ clearInterval(indexingPollInterval);
+ }
+
+ // Poll every 2 seconds
+ indexingPollInterval = setInterval(async () => {
+ try {
+ const response = await safeFetch(`/library/api/collections/${COLLECTION_ID}/index/status`);
+ const data = await response.json();
+
+ updateProgressFromStatus(data);
+
+ // Stop polling if indexing is done
+ if (['completed', 'failed', 'cancelled', 'idle'].includes(data.status)) {
+ clearInterval(indexingPollInterval);
+ indexingPollInterval = null;
+
+ if (data.status === 'completed') {
+ addLogEntry(data.progress_message || 'Indexing completed!', 'success');
+ } else if (data.status === 'failed') {
+ addLogEntry(`Indexing failed: ${data.error_message || 'Unknown error'}`, 'error');
+ } else if (data.status === 'cancelled') {
+ addLogEntry('Indexing was cancelled', 'warning');
+ }
+
+ hideProgressUI();
+ loadCollectionDetails();
+ }
+ } catch (error) {
+ console.error('Error polling status:', error);
+ }
+ }, 2000);
+}
+
+/**
+ * Update progress UI from status data
+ */
+function updateProgressFromStatus(data) {
const progressFill = document.getElementById('progress-fill');
const progressText = document.getElementById('progress-text');
- const progressLog = document.getElementById('progress-log');
- console.log('🎯 UI elements found:', {
- progressSection: !!progressSection,
- progressFill: !!progressFill,
- progressText: !!progressText,
- progressLog: !!progressLog
- });
+ if (data.progress_total > 0) {
+ const percent = Math.round((data.progress_current / data.progress_total) * 100);
+ progressFill.style.width = percent + '%';
+ }
+
+ if (data.progress_message) {
+ progressText.textContent = data.progress_message;
+ }
+}
+
+/**
+ * Cancel indexing
+ */
+async function cancelIndexing() {
+ if (!confirm('Cancel the current indexing operation?')) {
+ return;
+ }
try {
- // Show progress section
- progressSection.style.display = 'block';
- progressFill.style.width = '0%';
- progressText.textContent = 'Starting...';
- progressLog.innerHTML = '';
-
- // Use Server-Sent Events for progress updates
- const url = URLBuilder.build(URLS.LIBRARY_API.COLLECTION_INDEX, COLLECTION_ID) + `?force_reindex=${forceReindex}`;
- console.log('🌐 Indexing URL:', url);
-
- console.log('📡 Creating EventSource...');
- const eventSource = new EventSource(url);
- console.log('📡 EventSource created:', eventSource);
-
- eventSource.onmessage = function(event) {
- console.log('📨 EventSource message received:', event.data);
- const data = JSON.parse(event.data);
- console.log('📨 Parsed data:', data);
-
- if (data.type === 'start') {
- console.log('▶️ Indexing started:', data.message);
- progressText.textContent = data.message;
- addLogEntry(data.message, 'info');
- } else if (data.type === 'progress') {
- console.log('📊 Indexing progress:', data);
- const percent = data.percent || 0;
- progressFill.style.width = percent + '%';
- progressText.textContent = `Processing ${data.current}/${data.total}: ${data.filename}`;
- addLogEntry(`[${data.current}/${data.total}] ${data.filename}`, 'info');
- } else if (data.type === 'complete') {
- console.log('✅ Indexing complete:', data.results);
- const results = data.results;
- progressFill.style.width = '100%';
- progressText.textContent = `Complete! ${results.successful} indexed, ${results.failed} failed.`;
- addLogEntry(`Indexing complete! ${results.successful} documents indexed, ${results.failed} failed.`, 'success');
-
- eventSource.close();
-
- // Reload collection details after a delay
- setTimeout(() => {
- loadCollectionDetails();
- progressSection.style.display = 'none';
- }, 5000);
- } else if (data.type === 'error') {
- console.log('❌ Indexing error:', data.error);
- addLogEntry(`Error: ${data.error}`, 'error');
- progressText.textContent = 'Indexing failed';
- eventSource.close();
+ const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';
+
+ const response = await safeFetch(`/library/api/collections/${COLLECTION_ID}/index/cancel`, {
+ method: 'POST',
+ headers: {
+ 'X-CSRFToken': csrfToken
}
- };
-
- eventSource.onerror = function(error) {
- console.error('❌ EventSource error:', error);
- console.error('❌ EventSource readyState:', eventSource.readyState);
- addLogEntry('Connection error during indexing', 'error');
- progressText.textContent = 'Indexing failed';
- eventSource.close();
- };
+ });
+
+ const data = await response.json();
+
+ if (data.success) {
+ const progressText = document.getElementById('progress-text');
+ progressText.textContent = 'Cancelling...';
+ addLogEntry('Cancellation requested...', 'warning');
+ } else {
+ showError(data.error || 'Failed to cancel indexing');
+ }
} catch (error) {
- console.error('Error indexing collection:', error);
- showError('Failed to index collection');
+ console.error('Error cancelling indexing:', error);
+ showError('Failed to cancel indexing');
}
}
diff --git a/src/local_deep_research/web/templates/pages/collection_details.html b/src/local_deep_research/web/templates/pages/collection_details.html
index 7d1f939d9a..9c95dab0cd 100644
--- a/src/local_deep_research/web/templates/pages/collection_details.html
+++ b/src/local_deep_research/web/templates/pages/collection_details.html
@@ -96,7 +96,10 @@
<!-- Indexing Progress -->
<div id="indexing-progress" class="ldr-rag-section" style="display: none;">
<div class="ldr-section-header">
- <h2><i class="fas fa-spinner fa-spin"></i> Indexing Progress</h2>
+ <h2><i class="fas fa-spinner fa-spin" id="indexing-spinner"></i> Indexing Progress</h2>
+ <button id="cancel-indexing-btn" class="ldr-btn-collections ldr-btn-collections-danger" style="display: none;">
+ <i class="fas fa-times"></i> Cancel Indexing
+ </button>
</div>
<div class="ldr-progress-container">
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_local.py b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
index 4d05f10923..3c33ecf6b4 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_local.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
@@ -140,6 +140,8 @@ class LocalEmbeddingManager:
self.username = (
settings_snapshot.get("_username") if settings_snapshot else None
)
+ # Password for encrypted database access (can be set later)
+ self.db_password = None
# Create cache directory if it doesn't exist
self.cache_dir.mkdir(parents=True, exist_ok=True)
@@ -236,7 +238,9 @@ class LocalEmbeddingManager:
chunk_ids = []
try:
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(
+ self.username, self.db_password
+ ) as session:
for idx, chunk in enumerate(chunks):
# Generate unique hash for chunk
chunk_text = chunk.page_content
@@ -392,7 +396,9 @@ class LocalEmbeddingManager:
return 0
try:
- with get_user_db_session(self.username) as session:
+ with get_user_db_session(
+ self.username, self.db_password
+ ) as session:
query = session.query(DocumentChunk).filter_by(
collection_name=collection_name
)