#!/usr/bin/env python3 """Exercise the framed host protocol without granting owner fields to a model.""" import json import struct import subprocess import sys def read_exact(stream, size): data = bytearray() while len(data) < size: part = stream.read(size - len(data)) if not part: raise EOFError("worker closed the pipe") data.extend(part) return bytes(data) def main(): command = sys.argv[1:] or ["./bin/cfbrowser", "serve"] process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) try: request = {"version": 1, "id": 1, "method": "tools.call", "params": { "owner": {"environmentId": "local", "threadId": "t1", "runId": "r1"}, "name": "cf_browser_session", "arguments": {"operation": "list"}}} payload = json.dumps(request).encode("utf-8") process.stdin.write(struct.pack(">I", len(payload)) + payload) process.stdin.flush() length = struct.unpack(">I", read_exact(process.stdout, 4))[0] if not 0 < length <= 512 * 1024: raise ValueError("invalid response length") print(json.loads(read_exact(process.stdout, length))) finally: process.stdin.close() try: process.wait(timeout=5) except subprocess.TimeoutExpired: process.kill() process.wait() if __name__ == "__main__": main()