|
| 1 | +"""Live driver that feeds ``ComputerUseRecorder`` from a real browser (post-v5.0; ADR-0034). |
| 2 | +
|
| 3 | +M31 shipped the transport-free core (``ActionEvent`` / recorder / replay). ``PlaywrightDriver`` is |
| 4 | +the live transport — the analog of the MCP stdio adapter: it wraps a Playwright ``Page``, performs |
| 5 | +each action on the real page, hashes the screen *before* and *after*, and records an |
| 6 | +``ActionEvent``. The recording then replays deterministically offline via |
| 7 | +``ComputerUseReplayServer`` — flagging any (action, screen) it never saw. |
| 8 | +
|
| 9 | +The driver is **duck-typed** over the Page: it never imports ``playwright`` and only calls the |
| 10 | +methods it needs (``screenshot``, ``goto``, ``click``, ``fill``), so a real Playwright page drives |
| 11 | +it in production and a fake page drives it in tests — no browser needed in CI. ``playwright`` is an |
| 12 | +optional extra (``volo-computeruse[playwright]``). |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import Any, Protocol |
| 18 | + |
| 19 | +from volo_computeruse.events import ActionEvent, screenshot_hash |
| 20 | +from volo_computeruse.recorder import ComputerUseRecorder |
| 21 | + |
| 22 | + |
| 23 | +class Page(Protocol): |
| 24 | + """The slice of the Playwright sync ``Page`` API the driver uses.""" |
| 25 | + |
| 26 | + def screenshot(self) -> bytes: ... |
| 27 | + def goto(self, url: str) -> Any: ... |
| 28 | + def click(self, selector: str) -> Any: ... |
| 29 | + def fill(self, selector: str, value: str) -> Any: ... |
| 30 | + |
| 31 | + |
| 32 | +class PlaywrightDriver: |
| 33 | + """Drive a browser page and record every action as an ``ActionEvent``.""" |
| 34 | + |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + page: Page, |
| 38 | + *, |
| 39 | + recorder: ComputerUseRecorder | None = None, |
| 40 | + session_name: str = "playwright", |
| 41 | + ) -> None: |
| 42 | + self._page = page |
| 43 | + self.recorder = recorder or ComputerUseRecorder(session_name=session_name) |
| 44 | + |
| 45 | + def _screen(self) -> str: |
| 46 | + # Hash the current screenshot; a DOM serialization would work identically. |
| 47 | + return screenshot_hash(self._page.screenshot()) |
| 48 | + |
| 49 | + def _record( |
| 50 | + self, kind: str, *, target: str, value: str, before: str, result: dict[str, Any] |
| 51 | + ) -> str: |
| 52 | + after = self._screen() |
| 53 | + self.recorder.record( |
| 54 | + ActionEvent(kind=kind, target=target, value=value, screen=before), |
| 55 | + result=result, |
| 56 | + screen_after=after, |
| 57 | + ) |
| 58 | + return after |
| 59 | + |
| 60 | + def navigate(self, url: str) -> str: |
| 61 | + """Go to ``url`` and record it; returns the resulting screen hash.""" |
| 62 | + before = self._screen() |
| 63 | + self._page.goto(url) |
| 64 | + return self._record("navigate", target="", value=url, before=before, result={"ok": True}) |
| 65 | + |
| 66 | + def click(self, selector: str) -> str: |
| 67 | + before = self._screen() |
| 68 | + self._page.click(selector) |
| 69 | + return self._record("click", target=selector, value="", before=before, result={"ok": True}) |
| 70 | + |
| 71 | + def type(self, selector: str, text: str) -> str: |
| 72 | + before = self._screen() |
| 73 | + self._page.fill(selector, text) |
| 74 | + return self._record("type", target=selector, value=text, before=before, result={"ok": True}) |
| 75 | + |
| 76 | + @property |
| 77 | + def recording(self): # type: ignore[no-untyped-def] |
| 78 | + return self.recorder.recording |
| 79 | + |
| 80 | + def save(self, path: Any = None) -> Any: |
| 81 | + return self.recorder.save(path) |
0 commit comments