48 lines
2.8 KiB
TypeScript
48 lines
2.8 KiB
TypeScript
// PEP-227: what the Claude CLI shows on its screen when it is not the agent's work that stopped it —
|
||
// the «<model> now uses usage credits» dialog (20.11: Fable 5.2 began to need separate credits and
|
||
// agents without a model in their settings hung on it). The real screen is not captured yet, so the
|
||
// patterns are lenient about wording, strict about shape (PEP-303): a question, choices at the bottom.
|
||
import * as tmux from "./tmux.ts";
|
||
import { isQuotedLine, screenBottom, unbox } from "./screen-lines.ts";
|
||
|
||
// PEP-303: the question line begins with the model's name («Fable 5.2 now uses usage credits») after at
|
||
// most one glyph of the CLI — not a comment, a string in code or a sentence that quotes it.
|
||
const CREDITS_LINE_RE = /^\s*(?:[^\w\s"'`«(\[{<>|:+\-#*\/]\s*)?[\w .-]{0,40}?\bnow uses (?:usage )?credits\b/iu;
|
||
const CHOICE_RE = /^\s*(?:[›❯>]\s*)?(\d)[.)]\s+(\S.*)$/;
|
||
const SWITCH_RE = /^Switch to Opus\b/i;
|
||
/** Below a waiting dialog: blank rows, its frame and key hints («Enter to confirm · Esc to cancel»). */
|
||
const FOOTER_RE = /^\s*$|^[\s─━═╭╮╰╯┌┐└┘]+$|\b(?:enter|esc|tab)\b|↑|↓|to (?:select|confirm|cancel|navigate)/i;
|
||
|
||
/**
|
||
* The credits dialog waits on screen: `{ digit }` of «Switch to Opus…» (null when there is no such
|
||
* choice), else null. PEP-303: a dialog is the question, numbered choices 1…N under it at the bottom
|
||
* of the screen and nothing below them but key hints — a diff, grep output or the agent's answer
|
||
* that mentions the phrase is not one (the caller also wants the screen unchanged between two passes).
|
||
*/
|
||
export function claudeCreditsDialog(screen: string): { digit: string | null } | null {
|
||
const lines = screenBottom(screen, 30).map(unbox);
|
||
let end = lines.length;
|
||
while (end > 0 && !CHOICE_RE.test(lines[end - 1]!) && FOOTER_RE.test(lines[end - 1]!)) end--;
|
||
// The choices block, bottom up (blank rows between choices are allowed).
|
||
const choices: { n: number; text: string }[] = [];
|
||
let i = end - 1;
|
||
for (; i >= 0; i--) {
|
||
const c = CHOICE_RE.exec(lines[i]!);
|
||
if (c && !isQuotedLine(lines[i]!)) choices.unshift({ n: Number(c[1]), text: c[2]!.trim() });
|
||
else if (!lines[i]!.trim() && choices.length) continue;
|
||
else break;
|
||
}
|
||
if (choices.length < 2 || choices.some((c, k) => c.n !== k + 1)) return null;
|
||
const question = lines.slice(Math.max(0, i - 7), i + 1).some((l) => CREDITS_LINE_RE.test(l) && !isQuotedLine(l));
|
||
if (!question) return null;
|
||
return { digit: String(choices.find((c) => SWITCH_RE.test(c.text))?.n ?? "") || null };
|
||
}
|
||
|
||
/** Picks «Switch to Opus…» in the credits dialog. Returns true when a key was sent. */
|
||
export async function dismissCreditsDialog(pane: tmux.Pane, screen: string): Promise<boolean> {
|
||
const digit = claudeCreditsDialog(screen)?.digit;
|
||
if (!digit) return false;
|
||
await tmux.sendKeys(pane, digit);
|
||
return true;
|
||
}
|