UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 3.35 kB
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{$ as t,Gl as n,Gu as r,K as i,Ka as a,Z as o,q as s}from"./_4____________________________.js";import{JS as c,YS as l}from"./_2_____________________________.js";function u(e){let t=r(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((()=>{o(),l(),i(),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,r=e.snapshot;if(r.phase!==`connected`||!r.client){this.client&&(this.client=null,this.cancel());return}let i=n(e.connection.gatewayUrl)+` `+(r.selfUser?.id??``);i!==this.scope&&(this.cancel(),this.scope=i,this.client=null,this.settled=!1,this.pendingSince=null,this.publish({kind:`hidden`})),r.client!==this.client&&(this.client=r.client,t(r,`visitor-access.welcome.ensure`)!==!0||!s(r.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 t=this.epoch,r;try{r=u(await e.request(c,{}))}catch{t===this.epoch&&this.options.gateway.snapshot.client===e&&this.fail(null);return}if(t===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+n(this.options.gateway.connection.gatewayUrl)+`:`+encodeURIComponent(r.sessionKey);try{if(a()?.getItem(e)===`1`)return}catch{}let i=!1;try{i=await this.options.isHomeLanding()}catch{}if(!i||t!==this.epoch)return;let o=!1;try{let t=a();t?.setItem(e,`1`),o=t!==null}catch{}if(!o&&!r.created)return;this.options.openSession(r.sessionKey,r.agentId)}}}}}})))()}h();export{m as GuestWelcome};