UNPKG

@phren/cli

Version:

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

205 lines (204 loc) • 9.25 kB
import { z } from "zod"; import { type Json, type Target } from "./protocol.js"; import type { DeliveryStatus } from "./prompt-deliveries.js"; import { type CiFailure } from "./ci-failure-contract.js"; export declare const queuedHandOffSchema: z.ZodObject<{ target: z.ZodObject<{ server: z.ZodString; workspace: z.ZodString; tab: z.ZodString; pane: z.ZodString; source: z.ZodEnum<{ claude: "claude"; codex: "codex"; copilot: "copilot"; opencode: "opencode"; phren: "phren"; }>; session: z.ZodUnion<readonly [z.ZodString, z.ZodString]>; }, z.core.$strip>; text: z.ZodString; deliveryId: z.ZodOptional<z.ZodString>; origin: z.ZodOptional<z.ZodObject<{ server: z.ZodString; workspace: z.ZodString; tab: z.ZodString; pane: z.ZodString; source: z.ZodEnum<{ claude: "claude"; codex: "codex"; copilot: "copilot"; opencode: "opencode"; phren: "phren"; }>; session: z.ZodUnion<readonly [z.ZodString, z.ZodString]>; }, z.core.$strip>>; originComputer: z.ZodOptional<z.ZodString>; }, z.core.$strict>; declare const rowSchema: z.ZodObject<{ target: z.ZodObject<{ server: z.ZodString; workspace: z.ZodString; tab: z.ZodString; pane: z.ZodString; source: z.ZodEnum<{ claude: "claude"; codex: "codex"; copilot: "copilot"; opencode: "opencode"; phren: "phren"; }>; session: z.ZodUnion<readonly [z.ZodString, z.ZodString]>; }, z.core.$strip>; text: z.ZodString; origin: z.ZodOptional<z.ZodObject<{ server: z.ZodString; workspace: z.ZodString; tab: z.ZodString; pane: z.ZodString; source: z.ZodEnum<{ claude: "claude"; codex: "codex"; copilot: "copilot"; opencode: "opencode"; phren: "phren"; }>; session: z.ZodUnion<readonly [z.ZodString, z.ZodString]>; }, z.core.$strip>>; originComputer: z.ZodOptional<z.ZodString>; deliveryId: z.ZodString; terminal: z.ZodString; state: z.ZodEnum<{ attempting: "attempting"; delivered: "delivered"; failed: "failed"; queued: "queued"; uncertain: "uncertain"; }>; createdAt: z.ZodString; updatedAt: z.ZodString; error: z.ZodOptional<z.ZodString>; notified: z.ZodOptional<z.ZodBoolean>; ciFailure: z.ZodOptional<z.ZodObject<{ repo: z.ZodString; branch: z.ZodOptional<z.ZodString>; commit: z.ZodOptional<z.ZodString>; snapshotId: z.ZodOptional<z.ZodString>; step: z.ZodString; failedTests: z.ZodArray<z.ZodString>; logTail: z.ZodString; sessionId: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodString]>>; eventId: z.ZodOptional<z.ZodString>; }, z.core.$strict>>; ciContentHash: z.ZodOptional<z.ZodString>; }, z.core.$strict>; type Row = z.infer<typeof rowSchema>; export interface QueueOptions { validate: (target: Target) => Promise<Json>; send: (target: Target, text: string, deliveryId: string, typing: () => Promise<void>, sessionTerminal?: string) => Promise<Json>; forward?: (target: Target, text: string, deliveryId: string) => Promise<{ delivered: boolean; } | undefined>; notify?: (target: Target, text: string, deliveryId: string, computer: string) => Promise<Json>; /** The conversation the target's pane shows now, and its terminal. */ current?: (target: Target) => Promise<{ session: string; terminal: unknown; } | undefined>; /** What became of the message typed under `deliveryId` (prompt-deliveries.ts): an agent whose * UserPromptSubmit comes after the send returned settles it there, later. */ delivery?: (target: Target, deliveryId: string) => DeliveryStatus; /** Whether text typed into the target's pane is still waiting to be taken. Nothing more is typed * there until it is: a second paste would land in the same input line, after the first. */ inFlight?: (target: Target) => boolean; /** Whether the message typed under `deliveryId` may still be taken: held, and not past the time a pane * that sends no hooks is given (prompt-deliveries.ts `held`). */ held?: (target: Target, deliveryId: string) => boolean; /** Whether the target's agent takes a typed prompt now (session-activity.ts `takesPrompt`): idle or * done, or a Claude turn that ended while its background agents run on, which Herdr reads working. * Without it, only an idle or done pane is typed into. */ ready?: (target: Target, pane: Json) => Promise<boolean>; now?: () => number; root?: string; } /** Settled rows (and notice tombstones) are kept this long, so a late retry of * an old delivery id still replays instead of typing again. */ export declare const HAND_OFF_RETENTION_MS: number; /** At most this many rows stay on disk; the oldest settled rows go first. */ export declare const HAND_OFF_MAX_ROWS = 512; /** A durable outbox on the receiving Hook. Persist 'attempting' before input, * so a crash in the input/ack gap recovers as uncertain, never as a retry. * Tombstones are retained for HAND_OFF_RETENTION_MS, so a retried delivery id * replays instead of typing a second time; a queued or attempting row is never * pruned. The sweep runs on enqueue and at most daily from tick(). */ export declare class HandOffQueue { private options; private pending; private root; private now; private lastPrune; private ticking?; constructor(options: QueueOptions); private serial; private file; private save; private read; /** An uncertain row takes the outcome its typed message came to: delivered once the agent's * UserPromptSubmit came, failed (with why) once the Hook knows the agent will not take it. A * failed row still turns delivered when the agent takes the message late. */ private follow; /** Uncertain only while the agent may still take it: its typed message is held, not yet settled, and * in a pane whose hooks will settle it or typed only moments ago. Past that the sender hears "uncertain". */ private stillHeld; private rows; /** Drops settled rows past retention, then the oldest settled rows over the * cap (those still owing their sender a notice last). Returns what is kept. */ private prune; private reply; enqueue(input: unknown): Promise<Json>; enqueueCiFailure(ciFailure: CiFailure, deliveryId: string, text: string, ciContentHash: string, target: Target, terminal: string): Promise<Json>; ciFailures(target: Target): Promise<Json>; private enqueueNew; /** * Runs `action` only while the message `id` for `target` has not been typed: its row is still queued, * or not made yet. Under the queue's lock, so a sweep cannot type it between the check and the action * (a move's "stop and hand off now" sends Escape this way: before the request is typed, never into the * hand-off it asked for). Otherwise answers the row's state. `action` gets the row (when made): the * terminal it was queued for, and the conversation it is for now (it follows one that replaced the first * in the same terminal). */ whileQueued(id: string, target: Target, action: (row?: { terminal: string; session?: string; }) => Promise<void>): Promise<{ ran: true; } | { ran: false; state: Row["state"]; }>; /** * Takes back the message `id` for `target` while it is still queued, never typed: its row fails with * `reason`, and no sweep types it later (a move whose hand-off wait ended no longer wants its request in the * old agent). Answers the row's state after: `failed` once withdrawn, else what it already came to * (`delivered`: the agent took it), or undefined when there is no such row. */ withdraw(id: string, target: Target, reason: string): Promise<Row["state"] | undefined>; status(id: string, target: Target): Promise<Json>; /** Validates the row's target. When the pane's conversation was replaced * in the same terminal (a Codex thread change, a Claude /clear), the row * follows it: a queued message is for the pane, not for a conversation * that is no longer there. Saved by the caller's next state change. */ private validate; private attempt; /** Coalesced: the Hook calls this every 5 s whether or not the last tick * finished, and queuing each behind the lock would hold every enqueue and * status behind a backlog that grows faster than a slow tick drains it. */ tick(): Promise<void>; private sweep; private noticeId; /** Hold enqueues and delivery while the Hook validates and closes a worker. * A new hand-off cannot be accepted between the pending check and closure. */ whenNoPending(target: Target, close: () => Promise<Json>): Promise<Json>; hasPending(target: Target): Promise<boolean>; } export {};