UNPKG

@phren/cli

Version:

Knowledge layer for AI agents - CLI, MCP server, and data layer

114 lines (113 loc) • 5.39 kB
import type { Readable } from "node:stream"; import { type Json } from "./protocol.js"; export { FrameReader, MAX_FRAME_BYTES } from "./frame-reader.js"; /** * `WS /v1/simulators/stream?udid=&fps=&size=&quality=`: one booted * simulator's screen, live. The native helper (native/simcast.swift) reads the * device's framebuffer through CoreSimulator, as `simctl io` does, and writes * each changed frame as a length-prefixed JPEG; this side forwards them. * * Why JPEG frames from the framebuffer, not the alternatives: * - `simctl io screenshot` took about 1.1 s a shot (Xcode 27.1, M1 Max), * which is today's 1 fps poll and no more. * - `simctl io recordVideo` writes a QuickTime movie that is only readable * once finalized, so it cannot feed a live socket. * - ScreenCaptureKit on the Simulator window needs a Screen Recording grant * (which macOS re-asks for monthly), captures the window chrome, and stops * when the window is minimized; the framebuffer needs no grant. * - H.264 halves the bytes but ties frames together: a dropped frame corrupts * the picture until the next keyframe, so "always the latest frame" would * need keyframe requests across the socket. Every JPEG stands alone, so a * frame the phone can't take yet is simply replaced by the next one. * - The helper sends only frames whose framebuffer changed (its IOSurface * seed moved): a still screen costs nothing on the wire. * * Frames: a text `{ type: "start", format: "jpeg", fps, size, quality, ack }`, * then one binary message per frame, each a whole JPEG. A phone that asks * with `ack=1` sends `{ type: "ack" }` for each frame it receives, and at * most two frames are unacknowledged: the SSH channel and TCP buffers between * here and the phone then never hold more than that, so what the phone shows * is at most two frames old however slow the link. Without it, a frame is * done once written to the gateway's socket. When the stream * ends, the socket closes with the reason (1008 for a bad request, 1011 when * capture failed); the phone then falls back to `/v1/simulators/screenshot`. * Closing the socket stops the helper. Taps and keys stay on * `/v1/simulators/action`. */ /** The wire budget: about 24 Mbit/s, so animations at full rate on Wi-Fi, and * the phone asks for a smaller `size` on a slower link. */ export declare const STREAM_BYTES_PER_SECOND: number; /** Each stream runs its own helper; a few phones may watch at once. */ export declare const MAX_STREAMS = 4; export interface StreamOptions { udid: string; fps: number; size: number; quality: number; ack: boolean; } /** Frames in flight when the phone acknowledges them: one decoding on the * phone while the next crosses the network. */ export declare const ACK_WINDOW = 2; /** The query, validated before anything runs. */ export declare function streamOptions(query: URLSearchParams): StreamOptions; /** Sends the newest frame only. At most `window` frames are in flight (until * written, or until acknowledged when `acked`); frames that arrive meanwhile * replace the one waiting, so a slow link sees fewer frames, never older * ones. A byte budget, refilled continuously with one second of burst, bounds * the bitrate the same way. */ export declare class LatestFrame { private send; private options; private now; private waiting?; private inFlight; private tokens; private refilled; private timer?; private closed; sent: number; dropped: number; constructor(send: (frame: Buffer, done: (error?: Error) => void) => void, options?: { bytesPerSecond?: number; window?: number; acked?: boolean; }, now?: () => number); offer(frame: Buffer): void; /** The phone has a frame; an acknowledgement it owes nothing for is ignored. */ ack(): void; /** Discard a previous page's queued frame without forgiving its outstanding acknowledgements. */ discardWaiting(): void; close(): void; private get bytesPerSecond(); private pump; } /** The phone's socket, or a test double. */ export interface StreamClient { readonly readyState: number; send(data: string | Buffer, options: { binary: boolean; }, done?: (error?: Error) => void): void; close(code?: number, reason?: string): void; once(event: "close", listener: (code: number) => void): unknown; once(event: "error", listener: () => void): unknown; on(event: "message", listener: (data: Buffer, isBinary: boolean) => void): unknown; } /** The capture process, or a test double. */ export interface CaptureProcess { readonly stdout: Readable | null; readonly stderr: Readable | null; kill(signal?: NodeJS.Signals): boolean; once(event: "exit", listener: (code: number | null, signal: NodeJS.Signals | null) => void): unknown; once(event: "error", listener: (error: Error) => void): unknown; } export interface StreamDeps { booted: () => Promise<Json[]>; helper: () => Promise<string>; spawn: (binary: string, args: string[]) => CaptureProcess; platform: () => NodeJS.Platform; bytesPerSecond?: number; } /** Streams running now, for tests and health. */ export declare function activeSimulatorStreams(): number; export declare function streamSimulator(client: StreamClient, query: URLSearchParams, deps?: StreamDeps): Promise<void>;