OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
fix: make onboarding not loop when error occurs in ingestion, fix double tasks for OpenRAG docs (#1244) (#1248)
Lucas Oliveira8ed287910c73
diff --git a/frontend/app/api/mutations/useOnboardingRollbackMutation.ts b/frontend/app/api/mutations/useOnboardingRollbackMutation.tsindex b53ded1f7..272193279 100644--- a/frontend/app/api/mutations/useOnboardingRollbackMutation.ts+++ b/frontend/app/api/mutations/useOnboardingRollbackMutation.ts@@ -10,20 +10,33 @@ interface OnboardingRollbackResponse {deleted_files: number;}+interface RollbackParams {+ embedding_only?: boolean;+}+export const useOnboardingRollbackMutation = (options?: Omit<- UseMutationOptions<OnboardingRollbackResponse, Error, void>,+ UseMutationOptions<+ OnboardingRollbackResponse,+ Error,+ RollbackParams | void+ >,"mutationFn">,) => {const queryClient = useQueryClient();- async function rollbackOnboarding(): Promise<OnboardingRollbackResponse> {+ async function rollbackOnboarding(+ params: RollbackParams | void,+ ): Promise<OnboardingRollbackResponse> {+ const requestBody = params || { embedding_only: false };+const response = await fetch("/api/onboarding/rollback", {method: "POST",headers: {"Content-Type": "application/json",},+ body: JSON.stringify(requestBody),});if (!response.ok) {diff --git a/frontend/app/onboarding/_components/onboarding-card.tsx b/frontend/app/onboarding/_components/onboarding-card.tsxindex 182f318f7..fe5e5a85b 100644--- a/frontend/app/onboarding/_components/onboarding-card.tsx+++ b/frontend/app/onboarding/_components/onboarding-card.tsx@@ -233,7 +233,7 @@ const OnboardingCard = ({onError: (error) => {setError(error.message);setCurrentStep(totalSteps);- rollbackMutation.mutate();+ rollbackMutation.mutate({ embedding_only: isEmbedding });},});@@ -328,14 +328,16 @@ const OnboardingCard = ({// Set error message and jump back one step (exactly like onboardingMutation.onError)setError(errorMessage);setCurrentStep(totalSteps);- rollbackMutation.mutate();+ rollbackMutation.mutate({ embedding_only: isEmbedding });return;}- const hasSuccessfulTasks = relevantTasks.length > 0 &&+ const hasSuccessfulTasks =+ relevantTasks.length > 0 &&(!activeTasks || (activeTasks.successful_files ?? 0) > 0);- const hasIngestionDisabledOrDone = !onboardingTaskId && currentStep === totalSteps - 1;+ const hasIngestionDisabledOrDone =+ !onboardingTaskId && currentStep === totalSteps - 1;// If at least one processed file, no failures, and we've started onboarding, complete itif (diff --git a/frontend/contexts/task-context.tsx b/frontend/contexts/task-context.tsxindex d2b9c61bc..7eb8b4012 100644--- a/frontend/contexts/task-context.tsx+++ b/frontend/contexts/task-context.tsx@@ -359,9 +359,11 @@ export function TaskProvider({ children }: { children: React.ReactNode }) {!isTerminalFailedTask(previousTask) &&isTerminalFailedTask(currentTask)) {- selectTask(currentTask.task_id);- setIsMenuOpen(true);- setIsRecentTasksExpanded(true);+ if (!isOnboardingActive()) {+ selectTask(currentTask.task_id);+ setIsMenuOpen(true);+ setIsRecentTasksExpanded(true);+ }// Task just failed - show error toasttoast.error("Task failed", {description: `Task ${currentTask.task_id} failed: ${diff --git a/src/api/settings.py b/src/api/settings.pyindex 36bad4925..a23b1a7c7 100644--- a/src/api/settings.py+++ b/src/api/settings.py@@ -209,6 +209,9 @@ class RollbackResponse(BaseModel):cancelled_tasks: intdeleted_files: int+class RollbackBody(BaseModel):+ embedding_only: bool = False+# Docling preset configurationsdef get_docling_preset_configs(@@ -1604,8 +1607,10 @@ async def reapply_all_settings(session_manager = None):async def rollback_onboarding(request: Request,+ body: Optional[RollbackBody] = None,session_manager=Depends(get_session_manager),task_service=Depends(get_task_service),+ knowledge_filter_service=Depends(get_knowledge_filter_service),user: User = Depends(get_current_user),) -> RollbackResponse:"""Rollback onboarding configuration when sample data files fail.@@ -1635,7 +1640,36 @@ async def rollback_onboarding(cancelled_tasks = []deleted_files = []+ # Delete knowledge filters created during onboarding+ try:+ async def remove_filter(filter_id: Optional[str]):+ if filter_id and knowledge_filter_service:+ try:+ result = await knowledge_filter_service.delete_knowledge_filter(+ filter_id, user.user_id, user.jwt_token+ )+ if result and result.get("success"):+ logger.info(f"Deleted knowledge filter {filter_id}")+ else:+ error_msg = result.get("error") if result else "Unknown error"+ logger.warning(f"Could not delete knowledge filter {filter_id}: {error_msg}")+ except Exception as e:+ logger.warning(f"Exception deleting knowledge filter {filter_id}: {str(e)}")++ if getattr(current_config.onboarding, 'openrag_docs_filter_id', None):+ await remove_filter(current_config.onboarding.openrag_docs_filter_id)+ current_config.onboarding.openrag_docs_filter_id = None++ if getattr(current_config.onboarding, 'user_doc_filter_id', None):+ await remove_filter(current_config.onboarding.user_doc_filter_id)+ current_config.onboarding.user_doc_filter_id = None+ except Exception as e:+ logger.error(f"Error while cleaning up knowledge filters: {e}")+# Cancel all active tasks and collect successfully ingested files+ from session_manager import AnonymousUser+ anonymous_user_id = AnonymousUser().user_id+for task_data in all_tasks:task_id = task_data.get("task_id")task_status = task_data.get("status")@@ -1650,41 +1684,40 @@ async def rollback_onboarding(except Exception as e:logger.error(f"Failed to cancel task {task_id}: {str(e)}")- # For completed tasks, find successfully ingested files and delete them- elif task_status == "completed":- files = task_data.get("files", {})- if isinstance(files, dict):- for file_path, file_info in files.items():- # Check if file was successfully ingested- if isinstance(file_info, dict):- file_status = file_info.get("status")- filename = file_info.get("filename") or file_path.split("/")[-1]-- if file_status == "completed" and filename:- try:- # Get user's OpenSearch client- opensearch_client = session_manager.get_user_opensearch_client(- user.user_id, jwt_token- )-- # Delete documents by filename- from utils.opensearch_queries import build_filename_delete_body- from config.settings import get_index_name-- delete_query = build_filename_delete_body(filename)-- result = await opensearch_client.delete_by_query(- index=get_index_name(),- body=delete_query,- conflicts="proceed"- )-- deleted_count = result.get("deleted", 0)- if deleted_count > 0:- deleted_files.append(filename)- logger.info(f"Deleted {deleted_count} chunks for filename {filename}")- except Exception as e:- logger.error(f"Failed to delete documents for {filename}: {str(e)}")+ # Delete all files associated with any task, regardless of whether+ # the task failed or completed, to ensure no partial chunks remain in OpenSearch.+ files = task_data.get("files", {})+ if isinstance(files, dict):+ for file_path, file_info in files.items():+ if isinstance(file_info, dict):+ filename = file_info.get("filename") or file_path.split("/")[-1]+ if filename:+ try:+ opensearch_client = session_manager.get_user_opensearch_client(+ user.user_id, jwt_token+ )+ from utils.opensearch_queries import build_filename_delete_body+ from config.settings import get_index_name++ delete_query = build_filename_delete_body(filename)+ result = await opensearch_client.delete_by_query(+ index=get_index_name(),+ body=delete_query,+ conflicts="proceed"+ )+ deleted_count = result.get("deleted", 0)+ if deleted_count > 0:+ deleted_files.append(filename)+ logger.info(f"Deleted {deleted_count} chunks for filename {filename}")+ except Exception as e:+ logger.error(f"Failed to delete documents for {filename}: {str(e)}")++ # Wipe the task completely from memory so the frontend doesn't see it anymore+ for check_user_id in [user.user_id, anonymous_user_id]:+ if check_user_id in task_service.task_store and task_id in task_service.task_store[check_user_id]:+ task_service._task_locks.pop(task_id, None)+ task_service.task_store[check_user_id].pop(task_id, None)+ logger.info(f"Purged task {task_id} completely from task_store for user {check_user_id}")# Clear embedding provider and model settingscurrent_config.knowledge.embedding_provider = "openai" # Reset to default@@ -1692,11 +1725,21 @@ async def rollback_onboarding(current_config.onboarding.openrag_docs_ingested_version = Nonecurrent_config.onboarding.openrag_docs_remote_signature = None+ embedding_only = body.embedding_only if body else False+# Mark config as not edited so user can go through onboarding again- current_config.edited = False- current_config.onboarding.current_step = 0+ if not embedding_only:+ current_config.edited = False+ current_config.onboarding.current_step = 0+ # Also clear LLM provider and model settings when doing a full rollback+ current_config.agent.llm_provider = "openai" # Reset to default+ current_config.agent.llm_model = ""+ else:+ # When rolling back embedding only, we keep edited=True+ # and set current_step to 1 (which is the embedding step)+ current_config.onboarding.current_step = 1- # Save the rolled back configuration manually to avoid save_config_file setting edited=True+ # Save the rolled back configuration manuallytry:import yamlconfig_file = config_manager.config_file@@ -1704,14 +1747,14 @@ async def rollback_onboarding(# Ensure directory existsconfig_file.parent.mkdir(parents=True, exist_ok=True)- # Save config with edited=False+ # Save config with current edited statewith open(config_file, "w") as f:yaml.dump(current_config.to_dict(), f, default_flow_style=False, indent=2)# Update cached configconfig_manager._config = current_config- logger.info("Successfully saved rolled back configuration with edited=False")+ logger.info(f"Successfully saved rolled back configuration with edited={current_config.edited}")except Exception as e:logger.error(f"Failed to save rolled back configuration: {e}")return JSONResponse(diff --git a/src/main.py b/src/main.pyindex 8f59edf56..2586d849d 100644--- a/src/main.py+++ b/src/main.py@@ -412,13 +412,14 @@ async def ingest_openrag_docs_when_ready():"""Ingest OpenRAG docs during onboarding."""use_url_ingest = _should_use_url_default_docs_ingest()+ task_id = Noneif use_url_ingest:try:await TelemetryClient.send_event(Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_URL_START)if DISABLE_INGEST_WITH_LANGFLOW:- await _ingest_default_documents_url(+ task_id = await _ingest_default_documents_url(document_service=document_service,docs_url=DEFAULT_DOCS_URL,crawl_depth=DEFAULT_DOCS_CRAWL_DEPTH,@@ -428,7 +429,7 @@ async def ingest_openrag_docs_when_ready("Ingesting default documents using Langflow",docs_url=DEFAULT_DOCS_URL,)- await _ingest_default_documents_url_langflow(+ task_id = await _ingest_default_documents_url_langflow(langflow_file_service=langflow_file_service,session_manager=session_manager,task_service=task_service,@@ -443,6 +444,7 @@ async def ingest_openrag_docs_when_ready(await TelemetryClient.send_event(Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_URL_FAILED)+ return task_idasync def ingest_default_documents_when_ready(@@ -458,9 +460,11 @@ async def ingest_default_documents_when_ready(await TelemetryClient.send_event(Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_START)- await ingest_openrag_docs_when_ready(- document_service, task_service, langflow_file_service, session_manager- )+ task_id = None+ if _should_use_url_default_docs_ingest():+ task_id = await ingest_openrag_docs_when_ready(+ document_service, task_service, langflow_file_service, session_manager+ )base_dir = _get_documents_dir()if not os.path.isdir(base_dir):@@ -471,8 +475,7 @@ async def ingest_default_documents_when_ready(excluded_files = set(EXCLUDED_INGESTION_FILES)if _should_use_url_default_docs_ingest():excluded_files.update(URL_INGEST_EXCLUDED_INGESTION_FILES)-- # Collect files recursively, excluding warmup files and URL-ingested docs+file_paths = [os.path.join(root, fn)for root, _, files in os.walk(base_dir)@@ -484,18 +487,22 @@ async def ingest_default_documents_when_ready(raise FileNotFoundError(f"No default documents found in {base_dir}")if DISABLE_INGEST_WITH_LANGFLOW:- await _ingest_default_documents_openrag(- document_service, task_service, file_paths+ new_task_id = await _ingest_default_documents_openrag(+ document_service, task_service, file_paths, existing_task_id=task_id, connector_type="local")+ task_id = new_task_id or task_idelse:- await _ingest_default_documents_langflow(- langflow_file_service, session_manager, task_service, file_paths+ new_task_id = await _ingest_default_documents_langflow(+ langflow_file_service, session_manager, task_service, file_paths, existing_task_id=task_id, connector_type="local")+ task_id = new_task_id or task_idawait TelemetryClient.send_event(Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_COMPLETE)+ return task_id+except Exception as e:logger.error("Default documents ingestion failed", error=str(e))await TelemetryClient.send_event(@@ -505,7 +512,7 @@ async def ingest_default_documents_when_ready(async def _ingest_default_documents_langflow(- langflow_file_service, session_manager, task_service, file_paths+ langflow_file_service, session_manager, task_service, file_paths, existing_task_id: str = None, connector_type: str = "openrag_docs"):"""Ingest default documents using Langflow upload-ingest-delete pipeline."""@@ -540,7 +547,7 @@ async def _ingest_default_documents_langflow({"key": "owner", "value": None},{"key": "owner_name", "value": anonymous_user.name},{"key": "owner_email", "value": anonymous_user.email},- {"key": "connector_type", "value": "system_default"},+ {"key": "connector_type", "value": "openrag_docs"},{"key": "is_sample_data", "value": "true"},]}@@ -560,6 +567,8 @@ async def _ingest_default_documents_langflow(settings=None, # Use default ingestion settingsdelete_after_ingest=True, # Clean up after ingestionreplace_duplicates=True,+ connector_type=connector_type,+ existing_task_id=existing_task_id,)logger.info(@@ -567,6 +576,7 @@ async def _ingest_default_documents_langflow(task_id=task_id,file_count=len(file_paths),)+ return task_idasync def _ingest_default_documents_url_langflow(@@ -628,6 +638,7 @@ async def _ingest_default_documents_url_langflow(task_id=task_id,docs_url=docs_url,)+ return task_idasync def _ingest_default_documents_url(@@ -659,7 +670,7 @@ async def _ingest_default_documents_url(owner_name=None,owner_email=None,is_sample_data=True,- connector_type="system_default",+ connector_type="openrag_docs",)await processor.process_document_standard(file_path=temp_file_path,@@ -670,7 +681,7 @@ async def _ingest_default_documents_url(owner_name=None,owner_email=None,file_size=os.path.getsize(temp_file_path),- connector_type="system_default",+ connector_type="openrag_docs",is_sample_data=True,)finally:@@ -1037,7 +1048,7 @@ async def opensearch_health_ready(request):async def _ingest_default_documents_openrag(- document_service, task_service, file_paths, connector_type: str = "local"+ document_service, task_service, file_paths, existing_task_id: str = None, connector_type: str = "openrag_docs"):"""Ingest default documents using traditional OpenRAG processor."""logger.info(@@ -1058,12 +1069,13 @@ async def _ingest_default_documents_openrag(connector_type=connector_type,)- task_id = await task_service.create_custom_task("anonymous", file_paths, processor)+ task_id = await task_service.create_custom_task("anonymous", file_paths, processor, existing_task_id=existing_task_id)logger.info("Started traditional OpenRAG ingestion task",task_id=task_id,file_count=len(file_paths),)+ return task_idasync def _update_mcp_servers_with_provider_credentials(services):diff --git a/src/models/processors.py b/src/models/processors.pyindex d317a88fa..f4fb72694 100644--- a/src/models/processors.py+++ b/src/models/processors.py@@ -702,6 +702,7 @@ class LangflowFileProcessor(TaskProcessor):settings: dict = None,delete_after_ingest: bool = True,replace_duplicates: bool = False,+ connector_type: str = "local",):super().__init__()self.langflow_file_service = langflow_file_service@@ -715,6 +716,7 @@ class LangflowFileProcessor(TaskProcessor):self.settings = settingsself.delete_after_ingest = delete_after_ingestself.replace_duplicates = replace_duplicates+ self.connector_type = connector_typeasync def process_item(self, upload_task: UploadTask, item: str, file_task: FileTask@@ -800,8 +802,7 @@ class LangflowFileProcessor(TaskProcessor):owner=self.owner_user_id,owner_name=self.owner_name,owner_email=self.owner_email,- connector_type="local",-+ connector_type=self.connector_type,)# Update task with successdiff --git a/src/services/task_service.py b/src/services/task_service.pyindex 5e6a14044..2ec3afaf0 100644--- a/src/services/task_service.py+++ b/src/services/task_service.py@@ -137,6 +137,8 @@ class TaskService:settings: dict = None,delete_after_ingest: bool = True,replace_duplicates: bool = False,+ connector_type: str = "local",+ existing_task_id: str = None,) -> str:"""Create a new upload task for Langflow file processing with upload and ingest"""# Use LangflowFileProcessor with user context@@ -154,8 +156,9 @@ class TaskService:settings=settings,delete_after_ingest=delete_after_ingest,replace_duplicates=replace_duplicates,+ connector_type=connector_type,)- return await self.create_custom_task(user_id, file_paths, processor, original_filenames)+ return await self.create_custom_task(user_id, file_paths, processor, original_filenames, existing_task_id=existing_task_id)async def create_langflow_url_upload_task(self,@@ -170,6 +173,7 @@ class TaskService:connector_type: str = "openrag_docs",prevent_outside: bool = True,tweaks: dict = None,+ existing_task_id: str = None,) -> str:"""Create a new upload task for Langflow URL ingestion."""from models.url import LangflowUrlProcessor@@ -187,14 +191,14 @@ class TaskService:prevent_outside=prevent_outside,tweaks=tweaks,)- return await self.create_custom_task(owner_user_id, [docs_url], processor)+ return await self.create_custom_task(owner_user_id, [docs_url], processor, existing_task_id=existing_task_id)- async def create_custom_task(self, user_id: str, items: list, processor, original_filenames: dict | None = None) -> str:+ async def create_custom_task(self, user_id: str, items: list, processor, original_filenames: dict | None = None, existing_task_id: str = None) -> str:"""Create a new task with custom processor for any type of items"""import os# Store anonymous tasks under a stable key so they can be retrieved laterstore_user_id = user_id or AnonymousUser().user_id- task_id = str(uuid.uuid4())+ task_id = existing_task_id or str(uuid.uuid4())# Create file tasks with original filenames if providednormalized_originals = (@@ -210,28 +214,32 @@ class TaskService:for item in items}- upload_task = UploadTask(- task_id=task_id,- total_files=len(items),- file_tasks=file_tasks,- )-- # Attach the custom processor to the task- upload_task.processor = processor-- if store_user_id not in self.task_store:- self.task_store[store_user_id] = {}- self.task_store[store_user_id][task_id] = upload_task+ if existing_task_id and store_user_id in self.task_store and existing_task_id in self.task_store[store_user_id]:+ upload_task = self.task_store[store_user_id][existing_task_id]+ upload_task.file_tasks.update(file_tasks)+ upload_task.total_files += len(items)+ upload_task.status = TaskStatus.RUNNING+ else:+ upload_task = UploadTask(+ task_id=task_id,+ total_files=len(items),+ file_tasks=file_tasks,+ )+ upload_task.processor = processor+ if store_user_id not in self.task_store:+ self.task_store[store_user_id] = {}+ self.task_store[store_user_id][task_id] = upload_task# Start background processingbackground_task = asyncio.create_task(- self.background_custom_processor(store_user_id, task_id, items)+ self.background_custom_processor(store_user_id, task_id, items, processor))self.background_tasks.add(background_task)background_task.add_done_callback(self.background_tasks.discard)- # Store reference to background task for cancellation- upload_task.background_task = background_task+ # Store reference to background task for cancellation if newly created+ if not existing_task_id:+ upload_task.background_task = background_task# Send telemetry event for task creation with metadataasyncio.create_task(@@ -282,7 +290,7 @@ class TaskService:return f"{hours}h {mins}m {secs}s"async def background_custom_processor(- self, user_id: str, task_id: str, items: list+ self, user_id: str, task_id: str, items: list, processor=None) -> None:"""Background task to process items using custom processor"""try:@@ -290,7 +298,7 @@ class TaskService:upload_task.status = TaskStatus.RUNNINGupload_task.updated_at = time.time()- processor = upload_task.processor+ processor = processor or upload_task.processorlogger.info("Upload / ingestion task started",@@ -401,9 +409,10 @@ class TaskService:await asyncio.gather(*tasks, return_exceptions=True)- # Mark task as completed- upload_task.status = TaskStatus.COMPLETED- upload_task.updated_at = time.time()+ # Mark task as completed if all files (including appended ones) are done+ if upload_task.processed_files >= upload_task.total_files:+ upload_task.status = TaskStatus.COMPLETED+ upload_task.updated_at = time.time()status: str = "FAILED"