|
| 1 | +""" |
| 2 | +Runtime availability probes for the opt-in heavy extraction engines. |
| 3 | +
|
| 4 | +Docling and local Crawl4AI are installed on demand at container startup (see |
| 5 | +scripts/docker-entrypoint.sh and |
| 6 | +docs/7-DEVELOPMENT/decisions/ADR-007-optin-runtimes.md), so availability is a |
| 7 | +*runtime* property: it cannot be read off the enable flags, and it does not |
| 8 | +survive a redeploy that drops them. |
| 9 | +
|
| 10 | +This lives in open_notebook.utils (not api/) because both layers need it: the |
| 11 | +capabilities router reports it to the frontend, and the source-processing graph |
| 12 | +must not hand content-core an engine whose runtime is absent — the engine |
| 13 | +choice is persisted in the database and therefore outlives the environment that |
| 14 | +made it available. |
| 15 | +""" |
| 16 | + |
| 17 | +import importlib.util |
| 18 | +import os |
| 19 | +import sys |
| 20 | + |
| 21 | +from loguru import logger |
| 22 | + |
| 23 | + |
| 24 | +def docling_available() -> bool: |
| 25 | + """True when Docling is installed (its document engine, OCR and image sources work).""" |
| 26 | + try: |
| 27 | + # content-core's own routing gate — the authoritative signal, not just a spec check. |
| 28 | + from content_core.extraction import DOCLING_AVAILABLE |
| 29 | + |
| 30 | + return bool(DOCLING_AVAILABLE) |
| 31 | + except (ImportError, AttributeError): |
| 32 | + # Content-core absent or its API moved — fall back to a plain spec check. |
| 33 | + return importlib.util.find_spec("docling") is not None |
| 34 | + except Exception: |
| 35 | + # An unexpected failure shouldn't be silently masked as "unavailable". |
| 36 | + logger.opt(exception=True).warning( |
| 37 | + "Unexpected error probing Docling availability; reporting unavailable" |
| 38 | + ) |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | +def crawl4ai_remote_configured() -> bool: |
| 43 | + """True when a remote Crawl4AI server is configured (CRAWL4AI_API_URL).""" |
| 44 | + try: |
| 45 | + from content_core.config import get_crawl4ai_api_url |
| 46 | + |
| 47 | + return bool(get_crawl4ai_api_url()) |
| 48 | + except (ImportError, AttributeError): |
| 49 | + return bool(os.environ.get("CRAWL4AI_API_URL")) |
| 50 | + except Exception: |
| 51 | + logger.opt(exception=True).warning( |
| 52 | + "Unexpected error probing Crawl4AI remote config; falling back to env var" |
| 53 | + ) |
| 54 | + return bool(os.environ.get("CRAWL4AI_API_URL")) |
| 55 | + |
| 56 | + |
| 57 | +def _default_playwright_cache() -> str | None: |
| 58 | + """Playwright's default browser download directory when PLAYWRIGHT_BROWSERS_PATH is unset.""" |
| 59 | + if sys.platform == "darwin": |
| 60 | + return os.path.expanduser("~/Library/Caches/ms-playwright") |
| 61 | + if sys.platform == "win32": |
| 62 | + local = os.environ.get("LOCALAPPDATA") |
| 63 | + return os.path.join(local, "ms-playwright") if local else None |
| 64 | + return os.path.expanduser("~/.cache/ms-playwright") # linux and others |
| 65 | + |
| 66 | + |
| 67 | +def _chromium_browser_present() -> bool: |
| 68 | + """True when a Playwright Chromium browser is installed on disk. |
| 69 | +
|
| 70 | + Local Crawl4AI needs both the package AND a Chromium browser. The startup |
| 71 | + installer downloads them in separate steps and degrades gracefully, so the |
| 72 | + package can be present while the browser download failed — checking the |
| 73 | + browser here keeps this an honest "usable capability" signal. |
| 74 | +
|
| 75 | + Playwright installs browsers into PLAYWRIGHT_BROWSERS_PATH (Docker) or, when |
| 76 | + that's unset, its per-user default cache (dev). Resolving the path does not |
| 77 | + download anything, so we must confirm a chromium build actually exists in |
| 78 | + whichever directory applies before reporting local Crawl4AI available. |
| 79 | + """ |
| 80 | + base = os.environ.get("PLAYWRIGHT_BROWSERS_PATH") or _default_playwright_cache() |
| 81 | + if not base or not os.path.isdir(base): |
| 82 | + return False |
| 83 | + try: |
| 84 | + return any("chromium" in name for name in os.listdir(base)) |
| 85 | + except OSError: |
| 86 | + return False |
| 87 | + |
| 88 | + |
| 89 | +def crawl4ai_local_ready() -> bool: |
| 90 | + """True when local Crawl4AI can actually render: package installed + Chromium present.""" |
| 91 | + if importlib.util.find_spec("crawl4ai") is None: |
| 92 | + return False |
| 93 | + return _chromium_browser_present() |
| 94 | + |
| 95 | + |
| 96 | +def crawl4ai_available() -> bool: |
| 97 | + """True when Crawl4AI can run at all — locally installed or offloaded to a server.""" |
| 98 | + return crawl4ai_local_ready() or crawl4ai_remote_configured() |
| 99 | + |
| 100 | + |
| 101 | +# Engine name -> (availability probe, env var that enables it). Engines absent |
| 102 | +# from this map need no runtime and are always usable. |
| 103 | +_ENGINE_RUNTIMES: dict[str, tuple[str, str]] = { |
| 104 | + "crawl4ai": ("crawl4ai_available", "OPEN_NOTEBOOK_ENABLE_CRAWL4AI"), |
| 105 | + "docling": ("docling_available", "OPEN_NOTEBOOK_ENABLE_DOCLING"), |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +def engine_runtime_missing(engine: str | None) -> str | None: |
| 110 | + """Return the env var that would enable ``engine``, or None if it is usable. |
| 111 | +
|
| 112 | + Used to avoid passing content-core an engine whose runtime is absent, which |
| 113 | + fails extraction outright with no indication of the real cause. |
| 114 | + """ |
| 115 | + if not engine: |
| 116 | + return None |
| 117 | + entry = _ENGINE_RUNTIMES.get(engine.strip().lower()) |
| 118 | + if entry is None: |
| 119 | + return None # Engine needs no opt-in runtime (auto/simple/firecrawl/jina). |
| 120 | + probe_name, env_var = entry |
| 121 | + probe = globals()[probe_name] |
| 122 | + return None if probe() else env_var |
0 commit comments