peppa/packages/runner/src/claude-screen.ts

25 lines
1.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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: the question anywhere near the bottom, a numbered «Switch to Opus…» choice.
import * as tmux from "./tmux.ts";
const CREDITS_RE = /now uses (?:usage )?credits/i;
const SWITCH_RE = /^\s*(?:[›❯>]\s*)?(\d)[.)]\s*Switch to Opus\b/im;
/** The credits dialog is on screen: `{ digit }` of «Switch to Opus…» (null when there is no such choice), else null. */
export function claudeCreditsDialog(screen: string): { digit: string | null } | null {
// The pane is padded with blank rows below the text.
const tail = screen.trimEnd().split("\n").slice(-30).join("\n");
if (!CREDITS_RE.test(tail)) return null;
return { digit: SWITCH_RE.exec(tail)?.[1] ?? 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;
}