UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

151 lines (150 loc) • 5.68 kB
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { t as ExitError } from "./runtime-B-SLYP-m.mjs"; import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs"; import { t as sleep } from "./sleep-CkI3nkiJ.mjs"; import { i as formatInvalidConfigDetails, r as createInvalidConfigError } from "./io.invalid-config-DK-BYZ4Y.mjs"; import { s as readConfigFileSnapshot } from "./io.runtime-DsJ2dLCQ.mjs"; import "./config-CMA-J084.mjs"; import { t as resolveManifestDeclaredProviderAuthChoices } from "./provider-auth-choices-BfC_56hU.mjs"; import { n as WizardCancelledError } from "./prompts-CCXo2K24.mjs"; import { t as createClackPrompter } from "./clack-prompter-oqVcatgQ.mjs"; import { r as isProviderCliLoginChoiceStartable, t as formatProviderLoginChoiceRef } from "./provider-login-options-DZ4vdFqU.mjs"; import { t as answerGatewayWizardStep } from "./gateway-wizard-step-BShKPhM0.mjs"; import { n as normalizeManualAuthProvider } from "./auth-manual-input-DeJ4s3kg.mjs"; import { t as readGatewayApiKeyParams } from "./auth-gateway-DnfvdDBo.mjs"; //#region src/commands/models/auth-login-gateway.ts async function readGatewayLoginParams(e, t, n) { const r = e.provider ? normalizeManualAuthProvider(e.provider) : void 0; const i = normalizeLowercaseStringOrEmpty(e.method); const a = await readConfigFileSnapshot({ observe: false, isolateEnv: true, skipPluginValidation: true }); n.throwIfAborted(); if (!a.valid) throw createInvalidConfigError(a.path, formatInvalidConfigDetails(a.issues)); const o = resolveManifestDeclaredProviderAuthChoices({ config: a.runtimeConfig, includeUntrustedWorkspacePlugins: false, includeWorkspacePlugins: false }).filter((e) => isProviderCliLoginChoiceStartable(e) && (r === void 0 || e.providerId === r) && (!i || normalizeLowercaseStringOrEmpty(e.methodId) === i)); if (!o.length) throw new Error("No Gateway-compatible login matches this provider and method. Choose a declared credential-only sign-in method, or stop the Gateway through its service owner and rerun this command offline."); const s = createClackPrompter(process.stderr, n); const c = o.length === 1 ? o[0] : void 0; const l = c ? formatProviderLoginChoiceRef(c) : await s.select({ message: "Choose a provider sign-in method", options: o.map((e) => ({ value: formatProviderLoginChoiceRef(e), label: e.choiceLabel, hint: `${e.providerId} / ${e.methodId}` })) }).catch((e) => { if (e instanceof WizardCancelledError) throw new ExitError(0); throw e; }); n.throwIfAborted(); return { sessionId: t, authChoice: l, agentId: e.agent }; } async function answerLoginStep(e, t, n) { const r = sanitizeTerminalText(e.message ?? e.title ?? "Continue"); if (e.externalUrl) await t.note(sanitizeTerminalText(e.externalUrl), "Open this URL to continue"); if (e.deviceCode) await t.note(sanitizeTerminalText(e.deviceCode.code), e.title ?? "Device code"); if (e.type === "text" && !process.stdin.isTTY && e.sensitive) return (await readGatewayApiKeyParams({ provider: "login" }, n)).apiKey; if (e.type === "progress") { await t.note(r, e.title); return; } if ((e.type === "confirm" || e.type === "action") && e.executor === "gateway") return; return await answerGatewayWizardStep({ ...e, message: r }, t, { signal: n }); } async function answerBrowserStep(e, t, n, r) { const i = new AbortController(); const a = AbortSignal.any([r, i.signal]); const o = answerLoginStep(n, createClackPrompter(process.stderr, a), a).then((e) => ({ value: e }), (e) => ({ error: e })); try { for (;;) { const i = new AbortController(); let a; try { a = await Promise.race([o, sleep(1e3, AbortSignal.any([r, i.signal])).then(() => void 0)]); } finally { i.abort(); } r.throwIfAborted(); if (a) { if ("error" in a) throw a.error; return a; } const s = await e("wizard.next", { sessionId: t }, { signal: r }); if (s.done || s.step?.id !== n.id) return { next: s }; } } finally { i.abort(); await o; } } async function runGatewayLoginWizard(e, t, n, r) { const i = createClackPrompter(process.stderr, n); let a = false; try { let o = await e("wizard.next", { sessionId: t }, { signal: n, timeoutMs: 15e5 }); while (!o.done) { n.throwIfAborted(); if (o.error) r.error(sanitizeTerminalText(o.error)); const a = o.step; let s; if (a?.type === "text" && a.externalUrl) { const r = await answerBrowserStep(e, t, a, n); if ("next" in r) { o = r.next; continue; } s = r.value; } else s = a ? await answerLoginStep(a, i, n) : void 0; n.throwIfAborted(); try { o = await e("wizard.next", { sessionId: t, ...a && a.type !== "progress" && a.executor !== "gateway" ? { answer: { stepId: a.id, value: s } } : {} }, { signal: n, timeoutMs: 15e5 }); } catch (r) { if (a?.type !== "text" || !a.externalUrl) throw r; const i = await e("wizard.next", { sessionId: t }, { signal: n }); if (!i.done) throw r; o = i; } } a = true; if (o.status === "cancelled") throw new WizardCancelledError(o.error); if (o.status === "error") throw new Error(o.error ?? "Gateway provider login failed."); r.log("Provider sign-in saved and connection update confirmed by the Gateway."); } catch (i) { if (!a && !n.aborted) await e("wizard.cancel", { sessionId: t, closeInput: true }); if (i instanceof WizardCancelledError) { r.log("Login session closed. Credentials already saved were not undone."); return; } throw i; } } //#endregion export { readGatewayLoginParams, runGatewayLoginWizard };