UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

126 lines (125 loc) • 5.44 kB
import { s as raceWithTimeout } from "./src-BPmQBLJp.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs"; import { r as ensureGlobalUndiciEnvProxyDispatcher } from "./undici-global-dispatcher-CdeU188Q.mjs"; import "./time-runtime-DoAwjF0T.mjs"; import "./error-runtime-BM740gJj.mjs"; import "./runtime-env-D-Pa5lD6.mjs"; import "./setup-tools-NrA1nFHA.mjs"; import { t as loginOpenAICodex } from "./openai-chatgpt-oauth-flow.runtime.js"; import { t as runOpenAIOAuthTlsPreflight } from "./openai-chatgpt-oauth-preflight.runtime.js"; import path from "node:path"; //#region extensions/openai/openai-chatgpt-oauth.runtime.ts const manualInputPromptMessage = "Paste the authorization code (or full redirect URL):"; const openAICodexOAuthOriginator = "openclaw"; const localManualFallbackDelayMs = 15e3; const localManualFallbackGraceMs = 1e3; function createOpenAICodexOAuthError(e, t, n) { return Object.assign(new Error(`OpenAI Codex OAuth failed (${e}): ${t}`, { cause: n }), { code: e }); } function rewriteOpenAICodexOAuthError(e) { const t = formatErrorMessage(e); if (/unsupported_country_region_territory/i.test(t)) return createOpenAICodexOAuthError("unsupported_region", ["OpenAI rejected the token exchange for this country, region, or network route.", "If you normally use a proxy, verify HTTPS_PROXY, HTTP_PROXY, or ALL_PROXY is set for the OpenClaw process and then retry `openclaw models auth login --provider openai`."].join(" "), e); if (/state mismatch|missing authorization code/i.test(t)) return createOpenAICodexOAuthError("callback_validation_failed", t, e); return e instanceof Error ? e : new Error(t); } async function loginOpenAICodexOAuth(e) { const { prompter: t, runtime: n, isRemote: r, openUrl: i, localBrowserMessage: a } = e; ensureGlobalUndiciEnvProxyDispatcher(); const o = await runOpenAIOAuthTlsPreflight({ signal: e.signal, assertCurrent: e.assertCurrent }); e.assertCurrent?.(); if (!o.ok && o.kind === "tls-cert") { const e = process.execPath; const r = `${path.sep}Cellar${path.sep}`; const i = e.indexOf(r); const a = i > 0 ? e.slice(0, i) : process.env.HOMEBREW_PREFIX?.trim() || null; const s = a ? path.join(a, "etc", "openssl@3", "cert.pem") : null; const c = [ "OpenAI OAuth prerequisites check failed: Node/OpenSSL cannot validate TLS certificates.", `Cause: ${o.code ? `${o.code} (${o.message})` : o.message}`, "", "Fix (Homebrew Node/OpenSSL):", `- ${formatCliCommand("brew postinstall ca-certificates")}`, `- ${formatCliCommand("brew postinstall openssl@3")}` ]; if (s) c.push(`- Verify cert bundle exists: ${s}`); c.push("- Retry the OAuth login flow."); const l = c.join("\n"); await t.note(l, "OAuth prerequisites"); n.error(l); throw new Error(`OpenAI Codex OAuth prerequisites failed: ${o.message}`); } await t.note(r ? [ "You are running in a remote/VPS environment.", "A URL will be shown for you to open in your LOCAL browser.", "Open it, sign in, then paste the redirect URL here.", "If this OpenClaw process can receive the browser callback, sign-in may finish automatically before you paste." ].join("\n") : [ "Browser will open for OpenAI authentication.", "If the callback doesn't auto-complete, paste the redirect URL.", "OpenAI OAuth uses localhost:1455 for the callback." ].join("\n"), "OpenAI Codex OAuth"); const s = t.progress("Starting OAuth flow..."); let c = true; const updateProgress = (e) => { if (c) s.update(e); }; const stopProgress = (e) => { if (c) { c = false; s.stop(e); } }; let l; const u = new Promise((e) => { l = e; }); const d = new AbortController(); try { const { onAuth: o, onPrompt: c } = e.oauth.createVpsAwareHandlers({ isRemote: r, prompter: t, runtime: n, spin: s, openUrl: i, localBrowserMessage: a ?? "Complete sign-in in browser...", manualPromptMessage: manualInputPromptMessage, manualPromptSignal: d.signal }); const promptForManualCode = () => c({ message: manualInputPromptMessage }); const switchToManualEntry = async (e, t) => { updateProgress(e); if (t) n.log(t); stopProgress("Manual OAuth entry required"); return await promptForManualCode(); }; const runLocalManualFallback = async () => { for (const e of [localManualFallbackDelayMs, localManualFallbackGraceMs]) if (await raceWithTimeout(u.then(() => "settled", () => "settled"), e, () => "delay") === "settled") return await new Promise(() => {}); return await switchToManualEntry("Browser callback did not finish. Paste the redirect URL to continue...", `OpenAI Codex OAuth callback did not arrive within ${localManualFallbackDelayMs}ms; switching to manual entry (callback_timeout).`); }; const l = await loginOpenAICodex({ onAuth: o, onPrompt: c, originator: openAICodexOAuthOriginator, onManualCodeInput: e.onManualCodeInput ?? (r ? promptForManualCode : runLocalManualFallback), signal: e.signal, assertCurrent: e.assertCurrent }); stopProgress("OpenAI OAuth complete"); return l ?? null; } catch (e) { stopProgress("OpenAI OAuth failed"); const r = rewriteOpenAICodexOAuthError(e); n.error(String(r)); await t.note("Trouble with OAuth? See https://docs.openclaw.ai/start/faq", "OAuth help"); throw r; } finally { d.abort(); l(); } } //#endregion export { loginOpenAICodexOAuth as t };