UNPKG

@phren/cli

Version:

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

200 lines (199 loc) • 9.34 kB
import { z } from "zod"; import { type NativeHelper } from "./native-helpers.js"; import { type Json } from "./protocol.js"; /** * Phone control of this Mac's windows: list a Mac's windows, take one still * JPEG of a window, tap, type and key into it. Off by default per computer * (`phren mac-windows on` writes mac-windows.json); every route is owner-only * and every control request is audited without its typed text. The Swift * helpers, their commands and their exit codes are in docs/phren-hook.md * ("Mac windows on a Mac"). */ /** How long a session (and its badge) lives after the last request. */ export declare const MAC_WINDOWS_IDLE_MS = 120000; /** How long requests are refused after the person presses Stop on the Mac. */ export declare const MAC_WINDOWS_STOP_MS = 60000; /** The whole indicator startup (swiftc build included). */ export declare const MAC_WINDOWS_BUILD_MS = 240000; /** How often an open session re-reads the enable file. */ export declare const MAC_WINDOWS_ENABLE_POLL_MS = 500; /** How long wininput gets to finish its event pair after SIGTERM. */ export declare const MAC_WINDOWS_TERM_GRACE_MS = 500; /** One deadline for the badge's `ready` line, counted from spawn. */ export declare const MAC_WINDOWS_STREAM_READY_MS = 5000; export type MacWindowPermissions = { screenRecording: boolean; accessibility: boolean; }; /** One grant: `granted`, or why the helper could not even say ("helper unavailable"). */ export type MacWindowPermissionState = { granted: boolean; unavailable?: string; }; export type MacWindowPermissionReport = { screenRecording: MacWindowPermissionState; accessibility: MacWindowPermissionState; }; export type MacWindow = { id: number; app: string; bundleId?: string; pid: number; title: string; frame: { x: number; y: number; width: number; height: number; }; onScreen: boolean; }; export type MacWindowsSnapshot = { enabled: boolean; permissions: MacWindowPermissions; windows: MacWindow[]; }; export type MacWindowPermission = "screen-recording" | "accessibility"; /** The keys wininput.swift knows, plus one letter or digit for ⌘-shortcuts. */ export declare const MAC_WINDOW_KEYS: readonly ["return", "escape", "tab", "delete", "forwarddelete", "space", "up", "down", "left", "right", "home", "end", "pageup", "pagedown"]; /** User-perceived characters, as the phone and wininput.swift count them. */ export declare function graphemeCount(text: string): number; /** One control request, as the phone sends it (the id is on every shape). */ export declare const macWindowAction: z.ZodDiscriminatedUnion<[z.ZodObject<{ action: z.ZodLiteral<"tap">; id: z.ZodNumber; x: z.ZodNumber; y: z.ZodNumber; }, z.core.$strip>, z.ZodObject<{ action: z.ZodLiteral<"type">; id: z.ZodNumber; text: z.ZodString; submit: z.ZodOptional<z.ZodBoolean>; }, z.core.$strip>, z.ZodObject<{ action: z.ZodLiteral<"key">; id: z.ZodNumber; key: z.ZodUnion<readonly [z.ZodEnum<{ delete: "delete"; down: "down"; end: "end"; escape: "escape"; forwarddelete: "forwarddelete"; home: "home"; left: "left"; pagedown: "pagedown"; pageup: "pageup"; return: "return"; right: "right"; space: "space"; tab: "tab"; up: "up"; }>, z.ZodString]>; modifiers: z.ZodOptional<z.ZodArray<z.ZodEnum<{ cmd: "cmd"; control: "control"; option: "option"; shift: "shift"; }>>>; }, z.core.$strip>, z.ZodObject<{ action: z.ZodLiteral<"raise">; id: z.ZodNumber; }, z.core.$strip>, z.ZodObject<{ action: z.ZodLiteral<"scroll">; id: z.ZodNumber; x: z.ZodNumber; y: z.ZodNumber; dx: z.ZodNumber; dy: z.ZodNumber; }, z.core.$strip>, z.ZodObject<{ action: z.ZodLiteral<"drag">; id: z.ZodNumber; x1: z.ZodNumber; y1: z.ZodNumber; x2: z.ZodNumber; y2: z.ZodNumber; durationMs: z.ZodNumber; }, z.core.$strip>], "action">; export type MacWindowAction = z.infer<typeof macWindowAction>; /** Whether phone control is on for this computer; off unless its file says so. */ export declare function macWindowsEnabled(): Promise<boolean>; /** Turn phone control on or off for this computer (file 0600, dir 0700). */ export declare function setMacWindowsEnabled(enabled: boolean): Promise<void>; /** macOS 14 or later on a Mac: the one check behind the capability and every route. */ export declare function macWindowsSupported(platform?: NodeJS.Platform, release?: string): boolean; /** The stream route checks this before reading its query or reserving a slot. */ export declare function macWindowStreamSupported(): boolean; export interface MacWindowsHelperCall { helper: NativeHelper; args: string[]; input?: string; timeoutMs?: number; local?: boolean; } export interface MacWindowsHelperResult { stdout: Buffer; stderr: string; code: number; } /** The winbadge child: its stdin stays open for the session, `ready` when it shows. */ export interface MacWindowsBadge { ready: Promise<void>; kill(signal: NodeJS.Signals): void; onExit(listener: (code: number | null) => void): void; } interface MacWindowsDeps { platform: () => NodeJS.Platform; release: () => string; run: (call: MacWindowsHelperCall, signal: AbortSignal) => Promise<MacWindowsHelperResult>; spawnBadge: (signal: AbortSignal) => Promise<MacWindowsBadge>; now: () => number; pollMs: () => number; /** The pre-spawn setting read (local probes skip it). */ enabled: () => Promise<boolean>; } /** Runs one helper with the session signal; local probes skip the setting gate. */ export declare function runMacWindowsHelper(call: MacWindowsHelperCall, signal: AbortSignal): Promise<MacWindowsHelperResult>; /** Replaces part of the process seam (tests need no Mac); answers a restore. */ export declare function setMacWindowsDeps(replacement: Partial<MacWindowsDeps>): () => void; /** Test seam: forget the session, badge and stop window. */ export declare function resetMacWindowsState(): void; /** The only action names the audit log ever carries. */ export declare const MAC_WINDOW_AUDIT_ACTIONS: readonly ["tap", "type", "key", "raise", "scroll", "drag", "list", "snapshot", "permissions", "stream-start", "stream-end", "session-end", "invalid"]; /** * Logs a refused request with fixed fields: one of MAC_WINDOW_AUDIT_ACTIONS, a * parseable window id and a stable error code. Called at the route boundary, so * owner-proof, body-parse, owner-only, disabled and invalid-input refusals are * audited too. Errors whose row already exists are left alone. */ export declare function macWindowsRefusal(action: unknown, id: unknown, error: unknown): Promise<void>; /** Test seam: the audit write of the most recent session end. */ export declare function macWindowsSessionEnd(): Promise<void>; /** Ends the current session and kills the badge; the Hook calls this when it stops. */ export declare function stopMacWindows(): Promise<void>; /** Both grants, with a helper that could not build or run told apart from a denied grant. */ export declare function macWindowsPermissionReport(signal?: AbortSignal, local?: boolean): Promise<MacWindowPermissionReport>; /** The CLI status probes: local and read-only, so the setting and a session do not gate them. */ export declare function macWindowsLocalPermissionReport(): Promise<MacWindowPermissionReport>; /** The two macOS grants the window helpers need. */ export declare function macWindowPermissions(signal?: AbortSignal): Promise<MacWindowPermissions>; /** Everything the phone asks before showing the Mac windows list. */ export declare function macWindowsList(): Promise<MacWindowsSnapshot>; /** One still JPEG of a window, longest side at most `size`, quality 30 to 90. */ export declare function macWindowsSnapshot(id: unknown, size?: unknown, quality?: unknown): Promise<Buffer>; /** Taps, types, keys or raises one window; inputs run one at a time. */ export declare function macWindowsAct(input: unknown): Promise<Json>; /** Asks macOS for one grant (it shows the system dialog) and answers both. */ export declare function macWindowsRequestPermission(permission: unknown): Promise<MacWindowPermissions>; /** The owner-only gate the route layer applies to every Mac window route. */ export declare function macWindowsOwnerOnly(owner: boolean): void; /** Why a lease ended without the phone closing it. */ export type MacWindowStreamEnd = "mac-windows-disabled" | "mac-windows-stopped" | "mac-windows-badge" | "shutdown"; /** One admitted stream: the session it pins, released by `finish`. */ export interface MacWindowStreamLease { recheckAdmission(): Promise<void>; finish(reason?: string): Promise<void>; } /** A refused stream start (owner, bad query, busy, disabled, stopped, badge, permission), never its own text. */ export declare function logMacWindowStreamRefusal(reason: string, id?: number): Promise<void>; /** Admit one live window and pin its session until the stream ends. */ export declare function beginMacWindowStream(owner: boolean, id: number, onEnd: (reason: MacWindowStreamEnd) => void, connection?: AbortSignal): Promise<MacWindowStreamLease>; export {};