28 lines
1.4 KiB
TypeScript
28 lines
1.4 KiB
TypeScript
// PEP-205: what the Codex CLI shows on its screen when it is not the agent's work that stopped it —
|
||
// the «Switch to <model>?» menu near the limit and a sign-in hiccup. Kept apart from the watchdog so
|
||
// the texts can be checked on real screens (packages/testkit/fixtures).
|
||
import * as tmux from "./tmux.ts";
|
||
|
||
/** «Switch to gpt-… for lower credit usage?» with its three choices. */
|
||
const MENU_QUESTION_RE = /Switch to \S+.*\?/;
|
||
const MENU_KEEP_RE = /^\s*(?:›\s*)?(\d)\.\s*Keep current model\s*$/m;
|
||
|
||
/** The digit of «Keep current model» when the model menu is on screen, else null. */
|
||
export function codexModelMenu(screen: string): string | null {
|
||
// The pane is padded with blank rows below the text.
|
||
const tail = screen.trimEnd().split("\n").slice(-25).join("\n");
|
||
if (!MENU_QUESTION_RE.test(tail) || !/enter select/i.test(tail)) return null;
|
||
return MENU_KEEP_RE.exec(tail)?.[1] ?? null;
|
||
}
|
||
|
||
/** Answers the menu with «Keep current model». Returns true when a key was sent. */
|
||
export async function dismissCodexMenu(pane: tmux.Pane, screen: string): Promise<boolean> {
|
||
const digit = codexModelMenu(screen);
|
||
if (!digit) return false;
|
||
await tmux.sendKeys(pane, digit);
|
||
return true;
|
||
}
|
||
|
||
/** The CLI could not read the account at start: a network hiccup on the way to the provider, not the agent's failure. */
|
||
export const CODEX_LOGIN_FAIL_RE = /account\/read failed|workspace routing discovery timed out/i;
|