From 02a3035c824260066638ed77bd5f727e5a2fbbeb Mon Sep 17 00:00:00 2001 From: phact Date: Thu, 18 Sep 2025 12:02:08 -0400 Subject: [PATCH] tui copy sample docs --- src/tui/main.py | 36 +++++++++++++++++++++++++ src/tui/managers/container_manager.py | 39 +++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/tui/main.py b/src/tui/main.py index c2a785f3..b68293fe 100644 --- a/src/tui/main.py +++ b/src/tui/main.py @@ -4,6 +4,10 @@ import sys from pathlib import Path from textual.app import App, ComposeResult from utils.logging_config import get_logger +try: + from importlib.resources import files +except ImportError: + from importlib_resources import files logger = get_logger(__name__) @@ -301,10 +305,42 @@ class OpenRAGTUI(App): return True, "Runtime requirements satisfied" +def copy_sample_documents(): + """Copy sample documents from package to current directory if they don't exist.""" + documents_dir = Path("documents") + + # Check if documents directory already exists and has files + if documents_dir.exists() and any(documents_dir.glob("*.pdf")): + return # Documents already exist, don't overwrite + + try: + # Get sample documents from package assets + assets_files = files("tui._assets.documents") + + # Create documents directory if it doesn't exist + documents_dir.mkdir(exist_ok=True) + + # Copy each sample document + for resource in assets_files.iterdir(): + if resource.is_file() and resource.name.endswith('.pdf'): + dest_path = documents_dir / resource.name + if not dest_path.exists(): + content = resource.read_bytes() + dest_path.write_bytes(content) + logger.info(f"Copied sample document: {resource.name}") + + except Exception as e: + logger.debug(f"Could not copy sample documents: {e}") + # This is not a critical error - the app can work without sample documents + + def run_tui(): """Run the OpenRAG TUI application.""" app = None try: + # Copy sample documents on first run + copy_sample_documents() + app = OpenRAGTUI() app.run() except KeyboardInterrupt: diff --git a/src/tui/managers/container_manager.py b/src/tui/managers/container_manager.py index 43378508..d1be1e9f 100644 --- a/src/tui/managers/container_manager.py +++ b/src/tui/managers/container_manager.py @@ -88,21 +88,36 @@ class ContainerManager: """Find compose file in current directory or package resources.""" # First check current working directory cwd_path = Path(filename) + self._compose_search_log = f"Searching for {filename}:\n" + self._compose_search_log += f" 1. Current directory: {cwd_path.absolute()}" + if cwd_path.exists(): + self._compose_search_log += " ✓ FOUND" return cwd_path + else: + self._compose_search_log += " ✗ NOT FOUND" # Then check package resources + self._compose_search_log += f"\n 2. Package resources: " try: - pkg_files = files("openrag") - if (pkg_files / filename).is_file(): + pkg_files = files("tui._assets") + self._compose_search_log += f"{pkg_files}" + compose_resource = pkg_files / filename + + if compose_resource.is_file(): + self._compose_search_log += f" ✓ FOUND, copying to current directory" # Copy to cwd for compose command to work - content = (pkg_files / filename).read_text() + content = compose_resource.read_text() cwd_path.write_text(content) return cwd_path - except Exception: - pass + else: + self._compose_search_log += f" ✗ NOT FOUND" + except Exception as e: + self._compose_search_log += f" ✗ SKIPPED ({e})" + # Don't log this as an error since it's expected when running from source # Fall back to original path (will fail later if not found) + self._compose_search_log += f"\n 3. Falling back to: {cwd_path.absolute()}" return Path(filename) def is_available(self) -> bool: @@ -494,6 +509,20 @@ class ContainerManager: yield False, "No container runtime available" return + # Diagnostic info about compose files + compose_file = self.cpu_compose_file if (cpu_mode if cpu_mode is not None else self.use_cpu_compose) else self.compose_file + + # Show the search process for debugging + if hasattr(self, '_compose_search_log'): + for line in self._compose_search_log.split('\n'): + if line.strip(): + yield False, line + + yield False, f"Final compose file: {compose_file.absolute()}" + if not compose_file.exists(): + yield False, f"ERROR: Compose file not found at {compose_file.absolute()}" + return + yield False, "Starting OpenRAG services..." missing_images: List[str] = []