openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.84 kB
JavaScript
import{n as createNonExitingRuntime}from"./runtime-DIcLIHcp.mjs";import{Sa as validateSystemAgentSetupAuthStartParams}from"./src-BDZMFmKa.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{t as resolveManifestDeclaredProviderAuthChoices}from"./provider-auth-choices-Ca4qv7bL.mjs";import{t as WizardSession}from"./session-CN7tnCR5.mjs";import{i as createProviderBrowserAuthSession}from"./provider-browser-auth-r1C9K1AJ.mjs";import{n as defineValidatedGatewayHandler}from"./validation-CM41YclJ.mjs";import{n as ProviderCredentialsSavedError}from"./provider-auth-result-BBq2IiCp.mjs";import{n as localStateOwnerChangedError,t as captureLocalStateMutationGuard}from"./local-state-owner-DweIAMz2.mjs";import{i as isProviderLoginChoiceStartable,r as isProviderCliLoginChoiceStartable,t as formatProviderLoginChoiceRef}from"./provider-login-options-C_gsGjlo.mjs";import{c as runModelsAuthLoginFlowForGateway}from"./auth-oFv-kj-8.mjs";import{t as refreshModelAuthStateAfterMutation}from"./model-auth-refresh-DuOWCz8I.mjs";import{a as startGatewayWizard}from"./setup-admission-7MXhXxj9.mjs";import{n as resolveSystemAgentSessionOwnerKey,t as startSetupActivationWizard}from"./system-agent-setup-wizard-Cm1liTIq.mjs";
//!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();const openUrl=async e=>{assertFlowCurrent();await n.openUrl?.(e);assertFlowCurrent()};const l=i.browserOrigin?createProviderBrowserAuthSession({signal:a,openUrl,browserOrigin:i.browserOrigin,authPolicy:i.authPolicy}):void 0;const assertFlowCurrent=()=>{a.throwIfAborted();e();l?.assertCurrent()};let u;try{u=await runModelsAuthLoginFlowForGateway({provider:o.providerId,method:o.methodId,ownerPluginId:o.pluginId,credentialOnly:true,agent:t.agentId,config:r.getRuntimeConfig(),runtime:c,prompter:n,signal:l?.signal??a,isRemote:!t.expectedOwnerId,openUrl,browserAuthorization:l?.available?l.authorize:void 0,assertCurrent:assertFlowCurrent,beforePersistentEffect:()=>{assertFlowCurrent();s.lockCancellationForPreparation()},refreshAfterLogin:e=>refreshModelAuthStateAfterMutation(r.getRuntimeConfig,e)});if(u.profiles.length===0)throw new Error(`${o.choiceLabel} did not return a credential profile.`)}finally{l?.close()}if(u.modelAccess&&!u.modelAccess.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(u.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})})})};export{modelsAuthLoginHandlers};