""" Unix socket client — sends commands to the native host relay socket. Used by both the CLI and the public Python API. """ import json import socket import struct import uuid from typing import Any SOCKET_PATH = "/tmp/browser-cli.sock" class BrowserNotConnected(Exception): """Raised when the native host socket is not available.""" def send_command(command: str, args: dict | None = None) -> Any: """Send a command to the browser and return the response data.""" msg = { "id": str(uuid.uuid4()), "command": command, "args": args or {}, } payload = json.dumps(msg).encode("utf-8") framed = struct.pack(" bytes: raw_len = _recv_exact(sock, 4) msg_len = struct.unpack(" bytes: buf = b"" while len(buf) < n: chunk = sock.recv(n - len(buf)) if not chunk: raise ConnectionError("Socket closed before full message received") buf += chunk return buf