|
| 1 | +""" |
| 2 | +Example: Agent Runtime with browser-use Integration |
| 3 | +
|
| 4 | +Demonstrates how to use AgentRuntime with browser-use library via BrowserBackendV0 protocol. |
| 5 | +This pattern enables framework-agnostic browser integration for agent verification loops. |
| 6 | +
|
| 7 | +Key features: |
| 8 | +- BrowserUseAdapter: Wraps browser-use BrowserSession into CDPBackendV0 |
| 9 | +- BrowserBackendV0 protocol: Minimal interface for browser operations |
| 10 | +- Direct AgentRuntime construction: No need for from_sentience_browser factory |
| 11 | +
|
| 12 | +Requirements: |
| 13 | +- browser-use library: pip install browser-use |
| 14 | +- SENTIENCE_API_KEY (optional) - enables Pro tier Gateway refinement |
| 15 | +
|
| 16 | +Usage: |
| 17 | + python examples/agent_runtime_browser_use.py |
| 18 | +""" |
| 19 | + |
| 20 | +import asyncio |
| 21 | +import os |
| 22 | + |
| 23 | +from sentience import get_extension_dir |
| 24 | +from sentience.agent_runtime import AgentRuntime |
| 25 | +from sentience.backends import BrowserUseAdapter |
| 26 | +from sentience.tracing import JsonlTraceSink, Tracer |
| 27 | +from sentience.verification import ( |
| 28 | + all_of, |
| 29 | + exists, |
| 30 | + not_exists, |
| 31 | + url_contains, |
| 32 | + url_matches, |
| 33 | +) |
| 34 | + |
| 35 | +# browser-use imports (requires: pip install browser-use) |
| 36 | +try: |
| 37 | + from browser_use import BrowserProfile, BrowserSession |
| 38 | +except ImportError: |
| 39 | + print("Error: browser-use library not installed.") |
| 40 | + print("Install with: pip install browser-use") |
| 41 | + exit(1) |
| 42 | + |
| 43 | + |
| 44 | +async def main(): |
| 45 | + # Get API key from environment (optional - enables Pro tier features) |
| 46 | + sentience_key = os.environ.get("SENTIENCE_API_KEY") |
| 47 | + |
| 48 | + print("Starting Agent Runtime with browser-use Integration Demo\n") |
| 49 | + |
| 50 | + # 1. Create tracer for verification event emission |
| 51 | + run_id = "browser-use-demo" |
| 52 | + sink = JsonlTraceSink(f"traces/{run_id}.jsonl") |
| 53 | + tracer = Tracer(run_id=run_id, sink=sink) |
| 54 | + print(f"Run ID: {run_id}\n") |
| 55 | + |
| 56 | + # 2. Create browser-use session with Sentience extension loaded |
| 57 | + # The extension is required for snapshot() to work |
| 58 | + extension_dir = get_extension_dir() |
| 59 | + profile = BrowserProfile( |
| 60 | + args=[f"--load-extension={extension_dir}"], |
| 61 | + headless=False, |
| 62 | + ) |
| 63 | + session = BrowserSession(browser_profile=profile) |
| 64 | + await session.start() |
| 65 | + |
| 66 | + try: |
| 67 | + # 3. Create BrowserBackendV0 using BrowserUseAdapter |
| 68 | + # This wraps the browser-use session into the standard backend protocol |
| 69 | + adapter = BrowserUseAdapter(session) |
| 70 | + backend = await adapter.create_backend() |
| 71 | + print("Created CDPBackendV0 from browser-use session\n") |
| 72 | + |
| 73 | + # 4. Create AgentRuntime directly with backend |
| 74 | + # For Pro tier, pass sentience_api_key for Gateway element refinement |
| 75 | + runtime = AgentRuntime( |
| 76 | + backend=backend, |
| 77 | + tracer=tracer, |
| 78 | + sentience_api_key=sentience_key, # Optional: enables Pro tier |
| 79 | + ) |
| 80 | + |
| 81 | + # 5. Navigate using browser-use |
| 82 | + page = await session.get_current_page() |
| 83 | + print("Navigating to example.com...\n") |
| 84 | + await page.goto("https://example.com") |
| 85 | + await page.wait_for_load_state("networkidle") |
| 86 | + |
| 87 | + # 6. Begin a verification step |
| 88 | + runtime.begin_step("Verify page loaded correctly") |
| 89 | + |
| 90 | + # 7. Take a snapshot (uses Sentience extension via backend.eval()) |
| 91 | + snapshot = await runtime.snapshot() |
| 92 | + print(f"Snapshot taken: {len(snapshot.elements)} elements found\n") |
| 93 | + |
| 94 | + # 8. Run assertions against current state |
| 95 | + print("Running assertions:\n") |
| 96 | + |
| 97 | + # URL assertions |
| 98 | + url_ok = runtime.assert_(url_contains("example.com"), "on_example_domain") |
| 99 | + print(f" [{'PASS' if url_ok else 'FAIL'}] on_example_domain") |
| 100 | + |
| 101 | + url_match = runtime.assert_(url_matches(r"https://.*example\.com"), "url_is_https") |
| 102 | + print(f" [{'PASS' if url_match else 'FAIL'}] url_is_https") |
| 103 | + |
| 104 | + # Element assertions |
| 105 | + has_heading = runtime.assert_(exists("role=heading"), "has_heading") |
| 106 | + print(f" [{'PASS' if has_heading else 'FAIL'}] has_heading") |
| 107 | + |
| 108 | + no_error = runtime.assert_(not_exists("text~'Error'"), "no_error_message") |
| 109 | + print(f" [{'PASS' if no_error else 'FAIL'}] no_error_message") |
| 110 | + |
| 111 | + # Combined assertion with all_of |
| 112 | + page_ready = runtime.assert_( |
| 113 | + all_of(url_contains("example"), exists("role=link")), |
| 114 | + "page_fully_ready", |
| 115 | + ) |
| 116 | + print(f" [{'PASS' if page_ready else 'FAIL'}] page_fully_ready") |
| 117 | + |
| 118 | + # 9. Check if task is done (required assertion) |
| 119 | + task_complete = runtime.assert_done( |
| 120 | + exists("text~'Example Domain'"), |
| 121 | + "reached_example_page", |
| 122 | + ) |
| 123 | + print(f"\n [{'DONE' if task_complete else 'NOT DONE'}] reached_example_page") |
| 124 | + |
| 125 | + # 10. Get accumulated assertions for step_end event |
| 126 | + assertions_data = runtime.get_assertions_for_step_end() |
| 127 | + print(f"\nTotal assertions: {len(assertions_data['assertions'])}") |
| 128 | + print(f"Task done: {assertions_data.get('task_done', False)}") |
| 129 | + |
| 130 | + # 11. Check overall status |
| 131 | + print("\nVerification Summary:") |
| 132 | + print(f" All passed: {runtime.all_assertions_passed()}") |
| 133 | + print(f" Required passed: {runtime.required_assertions_passed()}") |
| 134 | + print(f" Task complete: {runtime.is_task_done}") |
| 135 | + |
| 136 | + finally: |
| 137 | + # Close browser-use session |
| 138 | + await session.close() |
| 139 | + |
| 140 | + # Close tracer |
| 141 | + print("\nClosing tracer...") |
| 142 | + tracer.close() |
| 143 | + print(f"Trace saved to: traces/{run_id}.jsonl") |
| 144 | + print("Done!") |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + asyncio.run(main()) |
0 commit comments