|
1 | 1 | """ |
2 | | -Shared extension loading logic for sync and async implementations |
| 2 | +Shared extension loading logic for sync and async implementations. |
| 3 | +
|
| 4 | +Provides: |
| 5 | +- get_extension_dir(): Returns path to bundled extension (for browser-use integration) |
| 6 | +- verify_extension_injected(): Verifies window.sentience API is available |
| 7 | +- get_extension_version(): Gets extension version from manifest |
| 8 | +- verify_extension_version(): Checks SDK-extension version compatibility |
3 | 9 | """ |
4 | 10 |
|
| 11 | +import json |
5 | 12 | from pathlib import Path |
| 13 | +from typing import TYPE_CHECKING, Any |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from .protocols import AsyncPageProtocol, PageProtocol |
6 | 17 |
|
7 | 18 |
|
8 | 19 | def find_extension_path() -> Path: |
@@ -38,3 +49,147 @@ def find_extension_path() -> Path: |
38 | 49 | f"2. {dev_ext_path}\n" |
39 | 50 | "Make sure the extension is built and 'sentience/extension' directory exists." |
40 | 51 | ) |
| 52 | + |
| 53 | + |
| 54 | +def get_extension_dir() -> str: |
| 55 | + """ |
| 56 | + Get path to the bundled Sentience extension directory. |
| 57 | +
|
| 58 | + Use this to load the extension into browser-use or other Chromium-based browsers: |
| 59 | +
|
| 60 | + from sentience import get_extension_dir |
| 61 | + from browser_use import BrowserSession, BrowserProfile |
| 62 | +
|
| 63 | + profile = BrowserProfile( |
| 64 | + args=[f"--load-extension={get_extension_dir()}"], |
| 65 | + ) |
| 66 | + session = BrowserSession(browser_profile=profile) |
| 67 | +
|
| 68 | + Returns: |
| 69 | + Absolute path to extension directory as string |
| 70 | +
|
| 71 | + Raises: |
| 72 | + FileNotFoundError: If extension not found in package |
| 73 | + """ |
| 74 | + return str(find_extension_path()) |
| 75 | + |
| 76 | + |
| 77 | +def get_extension_version() -> str: |
| 78 | + """ |
| 79 | + Get the version of the bundled extension from manifest.json. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + Version string (e.g., "2.2.0") |
| 83 | +
|
| 84 | + Raises: |
| 85 | + FileNotFoundError: If extension or manifest not found |
| 86 | + """ |
| 87 | + ext_path = find_extension_path() |
| 88 | + manifest_path = ext_path / "manifest.json" |
| 89 | + with open(manifest_path) as f: |
| 90 | + manifest = json.load(f) |
| 91 | + return manifest.get("version", "unknown") |
| 92 | + |
| 93 | + |
| 94 | +def verify_extension_injected(page: "PageProtocol") -> bool: |
| 95 | + """ |
| 96 | + Verify the Sentience extension injected window.sentience API (sync). |
| 97 | +
|
| 98 | + Call this after navigating to a page to confirm the extension is working: |
| 99 | +
|
| 100 | + browser.goto("https://example.com") |
| 101 | + if not verify_extension_injected(browser.page): |
| 102 | + raise RuntimeError("Extension not injected") |
| 103 | +
|
| 104 | + Args: |
| 105 | + page: Playwright Page object (sync) |
| 106 | +
|
| 107 | + Returns: |
| 108 | + True if window.sentience.snapshot is available, False otherwise |
| 109 | + """ |
| 110 | + try: |
| 111 | + result = page.evaluate( |
| 112 | + "(() => !!(window.sentience && typeof window.sentience.snapshot === 'function'))()" |
| 113 | + ) |
| 114 | + return bool(result) |
| 115 | + except Exception: |
| 116 | + return False |
| 117 | + |
| 118 | + |
| 119 | +async def verify_extension_injected_async(page: "AsyncPageProtocol") -> bool: |
| 120 | + """ |
| 121 | + Verify the Sentience extension injected window.sentience API (async). |
| 122 | +
|
| 123 | + Call this after navigating to a page to confirm the extension is working: |
| 124 | +
|
| 125 | + await browser.goto("https://example.com") |
| 126 | + if not await verify_extension_injected_async(browser.page): |
| 127 | + raise RuntimeError("Extension not injected") |
| 128 | +
|
| 129 | + Args: |
| 130 | + page: Playwright Page object (async) |
| 131 | +
|
| 132 | + Returns: |
| 133 | + True if window.sentience.snapshot is available, False otherwise |
| 134 | + """ |
| 135 | + try: |
| 136 | + result = await page.evaluate( |
| 137 | + "(() => !!(window.sentience && typeof window.sentience.snapshot === 'function'))()" |
| 138 | + ) |
| 139 | + return bool(result) |
| 140 | + except Exception: |
| 141 | + return False |
| 142 | + |
| 143 | + |
| 144 | +def verify_extension_version(page: "PageProtocol", expected: str | None = None) -> str | None: |
| 145 | + """ |
| 146 | + Check extension version exposed in page (sync). |
| 147 | +
|
| 148 | + The extension sets window.__SENTIENCE_EXTENSION_VERSION__ when injected. |
| 149 | +
|
| 150 | + Args: |
| 151 | + page: Playwright Page object (sync) |
| 152 | + expected: If provided, raises RuntimeError on mismatch |
| 153 | +
|
| 154 | + Returns: |
| 155 | + Version string if found, None if not set (page may not have injected yet) |
| 156 | +
|
| 157 | + Raises: |
| 158 | + RuntimeError: If expected version provided and doesn't match |
| 159 | + """ |
| 160 | + try: |
| 161 | + got = page.evaluate("window.__SENTIENCE_EXTENSION_VERSION__ || null") |
| 162 | + except Exception: |
| 163 | + got = None |
| 164 | + |
| 165 | + if expected and got and got != expected: |
| 166 | + raise RuntimeError(f"Sentience extension version mismatch: expected {expected}, got {got}") |
| 167 | + return got |
| 168 | + |
| 169 | + |
| 170 | +async def verify_extension_version_async( |
| 171 | + page: "AsyncPageProtocol", expected: str | None = None |
| 172 | +) -> str | None: |
| 173 | + """ |
| 174 | + Check extension version exposed in page (async). |
| 175 | +
|
| 176 | + The extension sets window.__SENTIENCE_EXTENSION_VERSION__ when injected. |
| 177 | +
|
| 178 | + Args: |
| 179 | + page: Playwright Page object (async) |
| 180 | + expected: If provided, raises RuntimeError on mismatch |
| 181 | +
|
| 182 | + Returns: |
| 183 | + Version string if found, None if not set (page may not have injected yet) |
| 184 | +
|
| 185 | + Raises: |
| 186 | + RuntimeError: If expected version provided and doesn't match |
| 187 | + """ |
| 188 | + try: |
| 189 | + got = await page.evaluate("window.__SENTIENCE_EXTENSION_VERSION__ || null") |
| 190 | + except Exception: |
| 191 | + got = None |
| 192 | + |
| 193 | + if expected and got and got != expected: |
| 194 | + raise RuntimeError(f"Sentience extension version mismatch: expected {expected}, got {got}") |
| 195 | + return got |
0 commit comments