#!/usr/bin/env python3 """Drive the session's browser over CDP, then put a terminal next to it. The acceptance proof for Agent Eyes: the agent searches Gunpost for an SKS, opens a few listings in their own tabs, clears the browser away, and runs cowsay in a terminal on the same screen. It is the demonstration that the agent can see and work a real desktop, and it lives here rather than in a scratch directory because it has been asked for more than once and a tool kept in scratch gets pruned. Usage, with the browser session already started and its CDP port up: hhstream session start chromium -- --ozone-platform=wayland ... \\ --remote-debugging-port=9334 --remote-debugging-address=127.0.0.1 about:blank hhstream record --seconds 80 --out proof.mp4 & ./proof-video.py Only the browser's own HTTP endpoints are used (/json/new, /json/activate) -- no websocket, no shell, no typing. The URLs are real: the search is the one the gun watch uses (`key=`, singular, which is a bug that once cost every search its filtering) and the listings are Gunpost pages the watch itself collected. """ from __future__ import annotations import json import subprocess import sys import time import urllib.parse import urllib.request CDP = "http://127.0.0.1:9334" SEARCH = "https://www.gunpost.ca/ads?key=SKS&sort=created&order=desc" LISTINGS = [ "https://www.gunpost.ca/firearms/rifles/hamilton/sks-1951-russian-excellent-rifle-762x39-100000-obo", "https://www.gunpost.ca/firearms/rifles/pembroke/1953-tula-russian-sks-762x39-crisp-markings-solid-hardwood-stock-box", "https://www.gunpost.ca/firearms/rifles/markham-york-region/chinese-type-56-sks-hush-folding-stock-original-wood-stock", ] MESSAGE = "hahaha I can see the world now" #: Absolute, because the broker is a systemd unit and a unit's PATH has no system #: profile in it: a bare `cowsay` fails inside foot with "No such file or directory". COWSAY = "/nix/store/s4hlzmy3ll1bphdsy8vjdpcw447vbr93-cowsay-3.8.4/bin/cowsay" def call(path: str, method: str = "GET") -> dict: req = urllib.request.Request(CDP + path, method=method) with urllib.request.urlopen(req, timeout=15) as response: body = response.read().decode() return json.loads(body) if body.strip() else {} def open_tab(url: str) -> str: tab = call("/json/new?" + urllib.parse.quote(url, safe=""), method="PUT") return tab["id"] def activate(tab_id: str) -> None: """Bring a tab to the front. Cosmetic: never fail the recording over it.""" try: call("/json/activate/" + tab_id) except Exception as exc: # noqa: BLE001 - a failed activation is not fatal print(f" (activate {tab_id[:8]} said: {exc})", flush=True) def hhstream(*args: str) -> str: out = subprocess.run(["hhstream", *args], capture_output=True, text=True, timeout=180) if out.returncode != 0: print(f" (hhstream {' '.join(args[:2])} -> {out.returncode}: {out.stderr.strip()[:120]})") return out.stdout.strip() def main() -> int: session = sys.argv[1] if len(sys.argv) > 1 else None print("=== beat 1: the search ===", flush=True) search_tab = open_tab(SEARCH) activate(search_tab) time.sleep(13) # long enough for Cloudflare's check to clear and results to paint print("=== beats 2-4: three listings, each in its own tab ===", flush=True) for n, url in enumerate(LISTINGS, 1): tab = open_tab(url) print(f"listing {n}: {url}", flush=True) activate(tab) time.sleep(7) print("=== beat 5: back to the results ===", flush=True) activate(search_tab) time.sleep(5) print("=== beat 6: clear the browser away ===", flush=True) if session: print(f" killed: {hhstream('session', 'kill', session)[:80]}", flush=True) time.sleep(3) print("=== beat 7: a terminal, running cowsay ===", flush=True) print( " started: " + hhstream("session", "start", "foot", "--", "-H", "-f", "monospace:size=20", COWSAY, MESSAGE)[:80], flush=True, ) time.sleep(20) return 0 if __name__ == "__main__": sys.exit(main())