UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 3.36 kB
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{Aa as t,It as n,Mt as r,Rt as i,jt as a,nu as o,tl as s}from"./control-ui-connection-Bfj9beF_.js";import{eS as c,tS as l}from"./control-ui-boot-shared-DFTSUQQ0.js";function u(e){let t=o(e);if(!t)return null;switch(t.status){case`not_applicable`:return{status:`not_applicable`};case`pending`:return{status:`pending`,retryAfterMs:typeof t.retryAfterMs==`number`&&Number.isFinite(t.retryAfterMs)?t.retryAfterMs:d};case`ready`:return typeof t.sessionKey==`string`&&t.sessionKey.trim()&&typeof t.agentId==`string`&&t.agentId.trim()?{status:`ready`,sessionKey:t.sessionKey,agentId:t.agentId,created:t.created===!0}:null;case`failed`:return{status:`failed`,message:typeof t.message==`string`?t.message.trim():``};default:return null}}var d,f,p,m;function h(){return(h=e((()=>{n(),l(),a(),d=500,f=5e3,p=`openclaw.guestWelcome.visited.v1:`,m=class{constructor(e){this.options=e,this.current={kind:`hidden`},this.listeners=new Set,this.scope=null,this.client=null,this.epoch=0,this.pendingSince=null,this.settled=!1}get presentation(){return this.current}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}start(){let e=this.options.gateway.subscribe(()=>this.synchronize());return this.synchronize(),()=>{e(),this.cancel(),this.client=null}}retry(){this.client&&this.current.kind===`failed`&&(this.pendingSince=null,this.publish({kind:`pending`}),this.ensure())}publish(e){let t=this.current;if(t.kind!==e.kind||t.kind===`failed`&&e.kind===`failed`&&t.message!==e.message){this.current=e;for(let e of this.listeners)e()}}cancel(){this.epoch+=1,this.timer!==void 0&&(clearTimeout(this.timer),this.timer=void 0)}synchronize(){let{gateway:e}=this.options,t=e.snapshot;if(t.phase!==`connected`||!t.client){this.client&&(this.client=null,this.cancel());return}let n=s(e.connection.gatewayUrl)+` `+(t.selfUser?.id??``);n!==this.scope&&(this.cancel(),this.scope=n,this.client=null,this.settled=!1,this.pendingSince=null,this.publish({kind:`hidden`})),t.client!==this.client&&(this.client=t.client,i(t,`visitor-access.welcome.ensure`)!==!0||!r(t.hello?.auth??null)?this.publish({kind:`hidden`}):this.settled||this.ensure())}fail(e){this.pendingSince=null,this.publish({kind:`failed`,message:e})}async ensure(){let e=this.client;if(!e)return;this.cancel();let n=this.epoch,r;try{r=u(await e.request(c,{}))}catch{n===this.epoch&&this.options.gateway.snapshot.client===e&&this.fail(null);return}if(n===this.epoch){if(!r)this.fail(null);else switch(r.status){case`not_applicable`:this.settled=!0,this.publish({kind:`hidden`});return;case`pending`:{let e=Date.now();if(this.pendingSince??=e,e-this.pendingSince>=18e4){this.fail(null);return}this.publish({kind:`pending`}),this.timer=setTimeout(()=>{this.timer=void 0,this.ensure()},Math.min(f,Math.max(d,r.retryAfterMs)));return}case`failed`:this.fail(r.message||null);return;case`ready`:{this.settled=!0,this.pendingSince=null,this.publish({kind:`hidden`});let e=p+s(this.options.gateway.connection.gatewayUrl)+`:`+encodeURIComponent(r.sessionKey);try{if(t()?.getItem(e)===`1`)return}catch{}let i=!1;try{i=await this.options.isHomeLanding()}catch{}if(!i||n!==this.epoch)return;let a=!1;try{let n=t();n?.setItem(e,`1`),a=n!==null}catch{}if(!a&&!r.created)return;this.options.openSession(r.sessionKey,r.agentId)}}}}}})))()}h();export{m as GuestWelcome};