openai-codex: classify auth and runtime failures

This commit is contained in:
Eva
2026-04-10 18:55:28 +07:00
committed by Peter Steinberger
parent 776c8e037e
commit 8166d592d9
10 changed files with 407 additions and 10 deletions

View File

@@ -10,6 +10,41 @@ import {
const manualInputPromptMessage = "Paste the authorization code (or full redirect URL):";
const openAICodexOAuthOriginator = "openclaw";
const OPENAI_CODEX_OAUTH_REQUIRED_SCOPES = [
"openid",
"profile",
"email",
"offline_access",
"model.request",
"api.responses.write",
] as const;
function normalizeOpenAICodexAuthorizeUrl(rawUrl: string): string {
const trimmed = rawUrl.trim();
if (!trimmed) {
return rawUrl;
}
try {
const url = new URL(trimmed);
if (!/openai\.com$/i.test(url.hostname) || !/\/oauth\/authorize$/i.test(url.pathname)) {
return rawUrl;
}
const existing = new Set(
(url.searchParams.get("scope") ?? "")
.split(/\s+/)
.map((scope) => scope.trim())
.filter(Boolean),
);
for (const scope of OPENAI_CODEX_OAUTH_REQUIRED_SCOPES) {
existing.add(scope);
}
url.searchParams.set("scope", Array.from(existing).join(" "));
return url.toString();
} catch {
return rawUrl;
}
}
export async function loginOpenAICodexOAuth(params: {
prompter: WizardPrompter;
@@ -60,7 +95,11 @@ export async function loginOpenAICodexOAuth(params: {
});
const creds = await loginOpenAICodex({
onAuth: baseOnAuth,
onAuth: async (event) =>
await baseOnAuth({
...event,
url: normalizeOpenAICodexAuthorizeUrl(event.url),
}),
onPrompt,
originator: openAICodexOAuthOriginator,
onManualCodeInput: isRemote