@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
173 lines (172 loc) • 8.46 kB
JavaScript
// Claude Code's predicted next prompt: after a turn it draws a short guess at
// what the owner will type next as dim text in its empty input box. The screen
// is the only place a live pane shows it. Claude generates it in a side request
// that skips the transcript, keeps it in memory only (no config or state file),
// and no hook event carries it; `claude -p --prompt-suggestions` streams it as
// a `prompt_suggestion` message, but only in print mode, never in a pane.
import { stripTerminal } from "../terminal-text.js";
import { screenDialog } from "./pane-status.js";
import { terminalProvider } from "./terminal.js";
const MAX_SUGGESTION = 500;
// The input box sits between two rules; the upper one can carry the session
// title ("───── Docs refresh ─").
const rule = (line) => /^[─━═]{3,}/.test(line.trim());
// Dim text Claude also draws in the empty input that is not a prediction: the
// first-turn example, the queued-message hints and a teammate's name.
const PLACEHOLDERS = [/^Try ".*"$/, /^Press (?:up|Enter) to /, /^Message @.*…$/];
/**
* The suggestion on a Claude screen read with its styles, or undefined when
* there is none. Only an input line whose every character after the prompt is
* dim counts: once the owner types, their text is drawn plain. A narrow pane
* cuts a long suggestion with "…" instead of wrapping it; a cut one is not
* offered, since sending half a sentence would be worse than nothing.
*/
export function claudeSuggestion(screen) {
const text = liveInputDimText(screen);
if (!text || text.endsWith("…") || text.length > MAX_SUGGESTION || PLACEHOLDERS.some(pattern => pattern.test(text)))
return undefined;
return text;
}
/** The suggestion as drawn, a narrow pane's cut one ("…" at the end) included, for refusing a relay of what
* a screen read shows; undefined when there is none. */
export function claudeSuggestionShown(screen) {
const text = liveInputDimText(screen);
if (!text || text.length > MAX_SUGGESTION || PLACEHOLDERS.some(pattern => pattern.test(text)) || SUBAGENT_VIEW.test(text))
return undefined;
return text;
}
/** Claude Code showing a subagent's (or teammate's) view: its empty input box
* reads "Message @<agent>…". Text typed there goes to that agent, not the
* conversation, so a phone message typed into it never reaches the turn;
* Escape returns to the main view. */
// Only the bare placeholder, one agent name and nothing else: a suggestion
// that a narrow pane cut short ("Message @researcher to check…") is not one.
// Claude Code (2.1.303) cuts a name past 20 columns with its own "…" before the
// placeholder's: "Message @researcher-performa……".
export const SUBAGENT_VIEW = /^Message @[\w.-]{1,20}…?…$/;
export function claudeSubagentView(screen) {
const text = liveInputDimText(screen);
return !!text && SUBAGENT_VIEW.test(text);
}
/** Whether Claude's live input box holds no typed text: nothing after the
* prompt glyph, or only dim placeholder or suggestion text. False for typed
* text, a subagent's view, or no single-line live box on screen (typed text
* that wraps leaves no rule under the prompt line). */
export function claudeInputEmpty(screen) {
const line = liveInputLine(screen);
if (line === undefined)
return false;
if (!stripTerminal(line).replace(/^\s*❯/, "").trim())
return true;
const dim = dimText(line);
return !!dim && !SUBAGENT_VIEW.test(dim);
}
/** Exact visible typed text in an unambiguous single-line composer. Wrapped,
* collapsed-paste and dim suggestion inputs cannot prove the delivery's text. */
export function claudeInputMatches(screen, text) {
const line = liveInputLine(screen);
if (line === undefined || dimText(line) || /[\r\n]/.test(text))
return false;
// Remove only Claude's prompt and its separator space. Extra whitespace,
// including ambiguous screen padding, cannot prove an exact match.
return stripTerminal(line).replace(/^\s*❯[ \u00a0]/, "") === text;
}
/** A styled visible read distinguishes Claude's dim hints from typed text. */
export function readClaudeComposerScreen(target) {
return terminalProvider().readScreen(target.server, target.pane, {
scope: "pane", source: "visible", lines: 40, format: "ansi", timeoutMs: 1_000,
}).catch(() => undefined);
}
/** The full visible draft, when Claude displays it in an unambiguous single-line composer. */
export function claudeDraft(screen) {
return composer(screen).draft;
}
export function claudeComposerState(screen) {
return screen === undefined ? "unreadable" : composer(screen).state;
}
function composer(screen) {
const line = singleLineComposer(screen);
if (line === undefined || screenDialog("claude", screen))
return { state: "unreadable" };
// tmux captures the cursor at the end as a reverse-video blank. Preserve
// the other characters, including whitespace, for the draft fingerprint.
const text = stripTerminal(line.replace(/\x1b\[7m \x1b\[0m$/, "")).replace(/^\s*❯[ \u00a0]?/, "");
const dim = dimText(line);
if (!text || (dim && !SUBAGENT_VIEW.test(dim)))
return { state: "verified-empty" };
// A plain placeholder could be literal text typed by the owner. It cannot
// prove an empty composer, even though it also cannot prove a draft.
if (dim || PLACEHOLDERS.some(pattern => pattern.test(text.trim())))
return { state: "occupied" };
return { state: "occupied", draft: text };
}
function singleLineComposer(screen) {
const lines = screen.split(/\r?\n/);
// Start at the last complete, unindented rule. A continuation can begin
// with rule glyphs, so use the lower border to check the entire box.
for (let bottom = lines.length - 1; bottom >= 2; bottom--) {
const border = /^([─━═])\1{2,}[ \t]*$/.exec(stripTerminal(lines[bottom]));
if (!border)
continue;
const line = lines[bottom - 1];
if (!/^❯(?:[ \u00a0]|$)/.test(stripTerminal(line))
|| !stripTerminal(lines[bottom - 2]).startsWith(border[1].repeat(3)))
return undefined;
return line;
}
return undefined;
}
/** The dim text in the live input box (between its two rules), "" when the
* box holds typed text, undefined when there is no live box on screen. */
function liveInputDimText(screen) {
const line = liveInputLine(screen);
return line === undefined ? undefined : dimText(line);
}
/** The live input box's prompt line, as read with its styles. */
function liveInputLine(screen) {
const lines = screen.split("\n").map(line => line.replace(/\r$/, ""));
for (let index = lines.length - 2; index > 0; index--) {
const plain = stripTerminal(lines[index]);
if (!/^\s*❯/.test(plain))
continue;
// Only the live input box: a sent prompt in the history has no rules around it.
if (!rule(stripTerminal(lines[index - 1])) || !rule(stripTerminal(lines[index + 1])))
return undefined;
return lines[index];
}
return undefined;
}
/** The text after the prompt glyph when all of it is dim (SGR 2), else "". */
function dimText(line) {
let dim = false, prompt = false, text = "";
for (const token of line.match(/\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|[^\x1b]/gu) ?? []) {
if (token.startsWith("\x1b")) {
if (!token.startsWith("\x1b[") || !token.endsWith("m"))
continue;
const codes = (token.slice(2, -1) || "0").split(/[;:]/).map(Number);
for (let i = 0; i < codes.length; i++) {
const code = codes[i];
// Extended colors carry their own numbers: 38;5;2 is not dim.
if (code === 38 || code === 48 || code === 58) {
i += codes[i + 1] === 5 ? 2 : codes[i + 1] === 2 ? 4 : 0;
continue;
}
if (code === 0 || code === 22)
dim = false;
else if (code === 2)
dim = true;
}
}
else if (!prompt) {
if (token === "❯")
prompt = true;
}
else if (!token.trim())
text += " ";
else if (!dim)
return "";
else
text += token;
}
return text.replace(/\s+/g, " ").trim();
}