openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.92 kB
JavaScript
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{Cl as t,Rl as n,Sl as r,Wo as i,al as a,ol as o,zl as s}from"./_4____________________________.js";var c,l,u,d;function f(){return(f=e((()=>{t(),n(),o(),c=1e4,l=r,u=class extends Error{},d=class{constructor(e){this.options=e,this.active=!0,this.proxyReady=!1,this.readyTimer=null,this.retryTimer=null,this.slowTimer=null,this.retryDelayMs=1e3,this.documentLoaded=!1,this.renderId=null,this.pendingDocument=null,this.activeLoad=null,queueMicrotask(()=>this.start())}get frame(){return this.options.frame}get ready(){return this.proxyReady}get loaded(){return this.documentLoaded}get rendered(){return this.loaded&&this.renderId===null}update(e){let t=this.options.frame!==e.frame||this.options.sandboxUrl!==e.sandboxUrl,n=this.options.documentKey!==e.documentKey;this.options=e,(t||n)&&this.reset(),t&&(this.proxyReady=!1),this.start()}setActive(e){e!==this.active&&(this.active=e,e?this.start():(this.clearReadyTimeout(),this.clearRetry(),this.cancelLoad()))}reset(){this.clearRetry(),this.cancelLoad(),this.clearReadyTimeout(),this.renderId=null,this.documentLoaded=!1,this.pendingDocument=null}dispose(){this.active=!1,this.reset(),this.proxyReady=!1}handleMessage(e){e.source===this.frame.contentWindow&&e.origin===this.options.sandboxOrigin&&(e.data?.method===`ui/notifications/sandbox-bootstrap-ready`&&e.data?.params?.version===1&&this.options.sandboxShellHtml!==void 0?this.frame.contentWindow?.postMessage({jsonrpc:`2.0`,method:`ui/notifications/sandbox-bootstrap-shell`,params:{html:this.options.sandboxShellHtml}},this.options.sandboxOrigin):e.data?.method===`ui/notifications/sandbox-resource-loaded`?this.renderId&&e.data?.params?.renderId===this.renderId&&(this.clearReadyTimeout(),this.renderId=null,this.options.onRendered?.()):e.data?.method===`ui/notifications/sandbox-proxy-ready`&&e.data?.params?.sandboxUrl===this.options.sandboxUrl&&(this.loaded&&this.options.onProxyReload&&(this.reset(),this.options.onProxyReload()),this.proxyReady=!0,this.clearRetry(),this.clearReadyTimeout(),this.start()))}handleFrameError(){this.active&&!this.proxyReady&&this.frame.isConnected&&(this.clearReadyTimeout(),this.retrySandboxFrame())}start(){this.active&&this.frame.isConnected&&this.retryTimer===null&&(this.scheduleReadyTimeout(),this.deliverDocument(),this.loadDocument())}clearReadyTimeout(){for(let e of[`slowTimer`,`readyTimer`])this[e]!==null&&(window.clearTimeout(this[e]),this[e]=null)}scheduleReadyTimeout(){(!this.proxyReady||this.renderId&&this.options.onRendered)&&this.readyTimer===null&&(this.slowTimer=window.setTimeout(()=>{this.slowTimer=null,this.active&&this.frame.isConnected&&this.options.onPending?.()},c),this.readyTimer=window.setTimeout(()=>{this.readyTimer=null,this.active&&!this.proxyReady&&this.frame.isConnected?this.retrySandboxFrame():this.active&&this.renderId&&this.frame.isConnected&&this.options.onError(new u(`Widget content did not finish loading. Reload the dashboard to try again.`))},l))}retrySandboxFrame(){this.proxyReady=!1,this.reset(),this.options.onReadyTimeout(),this.scheduleRetry(()=>{this.frame.src=this.options.sandboxUrl,this.start()})}clearRetry(){this.retryTimer!==null&&(window.clearTimeout(this.retryTimer),this.retryTimer=null)}scheduleRetry(e){this.active&&this.frame.isConnected&&this.retryTimer===null&&(this.retryTimer=window.setTimeout(()=>{this.retryTimer=null,this.active&&this.frame.isConnected&&e()},this.retryDelayMs),this.retryDelayMs=Math.min(this.retryDelayMs*2,3e4))}cancelLoad(){let e=this.activeLoad;this.activeLoad=null,e&&(window.clearTimeout(e.timeout),window.clearTimeout(e.notice),e.controller.abort())}async loadDocument(){let{loadDocument:e}=this.options;if(this.loaded||this.pendingDocument!==null||this.activeLoad!==null)return;let t=new AbortController,n={controller:t,notice:window.setTimeout(()=>{this.activeLoad===n&&this.options.onPending?.()},c),timeout:window.setTimeout(()=>t.abort(new DOMException(`The operation timed out.`,`TimeoutError`)),l)};this.activeLoad=n;try{let r=await s(()=>e(t.signal),t.signal,({reason:e})=>e instanceof Error?e:new DOMException(`Aborted`,`AbortError`));if(!this.active||this.activeLoad!==n||!this.frame.isConnected)return;this.pendingDocument=r,this.deliverDocument()}catch(e){if(this.activeLoad===n&&this.active&&this.frame.isConnected){let n=t.signal.aborted?t.signal.reason:e;this.options.onError(n),this.options.retryDocument?.(n)&&this.scheduleRetry(()=>this.start())}}finally{window.clearTimeout(n.timeout),window.clearTimeout(n.notice),this.activeLoad===n&&(this.activeLoad=null)}}deliverDocument(){let e=this.pendingDocument;this.active&&this.proxyReady&&e!==null&&this.frame.isConnected&&(this.renderId=a(),this.scheduleReadyTimeout(),this.frame.contentWindow?.postMessage({jsonrpc:`2.0`,method:`ui/notifications/sandbox-resource-ready`,params:{html:e,renderId:this.renderId,...this.options.allowScripts===!1?{allowScripts:!1}:{}}},this.options.sandboxOrigin),this.pendingDocument=null,this.documentLoaded=!0,this.retryDelayMs=1e3,this.options.onLoaded())}}})))()}function p(e,t,n){let r=m.get(e);if(!r){let t=new Map,n=new Set,a=new WeakMap;r={subscriptions:t,frames:n,nextOrder:0,stop:i(e,`message`,e=>{let r=e.source;if(!r)return;let i=a.get(r);if(!i||!n.has(i)||i.contentWindow!==r){i=void 0;for(let e of n)if(e.contentWindow===r){i=e,a.set(r,i);break}}let o=t.get(r),s=i&&t.get(i);if(!o&&!s)return;let c=o&&s?[...o,...s].toSorted((e,t)=>e.order-t.order):[...o??s];for(let t of c)if(o?.has(t)||s?.has(t)&&i?.contentWindow===r)try{t.callback(e)}catch(e){reportError(e)}})},m.set(e,r)}t instanceof HTMLIFrameElement&&r.frames.add(t);let a={callback:n,order:r.nextOrder++},o=r.subscriptions.get(t)??new Set;return r.subscriptions.set(t,o),o.add(a),()=>{!o.delete(a)||o.size>0||(r.subscriptions.delete(t),t instanceof HTMLIFrameElement&&r.frames.delete(t),r.subscriptions.size===0&&(r.stop(),m.delete(e)))}}var m;function h(){return(h=e((()=>{m=new WeakMap})))()}function g(e,t){let n=new URL(e||t,t);if(n.protocol===`ws:`?n.protocol=`http:`:n.protocol===`wss:`&&(n.protocol=`https:`),n.protocol!==`http:`&&n.protocol!==`https:`)throw Error(`Gateway URL is invalid`);return n.origin}function _(e,t,n,r,i,a=`Sandbox host URL is invalid`,o){if(!Number.isInteger(t)||t<1||t>65535)throw Error(a);let s;try{s=g(r,i)}catch{throw Error(a)}let c=n?new URL(n):new URL(s);if(n){if(c.origin!==n.replace(/\/$/u,``)||c.username!==``||c.password!==``)throw Error(a)}else c.port=String(t);c.pathname=`/`,c.search=``,c.hash=``;let l=new URL(e,c),u=o?e===o&&o.startsWith(`/__openclaw__/file-preview/`)&&l.pathname===o&&l.search===``&&l.hash===``:l.pathname===`/mcp-app-sandbox`;if(c.protocol!==`http:`&&c.protocol!==`https:`||c.origin===new URL(i).origin||c.origin===s||l.origin!==c.origin||!u)throw Error(a);return l.href}function v(){return(v=e((()=>{})))()}export{p as a,u as c,h as i,d as l,g as n,c as o,_ as r,l as s,v as t,f as u};