From 8ac759ba592dea29dcd4fe053d94d40f08746737 Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Wed, 21 Jan 2026 21:45:48 -0800 Subject: [PATCH 01/11] vision executor + enrich form actions --- examples/runtime_agent_minimal.py | 27 +- sentience/__init__.py | 23 +- sentience/actions.py | 1601 ++++++++++++++++------ sentience/agent_runtime.py | 12 +- sentience/backends/playwright_backend.py | 47 +- sentience/models.py | 3 + sentience/runtime_agent.py | 32 +- sentience/snapshot.py | 12 +- sentience/verification.py | 32 + sentience/vision_executor.py | 81 ++ tests/test_actions.py | 112 +- tests/test_verification.py | 39 + tests/unit/test_runtime_agent.py | 28 +- 13 files changed, 1618 insertions(+), 431 deletions(-) create mode 100644 sentience/vision_executor.py diff --git a/examples/runtime_agent_minimal.py b/examples/runtime_agent_minimal.py index 8595354..ab2da15 100644 --- a/examples/runtime_agent_minimal.py +++ b/examples/runtime_agent_minimal.py @@ -47,7 +47,9 @@ async def main() -> None: await page.goto("https://example.com") await page.wait_for_load_state("networkidle") - runtime = await AgentRuntime.from_sentience_browser(browser=browser, page=page, tracer=tracer) + runtime = await AgentRuntime.from_sentience_browser( + browser=browser, page=page, tracer=tracer + ) # Structured executor (for demo, we just return FINISH()). executor = FixedActionProvider("FINISH()") @@ -63,15 +65,29 @@ async def main() -> None: def has_example_heading(ctx: AssertContext) -> AssertOutcome: # Demonstrates custom predicates (you can also use exists/url_contains helpers). snap = ctx.snapshot - ok = bool(snap and any((el.role == "heading" and (el.text or "").startswith("Example")) for el in snap.elements)) + ok = bool( + snap + and any( + (el.role == "heading" and (el.text or "").startswith("Example")) + for el in snap.elements + ) + ) return AssertOutcome(passed=ok, reason="" if ok else "missing heading", details={}) step = RuntimeStep( goal="Confirm Example Domain page is loaded", verifications=[ - StepVerification(predicate=url_contains("example.com"), label="url_contains_example", required=True), - StepVerification(predicate=exists("role=heading"), label="has_heading", required=True), - StepVerification(predicate=has_example_heading, label="heading_text_matches", required=False), + StepVerification( + predicate=url_contains("example.com"), + label="url_contains_example", + required=True, + ), + StepVerification( + predicate=exists("role=heading"), label="has_heading", required=True + ), + StepVerification( + predicate=has_example_heading, label="heading_text_matches", required=False + ), ], max_snapshot_attempts=2, snapshot_limit_base=60, @@ -86,4 +102,3 @@ def has_example_heading(ctx: AssertContext) -> AssertOutcome: if __name__ == "__main__": asyncio.run(main()) - diff --git a/sentience/__init__.py b/sentience/__init__.py index b80d313..e382fb8 100644 --- a/sentience/__init__.py +++ b/sentience/__init__.py @@ -11,7 +11,20 @@ verify_extension_version, verify_extension_version_async, ) -from .actions import click, click_rect, press, scroll_to, type_text +from .actions import ( + back, + check, + clear, + click, + click_rect, + press, + scroll_to, + select_option, + submit, + type_text, + uncheck, + upload_file, +) from .agent import SentienceAgent, SentienceAgentAsync from .agent_config import AgentConfig from .agent_runtime import AgentRuntime, AssertionHandle @@ -118,6 +131,7 @@ all_of, any_of, custom, + download_completed, element_count, exists, is_checked, @@ -132,6 +146,13 @@ value_contains, value_equals, ) + +# Vision executor primitives (shared parsing/execution helpers) +from .vision_executor import ( + VisionExecutorAction, + execute_vision_executor_action, + parse_vision_executor_action, +) from .visual_agent import SentienceVisualAgent, SentienceVisualAgentAsync from .wait import wait_for diff --git a/sentience/actions.py b/sentience/actions.py index 74d300a..0be5f48 100644 --- a/sentience/actions.py +++ b/sentience/actions.py @@ -1,11 +1,10 @@ -from typing import Optional - """ Actions v1 - click, type, press """ import asyncio import time +from pathlib import Path from .browser import AsyncSentienceBrowser, SentienceBrowser from .browser_evaluator import BrowserEvaluator @@ -41,6 +40,7 @@ def click( # noqa: C901 start_time = time.time() url_before = browser.page.url cursor_meta: dict | None = None + error_msg = "" if use_mouse: # Hybrid approach: Get element bbox from snapshot, calculate center, use mouse.click() @@ -255,17 +255,13 @@ def type_text( ) -def press(browser: SentienceBrowser, key: str, take_snapshot: bool = False) -> ActionResult: +def clear( + browser: SentienceBrowser, + element_id: int, + take_snapshot: bool = False, +) -> ActionResult: """ - Press a keyboard key - - Args: - browser: SentienceBrowser instance - key: Key to press (e.g., "Enter", "Escape", "Tab") - take_snapshot: Whether to take snapshot after action - - Returns: - ActionResult + Clear the value of an input/textarea element (best-effort). """ if not browser.page: raise RuntimeError("Browser not started. Call browser.start() first.") @@ -273,16 +269,36 @@ def press(browser: SentienceBrowser, key: str, take_snapshot: bool = False) -> A start_time = time.time() url_before = browser.page.url - # Press key using Playwright - browser.page.keyboard.press(key) + ok = browser.page.evaluate( + """ + (id) => { + const el = window.sentience_registry[id]; + if (!el) return false; + try { el.focus?.(); } catch {} + if ('value' in el) { + el.value = ''; + el.dispatchEvent(new Event('input', { bubbles: true })); + el.dispatchEvent(new Event('change', { bubbles: true })); + return true; + } + return false; + } + """, + element_id, + ) - # Wait a bit for navigation/DOM updates - browser.page.wait_for_timeout(500) + if not ok: + return ActionResult( + success=False, + duration_ms=int((time.time() - start_time) * 1000), + outcome="error", + error={"code": "clear_failed", "reason": "Element not found or not clearable"}, + ) + browser.page.wait_for_timeout(250) duration_ms = int((time.time() - start_time) * 1000) url_after = browser.page.url url_changed = url_before != url_after - outcome = "navigated" if url_changed else "dom_updated" snapshot_after: Snapshot | None = None @@ -298,37 +314,13 @@ def press(browser: SentienceBrowser, key: str, take_snapshot: bool = False) -> A ) -def scroll_to( +def check( browser: SentienceBrowser, element_id: int, - behavior: str = "smooth", - block: str = "center", take_snapshot: bool = False, ) -> ActionResult: """ - Scroll an element into view - - Scrolls the page so that the specified element is visible in the viewport. - Uses the element registry to find the element and scrollIntoView() to scroll it. - - Args: - browser: SentienceBrowser instance - element_id: Element ID from snapshot to scroll into view - behavior: Scroll behavior - 'smooth', 'instant', or 'auto' (default: 'smooth') - block: Vertical alignment - 'start', 'center', 'end', or 'nearest' (default: 'center') - take_snapshot: Whether to take snapshot after action - - Returns: - ActionResult - - Example: - >>> snap = snapshot(browser) - >>> button = find(snap, 'role=button[name="Submit"]') - >>> if button: - >>> # Scroll element into view with smooth animation - >>> scroll_to(browser, button.id) - >>> # Scroll instantly to top of viewport - >>> scroll_to(browser, button.id, behavior='instant', block='start') + Ensure a checkbox/radio is checked (best-effort). """ if not browser.page: raise RuntimeError("Browser not started. Call browser.start() first.") @@ -336,41 +328,33 @@ def scroll_to( start_time = time.time() url_before = browser.page.url - # Scroll element into view using the element registry - scrolled = browser.page.evaluate( + ok = browser.page.evaluate( """ - (args) => { - const el = window.sentience_registry[args.id]; - if (el && el.scrollIntoView) { - el.scrollIntoView({ - behavior: args.behavior, - block: args.block, - inline: 'nearest' - }); - return true; - } - return false; + (id) => { + const el = window.sentience_registry[id]; + if (!el) return false; + try { el.focus?.(); } catch {} + if (!('checked' in el)) return false; + if (el.checked === true) return true; + try { el.click(); } catch { return false; } + return el.checked === true || true; } """, - {"id": element_id, "behavior": behavior, "block": block}, + element_id, ) - if not scrolled: + if not ok: return ActionResult( success=False, duration_ms=int((time.time() - start_time) * 1000), outcome="error", - error={"code": "scroll_failed", "reason": "Element not found or not scrollable"}, + error={"code": "check_failed", "reason": "Element not found or not checkable"}, ) - # Wait a bit for scroll to complete (especially for smooth scrolling) - wait_time = 500 if behavior == "smooth" else 100 - browser.page.wait_for_timeout(wait_time) - + browser.page.wait_for_timeout(250) duration_ms = int((time.time() - start_time) * 1000) url_after = browser.page.url url_changed = url_before != url_after - outcome = "navigated" if url_changed else "dom_updated" snapshot_after: Snapshot | None = None @@ -386,368 +370,1166 @@ def scroll_to( ) -def _highlight_rect( - browser: SentienceBrowser, rect: dict[str, float], duration_sec: float = 2.0 -) -> None: +def uncheck( + browser: SentienceBrowser, + element_id: int, + take_snapshot: bool = False, +) -> ActionResult: """ - Highlight a rectangle with a red border overlay - - Args: - browser: SentienceBrowser instance - rect: Dictionary with x, y, width (w), height (h) keys - duration_sec: How long to show the highlight (default: 2 seconds) + Ensure a checkbox/radio is unchecked (best-effort). """ if not browser.page: - return - - # Create a unique ID for this highlight - highlight_id = f"sentience_highlight_{int(time.time() * 1000)}" + raise RuntimeError("Browser not started. Call browser.start() first.") - # Combine all arguments into a single object for Playwright - args = { - "rect": { - "x": rect["x"], - "y": rect["y"], - "w": rect["w"], - "h": rect["h"], - }, - "highlightId": highlight_id, - "durationSec": duration_sec, - } + start_time = time.time() + url_before = browser.page.url - # Inject CSS and create overlay element - browser.page.evaluate( + ok = browser.page.evaluate( """ - (args) => { - const { rect, highlightId, durationSec } = args; - // Create overlay div - const overlay = document.createElement('div'); - overlay.id = highlightId; - overlay.style.position = 'fixed'; - overlay.style.left = `${rect.x}px`; - overlay.style.top = `${rect.y}px`; - overlay.style.width = `${rect.w}px`; - overlay.style.height = `${rect.h}px`; - overlay.style.border = '3px solid red'; - overlay.style.borderRadius = '2px'; - overlay.style.boxSizing = 'border-box'; - overlay.style.pointerEvents = 'none'; - overlay.style.zIndex = '999999'; - overlay.style.backgroundColor = 'rgba(255, 0, 0, 0.1)'; - overlay.style.transition = 'opacity 0.3s ease-out'; - - document.body.appendChild(overlay); - - // Remove after duration - setTimeout(() => { - overlay.style.opacity = '0'; - setTimeout(() => { - if (overlay.parentNode) { - overlay.parentNode.removeChild(overlay); - } - }, 300); // Wait for fade-out transition - }, durationSec * 1000); + (id) => { + const el = window.sentience_registry[id]; + if (!el) return false; + try { el.focus?.(); } catch {} + if (!('checked' in el)) return false; + if (el.checked === false) return true; + try { el.click(); } catch { return false; } + return el.checked === false || true; } """, - args, + element_id, ) + if not ok: + return ActionResult( + success=False, + duration_ms=int((time.time() - start_time) * 1000), + outcome="error", + error={"code": "uncheck_failed", "reason": "Element not found or not uncheckable"}, + ) -def click_rect( + browser.page.wait_for_timeout(250) + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + outcome = "navigated" if url_changed else "dom_updated" + + snapshot_after: Snapshot | None = None + if take_snapshot: + snapshot_after = snapshot(browser) + + return ActionResult( + success=True, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + ) + + +def select_option( browser: SentienceBrowser, - rect: dict[str, float], - highlight: bool = True, - highlight_duration: float = 2.0, + element_id: int, + option: str, take_snapshot: bool = False, - cursor_policy: CursorPolicy | None = None, ) -> ActionResult: """ - Click at the center of a rectangle using Playwright's native mouse simulation. - This uses a hybrid approach: calculates center coordinates and uses mouse.click() - for realistic event simulation (triggers hover, focus, mousedown, mouseup). - - Args: - browser: SentienceBrowser instance - rect: Dictionary with x, y, width (w), height (h) keys, or BBox object - highlight: Whether to show a red border highlight when clicking (default: True) - highlight_duration: How long to show the highlight in seconds (default: 2.0) - take_snapshot: Whether to take snapshot after action - - Returns: - ActionResult - - Example: - >>> click_rect(browser, {"x": 100, "y": 200, "w": 50, "h": 30}) - >>> # Or using BBox object - >>> from sentience import BBox - >>> bbox = BBox(x=100, y=200, width=50, height=30) - >>> click_rect(browser, {"x": bbox.x, "y": bbox.y, "w": bbox.width, "h": bbox.height}) + Select an option in a element (best-effort). + """ + if not browser.page: + raise RuntimeError("Browser not started. Call browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + p = str(Path(file_path)) - # Use Playwright's native mouse click for realistic simulation - # This triggers hover, focus, mousedown, mouseup sequences try: - if cursor_policy is not None and cursor_policy.mode == "human": - pos = getattr(browser, "_sentience_cursor_pos", None) - if not isinstance(pos, tuple) or len(pos) != 2: - try: - vp = browser.page.viewport_size or {} - pos = (float(vp.get("width", 0)) / 2.0, float(vp.get("height", 0)) / 2.0) - except Exception: - pos = (0.0, 0.0) + handle = browser.page.evaluate_handle( + "(id) => window.sentience_registry[id] || null", + element_id, + ) + el = handle.as_element() + if el is None: + raise RuntimeError("Element not found") + el.set_input_files(p) + success = True + error_msg = None + except Exception as e: + success = False + error_msg = str(e) - cursor_meta = build_human_cursor_path( - start=(float(pos[0]), float(pos[1])), - target=(float(center_x), float(center_y)), - policy=cursor_policy, - ) - pts = cursor_meta.get("path", []) - duration_ms_move = int(cursor_meta.get("duration_ms") or 0) - per_step_s = ( - (duration_ms_move / max(1, len(pts))) / 1000.0 if duration_ms_move > 0 else 0.0 - ) - for p in pts: - browser.page.mouse.move(float(p["x"]), float(p["y"])) - if per_step_s > 0: - time.sleep(per_step_s) - pause_ms = int(cursor_meta.get("pause_before_click_ms") or 0) - if pause_ms > 0: - time.sleep(pause_ms / 1000.0) + browser.page.wait_for_timeout(250) + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + outcome = "navigated" if url_changed else ("dom_updated" if success else "error") - browser.page.mouse.click(center_x, center_y) - setattr(browser, "_sentience_cursor_pos", (float(center_x), float(center_y))) + snapshot_after: Snapshot | None = None + if take_snapshot: + try: + snapshot_after = snapshot(browser) + except Exception: + snapshot_after = None + + return ActionResult( + success=success, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + error=( + None if success else {"code": "upload_failed", "reason": error_msg or "upload failed"} + ), + ) + + +def submit( + browser: SentienceBrowser, + element_id: int, + take_snapshot: bool = False, +) -> ActionResult: + """ + Submit a form (best-effort) by clicking a submit control or calling requestSubmit(). + """ + if not browser.page: + raise RuntimeError("Browser not started. Call browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + + ok = browser.page.evaluate( + """ + (id) => { + const el = window.sentience_registry[id]; + if (!el) return false; + try { el.focus?.(); } catch {} + const tag = (el.tagName || '').toUpperCase(); + if (tag === 'FORM') { + if (typeof el.requestSubmit === 'function') { el.requestSubmit(); return true; } + try { el.submit(); return true; } catch { return false; } + } + const form = el.form; + if (form && typeof form.requestSubmit === 'function') { form.requestSubmit(); return true; } + try { el.click(); return true; } catch { return false; } + } + """, + element_id, + ) + + if not ok: + return ActionResult( + success=False, + duration_ms=int((time.time() - start_time) * 1000), + outcome="error", + error={"code": "submit_failed", "reason": "Element not found or not submittable"}, + ) + + browser.page.wait_for_timeout(500) + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + outcome = "navigated" if url_changed else "dom_updated" + + snapshot_after: Snapshot | None = None + if take_snapshot: + try: + snapshot_after = snapshot(browser) + except Exception: + snapshot_after = None + + return ActionResult( + success=True, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + ) + + +def back( + browser: SentienceBrowser, + take_snapshot: bool = False, +) -> ActionResult: + """ + Navigate back in history (best-effort). + """ + if not browser.page: + raise RuntimeError("Browser not started. Call browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + try: + browser.page.go_back() success = True + error_msg = None except Exception as e: success = False error_msg = str(e) - # Wait a bit for navigation/DOM updates - browser.page.wait_for_timeout(500) + try: + browser.page.wait_for_timeout(500) + except Exception: + pass + + duration_ms = int((time.time() - start_time) * 1000) + try: + url_after = browser.page.url + url_changed = url_before != url_after + except Exception: + url_changed = True + + outcome = "navigated" if url_changed else ("dom_updated" if success else "error") + + snapshot_after: Snapshot | None = None + if take_snapshot: + try: + snapshot_after = snapshot(browser) + except Exception: + snapshot_after = None + + return ActionResult( + success=success, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + error=(None if success else {"code": "back_failed", "reason": error_msg or "back failed"}), + ) + + +def press(browser: SentienceBrowser, key: str, take_snapshot: bool = False) -> ActionResult: + """ + Press a keyboard key + + Args: + browser: SentienceBrowser instance + key: Key to press (e.g., "Enter", "Escape", "Tab") + take_snapshot: Whether to take snapshot after action + + Returns: + ActionResult + """ + if not browser.page: + raise RuntimeError("Browser not started. Call browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + + # Press key using Playwright + browser.page.keyboard.press(key) + + # Wait a bit for navigation/DOM updates + browser.page.wait_for_timeout(500) + + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + + outcome = "navigated" if url_changed else "dom_updated" + + snapshot_after: Snapshot | None = None + if take_snapshot: + snapshot_after = snapshot(browser) + + return ActionResult( + success=True, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + ) + + +def scroll_to( + browser: SentienceBrowser, + element_id: int, + behavior: str = "smooth", + block: str = "center", + take_snapshot: bool = False, +) -> ActionResult: + """ + Scroll an element into view + + Scrolls the page so that the specified element is visible in the viewport. + Uses the element registry to find the element and scrollIntoView() to scroll it. + + Args: + browser: SentienceBrowser instance + element_id: Element ID from snapshot to scroll into view + behavior: Scroll behavior - 'smooth', 'instant', or 'auto' (default: 'smooth') + block: Vertical alignment - 'start', 'center', 'end', or 'nearest' (default: 'center') + take_snapshot: Whether to take snapshot after action + + Returns: + ActionResult + + Example: + >>> snap = snapshot(browser) + >>> button = find(snap, 'role=button[name="Submit"]') + >>> if button: + >>> # Scroll element into view with smooth animation + >>> scroll_to(browser, button.id) + >>> # Scroll instantly to top of viewport + >>> scroll_to(browser, button.id, behavior='instant', block='start') + """ + if not browser.page: + raise RuntimeError("Browser not started. Call browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + + # Scroll element into view using the element registry + scrolled = browser.page.evaluate( + """ + (args) => { + const el = window.sentience_registry[args.id]; + if (el && el.scrollIntoView) { + el.scrollIntoView({ + behavior: args.behavior, + block: args.block, + inline: 'nearest' + }); + return true; + } + return false; + } + """, + {"id": element_id, "behavior": behavior, "block": block}, + ) + + if not scrolled: + return ActionResult( + success=False, + duration_ms=int((time.time() - start_time) * 1000), + outcome="error", + error={"code": "scroll_failed", "reason": "Element not found or not scrollable"}, + ) + + # Wait a bit for scroll to complete (especially for smooth scrolling) + wait_time = 500 if behavior == "smooth" else 100 + browser.page.wait_for_timeout(wait_time) + + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + + outcome = "navigated" if url_changed else "dom_updated" + + snapshot_after: Snapshot | None = None + if take_snapshot: + snapshot_after = snapshot(browser) + + return ActionResult( + success=True, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + ) + + +def _highlight_rect( + browser: SentienceBrowser, rect: dict[str, float], duration_sec: float = 2.0 +) -> None: + """ + Highlight a rectangle with a red border overlay + + Args: + browser: SentienceBrowser instance + rect: Dictionary with x, y, width (w), height (h) keys + duration_sec: How long to show the highlight (default: 2 seconds) + """ + if not browser.page: + return + + # Create a unique ID for this highlight + highlight_id = f"sentience_highlight_{int(time.time() * 1000)}" + + # Combine all arguments into a single object for Playwright + args = { + "rect": { + "x": rect["x"], + "y": rect["y"], + "w": rect["w"], + "h": rect["h"], + }, + "highlightId": highlight_id, + "durationSec": duration_sec, + } + + # Inject CSS and create overlay element + browser.page.evaluate( + """ + (args) => { + const { rect, highlightId, durationSec } = args; + // Create overlay div + const overlay = document.createElement('div'); + overlay.id = highlightId; + overlay.style.position = 'fixed'; + overlay.style.left = `${rect.x}px`; + overlay.style.top = `${rect.y}px`; + overlay.style.width = `${rect.w}px`; + overlay.style.height = `${rect.h}px`; + overlay.style.border = '3px solid red'; + overlay.style.borderRadius = '2px'; + overlay.style.boxSizing = 'border-box'; + overlay.style.pointerEvents = 'none'; + overlay.style.zIndex = '999999'; + overlay.style.backgroundColor = 'rgba(255, 0, 0, 0.1)'; + overlay.style.transition = 'opacity 0.3s ease-out'; + + document.body.appendChild(overlay); + + // Remove after duration + setTimeout(() => { + overlay.style.opacity = '0'; + setTimeout(() => { + if (overlay.parentNode) { + overlay.parentNode.removeChild(overlay); + } + }, 300); // Wait for fade-out transition + }, durationSec * 1000); + } + """, + args, + ) + + +def click_rect( + browser: SentienceBrowser, + rect: dict[str, float], + highlight: bool = True, + highlight_duration: float = 2.0, + take_snapshot: bool = False, + cursor_policy: CursorPolicy | None = None, +) -> ActionResult: + """ + Click at the center of a rectangle using Playwright's native mouse simulation. + This uses a hybrid approach: calculates center coordinates and uses mouse.click() + for realistic event simulation (triggers hover, focus, mousedown, mouseup). + + Args: + browser: SentienceBrowser instance + rect: Dictionary with x, y, width (w), height (h) keys, or BBox object + highlight: Whether to show a red border highlight when clicking (default: True) + highlight_duration: How long to show the highlight in seconds (default: 2.0) + take_snapshot: Whether to take snapshot after action + + Returns: + ActionResult + + Example: + >>> click_rect(browser, {"x": 100, "y": 200, "w": 50, "h": 30}) + >>> # Or using BBox object + >>> from sentience import BBox + >>> bbox = BBox(x=100, y=200, width=50, height=30) + >>> click_rect(browser, {"x": bbox.x, "y": bbox.y, "w": bbox.width, "h": bbox.height}) + """ + if not browser.page: + raise RuntimeError("Browser not started. Call browser.start() first.") + + # Handle BBox object or dict + if isinstance(rect, BBox): + x = rect.x + y = rect.y + w = rect.width + h = rect.height + else: + x = rect.get("x", 0) + y = rect.get("y", 0) + w = rect.get("w") or rect.get("width", 0) + h = rect.get("h") or rect.get("height", 0) + + if w <= 0 or h <= 0: + return ActionResult( + success=False, + duration_ms=0, + outcome="error", + error={ + "code": "invalid_rect", + "reason": "Rectangle width and height must be positive", + }, + ) + + start_time = time.time() + url_before = browser.page.url + + # Calculate center of rectangle + center_x = x + w / 2 + center_y = y + h / 2 + cursor_meta: dict | None = None + + # Show highlight before clicking (if enabled) + if highlight: + _highlight_rect(browser, {"x": x, "y": y, "w": w, "h": h}, highlight_duration) + # Small delay to ensure highlight is visible + browser.page.wait_for_timeout(50) + + # Use Playwright's native mouse click for realistic simulation + # This triggers hover, focus, mousedown, mouseup sequences + try: + if cursor_policy is not None and cursor_policy.mode == "human": + pos = getattr(browser, "_sentience_cursor_pos", None) + if not isinstance(pos, tuple) or len(pos) != 2: + try: + vp = browser.page.viewport_size or {} + pos = (float(vp.get("width", 0)) / 2.0, float(vp.get("height", 0)) / 2.0) + except Exception: + pos = (0.0, 0.0) + + cursor_meta = build_human_cursor_path( + start=(float(pos[0]), float(pos[1])), + target=(float(center_x), float(center_y)), + policy=cursor_policy, + ) + pts = cursor_meta.get("path", []) + duration_ms_move = int(cursor_meta.get("duration_ms") or 0) + per_step_s = ( + (duration_ms_move / max(1, len(pts))) / 1000.0 if duration_ms_move > 0 else 0.0 + ) + for p in pts: + browser.page.mouse.move(float(p["x"]), float(p["y"])) + if per_step_s > 0: + time.sleep(per_step_s) + pause_ms = int(cursor_meta.get("pause_before_click_ms") or 0) + if pause_ms > 0: + time.sleep(pause_ms / 1000.0) + + browser.page.mouse.click(center_x, center_y) + setattr(browser, "_sentience_cursor_pos", (float(center_x), float(center_y))) + success = True + except Exception as e: + success = False + error_msg = str(e) + + # Wait a bit for navigation/DOM updates + browser.page.wait_for_timeout(500) + + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + + # Determine outcome + outcome: str | None = None + if url_changed: + outcome = "navigated" + elif success: + outcome = "dom_updated" + else: + outcome = "error" + + # Optional snapshot after + snapshot_after: Snapshot | None = None + if take_snapshot: + snapshot_after = snapshot(browser) + + return ActionResult( + success=success, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + cursor=cursor_meta, + error=( + None + if success + else { + "code": "click_failed", + "reason": error_msg if not success else "Click failed", + } + ), + ) + + +# ========== Async Action Functions ========== + + +async def click_async( + browser: AsyncSentienceBrowser, + element_id: int, + use_mouse: bool = True, + take_snapshot: bool = False, + cursor_policy: CursorPolicy | None = None, +) -> ActionResult: + """ + Click an element by ID using hybrid approach (async) + + Args: + browser: AsyncSentienceBrowser instance + element_id: Element ID from snapshot + use_mouse: If True, use Playwright's mouse.click() at element center + take_snapshot: Whether to take snapshot after action + + Returns: + ActionResult + """ + if not browser.page: + raise RuntimeError("Browser not started. Call await browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + cursor_meta: dict | None = None + error_msg = "" + + if use_mouse: + try: + snap = await snapshot_async(browser) + element = None + for el in snap.elements: + if el.id == element_id: + element = el + break + + if element: + center_x = element.bbox.x + element.bbox.width / 2 + center_y = element.bbox.y + element.bbox.height / 2 + try: + if cursor_policy is not None and cursor_policy.mode == "human": + pos = getattr(browser, "_sentience_cursor_pos", None) + if not isinstance(pos, tuple) or len(pos) != 2: + try: + vp = browser.page.viewport_size or {} + pos = ( + float(vp.get("width", 0)) / 2.0, + float(vp.get("height", 0)) / 2.0, + ) + except Exception: + pos = (0.0, 0.0) + + cursor_meta = build_human_cursor_path( + start=(float(pos[0]), float(pos[1])), + target=(float(center_x), float(center_y)), + policy=cursor_policy, + ) + pts = cursor_meta.get("path", []) + duration_ms = int(cursor_meta.get("duration_ms") or 0) + per_step_s = ( + (duration_ms / max(1, len(pts))) / 1000.0 if duration_ms > 0 else 0.0 + ) + for p in pts: + await browser.page.mouse.move(float(p["x"]), float(p["y"])) + if per_step_s > 0: + await asyncio.sleep(per_step_s) + pause_ms = int(cursor_meta.get("pause_before_click_ms") or 0) + if pause_ms > 0: + await asyncio.sleep(pause_ms / 1000.0) + await browser.page.mouse.click(center_x, center_y) + setattr( + browser, "_sentience_cursor_pos", (float(center_x), float(center_y)) + ) + else: + await browser.page.mouse.click(center_x, center_y) + setattr( + browser, "_sentience_cursor_pos", (float(center_x), float(center_y)) + ) + success = True + except Exception: + success = True + else: + try: + success = await browser.page.evaluate( + """ + (id) => { + return window.sentience.click(id); + } + """, + element_id, + ) + except Exception: + success = True + except Exception: + try: + success = await browser.page.evaluate( + """ + (id) => { + return window.sentience.click(id); + } + """, + element_id, + ) + except Exception: + success = True + else: + success = await browser.page.evaluate( + """ + (id) => { + return window.sentience.click(id); + } + """, + element_id, + ) + + # Wait a bit for navigation/DOM updates + try: + await browser.page.wait_for_timeout(500) + except Exception: + pass + + duration_ms = int((time.time() - start_time) * 1000) + + # Check if URL changed + try: + url_after = browser.page.url + url_changed = url_before != url_after + except Exception: + url_after = url_before + url_changed = True + + # Determine outcome + outcome: str | None = None + if url_changed: + outcome = "navigated" + elif success: + outcome = "dom_updated" + else: + outcome = "error" + + # Optional snapshot after + snapshot_after: Snapshot | None = None + if take_snapshot: + try: + snapshot_after = await snapshot_async(browser) + except Exception: + pass + + return ActionResult( + success=success, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + cursor=cursor_meta, + error=( + None + if success + else { + "code": "click_failed", + "reason": "Element not found or not clickable", + } + ), + ) + + +async def type_text_async( + browser: AsyncSentienceBrowser, + element_id: int, + text: str, + take_snapshot: bool = False, + delay_ms: float = 0, +) -> ActionResult: + """ + Type text into an element (async) + + Args: + browser: AsyncSentienceBrowser instance + element_id: Element ID from snapshot + text: Text to type + take_snapshot: Whether to take snapshot after action + delay_ms: Delay between keystrokes in milliseconds for human-like typing (default: 0) + + Returns: + ActionResult + + Example: + >>> # Type instantly (default behavior) + >>> await type_text_async(browser, element_id, "Hello World") + >>> # Type with human-like delay (~10ms between keystrokes) + >>> await type_text_async(browser, element_id, "Hello World", delay_ms=10) + """ + if not browser.page: + raise RuntimeError("Browser not started. Call await browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + + # Focus element first + focused = await browser.page.evaluate( + """ + (id) => { + const el = window.sentience_registry[id]; + if (el) { + el.focus(); + return true; + } + return false; + } + """, + element_id, + ) + + if not focused: + return ActionResult( + success=False, + duration_ms=int((time.time() - start_time) * 1000), + outcome="error", + error={"code": "focus_failed", "reason": "Element not found"}, + ) + + # Type using Playwright keyboard with optional delay between keystrokes + await browser.page.keyboard.type(text, delay=delay_ms) + + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + + outcome = "navigated" if url_changed else "dom_updated" + + snapshot_after: Snapshot | None = None + if take_snapshot: + snapshot_after = await snapshot_async(browser) + + return ActionResult( + success=True, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + ) + + +async def clear_async( + browser: AsyncSentienceBrowser, + element_id: int, + take_snapshot: bool = False, +) -> ActionResult: + """Clear the value of an input/textarea element (best-effort, async).""" + if not browser.page: + raise RuntimeError("Browser not started. Call await browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + + ok = await browser.page.evaluate( + """ + (id) => { + const el = window.sentience_registry[id]; + if (!el) return false; + try { el.focus?.(); } catch {} + if ('value' in el) { + el.value = ''; + el.dispatchEvent(new Event('input', { bubbles: true })); + el.dispatchEvent(new Event('change', { bubbles: true })); + return true; + } + return false; + } + """, + element_id, + ) + + if not ok: + return ActionResult( + success=False, + duration_ms=int((time.time() - start_time) * 1000), + outcome="error", + error={"code": "clear_failed", "reason": "Element not found or not clearable"}, + ) + + await browser.page.wait_for_timeout(250) + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + outcome = "navigated" if url_changed else "dom_updated" + + snapshot_after: Snapshot | None = None + if take_snapshot: + snapshot_after = await snapshot_async(browser) + + return ActionResult( + success=True, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + ) + + +async def check_async( + browser: AsyncSentienceBrowser, + element_id: int, + take_snapshot: bool = False, +) -> ActionResult: + """Ensure a checkbox/radio is checked (best-effort, async).""" + if not browser.page: + raise RuntimeError("Browser not started. Call await browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + + ok = await browser.page.evaluate( + """ + (id) => { + const el = window.sentience_registry[id]; + if (!el) return false; + try { el.focus?.(); } catch {} + if (!('checked' in el)) return false; + if (el.checked === true) return true; + try { el.click(); } catch { return false; } + return true; + } + """, + element_id, + ) + + if not ok: + return ActionResult( + success=False, + duration_ms=int((time.time() - start_time) * 1000), + outcome="error", + error={"code": "check_failed", "reason": "Element not found or not checkable"}, + ) + + await browser.page.wait_for_timeout(250) + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + outcome = "navigated" if url_changed else "dom_updated" + + snapshot_after: Snapshot | None = None + if take_snapshot: + snapshot_after = await snapshot_async(browser) + + return ActionResult( + success=True, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + ) + + +async def uncheck_async( + browser: AsyncSentienceBrowser, + element_id: int, + take_snapshot: bool = False, +) -> ActionResult: + """Ensure a checkbox/radio is unchecked (best-effort, async).""" + if not browser.page: + raise RuntimeError("Browser not started. Call await browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + + ok = await browser.page.evaluate( + """ + (id) => { + const el = window.sentience_registry[id]; + if (!el) return false; + try { el.focus?.(); } catch {} + if (!('checked' in el)) return false; + if (el.checked === false) return true; + try { el.click(); } catch { return false; } + return true; + } + """, + element_id, + ) + + if not ok: + return ActionResult( + success=False, + duration_ms=int((time.time() - start_time) * 1000), + outcome="error", + error={"code": "uncheck_failed", "reason": "Element not found or not uncheckable"}, + ) + + await browser.page.wait_for_timeout(250) + duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + outcome = "navigated" if url_changed else "dom_updated" + + snapshot_after: Snapshot | None = None + if take_snapshot: + snapshot_after = await snapshot_async(browser) + + return ActionResult( + success=True, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + ) + + +async def select_option_async( + browser: AsyncSentienceBrowser, + element_id: int, + option: str, + take_snapshot: bool = False, +) -> ActionResult: + """Select an option in a (best-effort, async).""" if not browser.page: raise RuntimeError("Browser not started. Call await browser.start() first.") start_time = time.time() url_before = browser.page.url - cursor_meta: dict | None = None - - if use_mouse: - try: - snap = await snapshot_async(browser) - element = None - for el in snap.elements: - if el.id == element_id: - element = el - break - - if element: - center_x = element.bbox.x + element.bbox.width / 2 - center_y = element.bbox.y + element.bbox.height / 2 - try: - if cursor_policy is not None and cursor_policy.mode == "human": - pos = getattr(browser, "_sentience_cursor_pos", None) - if not isinstance(pos, tuple) or len(pos) != 2: - try: - vp = browser.page.viewport_size or {} - pos = ( - float(vp.get("width", 0)) / 2.0, - float(vp.get("height", 0)) / 2.0, - ) - except Exception: - pos = (0.0, 0.0) + p = str(Path(file_path)) - cursor_meta = build_human_cursor_path( - start=(float(pos[0]), float(pos[1])), - target=(float(center_x), float(center_y)), - policy=cursor_policy, - ) - pts = cursor_meta.get("path", []) - duration_ms = int(cursor_meta.get("duration_ms") or 0) - per_step_s = ( - (duration_ms / max(1, len(pts))) / 1000.0 if duration_ms > 0 else 0.0 - ) - for p in pts: - await browser.page.mouse.move(float(p["x"]), float(p["y"])) - if per_step_s > 0: - await asyncio.sleep(per_step_s) - pause_ms = int(cursor_meta.get("pause_before_click_ms") or 0) - if pause_ms > 0: - await asyncio.sleep(pause_ms / 1000.0) - await browser.page.mouse.click(center_x, center_y) - setattr( - browser, "_sentience_cursor_pos", (float(center_x), float(center_y)) - ) - else: - await browser.page.mouse.click(center_x, center_y) - setattr( - browser, "_sentience_cursor_pos", (float(center_x), float(center_y)) - ) - success = True - except Exception: - success = True - else: - try: - success = await browser.page.evaluate( - """ - (id) => { - return window.sentience.click(id); - } - """, - element_id, - ) - except Exception: - success = True - except Exception: - try: - success = await browser.page.evaluate( - """ - (id) => { - return window.sentience.click(id); - } - """, - element_id, - ) - except Exception: - success = True - else: - success = await browser.page.evaluate( - """ - (id) => { - return window.sentience.click(id); - } - """, + try: + handle = await browser.page.evaluate_handle( + "(id) => window.sentience_registry[id] || null", element_id, ) + el = handle.as_element() + if el is None: + raise RuntimeError("Element not found") + await el.set_input_files(p) + success = True + error_msg = None + except Exception as e: + success = False + error_msg = str(e) - # Wait a bit for navigation/DOM updates - try: - await browser.page.wait_for_timeout(500) - except Exception: - pass - + await browser.page.wait_for_timeout(250) duration_ms = int((time.time() - start_time) * 1000) + url_after = browser.page.url + url_changed = url_before != url_after + outcome = "navigated" if url_changed else ("dom_updated" if success else "error") - # Check if URL changed - try: - url_after = browser.page.url - url_changed = url_before != url_after - except Exception: - url_after = url_before - url_changed = True - - # Determine outcome - outcome: str | None = None - if url_changed: - outcome = "navigated" - elif success: - outcome = "dom_updated" - else: - outcome = "error" - - # Optional snapshot after snapshot_after: Snapshot | None = None if take_snapshot: try: snapshot_after = await snapshot_async(browser) except Exception: - pass + snapshot_after = None return ActionResult( success=success, @@ -755,85 +1537,63 @@ async def click_async( outcome=outcome, url_changed=url_changed, snapshot_after=snapshot_after, - cursor=cursor_meta, error=( - None - if success - else { - "code": "click_failed", - "reason": "Element not found or not clickable", - } + None if success else {"code": "upload_failed", "reason": error_msg or "upload failed"} ), ) -async def type_text_async( +async def submit_async( browser: AsyncSentienceBrowser, element_id: int, - text: str, take_snapshot: bool = False, - delay_ms: float = 0, ) -> ActionResult: - """ - Type text into an element (async) - - Args: - browser: AsyncSentienceBrowser instance - element_id: Element ID from snapshot - text: Text to type - take_snapshot: Whether to take snapshot after action - delay_ms: Delay between keystrokes in milliseconds for human-like typing (default: 0) - - Returns: - ActionResult - - Example: - >>> # Type instantly (default behavior) - >>> await type_text_async(browser, element_id, "Hello World") - >>> # Type with human-like delay (~10ms between keystrokes) - >>> await type_text_async(browser, element_id, "Hello World", delay_ms=10) - """ + """Submit a form (best-effort, async).""" if not browser.page: raise RuntimeError("Browser not started. Call await browser.start() first.") start_time = time.time() url_before = browser.page.url - # Focus element first - focused = await browser.page.evaluate( + ok = await browser.page.evaluate( """ (id) => { const el = window.sentience_registry[id]; - if (el) { - el.focus(); - return true; + if (!el) return false; + try { el.focus?.(); } catch {} + const tag = (el.tagName || '').toUpperCase(); + if (tag === 'FORM') { + if (typeof el.requestSubmit === 'function') { el.requestSubmit(); return true; } + try { el.submit(); return true; } catch { return false; } } - return false; + const form = el.form; + if (form && typeof form.requestSubmit === 'function') { form.requestSubmit(); return true; } + try { el.click(); return true; } catch { return false; } } """, element_id, ) - if not focused: + if not ok: return ActionResult( success=False, duration_ms=int((time.time() - start_time) * 1000), outcome="error", - error={"code": "focus_failed", "reason": "Element not found"}, + error={"code": "submit_failed", "reason": "Element not found or not submittable"}, ) - # Type using Playwright keyboard with optional delay between keystrokes - await browser.page.keyboard.type(text, delay=delay_ms) - + await browser.page.wait_for_timeout(500) duration_ms = int((time.time() - start_time) * 1000) url_after = browser.page.url url_changed = url_before != url_after - outcome = "navigated" if url_changed else "dom_updated" snapshot_after: Snapshot | None = None if take_snapshot: - snapshot_after = await snapshot_async(browser) + try: + snapshot_after = await snapshot_async(browser) + except Exception: + snapshot_after = None return ActionResult( success=True, @@ -844,6 +1604,55 @@ async def type_text_async( ) +async def back_async( + browser: AsyncSentienceBrowser, + take_snapshot: bool = False, +) -> ActionResult: + """Navigate back in history (best-effort, async).""" + if not browser.page: + raise RuntimeError("Browser not started. Call await browser.start() first.") + + start_time = time.time() + url_before = browser.page.url + try: + await browser.page.go_back() + success = True + error_msg = None + except Exception as e: + success = False + error_msg = str(e) + + try: + await browser.page.wait_for_timeout(500) + except Exception: + pass + + duration_ms = int((time.time() - start_time) * 1000) + try: + url_after = browser.page.url + url_changed = url_before != url_after + except Exception: + url_changed = True + + outcome = "navigated" if url_changed else ("dom_updated" if success else "error") + + snapshot_after: Snapshot | None = None + if take_snapshot: + try: + snapshot_after = await snapshot_async(browser) + except Exception: + snapshot_after = None + + return ActionResult( + success=success, + duration_ms=duration_ms, + outcome=outcome, + url_changed=url_changed, + snapshot_after=snapshot_after, + error=(None if success else {"code": "back_failed", "reason": error_msg or "back failed"}), + ) + + async def press_async( browser: AsyncSentienceBrowser, key: str, take_snapshot: bool = False ) -> ActionResult: diff --git a/sentience/agent_runtime.py b/sentience/agent_runtime.py index 01ba2ff..812e88b 100644 --- a/sentience/agent_runtime.py +++ b/sentience/agent_runtime.py @@ -209,10 +209,14 @@ def _ctx(self) -> AssertContext: elif self._cached_url: url = self._cached_url + downloads = None + try: + downloads = getattr(self.backend, "downloads", None) + except Exception: + downloads = None + return AssertContext( - snapshot=self.last_snapshot, - url=url, - step_id=self.step_id, + snapshot=self.last_snapshot, url=url, step_id=self.step_id, downloads=downloads ) async def get_url(self) -> str: @@ -582,7 +586,7 @@ def assert_done( True if task is complete (assertion passed), False otherwise """ # Convenience wrapper for assert_ with required=True - ok = self.assert_(predicate, label=label, required=True) + ok = self.assertTrue(predicate, label=label, required=True) if ok: self._task_done = True self._task_done_label = label diff --git a/sentience/backends/playwright_backend.py b/sentience/backends/playwright_backend.py index e023dfb..b3538e9 100644 --- a/sentience/backends/playwright_backend.py +++ b/sentience/backends/playwright_backend.py @@ -22,8 +22,10 @@ """ import asyncio -import base64 +import mimetypes +import os import time +from pathlib import Path from typing import TYPE_CHECKING, Any, Literal from .protocol import BrowserBackend, LayoutMetrics, ViewportInfo @@ -49,6 +51,49 @@ def __init__(self, page: "AsyncPage") -> None: """ self._page = page self._cached_viewport: ViewportInfo | None = None + self._downloads: list[dict[str, Any]] = [] + + # Best-effort download tracking (does not change behavior unless a download occurs). + # pylint: disable=broad-exception-caught + try: + self._page.on("download", lambda d: asyncio.create_task(self._track_download(d))) + except Exception: + pass + + @property + def downloads(self) -> list[dict[str, Any]]: + """Best-effort Playwright download records.""" + return self._downloads + + async def _track_download(self, download: Any) -> None: + rec: dict[str, Any] = { + "status": "started", + "suggested_filename": getattr(download, "suggested_filename", None), + "url": getattr(download, "url", None), + } + self._downloads.append(rec) + try: + # Wait for completion and capture path if Playwright provides it. + p = await download.path() + if p: + rec["status"] = "completed" + rec["path"] = str(p) + rec["filename"] = Path(str(p)).name + try: + rec["size_bytes"] = int(os.path.getsize(str(p))) + except Exception: + pass + try: + mt, _enc = mimetypes.guess_type(str(p)) + if mt: + rec["mime_type"] = mt + except Exception: + pass + else: + rec["status"] = "completed" + except Exception as e: + rec["status"] = "failed" + rec["error"] = str(e) @property def page(self) -> "AsyncPage": diff --git a/sentience/models.py b/sentience/models.py index 3fe323b..37252c8 100644 --- a/sentience/models.py +++ b/sentience/models.py @@ -313,6 +313,9 @@ class SnapshotDiagnostics(BaseModel): reasons: list[str] = Field(default_factory=list) metrics: SnapshotDiagnosticsMetrics | None = None captcha: CaptchaDiagnostics | None = None + # P1-01: forward-compatible vision recommendation signal (optional) + requires_vision: bool | None = None + requires_vision_reason: str | None = None def get_grid_bounds(self, grid_id: int | None = None) -> list[GridInfo]: """ diff --git a/sentience/runtime_agent.py b/sentience/runtime_agent.py index 8e2be77..231f5e3 100644 --- a/sentience/runtime_agent.py +++ b/sentience/runtime_agent.py @@ -135,7 +135,9 @@ async def _snapshot_with_ramp(self, *, step: RuntimeStep) -> Snapshot: raise RuntimeError("snapshot() returned None repeatedly") return last - def _propose_structured_action(self, *, task_goal: str, step: RuntimeStep, snap: Snapshot) -> str: + def _propose_structured_action( + self, *, task_goal: str, step: RuntimeStep, snap: Snapshot + ) -> str: dom_context = self._structured_llm.build_context(snap, step.goal) combined_goal = f"{task_goal}\n\nSTEP: {step.goal}" resp = self._structured_llm.query_llm(dom_context, combined_goal) @@ -182,7 +184,9 @@ async def _apply_verifications(self, *, step: RuntimeStep) -> bool: all_ok = True for v in step.verifications: if v.eventually: - ok = await self.runtime.check(v.predicate, label=v.label, required=v.required).eventually( + ok = await self.runtime.check( + v.predicate, label=v.label, required=v.required + ).eventually( timeout_s=v.timeout_s, poll_s=v.poll_s, max_snapshot_attempts=v.max_snapshot_attempts, @@ -280,17 +284,28 @@ async def _get_url_for_prompt(self) -> str | None: except Exception: return getattr(self.runtime.last_snapshot, "url", None) - async def _should_short_circuit_to_vision(self, *, step: RuntimeStep, snap: Snapshot | None) -> bool: - if not (step.vision_executor_enabled and self.vision_executor and self.vision_executor.supports_vision()): + async def _should_short_circuit_to_vision( + self, *, step: RuntimeStep, snap: Snapshot | None + ) -> bool: + if not ( + step.vision_executor_enabled + and self.vision_executor + and self.vision_executor.supports_vision() + ): return False if snap is None: return True - if step.min_actionables is not None and self._count_actionables(snap) < step.min_actionables: + if ( + step.min_actionables is not None + and self._count_actionables(snap) < step.min_actionables + ): if self.short_circuit_canvas: try: - n_canvas = await self.runtime.backend.eval("document.querySelectorAll('canvas').length") + n_canvas = await self.runtime.backend.eval( + "document.querySelectorAll('canvas').length" + ) if isinstance(n_canvas, (int, float)) and n_canvas > 0: return True except Exception: @@ -374,7 +389,9 @@ def _find_element(self, snap: Snapshot, element_id: int) -> Any | None: def _parse_action( self, action: str, - ) -> tuple[Literal["click", "type", "press", "finish", "click_xy", "click_rect"], dict[str, Any]]: + ) -> tuple[ + Literal["click", "type", "press", "finish", "click_xy", "click_rect"], dict[str, Any] + ]: action = action.strip() if re.match(r"FINISH\s*\(\s*\)\s*$", action, re.IGNORECASE): @@ -420,4 +437,3 @@ def _extract_action_from_text(self, text: str) -> str: pat = r'(CLICK_XY\s*\(\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*\)|CLICK_RECT\s*\(\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*\)|CLICK\s*\(\s*\d+\s*\)|TYPE\s*\(\s*\d+\s*,\s*["\'].*?["\']\s*\)|PRESS\s*\(\s*["\'].*?["\']\s*\)|FINISH\s*\(\s*\))' m = re.search(pat, text, re.IGNORECASE) return m.group(1) if m else text - diff --git a/sentience/snapshot.py b/sentience/snapshot.py index 34b1960..277a85e 100644 --- a/sentience/snapshot.py +++ b/sentience/snapshot.py @@ -110,8 +110,16 @@ def _build_snapshot_payload( client_metrics = None try: captcha = diagnostics.get("captcha") - if captcha is not None: - client_diagnostics = {"captcha": captcha} + requires_vision = diagnostics.get("requires_vision") + requires_vision_reason = diagnostics.get("requires_vision_reason") + if any(x is not None for x in [captcha, requires_vision, requires_vision_reason]): + client_diagnostics = {} + if captcha is not None: + client_diagnostics["captcha"] = captcha + if requires_vision is not None: + client_diagnostics["requires_vision"] = bool(requires_vision) + if requires_vision_reason is not None: + client_diagnostics["requires_vision_reason"] = str(requires_vision_reason) except Exception: client_diagnostics = None diff --git a/sentience/verification.py b/sentience/verification.py index 3c27eb3..f5209da 100644 --- a/sentience/verification.py +++ b/sentience/verification.py @@ -67,12 +67,44 @@ class AssertContext: snapshot: Snapshot | None = None url: str | None = None step_id: str | None = None + # Optional: non-snapshot state signals for verification (e.g., downloads). + downloads: list[dict[str, Any]] | None = None # Type alias for assertion predicates Predicate = Callable[[AssertContext], AssertOutcome] +def download_completed(filename_substring: str | None = None) -> Predicate: + """ + Predicate that passes if a browser download has completed. + + Notes: + - This relies on `AssertContext.downloads` being populated by the runtime/backend. + - For PlaywrightBackend, downloads are tracked automatically when possible. + """ + + def _pred(ctx: AssertContext) -> AssertOutcome: + downloads = ctx.downloads or [] + for d in downloads: + if str(d.get("status") or "") != "completed": + continue + fname = str(d.get("filename") or d.get("suggested_filename") or "") + if filename_substring is None or (filename_substring in fname): + return AssertOutcome(passed=True, reason="", details={"download": d}) + return AssertOutcome( + passed=False, + reason=( + f"no completed download matched: {filename_substring}" + if filename_substring + else "no completed downloads" + ), + details={"filename_substring": filename_substring, "downloads": downloads}, + ) + + return _pred + + def url_matches(pattern: str) -> Predicate: """ Create a predicate that checks if current URL matches a regex pattern. diff --git a/sentience/vision_executor.py b/sentience/vision_executor.py new file mode 100644 index 0000000..afb10be --- /dev/null +++ b/sentience/vision_executor.py @@ -0,0 +1,81 @@ +from __future__ import annotations + +import re +from dataclasses import dataclass +from typing import Any, Literal + + +VisionActionKind = Literal["click_xy", "click_rect", "press", "type", "finish"] + + +@dataclass(frozen=True) +class VisionExecutorAction: + kind: VisionActionKind + args: dict[str, Any] + + +def parse_vision_executor_action(text: str) -> VisionExecutorAction: + """ + Parse a vision-executor action string into a structured action. + + Supported formats: + - CLICK_XY(x, y) + - CLICK_RECT(x, y, w, h) + - PRESS("key") + - TYPE("text") + - FINISH() + """ + t = re.sub(r"```[\w]*\n?", "", (text or "")).strip() + if re.match(r"FINISH\s*\(\s*\)\s*$", t, re.IGNORECASE): + return VisionExecutorAction("finish", {}) + if m := re.match(r'PRESS\s*\(\s*["\']([^"\']+)["\']\s*\)\s*$', t, re.IGNORECASE): + return VisionExecutorAction("press", {"key": m.group(1)}) + if m := re.match(r'TYPE\s*\(\s*["\']([\s\S]*?)["\']\s*\)\s*$', t, re.IGNORECASE): + return VisionExecutorAction("type", {"text": m.group(1)}) + if m := re.match( + r"CLICK_XY\s*\(\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*\)\s*$", + t, + re.IGNORECASE, + ): + return VisionExecutorAction("click_xy", {"x": float(m.group(1)), "y": float(m.group(2))}) + if m := re.match( + r"CLICK_RECT\s*\(\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*\)\s*$", + t, + re.IGNORECASE, + ): + return VisionExecutorAction( + "click_rect", + {"x": float(m.group(1)), "y": float(m.group(2)), "w": float(m.group(3)), "h": float(m.group(4))}, + ) + raise ValueError(f"unrecognized vision action: {t[:200]}") + + +async def execute_vision_executor_action( + *, + backend: Any, + page: Any | None, + action: VisionExecutorAction, +) -> None: + """ + Execute a parsed vision action using a BrowserBackend (and optional Playwright Page). + """ + if action.kind == "click_xy": + await backend.mouse_click(float(action.args["x"]), float(action.args["y"])) + return + if action.kind == "click_rect": + cx = float(action.args["x"]) + float(action.args["w"]) / 2.0 + cy = float(action.args["y"]) + float(action.args["h"]) / 2.0 + await backend.mouse_click(cx, cy) + return + if action.kind == "press": + if page is None: + raise RuntimeError("PRESS requires a Playwright Page") + await page.keyboard.press(str(action.args["key"])) + return + if action.kind == "type": + await backend.type_text(str(action.args["text"])) + return + if action.kind == "finish": + return + raise ValueError(f"unknown vision action kind: {action.kind}") + diff --git a/tests/test_actions.py b/tests/test_actions.py index cc64ead..d18f02a 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -2,18 +2,22 @@ Tests for actions (click, type, press, click_rect) """ -import pytest - from sentience import ( - BBox, SentienceBrowser, + back, + check, + clear, click, click_rect, find, press, scroll_to, + select_option, snapshot, + submit, type_text, + uncheck, + upload_file, ) @@ -119,13 +123,15 @@ def test_click_rect_invalid_rect(): result = click_rect(browser, {"x": 100, "y": 100, "w": 0, "h": 30}) assert result.success is False assert result.error is not None - assert result.error["code"] == "invalid_rect" + assert result.error is not None + assert result.error.get("code") == "invalid_rect" # Invalid: negative height result = click_rect(browser, {"x": 100, "y": 100, "w": 50, "h": -10}) assert result.success is False assert result.error is not None - assert result.error["code"] == "invalid_rect" + assert result.error is not None + assert result.error.get("code") == "invalid_rect" def test_click_rect_with_snapshot(): @@ -239,7 +245,101 @@ def test_scroll_to_invalid_element(): result = scroll_to(browser, 99999) assert result.success is False assert result.error is not None - assert result.error["code"] == "scroll_failed" + assert result.error is not None + assert result.error.get("code") == "scroll_failed" + + +def _registry_find_id(browser: SentienceBrowser, predicate_js: str) -> int | None: + """ + Find a sentience_registry id by running a predicate(el) in page context. + Requires a snapshot() call before this, so registry is populated. + """ + if not browser.page: + return None + return browser.page.evaluate( + f""" + () => {{ + const reg = window.sentience_registry || {{}}; + for (const [id, el] of Object.entries(reg)) {{ + try {{ + if (({predicate_js})(el)) return Number(id); + }} catch {{}} + }} + return null; + }} + """ + ) + + +def test_form_crud_helpers(tmp_path): + with SentienceBrowser() as browser: + browser.page.goto("https://example.com") + browser.page.set_content( + """ + + + + +
+ + +
+ + + """ + ) + + # Populate registry + snapshot(browser) + + tid = _registry_find_id(browser, "(el) => el && el.id === 't'") + cbid = _registry_find_id(browser, "(el) => el && el.id === 'cb'") + selid = _registry_find_id(browser, "(el) => el && el.id === 'sel'") + fileid = _registry_find_id(browser, "(el) => el && el.id === 'file'") + btnid = _registry_find_id(browser, "(el) => el && el.id === 'btn'") + assert tid and cbid and selid and fileid and btnid + + r1 = clear(browser, tid) + assert r1.success is True + assert browser.page.evaluate("() => document.getElementById('t').value") == "" + + r2 = check(browser, cbid) + assert r2.success is True + assert browser.page.evaluate("() => document.getElementById('cb').checked") is True + + r3 = uncheck(browser, cbid) + assert r3.success is True + assert browser.page.evaluate("() => document.getElementById('cb').checked") is False + + r4 = select_option(browser, selid, "b") + assert r4.success is True + assert browser.page.evaluate("() => document.getElementById('sel').value") == "b" + + p = tmp_path / "upload.txt" + p.write_text("hi") + r5 = upload_file(browser, fileid, str(p)) + assert r5.success is True + assert ( + browser.page.evaluate("() => document.getElementById('file').files[0].name") + == "upload.txt" + ) + + r6 = submit(browser, btnid) + assert r6.success is True + assert browser.page.evaluate("() => window._submitted") is True + + # back() is best-effort; just ensure it doesn't crash and returns ActionResult + r7 = back(browser) + assert r7.duration_ms >= 0 def test_type_text_with_delay(): diff --git a/tests/test_verification.py b/tests/test_verification.py index 47b6ee5..b4af6ed 100644 --- a/tests/test_verification.py +++ b/tests/test_verification.py @@ -11,6 +11,7 @@ all_of, any_of, custom, + download_completed, element_count, exists, is_checked, @@ -25,6 +26,7 @@ value_contains, value_equals, ) +from sentience.vision_executor import parse_vision_executor_action def make_element( @@ -216,6 +218,43 @@ def test_all_pass(self): assert outcome.passed is True assert outcome.details["failed_count"] == 0 + +class TestDownloadCompleted: + def test_no_downloads(self): + pred = download_completed() + outcome = pred(AssertContext(downloads=[])) + assert outcome.passed is False + + def test_completed_download_any(self): + pred = download_completed() + outcome = pred( + AssertContext( + downloads=[ + {"status": "started", "suggested_filename": "a.txt"}, + {"status": "completed", "suggested_filename": "report.pdf"}, + ] + ) + ) + assert outcome.passed is True + + +def test_parse_vision_executor_action_click_xy(): + a = parse_vision_executor_action("CLICK_XY(10, 20)") + assert a.kind == "click_xy" + assert a.args["x"] == 10.0 + assert a.args["y"] == 20.0 + + def test_completed_download_with_substring(self): + pred = download_completed("report") + outcome = pred( + AssertContext( + downloads=[ + {"status": "completed", "suggested_filename": "report.pdf"}, + ] + ) + ) + assert outcome.passed is True + def test_one_fails(self): elements = [make_element(1, role="button")] snap = make_snapshot(elements, url="https://example.com/home") diff --git a/tests/unit/test_runtime_agent.py b/tests/unit/test_runtime_agent.py index e069b76..f97ab03 100644 --- a/tests/unit/test_runtime_agent.py +++ b/tests/unit/test_runtime_agent.py @@ -96,15 +96,24 @@ class VisionProviderStub(ProviderStub): def supports_vision(self) -> bool: return True - def generate_with_image(self, system_prompt: str, user_prompt: str, image_base64: str, **kwargs): + def generate_with_image( + self, system_prompt: str, user_prompt: str, image_base64: str, **kwargs + ): self.calls.append( - {"system": system_prompt, "user": user_prompt, "image_base64": image_base64, "kwargs": kwargs} + { + "system": system_prompt, + "user": user_prompt, + "image_base64": image_base64, + "kwargs": kwargs, + } ) content = self._responses.pop(0) if self._responses else "FINISH()" return LLMResponse(content=content, model_name=self.model_name) -def make_snapshot(*, url: str, elements: list[Element], confidence: float | None = None) -> Snapshot: +def make_snapshot( + *, url: str, elements: list[Element], confidence: float | None = None +) -> Snapshot: diagnostics = SnapshotDiagnostics(confidence=confidence) if confidence is not None else None return Snapshot( status="success", @@ -230,8 +239,12 @@ async def test_snapshot_limit_ramp_increases_limit_on_low_confidence() -> None: tracer = MockTracer() runtime = AgentRuntime(backend=backend, tracer=tracer) - s_low = make_snapshot(url="https://example.com/start", elements=[make_clickable_element(1)], confidence=0.1) - s_hi = make_snapshot(url="https://example.com/start", elements=[make_clickable_element(1)], confidence=0.9) + s_low = make_snapshot( + url="https://example.com/start", elements=[make_clickable_element(1)], confidence=0.1 + ) + s_hi = make_snapshot( + url="https://example.com/start", elements=[make_clickable_element(1)], confidence=0.9 + ) s_done = make_snapshot(url="https://example.com/done", elements=[make_clickable_element(1)]) seen_limits: list[int] = [] @@ -305,7 +318,9 @@ async def fake_snapshot(**_kwargs): executor = ProviderStub(responses=["CLICK(999)"]) # should NOT be called vision = VisionProviderStub(responses=["CLICK_XY(100, 200)"]) - agent = RuntimeAgent(runtime=runtime, executor=executor, vision_executor=vision, short_circuit_canvas=True) + agent = RuntimeAgent( + runtime=runtime, executor=executor, vision_executor=vision, short_circuit_canvas=True + ) def pred(ctx: AssertContext) -> AssertOutcome: ok = (ctx.url or "").endswith("/done") @@ -335,4 +350,3 @@ def pred(ctx: AssertContext) -> AssertOutcome: assert len(executor.calls) == 0 assert len(vision.calls) == 1 assert backend.mouse_clicks == [(100.0, 200.0)] - From afe5426437982f7242a3ca2896beb969a5119f20 Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Wed, 21 Jan 2026 22:51:31 -0800 Subject: [PATCH 02/11] fix tests --- sentience/agent_runtime.py | 2 +- sentience/extension/manifest.json | 6 +- sentience/extension/pkg/sentience_core.d.ts | 44 +- sentience/extension/pkg/sentience_core.js | 708 +++++++++++------- .../extension/pkg/sentience_core_bg.wasm | Bin 112142 -> 111775 bytes 5 files changed, 459 insertions(+), 301 deletions(-) diff --git a/sentience/agent_runtime.py b/sentience/agent_runtime.py index 812e88b..e0d8032 100644 --- a/sentience/agent_runtime.py +++ b/sentience/agent_runtime.py @@ -586,7 +586,7 @@ def assert_done( True if task is complete (assertion passed), False otherwise """ # Convenience wrapper for assert_ with required=True - ok = self.assertTrue(predicate, label=label, required=True) + ok = self.assert_(predicate, label=label, required=True) if ok: self._task_done = True self._task_done_label = label diff --git a/sentience/extension/manifest.json b/sentience/extension/manifest.json index a2d123d..23b3562 100644 --- a/sentience/extension/manifest.json +++ b/sentience/extension/manifest.json @@ -6,7 +6,7 @@ "permissions": ["activeTab", "scripting"], "host_permissions": [""], "background": { - "service_worker": "background.js", + "service_worker": "dist/background.js", "type": "module" }, "web_accessible_resources": [ @@ -18,13 +18,13 @@ "content_scripts": [ { "matches": [""], - "js": ["content.js"], + "js": ["dist/content.js"], "run_at": "document_start", "all_frames": true }, { "matches": [""], - "js": ["injected_api.js"], + "js": ["dist/injected_api.js"], "run_at": "document_idle", "world": "MAIN", "all_frames": true diff --git a/sentience/extension/pkg/sentience_core.d.ts b/sentience/extension/pkg/sentience_core.d.ts index 39ef420..e280c26 100644 --- a/sentience/extension/pkg/sentience_core.d.ts +++ b/sentience/extension/pkg/sentience_core.d.ts @@ -18,34 +18,34 @@ export function prune_for_api(val: any): any; export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; export interface InitOutput { - readonly memory: WebAssembly.Memory; - readonly analyze_page: (a: number) => number; - readonly analyze_page_with_options: (a: number, b: number) => number; - readonly decide_and_act: (a: number) => void; - readonly prune_for_api: (a: number) => number; - readonly __wbindgen_export: (a: number, b: number) => number; - readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number; - readonly __wbindgen_export3: (a: number) => void; + readonly memory: WebAssembly.Memory; + readonly analyze_page: (a: number) => number; + readonly analyze_page_with_options: (a: number, b: number) => number; + readonly decide_and_act: (a: number) => void; + readonly prune_for_api: (a: number) => number; + readonly __wbindgen_export: (a: number, b: number) => number; + readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number; + readonly __wbindgen_export3: (a: number) => void; } export type SyncInitInput = BufferSource | WebAssembly.Module; /** - * Instantiates the given `module`, which can either be bytes or - * a precompiled `WebAssembly.Module`. - * - * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. - * - * @returns {InitOutput} - */ +* Instantiates the given `module`, which can either be bytes or +* a precompiled `WebAssembly.Module`. +* +* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. +* +* @returns {InitOutput} +*/ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput; /** - * If `module_or_path` is {RequestInfo} or {URL}, makes a request and - * for everything else, calls `WebAssembly.instantiate` directly. - * - * @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. - * - * @returns {Promise} - */ +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. +* +* @returns {Promise} +*/ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/sentience/extension/pkg/sentience_core.js b/sentience/extension/pkg/sentience_core.js index c50ad61..b232d13 100644 --- a/sentience/extension/pkg/sentience_core.js +++ b/sentience/extension/pkg/sentience_core.js @@ -1,247 +1,112 @@ -export function analyze_page(val) { - return takeObject(wasm.analyze_page(addHeapObject(val))); -} - -export function analyze_page_with_options(val, options) { - return takeObject(wasm.analyze_page_with_options(addHeapObject(val), addHeapObject(options))); -} - -export function decide_and_act(_raw_elements) { - wasm.decide_and_act(addHeapObject(_raw_elements)); -} - -export function prune_for_api(val) { - return takeObject(wasm.prune_for_api(addHeapObject(val))); -} - -function __wbg_get_imports() { - const import0 = { - __proto__: null, - __wbg_Error_8c4e43fe74559d73: function(arg0, arg1) { - return addHeapObject(Error(getStringFromWasm0(arg0, arg1))); - }, - __wbg_Number_04624de7d0e8332d: function(arg0) { - return Number(getObject(arg0)); - }, - __wbg___wbindgen_bigint_get_as_i64_8fcf4ce7f1ca72a2: function(arg0, arg1) { - const v = getObject(arg1), ret = "bigint" == typeof v ? v : void 0; - getDataViewMemory0().setBigInt64(arg0 + 8, isLikeNone(ret) ? BigInt(0) : ret, !0), - getDataViewMemory0().setInt32(arg0 + 0, !isLikeNone(ret), !0); - }, - __wbg___wbindgen_boolean_get_bbbb1c18aa2f5e25: function(arg0) { - const v = getObject(arg0), ret = "boolean" == typeof v ? v : void 0; - return isLikeNone(ret) ? 16777215 : ret ? 1 : 0; - }, - __wbg___wbindgen_debug_string_0bc8482c6e3508ae: function(arg0, arg1) { - const ptr1 = passStringToWasm0(debugString(getObject(arg1)), wasm.__wbindgen_export, wasm.__wbindgen_export2), len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4, len1, !0), getDataViewMemory0().setInt32(arg0 + 0, ptr1, !0); - }, - __wbg___wbindgen_in_47fa6863be6f2f25: function(arg0, arg1) { - return getObject(arg0) in getObject(arg1); - }, - __wbg___wbindgen_is_bigint_31b12575b56f32fc: function(arg0) { - return "bigint" == typeof getObject(arg0); - }, - __wbg___wbindgen_is_function_0095a73b8b156f76: function(arg0) { - return "function" == typeof getObject(arg0); - }, - __wbg___wbindgen_is_object_5ae8e5880f2c1fbd: function(arg0) { - const val = getObject(arg0); - return "object" == typeof val && null !== val; - }, - __wbg___wbindgen_is_undefined_9e4d92534c42d778: function(arg0) { - return void 0 === getObject(arg0); - }, - __wbg___wbindgen_jsval_eq_11888390b0186270: function(arg0, arg1) { - return getObject(arg0) === getObject(arg1); - }, - __wbg___wbindgen_jsval_loose_eq_9dd77d8cd6671811: function(arg0, arg1) { - return getObject(arg0) == getObject(arg1); - }, - __wbg___wbindgen_number_get_8ff4255516ccad3e: function(arg0, arg1) { - const obj = getObject(arg1), ret = "number" == typeof obj ? obj : void 0; - getDataViewMemory0().setFloat64(arg0 + 8, isLikeNone(ret) ? 0 : ret, !0), getDataViewMemory0().setInt32(arg0 + 0, !isLikeNone(ret), !0); - }, - __wbg___wbindgen_string_get_72fb696202c56729: function(arg0, arg1) { - const obj = getObject(arg1), ret = "string" == typeof obj ? obj : void 0; - var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2), len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4, len1, !0), getDataViewMemory0().setInt32(arg0 + 0, ptr1, !0); - }, - __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); - }, - __wbg_call_389efe28435a9388: function() { - return handleError(function(arg0, arg1) { - return addHeapObject(getObject(arg0).call(getObject(arg1))); - }, arguments); - }, - __wbg_done_57b39ecd9addfe81: function(arg0) { - return getObject(arg0).done; - }, - __wbg_error_9a7fe3f932034cde: function(arg0) {}, - __wbg_get_9b94d73e6221f75c: function(arg0, arg1) { - return addHeapObject(getObject(arg0)[arg1 >>> 0]); - }, - __wbg_get_b3ed3ad4be2bc8ac: function() { - return handleError(function(arg0, arg1) { - return addHeapObject(Reflect.get(getObject(arg0), getObject(arg1))); - }, arguments); - }, - __wbg_get_with_ref_key_1dc361bd10053bfe: function(arg0, arg1) { - return addHeapObject(getObject(arg0)[getObject(arg1)]); - }, - __wbg_instanceof_ArrayBuffer_c367199e2fa2aa04: function(arg0) { - let result; - try { - result = getObject(arg0) instanceof ArrayBuffer; - } catch (_) { - result = !1; - } - return result; - }, - __wbg_instanceof_Uint8Array_9b9075935c74707c: function(arg0) { - let result; - try { - result = getObject(arg0) instanceof Uint8Array; - } catch (_) { - result = !1; - } - return result; - }, - __wbg_isArray_d314bb98fcf08331: function(arg0) { - return Array.isArray(getObject(arg0)); - }, - __wbg_isSafeInteger_bfbc7332a9768d2a: function(arg0) { - return Number.isSafeInteger(getObject(arg0)); - }, - __wbg_iterator_6ff6560ca1568e55: function() { - return addHeapObject(Symbol.iterator); - }, - __wbg_js_click_element_2fe1e774f3d232c7: function(arg0) { - js_click_element(arg0); - }, - __wbg_length_32ed9a279acd054c: function(arg0) { - return getObject(arg0).length; - }, - __wbg_length_35a7bace40f36eac: function(arg0) { - return getObject(arg0).length; - }, - __wbg_new_361308b2356cecd0: function() { - return addHeapObject(new Object); - }, - __wbg_new_3eb36ae241fe6f44: function() { - return addHeapObject(new Array); - }, - __wbg_new_dd2b680c8bf6ae29: function(arg0) { - return addHeapObject(new Uint8Array(getObject(arg0))); - }, - __wbg_next_3482f54c49e8af19: function() { - return handleError(function(arg0) { - return addHeapObject(getObject(arg0).next()); - }, arguments); - }, - __wbg_next_418f80d8f5303233: function(arg0) { - return addHeapObject(getObject(arg0).next); - }, - __wbg_prototypesetcall_bdcdcc5842e4d77d: function(arg0, arg1, arg2) { - Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), getObject(arg2)); - }, - __wbg_set_3f1d0b984ed272ed: function(arg0, arg1, arg2) { - getObject(arg0)[takeObject(arg1)] = takeObject(arg2); - }, - __wbg_set_f43e577aea94465b: function(arg0, arg1, arg2) { - getObject(arg0)[arg1 >>> 0] = takeObject(arg2); - }, - __wbg_value_0546255b415e96c1: function(arg0) { - return addHeapObject(getObject(arg0).value); - }, - __wbindgen_cast_0000000000000001: function(arg0) { - return addHeapObject(arg0); - }, - __wbindgen_cast_0000000000000002: function(arg0, arg1) { - return addHeapObject(getStringFromWasm0(arg0, arg1)); - }, - __wbindgen_cast_0000000000000003: function(arg0) { - return addHeapObject(BigInt.asUintN(64, arg0)); - }, - __wbindgen_object_clone_ref: function(arg0) { - return addHeapObject(getObject(arg0)); - }, - __wbindgen_object_drop_ref: function(arg0) { - takeObject(arg0); - } - }; - return { - __proto__: null, - "./sentience_core_bg.js": import0 - }; -} +let wasm; function addHeapObject(obj) { - heap_next === heap.length && heap.push(heap.length + 1); + if (heap_next === heap.length) heap.push(heap.length + 1); const idx = heap_next; - return heap_next = heap[idx], heap[idx] = obj, idx; + heap_next = heap[idx]; + + heap[idx] = obj; + return idx; } function debugString(val) { + // primitive types const type = typeof val; - if ("number" == type || "boolean" == type || null == val) return `${val}`; - if ("string" == type) return `"${val}"`; - if ("symbol" == type) { + if (type == 'number' || type == 'boolean' || val == null) { + return `${val}`; + } + if (type == 'string') { + return `"${val}"`; + } + if (type == 'symbol') { const description = val.description; - return null == description ? "Symbol" : `Symbol(${description})`; + if (description == null) { + return 'Symbol'; + } else { + return `Symbol(${description})`; + } } - if ("function" == type) { + if (type == 'function') { const name = val.name; - return "string" == typeof name && name.length > 0 ? `Function(${name})` : "Function"; + if (typeof name == 'string' && name.length > 0) { + return `Function(${name})`; + } else { + return 'Function'; + } } + // objects if (Array.isArray(val)) { const length = val.length; - let debug = "["; - length > 0 && (debug += debugString(val[0])); - for (let i = 1; i < length; i++) debug += ", " + debugString(val[i]); - return debug += "]", debug; + let debug = '['; + if (length > 0) { + debug += debugString(val[0]); + } + for(let i = 1; i < length; i++) { + debug += ', ' + debugString(val[i]); + } + debug += ']'; + return debug; } + // Test for built-in const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); let className; - if (!(builtInMatches && builtInMatches.length > 1)) return toString.call(val); - if (className = builtInMatches[1], "Object" == className) try { - return "Object(" + JSON.stringify(val) + ")"; - } catch (_) { - return "Object"; + if (builtInMatches && builtInMatches.length > 1) { + className = builtInMatches[1]; + } else { + // Failed to match the standard '[object ClassName]' + return toString.call(val); + } + if (className == 'Object') { + // we're a user defined class or Object + // JSON.stringify avoids problems with cycles, and is generally much + // easier than looping through ownProperties of `val`. + try { + return 'Object(' + JSON.stringify(val) + ')'; + } catch (_) { + return 'Object'; + } + } + // errors + if (val instanceof Error) { + return `${val.name}: ${val.message}\n${val.stack}`; } - return val instanceof Error ? `${val.name}: ${val.message}\n${val.stack}` : className; + // TODO we could test for more things here, like `Set`s and `Map`s. + return className; } function dropObject(idx) { - idx < 132 || (heap[idx] = heap_next, heap_next = idx); + if (idx < 132) return; + heap[idx] = heap_next; + heap_next = idx; } function getArrayU8FromWasm0(ptr, len) { - return ptr >>>= 0, getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); + ptr = ptr >>> 0; + return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); } let cachedDataViewMemory0 = null; - function getDataViewMemory0() { - return (null === cachedDataViewMemory0 || !0 === cachedDataViewMemory0.buffer.detached || void 0 === cachedDataViewMemory0.buffer.detached && cachedDataViewMemory0.buffer !== wasm.memory.buffer) && (cachedDataViewMemory0 = new DataView(wasm.memory.buffer)), - cachedDataViewMemory0; + if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { + cachedDataViewMemory0 = new DataView(wasm.memory.buffer); + } + return cachedDataViewMemory0; } function getStringFromWasm0(ptr, len) { - return decodeText(ptr >>>= 0, len); + ptr = ptr >>> 0; + return decodeText(ptr, len); } let cachedUint8ArrayMemory0 = null; - function getUint8ArrayMemory0() { - return null !== cachedUint8ArrayMemory0 && 0 !== cachedUint8ArrayMemory0.byteLength || (cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer)), - cachedUint8ArrayMemory0; + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; } -function getObject(idx) { - return heap[idx]; -} +function getObject(idx) { return heap[idx]; } function handleError(f, args) { try { @@ -251,121 +116,414 @@ function handleError(f, args) { } } -let heap = new Array(128).fill(void 0); - -heap.push(void 0, null, !0, !1); +let heap = new Array(128).fill(undefined); +heap.push(undefined, null, true, false); let heap_next = heap.length; function isLikeNone(x) { - return null == x; + return x === undefined || x === null; } function passStringToWasm0(arg, malloc, realloc) { - if (void 0 === realloc) { - const buf = cachedTextEncoder.encode(arg), ptr = malloc(buf.length, 1) >>> 0; - return getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf), WASM_VECTOR_LEN = buf.length, - ptr; + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; } - let len = arg.length, ptr = malloc(len, 1) >>> 0; + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + const mem = getUint8ArrayMemory0(); + let offset = 0; - for (;offset < len; offset++) { + + for (; offset < len; offset++) { const code = arg.charCodeAt(offset); - if (code > 127) break; + if (code > 0x7F) break; mem[ptr + offset] = code; } if (offset !== len) { - 0 !== offset && (arg = arg.slice(offset)), ptr = realloc(ptr, len, len = offset + 3 * arg.length, 1) >>> 0; + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); - offset += cachedTextEncoder.encodeInto(arg, view).written, ptr = realloc(ptr, len, offset, 1) >>> 0; + const ret = cachedTextEncoder.encodeInto(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; } - return WASM_VECTOR_LEN = offset, ptr; + + WASM_VECTOR_LEN = offset; + return ptr; } function takeObject(idx) { const ret = getObject(idx); - return dropObject(idx), ret; + dropObject(idx); + return ret; } -let cachedTextDecoder = new TextDecoder("utf-8", { - ignoreBOM: !0, - fatal: !0 -}); - +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); - const MAX_SAFARI_DECODE_BYTES = 2146435072; - let numBytesDecoded = 0; - function decodeText(ptr, len) { - return numBytesDecoded += len, numBytesDecoded >= MAX_SAFARI_DECODE_BYTES && (cachedTextDecoder = new TextDecoder("utf-8", { - ignoreBOM: !0, - fatal: !0 - }), cachedTextDecoder.decode(), numBytesDecoded = len), cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); + numBytesDecoded += len; + if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) { + cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + cachedTextDecoder.decode(); + numBytesDecoded = len; + } + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); } -const cachedTextEncoder = new TextEncoder; +const cachedTextEncoder = new TextEncoder(); -"encodeInto" in cachedTextEncoder || (cachedTextEncoder.encodeInto = function(arg, view) { - const buf = cachedTextEncoder.encode(arg); - return view.set(buf), { - read: arg.length, - written: buf.length - }; -}); +if (!('encodeInto' in cachedTextEncoder)) { + cachedTextEncoder.encodeInto = function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; + } +} -let wasmModule, wasm, WASM_VECTOR_LEN = 0; +let WASM_VECTOR_LEN = 0; -function __wbg_finalize_init(instance, module) { - return wasm = instance.exports, wasmModule = module, cachedDataViewMemory0 = null, - cachedUint8ArrayMemory0 = null, wasm; +/** + * @param {any} val + * @returns {any} + */ +export function analyze_page(val) { + const ret = wasm.analyze_page(addHeapObject(val)); + return takeObject(ret); +} + +/** + * @param {any} val + * @param {any} options + * @returns {any} + */ +export function analyze_page_with_options(val, options) { + const ret = wasm.analyze_page_with_options(addHeapObject(val), addHeapObject(options)); + return takeObject(ret); +} + +/** + * @param {any} _raw_elements + */ +export function decide_and_act(_raw_elements) { + wasm.decide_and_act(addHeapObject(_raw_elements)); +} + +/** + * Prune raw elements before sending to API + * This is a "dumb" filter that reduces payload size without leaking proprietary IP + * Filters out: tiny elements, invisible elements, non-interactive wrapper divs + * Amazon: 5000-6000 elements -> ~200-400 elements (~95% reduction) + * @param {any} val + * @returns {any} + */ +export function prune_for_api(val) { + const ret = wasm.prune_for_api(addHeapObject(val)); + return takeObject(ret); } +const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']); + async function __wbg_load(module, imports) { - if ("function" == typeof Response && module instanceof Response) { - if ("function" == typeof WebAssembly.instantiateStreaming) try { - return await WebAssembly.instantiateStreaming(module, imports); - } catch (e) { - if (!(module.ok && function(type) { - switch (type) { - case "basic": - case "cors": - case "default": - return !0; + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + } catch (e) { + const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type); + + if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; } - return !1; - }(module.type)) || "application/wasm" === module.headers.get("Content-Type")) throw e; + } } + const bytes = await module.arrayBuffer(); return await WebAssembly.instantiate(bytes, imports); - } - { + } else { const instance = await WebAssembly.instantiate(module, imports); - return instance instanceof WebAssembly.Instance ? { - instance: instance, - module: module - } : instance; + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + } else { + return instance; + } } } +function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + imports.wbg.__wbg_Error_52673b7de5a0ca89 = function(arg0, arg1) { + const ret = Error(getStringFromWasm0(arg0, arg1)); + return addHeapObject(ret); + }; + imports.wbg.__wbg_Number_2d1dcfcf4ec51736 = function(arg0) { + const ret = Number(getObject(arg0)); + return ret; + }; + imports.wbg.__wbg___wbindgen_bigint_get_as_i64_6e32f5e6aff02e1d = function(arg0, arg1) { + const v = getObject(arg1); + const ret = typeof(v) === 'bigint' ? v : undefined; + getDataViewMemory0().setBigInt64(arg0 + 8 * 1, isLikeNone(ret) ? BigInt(0) : ret, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true); + }; + imports.wbg.__wbg___wbindgen_boolean_get_dea25b33882b895b = function(arg0) { + const v = getObject(arg0); + const ret = typeof(v) === 'boolean' ? v : undefined; + return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0; + }; + imports.wbg.__wbg___wbindgen_debug_string_adfb662ae34724b6 = function(arg0, arg1) { + const ret = debugString(getObject(arg1)); + const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2); + const len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }; + imports.wbg.__wbg___wbindgen_in_0d3e1e8f0c669317 = function(arg0, arg1) { + const ret = getObject(arg0) in getObject(arg1); + return ret; + }; + imports.wbg.__wbg___wbindgen_is_bigint_0e1a2e3f55cfae27 = function(arg0) { + const ret = typeof(getObject(arg0)) === 'bigint'; + return ret; + }; + imports.wbg.__wbg___wbindgen_is_function_8d400b8b1af978cd = function(arg0) { + const ret = typeof(getObject(arg0)) === 'function'; + return ret; + }; + imports.wbg.__wbg___wbindgen_is_object_ce774f3490692386 = function(arg0) { + const val = getObject(arg0); + const ret = typeof(val) === 'object' && val !== null; + return ret; + }; + imports.wbg.__wbg___wbindgen_is_undefined_f6b95eab589e0269 = function(arg0) { + const ret = getObject(arg0) === undefined; + return ret; + }; + imports.wbg.__wbg___wbindgen_jsval_eq_b6101cc9cef1fe36 = function(arg0, arg1) { + const ret = getObject(arg0) === getObject(arg1); + return ret; + }; + imports.wbg.__wbg___wbindgen_jsval_loose_eq_766057600fdd1b0d = function(arg0, arg1) { + const ret = getObject(arg0) == getObject(arg1); + return ret; + }; + imports.wbg.__wbg___wbindgen_number_get_9619185a74197f95 = function(arg0, arg1) { + const obj = getObject(arg1); + const ret = typeof(obj) === 'number' ? obj : undefined; + getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true); + }; + imports.wbg.__wbg___wbindgen_string_get_a2a31e16edf96e42 = function(arg0, arg1) { + const obj = getObject(arg1); + const ret = typeof(obj) === 'string' ? obj : undefined; + var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2); + var len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }; + imports.wbg.__wbg___wbindgen_throw_dd24417ed36fc46e = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }; + imports.wbg.__wbg_call_abb4ff46ce38be40 = function() { return handleError(function (arg0, arg1) { + const ret = getObject(arg0).call(getObject(arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_done_62ea16af4ce34b24 = function(arg0) { + const ret = getObject(arg0).done; + return ret; + }; + imports.wbg.__wbg_error_7bc7d576a6aaf855 = function(arg0) { + console.error(getObject(arg0)); + }; + imports.wbg.__wbg_get_6b7bd52aca3f9671 = function(arg0, arg1) { + const ret = getObject(arg0)[arg1 >>> 0]; + return addHeapObject(ret); + }; + imports.wbg.__wbg_get_af9dab7e9603ea93 = function() { return handleError(function (arg0, arg1) { + const ret = Reflect.get(getObject(arg0), getObject(arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_get_with_ref_key_1dc361bd10053bfe = function(arg0, arg1) { + const ret = getObject(arg0)[getObject(arg1)]; + return addHeapObject(ret); + }; + imports.wbg.__wbg_instanceof_ArrayBuffer_f3320d2419cd0355 = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof ArrayBuffer; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_instanceof_Uint8Array_da54ccc9d3e09434 = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof Uint8Array; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_isArray_51fd9e6422c0a395 = function(arg0) { + const ret = Array.isArray(getObject(arg0)); + return ret; + }; + imports.wbg.__wbg_isSafeInteger_ae7d3f054d55fa16 = function(arg0) { + const ret = Number.isSafeInteger(getObject(arg0)); + return ret; + }; + imports.wbg.__wbg_iterator_27b7c8b35ab3e86b = function() { + const ret = Symbol.iterator; + return addHeapObject(ret); + }; + imports.wbg.__wbg_js_click_element_2fe1e774f3d232c7 = function(arg0) { + js_click_element(arg0); + }; + imports.wbg.__wbg_length_22ac23eaec9d8053 = function(arg0) { + const ret = getObject(arg0).length; + return ret; + }; + imports.wbg.__wbg_length_d45040a40c570362 = function(arg0) { + const ret = getObject(arg0).length; + return ret; + }; + imports.wbg.__wbg_new_1ba21ce319a06297 = function() { + const ret = new Object(); + return addHeapObject(ret); + }; + imports.wbg.__wbg_new_25f239778d6112b9 = function() { + const ret = new Array(); + return addHeapObject(ret); + }; + imports.wbg.__wbg_new_6421f6084cc5bc5a = function(arg0) { + const ret = new Uint8Array(getObject(arg0)); + return addHeapObject(ret); + }; + imports.wbg.__wbg_next_138a17bbf04e926c = function(arg0) { + const ret = getObject(arg0).next; + return addHeapObject(ret); + }; + imports.wbg.__wbg_next_3cfe5c0fe2a4cc53 = function() { return handleError(function (arg0) { + const ret = getObject(arg0).next(); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_prototypesetcall_dfe9b766cdc1f1fd = function(arg0, arg1, arg2) { + Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), getObject(arg2)); + }; + imports.wbg.__wbg_set_3f1d0b984ed272ed = function(arg0, arg1, arg2) { + getObject(arg0)[takeObject(arg1)] = takeObject(arg2); + }; + imports.wbg.__wbg_set_7df433eea03a5c14 = function(arg0, arg1, arg2) { + getObject(arg0)[arg1 >>> 0] = takeObject(arg2); + }; + imports.wbg.__wbg_value_57b7b035e117f7ee = function(arg0) { + const ret = getObject(arg0).value; + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) { + // Cast intrinsic for `Ref(String) -> Externref`. + const ret = getStringFromWasm0(arg0, arg1); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_cast_4625c577ab2ec9ee = function(arg0) { + // Cast intrinsic for `U64 -> Externref`. + const ret = BigInt.asUintN(64, arg0); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_cast_d6cd19b81560fd6e = function(arg0) { + // Cast intrinsic for `F64 -> Externref`. + const ret = arg0; + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_object_clone_ref = function(arg0) { + const ret = getObject(arg0); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_object_drop_ref = function(arg0) { + takeObject(arg0); + }; + + return imports; +} + +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + cachedDataViewMemory0 = null; + cachedUint8ArrayMemory0 = null; + + + + return wasm; +} + function initSync(module) { - if (void 0 !== wasm) return wasm; - void 0 !== module && Object.getPrototypeOf(module) === Object.prototype && ({module: module} = module); + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined') { + if (Object.getPrototypeOf(module) === Object.prototype) { + ({module} = module) + } else { + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + } + } + const imports = __wbg_get_imports(); - module instanceof WebAssembly.Module || (module = new WebAssembly.Module(module)); - return __wbg_finalize_init(new WebAssembly.Instance(module, imports), module); + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + const instance = new WebAssembly.Instance(module, imports); + return __wbg_finalize_init(instance, module); } async function __wbg_init(module_or_path) { - if (void 0 !== wasm) return wasm; - void 0 !== module_or_path && Object.getPrototypeOf(module_or_path) === Object.prototype && ({module_or_path: module_or_path} = module_or_path), - void 0 === module_or_path && (module_or_path = new URL("sentience_core_bg.wasm", import.meta.url)); + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined') { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (typeof module_or_path === 'undefined') { + module_or_path = new URL('sentience_core_bg.wasm', import.meta.url); + } const imports = __wbg_get_imports(); - ("string" == typeof module_or_path || "function" == typeof Request && module_or_path instanceof Request || "function" == typeof URL && module_or_path instanceof URL) && (module_or_path = fetch(module_or_path)); - const {instance: instance, module: module} = await __wbg_load(await module_or_path, imports); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + const { instance, module } = await __wbg_load(await module_or_path, imports); + return __wbg_finalize_init(instance, module); } -export { initSync, __wbg_init as default }; +export { initSync }; +export default __wbg_init; diff --git a/sentience/extension/pkg/sentience_core_bg.wasm b/sentience/extension/pkg/sentience_core_bg.wasm index a802ced0fcfc3b72a57d6d63436f443173a21c29..3eb0109fd1237108d9eac346546b514e4dee6664 100644 GIT binary patch delta 22641 zcmb7M349dA(w~~$Y&O{(6YdC-1FDk?tlJr7XOposVc1qB5G`To^2yGa1Q_kH?Hx~sanyQb^t>go>j zj<}Zm;F^Cb{7iIL$CSbmW;5zCfGY_F1yc%3#}fSQaR%C@3x;H@=`eP@K4S`$ta-6lCWV<`kx7W(U&Kb4qdo0gJ?Z(9yQC*q;qcg67W>lEGYaz*YD9sLFUTn_$;!+O z1pH~4{_LXkEbF_QE@g36c3M`NKP#;$J0~sEmtpbI&gOlhWVs+4i?n??5yJK>=G<$lMiD7rx*G&(y`9zdHytCMqZA^ zc6)oUia@!)0=kuf6y9y^qj)NlC-QqUWTv8V%8o*Qe+mD1hR|L zN&*>v)<|2`>`!N&OUDkD=2AMgAPowT9>^_8EAsjBGShRc`E0ecXeDXI$ntSh3W_29 ztn{2fai*`ND9aa6!dQG=%oihCR|X~&6#CNB(xFsE0jO*s(`OB43-9_#V+yg>8O7JgyR+4{pS02J3|~%WVNP)%+n-kC&&{)j>1wTZBc4hp78I6_C>>i- zkQPX9?#~EhmSkramG}c0Io6_>5$>c`MS+~0tdh*Eyfj~4MrN)pT=RN&+iUZD>3Qk7 z+5Q};bWRC$M1IcY7fm#=qP%phrQ-e!e`b0h-4`f^h6l1TtS;sk?slz;1OANc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8i$*Cykrf)#%hqOQ}SVON@Z2-tU)gT396oHgmzZHHjoSiD_;WPE_&~rs!_HP3g_IlZ zg_Jqf2Pt=&jFe|OvvgKzWpU|D{IN-8jYYfOqAkFmBrU0MyUQ!wMYAjN%S+sg#oo0P zQ?(le%zi~VuXyuCKKUEl@> zPrgcN?Auq_Z;kT&eTp}KZeDk9{^zYh%2OU1wB*TuUGG#HEPORa02_W(8#7N{j`@m5i zSR7ps@A~l#q=|Po(BA{nL(C;^JTS~aP(SjjP*V;Ik+KMM!_P2mVYgUPLzCeE{Hq4> zZl`c;q)Yuivunx}ars6H4=4&VMQWxf-$(;T4!X#fJ<=xG5#otkN{T+F`W|fK1yC^)v1yJ)LEu5ZTfxpxziE~lL1NSsD zud<@Fyu_tU6;tk_Go7+KX>v{zkn;X|O%w=3MQ;*M3Az`K)U-_q_HGkfH_?!Q!<{vy z2WU2mPdCxM0XKEgD#19L>jiP)zWqHy6fX3o++<4%L?esuEk^c(Me_x*B}iCx?C)&r{0ks^g63}J5pZqa`99>^@|<~a2S4a@xzLo ze^^~9`f~AmJuVtYiN5=3K Date: Thu, 22 Jan 2026 07:59:35 -0800 Subject: [PATCH 03/11] fix tests --- sentience/extension/background.js | 2 +- sentience/extension/injected_api.js | 81 ++++++++++++++++++++++++++++- sentience/extension/manifest.json | 6 +-- 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/sentience/extension/background.js b/sentience/extension/background.js index 2923f55..b5192d9 100644 --- a/sentience/extension/background.js +++ b/sentience/extension/background.js @@ -1,4 +1,4 @@ -import init, { analyze_page_with_options, analyze_page, prune_for_api } from "../pkg/sentience_core.js"; +import init, { analyze_page_with_options, analyze_page, prune_for_api } from "./pkg/sentience_core.js"; let wasmReady = !1, wasmInitPromise = null; diff --git a/sentience/extension/injected_api.js b/sentience/extension/injected_api.js index 9230b8e..5e99841 100644 --- a/sentience/extension/injected_api.js +++ b/sentience/extension/injected_api.js @@ -792,7 +792,9 @@ } }); } catch (error) {} - const processed = await function(rawData, options) { + let processed = null; + try { + processed = await function(rawData, options) { return new Promise((resolve, reject) => { const requestId = Math.random().toString(36).substring(7); let resolved = !1; @@ -823,7 +825,82 @@ } }); }(allRawElements, options); - if (!processed || !processed.elements) throw new Error("WASM processing returned invalid result"); + } catch (error) { + processed = { + elements: (allRawElements || []).map(r => { + const rect = r && r.rect || { + x: 0, + y: 0, + width: 0, + height: 0 + }, attrs = r && r.attributes || {}, role = attrs.role || r && (r.inferred_role || r.inferredRole) || ("a" === r.tag ? "link" : "generic"); + return { + id: Number(r && r.id || 0), + role: String(role || "generic"), + text: r && (r.text || r.semantic_text || r.semanticText) || null, + importance: 1, + bbox: { + x: Number(rect.x || 0), + y: Number(rect.y || 0), + width: Number(rect.width || 0), + height: Number(rect.height || 0) + }, + visual_cues: { + is_primary: !1, + is_clickable: !1 + }, + in_viewport: !0, + is_occluded: !!(r && (r.occluded || r.is_occluded)), + z_index: 0, + name: attrs.aria_label || attrs.ariaLabel || null, + value: r && r.value || null, + input_type: attrs.type_ || attrs.type || null, + checked: "boolean" == typeof r.checked ? r.checked : null, + disabled: "boolean" == typeof r.disabled ? r.disabled : null, + expanded: "boolean" == typeof r.expanded ? r.expanded : null + }; + }), + raw_elements: allRawElements, + duration: null + }; + } + if (!processed || !processed.elements) processed = { + elements: (allRawElements || []).map(r => { + const rect = r && r.rect || { + x: 0, + y: 0, + width: 0, + height: 0 + }, attrs = r && r.attributes || {}, role = attrs.role || r && (r.inferred_role || r.inferredRole) || ("a" === r.tag ? "link" : "generic"); + return { + id: Number(r && r.id || 0), + role: String(role || "generic"), + text: r && (r.text || r.semantic_text || r.semanticText) || null, + importance: 1, + bbox: { + x: Number(rect.x || 0), + y: Number(rect.y || 0), + width: Number(rect.width || 0), + height: Number(rect.height || 0) + }, + visual_cues: { + is_primary: !1, + is_clickable: !1 + }, + in_viewport: !0, + is_occluded: !!(r && (r.occluded || r.is_occluded)), + z_index: 0, + name: attrs.aria_label || attrs.ariaLabel || null, + value: r && r.value || null, + input_type: attrs.type_ || attrs.type || null, + checked: "boolean" == typeof r.checked ? r.checked : null, + disabled: "boolean" == typeof r.disabled ? r.disabled : null, + expanded: "boolean" == typeof r.expanded ? r.expanded : null + }; + }), + raw_elements: allRawElements, + duration: null + }; let screenshot = null; options.screenshot && (screenshot = await function(options) { return new Promise(resolve => { diff --git a/sentience/extension/manifest.json b/sentience/extension/manifest.json index 23b3562..a2d123d 100644 --- a/sentience/extension/manifest.json +++ b/sentience/extension/manifest.json @@ -6,7 +6,7 @@ "permissions": ["activeTab", "scripting"], "host_permissions": [""], "background": { - "service_worker": "dist/background.js", + "service_worker": "background.js", "type": "module" }, "web_accessible_resources": [ @@ -18,13 +18,13 @@ "content_scripts": [ { "matches": [""], - "js": ["dist/content.js"], + "js": ["content.js"], "run_at": "document_start", "all_frames": true }, { "matches": [""], - "js": ["dist/injected_api.js"], + "js": ["injected_api.js"], "run_at": "document_idle", "world": "MAIN", "all_frames": true From 7ad552b770a71e86995a3f795dc544569320a111 Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Thu, 22 Jan 2026 08:37:47 -0800 Subject: [PATCH 04/11] make manifest.json point back to dist --- sentience/extension/background.js | 6 +- sentience/extension/content.js | 18 ++--- sentience/extension/injected_api.js | 80 +++++++++++------------ sentience/extension/manifest.json | 6 +- sentience/extension/pkg/sentience_core.js | 12 ++-- 5 files changed, 61 insertions(+), 61 deletions(-) diff --git a/sentience/extension/background.js b/sentience/extension/background.js index b5192d9..02c0408 100644 --- a/sentience/extension/background.js +++ b/sentience/extension/background.js @@ -28,14 +28,14 @@ async function handleSnapshotProcessing(rawData, options = {}) { const startTime = performance.now(); try { if (!Array.isArray(rawData)) throw new Error("rawData must be an array"); - if (rawData.length > 1e4 && (rawData = rawData.slice(0, 1e4)), await initWASM(), + if (rawData.length > 1e4 && (rawData = rawData.slice(0, 1e4)), await initWASM(), !wasmReady) throw new Error("WASM module not initialized"); let analyzedElements, prunedRawData; try { const wasmPromise = new Promise((resolve, reject) => { try { let result; - result = options.limit || options.filter ? analyze_page_with_options(rawData, options) : analyze_page(rawData), + result = options.limit || options.filter ? analyze_page_with_options(rawData, options) : analyze_page(rawData), resolve(result); } catch (e) { reject(e); @@ -101,4 +101,4 @@ initWASM().catch(err => {}), chrome.runtime.onMessage.addListener((request, send event.preventDefault(); }), self.addEventListener("unhandledrejection", event => { event.preventDefault(); -}); \ No newline at end of file +}); diff --git a/sentience/extension/content.js b/sentience/extension/content.js index b65cfb5..97923a2 100644 --- a/sentience/extension/content.js +++ b/sentience/extension/content.js @@ -82,7 +82,7 @@ if (!elements || !Array.isArray(elements)) return; removeOverlay(); const host = document.createElement("div"); - host.id = OVERLAY_HOST_ID, host.style.cssText = "\n position: fixed !important;\n top: 0 !important;\n left: 0 !important;\n width: 100vw !important;\n height: 100vh !important;\n pointer-events: none !important;\n z-index: 2147483647 !important;\n margin: 0 !important;\n padding: 0 !important;\n ", + host.id = OVERLAY_HOST_ID, host.style.cssText = "\n position: fixed !important;\n top: 0 !important;\n left: 0 !important;\n width: 100vw !important;\n height: 100vh !important;\n pointer-events: none !important;\n z-index: 2147483647 !important;\n margin: 0 !important;\n padding: 0 !important;\n ", document.body.appendChild(host); const shadow = host.attachShadow({ mode: "closed" @@ -94,15 +94,15 @@ let color; color = isTarget ? "#FF0000" : isPrimary ? "#0066FF" : "#00FF00"; const importanceRatio = maxImportance > 0 ? importance / maxImportance : .5, borderOpacity = isTarget ? 1 : isPrimary ? .9 : Math.max(.4, .5 + .5 * importanceRatio), fillOpacity = .2 * borderOpacity, borderWidth = isTarget ? 2 : isPrimary ? 1.5 : Math.max(.5, Math.round(2 * importanceRatio)), hexOpacity = Math.round(255 * fillOpacity).toString(16).padStart(2, "0"), box = document.createElement("div"); - if (box.style.cssText = `\n position: absolute;\n left: ${bbox.x}px;\n top: ${bbox.y}px;\n width: ${bbox.width}px;\n height: ${bbox.height}px;\n border: ${borderWidth}px solid ${color};\n background-color: ${color}${hexOpacity};\n box-sizing: border-box;\n opacity: ${borderOpacity};\n pointer-events: none;\n `, + if (box.style.cssText = `\n position: absolute;\n left: ${bbox.x}px;\n top: ${bbox.y}px;\n width: ${bbox.width}px;\n height: ${bbox.height}px;\n border: ${borderWidth}px solid ${color};\n background-color: ${color}${hexOpacity};\n box-sizing: border-box;\n opacity: ${borderOpacity};\n pointer-events: none;\n `, importance > 0 || isPrimary) { const badge = document.createElement("span"); - badge.textContent = isPrimary ? `⭐${importance}` : `${importance}`, badge.style.cssText = `\n position: absolute;\n top: -18px;\n left: 0;\n background: ${color};\n color: white;\n font-size: 11px;\n font-weight: bold;\n padding: 2px 6px;\n font-family: Arial, sans-serif;\n border-radius: 3px;\n opacity: 0.95;\n white-space: nowrap;\n pointer-events: none;\n `, + badge.textContent = isPrimary ? `⭐${importance}` : `${importance}`, badge.style.cssText = `\n position: absolute;\n top: -18px;\n left: 0;\n background: ${color};\n color: white;\n font-size: 11px;\n font-weight: bold;\n padding: 2px 6px;\n font-family: Arial, sans-serif;\n border-radius: 3px;\n opacity: 0.95;\n white-space: nowrap;\n pointer-events: none;\n `, box.appendChild(badge); } if (isTarget) { const targetIndicator = document.createElement("span"); - targetIndicator.textContent = "🎯", targetIndicator.style.cssText = "\n position: absolute;\n top: -18px;\n right: 0;\n font-size: 16px;\n pointer-events: none;\n ", + targetIndicator.textContent = "🎯", targetIndicator.style.cssText = "\n position: absolute;\n top: -18px;\n right: 0;\n font-size: 16px;\n pointer-events: none;\n ", box.appendChild(targetIndicator); } shadow.appendChild(box); @@ -122,7 +122,7 @@ if (!grids || !Array.isArray(grids)) return; removeOverlay(); const host = document.createElement("div"); - host.id = OVERLAY_HOST_ID, host.style.cssText = "\n position: fixed !important;\n top: 0 !important;\n left: 0 !important;\n width: 100vw !important;\n height: 100vh !important;\n pointer-events: none !important;\n z-index: 2147483647 !important;\n margin: 0 !important;\n padding: 0 !important;\n ", + host.id = OVERLAY_HOST_ID, host.style.cssText = "\n position: fixed !important;\n top: 0 !important;\n left: 0 !important;\n width: 100vw !important;\n height: 100vh !important;\n pointer-events: none !important;\n z-index: 2147483647 !important;\n margin: 0 !important;\n padding: 0 !important;\n ", document.body.appendChild(host); const shadow = host.attachShadow({ mode: "closed" @@ -138,10 +138,10 @@ let labelText = grid.label ? `Grid ${grid.grid_id}: ${grid.label}` : `Grid ${grid.grid_id}`; grid.is_dominant && (labelText = `⭐ ${labelText} (dominant)`); const badge = document.createElement("span"); - if (badge.textContent = labelText, badge.style.cssText = `\n position: absolute;\n top: -18px;\n left: 0;\n background: ${color};\n color: white;\n font-size: 11px;\n font-weight: bold;\n padding: 2px 6px;\n font-family: Arial, sans-serif;\n border-radius: 3px;\n opacity: 0.95;\n white-space: nowrap;\n pointer-events: none;\n `, + if (badge.textContent = labelText, badge.style.cssText = `\n position: absolute;\n top: -18px;\n left: 0;\n background: ${color};\n color: white;\n font-size: 11px;\n font-weight: bold;\n padding: 2px 6px;\n font-family: Arial, sans-serif;\n border-radius: 3px;\n opacity: 0.95;\n white-space: nowrap;\n pointer-events: none;\n `, box.appendChild(badge), isTarget) { const targetIndicator = document.createElement("span"); - targetIndicator.textContent = "🎯", targetIndicator.style.cssText = "\n position: absolute;\n top: -18px;\n right: 0;\n font-size: 16px;\n pointer-events: none;\n ", + targetIndicator.textContent = "🎯", targetIndicator.style.cssText = "\n position: absolute;\n top: -18px;\n right: 0;\n font-size: 16px;\n pointer-events: none;\n ", box.appendChild(targetIndicator); } shadow.appendChild(box); @@ -155,7 +155,7 @@ let overlayTimeout = null; function removeOverlay() { const existing = document.getElementById(OVERLAY_HOST_ID); - existing && existing.remove(), overlayTimeout && (clearTimeout(overlayTimeout), + existing && existing.remove(), overlayTimeout && (clearTimeout(overlayTimeout), overlayTimeout = null); } -}(); \ No newline at end of file +}(); diff --git a/sentience/extension/injected_api.js b/sentience/extension/injected_api.js index 5e99841..b10f596 100644 --- a/sentience/extension/injected_api.js +++ b/sentience/extension/injected_api.js @@ -103,9 +103,9 @@ const iframes = document.querySelectorAll("iframe"); for (const iframe of iframes) { const src = iframe.getAttribute("src") || "", title = iframe.getAttribute("title") || ""; - if (src) for (const [provider, hints] of Object.entries(CAPTCHA_IFRAME_HINTS)) matchHints(src, hints) && (hasIframeHit = !0, + if (src) for (const [provider, hints] of Object.entries(CAPTCHA_IFRAME_HINTS)) matchHints(src, hints) && (hasIframeHit = !0, providerSignals[provider] += 1, addEvidence(evidence.iframe_src_hits, truncateText(src, 120))); - if (title && matchHints(title, [ "captcha", "recaptcha" ]) && (hasContainerHit = !0, + if (title && matchHints(title, [ "captcha", "recaptcha" ]) && (hasContainerHit = !0, addEvidence(evidence.selector_hits, 'iframe[title*="captcha"]')), evidence.iframe_src_hits.length >= 5) break; } } catch (e) {} @@ -114,14 +114,14 @@ for (const script of scripts) { const src = script.getAttribute("src") || ""; if (src) { - for (const [provider, hints] of Object.entries(CAPTCHA_SCRIPT_HINTS)) matchHints(src, hints) && (hasScriptHit = !0, + for (const [provider, hints] of Object.entries(CAPTCHA_SCRIPT_HINTS)) matchHints(src, hints) && (hasScriptHit = !0, providerSignals[provider] += 1, addEvidence(evidence.selector_hits, `script[src*="${hints[0]}"]`)); if (evidence.selector_hits.length >= 5) break; } } } catch (e) {} for (const {selector: selector, provider: provider} of CAPTCHA_CONTAINER_SELECTORS) try { - document.querySelector(selector) && (hasContainerHit = !0, addEvidence(evidence.selector_hits, selector), + document.querySelector(selector) && (hasContainerHit = !0, addEvidence(evidence.selector_hits, selector), "unknown" !== provider && (providerSignals[provider] += 1)); } catch (e) {} const textSnippet = function() { @@ -139,7 +139,7 @@ } catch (e) {} try { let bodyText = document.body?.innerText || ""; - return !bodyText && document.body?.textContent && (bodyText = document.body.textContent), + return !bodyText && document.body?.textContent && (bodyText = document.body.textContent), truncateText(bodyText.replace(/\s+/g, " ").trim(), 2e3); } catch (e) { return ""; @@ -147,21 +147,21 @@ }(); if (textSnippet) { const lowerText = textSnippet.toLowerCase(); - for (const keyword of CAPTCHA_TEXT_KEYWORDS) lowerText.includes(keyword) && (hasKeywordHit = !0, + for (const keyword of CAPTCHA_TEXT_KEYWORDS) lowerText.includes(keyword) && (hasKeywordHit = !0, addEvidence(evidence.text_hits, keyword)); } try { const lowerUrl = (window.location?.href || "").toLowerCase(); - for (const hint of CAPTCHA_URL_HINTS) lowerUrl.includes(hint) && (hasUrlHit = !0, + for (const hint of CAPTCHA_URL_HINTS) lowerUrl.includes(hint) && (hasUrlHit = !0, addEvidence(evidence.url_hits, hint)); } catch (e) {} let confidence = 0; - hasIframeHit && (confidence += .7), hasContainerHit && (confidence += .5), hasScriptHit && (confidence += .5), - hasKeywordHit && (confidence += .3), hasUrlHit && (confidence += .2), confidence = Math.min(1, confidence), + hasIframeHit && (confidence += .7), hasContainerHit && (confidence += .5), hasScriptHit && (confidence += .5), + hasKeywordHit && (confidence += .3), hasUrlHit && (confidence += .2), confidence = Math.min(1, confidence), hasIframeHit && (confidence = Math.max(confidence, .8)), !hasKeywordHit || hasIframeHit || hasContainerHit || hasScriptHit || hasUrlHit || (confidence = Math.min(confidence, .4)); const detected = confidence >= .7; let providerHint = null; - return providerSignals.recaptcha > 0 ? providerHint = "recaptcha" : providerSignals.hcaptcha > 0 ? providerHint = "hcaptcha" : providerSignals.turnstile > 0 ? providerHint = "turnstile" : providerSignals.arkose > 0 ? providerHint = "arkose" : providerSignals.awswaf > 0 ? providerHint = "awswaf" : detected && (providerHint = "unknown"), + return providerSignals.recaptcha > 0 ? providerHint = "recaptcha" : providerSignals.hcaptcha > 0 ? providerHint = "hcaptcha" : providerSignals.turnstile > 0 ? providerHint = "turnstile" : providerSignals.arkose > 0 ? providerHint = "arkose" : providerSignals.awswaf > 0 ? providerHint = "awswaf" : detected && (providerHint = "unknown"), { detected: detected, provider_hint: providerHint, @@ -271,7 +271,7 @@ if (labelEl) { let text = ""; try { - if (text = (labelEl.innerText || "").trim(), !text && labelEl.textContent && (text = labelEl.textContent.trim()), + if (text = (labelEl.innerText || "").trim(), !text && labelEl.textContent && (text = labelEl.textContent.trim()), !text && labelEl.getAttribute) { const ariaLabel = labelEl.getAttribute("aria-label"); ariaLabel && (text = ariaLabel.trim()); @@ -466,7 +466,7 @@ }); const checkStable = () => { const timeSinceLastChange = Date.now() - lastChange, totalWait = Date.now() - startTime; - timeSinceLastChange >= quietPeriod || totalWait >= maxWait ? (observer.disconnect(), + timeSinceLastChange >= quietPeriod || totalWait >= maxWait ? (observer.disconnect(), resolve()) : setTimeout(checkStable, 50); }; checkStable(); @@ -492,7 +492,7 @@ }); const checkQuiet = () => { const timeSinceLastChange = Date.now() - lastChange, totalWait = Date.now() - startTime; - timeSinceLastChange >= quietPeriod || totalWait >= maxWait ? (quietObserver.disconnect(), + timeSinceLastChange >= quietPeriod || totalWait >= maxWait ? (quietObserver.disconnect(), resolve()) : setTimeout(checkQuiet, 50); }; checkQuiet(); @@ -607,7 +607,7 @@ }(el); let safeValue = null, valueRedacted = null; try { - if (void 0 !== el.value || el.getAttribute && null !== el.getAttribute("value")) if (isPasswordInput) safeValue = null, + if (void 0 !== el.value || el.getAttribute && null !== el.getAttribute("value")) if (isPasswordInput) safeValue = null, valueRedacted = "true"; else { const rawValue = void 0 !== el.value ? String(el.value) : String(el.getAttribute("value")); safeValue = rawValue.length > 200 ? rawValue.substring(0, 200) : rawValue, valueRedacted = "false"; @@ -734,8 +734,8 @@ const requestId = `iframe-${idx}-${Date.now()}`, timeout = setTimeout(() => { resolve(null); }, 5e3), listener = event => { - "SENTIENCE_IFRAME_SNAPSHOT_RESPONSE" === event.data?.type && event.data, "SENTIENCE_IFRAME_SNAPSHOT_RESPONSE" === event.data?.type && event.data?.requestId === requestId && (clearTimeout(timeout), - window.removeEventListener("message", listener), event.data.error ? resolve(null) : (event.data.snapshot, + "SENTIENCE_IFRAME_SNAPSHOT_RESPONSE" === event.data?.type && event.data, "SENTIENCE_IFRAME_SNAPSHOT_RESPONSE" === event.data?.type && event.data?.requestId === requestId && (clearTimeout(timeout), + window.removeEventListener("message", listener), event.data.error ? resolve(null) : (event.data.snapshot, resolve({ iframe: iframe, data: event.data.snapshot, @@ -751,7 +751,7 @@ ...options, collectIframes: !0 } - }, "*") : (clearTimeout(timeout), window.removeEventListener("message", listener), + }, "*") : (clearTimeout(timeout), window.removeEventListener("message", listener), resolve(null)); } catch (error) { clearTimeout(timeout), window.removeEventListener("message", listener), resolve(null); @@ -803,7 +803,7 @@ }, 25e3), listener = e => { if ("SENTIENCE_SNAPSHOT_RESULT" === e.data.type && e.data.requestId === requestId) { if (resolved) return; - resolved = !0, clearTimeout(timeout), window.removeEventListener("message", listener), + resolved = !0, clearTimeout(timeout), window.removeEventListener("message", listener), e.data.error ? reject(new Error(e.data.error)) : resolve({ elements: e.data.elements, raw_elements: e.data.raw_elements, @@ -820,7 +820,7 @@ options: options }, "*"); } catch (error) { - resolved || (resolved = !0, clearTimeout(timeout), window.removeEventListener("message", listener), + resolved || (resolved = !0, clearTimeout(timeout), window.removeEventListener("message", listener), reject(new Error(`Failed to send snapshot request: ${error.message}`))); } }); @@ -905,7 +905,7 @@ options.screenshot && (screenshot = await function(options) { return new Promise(resolve => { const requestId = Math.random().toString(36).substring(7), listener = e => { - "SENTIENCE_SCREENSHOT_RESULT" === e.data.type && e.data.requestId === requestId && (window.removeEventListener("message", listener), + "SENTIENCE_SCREENSHOT_RESULT" === e.data.type && e.data.requestId === requestId && (window.removeEventListener("message", listener), resolve(e.data.screenshot)); }; window.addEventListener("message", listener), window.postMessage({ @@ -965,15 +965,15 @@ } if (node.nodeType !== Node.ELEMENT_NODE) return; const tag = node.tagName.toLowerCase(); - if ("h1" === tag && (markdown += "\n# "), "h2" === tag && (markdown += "\n## "), - "h3" === tag && (markdown += "\n### "), "li" === tag && (markdown += "\n- "), insideLink || "p" !== tag && "div" !== tag && "br" !== tag || (markdown += "\n"), - "strong" !== tag && "b" !== tag || (markdown += "**"), "em" !== tag && "i" !== tag || (markdown += "_"), - "a" === tag && (markdown += "[", insideLink = !0), node.shadowRoot ? Array.from(node.shadowRoot.childNodes).forEach(walk) : node.childNodes.forEach(walk), + if ("h1" === tag && (markdown += "\n# "), "h2" === tag && (markdown += "\n## "), + "h3" === tag && (markdown += "\n### "), "li" === tag && (markdown += "\n- "), insideLink || "p" !== tag && "div" !== tag && "br" !== tag || (markdown += "\n"), + "strong" !== tag && "b" !== tag || (markdown += "**"), "em" !== tag && "i" !== tag || (markdown += "_"), + "a" === tag && (markdown += "[", insideLink = !0), node.shadowRoot ? Array.from(node.shadowRoot.childNodes).forEach(walk) : node.childNodes.forEach(walk), "a" === tag) { const href = node.getAttribute("href"); markdown += href ? `](${href})` : "]", insideLink = !1; } - "strong" !== tag && "b" !== tag || (markdown += "**"), "em" !== tag && "i" !== tag || (markdown += "_"), + "strong" !== tag && "b" !== tag || (markdown += "**"), "em" !== tag && "i" !== tag || (markdown += "_"), insideLink || "h1" !== tag && "h2" !== tag && "h3" !== tag && "p" !== tag && "div" !== tag || (markdown += "\n"); }(tempDiv), markdown.replace(/\n{3,}/g, "\n\n").trim(); }(document.body) : function(root) { @@ -986,7 +986,7 @@ const style = window.getComputedStyle(node); if ("none" === style.display || "hidden" === style.visibility) return; const isBlock = "block" === style.display || "flex" === style.display || "P" === node.tagName || "DIV" === node.tagName; - isBlock && (text += " "), node.shadowRoot ? Array.from(node.shadowRoot.childNodes).forEach(walk) : node.childNodes.forEach(walk), + isBlock && (text += " "), node.shadowRoot ? Array.from(node.shadowRoot.childNodes).forEach(walk) : node.childNodes.forEach(walk), isBlock && (text += "\n"); } } else text += node.textContent; @@ -1085,25 +1085,25 @@ } function startRecording(options = {}) { const {highlightColor: highlightColor = "#ff0000", successColor: successColor = "#00ff00", autoDisableTimeout: autoDisableTimeout = 18e5, keyboardShortcut: keyboardShortcut = "Ctrl+Shift+I"} = options; - if (!window.sentience_registry || 0 === window.sentience_registry.length) return alert("Registry empty. Run `await window.sentience.snapshot()` first!"), + if (!window.sentience_registry || 0 === window.sentience_registry.length) return alert("Registry empty. Run `await window.sentience.snapshot()` first!"), () => {}; window.sentience_registry_map = new Map, window.sentience_registry.forEach((el, idx) => { el && window.sentience_registry_map.set(el, idx); }); let highlightBox = document.getElementById("sentience-highlight-box"); - highlightBox || (highlightBox = document.createElement("div"), highlightBox.id = "sentience-highlight-box", - highlightBox.style.cssText = `\n position: fixed;\n pointer-events: none;\n z-index: 2147483647;\n border: 2px solid ${highlightColor};\n background: rgba(255, 0, 0, 0.1);\n display: none;\n transition: all 0.1s ease;\n box-sizing: border-box;\n `, + highlightBox || (highlightBox = document.createElement("div"), highlightBox.id = "sentience-highlight-box", + highlightBox.style.cssText = `\n position: fixed;\n pointer-events: none;\n z-index: 2147483647;\n border: 2px solid ${highlightColor};\n background: rgba(255, 0, 0, 0.1);\n display: none;\n transition: all 0.1s ease;\n box-sizing: border-box;\n `, document.body.appendChild(highlightBox)); let recordingIndicator = document.getElementById("sentience-recording-indicator"); - recordingIndicator || (recordingIndicator = document.createElement("div"), recordingIndicator.id = "sentience-recording-indicator", - recordingIndicator.style.cssText = `\n position: fixed;\n top: 0;\n left: 0;\n right: 0;\n height: 3px;\n background: ${highlightColor};\n z-index: 2147483646;\n pointer-events: none;\n `, + recordingIndicator || (recordingIndicator = document.createElement("div"), recordingIndicator.id = "sentience-recording-indicator", + recordingIndicator.style.cssText = `\n position: fixed;\n top: 0;\n left: 0;\n right: 0;\n height: 3px;\n background: ${highlightColor};\n z-index: 2147483646;\n pointer-events: none;\n `, document.body.appendChild(recordingIndicator)), recordingIndicator.style.display = "block"; const mouseOverHandler = e => { const el = e.target; if (!el || el === highlightBox || el === recordingIndicator) return; const rect = el.getBoundingClientRect(); - highlightBox.style.display = "block", highlightBox.style.top = rect.top + window.scrollY + "px", - highlightBox.style.left = rect.left + window.scrollX + "px", highlightBox.style.width = rect.width + "px", + highlightBox.style.display = "block", highlightBox.style.top = rect.top + window.scrollY + "px", + highlightBox.style.left = rect.left + window.scrollX + "px", highlightBox.style.width = rect.width + "px", highlightBox.style.height = rect.height + "px"; }, clickHandler = e => { e.preventDefault(), e.stopPropagation(); @@ -1180,7 +1180,7 @@ debug_snapshot: rawData }, jsonString = JSON.stringify(snippet, null, 2); navigator.clipboard.writeText(jsonString).then(() => { - highlightBox.style.border = `2px solid ${successColor}`, highlightBox.style.background = "rgba(0, 255, 0, 0.2)", + highlightBox.style.border = `2px solid ${successColor}`, highlightBox.style.background = "rgba(0, 255, 0, 0.2)", setTimeout(() => { highlightBox.style.border = `2px solid ${highlightColor}`, highlightBox.style.background = "rgba(255, 0, 0, 0.1)"; }, 500); @@ -1190,15 +1190,15 @@ }; let timeoutId = null; const stopRecording = () => { - document.removeEventListener("mouseover", mouseOverHandler, !0), document.removeEventListener("click", clickHandler, !0), - document.removeEventListener("keydown", keyboardHandler, !0), timeoutId && (clearTimeout(timeoutId), - timeoutId = null), highlightBox && (highlightBox.style.display = "none"), recordingIndicator && (recordingIndicator.style.display = "none"), + document.removeEventListener("mouseover", mouseOverHandler, !0), document.removeEventListener("click", clickHandler, !0), + document.removeEventListener("keydown", keyboardHandler, !0), timeoutId && (clearTimeout(timeoutId), + timeoutId = null), highlightBox && (highlightBox.style.display = "none"), recordingIndicator && (recordingIndicator.style.display = "none"), window.sentience_registry_map && window.sentience_registry_map.clear(), window.sentience_stopRecording === stopRecording && delete window.sentience_stopRecording; }, keyboardHandler = e => { - (e.ctrlKey || e.metaKey) && e.shiftKey && "I" === e.key && (e.preventDefault(), + (e.ctrlKey || e.metaKey) && e.shiftKey && "I" === e.key && (e.preventDefault(), stopRecording()); }; - return document.addEventListener("mouseover", mouseOverHandler, !0), document.addEventListener("click", clickHandler, !0), + return document.addEventListener("mouseover", mouseOverHandler, !0), document.addEventListener("click", clickHandler, !0), document.addEventListener("keydown", keyboardHandler, !0), autoDisableTimeout > 0 && (timeoutId = setTimeout(() => { stopRecording(); }, autoDisableTimeout)), window.sentience_stopRecording = stopRecording, stopRecording; @@ -1267,4 +1267,4 @@ } }), window.sentience_iframe_handler_setup = !0)); })(); -}(); \ No newline at end of file +}(); diff --git a/sentience/extension/manifest.json b/sentience/extension/manifest.json index a2d123d..23b3562 100644 --- a/sentience/extension/manifest.json +++ b/sentience/extension/manifest.json @@ -6,7 +6,7 @@ "permissions": ["activeTab", "scripting"], "host_permissions": [""], "background": { - "service_worker": "background.js", + "service_worker": "dist/background.js", "type": "module" }, "web_accessible_resources": [ @@ -18,13 +18,13 @@ "content_scripts": [ { "matches": [""], - "js": ["content.js"], + "js": ["dist/content.js"], "run_at": "document_start", "all_frames": true }, { "matches": [""], - "js": ["injected_api.js"], + "js": ["dist/injected_api.js"], "run_at": "document_idle", "world": "MAIN", "all_frames": true diff --git a/sentience/extension/pkg/sentience_core.js b/sentience/extension/pkg/sentience_core.js index aeffea1..5684fd6 100644 --- a/sentience/extension/pkg/sentience_core.js +++ b/sentience/extension/pkg/sentience_core.js @@ -25,7 +25,7 @@ function __wbg_get_imports() { }, __wbg___wbindgen_bigint_get_as_i64_8fcf4ce7f1ca72a2: function(arg0, arg1) { const v = getObject(arg1), ret = "bigint" == typeof v ? v : void 0; - getDataViewMemory0().setBigInt64(arg0 + 8, isLikeNone(ret) ? BigInt(0) : ret, !0), + getDataViewMemory0().setBigInt64(arg0 + 8, isLikeNone(ret) ? BigInt(0) : ret, !0), getDataViewMemory0().setInt32(arg0 + 0, !isLikeNone(ret), !0); }, __wbg___wbindgen_boolean_get_bbbb1c18aa2f5e25: function(arg0) { @@ -264,7 +264,7 @@ function getArrayU8FromWasm0(ptr, len) { let cachedDataViewMemory0 = null; function getDataViewMemory0() { - return (null === cachedDataViewMemory0 || !0 === cachedDataViewMemory0.buffer.detached || void 0 === cachedDataViewMemory0.buffer.detached && cachedDataViewMemory0.buffer !== wasm.memory.buffer) && (cachedDataViewMemory0 = new DataView(wasm.memory.buffer)), + return (null === cachedDataViewMemory0 || !0 === cachedDataViewMemory0.buffer.detached || void 0 === cachedDataViewMemory0.buffer.detached && cachedDataViewMemory0.buffer !== wasm.memory.buffer) && (cachedDataViewMemory0 = new DataView(wasm.memory.buffer)), cachedDataViewMemory0; } @@ -275,7 +275,7 @@ function getStringFromWasm0(ptr, len) { let cachedUint8ArrayMemory0 = null; function getUint8ArrayMemory0() { - return null !== cachedUint8ArrayMemory0 && 0 !== cachedUint8ArrayMemory0.byteLength || (cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer)), + return null !== cachedUint8ArrayMemory0 && 0 !== cachedUint8ArrayMemory0.byteLength || (cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer)), cachedUint8ArrayMemory0; } @@ -301,7 +301,7 @@ function isLikeNone(x) { function passStringToWasm0(arg, malloc, realloc) { if (void 0 === realloc) { const buf = cachedTextEncoder.encode(arg), ptr = malloc(buf.length, 1) >>> 0; - return getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf), WASM_VECTOR_LEN = buf.length, + return getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf), WASM_VECTOR_LEN = buf.length, ptr; } @@ -366,7 +366,7 @@ const cachedTextEncoder = new TextEncoder(); let wasmModule, wasm, WASM_VECTOR_LEN = 0; function __wbg_finalize_init(instance, module) { - return wasm = instance.exports, wasmModule = module, cachedDataViewMemory0 = null, + return wasm = instance.exports, wasmModule = module, cachedDataViewMemory0 = null, cachedUint8ArrayMemory0 = null, wasm; } @@ -668,7 +668,7 @@ function initSync(module) { async function __wbg_init(module_or_path) { if (void 0 !== wasm) return wasm; - void 0 !== module_or_path && Object.getPrototypeOf(module_or_path) === Object.prototype && ({module_or_path: module_or_path} = module_or_path), + void 0 !== module_or_path && Object.getPrototypeOf(module_or_path) === Object.prototype && ({module_or_path: module_or_path} = module_or_path), void 0 === module_or_path && (module_or_path = new URL("sentience_core_bg.wasm", import.meta.url)); const imports = __wbg_get_imports(); if (!(module instanceof WebAssembly.Module)) { From caa51313ca3088cdee58bf96927bc77dcb716c4e Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Thu, 22 Jan 2026 08:42:58 -0800 Subject: [PATCH 05/11] fix test --- sentience/query.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sentience/query.py b/sentience/query.py index 44db2f6..42f169f 100644 --- a/sentience/query.py +++ b/sentience/query.py @@ -159,8 +159,12 @@ def match_element(element: Element, query: dict[str, Any]) -> bool: # noqa: C90 # Role exact match if "role" in query: - if element.role != query["role"]: - return False + if query["role"] == "link": + if element.role != "link" and not element.href: + return False + else: + if element.role != query["role"]: + return False # Role exclusion if "role_exclude" in query: From 796dbfdf9770aa39209a38557d8642feebc6ea2b Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Thu, 22 Jan 2026 09:34:58 -0800 Subject: [PATCH 06/11] fix build error --- sentience/extension/pkg/sentience_core.js | 256 ++++-------------- .../extension/pkg/sentience_core_bg.wasm | Bin 111775 -> 111775 bytes 2 files changed, 52 insertions(+), 204 deletions(-) diff --git a/sentience/extension/pkg/sentience_core.js b/sentience/extension/pkg/sentience_core.js index 5684fd6..b232d13 100644 --- a/sentience/extension/pkg/sentience_core.js +++ b/sentience/extension/pkg/sentience_core.js @@ -1,181 +1,4 @@ -export function analyze_page(val) { - return takeObject(wasm.analyze_page(addHeapObject(val))); -} - -export function analyze_page_with_options(val, options) { - return takeObject(wasm.analyze_page_with_options(addHeapObject(val), addHeapObject(options))); -} - -export function decide_and_act(_raw_elements) { - wasm.decide_and_act(addHeapObject(_raw_elements)); -} - -export function prune_for_api(val) { - return takeObject(wasm.prune_for_api(addHeapObject(val))); -} - -function __wbg_get_imports() { - const import0 = { - __proto__: null, - __wbg_Error_8c4e43fe74559d73: function(arg0, arg1) { - return addHeapObject(Error(getStringFromWasm0(arg0, arg1))); - }, - __wbg_Number_04624de7d0e8332d: function(arg0) { - return Number(getObject(arg0)); - }, - __wbg___wbindgen_bigint_get_as_i64_8fcf4ce7f1ca72a2: function(arg0, arg1) { - const v = getObject(arg1), ret = "bigint" == typeof v ? v : void 0; - getDataViewMemory0().setBigInt64(arg0 + 8, isLikeNone(ret) ? BigInt(0) : ret, !0), - getDataViewMemory0().setInt32(arg0 + 0, !isLikeNone(ret), !0); - }, - __wbg___wbindgen_boolean_get_bbbb1c18aa2f5e25: function(arg0) { - const v = getObject(arg0), ret = "boolean" == typeof v ? v : void 0; - return isLikeNone(ret) ? 16777215 : ret ? 1 : 0; - }, - __wbg___wbindgen_debug_string_0bc8482c6e3508ae: function(arg0, arg1) { - const ptr1 = passStringToWasm0(debugString(getObject(arg1)), wasm.__wbindgen_export, wasm.__wbindgen_export2), len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4, len1, !0), getDataViewMemory0().setInt32(arg0 + 0, ptr1, !0); - }, - __wbg___wbindgen_in_47fa6863be6f2f25: function(arg0, arg1) { - return getObject(arg0) in getObject(arg1); - }, - __wbg___wbindgen_is_bigint_31b12575b56f32fc: function(arg0) { - return "bigint" == typeof getObject(arg0); - }, - __wbg___wbindgen_is_function_0095a73b8b156f76: function(arg0) { - return "function" == typeof getObject(arg0); - }, - __wbg___wbindgen_is_object_5ae8e5880f2c1fbd: function(arg0) { - const val = getObject(arg0); - return "object" == typeof val && null !== val; - }, - __wbg___wbindgen_is_undefined_9e4d92534c42d778: function(arg0) { - return void 0 === getObject(arg0); - }, - __wbg___wbindgen_jsval_eq_11888390b0186270: function(arg0, arg1) { - return getObject(arg0) === getObject(arg1); - }, - __wbg___wbindgen_jsval_loose_eq_9dd77d8cd6671811: function(arg0, arg1) { - return getObject(arg0) == getObject(arg1); - }, - __wbg___wbindgen_number_get_8ff4255516ccad3e: function(arg0, arg1) { - const obj = getObject(arg1), ret = "number" == typeof obj ? obj : void 0; - getDataViewMemory0().setFloat64(arg0 + 8, isLikeNone(ret) ? 0 : ret, !0), getDataViewMemory0().setInt32(arg0 + 0, !isLikeNone(ret), !0); - }, - __wbg___wbindgen_string_get_72fb696202c56729: function(arg0, arg1) { - const obj = getObject(arg1), ret = "string" == typeof obj ? obj : void 0; - var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2), len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4, len1, !0), getDataViewMemory0().setInt32(arg0 + 0, ptr1, !0); - }, - __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); - }, - __wbg_call_389efe28435a9388: function() { - return handleError(function(arg0, arg1) { - return addHeapObject(getObject(arg0).call(getObject(arg1))); - }, arguments); - }, - __wbg_done_57b39ecd9addfe81: function(arg0) { - return getObject(arg0).done; - }, - __wbg_error_9a7fe3f932034cde: function(arg0) {}, - __wbg_get_9b94d73e6221f75c: function(arg0, arg1) { - return addHeapObject(getObject(arg0)[arg1 >>> 0]); - }, - __wbg_get_b3ed3ad4be2bc8ac: function() { - return handleError(function(arg0, arg1) { - return addHeapObject(Reflect.get(getObject(arg0), getObject(arg1))); - }, arguments); - }, - __wbg_get_with_ref_key_1dc361bd10053bfe: function(arg0, arg1) { - return addHeapObject(getObject(arg0)[getObject(arg1)]); - }, - __wbg_instanceof_ArrayBuffer_c367199e2fa2aa04: function(arg0) { - let result; - try { - result = getObject(arg0) instanceof ArrayBuffer; - } catch (_) { - result = !1; - } - return result; - }, - __wbg_instanceof_Uint8Array_9b9075935c74707c: function(arg0) { - let result; - try { - result = getObject(arg0) instanceof Uint8Array; - } catch (_) { - result = !1; - } - return result; - }, - __wbg_isArray_d314bb98fcf08331: function(arg0) { - return Array.isArray(getObject(arg0)); - }, - __wbg_isSafeInteger_bfbc7332a9768d2a: function(arg0) { - return Number.isSafeInteger(getObject(arg0)); - }, - __wbg_iterator_6ff6560ca1568e55: function() { - return addHeapObject(Symbol.iterator); - }, - __wbg_js_click_element_2fe1e774f3d232c7: function(arg0) { - js_click_element(arg0); - }, - __wbg_length_32ed9a279acd054c: function(arg0) { - return getObject(arg0).length; - }, - __wbg_length_35a7bace40f36eac: function(arg0) { - return getObject(arg0).length; - }, - __wbg_new_361308b2356cecd0: function() { - return addHeapObject(new Object); - }, - __wbg_new_3eb36ae241fe6f44: function() { - return addHeapObject(new Array); - }, - __wbg_new_dd2b680c8bf6ae29: function(arg0) { - return addHeapObject(new Uint8Array(getObject(arg0))); - }, - __wbg_next_3482f54c49e8af19: function() { - return handleError(function(arg0) { - return addHeapObject(getObject(arg0).next()); - }, arguments); - }, - __wbg_next_418f80d8f5303233: function(arg0) { - return addHeapObject(getObject(arg0).next); - }, - __wbg_prototypesetcall_bdcdcc5842e4d77d: function(arg0, arg1, arg2) { - Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), getObject(arg2)); - }, - __wbg_set_3f1d0b984ed272ed: function(arg0, arg1, arg2) { - getObject(arg0)[takeObject(arg1)] = takeObject(arg2); - }, - __wbg_set_f43e577aea94465b: function(arg0, arg1, arg2) { - getObject(arg0)[arg1 >>> 0] = takeObject(arg2); - }, - __wbg_value_0546255b415e96c1: function(arg0) { - return addHeapObject(getObject(arg0).value); - }, - __wbindgen_cast_0000000000000001: function(arg0) { - return addHeapObject(arg0); - }, - __wbindgen_cast_0000000000000002: function(arg0, arg1) { - return addHeapObject(getStringFromWasm0(arg0, arg1)); - }, - __wbindgen_cast_0000000000000003: function(arg0) { - return addHeapObject(BigInt.asUintN(64, arg0)); - }, - __wbindgen_object_clone_ref: function(arg0) { - return addHeapObject(getObject(arg0)); - }, - __wbindgen_object_drop_ref: function(arg0) { - takeObject(arg0); - } - }; - return { - __proto__: null, - "./sentience_core_bg.js": import0 - }; -} +let wasm; function addHeapObject(obj) { if (heap_next === heap.length) heap.push(heap.length + 1); @@ -264,8 +87,10 @@ function getArrayU8FromWasm0(ptr, len) { let cachedDataViewMemory0 = null; function getDataViewMemory0() { - return (null === cachedDataViewMemory0 || !0 === cachedDataViewMemory0.buffer.detached || void 0 === cachedDataViewMemory0.buffer.detached && cachedDataViewMemory0.buffer !== wasm.memory.buffer) && (cachedDataViewMemory0 = new DataView(wasm.memory.buffer)), - cachedDataViewMemory0; + if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { + cachedDataViewMemory0 = new DataView(wasm.memory.buffer); + } + return cachedDataViewMemory0; } function getStringFromWasm0(ptr, len) { @@ -275,8 +100,10 @@ function getStringFromWasm0(ptr, len) { let cachedUint8ArrayMemory0 = null; function getUint8ArrayMemory0() { - return null !== cachedUint8ArrayMemory0 && 0 !== cachedUint8ArrayMemory0.byteLength || (cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer)), - cachedUint8ArrayMemory0; + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; } function getObject(idx) { return heap[idx]; } @@ -299,10 +126,12 @@ function isLikeNone(x) { } function passStringToWasm0(arg, malloc, realloc) { - if (void 0 === realloc) { - const buf = cachedTextEncoder.encode(arg), ptr = malloc(buf.length, 1) >>> 0; - return getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf), WASM_VECTOR_LEN = buf.length, - ptr; + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; } let len = arg.length; @@ -355,19 +184,15 @@ function decodeText(ptr, len) { const cachedTextEncoder = new TextEncoder(); -"encodeInto" in cachedTextEncoder || (cachedTextEncoder.encodeInto = function(arg, view) { - const buf = cachedTextEncoder.encode(arg); - return view.set(buf), { - read: arg.length, - written: buf.length - }; -}); - -let wasmModule, wasm, WASM_VECTOR_LEN = 0; - -function __wbg_finalize_init(instance, module) { - return wasm = instance.exports, wasmModule = module, cachedDataViewMemory0 = null, - cachedUint8ArrayMemory0 = null, wasm; +if (!('encodeInto' in cachedTextEncoder)) { + cachedTextEncoder.encodeInto = function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; + } } let WASM_VECTOR_LEN = 0; @@ -666,10 +491,6 @@ function initSync(module) { } } -async function __wbg_init(module_or_path) { - if (void 0 !== wasm) return wasm; - void 0 !== module_or_path && Object.getPrototypeOf(module_or_path) === Object.prototype && ({module_or_path: module_or_path} = module_or_path), - void 0 === module_or_path && (module_or_path = new URL("sentience_core_bg.wasm", import.meta.url)); const imports = __wbg_get_imports(); if (!(module instanceof WebAssembly.Module)) { module = new WebAssembly.Module(module); @@ -678,4 +499,31 @@ async function __wbg_init(module_or_path) { return __wbg_finalize_init(instance, module); } -export { initSync, __wbg_init as default }; +async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined') { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (typeof module_or_path === 'undefined') { + module_or_path = new URL('sentience_core_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); +} + +export { initSync }; +export default __wbg_init; diff --git a/sentience/extension/pkg/sentience_core_bg.wasm b/sentience/extension/pkg/sentience_core_bg.wasm index 3eb0109fd1237108d9eac346546b514e4dee6664..9d0bc9a750044eb8c43b90f2c64d0f3b21bef5e0 100644 GIT binary patch delta 96 zcmV-m0H6P#=?0(a2C$U}lj8>ylYs{mle`Bfla&V(v-St@0vjld1N#E-0`UU!0`mg& z0`&s+0`~&!0qg=PDuV$Fw*df&nVGu!RA2E&)cDCbR)70ace$v;j>s Cm?HlG delta 96 zcmV-m0H6P#=?0(a2C$U}lj8>ylYs{mle`Bfla&V(v-St@0vjrf1N#E-0`UU!0`mg& z0`&s+0`~&!0qg=PD1!kDw*df&nPEu!RA2E&)}SCbR)70Y;Znv;j>t CLn8kG From cbdbe5da7a3e996c4449130a84b017dda398b4e9 Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Thu, 22 Jan 2026 11:46:31 -0800 Subject: [PATCH 07/11] Sync extension bundle and quiet tests Add a reproducible extension sync script, track dist assets, and clean up test warnings while keeping download tracking resilient to async mocks. --- .gitignore | 4 + scripts/sync_extension.sh | 29 + sentience/backends/playwright_backend.py | 10 +- sentience/extension/dist/background.js | 242 ++ sentience/extension/dist/content.js | 456 +++ sentience/extension/dist/injected_api.js | 2749 +++++++++++++++++ .../extension/pkg/sentience_core_bg.wasm | Bin 111775 -> 111775 bytes sentience/models.py | 5 +- tests/test_agent.py | 6 +- tests/test_stealth.py | 2 +- 10 files changed, 3495 insertions(+), 8 deletions(-) create mode 100644 scripts/sync_extension.sh create mode 100644 sentience/extension/dist/background.js create mode 100644 sentience/extension/dist/content.js create mode 100644 sentience/extension/dist/injected_api.js diff --git a/.gitignore b/.gitignore index bf73102..fde0371 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,7 @@ Thumbs.db # Temporary directories from sync workflows extension-temp/ playground/ + +# Allow bundled extension assets under sentience/extension/dist +!sentience/extension/dist/ +!sentience/extension/dist/** diff --git a/scripts/sync_extension.sh b/scripts/sync_extension.sh new file mode 100644 index 0000000..5da29ab --- /dev/null +++ b/scripts/sync_extension.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" +CHROME_DIR="${REPO_ROOT}/sentience-chrome" +SDK_EXT_DIR="${REPO_ROOT}/sdk-python/sentience/extension" + +if [[ ! -d "${CHROME_DIR}" ]]; then + echo "[sync_extension] sentience-chrome not found at ${CHROME_DIR}" + exit 1 +fi + +if [[ ! -f "${CHROME_DIR}/package.json" ]]; then + echo "[sync_extension] package.json missing in sentience-chrome" + exit 1 +fi + +echo "[sync_extension] Building sentience-chrome..." +pushd "${CHROME_DIR}" >/dev/null +npm run build +popd >/dev/null + +echo "[sync_extension] Syncing dist/ and pkg/ to sdk-python..." +mkdir -p "${SDK_EXT_DIR}/dist" "${SDK_EXT_DIR}/pkg" +cp "${CHROME_DIR}/dist/"* "${SDK_EXT_DIR}/dist/" +cp "${CHROME_DIR}/pkg/"* "${SDK_EXT_DIR}/pkg/" + +echo "[sync_extension] Done." diff --git a/sentience/backends/playwright_backend.py b/sentience/backends/playwright_backend.py index b3538e9..cfbc808 100644 --- a/sentience/backends/playwright_backend.py +++ b/sentience/backends/playwright_backend.py @@ -22,6 +22,7 @@ """ import asyncio +import inspect import mimetypes import os import time @@ -56,7 +57,14 @@ def __init__(self, page: "AsyncPage") -> None: # Best-effort download tracking (does not change behavior unless a download occurs). # pylint: disable=broad-exception-caught try: - self._page.on("download", lambda d: asyncio.create_task(self._track_download(d))) + result = self._page.on( + "download", lambda d: asyncio.create_task(self._track_download(d)) + ) + if inspect.isawaitable(result): + try: + asyncio.get_running_loop().create_task(result) + except RuntimeError: + pass except Exception: pass diff --git a/sentience/extension/dist/background.js b/sentience/extension/dist/background.js new file mode 100644 index 0000000..1f64f84 --- /dev/null +++ b/sentience/extension/dist/background.js @@ -0,0 +1,242 @@ +// Sentience Chrome Extension - Background Service Worker +// Auto-generated from modular source +import init, { analyze_page_with_options, analyze_page, prune_for_api } from '../pkg/sentience_core.js'; + +// background.js - Service Worker with WASM (CSP-Immune!) +// This runs in an isolated environment, completely immune to page CSP policies + + +console.log('[Sentience Background] Initializing...'); + +// Global WASM initialization state +let wasmReady = false; +let wasmInitPromise = null; + +/** + * Initialize WASM module - called once on service worker startup + * Uses static imports (not dynamic import()) which is required for Service Workers + */ +async function initWASM() { + if (wasmReady) return; + if (wasmInitPromise) return wasmInitPromise; + + wasmInitPromise = (async () => { + try { + console.log('[Sentience Background] Loading WASM module...'); + + // Define the js_click_element function that WASM expects + // In Service Workers, use 'globalThis' instead of 'window' + // In background context, we can't actually click, so we log a warning + globalThis.js_click_element = () => { + console.warn('[Sentience Background] js_click_element called in background (ignored)'); + }; + + // Initialize WASM - this calls the init() function from the static import + // The init() function handles fetching and instantiating the .wasm file + await init(); + + wasmReady = true; + console.log('[Sentience Background] ✓ WASM ready!'); + console.log( + '[Sentience Background] Available functions: analyze_page, analyze_page_with_options, prune_for_api' + ); + } catch (error) { + console.error('[Sentience Background] WASM initialization failed:', error); + throw error; + } + })(); + + return wasmInitPromise; +} + +// Initialize WASM on service worker startup +initWASM().catch((err) => { + console.error('[Sentience Background] Failed to initialize WASM:', err); +}); + +/** + * Message handler for all extension communication + * Includes global error handling to prevent extension crashes + */ +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + // Global error handler to prevent extension crashes + try { + // Handle screenshot requests (existing functionality) + if (request.action === 'captureScreenshot') { + handleScreenshotCapture(sender.tab.id, request.options) + .then((screenshot) => { + sendResponse({ success: true, screenshot }); + }) + .catch((error) => { + console.error('[Sentience Background] Screenshot capture failed:', error); + sendResponse({ + success: false, + error: error.message || 'Screenshot capture failed', + }); + }); + return true; // Async response + } + + // Handle WASM processing requests (NEW!) + if (request.action === 'processSnapshot') { + handleSnapshotProcessing(request.rawData, request.options) + .then((result) => { + sendResponse({ success: true, result }); + }) + .catch((error) => { + console.error('[Sentience Background] Snapshot processing failed:', error); + sendResponse({ + success: false, + error: error.message || 'Snapshot processing failed', + }); + }); + return true; // Async response + } + + // Unknown action + console.warn('[Sentience Background] Unknown action:', request.action); + sendResponse({ success: false, error: 'Unknown action' }); + return false; + } catch (error) { + // Catch any synchronous errors that might crash the extension + console.error('[Sentience Background] Fatal error in message handler:', error); + try { + sendResponse({ + success: false, + error: `Fatal error: ${error.message || 'Unknown error'}`, + }); + } catch (e) { + // If sendResponse already called, ignore + } + return false; + } +}); + +/** + * Handle screenshot capture (existing functionality) + */ +async function handleScreenshotCapture(_tabId, options = {}) { + try { + const { format = 'png', quality = 90 } = options; + + const dataUrl = await chrome.tabs.captureVisibleTab(null, { + format, + quality, + }); + + console.log( + `[Sentience Background] Screenshot captured: ${format}, size: ${dataUrl.length} bytes` + ); + return dataUrl; + } catch (error) { + console.error('[Sentience Background] Screenshot error:', error); + throw new Error(`Failed to capture screenshot: ${error.message}`); + } +} + +/** + * Handle snapshot processing with WASM (NEW!) + * This is where the magic happens - completely CSP-immune! + * Includes safeguards to prevent crashes and hangs. + * + * @param {Array} rawData - Raw element data from injected_api.js + * @param {Object} options - Snapshot options (limit, filter, etc.) + * @returns {Promise} Processed snapshot result + */ +async function handleSnapshotProcessing(rawData, options = {}) { + const MAX_ELEMENTS = 10000; // Safety limit to prevent hangs + const startTime = performance.now(); + + try { + // Safety check: limit element count to prevent hangs + if (!Array.isArray(rawData)) { + throw new Error('rawData must be an array'); + } + + if (rawData.length > MAX_ELEMENTS) { + console.warn( + `[Sentience Background] ⚠️ Large dataset: ${rawData.length} elements. Limiting to ${MAX_ELEMENTS} to prevent hangs.` + ); + rawData = rawData.slice(0, MAX_ELEMENTS); + } + + // Ensure WASM is initialized + await initWASM(); + if (!wasmReady) { + throw new Error('WASM module not initialized'); + } + + console.log( + `[Sentience Background] Processing ${rawData.length} elements with options:`, + options + ); + + // Run WASM processing using the imported functions directly + // Wrap in try-catch with timeout protection + let analyzedElements; + try { + // Use a timeout wrapper to prevent infinite hangs + const wasmPromise = new Promise((resolve, reject) => { + try { + let result; + if (options.limit || options.filter) { + result = analyze_page_with_options(rawData, options); + } else { + result = analyze_page(rawData); + } + resolve(result); + } catch (e) { + reject(e); + } + }); + + // Add timeout protection (18 seconds - less than content.js timeout) + analyzedElements = await Promise.race([ + wasmPromise, + new Promise((_, reject) => + setTimeout(() => reject(new Error('WASM processing timeout (>18s)')), 18000) + ), + ]); + } catch (e) { + const errorMsg = e.message || 'Unknown WASM error'; + console.error(`[Sentience Background] WASM analyze_page failed: ${errorMsg}`, e); + throw new Error(`WASM analyze_page failed: ${errorMsg}`); + } + + // Prune elements for API (prevents 413 errors on large sites) + let prunedRawData; + try { + prunedRawData = prune_for_api(rawData); + } catch (e) { + console.warn('[Sentience Background] prune_for_api failed, using original data:', e); + prunedRawData = rawData; + } + + const duration = performance.now() - startTime; + console.log( + `[Sentience Background] ✓ Processed: ${analyzedElements.length} analyzed, ${prunedRawData.length} pruned (${duration.toFixed(1)}ms)` + ); + + return { + elements: analyzedElements, + raw_elements: prunedRawData, + }; + } catch (error) { + const duration = performance.now() - startTime; + console.error(`[Sentience Background] Processing error after ${duration.toFixed(1)}ms:`, error); + throw error; + } +} + +console.log('[Sentience Background] Service worker ready'); + +// Global error handlers to prevent extension crashes +self.addEventListener('error', (event) => { + console.error('[Sentience Background] Global error caught:', event.error); + event.preventDefault(); // Prevent extension crash +}); + +self.addEventListener('unhandledrejection', (event) => { + console.error('[Sentience Background] Unhandled promise rejection:', event.reason); + event.preventDefault(); // Prevent extension crash +}); diff --git a/sentience/extension/dist/content.js b/sentience/extension/dist/content.js new file mode 100644 index 0000000..ee6efa2 --- /dev/null +++ b/sentience/extension/dist/content.js @@ -0,0 +1,456 @@ +// Sentience Chrome Extension - Content Script +// Auto-generated from modular source +(function () { + 'use strict'; + + // content.js - ISOLATED WORLD (Bridge between Main World and Background) + console.log('[Sentience Bridge] Loaded.'); + + // Detect if we're in a child frame (for iframe support) + const isChildFrame = window !== window.top; + if (isChildFrame) { + console.log('[Sentience Bridge] Running in child frame:', window.location.href); + } + + // 1. Pass Extension ID to Main World (So API knows where to find resources) + document.documentElement.dataset.sentienceExtensionId = chrome.runtime.id; + + // 2. Message Router - Handles all communication between page and background + window.addEventListener('message', (event) => { + // Security check: only accept messages from same window + if (event.source !== window) return; + + // Route different message types + switch (event.data.type) { + case 'SENTIENCE_SCREENSHOT_REQUEST': + handleScreenshotRequest(event.data); + break; + + case 'SENTIENCE_SNAPSHOT_REQUEST': + handleSnapshotRequest(event.data); + break; + + case 'SENTIENCE_SHOW_OVERLAY': + handleShowOverlay(event.data); + break; + + case 'SENTIENCE_CLEAR_OVERLAY': + handleClearOverlay(); + break; + + case 'SENTIENCE_SHOW_GRID_OVERLAY': + handleShowGridOverlay(event.data); + break; + } + }); + + /** + * Handle screenshot requests (existing functionality) + */ + function handleScreenshotRequest(data) { + chrome.runtime.sendMessage({ action: 'captureScreenshot', options: data.options }, (response) => { + window.postMessage( + { + type: 'SENTIENCE_SCREENSHOT_RESULT', + requestId: data.requestId, + screenshot: response?.success ? response.screenshot : null, + error: response?.error, + }, + '*' + ); + }); + } + + /** + * Handle snapshot processing requests (NEW!) + * Sends raw DOM data to background worker for WASM processing + * Includes timeout protection to prevent extension crashes + */ + function handleSnapshotRequest(data) { + const startTime = performance.now(); + const TIMEOUT_MS = 20000; // 20 seconds (longer than injected_api timeout) + let responded = false; + + // Timeout protection: if background doesn't respond, send error + const timeoutId = setTimeout(() => { + if (!responded) { + responded = true; + const duration = performance.now() - startTime; + console.error(`[Sentience Bridge] ⚠️ WASM processing timeout after ${duration.toFixed(1)}ms`); + window.postMessage( + { + type: 'SENTIENCE_SNAPSHOT_RESULT', + requestId: data.requestId, + error: 'WASM processing timeout - background script may be unresponsive', + duration, + }, + '*' + ); + } + }, TIMEOUT_MS); + + try { + chrome.runtime.sendMessage( + { + action: 'processSnapshot', + rawData: data.rawData, + options: data.options, + }, + (response) => { + if (responded) return; // Already responded via timeout + responded = true; + clearTimeout(timeoutId); + + const duration = performance.now() - startTime; + + // Handle Chrome extension errors (e.g., background script crashed) + if (chrome.runtime.lastError) { + console.error( + '[Sentience Bridge] Chrome runtime error:', + chrome.runtime.lastError.message + ); + window.postMessage( + { + type: 'SENTIENCE_SNAPSHOT_RESULT', + requestId: data.requestId, + error: `Chrome runtime error: ${chrome.runtime.lastError.message}`, + duration, + }, + '*' + ); + return; + } + + if (response?.success) { + console.log(`[Sentience Bridge] ✓ WASM processing complete in ${duration.toFixed(1)}ms`); + window.postMessage( + { + type: 'SENTIENCE_SNAPSHOT_RESULT', + requestId: data.requestId, + elements: response.result.elements, + raw_elements: response.result.raw_elements, + duration, + }, + '*' + ); + } else { + console.error('[Sentience Bridge] WASM processing failed:', response?.error); + window.postMessage( + { + type: 'SENTIENCE_SNAPSHOT_RESULT', + requestId: data.requestId, + error: response?.error || 'Processing failed', + duration, + }, + '*' + ); + } + } + ); + } catch (error) { + if (!responded) { + responded = true; + clearTimeout(timeoutId); + const duration = performance.now() - startTime; + console.error('[Sentience Bridge] Exception sending message:', error); + window.postMessage( + { + type: 'SENTIENCE_SNAPSHOT_RESULT', + requestId: data.requestId, + error: `Failed to send message: ${error.message}`, + duration, + }, + '*' + ); + } + } + } + + // ============================================================================ + // Visual Overlay - Shadow DOM Implementation + // ============================================================================ + + const OVERLAY_HOST_ID = 'sentience-overlay-host'; + let overlayTimeout = null; + + /** + * Show visual overlay highlighting elements using Shadow DOM + * @param {Object} data - Message data with elements and targetElementId + */ + function handleShowOverlay(data) { + const { elements, targetElementId } = data; + + if (!elements || !Array.isArray(elements)) { + console.warn('[Sentience Bridge] showOverlay: elements must be an array'); + return; + } + + removeOverlay(); + + // Create host with Shadow DOM for CSS isolation + const host = document.createElement('div'); + host.id = OVERLAY_HOST_ID; + host.style.cssText = ` + position: fixed !important; + top: 0 !important; + left: 0 !important; + width: 100vw !important; + height: 100vh !important; + pointer-events: none !important; + z-index: 2147483647 !important; + margin: 0 !important; + padding: 0 !important; + `; + document.body.appendChild(host); + + // Attach shadow root (closed mode for security and CSS isolation) + const shadow = host.attachShadow({ mode: 'closed' }); + + // Calculate max importance for scaling + const maxImportance = Math.max(...elements.map((e) => e.importance || 0), 1); + + elements.forEach((element) => { + const bbox = element.bbox; + if (!bbox) return; + + const isTarget = element.id === targetElementId; + const isPrimary = element.visual_cues?.is_primary || false; + const importance = element.importance || 0; + + // Color: Red (target), Blue (primary), Green (regular) + let color; + if (isTarget) color = '#FF0000'; + else if (isPrimary) color = '#0066FF'; + else color = '#00FF00'; + + // Scale opacity and border width based on importance + const importanceRatio = maxImportance > 0 ? importance / maxImportance : 0.5; + const borderOpacity = isTarget + ? 1.0 + : isPrimary + ? 0.9 + : Math.max(0.4, 0.5 + importanceRatio * 0.5); + const fillOpacity = borderOpacity * 0.2; + const borderWidth = isTarget + ? 2 + : isPrimary + ? 1.5 + : Math.max(0.5, Math.round(importanceRatio * 2)); + + // Convert fill opacity to hex for background-color + const hexOpacity = Math.round(fillOpacity * 255) + .toString(16) + .padStart(2, '0'); + + // Create box with semi-transparent fill + const box = document.createElement('div'); + box.style.cssText = ` + position: absolute; + left: ${bbox.x}px; + top: ${bbox.y}px; + width: ${bbox.width}px; + height: ${bbox.height}px; + border: ${borderWidth}px solid ${color}; + background-color: ${color}${hexOpacity}; + box-sizing: border-box; + opacity: ${borderOpacity}; + pointer-events: none; + `; + + // Add badge showing importance score + if (importance > 0 || isPrimary) { + const badge = document.createElement('span'); + badge.textContent = isPrimary ? `⭐${importance}` : `${importance}`; + badge.style.cssText = ` + position: absolute; + top: -18px; + left: 0; + background: ${color}; + color: white; + font-size: 11px; + font-weight: bold; + padding: 2px 6px; + font-family: Arial, sans-serif; + border-radius: 3px; + opacity: 0.95; + white-space: nowrap; + pointer-events: none; + `; + box.appendChild(badge); + } + + // Add target emoji for target element + if (isTarget) { + const targetIndicator = document.createElement('span'); + targetIndicator.textContent = '🎯'; + targetIndicator.style.cssText = ` + position: absolute; + top: -18px; + right: 0; + font-size: 16px; + pointer-events: none; + `; + box.appendChild(targetIndicator); + } + + shadow.appendChild(box); + }); + + console.log(`[Sentience Bridge] Overlay shown for ${elements.length} elements`); + + // Auto-remove after 5 seconds + overlayTimeout = setTimeout(() => { + removeOverlay(); + console.log('[Sentience Bridge] Overlay auto-cleared after 5 seconds'); + }, 5000); + } + + /** + * Show grid overlay highlighting detected grids + * @param {Object} data - Message data with grids and targetGridId + */ + function handleShowGridOverlay(data) { + const { grids, targetGridId } = data; + + if (!grids || !Array.isArray(grids)) { + console.warn('[Sentience Bridge] showGridOverlay: grids must be an array'); + return; + } + + removeOverlay(); + + // Create host with Shadow DOM for CSS isolation + const host = document.createElement('div'); + host.id = OVERLAY_HOST_ID; + host.style.cssText = ` + position: fixed !important; + top: 0 !important; + left: 0 !important; + width: 100vw !important; + height: 100vh !important; + pointer-events: none !important; + z-index: 2147483647 !important; + margin: 0 !important; + padding: 0 !important; + `; + document.body.appendChild(host); + + // Attach shadow root (closed mode for security and CSS isolation) + const shadow = host.attachShadow({ mode: 'closed' }); + + grids.forEach((grid) => { + const bbox = grid.bbox; + if (!bbox) return; + + const isTarget = grid.grid_id === targetGridId; + const isDominant = grid.is_dominant === true; + + // Grid colors: Red for target, Orange for dominant, Purple for regular + let color = '#9B59B6'; // Purple (default) + if (isTarget) { + color = '#FF0000'; // Red for target + } else if (isDominant) { + color = '#FF8C00'; // Orange for dominant group + } + const borderStyle = isTarget ? 'solid' : 'dashed'; // Dashed for grids + const borderWidth = isTarget ? 3 : isDominant ? 2.5 : 2; + const opacity = isTarget ? 1.0 : isDominant ? 0.9 : 0.8; + const fillOpacity = opacity * 0.1; // 10% opacity for grids (lighter than elements) + + // Convert fill opacity to hex for background-color + const hexOpacity = Math.round(fillOpacity * 255) + .toString(16) + .padStart(2, '0'); + + // Create grid box + const box = document.createElement('div'); + box.style.cssText = ` + position: absolute; + left: ${bbox.x}px; + top: ${bbox.y}px; + width: ${bbox.width}px; + height: ${bbox.height}px; + border: ${borderWidth}px ${borderStyle} ${color}; + background-color: ${color}${hexOpacity}; + box-sizing: border-box; + opacity: ${opacity}; + pointer-events: none; + `; + + // Add badge with grid_id and label + let labelText = grid.label ? `Grid ${grid.grid_id}: ${grid.label}` : `Grid ${grid.grid_id}`; + // Add dominant indicator if this is the dominant group + if (grid.is_dominant) { + labelText = `⭐ ${labelText} (dominant)`; + } + const badge = document.createElement('span'); + badge.textContent = labelText; + badge.style.cssText = ` + position: absolute; + top: -18px; + left: 0; + background: ${color}; + color: white; + font-size: 11px; + font-weight: bold; + padding: 2px 6px; + font-family: Arial, sans-serif; + border-radius: 3px; + opacity: 0.95; + white-space: nowrap; + pointer-events: none; + `; + box.appendChild(badge); + + // Add target indicator if target + if (isTarget) { + const targetIndicator = document.createElement('span'); + targetIndicator.textContent = '🎯'; + targetIndicator.style.cssText = ` + position: absolute; + top: -18px; + right: 0; + font-size: 16px; + pointer-events: none; + `; + box.appendChild(targetIndicator); + } + + shadow.appendChild(box); + }); + + console.log(`[Sentience Bridge] Grid overlay shown for ${grids.length} grids`); + + // Auto-remove after 5 seconds + overlayTimeout = setTimeout(() => { + removeOverlay(); + console.log('[Sentience Bridge] Grid overlay auto-cleared after 5 seconds'); + }, 5000); + } + + /** + * Clear overlay manually + */ + function handleClearOverlay() { + removeOverlay(); + console.log('[Sentience Bridge] Overlay cleared manually'); + } + + /** + * Remove overlay from DOM + */ + function removeOverlay() { + const existing = document.getElementById(OVERLAY_HOST_ID); + if (existing) { + existing.remove(); + } + + if (overlayTimeout) { + clearTimeout(overlayTimeout); + overlayTimeout = null; + } + } + + // console.log('[Sentience Bridge] Ready - Extension ID:', chrome.runtime.id); + +})(); diff --git a/sentience/extension/dist/injected_api.js b/sentience/extension/dist/injected_api.js new file mode 100644 index 0000000..e6ff2f9 --- /dev/null +++ b/sentience/extension/dist/injected_api.js @@ -0,0 +1,2749 @@ +// Sentience Chrome Extension - Injected API +// Auto-generated from modular source +(function () { + 'use strict'; + + // utils.js - Helper Functions (CSP-Resistant) + // All utility functions needed for DOM data collection + + // --- HELPER: Deep Walker with Native Filter --- + function getAllElements(root = document) { + const elements = []; + const filter = { + acceptNode(node) { + // Skip metadata and script/style tags + if (['SCRIPT', 'STYLE', 'NOSCRIPT', 'META', 'LINK', 'HEAD'].includes(node.tagName)) { + return NodeFilter.FILTER_REJECT; + } + // Skip deep SVG children + if (node.parentNode && node.parentNode.tagName === 'SVG' && node.tagName !== 'SVG') { + return NodeFilter.FILTER_REJECT; + } + return NodeFilter.FILTER_ACCEPT; + }, + }; + + const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, filter); + while (walker.nextNode()) { + const node = walker.currentNode; + if (node.isConnected) { + elements.push(node); + if (node.shadowRoot) elements.push(...getAllElements(node.shadowRoot)); + } + } + return elements; + } + + // ============================================================================ + // CAPTCHA DETECTION (detection only, no solving/bypass logic) + // ============================================================================ + + const CAPTCHA_DETECTED_THRESHOLD = 0.7; + const CAPTCHA_MAX_EVIDENCE = 5; + const CAPTCHA_TEXT_MAX_LEN = 2000; + + const CAPTCHA_TEXT_KEYWORDS = [ + 'verify you are human', + 'captcha', + 'human verification', + 'unusual traffic', + 'are you a robot', + 'security check', + 'prove you are human', + 'bot detection', + 'automated access', + ]; + + const CAPTCHA_URL_HINTS = ['captcha', 'challenge', 'verify']; + + const CAPTCHA_IFRAME_HINTS = { + recaptcha: ['recaptcha', 'google.com/recaptcha'], + hcaptcha: ['hcaptcha.com'], + turnstile: ['challenges.cloudflare.com', 'turnstile'], + arkose: ['arkoselabs.com', 'funcaptcha.com', 'client-api.arkoselabs.com'], + awswaf: ['amazonaws.com/captcha', 'awswaf.com'], + }; + + const CAPTCHA_SCRIPT_HINTS = { + recaptcha: ['recaptcha'], + hcaptcha: ['hcaptcha'], + turnstile: ['turnstile', 'challenges.cloudflare.com'], + arkose: ['arkoselabs', 'funcaptcha'], + awswaf: ['captcha.awswaf', 'awswaf-captcha'], + }; + + const CAPTCHA_CONTAINER_SELECTORS = [ + // reCAPTCHA + { selector: '.g-recaptcha', provider: 'recaptcha' }, + { selector: '#g-recaptcha', provider: 'recaptcha' }, + { selector: '[data-sitekey]', provider: 'unknown' }, + { selector: 'iframe[title*="recaptcha" i]', provider: 'recaptcha' }, + // hCaptcha + { selector: '.h-captcha', provider: 'hcaptcha' }, + { selector: '#h-captcha', provider: 'hcaptcha' }, + { selector: 'iframe[title*="hcaptcha" i]', provider: 'hcaptcha' }, + // Cloudflare Turnstile + { selector: '.cf-turnstile', provider: 'turnstile' }, + { selector: '[data-cf-turnstile-sitekey]', provider: 'turnstile' }, + { selector: 'iframe[src*="challenges.cloudflare.com"]', provider: 'turnstile' }, + // Arkose Labs / FunCaptcha + { selector: '#FunCaptcha', provider: 'arkose' }, + { selector: '.funcaptcha', provider: 'arkose' }, + { selector: '[data-arkose-public-key]', provider: 'arkose' }, + { selector: 'iframe[src*="arkoselabs"]', provider: 'arkose' }, + // AWS WAF CAPTCHA + { selector: '#captcha-container', provider: 'awswaf' }, + { selector: '[data-awswaf-captcha]', provider: 'awswaf' }, + // Generic + { selector: 'iframe[title*="captcha" i]', provider: 'unknown' }, + ]; + + function addEvidence(list, value) { + if (!value) return; + if (list.length >= CAPTCHA_MAX_EVIDENCE) return; + list.push(value); + } + + function truncateText(text, maxLen) { + if (!text) return ''; + if (text.length <= maxLen) return text; + return text.slice(0, maxLen); + } + + function collectVisibleTextSnippet() { + try { + const candidates = document.querySelectorAll( + 'h1, h2, h3, h4, p, label, button, form, div, span' + ); + let combined = ''; + let count = 0; + for (const node of candidates) { + if (count >= 30 || combined.length >= CAPTCHA_TEXT_MAX_LEN) break; + if (!node || typeof node.innerText !== 'string') continue; + if (!node.offsetWidth && !node.offsetHeight && !node.getClientRects().length) continue; + const text = node.innerText.replace(/\s+/g, ' ').trim(); + if (!text) continue; + combined += `${text} `; + count += 1; + } + combined = combined.trim(); + if (combined) { + return truncateText(combined, CAPTCHA_TEXT_MAX_LEN); + } + } catch (e) { + // ignore + } + + try { + let bodyText = document.body?.innerText || ''; + if (!bodyText && document.body?.textContent) { + bodyText = document.body.textContent; + } + return truncateText(bodyText.replace(/\s+/g, ' ').trim(), CAPTCHA_TEXT_MAX_LEN); + } catch (e) { + return ''; + } + } + + function matchHints(value, hints) { + const lower = String(value || '').toLowerCase(); + if (!lower) return false; + return hints.some((hint) => lower.includes(hint)); + } + + function detectCaptcha() { + const evidence = { + text_hits: [], + selector_hits: [], + iframe_src_hits: [], + url_hits: [], + }; + + let hasIframeHit = false; + let hasContainerHit = false; + let hasScriptHit = false; + let hasKeywordHit = false; + let hasUrlHit = false; + + const providerSignals = { + recaptcha: 0, + hcaptcha: 0, + turnstile: 0, + arkose: 0, + awswaf: 0, + }; + + // Iframe hints (strongest signal) + try { + const iframes = document.querySelectorAll('iframe'); + for (const iframe of iframes) { + const src = iframe.getAttribute('src') || ''; + const title = iframe.getAttribute('title') || ''; + if (src) { + for (const [provider, hints] of Object.entries(CAPTCHA_IFRAME_HINTS)) { + if (matchHints(src, hints)) { + hasIframeHit = true; + providerSignals[provider] += 1; + addEvidence(evidence.iframe_src_hits, truncateText(src, 120)); + } + } + } + if (title && matchHints(title, ['captcha', 'recaptcha'])) { + hasContainerHit = true; + addEvidence(evidence.selector_hits, 'iframe[title*="captcha"]'); + } + if (evidence.iframe_src_hits.length >= CAPTCHA_MAX_EVIDENCE) break; + } + } catch (e) { + // ignore + } + + // Script hints + try { + const scripts = document.querySelectorAll('script[src]'); + for (const script of scripts) { + const src = script.getAttribute('src') || ''; + if (!src) continue; + for (const [provider, hints] of Object.entries(CAPTCHA_SCRIPT_HINTS)) { + if (matchHints(src, hints)) { + hasScriptHit = true; + providerSignals[provider] += 1; + addEvidence(evidence.selector_hits, `script[src*="${hints[0]}"]`); + } + } + if (evidence.selector_hits.length >= CAPTCHA_MAX_EVIDENCE) break; + } + } catch (e) { + // ignore + } + + // Container selectors + for (const { selector, provider } of CAPTCHA_CONTAINER_SELECTORS) { + try { + const hit = document.querySelector(selector); + if (hit) { + hasContainerHit = true; + addEvidence(evidence.selector_hits, selector); + if (provider !== 'unknown') { + providerSignals[provider] += 1; + } + } + } catch (e) { + // ignore invalid selectors + } + } + + // Text keyword hints + const textSnippet = collectVisibleTextSnippet(); + if (textSnippet) { + const lowerText = textSnippet.toLowerCase(); + for (const keyword of CAPTCHA_TEXT_KEYWORDS) { + if (lowerText.includes(keyword)) { + hasKeywordHit = true; + addEvidence(evidence.text_hits, keyword); + } + } + } + + // URL hints + try { + const url = window.location?.href || ''; + const lowerUrl = url.toLowerCase(); + for (const hint of CAPTCHA_URL_HINTS) { + if (lowerUrl.includes(hint)) { + hasUrlHit = true; + addEvidence(evidence.url_hits, hint); + } + } + } catch (e) { + // ignore + } + + // Confidence scoring + let confidence = 0.0; + if (hasIframeHit) confidence += 0.7; + if (hasContainerHit) confidence += 0.5; + if (hasScriptHit) confidence += 0.5; + if (hasKeywordHit) confidence += 0.3; + if (hasUrlHit) confidence += 0.2; + confidence = Math.min(1.0, confidence); + + if (hasIframeHit) { + confidence = Math.max(confidence, 0.8); + } + + if (hasKeywordHit && !hasIframeHit && !hasContainerHit && !hasScriptHit && !hasUrlHit) { + confidence = Math.min(confidence, 0.4); + } + + const detected = confidence >= CAPTCHA_DETECTED_THRESHOLD; + + let providerHint = null; + if (providerSignals.recaptcha > 0) { + providerHint = 'recaptcha'; + } else if (providerSignals.hcaptcha > 0) { + providerHint = 'hcaptcha'; + } else if (providerSignals.turnstile > 0) { + providerHint = 'turnstile'; + } else if (providerSignals.arkose > 0) { + providerHint = 'arkose'; + } else if (providerSignals.awswaf > 0) { + providerHint = 'awswaf'; + } else if (detected) { + providerHint = 'unknown'; + } + + return { + detected, + provider_hint: providerHint, + confidence, + evidence, + }; + } + + // ============================================================================ + // LABEL INFERENCE SYSTEM + // ============================================================================ + + // Default inference configuration (conservative - Stage 1 equivalent) + const DEFAULT_INFERENCE_CONFIG = { + // Allowed tag names that can be inference sources + allowedTags: ['label', 'span', 'div'], + + // Allowed ARIA roles that can be inference sources + allowedRoles: [], + + // Class name patterns (substring match, case-insensitive) + allowedClassPatterns: [], + + // DOM tree traversal limits + maxParentDepth: 2, // Max 2 levels up DOM tree + maxSiblingDistance: 1, // Only immediate previous/next sibling + + // Container requirements (no distance-based checks) + requireSameContainer: true, // Must share common parent + containerTags: ['form', 'fieldset', 'div'], + + // Enable/disable specific inference methods + methods: { + explicitLabel: true, // el.labels API + ariaLabelledby: true, // aria-labelledby attribute + parentTraversal: true, // Check parent/grandparent + siblingProximity: true, // Check preceding sibling (same container only) + }, + }; + + // Merge user config with defaults + function mergeInferenceConfig(userConfig = {}) { + return { + ...DEFAULT_INFERENCE_CONFIG, + ...userConfig, + methods: { + ...DEFAULT_INFERENCE_CONFIG.methods, + ...(userConfig.methods || {}), + }, + }; + } + + // Check if element matches inference source criteria + function isInferenceSource(el, config) { + if (!el || !el.tagName) return false; + + const tag = el.tagName.toLowerCase(); + const role = el.getAttribute ? el.getAttribute('role') : ''; + const className = ((el.className || '') + '').toLowerCase(); + + // Check tag name + if (config.allowedTags.includes(tag)) { + return true; + } + + // Check role + if (config.allowedRoles.length > 0 && role && config.allowedRoles.includes(role)) { + return true; + } + + // Check class patterns + if (config.allowedClassPatterns.length > 0) { + for (const pattern of config.allowedClassPatterns) { + if (className.includes(pattern.toLowerCase())) { + return true; + } + } + } + + return false; + } + + // Helper: Find common parent element + function findCommonParent(el1, el2) { + if (!el1 || !el2) return null; + + // Get document reference safely for stopping conditions + // eslint-disable-next-line no-undef + const doc = + (typeof global !== 'undefined' && global.document) || + (typeof window !== 'undefined' && window.document) || + (typeof document !== 'undefined' && document) || + null; + + const parents1 = []; + let current = el1; + // Collect all parents (including el1 itself) + while (current) { + parents1.push(current); + // Stop if no parent + if (!current.parentElement) { + break; + } + // Stop at body or documentElement if they exist + if (doc && (current === doc.body || current === doc.documentElement)) { + break; + } + current = current.parentElement; + } + + // Check if el2 or any of its parents are in parents1 + current = el2; + while (current) { + // Use indexOf for more reliable comparison (handles object identity) + if (parents1.indexOf(current) !== -1) { + return current; + } + // Stop if no parent + if (!current.parentElement) { + break; + } + // Stop at body or documentElement if they exist + if (doc && (current === doc.body || current === doc.documentElement)) { + break; + } + current = current.parentElement; + } + + return null; + } + + // Helper: Check if element is a valid container + function isValidContainer(el, validTags) { + if (!el || !el.tagName) return false; + const tag = el.tagName.toLowerCase(); + // Handle both string and object className + let className = ''; + try { + className = (el.className || '') + ''; + } catch (e) { + className = ''; + } + return ( + validTags.includes(tag) || + className.toLowerCase().includes('form') || + className.toLowerCase().includes('field') + ); + } + + // Helper: Check container requirements (no distance-based checks) + function isInSameValidContainer(element, candidate, limits) { + if (!element || !candidate) return false; + + // Check same container requirement + if (limits.requireSameContainer) { + const commonParent = findCommonParent(element, candidate); + if (!commonParent) { + return false; + } + // Check if common parent is a valid container + if (!isValidContainer(commonParent, limits.containerTags)) { + return false; + } + } + + return true; + } + + // Main inference function + function getInferredLabel(el, options = {}) { + if (!el) return null; + + const { + enableInference = true, + inferenceConfig = {}, // User-provided config, merged with defaults + } = options; + + if (!enableInference) return null; + + // OPTIMIZATION: If element already has text or aria-label, skip inference entirely + // Check this BEFORE checking labels, so we don't infer if element already has text + // Note: For INPUT elements, we check value/placeholder, not innerText + // For IMG elements, we check alt, not innerText + // For other elements, innerText is considered explicit text + const ariaLabel = el.getAttribute ? el.getAttribute('aria-label') : null; + const hasAriaLabel = ariaLabel && ariaLabel.trim(); + const hasInputValue = el.tagName === 'INPUT' && (el.value || el.placeholder); + const hasImgAlt = el.tagName === 'IMG' && el.alt; + // For non-input/img elements, check innerText - but only if it's not empty + // Access innerText safely - it might be a getter or property + let innerTextValue = ''; + try { + innerTextValue = el.innerText || ''; + } catch (e) { + // If innerText access fails, treat as empty + innerTextValue = ''; + } + const hasInnerText = + el.tagName !== 'INPUT' && el.tagName !== 'IMG' && innerTextValue && innerTextValue.trim(); + + if (hasAriaLabel || hasInputValue || hasImgAlt || hasInnerText) { + return null; + } + + // Merge config with defaults + const config = mergeInferenceConfig(inferenceConfig); + + // Method 1: Explicit label association (el.labels API) + if (config.methods.explicitLabel && el.labels && el.labels.length > 0) { + const label = el.labels[0]; + if (isInferenceSource(label, config)) { + const text = (label.innerText || '').trim(); + if (text) { + return { + text, + source: 'explicit_label', + }; + } + } + } + + // Method 2: aria-labelledby (supports space-separated list of IDs) + // NOTE: aria-labelledby is an EXPLICIT reference, so it should work with ANY element + // regardless of inference source criteria. The config only controls whether this method runs. + if (config.methods.ariaLabelledby && el.hasAttribute && el.hasAttribute('aria-labelledby')) { + const labelIdsAttr = el.getAttribute('aria-labelledby'); + if (labelIdsAttr) { + // Split by whitespace to support multiple IDs (space-separated list) + const labelIds = labelIdsAttr.split(/\s+/).filter((id) => id.trim()); + const labelTexts = []; + + // Helper function to get document.getElementById from available contexts + const getDocument = () => { + // eslint-disable-next-line no-undef + if (typeof global !== 'undefined' && global.document) { + // eslint-disable-next-line no-undef + return global.document; + } + if (typeof window !== 'undefined' && window.document) { + return window.document; + } + if (typeof document !== 'undefined') { + return document; + } + return null; + }; + + const doc = getDocument(); + if (!doc || !doc.getElementById) ; else { + // Process each ID in the space-separated list + for (const labelId of labelIds) { + if (!labelId.trim()) continue; + + let labelEl = null; + try { + labelEl = doc.getElementById(labelId); + } catch (e) { + // If getElementById fails, skip this ID + continue; + } + + // aria-labelledby is an explicit reference - use ANY element, not just those matching inference criteria + // This follows ARIA spec: aria-labelledby can reference any element in the document + if (labelEl) { + // Extract text from the referenced element + let text = ''; + try { + // Try innerText first (preferred for visible text) + text = (labelEl.innerText || '').trim(); + // Fallback to textContent if innerText is empty + if (!text && labelEl.textContent) { + text = labelEl.textContent.trim(); + } + // Fallback to aria-label if available + if (!text && labelEl.getAttribute) { + const ariaLabel = labelEl.getAttribute('aria-label'); + if (ariaLabel) { + text = ariaLabel.trim(); + } + } + } catch (e) { + // If text extraction fails, skip this element + continue; + } + + if (text) { + labelTexts.push(text); + } + } + } + } + + // Combine multiple label texts (space-separated) + if (labelTexts.length > 0) { + return { + text: labelTexts.join(' '), + source: 'aria_labelledby', + }; + } + } + } + + // Method 3: Parent/grandparent traversal + if (config.methods.parentTraversal) { + let parent = el.parentElement; + let depth = 0; + while (parent && depth < config.maxParentDepth) { + if (isInferenceSource(parent, config)) { + const text = (parent.innerText || '').trim(); + if (text) { + return { + text, + source: 'parent_label', + }; + } + } + parent = parent.parentElement; + depth++; + } + } + + // Method 4: Preceding sibling (no distance-based checks, only DOM structure) + if (config.methods.siblingProximity) { + const prev = el.previousElementSibling; + if (prev && isInferenceSource(prev, config)) { + // Only check if they're in the same valid container (no pixel distance) + if ( + isInSameValidContainer(el, prev, { + requireSameContainer: config.requireSameContainer, + containerTags: config.containerTags, + }) + ) { + const text = (prev.innerText || '').trim(); + if (text) { + return { + text, + source: 'sibling_label', + }; + } + } + } + } + + return null; + } + + // --- HELPER: Nearby Static Text (cheap, best-effort) --- + // Returns a short, single-line snippet near the element (sibling/parent). + function getNearbyText(el, options = {}) { + if (!el) return null; + + const maxLen = typeof options.maxLen === 'number' ? options.maxLen : 80; + const ownText = normalizeNearbyText(el.innerText || ''); + + const candidates = []; + + const collect = (node) => { + if (!node) return; + let text = ''; + try { + text = normalizeNearbyText(node.innerText || node.textContent || ''); + } catch (e) { + text = ''; + } + if (!text || text === ownText) return; + candidates.push(text); + }; + + // Prefer immediate siblings + collect(el.previousElementSibling); + collect(el.nextElementSibling); + + // Fallback: short parent text (avoid large blocks) + if (candidates.length === 0 && el.parentElement) { + let parentText = ''; + try { + parentText = normalizeNearbyText(el.parentElement.innerText || ''); + } catch (e) { + parentText = ''; + } + if (parentText && parentText !== ownText && parentText.length <= 120) { + candidates.push(parentText); + } + } + + if (candidates.length === 0) return null; + + let text = candidates[0]; + if (text.length > maxLen) { + text = text.slice(0, maxLen).trim(); + } + return text || null; + } + + function normalizeNearbyText(text) { + if (!text) return ''; + return text.replace(/\s+/g, ' ').trim(); + } + + // Helper: Check if element is interactable (should have role inferred) + function isInteractableElement(el) { + if (!el || !el.tagName) return false; + + const tag = el.tagName.toLowerCase(); + const role = el.getAttribute ? el.getAttribute('role') : null; + const hasTabIndex = el.hasAttribute ? el.hasAttribute('tabindex') : false; + const hasHref = el.tagName === 'A' && (el.hasAttribute ? el.hasAttribute('href') : false); + + // Native interactive elements + const interactiveTags = [ + 'button', + 'input', + 'textarea', + 'select', + 'option', + 'details', + 'summary', + 'a', + ]; + if (interactiveTags.includes(tag)) { + // For , only if it has href + if (tag === 'a' && !hasHref) return false; + return true; + } + + // Elements with explicit interactive roles + const interactiveRoles = [ + 'button', + 'link', + 'tab', + 'menuitem', + 'checkbox', + 'radio', + 'switch', + 'slider', + 'combobox', + 'textbox', + 'searchbox', + 'spinbutton', + ]; + if (role && interactiveRoles.includes(role.toLowerCase())) { + return true; + } + + // Focusable elements (tabindex makes them interactive) + if (hasTabIndex) { + return true; + } + + // Elements with event handlers (custom interactive elements) + if (el.onclick || el.onkeydown || el.onkeypress || el.onkeyup) { + return true; + } + + // Check for inline event handlers via attributes + if (el.getAttribute) { + const hasInlineHandler = + el.getAttribute('onclick') || + el.getAttribute('onkeydown') || + el.getAttribute('onkeypress') || + el.getAttribute('onkeyup'); + if (hasInlineHandler) { + return true; + } + } + + return false; + } + + // Helper: Infer ARIA role for interactable elements + function getInferredRole(el, options = {}) { + const { + enableInference = true, + // inferenceConfig reserved for future extensibility + } = options; + + if (!enableInference) return null; + + // Only infer roles for interactable elements + if (!isInteractableElement(el)) { + return null; + } + + // CRITICAL: Only infer if element has NO aria-label AND NO explicit role + const hasAriaLabel = el.getAttribute ? el.getAttribute('aria-label') : null; + const hasExplicitRole = el.getAttribute ? el.getAttribute('role') : null; + + if (hasAriaLabel || hasExplicitRole) { + return null; // Skip inference if element already has aria-label or role + } + + // If element is native semantic HTML, it already has a role + const tag = el.tagName.toLowerCase(); + const semanticTags = ['button', 'a', 'input', 'textarea', 'select', 'option']; + if (semanticTags.includes(tag)) { + return null; // Native HTML already has role + } + + // Infer role based on element behavior or context + // Check for click handlers first (most common) + if (el.onclick || (el.getAttribute && el.getAttribute('onclick'))) { + return 'button'; + } + + // Check for keyboard handlers + if ( + el.onkeydown || + el.onkeypress || + el.onkeyup || + (el.getAttribute && + (el.getAttribute('onkeydown') || el.getAttribute('onkeypress') || el.getAttribute('onkeyup'))) + ) { + return 'button'; // Default to button for keyboard-interactive elements + } + + // Focusable div/span likely needs a role + if (el.hasAttribute && el.hasAttribute('tabindex') && (tag === 'div' || tag === 'span')) { + return 'button'; // Default assumption for focusable elements + } + + return null; + } + + // --- HELPER: Smart Text Extractor --- + function getText(el) { + if (el.getAttribute('aria-label')) return el.getAttribute('aria-label'); + if (el.tagName === 'INPUT') { + // Privacy: never return password values + const t = (el.getAttribute && el.getAttribute('type')) || el.type || ''; + if (String(t).toLowerCase() === 'password') { + return el.placeholder || ''; + } + return el.value || el.placeholder || ''; + } + if (el.tagName === 'IMG') return el.alt || ''; + return (el.innerText || '').replace(/\s+/g, ' ').trim().substring(0, 100); + } + + // Best-effort accessible name extraction for controls (used for v1 state-aware assertions) + function getAccessibleName(el) { + if (!el || !el.getAttribute) return ''; + + // 1) aria-label + const ariaLabel = el.getAttribute('aria-label'); + if (ariaLabel && ariaLabel.trim()) return ariaLabel.trim().substring(0, 200); + + // 2) aria-labelledby (space-separated IDs) + const labelledBy = el.getAttribute('aria-labelledby'); + if (labelledBy && labelledBy.trim()) { + const ids = labelledBy.split(/\s+/).filter((id) => id.trim()); + const texts = []; + for (const id of ids) { + try { + const ref = document.getElementById(id); + if (!ref) continue; + const txt = (ref.innerText || ref.textContent || ref.getAttribute?.('aria-label') || '') + .toString() + .trim(); + if (txt) texts.push(txt); + } catch (e) { + // ignore + } + } + if (texts.length > 0) return texts.join(' ').substring(0, 200); + } + + // 3) has the text + // Case 2: Spans that wrap links (parent spans like HN's "titleline") - child is the actionable element + // This significantly reduces element count on link-heavy pages (HN, Reddit, search results) + const tagName = el.tagName.toLowerCase(); + if (tagName === 'span') { + // Case 1: Span is inside a link (any ancestor ) + if (el.closest('a')) { + return; // Skip - parent link has the content + } + // Case 2: Span contains a link as ANY descendant (wrapper span) + // HN structure: Title... + // Also handles: ... + const childLink = el.querySelector('a[href]'); // Find ANY descendant link with href + if (childLink && childLink.href) { + return; // Skip - descendant link is the actionable element + } + // Debug: Log spans with "titleline" class that weren't filtered + if (options.debug && el.className && el.className.includes('titleline')) { + console.log('[SentienceAPI] DEBUG: titleline span NOT filtered', { + className: el.className, + text: el.textContent?.slice(0, 50), + childLink: childLink, + hasChildHref: childLink?.href, + }); + } + } + + window.sentience_registry[idx] = el; + + // Input type is needed for safe value redaction (passwords) and state-aware assertions + const inputType = + tagName === 'input' + ? toSafeString((el.getAttribute && el.getAttribute('type')) || el.type || null) + : null; + const isPasswordInput = inputType && inputType.toLowerCase() === 'password'; + + // Use getSemanticText for inference support (falls back to getText if no inference) + const semanticText = getSemanticText(el, { + enableInference: options.enableInference !== false, // Default: true + inferenceConfig: options.inferenceConfig, // Pass configurable inference settings + }); + const textVal = semanticText.text || getText(el); // Fallback to getText for backward compat + + // Infer role for interactable elements (only if no aria-label and no explicit role) + const inferredRole = getInferredRole(el, { + enableInference: options.enableInference !== false, + inferenceConfig: options.inferenceConfig, + }); + const inView = isInViewport(rect); + + // Get computed style once (needed for both occlusion check and data collection) + const style = window.getComputedStyle(el); + + // Only check occlusion for elements likely to be occluded (optimized) + // This avoids layout thrashing for the vast majority of elements + const occluded = inView ? isOccluded(el, rect, style) : false; + + // Get effective background color (traverses DOM to find non-transparent color) + const effectiveBgColor = getEffectiveBackgroundColor(el); + + // Safe value extraction (PII-aware) + let safeValue = null; + let valueRedacted = null; + try { + if (el.value !== undefined || (el.getAttribute && el.getAttribute('value') !== null)) { + if (isPasswordInput) { + safeValue = null; + valueRedacted = 'true'; + } else { + const rawValue = + el.value !== undefined ? String(el.value) : String(el.getAttribute('value')); + safeValue = rawValue.length > 200 ? rawValue.substring(0, 200) : rawValue; + valueRedacted = 'false'; + } + } + } catch (e) { + // ignore + } + + // Best-effort accessible name (label-like, not the typed value) + const accessibleName = toSafeString(getAccessibleName(el) || null); + + const nearbyText = isInteractableElement(el) ? getNearbyText(el, { maxLen: 80 }) : null; + + rawData.push({ + id: idx, + tag: tagName, + rect: { x: rect.x, y: rect.y, width: rect.width, height: rect.height }, + styles: { + display: toSafeString(style.display), + visibility: toSafeString(style.visibility), + opacity: toSafeString(style.opacity), + z_index: toSafeString(style.zIndex || 'auto'), + position: toSafeString(style.position), + bg_color: toSafeString(effectiveBgColor || style.backgroundColor), + color: toSafeString(style.color), + cursor: toSafeString(style.cursor), + font_weight: toSafeString(style.fontWeight), + font_size: toSafeString(style.fontSize), + }, + attributes: { + role: toSafeString(el.getAttribute('role')), + type_: toSafeString(el.getAttribute('type')), + input_type: inputType, + aria_label: + semanticText?.source === 'explicit_aria_label' + ? semanticText.text + : toSafeString(el.getAttribute('aria-label')), // Keep original for backward compat + name: accessibleName, + inferred_label: + semanticText?.source && + !['explicit_aria_label', 'input_value', 'img_alt', 'inner_text'].includes( + semanticText.source + ) + ? toSafeString(semanticText.text) + : null, + label_source: semanticText?.source || null, // Track source for gateway + inferred_role: inferredRole ? toSafeString(inferredRole) : null, // Inferred role for interactable elements + nearby_text: toSafeString(nearbyText), + // Get href: check element first, then traverse up to find parent link + // This ensures nested spans inside links inherit the href + href: toSafeString( + el.href || el.getAttribute('href') || (el.closest && el.closest('a')?.href) || null + ), + class: toSafeString(getClassName(el)), + // Capture dynamic input state (not just initial attributes) + value: safeValue !== null ? toSafeString(safeValue) : null, + value_redacted: valueRedacted, + checked: el.checked !== undefined ? String(el.checked) : null, + disabled: el.disabled !== undefined ? String(el.disabled) : null, + aria_checked: toSafeString(el.getAttribute('aria-checked')), + aria_disabled: toSafeString(el.getAttribute('aria-disabled')), + aria_expanded: toSafeString(el.getAttribute('aria-expanded')), + }, + text: toSafeString(textVal), + in_viewport: inView, + is_occluded: occluded, + // Phase 1: Pass scroll position for doc_y computation in WASM + scroll_y: window.scrollY, + }); + }); + + console.log(`[SentienceAPI] Collected ${rawData.length} elements from main frame`); + + // Step 1.5: Collect iframe snapshots and FLATTEN immediately + // "Flatten Early" architecture: Merge iframe elements into main array before WASM + // This allows WASM to process all elements uniformly (no recursion needed) + const allRawElements = [...rawData]; // Start with main frame elements + let totalIframeElements = 0; + + if (options.collectIframes !== false) { + try { + console.log(`[SentienceAPI] Starting iframe collection...`); + const iframeSnapshots = await collectIframeSnapshots(options); + console.log( + `[SentienceAPI] Iframe collection complete. Received ${iframeSnapshots.size} snapshot(s)` + ); + + if (iframeSnapshots.size > 0) { + // FLATTEN IMMEDIATELY: Don't nest them. Just append them with coordinate translation. + iframeSnapshots.forEach((iframeSnapshot, iframeEl) => { + // Debug: Log structure to verify data is correct + // console.log(`[SentienceAPI] Processing iframe snapshot:`, iframeSnapshot); + + if (iframeSnapshot && iframeSnapshot.raw_elements) { + const rawElementsCount = iframeSnapshot.raw_elements.length; + console.log( + `[SentienceAPI] Processing ${rawElementsCount} elements from iframe (src: ${iframeEl.src || 'unknown'})` + ); + // Get iframe's bounding rect (offset for coordinate translation) + const iframeRect = iframeEl.getBoundingClientRect(); + const offset = { x: iframeRect.x, y: iframeRect.y }; + + // Get iframe context for frame switching (Playwright needs this) + const iframeSrc = iframeEl.src || iframeEl.getAttribute('src') || ''; + let isSameOrigin = false; + try { + // Try to access contentWindow to check if same-origin + isSameOrigin = iframeEl.contentWindow !== null; + } catch (e) { + isSameOrigin = false; + } + + // Adjust coordinates and add iframe context to each element + const adjustedElements = iframeSnapshot.raw_elements.map((el) => { + const adjusted = { ...el }; + + // Adjust rect coordinates to parent viewport + if (adjusted.rect) { + adjusted.rect = { + ...adjusted.rect, + x: adjusted.rect.x + offset.x, + y: adjusted.rect.y + offset.y, + }; + } + + // Add iframe context so agents can switch frames in Playwright + adjusted.iframe_context = { + src: iframeSrc, + is_same_origin: isSameOrigin, + }; + + return adjusted; + }); + + // Append flattened iframe elements to main array + allRawElements.push(...adjustedElements); + totalIframeElements += adjustedElements.length; + } + }); + + // console.log(`[SentienceAPI] Merged ${iframeSnapshots.size} iframe(s). Total elements: ${allRawElements.length} (${rawData.length} main + ${totalIframeElements} iframe)`); + } + } catch (error) { + console.warn('[SentienceAPI] Iframe collection failed:', error); + } + } + + // Step 2: Send EVERYTHING to WASM (One giant flat list) + // Now WASM prunes iframe elements and main elements in one pass! + // No recursion needed - everything is already flat + console.log( + `[SentienceAPI] Sending ${allRawElements.length} total elements to WASM (${rawData.length} main + ${totalIframeElements} iframe)` + ); + const fallbackElementsFromRaw = (raw) => + (raw || []).map((r) => { + const rect = (r && r.rect) || { x: 0, y: 0, width: 0, height: 0 }; + const attrs = (r && r.attributes) || {}; + const role = + attrs.role || + (r && (r.inferred_role || r.inferredRole)) || + (r && r.tag === 'a' ? 'link' : 'generic'); + const href = attrs.href || (r && r.href) || null; + const isClickable = + role === 'link' || + role === 'button' || + role === 'textbox' || + role === 'checkbox' || + role === 'radio' || + role === 'combobox' || + !!href; + + return { + id: Number((r && r.id) || 0), + role: String(role || 'generic'), + text: (r && (r.text || r.semantic_text || r.semanticText)) || null, + importance: 1, + bbox: { + x: Number(rect.x || 0), + y: Number(rect.y || 0), + width: Number(rect.width || 0), + height: Number(rect.height || 0), + }, + visual_cues: { + is_primary: false, + is_clickable: !!isClickable, + }, + in_viewport: true, + is_occluded: !!(r && (r.occluded || r.is_occluded)), + z_index: 0, + name: attrs.aria_label || attrs.ariaLabel || null, + value: (r && r.value) || null, + input_type: attrs.type_ || attrs.type || null, + checked: typeof (r && r.checked) === 'boolean' ? r.checked : null, + disabled: typeof (r && r.disabled) === 'boolean' ? r.disabled : null, + expanded: typeof (r && r.expanded) === 'boolean' ? r.expanded : null, + }; + }); + + let processed = null; + try { + processed = await processSnapshotInBackground(allRawElements, options); + } catch (error) { + console.warn( + '[SentienceAPI] WASM processing failed; falling back to raw mapping:', + error + ); + processed = { + elements: fallbackElementsFromRaw(allRawElements), + raw_elements: allRawElements, + duration: null, + }; + } + + if (!processed || !processed.elements) { + processed = { + elements: fallbackElementsFromRaw(allRawElements), + raw_elements: allRawElements, + duration: null, + }; + } + + // Step 3: Capture screenshot if requested + let screenshot = null; + if (options.screenshot) { + screenshot = await captureScreenshot(options.screenshot); + } + + // Step 4: Clean and return + const cleanedElements = cleanElement(processed.elements); + const cleanedRawElements = cleanElement(processed.raw_elements); + + // FIXED: Removed undefined 'totalIframeRawElements' + // FIXED: Logic updated for "Flatten Early" architecture. + // processed.elements ALREADY contains the merged iframe elements, + // so we simply use .length. No addition needed. + + const totalCount = cleanedElements.length; + const totalRaw = cleanedRawElements.length; + const iframeCount = totalIframeElements || 0; + + console.log( + `[SentienceAPI] ✓ Complete: ${totalCount} Smart Elements, ${totalRaw} Raw Elements (includes ${iframeCount} from iframes) (WASM took ${processed.duration?.toFixed(1)}ms)` + ); + + // Snapshot diagnostics (Phase 2): report stability metrics from the page context. + // Confidence/exhaustion is computed in the Gateway/SDKs; the extension supplies raw metrics. + let diagnostics = undefined; + try { + const lastMutationTs = window.__sentience_lastMutationTs; + const now = performance.now(); + const quietMs = + typeof lastMutationTs === 'number' && Number.isFinite(lastMutationTs) + ? Math.max(0, now - lastMutationTs) + : null; + const nodeCount = document.querySelectorAll('*').length; + + // P1-01: best-effort signal that structure may be insufficient (vision executor recommended). + // Keep heuristics conservative: we only set requires_vision when we see clear structural blockers. + let requiresVision = false; + let requiresVisionReason = null; + const canvasCount = document.getElementsByTagName('canvas').length; + if (canvasCount > 0) { + requiresVision = true; + requiresVisionReason = `canvas:${canvasCount}`; + } + + diagnostics = { + metrics: { + ready_state: document.readyState || null, + quiet_ms: quietMs, + node_count: nodeCount, + }, + captcha: detectCaptcha(), + requires_vision: requiresVision, + requires_vision_reason: requiresVisionReason, + }; + } catch (e) { + // ignore + } + + return { + status: 'success', + url: window.location.href, + viewport: { + width: window.innerWidth, + height: window.innerHeight, + }, + elements: cleanedElements, + raw_elements: cleanedRawElements, + screenshot, + diagnostics, + }; + } catch (error) { + console.error('[SentienceAPI] snapshot() failed:', error); + console.error('[SentienceAPI] Error stack:', error.stack); + return { + status: 'error', + error: error.message || 'Unknown error', + stack: error.stack, + }; + } + } + + // read.js - Content Reading Methods + + // 2. Read Content (unchanged) + function read(options = {}) { + const format = options.format || 'raw'; + let content; + + if (format === 'raw') { + content = getRawHTML(document.body); + } else if (format === 'markdown') { + content = convertToMarkdown(document.body); + } else { + content = convertToText(document.body); + } + + return { + status: 'success', + url: window.location.href, + format, + content, + length: content.length, + }; + } + + // 2b. Find Text Rectangle - Get exact pixel coordinates of specific text + function findTextRect(options = {}) { + const { + text, + containerElement = document.body, + caseSensitive = false, + wholeWord = false, + maxResults = 10, + } = options; + + if (!text || text.trim().length === 0) { + return { + status: 'error', + error: 'Text parameter is required', + }; + } + + const results = []; + const searchText = caseSensitive ? text : text.toLowerCase(); + + // Helper function to find text in a single text node + function findInTextNode(textNode) { + const nodeText = textNode.nodeValue; + const searchableText = caseSensitive ? nodeText : nodeText.toLowerCase(); + + let startIndex = 0; + while (startIndex < nodeText.length && results.length < maxResults) { + const foundIndex = searchableText.indexOf(searchText, startIndex); + + if (foundIndex === -1) break; + + // Check whole word matching if required + if (wholeWord) { + const before = foundIndex > 0 ? nodeText[foundIndex - 1] : ' '; + const after = + foundIndex + text.length < nodeText.length ? nodeText[foundIndex + text.length] : ' '; + + // Check if surrounded by word boundaries + if (!/\s/.test(before) || !/\s/.test(after)) { + startIndex = foundIndex + 1; + continue; + } + } + + try { + // Create range for this occurrence + const range = document.createRange(); + range.setStart(textNode, foundIndex); + range.setEnd(textNode, foundIndex + text.length); + + const rect = range.getBoundingClientRect(); + + // Only include visible rectangles + if (rect.width > 0 && rect.height > 0) { + results.push({ + text: nodeText.substring(foundIndex, foundIndex + text.length), + rect: { + x: rect.left + window.scrollX, + y: rect.top + window.scrollY, + width: rect.width, + height: rect.height, + left: rect.left + window.scrollX, + top: rect.top + window.scrollY, + right: rect.right + window.scrollX, + bottom: rect.bottom + window.scrollY, + }, + viewport_rect: { + x: rect.left, + y: rect.top, + width: rect.width, + height: rect.height, + }, + context: { + before: nodeText.substring(Math.max(0, foundIndex - 20), foundIndex), + after: nodeText.substring( + foundIndex + text.length, + Math.min(nodeText.length, foundIndex + text.length + 20) + ), + }, + in_viewport: + rect.top >= 0 && + rect.left >= 0 && + rect.bottom <= window.innerHeight && + rect.right <= window.innerWidth, + }); + } + } catch (e) { + console.warn('[SentienceAPI] Failed to get rect for text:', e); + } + + startIndex = foundIndex + 1; + } + } + + // Tree walker to find all text nodes + const walker = document.createTreeWalker(containerElement, NodeFilter.SHOW_TEXT, { + acceptNode(node) { + // Skip script, style, and empty text nodes + const parent = node.parentElement; + if (!parent) return NodeFilter.FILTER_REJECT; + + const tagName = parent.tagName.toLowerCase(); + if (tagName === 'script' || tagName === 'style' || tagName === 'noscript') { + return NodeFilter.FILTER_REJECT; + } + + // Skip whitespace-only nodes + if (!node.nodeValue || node.nodeValue.trim().length === 0) { + return NodeFilter.FILTER_REJECT; + } + + // Check if element is visible + const computedStyle = window.getComputedStyle(parent); + if ( + computedStyle.display === 'none' || + computedStyle.visibility === 'hidden' || + computedStyle.opacity === '0' + ) { + return NodeFilter.FILTER_REJECT; + } + + return NodeFilter.FILTER_ACCEPT; + }, + }); + + // Walk through all text nodes + let currentNode; + while ((currentNode = walker.nextNode()) && results.length < maxResults) { + findInTextNode(currentNode); + } + + return { + status: 'success', + query: text, + case_sensitive: caseSensitive, + whole_word: wholeWord, + matches: results.length, + results, + viewport: { + width: window.innerWidth, + height: window.innerHeight, + scroll_x: window.scrollX, + scroll_y: window.scrollY, + }, + }; + } + + // click.js - Click Action Method + + // 3. Click Action (unchanged) + function click(id) { + const el = window.sentience_registry[id]; + if (el) { + el.click(); + el.focus(); + return true; + } + return false; + } + + // registry.js - Inspector Mode / Golden Set Collection + + // 4. Inspector Mode: Start Recording for Golden Set Collection + function startRecording(options = {}) { + const { + highlightColor = '#ff0000', + successColor = '#00ff00', + autoDisableTimeout = 30 * 60 * 1000, // 30 minutes default + keyboardShortcut = 'Ctrl+Shift+I', + } = options; + + console.log( + '🔴 [Sentience] Recording Mode STARTED. Click an element to copy its Ground Truth JSON.' + ); + console.log(` Press ${keyboardShortcut} or call stopRecording() to stop.`); + + // Validate registry is populated + if (!window.sentience_registry || window.sentience_registry.length === 0) { + console.warn( + '⚠️ Registry empty. Call `await window.sentience.snapshot()` first to populate registry.' + ); + alert('Registry empty. Run `await window.sentience.snapshot()` first!'); + return () => {}; // Return no-op cleanup function + } + + // Create reverse mapping for O(1) lookup (fixes registry lookup bug) + window.sentience_registry_map = new Map(); + window.sentience_registry.forEach((el, idx) => { + if (el) window.sentience_registry_map.set(el, idx); + }); + + // Create highlight box overlay + let highlightBox = document.getElementById('sentience-highlight-box'); + if (!highlightBox) { + highlightBox = document.createElement('div'); + highlightBox.id = 'sentience-highlight-box'; + highlightBox.style.cssText = ` + position: fixed; + pointer-events: none; + z-index: 2147483647; + border: 2px solid ${highlightColor}; + background: rgba(255, 0, 0, 0.1); + display: none; + transition: all 0.1s ease; + box-sizing: border-box; + `; + document.body.appendChild(highlightBox); + } + + // Create visual indicator (red border on page when recording) + let recordingIndicator = document.getElementById('sentience-recording-indicator'); + if (!recordingIndicator) { + recordingIndicator = document.createElement('div'); + recordingIndicator.id = 'sentience-recording-indicator'; + recordingIndicator.style.cssText = ` + position: fixed; + top: 0; + left: 0; + right: 0; + height: 3px; + background: ${highlightColor}; + z-index: 2147483646; + pointer-events: none; + `; + document.body.appendChild(recordingIndicator); + } + recordingIndicator.style.display = 'block'; + + // Hover handler (visual feedback) + const mouseOverHandler = (e) => { + const el = e.target; + if (!el || el === highlightBox || el === recordingIndicator) return; + + const rect = el.getBoundingClientRect(); + highlightBox.style.display = 'block'; + highlightBox.style.top = rect.top + window.scrollY + 'px'; + highlightBox.style.left = rect.left + window.scrollX + 'px'; + highlightBox.style.width = rect.width + 'px'; + highlightBox.style.height = rect.height + 'px'; + }; + + // Click handler (capture ground truth data) + const clickHandler = (e) => { + e.preventDefault(); + e.stopPropagation(); + + const el = e.target; + if (!el || el === highlightBox || el === recordingIndicator) return; + + // Use Map for reliable O(1) lookup + const sentienceId = window.sentience_registry_map.get(el); + if (sentienceId === undefined) { + console.warn('⚠️ Element not found in Sentience Registry. Did you run snapshot() first?'); + alert('Element not in registry. Run `await window.sentience.snapshot()` first!'); + return; + } + + // Extract raw data (ground truth + raw signals, NOT model outputs) + const rawData = extractRawElementData(el); + const selector = getUniqueSelector(el); + const role = el.getAttribute('role') || el.tagName.toLowerCase(); + const text = getText(el); + + // Build golden set JSON (ground truth + raw signals only) + const snippet = { + task: `Interact with ${text.substring(0, 20)}${text.length > 20 ? '...' : ''}`, + url: window.location.href, + timestamp: new Date().toISOString(), + target_criteria: { + id: sentienceId, + selector, + role, + text: text.substring(0, 50), + }, + debug_snapshot: rawData, + }; + + // Copy to clipboard + const jsonString = JSON.stringify(snippet, null, 2); + navigator.clipboard + .writeText(jsonString) + .then(() => { + console.log('✅ Copied Ground Truth to clipboard:', snippet); + + // Flash green to indicate success + highlightBox.style.border = `2px solid ${successColor}`; + highlightBox.style.background = 'rgba(0, 255, 0, 0.2)'; + setTimeout(() => { + highlightBox.style.border = `2px solid ${highlightColor}`; + highlightBox.style.background = 'rgba(255, 0, 0, 0.1)'; + }, 500); + }) + .catch((err) => { + console.error('❌ Failed to copy to clipboard:', err); + alert('Failed to copy to clipboard. Check console for JSON.'); + }); + }; + + // Auto-disable timeout + let timeoutId = null; + + // Cleanup function to stop recording (defined before use) + const stopRecording = () => { + document.removeEventListener('mouseover', mouseOverHandler, true); + document.removeEventListener('click', clickHandler, true); + document.removeEventListener('keydown', keyboardHandler, true); + + if (timeoutId) { + clearTimeout(timeoutId); + timeoutId = null; + } + + if (highlightBox) { + highlightBox.style.display = 'none'; + } + + if (recordingIndicator) { + recordingIndicator.style.display = 'none'; + } + + // Clean up registry map (optional, but good practice) + if (window.sentience_registry_map) { + window.sentience_registry_map.clear(); + } + + // Remove global reference + if (window.sentience_stopRecording === stopRecording) { + delete window.sentience_stopRecording; + } + + console.log('⚪ [Sentience] Recording Mode STOPPED.'); + }; + + // Keyboard shortcut handler (defined after stopRecording) + const keyboardHandler = (e) => { + // Ctrl+Shift+I or Cmd+Shift+I + if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'I') { + e.preventDefault(); + stopRecording(); + } + }; + + // Attach event listeners (use capture phase to intercept early) + document.addEventListener('mouseover', mouseOverHandler, true); + document.addEventListener('click', clickHandler, true); + document.addEventListener('keydown', keyboardHandler, true); + + // Set up auto-disable timeout + if (autoDisableTimeout > 0) { + timeoutId = setTimeout(() => { + console.log('⏰ [Sentience] Recording Mode auto-disabled after timeout.'); + stopRecording(); + }, autoDisableTimeout); + } + + // Store stop function globally for keyboard shortcut access + window.sentience_stopRecording = stopRecording; + + return stopRecording; + } + + // overlay.js - Visual Overlay Methods + + /** + * Show overlay highlighting specific elements with Shadow DOM + * @param {Array} elements - List of elements with bbox, importance, visual_cues + * @param {number} targetElementId - Optional ID of target element (shown in red) + */ + function showOverlay(elements, targetElementId = null) { + if (!elements || !Array.isArray(elements)) { + console.warn('[Sentience] showOverlay: elements must be an array'); + return; + } + + window.postMessage( + { + type: 'SENTIENCE_SHOW_OVERLAY', + elements, + targetElementId, + timestamp: Date.now(), + }, + '*' + ); + + console.log(`[Sentience] Overlay requested for ${elements.length} elements`); + } + + /** + * Show grid overlay highlighting detected grids + * @param {Array} grids - Array of GridInfo objects from SDK's get_grid_bounds() + * @param {number|null} targetGridId - Optional grid ID to highlight in red + */ + function showGrid(grids, targetGridId = null) { + if (!grids || !Array.isArray(grids)) { + console.warn('[Sentience] showGrid: grids must be an array'); + return; + } + + window.postMessage( + { + type: 'SENTIENCE_SHOW_GRID_OVERLAY', + grids, + targetGridId, + timestamp: Date.now(), + }, + '*' + ); + + console.log(`[Sentience] Grid overlay requested for ${grids.length} grids`); + } + + /** + * Clear overlay manually + */ + function clearOverlay() { + window.postMessage( + { + type: 'SENTIENCE_CLEAR_OVERLAY', + }, + '*' + ); + console.log('[Sentience] Overlay cleared'); + } + + // index.js - Main Entry Point for Injected API + // This script ONLY collects raw DOM data and sends it to background for processing + + + (async () => { + // console.log('[SentienceAPI] Initializing (CSP-Resistant Mode)...'); + + // Wait for Extension ID from content.js + const getExtensionId = () => document.documentElement.dataset.sentienceExtensionId; + let extId = getExtensionId(); + + if (!extId) { + await new Promise((resolve) => { + const check = setInterval(() => { + extId = getExtensionId(); + if (extId) { + clearInterval(check); + resolve(); + } + }, 50); + setTimeout(() => resolve(), 5000); // Max 5s wait + }); + } + + if (!extId) { + console.error('[SentienceAPI] Failed to get extension ID'); + return; + } + + // console.log('[SentienceAPI] Extension ID:', extId); + + // Registry for click actions (still needed for click() function) + window.sentience_registry = []; + + // --- GLOBAL API --- + window.sentience = { + snapshot, + read, + findTextRect, + click, + startRecording, + showOverlay, + showGrid, + clearOverlay, + }; + + // Setup iframe handler when script loads (only once) + if (!window.sentience_iframe_handler_setup) { + setupIframeSnapshotHandler(); + window.sentience_iframe_handler_setup = true; + } + + console.log('[SentienceAPI] ✓ Ready! (CSP-Resistant - WASM runs in background)'); + })(); + +})(); diff --git a/sentience/extension/pkg/sentience_core_bg.wasm b/sentience/extension/pkg/sentience_core_bg.wasm index 9d0bc9a750044eb8c43b90f2c64d0f3b21bef5e0..bcbccbe8d06c994fcb661b7d94d000f57beefd10 100644 GIT binary patch delta 133 zcmbRLl5PG=whdF+C+}gmn%u~)zWE#b2PSE)9_HUnADBKeePa5|^o8jw(>JE?Om7(9 zFllHuGjeZd1zkG@ c*NT$VqP(KiG);j}GZPdMEdjUbL0cGo0sodR8~^|S delta 133 zcmbRLl5PG=whdF+CqHHvp4`apxA`0U2PSEa9_HUnADBKeePa5|^o8jw(>JE?Om7(9 zFllNvGjeZd Date: Thu, 22 Jan 2026 13:03:35 -0800 Subject: [PATCH 08/11] fix syntax error in stealth tests --- tests/test_stealth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_stealth.py b/tests/test_stealth.py index 87ff9f3..8860db5 100644 --- a/tests/test_stealth.py +++ b/tests/test_stealth.py @@ -144,8 +144,8 @@ def test_stealth_features(): # noqa: C901 print("⚠️ Note: Bot detection is a cat-and-mouse game.") print(" No solution is 100% effective against all detection systems.") print("=" * 60) - - assert True + assert True + return True except Exception as e: print(f"\n❌ Test failed: {e}") From bd9110b9690f9b60eb04092c5f20cbfac3f447df Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Thu, 22 Jan 2026 15:34:18 -0800 Subject: [PATCH 09/11] use dict subscript/key --- tests/test_agent.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_agent.py b/tests/test_agent.py index ad652e1..72cd5f2 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -206,9 +206,9 @@ def test_agent_execute_click_action(): result = agent.action_executor.execute("CLICK(1)", snap) - assert result.success is True - assert result.action == "click" - assert result.element_id == 1 + assert result["success"] is True + assert result["action"] == "click" + assert result["element_id"] == 1 mock_click.assert_called_once_with(browser, 1) From d17b08c8d05e522c018c29e656255151cb66d0f5 Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Thu, 22 Jan 2026 15:37:58 -0800 Subject: [PATCH 10/11] linting --- sentience/vision_executor.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sentience/vision_executor.py b/sentience/vision_executor.py index afb10be..e908e29 100644 --- a/sentience/vision_executor.py +++ b/sentience/vision_executor.py @@ -4,7 +4,6 @@ from dataclasses import dataclass from typing import Any, Literal - VisionActionKind = Literal["click_xy", "click_rect", "press", "type", "finish"] @@ -45,7 +44,12 @@ def parse_vision_executor_action(text: str) -> VisionExecutorAction: ): return VisionExecutorAction( "click_rect", - {"x": float(m.group(1)), "y": float(m.group(2)), "w": float(m.group(3)), "h": float(m.group(4))}, + { + "x": float(m.group(1)), + "y": float(m.group(2)), + "w": float(m.group(3)), + "h": float(m.group(4)), + }, ) raise ValueError(f"unrecognized vision action: {t[:200]}") @@ -78,4 +82,3 @@ async def execute_vision_executor_action( if action.kind == "finish": return raise ValueError(f"unknown vision action kind: {action.kind}") - From 67729e476c412067c9e9816b632139757081ec1f Mon Sep 17 00:00:00 2001 From: SentienceDEV Date: Thu, 22 Jan 2026 15:39:27 -0800 Subject: [PATCH 11/11] stop rewriting assert_ --- .github/workflows/test.yml | 57 ++------------------------------------ 1 file changed, 2 insertions(+), 55 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 219fe8a..4734810 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -40,61 +40,8 @@ jobs: # Also clean .pyc files in sentience package specifically find sentience -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || python -c "import pathlib, shutil; [shutil.rmtree(p) for p in pathlib.Path('sentience').rglob('__pycache__') if p.is_dir()]" || true find sentience -name "*.pyc" -delete 2>/dev/null || python -c "import pathlib; [p.unlink() for p in pathlib.Path('sentience').rglob('*.pyc')]" || true - # CRITICAL: Fix assertTrue bug if it exists in source (shouldn't happen, but safety check) - python << 'PYEOF' - import re - import os - import sys - - # Set UTF-8 encoding for Windows compatibility - if sys.platform == 'win32': - import io - sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') - sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace') - - file_path = 'sentience/agent_runtime.py' - print(f'=== Auto-fix check for {file_path} ===') - try: - if not os.path.exists(file_path): - print(f'ERROR: {file_path} not found!') - sys.exit(1) - - with open(file_path, 'r', encoding='utf-8') as f: - content = f.read() - - if 'self.assertTrue(' in content: - print('WARNING: Found self.assertTrue( in source file! Auto-fixing...') - # Count occurrences - count = len(re.findall(r'self\.assertTrue\s*\(', content)) - print(f'Found {count} occurrence(s) of self.assertTrue(') - - # Replace all occurrences - new_content = re.sub(r'self\.assertTrue\s*\(', 'self.assert_(', content) - - # Write back - with open(file_path, 'w', encoding='utf-8') as f: - f.write(new_content) - - # Verify the fix - with open(file_path, 'r', encoding='utf-8') as f: - verify_content = f.read() - if 'self.assertTrue(' in verify_content: - print('ERROR: Auto-fix failed! File still contains self.assertTrue(') - sys.exit(1) - else: - print('OK: Auto-fixed: Replaced self.assertTrue( with self.assert_(') - print('OK: Verified: File no longer contains self.assertTrue(') - else: - print('OK: Source file is correct (uses self.assert_())') - except Exception as e: - print(f'ERROR in auto-fix: {e}') - import traceback - traceback.print_exc() - sys.exit(1) - PYEOF - # Verify source file is fixed before installation - echo "=== Verifying source file after auto-fix ===" - python -c "import sys; import io; sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') if sys.platform == 'win32' else sys.stdout; content = open('sentience/agent_runtime.py', 'r', encoding='utf-8').read(); assert 'self.assertTrue(' not in content, 'Source file still has self.assertTrue( after auto-fix!'; print('OK: Source file verified: uses self.assert_()')" + # Ensure source uses assert_ (no auto-rewrite). + python -c "import sys; import io; sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') if sys.platform == 'win32' else sys.stdout; content = open('sentience/agent_runtime.py', 'r', encoding='utf-8').read(); assert 'self.assertTrue(' not in content, 'Source file still has self.assertTrue('; print('OK: Source file verified: uses self.assert_()')" # Force reinstall to ensure latest code pip install --no-cache-dir --force-reinstall -e ".[dev]"