UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines (4 loc) • 9.57 kB
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{$s as t,Cc as n,Qs as r,Qt as i,Xt as a,xc as o}from"./control-ui-connection-Bfj9beF_.js";import{Fc as s,Ic as c,Lc as l,Pc as u,ag as d,og as f,tg as p}from"./control-ui-boot-shared-DFTSUQQ0.js";import{p as m,s as h,t as g}from"./state-DiRAAGiU.js";var _;function v(){return(v=e((()=>{o(),i(),p(),u(),t(),h(),_=class{constructor(e){this.options=e,this.currentState={phase:`idle`},this.session=null,this.retirementGeneration=0}prepareSignIn(e,t,n){this.authLabel=t,this.authBrandId=n,this.pendingSignIn?.window?.close();let r=e===`oauth`||e===`device-code`;this.pendingSignIn={kind:r?e:`secret`,window:r?c():null}}get state(){return this.currentState}get hasAdmittedSession(){return this.session?.admitted===!0}suspend(e){let t=this.session;t&&(t.suspended=!0,t.abortController.abort(),t.reservedWindow?.close(),t.reservedWindow=null,this.setState({phase:`starting`,authChoice:t.authChoice,...e?{notice:e}:{}}))}restore(e,t){let n=this.options.getClient();n&&!this.session&&(this.session={...e,client:n,notes:[],admitted:!0,retirementGeneration:this.retirementGeneration,abortController:new AbortController,startMethod:`openclaw.setup.auth.start`,onTerminalResult:t},this.setState({phase:`starting`,authChoice:e.authChoice}))}async resume(){let e=this.session,t=this.options.getClient();if(!e?.admitted||!t)return null;e.retired=!0,e.abortController.abort();let n={...e,client:t,abortController:new AbortController,cancellationPromise:void 0,inputClosurePromise:void 0,suspended:!1,retired:!1};this.session=n,this.setState({phase:`starting`,authChoice:n.authChoice});try{return n.terminalResult?this.applyResult(n,n.terminalResult):await this.requestNext(n)}catch(e){return this.handleError(e,n),null}}async start(e,t=`openclaw.setup.auth.start`,n={},r){return this.startSession(e,t,{authChoice:e,...n,...r?{modelTarget:r}:{}})}activate(e,t){return this.startSession(e.authChoice??e.kind,`openclaw.setup.activate.start`,e,t)}startMcpLogin(e){return this.startSession(e,`mcp.authLogin`,{serverName:e})}startRuntime(e,t){return this.authLabel=t,this.authBrandId=void 0,this.startSession(e.runtimeId,`openclaw.setup.runtime.start`,e)}async startSession(e,t,n,i){let o=this.options.getClient();if(!o||this.currentState.phase!==`idle`)return null;let s=r(),c={client:o,sessionId:s,retirementGeneration:this.retirementGeneration,authChoice:e,authKind:this.pendingSignIn?.kind,notes:[],reservedWindow:this.pendingSignIn?.window,abortController:new AbortController,startMethod:t,activationTargetId:i,onTerminalResult:this.options.onStart?.(t,`kind`in n?n:t===`openclaw.setup.auth.start`&&`authChoice`in n?{...n,kind:`provider-auth`,wizard:{sessionId:s,authChoice:e,authKind:this.pendingSignIn?.kind}}:void 0)};this.pendingSignIn=void 0,this.session=c,this.setState({phase:`starting`,authChoice:e});try{let e=`agentId`in n?n.agentId:t===`mcp.authLogin`?null:this.options.getAgentId(),r=o.request(t,{sessionId:c.sessionId,...n,...e?{agentId:e}:{}},{timeoutMs:null}).catch(e=>{if(!d(e))throw e;return{done:!0,status:`not-admitted`,error:a(e,this.options.requestFailedMessage())}}),i=await this.awaitWizardStart(c,r);return i.done?this.applyResult(c,i):(c.admitted=!0,c===this.session?await this.requestNext(c):(await this.cancelSession(c),null))}catch(e){return this.handleError(e,c),null}}async answer(e,t=!0){let n=this.currentState,r=this.session;if(n.phase!==`step`||n.busy||!r)return null;this.setState({...n,busy:!0,validationError:null});let i=t?{stepId:n.step.id,value:e}:{stepId:n.step.id};try{return await this.requestNext(r,i)}catch(e){let t=r.externalInputRequest;if(t){r.externalInputRequest=void 0;let e=await t;if(e||r!==this.session)return e}return this.handleError(e,r),null}}async cancel(){let e=this.session;this.clearSession(),e&&await this.cancelSession(e)}async requestCancellation(){let e=this.session;if(!e)return this.close(),`cancelled`;e.cancellationRequested=!0;let t=()=>e===this.session&&!this.isRetired(e)&&!e.suspended,n;try{n=await this.sendCancellation(e)}catch(n){if(!t())return;if(f(n)){this.handleError(n,e);return}throw n}if(t()){if(n?.status===`cancelled`||n?.status===`error`){if(e.startMethod===`models.authLogin`||e.startMethod===`mcp.authLogin`||e.startMethod===`openclaw.setup.runtime.start`){try{await e.client.request(`wizard.status`,{sessionId:e.sessionId},{timeoutMs:null,signal:e.abortController.signal})}catch(e){if(!t())return;if(!f(e))throw e}if(!t())return}return this.close(),`cancelled`}if(n?.status===`running`)return e.cancellationRequested=!1,`running`}}close(e={}){e.retireOwner&&(this.retirementGeneration+=1),this.clearSession()}clearSession(){this.pendingSignIn?.window?.close(),this.pendingSignIn=void 0,this.session?.reservedWindow?.close(),clearTimeout(this.session?.externalInputTimer),this.session?.abortController.abort(),this.session=null,this.authLabel=void 0,this.authBrandId=void 0,this.setState({phase:`idle`})}fail(e){let{authLabel:t,authBrandId:n}=this;this.close(),this.authLabel=t,this.authBrandId=n,this.setState({phase:`error`,message:e})}async awaitWizardStart(e,t){let r=!1,i=t.then(async t=>(r&&(t.done?this.reportTerminalResult(e,t):await this.cancelSession(e)),t));return await n(i,g,()=>{throw r=!0,Error(this.options.gatewayNotRespondingMessage())})}async requestNext(e,t,n){if(e.suspended||this.isRetired(e))return null;let{client:r,sessionId:i,abortController:a,authChoice:o}=e,s=a.signal,c=t,l=n;for(;;){let t=await r.request(`wizard.next`,{sessionId:i,...c?{answer:c}:{}},{timeoutMs:null,signal:s});if(l&&!l()&&!t.done)return null;if(l=void 0,e===this.session&&!e.suspended&&!this.isRetired(e)&&!t.done&&t.step?.type===`note`&&(e.authKind===`oauth`||e.authKind===`device-code`)){t.step.message&&e.notes.push(t.step.message),(t.step.externalUrl||t.step.deviceCode)&&this.setState({phase:`step`,authChoice:o,step:t.step,busy:!0,validationError:null}),this.openSignInUrl(e,t.step.externalUrl),c={stepId:t.step.id};continue}let n=this.applyResult(e,t);if(e!==this.session||n)return n;let a=this.currentState;if(a.phase!==`step`||a.step.executor!==`gateway`)return null;c=void 0}}applyResult(e,t){if(e===this.session&&e.suspended&&t.done)return e.terminalResult=t,null;let n=this.reportTerminalResult(e,t);if(e!==this.session||e.suspended)return null;if(n?.()===!1||t.done&&t.status===`cancelled`&&e.cancellationRequested)return this.close(),null;let r=m(e.authChoice,t,t.status===`cancelled`?this.options.cancelledMessage():this.options.requestFailedMessage());return r.phase===`step`&&e.authKind===`oauth`&&r.step.type===`text`&&r.step.externalUrl&&(r={...r,externalAuthInput:!0}),e.notes.length&&(r.phase===`error`?r={...r,message:[r.message,...e.notes].join(` `)}:r.phase===`step`&&r.step.executor!==`gateway`&&!r.step.externalUrl&&(r={...r,step:{...r.step,message:[r.step.message,...e.notes].filter(Boolean).join(` `)}})),t.done&&(clearTimeout(e.externalInputTimer),e.reservedWindow?.close(),this.session=null),this.setState(r),r.phase===`step`&&(this.openSignInUrl(e,r.step.externalUrl),r.externalAuthInput?this.watchExternalInput(e,r):r.step.executor!==`gateway`&&(e.reservedWindow?.close(),e.reservedWindow=null)),!t.done||t.status!==`done`?null:{startMethod:e.startMethod,...e.activationTargetId?{activationTargetId:e.activationTargetId}:{},...n?{isCurrent:n}:{},...t.preparedModelRef?{preparedModelRef:t.preparedModelRef}:{},...t.modelActivation?{modelActivation:t.modelActivation}:{}}}openSignInUrl(e,t){if(!t||e.openedUrl===t)return;let n=l(t,window.location.href);n&&(e.reservedWindow&&!e.reservedWindow.closed?(e.reservedWindow.location.replace(n),e.reservedWindow=null):s(n),e.openedUrl=t)}watchExternalInput(e,t){let n=async()=>{let n=()=>e===this.session&&this.currentState===t&&!e.suspended;if(n())try{let t=this.requestNext(e,void 0,n);e.externalInputRequest=t;let r=await t;if(e.externalInputRequest!==t)return;if(e.externalInputRequest=void 0,r){let e=this.currentState,t=r.isCurrent;await this.options.onBackgroundCompletion?.({...r,isCurrent:()=>this.currentState===e&&t?.()!==!1})}}catch(t){n()&&this.handleError(t,e)}};e.externalInputTimer=setTimeout(()=>{n()},1e3)}handleError(e,t){if(t!==this.session||t.suspended)return;clearTimeout(t.externalInputTimer),t.reservedWindow?.close(),this.session=null,t.abortController.abort();let n=f(e);n?this.options.onSessionMissing?.():this.cancelSession(t);let r=n?this.options.sessionExpiredMessage():a(e,this.options.requestFailedMessage());this.setState({phase:`error`,message:[r,...t.notes].join(` `)})}async cancelSession(e){try{await this.sendCancellation(e,e.startMethod===`models.authLogin`||e.startMethod===`mcp.authLogin`||e.startMethod===`openclaw.setup.runtime.start`)}catch{}}async sendCancellation(e,t=!1){if(this.isRetired(e))return;let n=t?`inputClosurePromise`:`cancellationPromise`;return e[n]||(e[n]=e.client.request(`wizard.cancel`,{sessionId:e.sessionId,...t?{closeInput:!0}:{}},{timeoutMs:g}).then(t=>((t.status===`cancelled`||t.status===`error`)&&this.reportTerminalResult(e,{done:!0,...t}),t)).finally(()=>{e[n]=void 0})),e[n]}reportTerminalResult(e,t){if(this.isRetired(e)||e.suspended)return;let n=t.status===`cancelled`||t.status===`error`||t.status===`not-admitted`;if(t.done&&(e===this.session||n))return e.onTerminalResult?.(t)}isRetired(e){return e.retired===!0||e.retirementGeneration!==this.retirementGeneration}setState(e){clearTimeout(this.session?.externalInputTimer),this.authLabel&&(e.authLabel=this.authLabel),this.authBrandId&&(e.authBrandId=this.authBrandId),this.currentState=e,this.options.onChange(e)}}})))()}export{v as n,_ as t};