@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
64 lines (63 loc) • 3.19 kB
TypeScript
import { type ChildProcess } from "node:child_process";
import type { StreamClient } from "./simulator-stream.js";
/**
* `WS /v1/mac/windows/stream?id=&codec=&tier=`: one Mac window, live, as
* ScreenCaptureKit video encoded by `wincap stream`. The helper answers each
* `PULL <tier> <reset>` with one tagged record on stdout; this side paces the
* pulls, turns each encoded sample into one binary WS message with a contiguous
* sequence number, and owns the one unacknowledged frame, the token bucket and
* the close codes. The wire, tiers, deadlines and exit codes are
* .scratch/CONTRACT-S2.md sections 1-3.
*
* Invariants: at most one PULL, one staged (encoded, waiting for tokens) sample
* and one unacknowledged frame, and only one of those at a time. No PULL while
* anything is staged or unacknowledged, and never more than once per tier frame
* interval. A frame is only replaced by a new epoch's sync frame, never by a
* dependent delta.
*/
export type MacWindowCodec = "h264" | "hevc";
export interface MacWindowTier {
size: number;
fps: number;
bitsPerSecond: number;
}
/** Longest side in pixels, capture fps and encoder/wire target. */
export declare const MAC_WINDOW_TIERS: readonly MacWindowTier[];
export declare const MAC_WINDOW_MAX_TIER: number;
/** One Hook carries one live window at a time. */
export declare const MAX_MAC_WINDOW_STREAMS = 1;
/** One binary WS message: the 24-byte header, parameter sets and the access unit. */
export declare const MAX_MESSAGE_BYTES = 24576;
/** One helper stdout record: its tag byte plus at most one wire message. */
export declare const MAX_RECORD_BYTES: number;
export declare const HEADER_BYTES = 24;
/** Client controls are text and this short. */
export declare const MAX_CONTROL_BYTES = 256;
/** Video bursts may use the whole bucket, then refill at the tier's bytes/s. */
export declare const BUCKET_CAPACITY_BYTES = 24576;
/** No tier may refill faster than this, which bounds 3 s of video to 168576 B. */
export declare const BUCKET_CEILING_BYTES_PER_SECOND = 48000;
export declare const FIRST_SEND_DEADLINE_MS = 12000;
export declare const PULL_DEADLINE_MS = 5000;
export declare const ACK_DEADLINE_MS = 4000;
export declare const STOP_GRACE_MS = 250;
export declare const SIGKILL_AFTER_MS = 2000;
export interface MacWindowStreamOptions {
id: number;
codec: MacWindowCodec;
tier: number;
}
/** The stream query, validated before reserving a slot or starting a helper. */
export declare function streamQuery(query: URLSearchParams): MacWindowStreamOptions;
/** The helper and the clock, replaced in tests. */
export interface MacWindowStreamDeps {
helper(): Promise<string>;
spawn(binary: string, args: string[]): ChildProcess;
/** Monotonic milliseconds. */
now(): number;
}
/** Streams running now, for tests and health. */
export declare function activeMacWindowStreams(): number;
/** Hook shutdown: stop every live stream (1001) before the sockets are torn down. */
export declare function closeMacWindowStreams(): void;
export declare function streamMacWindow(client: StreamClient, query: URLSearchParams, owner: boolean, deps?: MacWindowStreamDeps): Promise<void>;