Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

3.2 KB
Raw Blame
"""Exercise the real Browser-Use CDP hook on a disposable local page."""

import asyncio
import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]


async def main():
    sys.path.insert(0, str(ROOT / "dodaios/society/browser"))
    output = sys.stdout
    import live_runner

    events = []
    live_runner.emit = lambda kind, **data: events.append({"kind": kind, **data})
    worker = live_runner.Worker()
    read_text = Path.read_text
    port_lock_retries = 0

    def temporarily_locked(path, *args, **kwargs):
        nonlocal port_lock_retries
        if path.name == "DevToolsActivePort" and port_lock_retries < 2:
            port_lock_retries += 1
            raise PermissionError("Chromium is publishing its port file")
        return read_text(path, *args, **kwargs)

    try:
        profile = Path(sys.argv[2])
        Path.read_text = temporarily_locked
        try:
            await worker.start(
                {
                    "profile_dir": str(profile),
                    "workspace": str(profile / "workspace"),
                    "executable": sys.argv[1],
                    "allowed_domains": [],
                }
            )
        finally:
            Path.read_text = read_text
        assert port_lock_retries == 2
        assert worker.browser is None  # First pixels need no agent engine.
        await worker.ensure_browser()
        await worker.page.goto("about:blank")
        await worker.page.set_content(
            '<button style="width:200px;height:100px" '
            'onclick="window.clicked=true">Click probe</button>'
        )
        await worker.focused()
        target = next(t for t, p in worker.tabs.items() if p is worker.page)
        client = worker.browser.cdp_client
        attached = await client.send.Target.attachToTarget({"targetId": target, "flatten": True})
        worker.visual_action = True  # The real gate enables this only after approval.
        for kind in ("mouseMoved", "mousePressed", "mouseReleased"):
            await client.send.Input.dispatchMouseEvent(
                {"type": kind, "x": 40, "y": 40, "button": "left", "clickCount": 1},
                session_id=attached["sessionId"],
            )
        assert await worker.page.evaluate("window.clicked") is True
        pointers = [e for e in events if e["kind"] == "pointer"]
        assert len(pointers) == 2
        assert pointers[-1]["click_id"] == 1
        if worker.native:
            assert pointers[-1]["y"] > 40
        worker.pointer.clear()
        assert events[-1]["visible"] is False
        print(json.dumps({"clicked": True, "pointer_events": len(pointers)}), file=output)
    finally:
        worker.closed = True
        tasks = [t for t in (worker.stream, worker.state_task) if t]
        for task in tasks:
            task.cancel()
        await asyncio.gather(*tasks, return_exceptions=True)
        if worker.native:
            worker.native.close()
        if worker.browser:
            await worker.browser.stop()
        if worker.context:
            await worker.context.close()
        if worker.playwright:
            await worker.playwright.stop()


if __name__ == "__main__":
    asyncio.run(main())