import type { ChatMsg, ChatOpts, ChatResult, LLMHealth, LLMProvider } from '../../contracts/llm.js'; /** * MODEL-DRIFT HARDENING (2026-07-13). * * The constant above is a snapshot of a catalog OpenAI changes without notice. When it * drifts, the failure is an opaque `HTTP 400 — "The 'X' model is not supported when * using Codex with a ChatGPT account"` from deep inside a chat call, with no hint of * what IS supported. That message cost real debugging time on 2026-07-13, and the * answer was sitting in a file on disk the whole time. * * The Codex CLI maintains `~/.codex/models_cache.json` — the account's actual catalog. * Read it, and turn an unusable 400 into an actionable error that names the models the * account can really use. * * Deliberately NON-FATAL if the cache is missing or unparseable: a stale/absent cache * must not break a provider that would otherwise work. We only *validate against* the * catalog when we can actually read one — never gate on its presence. */ export interface CodexCatalogEntry { id: string; displayName?: string; } export declare function readCodexModelCatalog(authPath?: string): CodexCatalogEntry[] | null; /** Prefer a `-mini` (cheap tier) model from the catalog; else the first entry. */ export declare function pickCheapestCodexModel(catalog: CodexCatalogEntry[]): string; /** * Validate the configured model against the account's real catalog, BEFORE any chat * call. Returns a human-actionable message, or null if the model is fine (or if we have * no catalog to check against, in which case we do not block). */ export declare function validateCodexModel(model: string, catalog: CodexCatalogEntry[] | null): string | null; export interface CodexSubscriptionLLMConfig { model?: string; /** Override for ~/.codex/auth.json — test-only seam; production callers * should leave this unset and use the real Codex CLI location. */ authPath?: string; } /** Default location of the Codex CLI's own auth file — exported so every * caller that needs to know "is Codex logged in" (init wizard, the doctor * probe, and the serve.ts first-run boot guard) derives the same path * instead of re-deriving `join(homedir(), '.codex', 'auth.json')` * independently. `src/cli/init.ts` previously duplicated this exact * expression under its own `defaultCodexAuthPath()` name. */ export declare function defaultAuthPath(): string; /** * Presence-only check: "does a Codex CLI login file exist at this path". * Deliberately does NOT parse or validate the file — a malformed or * token-expired auth.json is a different, more specific failure that * `codexAuthProbe` (doctor/probes/codex-subscription-probe.ts) already * reports with an actionable message. This helper answers the narrower, * cheaper question a boot-time guard needs before the first extraction call * is even attempted: has `codex login` ever been run at all. */ export declare function hasCodexLogin(authPath?: string): boolean; /** Decode a JWT's payload (middle segment) without verifying the signature — * we trust the token because it came from our own local auth.json / a * same-process refresh response, not from an untrusted third party. */ export declare function decodeJwtPayload(jwt: string): Record | undefined; export declare class CodexSubscriptionLLMProvider implements LLMProvider { readonly name: "codex-subscription"; readonly model: string; /** Public so doctor probes can check auth.json presence/expiry against the * exact path this provider will read — see * doctor/probes/codex-subscription-probe.ts. */ readonly authPath: string; private accessToken; private refreshToken; private accountId; private authLoaded; /** Raw auth.json content as of the last (re)load — FEAT-540's * reload-on-401 signal. Content-compared rather than mtime-compared: * filesystem mtime granularity varies by OS and is a flaky signal in * tests and on some platforms; a raw string compare is exact and just as * cheap for a file this small. */ private lastAuthRaw; constructor(config?: CodexSubscriptionLLMConfig); /** Lazily loads auth.json on first use rather than at construction, so * building the provider set at boot (providers/index.ts) never throws for * this provider even if login hasn't happened yet — the clear * `codex login` error instead surfaces from the first real chat()/health() * call (and from the dedicated doctor probe), which is where a distill job * can classify it and route to the retry/poison path instead of crashing * the daemon. */ private ensureAuthLoaded; /** Parses raw auth.json content and swaps it into the in-memory token * fields, recording it as the new `lastAuthRaw` baseline. Shared by the * initial load and the FEAT-540 reload-on-401 path. */ private applyAuthRaw; /** * FEAT-540: on a 401, re-read auth.json from disk and swap in fresh * tokens IF the file's content actually changed since we last loaded it. * This is how an already-running daemon picks up an operator's * out-of-band `codex login` (e.g. after the session-invalidating * 2026-07-17 quota incident) WITHOUT a full restart. * * Returns false (no-op, existing tokens left untouched) when: the file is * momentarily unreadable, the content is byte-identical to what we already * have loaded, or the new content fails to parse/validate — in every one * of those cases the caller falls through to the pre-existing OAuth * refresh_token flow (`refresh()`) instead. * * The unchanged-content check IS the reload-storm guard (AC-3): a 401 * against an auth.json nobody has touched is a hard "no", not a retry — * we only ever burn the extra fetch when there is genuinely new material * on disk to try, so a permanently-dead token can never trigger more than * one reload attempt per chat() call, and zero on every call after that * until the file changes again. */ private tryReloadAuthFromDisk; private refresh; private headers; /** * `externalSignal` (issue #261): caller-supplied cancellation, combined * with — never replacing — the provider's own CHAT_TIMEOUT_MS ceiling via * `AbortSignal.any`, same contract as the other three providers' * `fetchWithRetry` (which this now delegates to — see that function's doc * comment for why the 401 path above in chat() is unaffected). */ private callResponses; chat(messages: ChatMsg[], opts?: ChatOpts): Promise; health(): Promise; }