openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.34 kB
JavaScript
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{Wo as t}from"./_4____________________________.js";import{Ek as n,GE as r,JE as i,NS as a,ak as o,kk as s,ok as c}from"./_2_____________________________.js";import{B as l,P as u,U as d,ht as f}from"./_6____________________.js";var p;function m(){return(m=e((()=>{c(),n(),p=class{constructor(e,t){this.notify=e,this.options=t,this.lifecycle=a({client:null,phase:`stopped`}),this.stopActivation=[],this.currentGateway=null,this.currentSnapshot=null,this.currentClient=null,this.currentConnected=!1,this.hasBoundGateway=!1}connect(e=this.options.getGateway()){this.options.onPageActivation&&(this.stopActivation=[t(document,`visibilitychange`,this.options.onPageActivation),t(globalThis,`focus`,this.options.onPageActivation)]),this.refresh(e)}refresh(e=this.options.getGateway()){let t=e??null;if(this.hasBoundGateway&&t===this.currentGateway)return;this.stopGateway?.(),this.stopGateway=void 0;let n=!this.hasBoundGateway,r=this.hasBoundGateway&&this.currentGateway!==t;this.hasBoundGateway=!0,this.currentGateway=t,t&&(this.applySnapshot(t.snapshot,{initial:n,sourceChanged:r}),this.stopGateway=t.subscribe(e=>{this.currentGateway===t&&this.options.getGateway()===t&&this.applySnapshot(e,{initial:!1,sourceChanged:!1})}))}get gateway(){return this.currentGateway}get snapshot(){return this.currentSnapshot}get client(){return this.currentClient}get connected(){return this.currentConnected}get epoch(){return this.lifecycle.epoch}capture(){return this.lifecycle.capture()}isCurrent(e){return this.lifecycle.isCurrent(e)}invalidate(){this.lifecycle.invalidate()}isRouteDataCurrent(e){let t=this.options.getGateway();if(!t||e.gateway!==t)return!1;let n=t.snapshot;return e.gatewaySnapshot===n||e.gatewaySnapshot.phase===`connected`&&n.phase===`connected`&&e.gatewaySnapshot.client===n.client&&e.gatewaySnapshot.hello!==null&&e.gatewaySnapshot.hello===n.hello}dispose(){this.stopActivation.forEach(e=>e()),this.stopActivation=[],this.stopGateway?.(),this.stopGateway=void 0;let e=this.currentSnapshot;if(this.currentGateway=null,e){let t={...e,client:null,phase:`stopped`},n=this.lifecycle.transition(t),r=this.currentClient!==null,i=this.currentConnected;this.currentSnapshot=null,this.currentClient=null,this.currentConnected=!1,n&&this.options.invalidateRequests?.({snapshot:t,initial:!1,sourceChanged:!1,clientChanged:r,connectionChanged:i,identityChanged:!1,becameConnected:!1,becameAvailable:!1})}else this.lifecycle.invalidate();this.notify()}applySnapshot(e,t){let n=this.currentClient,r=this.currentConnected,i=this.currentSnapshot!==null&&s(this.currentSnapshot),a=e.phase===`connected`,c=n!==e.client,l=r!==a,u=this.currentGateway?o(this.currentGateway).key:void 0,d=!t.initial&&(t.sourceChanged||c||this.currentPresentationKey!==void 0&&this.currentPresentationKey!==u),f=this.lifecycle.transition(e);d&&!f&&this.lifecycle.invalidate(),this.currentSnapshot=e,this.currentClient=e.client,this.currentConnected=a,this.currentPresentationKey=u;let p={snapshot:e,initial:t.initial,sourceChanged:t.sourceChanged,clientChanged:c,connectionChanged:l,identityChanged:d,becameConnected:a&&!r,becameAvailable:s(e)&&!i};p.identityChanged&&this.options.onIdentityChange?.(p),!t.initial&&(f||d)&&this.options.invalidateRequests?.(p),this.options.onSnapshot?.(p),a&&(t.initial||p.identityChanged||p.connectionChanged)&&this.options.ensureInitialData?.(p),this.notify()}}})))()}async function h(e){if(e.isCurrent&&!e.isCurrent())return{results:[],sessions:[]};let{limit:t,offset:n,includeDerivedTitles:i,includeLastMessage:a,ownerFirst:o,rowMode:s,source:c,...l}=r(e.listOptions);return e.client.request(`sessions.search`,{query:e.query,limit:25,scope:l})}function g(){return(g=e((()=>{i()})))()}function _(e){let[t,n]=l(0,{ownedWrite:!0}),r=new p(()=>n(e=>e+1),e),i=!1;return u(e.getGateway,e=>{i?r.refresh(e??null):(i=!0,r.connect(e??null))}),f(()=>r.dispose()),{get gateway(){return t(),r.gateway},get snapshot(){return t(),r.snapshot},get client(){return t(),r.client},get connected(){return t(),r.connected},get epoch(){return t(),r.epoch},readEpoch:()=>r.epoch,capture:()=>r.capture(),isCurrent:e=>r.isCurrent(e),invalidate:()=>r.invalidate(),isRouteDataCurrent:e=>r.isRouteDataCurrent(e)}}function v(){return(v=e((()=>{d(),m()})))()}export{p as a,h as i,_ as n,m as o,g as r,v as t};