openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
63 lines (60 loc) • 15.1 kB
JavaScript
import{n as e}from"./rolldown-runtime-8BhlS34s.js";import{Et as t,Tt as n}from"./control-ui-foundation-ZY_Flbbd.js";import{$i as r,Gl as i,Xi as a,an as o,cn as s,ea as c,hu as l,on as u,ql as d,sn as f,uu as p}from"./control-ui-core-3P365fbR.js";import{Br as m}from"./control-ui-core-CFW9Qtjn.js";import{$ as h,X as g,Y as _}from"./lit-runtime-P5vbNq6b.js";import{n as v,t as y}from"./en-settings-CTjRjKt9.js";import{r as b,t as x}from"./wizard-step-controls-BmW-9g-y.js";import{f as S,o as C,s as w,t as T}from"./state-rFA9s-wO.js";var E;function D(){return(D=e((()=>{d(),a(),o(),t(),C(),E=class{constructor(e){this.options=e,this.currentState={phase:`idle`},this.session=null,this.retirementGeneration=0}prepareSignIn(e,t){this.authLabel=t,this.pendingSignIn?.window?.close();let n=e===`oauth`||e===`device-code`;this.pendingSignIn={kind:n?e:`secret`,window:n?f():null}}get state(){return this.currentState}get hasAdmittedSession(){return this.session?.admitted===!0}suspend(){let e=this.session;e&&(e.suspended=!0,e.abortController.abort(),this.setState({phase:`starting`,authChoice:e.authChoice}))}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.authChoice,n.terminalResult):await this.requestNext(n,n.authChoice)}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})}async startSession(e,t,a,o){let s=this.options.getClient();if(!s||this.currentState.phase!==`idle`)return null;let c=n(),l={client:s,sessionId:c,retirementGeneration:this.retirementGeneration,authChoice:e,authKind:this.pendingSignIn?.kind,notes:[],reservedWindow:this.pendingSignIn?.window,abortController:new AbortController,startMethod:t,activationTargetId:o,onTerminalResult:this.options.onStart?.(t,`kind`in a?a:t===`openclaw.setup.auth.start`&&`authChoice`in a?{...a,kind:`provider-auth`,wizard:{sessionId:c,authChoice:e,authKind:this.pendingSignIn?.kind}}:void 0)};this.pendingSignIn=void 0,this.session=l,this.setState({phase:`starting`,authChoice:e});try{let n=t===`mcp.authLogin`?null:this.options.getAgentId(),o=s.request(t,{sessionId:l.sessionId,...a,...n?{agentId:n}:{}},{timeoutMs:null}).catch(e=>{if(!r(e))throw e;return{done:!0,status:`not-admitted`,error:i(e,this.options.requestFailedMessage())}}),c=await this.awaitWizardStart(l,o);return c.done||(l.admitted=!0),l!==this.session&&!c.done?(await this.cancelSession(l),null):c.done?this.applyResult(l,e,c):await this.requestNext(l,e)}catch(e){return this.handleError(e,l),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,n.authChoice,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(e={}){this.pendingSignIn?.window?.close(),this.pendingSignIn=void 0;let t=this.session;t?.reservedWindow?.close(),clearTimeout(t?.externalInputTimer),e.settleActiveRequest||t?.abortController.abort(),this.session=null,this.authLabel=void 0,this.setState({phase:`idle`}),t&&await this.cancelSession(t)}async requestCancellation(){let e=this.session;if(!e)return this.close(),`cancelled`;e.cancellationRequested=!0;let t;try{t=await this.sendCancellation(e)}catch(t){if(e!==this.session||this.isRetired(e)||e.suspended)return;if(c(t)){this.handleError(t,e);return}throw t}if(!(e!==this.session||this.isRetired(e)||e.suspended)){if(t?.status===`cancelled`||t?.status===`error`){if(e.startMethod===`models.authLogin`||e.startMethod===`mcp.authLogin`){try{await e.client.request(`wizard.status`,{sessionId:e.sessionId},{timeoutMs:null,signal:e.abortController.signal})}catch(t){if(e!==this.session||this.isRetired(e)||e.suspended)return;if(!c(t))throw t}if(e!==this.session||this.isRetired(e)||e.suspended)return}return this.close(),`cancelled`}if(t?.status===`running`)return e.cancellationRequested=!1,`running`}}close(e={}){e.retireOwner&&(this.retirementGeneration+=1),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.setState({phase:`idle`})}fail(e){let t=this.authLabel;this.close(),this.authLabel=t,this.setState({phase:`error`,message:e})}async awaitWizardStart(e,t){let n=!1,r,i=t.then(async t=>(n&&(t.done?this.reportTerminalResult(e,t):await this.cancelSession(e)),t));try{return await Promise.race([i,new Promise((t,i)=>{r=setTimeout(()=>{n=!0,i(Error(`gateway request timed out after ${T}ms: ${e.startMethod}`))},T)})])}finally{clearTimeout(r)}}async requestNext(e,t,n,r){if(e.suspended||this.isRetired(e))return null;let{client:i,sessionId:a,abortController:o}=e,s=o.signal,c=n,l=r;for(;;){let n=await i.request(`wizard.next`,{sessionId:a,...c?{answer:c}:{}},{timeoutMs:null,signal:s});if(l&&!l()&&!n.done)return null;if(l=void 0,e===this.session&&!e.suspended&&!this.isRetired(e)&&!n.done&&n.step?.type===`note`&&(e.authKind===`oauth`||e.authKind===`device-code`)){n.step.message&&e.notes.push(n.step.message),(n.step.externalUrl||n.step.deviceCode)&&this.setState({phase:`step`,authChoice:t,step:n.step,busy:!0,validationError:null}),this.openSignInUrl(e,n.step.externalUrl),c={stepId:n.step.id};continue}let r=this.applyResult(e,t,n);if(e!==this.session||r)return r;let o=this.currentState;if(o.phase!==`step`||o.step.executor!==`gateway`)return null;c=void 0}}applyResult(e,t,n){if(e===this.session&&e.suspended&&n.done)return e.terminalResult=n,null;let r=this.reportTerminalResult(e,n);if(e!==this.session||e.suspended)return null;if(r?.()===!1||n.done&&n.status===`cancelled`&&e.cancellationRequested)return this.close(),null;let i=S(t,n,n.status===`cancelled`?this.options.cancelledMessage():this.options.requestFailedMessage());return i.phase===`step`&&e.authKind===`oauth`&&i.step.type===`text`&&i.step.externalUrl&&(i={...i,externalAuthInput:!0}),e.notes.length&&(i.phase===`error`?i={...i,message:[i.message,...e.notes].join(`
`)}:i.phase===`step`&&i.step.executor!==`gateway`&&!i.step.externalUrl&&(i={...i,step:{...i.step,message:[i.step.message,...e.notes].filter(Boolean).join(`
`)}})),clearTimeout(e.externalInputTimer),n.done&&(e.reservedWindow?.close(),this.session=null),this.setState(i),i.phase===`step`&&(this.openSignInUrl(e,i.step.externalUrl),i.externalAuthInput?this.watchExternalInput(e,i):i.step.executor!==`gateway`&&(e.reservedWindow?.close(),e.reservedWindow=null)),!n.done||n.status!==`done`?null:{startMethod:e.startMethod,...e.activationTargetId?{activationTargetId:e.activationTargetId}:{},...r?{isCurrent:r}:{},...n.preparedModelRef?{preparedModelRef:n.preparedModelRef}:{},...n.modelActivation?{modelActivation:n.modelActivation}:{}}}openSignInUrl(e,t){if(!t||e.openedUrl===t)return;let n=s(t,window.location.href);n&&(e.reservedWindow&&!e.reservedWindow.closed?(e.reservedWindow.location.replace(n),e.reservedWindow=null):u(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,e.authChoice,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=c(e);n||this.cancelSession(t);let r=n?this.options.sessionExpiredMessage():i(e,this.options.requestFailedMessage());this.setState({phase:`error`,message:[r,...t.notes].join(`
`)})}async cancelSession(e){try{return await this.sendCancellation(e,e.startMethod===`models.authLogin`||e.startMethod===`mcp.authLogin`)}catch{return}}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:T}).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.currentState=e,this.options.onChange(e)}}})))()}function O(e){if(e.state.phase===`idle`)return g;let t=e.state.phase===`starting`||e.state.phase===`step`;return h`
<openclaw-modal-dialog
label=${l(e.mode===`prepare`?`modelSetup.wizard.prepareDialogLabel`:e.mode===`activate`?`modelSetup.heading`:`modelSetup.wizard.dialogLabel`)}
-cancel=${t?e.onCancel:e.onClose}
>
<div class="model-setup-wizard">
<div class="model-setup-wizard__header">
<h2>
${e.state.authLabel?e.state.authLabel:e.state.phase===`step`&&e.state.step.title?e.state.step.title:l(e.mode===`prepare`?`modelSetup.wizard.prepareTitle`:e.mode===`activate`?`modelSetup.heading`:`modelSetup.wizard.title`)}
</h2>
</div>
<div class="model-setup-wizard__body">
${[e.refreshWarning,e.cancellationNotice].map(e=>e?h`<div class="callout warning" role="alert">${e}</div>`:g)}
${e.state.phase===`starting`?h`<div role="status">
${l(e.mode===`prepare`?`modelSetup.wizard.prepareStarting`:e.mode===`activate`?`modelSetup.wizard.checking`:`modelSetup.wizard.starting`)}
</div>`:e.state.phase===`done`?h`<div role="status">
${e.doneMessage??l(e.mode===`auth`?`modelSetup.wizard.connected`:`modelSetup.wizard.checking`)}
</div>`:e.state.phase===`error`||e.state.phase===`cancelled`?h`<div class="callout danger" role="alert">
${e.state.phase===`cancelled`||e.mode!==`auth`?e.state.message:l(`modelSetup.wizard.failed`)}
</div>
${e.state.phase===`error`&&e.mode===`auth`?h`<details>
<summary>${l(`modelSetup.wizard.details`)}</summary>
<p>${e.state.message}</p>
</details>`:g}`:h`
${e.state.validationError?h`<div
id="model-setup-wizard-validation-error"
class="callout danger"
role="alert"
>
${e.state.validationError}
</div>`:g}
${b({step:e.state.step,externalAuthInput:e.state.externalAuthInput,value:e.value,busy:e.state.busy,inputId:k,validationErrorId:e.state.validationError?`model-setup-wizard-validation-error`:void 0,confirmAffirmativeLabel:e.mode===`prepare`&&e.state.step.type===`confirm`?l(`modelSetup.wizard.continue`):void 0,leadingAction:h`<button
type="button"
class="btn"
=${e.onCancel}
>
${l(`common.cancel`)}
</button>`,onValueChange:e.onValueChange,onAnswer:e.onAnswer})}
${e.state.busy&&!e.state.step.externalUrl&&!e.state.step.deviceCode?h`<div role="status">${l(`modelSetup.wizard.working`)}</div>`:g}
`}
</div>
${e.state.phase===`step`?g:h`
<div class="model-setup-wizard__footer">
<button
type="button"
class="btn"
=${t?e.onCancel:e.onClose}
>
${l(t?`common.cancel`:`common.close`)}
</button>
</div>
`}
</div>
</openclaw-modal-dialog>
`}var k;function A(){return(A=e((()=>{_(),x(),p(),m(),k=`model-setup-wizard-text-input`})))()}function j(){return(j=e((()=>{})))()}var M;function N(){return(N=e((()=>{_(),p(),y(),d(),C(),D(),A(),v(),M=class{constructor(e,t){this.host=e,this.options=t,this.cancelling=!1,this.stepId=null,this.generation=0,this.cancellationNotice=null,this.runner=new E({...t,onChange:t=>{t.phase===`starting`&&(this.generation+=1),t.phase!==`step`&&(this.cancelling=!1,this.cancellationNotice=null),t.phase===`step`&&t.step.id!==this.stepId?this.value=w(t.step):t.phase!==`step`&&(this.value=void 0),this.stepId=t.phase===`step`?t.step.id:null,e.requestUpdate()},cancelledMessage:()=>l(`modelSetup.wizard.cancelled`)})}reset(){this.generation+=1,this.cancelling=!1,this.cancellationNotice=null,this.runner.cancel()}render(e={}){let t=this.runner.state;return h`<div -cancel=${e=>e.preventDefault()}>
${O({mode:e.mode??`auth`,state:t.phase===`step`?{...t,busy:t.busy||!!e.busy}:t,refreshWarning:e.refreshWarning??null,doneMessage:e.doneMessage,cancellationNotice:this.cancellationNotice,value:this.value,onValueChange:e=>{this.value=e,this.host.requestUpdate()},onAnswer:this.options.onAnswer??((e,t)=>void this.runner.answer(e,t)),onCancel:()=>void this.cancel(),onClose:this.options.onClose})}
</div>`}async cancel(){if(this.cancelling||this.runner.state.phase===`done`)return;let e=this.generation;this.cancelling=!0,this.cancellationNotice=null;try{let t=await this.runner.requestCancellation();if(e!==this.generation)return;t===`running`?this.cancellationNotice=l(`modelProviders.login.finishing`):t===`cancelled`&&this.options.onClose()}catch(t){e===this.generation&&(this.cancellationNotice=l(`modelSetup.wizard.cancelFailed`,{error:i(t,this.options.requestFailedMessage())}))}finally{e===this.generation&&(this.cancelling=!1,this.host.requestUpdate())}}}})))()}export{O as a,A as i,N as n,E as o,j as r,D as s,M as t};