UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

149 lines (148 loc) • 6.19 kB
import { n as createNonExitingRuntime } from "./runtime-B-SLYP-m.mjs"; import { ga as validateSystemAgentSetupAuthStartParams } from "./src-BX8OAe1J.mjs"; import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs"; import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs"; import { t as resolveManifestDeclaredProviderAuthChoices } from "./provider-auth-choices-BfC_56hU.mjs"; import { t as WizardSession } from "./session-beMMG7GW.mjs"; import { i as createProviderBrowserAuthSession } from "./provider-browser-auth-CczEsRoa.mjs"; import { n as defineValidatedGatewayHandler } from "./validation-DwNk6A-B.mjs"; import { n as ProviderCredentialsSavedError, t as ProviderAuthConfigApplyError } from "./provider-auth-result-Cm5Twlfj.mjs"; import { n as localStateOwnerChangedError, t as captureLocalStateMutationGuard } from "./local-state-owner-CnxUSfeW.mjs"; import { i as isProviderLoginChoiceStartable, r as isProviderCliLoginChoiceStartable, t as formatProviderLoginChoiceRef } from "./provider-login-options-DZ4vdFqU.mjs"; import { r as completeProviderModelAccess } from "./auth-model-policy-CtjtbGzO.mjs"; import { c as runModelsAuthLoginFlowForGateway } from "./auth-C-mzlQhy.mjs"; import { t as refreshModelAuthStateAfterMutation } from "./model-auth-refresh-0zj3uFap.mjs"; import { a as startGatewayWizard } from "./setup-admission-CcyIocc3.mjs"; import { n as resolveSystemAgentSessionOwnerKey, t as startSetupActivationWizard } from "./system-agent-setup-wizard-D4d0Wm2_.mjs"; //#region src/gateway/server-methods/models-auth-login.ts const modelsAuthLoginHandlers = { "models.authLogin": defineValidatedGatewayHandler("models.authLogin", validateSystemAgentSetupAuthStartParams, async (e) => { const { params: t, respond: n, context: r, client: i } = e; if (!i || !i.connect.scopes?.includes("operator.admin")) { n(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "Provider login requires an administrator connection.")); return; } let assertOwner; if (t.expectedOwnerId) try { const n = t.expectedOwnerId; assertOwner = () => captureLocalStateMutationGuard(n, e)(); assertOwner(); } catch (e) { n(false, void 0, localStateOwnerChangedError(e)); return; } const resolveChoice = () => { const e = resolveManifestDeclaredProviderAuthChoices({ config: r.getRuntimeConfig(), includeUntrustedWorkspacePlugins: false, includeWorkspacePlugins: false }).filter((e) => formatProviderLoginChoiceRef(e) === t.authChoice); return e.length === 1 ? e[0] : void 0; }; const a = t.expectedOwnerId ? isProviderCliLoginChoiceStartable : isProviderLoginChoiceStartable; const o = resolveChoice(); if (!o || !a(o)) { n(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "That provider login is no longer available. Refresh Models and choose an available sign-in option.")); return; } const assertLoginCurrent = () => { assertOwner?.(); if (i.invalidated || !i.connect.scopes?.includes("operator.admin")) throw new Error("Provider login authority is no longer active."); const e = resolveChoice(); if (!e || !a(e) || e.pluginId !== o.pluginId || e.providerId !== o.providerId || e.methodId !== o.methodId) throw new Error("That provider login is no longer available."); }; if (o.appGuidedAuth && o.credentialOnly !== true) { await startSetupActivationWizard({ request: e, sessionId: t.sessionId, ownerKey: resolveSystemAgentSessionOwnerKey({ client: i }), connectionBound: true, isLocalClient: i.internal?.isLocalClient === true, assertCurrent: assertLoginCurrent, activation: { kind: "provider-auth", authChoice: o.choiceId, agentId: t.agentId, selectModel: t.selectModel === true }, timeoutMs: 15e5 }); return; } await startGatewayWizard({ request: e, sessionId: t.sessionId, connectionBound: true, assertCurrent: assertLoginCurrent, createSession: (e) => new WizardSession(async (n, a, s) => { const c = createNonExitingRuntime(); let l; const openUrl = async (e) => { assertFlowCurrent(); await n.openUrl?.(e); assertFlowCurrent(); }; const u = i.browserOrigin ? createProviderBrowserAuthSession({ signal: a, openUrl, browserOrigin: i.browserOrigin }) : void 0; const assertFlowCurrent = () => { a.throwIfAborted(); e(); u?.assertCurrent(); }; let d; try { d = await runModelsAuthLoginFlowForGateway({ provider: o.providerId, method: o.methodId, ownerPluginId: o.pluginId, credentialOnly: true, onModelAccessRequested: (e) => { l = e; }, agent: t.agentId, config: r.getRuntimeConfig(), runtime: c, prompter: n, signal: u?.signal ?? a, isRemote: !t.expectedOwnerId, openUrl, browserAuthorization: u?.available ? u.authorize : void 0, assertCurrent: assertFlowCurrent, beforePersistentEffect: () => { assertFlowCurrent(); s.lockCancellationForPreparation(); }, refreshAfterLogin: (e) => refreshModelAuthStateAfterMutation(r.getRuntimeConfig, e) }); if (d.profiles.length === 0) throw new Error(`${o.choiceLabel} did not return a credential profile.`); } finally { u?.close(); } const assertModelAccessCurrent = () => { a.throwIfAborted(); e(); }; let f; try { f = await completeProviderModelAccess({ prepared: l, prompter: n, runtime: c, assertCurrent: assertModelAccessCurrent, beforeCommit: () => { assertModelAccessCurrent(); s.lockCancellation(); } }); } catch (e) { throw new ProviderAuthConfigApplyError(e); } if (f.kind === "saved" && f.application !== "applied") throw new ProviderCredentialsSavedError("Your sign-in and model access were saved, but OpenClaw has not confirmed that model access is active. Close this dialog. Open Settings and select Apply changes, then send /models."); if (d.authRefresh !== "refreshed") throw new ProviderCredentialsSavedError("Your sign-in was saved, but the connection update could not be confirmed. Send /login refresh in chat to try again."); }, { timeoutMs: 15e5 }) }); }) }; //#endregion export { modelsAuthLoginHandlers };