@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
133 lines (132 loc) • 6.3 kB
TypeScript
import { z } from "zod";
import type { InputCategory } from "./input-classification.js";
import { type Provider } from "./protocol.js";
/** Longest final reply kept from a Stop payload, in UTF-8 bytes (the receipt's limit). */
export declare const TURN_REPLY_LIMIT = 4000;
/** Most background tasks a record keeps from one Stop, running ones first. */
export declare const STOP_TASKS_LIMIT = 256;
declare const turnRecordSchema: z.ZodObject<{
terminal: z.ZodString;
source: z.ZodEnum<{
claude: "claude";
codex: "codex";
copilot: "copilot";
opencode: "opencode";
phren: "phren";
}>;
session: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
seq: z.ZodNumber;
dispatch: z.ZodOptional<z.ZodString>;
startedAt: z.ZodOptional<z.ZodString>;
observedDirectories: z.ZodOptional<z.ZodArray<z.ZodString>>;
prompt: z.ZodOptional<z.ZodObject<{
seq: z.ZodNumber;
at: z.ZodString;
}, z.core.$strip>>;
notification: z.ZodOptional<z.ZodObject<{
seq: z.ZodNumber;
at: z.ZodString;
reason: z.ZodOptional<z.ZodString>;
}, z.core.$strip>>;
stop: z.ZodOptional<z.ZodObject<{
seq: z.ZodNumber;
at: z.ZodString;
background: z.ZodOptional<z.ZodNumber>;
reply: z.ZodOptional<z.ZodString>;
truncated: z.ZodOptional<z.ZodBoolean>;
cwd: z.ZodOptional<z.ZodString>;
tasks: z.ZodOptional<z.ZodObject<{
running: z.ZodArray<z.ZodString>;
finished: z.ZodArray<z.ZodString>;
truncated: z.ZodOptional<z.ZodBoolean>;
}, z.core.$strip>>;
}, z.core.$strip>>;
at: z.ZodString;
}, z.core.$strip>;
export type TurnRecord = z.infer<typeof turnRecordSchema>;
export interface TurnEvent {
event: string;
terminal: string;
source: Provider;
session: string;
/** SessionStart / UserPromptSubmit: the dispatch id the worker's hook named. */
dispatch?: string;
/** SessionStart only: how the session started, as the harness names it (Claude: startup, resume, clear, compact). */
start?: string;
/** UserPromptSubmit: whose input this is. Trusted turn writers omit this for owner input;
* the callback endpoint explicitly classifies automatic and unresolved submissions. */
input?: InputCategory;
/** Deferred classification of an already recorded submission, preserving
* its original order relative to Stops and intervening owner prompts. */
resolvePrompt?: {
seq: number;
at: string;
previous: number;
};
/** Stop: this Stop belongs to background activity, not to a foreground turn that is still running. */
notificationStop?: boolean;
/** Stop only: the harness's count of in-flight background tasks, when it reports one. */
background?: number;
/** Stop only: the background tasks it listed, `{id, running}` each (validated here). */
backgroundTasks?: unknown;
/** Stop only: the harness's last assistant message, when it reports one. */
reply?: string;
/** Stop only: the agent's working directory, as its hook payload names it. */
cwd?: string;
observedDirectory?: string;
at?: number;
}
export declare const turnPath: (server: string, pane: string) => string;
/** `value` cut to at most `limit` UTF-8 bytes on a character boundary. */
export declare function truncateUtf8(value: string, limit?: number): {
text: string;
truncated: boolean;
};
/** The record after one lifecycle event; undefined for events that say nothing about turns.
* Another conversation or terminal in the pane starts a fresh record. */
export declare function nextTurn(previous: TurnRecord | undefined, event: TurnEvent): TurnRecord | undefined;
export type TurnPhase = {
phase: "unprompted";
} | {
phase: "working";
since: string;
} | {
phase: "ended";
at: string;
background?: number;
reply?: string;
truncated?: boolean;
};
/** Where the recorded conversation is: no prompt yet, a prompt with no Stop
* after it, or a turn that stopped. A Stop with no recorded prompt (hooks
* installed mid-conversation) still ended a turn. */
export declare function turnPhase(record: TurnRecord): TurnPhase;
/** The background tasks of a Stop at `stoppedAt` still running: the Stop's
* count less the tasks whose finish the transcript recorded after it
* (`FinalTurn.finishedTasks`). No turn ran since that Stop, so every task
* finishing after it is one the count included; a finish does not always
* bring a new Stop (its notification can wait in an idle session's queue). */
export declare function backgroundLeft(count: number | undefined, stoppedAt: string, finishedTasks?: readonly string[]): number;
/** Record one lifecycle event for `pane`. Writes run one at a time, so a Stop
* and the next prompt arriving together keep their order. */
export declare function noteTurn(server: string, pane: string, event: TurnEvent): Promise<TurnRecord | undefined>;
export declare function readCompletedTurn(source: string, session: string): Promise<TurnRecord | undefined>;
/** Keep the last completed turn through pane replacement and the next prompt. */
export declare function rememberCompletedTurn(record: TurnRecord): Promise<TurnRecord | undefined>;
/** The recorded turns of `pane`'s latest conversation, if its hooks reported any. */
export declare function readTurn(server: string, pane: string): Promise<TurnRecord | undefined>;
export interface StopTasks {
seq: number;
at: string;
running: string[];
finished: string[];
truncated?: boolean;
}
/** The background tasks the latest Stop of conversation `session` listed, from whichever pane's record
* holds it (the child tree knows the conversation, not its pane); undefined when no Stop listed any. */
export declare function latestStopTasks(source: Provider, session: string): Promise<StopTasks | undefined>;
/** OpenCode's turns for a pane running `pids`, from the stamps phren's OpenCode
* plugin writes on session.status busy / session.idle; undefined for an older
* plugin that writes none. `terminal` is the pane's, since the PIDs are. */
export declare function opencodeTurn(pids: number[], terminal: string, session?: string): Promise<TurnRecord | undefined>;
export {};