// PEP-265: questions an agent CLI asks on its screen before it does any work — the folder trust, // a model choice, a sign-in, a first-run setup. An agent stuck on one of them is a failure of the // platform (the launch), not of the story. Patterns are lenient: only the bottom of the screen is // read, and the watchdog asks only while the agent has made no action yet. import { claudeCreditsDialog } from "./claude-screen.ts"; import { codexModelMenu, codexTrustPrompt } from "./codex-screen.ts"; import { ownLines, screenBottom } from "./screen-lines.ts"; export type StartQuestion = "trust" | "model" | "login" | "setup"; const CLAUDE: [StartQuestion, RegExp][] = [ ["trust", /Do you trust the files in this folder\?|Is this a project you (?:created|trust)|Quick safety check|Yes, I trust this folder|Yes, proceed\b/i], // PEP-303: the credits dialog — only by its shape (`claudeCreditsDialog`), the phrase alone may be quoted. ["model", /Select model\b|Choose (?:a|the|your) model/i], ["login", /Select login method|Please run \/login|Invalid API key|Not logged in|OAuth token has expired|Paste code here/i], ["setup", /Choose the text style|Bypass Permissions mode|Yes, I accept|Use Claude Code's terminal setup|Press Enter to continue/i], ]; const CODEX: [StartQuestion, RegExp][] = [ ["trust", /Trust this folder\?|Do you trust the contents of this directory\?|Folder access/i], // PEP-303: the «Switch to …?» menu — only by its shape (`codexModelMenu`). ["model", /Choose (?:a|the|your) model/i], ["login", /Sign in with ChatGPT|Provide your own API key|Finish signing in|Login with ChatGPT/i], ["setup", /Approval mode|Allow Codex to work in this folder|Press enter to continue/i], ]; /** Which question the CLI waits on (null — none recognized). `provider`: "claude" | "codex". */ export function startQuestion(provider: string, screen: string): StartQuestion | null { // PEP-303: a resumed conversation shows its history — quoted diffs and grep output are not questions. const tail = ownLines(screenBottom(screen, 30).join("\n")); if (!tail.trim()) return null; if (provider === "codex") { if (codexTrustPrompt(tail)) return "trust"; if (codexModelMenu(tail)) return "model"; return CODEX.find(([, re]) => re.test(tail))?.[0] ?? null; } if (provider === "claude") { if (claudeCreditsDialog(tail)) return "model"; return CLAUDE.find(([, re]) => re.test(tail))?.[0] ?? null; } return null; }