diff --git a/pyproject.toml b/pyproject.toml index 9eb796f..a405fd4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "sentienceapi" -version = "0.90.9" +version = "0.90.10" description = "Python SDK for Sentience AI Agent Browser Automation" readme = "README.md" requires-python = ">=3.11" diff --git a/sentience/__init__.py b/sentience/__init__.py index 3274adf..1d62d62 100644 --- a/sentience/__init__.py +++ b/sentience/__init__.py @@ -70,7 +70,7 @@ ) from .wait import wait_for -__version__ = "0.90.9" +__version__ = "0.90.10" __all__ = [ # Core SDK diff --git a/sentience/text_search.py b/sentience/text_search.py index 66ab77b..2a2bf70 100644 --- a/sentience/text_search.py +++ b/sentience/text_search.py @@ -88,6 +88,49 @@ def find_text_rect( # Limit max_results to prevent performance issues max_results = min(max_results, 100) + # CRITICAL: Wait for extension injection to complete (CSP-resistant architecture) + # The new architecture loads injected_api.js asynchronously, so window.sentience + # may not be immediately available after page load + try: + browser.page.wait_for_function( + "typeof window.sentience !== 'undefined'", + timeout=5000, # 5 second timeout + ) + except Exception as e: + # Gather diagnostics if wait fails + try: + diag = browser.page.evaluate( + """() => ({ + sentience_defined: typeof window.sentience !== 'undefined', + extension_id: document.documentElement.dataset.sentienceExtensionId || 'not set', + url: window.location.href + })""" + ) + except Exception: + diag = {"error": "Could not gather diagnostics"} + + raise RuntimeError( + f"Sentience extension failed to inject window.sentience API. " + f"Is the extension loaded? Diagnostics: {diag}" + ) from e + + # Verify findTextRect method exists (for older extension versions that don't have it) + try: + has_find_text_rect = browser.page.evaluate( + "typeof window.sentience.findTextRect !== 'undefined'" + ) + if not has_find_text_rect: + raise RuntimeError( + "window.sentience.findTextRect is not available. " + "Please update the Sentience extension to the latest version." + ) + except RuntimeError: + raise + except Exception as e: + raise RuntimeError( + f"Failed to verify findTextRect availability: {e}" + ) from e + # Call the extension's findTextRect method result_dict = browser.page.evaluate( """