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{Eu as t,Ji as n,Qi as r,Zi as i,dn as a,du as o,fn as s,ju as c,mn as l,pn as u,pu as d}from"./control-ui-core-BncvAZjR.js";import{Wr as f}from"./control-ui-core-9qYjK5B2.js";import{$ as p,X as m,Y as h}from"./lit-runtime-B8uOr6OX.js";import{Ft as g,Pt as _,Yi as v,Zi as y}from"./ui-DuRgK6kL.js";import{n as b,t as x}from"./en-settings-KEcvGj5Z.js";import{r as S,t as C}from"./wizard-step-controls-C7I6IQZe.js";import{f as w,o as T,s as E,t as D}from"./state-DYR1Gs_f.js";var O;function k(){return(k=e((()=>{v(),d(),n(),a(),g(),T(),O=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?u():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})}async startSession(e,t,n,r){let a=this.options.getClient();if(!a||this.currentState.phase!==`idle`)return null;let s=_(),c={client:a,sessionId:s,retirementGeneration:this.retirementGeneration,authChoice:e,authKind:this.pendingSignIn?.kind,notes:[],reservedWindow:this.pendingSignIn?.window,abortController:new AbortController,startMethod:t,activationTargetId:r,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=t===`mcp.authLogin`?null:this.options.getAgentId(),r=a.request(t,{sessionId:c.sessionId,...n,...e?{agentId:e}:{}},{timeoutMs:null}).catch(e=>{if(!i(e))throw e;return{done:!0,status:`not-admitted`,error:o(e,this.options.requestFailedMessage())}}),s=await this.awaitWizardStart(c,r);return s.done||(c.admitted=!0),c!==this.session&&!s.done?(await this.cancelSession(c),null):s.done?this.applyResult(c,s):await this.requestNext(c)}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(e={}){let t=this.session;this.clearSession(!e.settleActiveRequest),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(r(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(!r(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.clearSession()}clearSession(e=!0){this.pendingSignIn?.window?.close(),this.pendingSignIn=void 0,this.session?.reservedWindow?.close(),clearTimeout(this.session?.externalInputTimer),e&&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=t.then(async t=>(n&&(t.done?this.reportTerminalResult(e,t):await this.cancelSession(e)),t));return await y(r,D,()=>{throw n=!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=w(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(`
`)}})),clearTimeout(e.externalInputTimer),t.done&&(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=r(e);n?this.options.onSessionMissing?.():this.cancelSession(t);let i=n?this.options.sessionExpiredMessage():o(e,this.options.requestFailedMessage());this.setState({phase:`error`,message:[i,...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:D}).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 A(e){if(e.state.phase===`idle`)return m;let t=e.state.phase===`starting`||e.state.phase===`step`;return p`
<openclaw-modal-dialog
label=${c(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:c(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,e.state.phase===`starting`?e.state.notice:void 0].map(e=>e?p`<div class="callout warning" role="alert">${e}</div>`:m)}
${e.state.phase===`starting`?p`<div role="status">
${c(e.mode===`prepare`?`modelSetup.wizard.prepareStarting`:e.mode===`activate`?`modelSetup.wizard.checking`:`modelSetup.wizard.starting`)}
</div>`:e.state.phase===`done`?p`<div role="status">
${e.doneMessage??c(e.mode===`auth`?`modelSetup.wizard.connected`:`modelSetup.wizard.checking`)}
</div>`:e.state.phase===`error`||e.state.phase===`cancelled`?p`<div class="callout danger" role="alert">
${e.state.phase===`cancelled`||e.mode!==`auth`?e.state.message:c(`modelSetup.wizard.failed`)}
</div>
${e.state.phase===`error`&&e.mode===`auth`?p`<details>
<summary>${c(`modelSetup.wizard.details`)}</summary>
<p>${e.state.message}</p>
</details>`:m}`:p`
${e.state.validationError?p`<div
id="model-setup-wizard-validation-error"
class="callout danger"
role="alert"
>
${e.state.validationError}
</div>`:m}
${S({step:e.state.step,externalAuthInput:e.state.externalAuthInput,value:e.value,busy:e.state.busy,inputId:j,validationErrorId:e.state.validationError?`model-setup-wizard-validation-error`:void 0,confirmAffirmativeLabel:e.mode===`prepare`&&e.state.step.type===`confirm`?c(`modelSetup.wizard.continue`):void 0,leadingAction:p`<button
type="button"
class="btn"
=${e.onCancel}
>
${c(`common.cancel`)}
</button>`,onValueChange:e.onValueChange,onAnswer:e.onAnswer})}
${e.state.busy&&!e.state.step.externalUrl&&!e.state.step.deviceCode?p`<div role="status">${c(`modelSetup.wizard.working`)}</div>`:m}
`}
</div>
${e.state.phase===`step`?m:p`
<div class="model-setup-wizard__footer">
<button
type="button"
class="btn"
=${t?e.onCancel:e.onClose}
>
${c(t?`common.cancel`:`common.close`)}
</button>
</div>
`}
</div>
</openclaw-modal-dialog>
`}var j;function M(){return(M=e((()=>{h(),C(),t(),f(),j=`model-setup-wizard-text-input`})))()}function N(){return(N=e((()=>{})))()}var P;function F(){return(F=e((()=>{h(),t(),x(),d(),T(),k(),M(),b(),P=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 O({...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=E(t.step):t.phase!==`step`&&(this.value=void 0),this.stepId=t.phase===`step`?t.step.id:null,e.requestUpdate()},cancelledMessage:()=>c(`modelSetup.wizard.cancelled`),gatewayNotRespondingMessage:()=>c(`modelSetup.wizard.gatewayNotResponding`)})}reset(){this.generation+=1,this.cancelling=!1,this.cancellationNotice=null,this.runner.cancel()}render(e={}){let t=this.runner.state;return p`<div -cancel=${e=>e.preventDefault()}>
${A({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=c(`modelProviders.login.finishing`):t===`cancelled`&&this.options.onClose()}catch(t){e===this.generation&&(this.cancellationNotice=c(`modelSetup.wizard.cancelFailed`,{error:o(t,this.options.requestFailedMessage())}))}finally{e===this.generation&&(this.cancelling=!1,this.host.requestUpdate())}}}})))()}export{A as a,M as i,F as n,O as o,N as r,k as s,P as t};