62 lines
3.1 KiB
TypeScript
62 lines
3.1 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;
|
||
|
||
/** PEP-241: «Trust this folder?» (0.169 «Do you trust the contents of this directory?», 0.171 «Folder access»). */
|
||
const TRUST_QUESTION_RE = /Trust this folder\?|Do you trust the contents of this directory\?/i;
|
||
const TRUST_YES_RE = /^\s*(?:›\s*)?(\d)\.\s*(?:Trust and continue|Yes, continue)\b/im;
|
||
|
||
export interface CodexTrustPrompt {
|
||
digit: string;
|
||
/** Absolute paths shown on the screen (the folder Codex asks about). */
|
||
paths: string[];
|
||
}
|
||
|
||
/** The trust question on Codex's screen: the digit of «Trust and continue» and the folders it names. */
|
||
export function codexTrustPrompt(screen: string): CodexTrustPrompt | null {
|
||
const tail = screen.trimEnd().split("\n").slice(-30).join("\n");
|
||
if (!TRUST_QUESTION_RE.test(tail)) return null;
|
||
const digit = TRUST_YES_RE.exec(tail)?.[1];
|
||
if (!digit) return null;
|
||
const paths = [...tail.matchAll(/(?:^|[\s(«"'`])(\/[^\s)»"'`]+)/g)].map((m) => m[1]!.replace(/[.,:;]+$/, "").replace(/(.)\/+$/, "$1"));
|
||
return { digit, paths: [...new Set(paths)] };
|
||
}
|
||
|
||
/**
|
||
* Answers «Trust and continue» — only for the folder Peppa gave the agent: every path on the screen
|
||
* must be one of `allowed` (a screen without a path is about the folder Codex was started in, `--cd`).
|
||
* Returns "sent", "foreign" (a folder that is not ours — nothing pressed) or null (no such screen).
|
||
*/
|
||
export async function dismissCodexTrust(pane: tmux.Pane, screen: string, allowed: string[]): Promise<"sent" | "foreign" | null> {
|
||
const prompt = codexTrustPrompt(screen);
|
||
if (!prompt) return null;
|
||
const ok = new Set(allowed.map((p) => p.replace(/(.)\/+$/, "$1")));
|
||
if (prompt.paths.some((p) => !ok.has(p))) return "foreign";
|
||
await tmux.sendKeys(pane, prompt.digit);
|
||
return "sent";
|
||
}
|