@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
199 lines (198 loc) • 9.2 kB
TypeScript
import { z } from "zod";
import { type RequestKind } from "./approval-summary.js";
import type { SudoRequestView } from "./sudo.js";
/** A phone registered through the phren push relay: the relay knows where to
* deliver, and `key` (32 bytes, base64url) encrypts what the notification says
* so only the phone reads it. The phone made the key and sent it over SSH. */
declare const relaySchema: z.ZodObject<{
url: z.ZodString;
relayId: z.ZodString;
secret: z.ZodString;
key: z.ZodString;
}, z.core.$strip>;
declare const storedDeviceSchema: z.ZodObject<{
deviceID: z.ZodString;
hostID: z.ZodString;
token: z.ZodOptional<z.ZodString>;
relay: z.ZodOptional<z.ZodObject<{
url: z.ZodString;
relayId: z.ZodString;
secret: z.ZodString;
key: z.ZodString;
}, z.core.$strip>>;
environment: z.ZodEnum<{
development: "development";
production: "production";
}>;
kinds: z.ZodDefault<z.ZodArray<z.ZodEnum<{
approval: "approval";
ownerInbox: "ownerInbox";
scheduleBlocked: "scheduleBlocked";
scheduleFailed: "scheduleFailed";
scheduleFinished: "scheduleFinished";
scheduleStarted: "scheduleStarted";
}>>>;
ownerInbox: z.ZodOptional<z.ZodBoolean>;
}, z.core.$strip>;
declare function normalizeDevice(device: z.infer<typeof storedDeviceSchema>): {
deviceID: string;
hostID: string;
token?: string | undefined;
relay?: {
url: string;
relayId: string;
secret: string;
key: string;
} | undefined;
environment: "development" | "production";
kinds: ("approval" | "scheduleBlocked" | "scheduleFailed" | "scheduleFinished" | "scheduleStarted")[];
ownerInbox: true | undefined;
};
export type PushDevice = ReturnType<typeof normalizeDevice>;
export interface ApprovalPush {
binding: string;
provider: string;
question: boolean;
expiresAt: string;
project?: string;
computer?: string;
request?: string;
requestKind?: RequestKind;
}
export interface MachineHealthPush {
computer: string;
id: string;
title: string;
detail: string;
fix: string;
}
export interface FanoutBlockedPush {
job: string;
label: string;
provider: string;
reason: string;
}
export type SchedulePushKind = "scheduleStarted" | "scheduleFinished" | "scheduleFailed" | "scheduleBlocked";
export interface SchedulePush {
kind: SchedulePushKind;
scheduleId: string;
project: string;
name: string;
computer: string;
runId: string;
status: "running" | "finished" | "needs-you" | "failed" | "blocked";
reason?: string;
route?: string;
}
export interface SchedulePushResult {
notified: boolean;
reason?: string;
}
/** One open owner-inbox row owned by this Hook. The ledger freezes `expiresAt`
* as the APNs expiration, and retries reuse collapse ID `inbox-<id>`. */
export interface OwnerInboxPush {
id: string;
title: string;
expiresAt: string;
}
export declare function scheduleCollapseId(kind: SchedulePushKind, runId: string): string;
export declare function approvalPushPayload(value: ApprovalPush, host?: string): Record<string, unknown>;
/** A sudo request waiting for its password. The command is what the owner
* approves; the password itself is typed only in the app, never in a
* notification action. */
export declare function sudoPushPayload(value: SudoRequestView, host?: string): Record<string, unknown>;
export declare function fanoutBlockedPushPayload(value: FanoutBlockedPush): Record<string, unknown>;
export declare function machineHealthPushPayload(value: MachineHealthPush, host?: string): Record<string, unknown>;
export declare function schedulePushPayload(value: SchedulePush): Record<string, unknown>;
/** The alert body is the item title, at most 500 UTF-8 bytes, cut between
* characters so a phone never shows a replacement glyph. */
export declare function ownerInboxPushBody(value: string): string;
/** A new open owner-inbox row. The phone-local host and the item id are the
* only routing data: the alert opens Inbox, it never carries commands. */
export declare function ownerInboxPushPayload(value: OwnerInboxPush, host: string): Record<string, unknown>;
/** What `phren bridge doctor` prints when the Hook has no APNs sender. */
export declare function apnsSetupSteps(configFile?: string): string;
/** The capability a phone reads: a Hook with its own APNs key sends direct;
* one whose phones registered through the relay sends through it. */
export declare function approvalPushCapability(status: {
configured: boolean;
direct?: boolean;
}): "direct-apns" | "relay" | undefined;
export declare function upsertPushDevice(devices: PushDevice[], value: unknown): PushDevice[];
/** The relay refuses ciphertext longer than this (it keeps the APNs payload under 4 KB). */
export declare const RELAY_MAX_CIPHERTEXT = 2800;
/** What the phone's notification extension shows, encrypted with the phone's
* key: ChaCha20-Poly1305, base64url of nonce (12) + ciphertext + tag (16), the
* layout CryptoKit's `ChaChaPoly.SealedBox(combined:)` reads. A long body is
* shortened until it fits the relay's limit. */
export declare function relayCiphertext(key: string, payload: Record<string, unknown>, nonce?: NonSharedBuffer): string;
/** Signs a relay send: base64url HMAC-SHA256 of `${timestamp}.${body}` under the phone's send secret. */
export declare function relaySignature(secret: string, timestamp: string, body: string): string;
type RelayRegistration = z.infer<typeof relaySchema>;
/** "gone" means the relay (Apple) no longer knows the phone: stop sending until it registers again. */
export type RelayResult = "sent" | "failed" | "gone";
export declare function sendThroughRelay(relay: RelayRegistration, payload: Record<string, unknown>, headers: {
expiration: string;
collapseId: string;
}, fetcher?: typeof fetch, now?: () => number): Promise<RelayResult>;
/** Direct Hook -> APNs delivery. Apple receives only a short-lived opaque
* binding; conversation identity, action id, command and SSH key stay local. */
export declare class ApprovalPushService {
private devices;
private sender?;
private readonly devicesFile;
/** Loading, registering, forgetting and dropping a gone phone each read or
* write the list and its file; one at a time, so an older write never lands
* over a newer one and a load never brings back what was just forgotten. */
private changes;
private change;
start(): Promise<void>;
/** Phones this Hook can reach: through the relay, or direct with its own key. */
private get reachable();
get available(): boolean;
get status(): {
supported: boolean;
configured: boolean;
direct: boolean;
devices: number;
relay: number;
};
register(value: unknown): Promise<void>;
/** Drop every registration; returns how many went. Registrations carry no
* SSH key, so removing a phone cannot pick out its own: all go, and the
* phones still paired register again the next time Phren renews (hourly, on open). */
forgetAll(): Promise<number>;
/** One phone: the relay when it registered through one, otherwise direct. */
private send;
/** Until when the owner is at Phren desktop (ms epoch): approval pushes wait for the desk to go quiet. */
private deskUntil;
private held;
/** Whether an approval is still unanswered; AgentHooks sets it. Unset means "assume pending". */
stillPending?: (binding: string) => boolean;
/** Phren desktop saw the owner's keyboard or mouse: hold phone alerts for `activeForMs` (at most 2 min). */
markDesk(activeForMs: number): void;
get deskActive(): boolean;
/**
* Desk first: while the owner is at the desktop, the desktop shows the
* approval and the phone stays quiet. Once the desk has been idle past its
* window and the approval is still pending, the phone gets the alert.
*/
notify(value: ApprovalPush): Promise<boolean>;
private holdForDesk;
protected notifyNow(value: ApprovalPush): Promise<boolean>;
notifySudo(value: SudoRequestView): Promise<boolean>;
/** A headless worker whose permission the plugin refused. Approval-registered
* phones already accept agent alerts; the payload carries the reason so the
* notification is actionable on its own. */
notifyFanoutBlocked(value: FanoutBlockedPush): Promise<boolean>;
/** A new high-severity machine health finding (machine-health-alerts.ts dedupes). */
notifyMachineHealth(value: MachineHealthPush): Promise<boolean>;
/** Phones that asked for Inbox alerts and can be reached now. The sweep
* asks again for every send, so a forgotten phone never gets one. */
ownerInboxPushDevices(): string[];
/** One phone only: each inbox row is offered per device, so retrying one
* phone never repeats an alert another phone already accepted. */
notifyOwnerInbox(value: OwnerInboxPush, deviceID: string): Promise<boolean>;
notifySchedule(value: SchedulePush): Promise<SchedulePushResult>;
}
export {};