UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

21 lines 6.09 kB
import{n as e}from"./rolldown-runtime-DkW27tQK.js";import{R as t,Un as n,wr as r,z as i}from"./control-ui-foundation-Cydpkyqa.js";import{Ct as a,Eo as o,Oo as s,Sc as c,St as l,yc as u}from"./control-ui-core-Rwzhj6nO.js";import{$ as d,K as f,X as p,_ as m,at as h,q as g,st as _,v}from"./lit-runtime-CV_MH99K.js";import{_ as y,g as b}from"./control-ui-core-YphAl4aM.js";import{Yt as x,en as S}from"./control-ui-core-vvcQQV2m.js";import{Fn as C,Pn as w,zn as T}from"./control-ui-boot-chat-_QSl8fbH.js";import{a as E,i as D,n as O}from"./control-ui-boot-chat-CazDfK3O.js";import{n as k,r as A,t as j}from"./widget-sandbox-host-BBp_TNUu.js";function M(e){let t=N.get(e.client);(!t||t.generation!==e.generation)&&(t={generation:e.generation,requests:new Map},N.set(e.client,t));let n=t.requests.get(e.docId);if(n)return n;let r=e.client.request(`canvas.document.view`,{docId:e.docId},{timeoutMs:j});if(t.requests.size<32){let n=t.requests;n.set(e.docId,r),r.finally(()=>n.delete(e.docId)).catch(()=>{})}return r}var N,P;function F(){return(F=e((()=>{i(),f(),d(),m(),y(),x(),a(),s(),A(),O(),c(),C(),N=new WeakMap,P=class extends u{constructor(...e){super(...e),this.docId=``,this.sessionKey=``,this.title=``,this.connectionGeneration=0,this.error=``,this.sandboxOrigin=``,this.scriptsAllowed=!0,this.sandboxGeneration=0,this.handleMessage=e=>{let t=this.sandboxHost,r=this.binding;if(!t||!this.isCurrent(r)||e.source!==t.frame.contentWindow||e.origin!==this.sandboxOrigin)return;t.handleMessage(e);let i=n(e.data);if(i?.type===`openclaw:widget-size`&&typeof i.height==`number`&&Number.isFinite(i.height)&&i.height>0&&(this.contentHeight=Math.min(8e3,Math.max(48,Math.trunc(i.height)))),i?.type===`openclaw:widget-bridge-ready`&&this.postHostState(),i?.type!==`openclaw:widget-prompt-offer`){i?.type===`openclaw:widget-bridge-port-offer`&&e.ports[0]?.close();return}let a=e.ports[0];if(!a||this.promptPort||!t.loaded){a?.close();return}this.promptPort=a,a.addEventListener(`message`,e=>{this.isCurrent(r)&&this.promptPort===a&&e.data?.type===`openclaw:widget-prompt`&&w(t.frame,e.data.prompt,`${this.sessionKey}\0${r.docId}\0${r.generation}`)}),a.start(),a.postMessage({type:`openclaw:widget-prompt-host-ready`})}}get allowScripts(){return this.scriptsAllowed}set allowScripts(e){e!==this.scriptsAllowed&&(this.clearSandbox(),this.sandboxGeneration+=1,this.view&&(this.error=``),this.scriptsAllowed=e)}get documentHtml(){return this.isCurrent(this.binding)?this.view?.html:void 0}connectedCallback(){super.connectedCallback(),window.addEventListener(`message`,this.handleMessage)}disconnectedCallback(){window.removeEventListener(`message`,this.handleMessage),this.clearView(),super.disconnectedCallback()}clearView(){this.binding=void 0,this.view=void 0,this.clearSandbox()}clearSandbox(){this.sandboxHost?.dispose(),this.sandboxHost=void 0,this.promptPort?.close(),this.promptPort=void 0,this.releaseTheme?.(),this.releaseTheme=void 0,this.contentHeight=void 0}isCurrent(e){return!!(e&&this.isConnected&&this.binding===e&&e.client===this.context?.gateway.snapshot.client&&e.docId===this.docId&&e.generation===l())}willUpdate(){let e=this.context?.gateway.snapshot.client;if(!e||!this.docId){this.clearView();return}if(this.binding?.client===e&&this.binding.docId===this.docId&&this.binding.generation===this.connectionGeneration)return;this.clearView(),this.error=``;let t={client:e,docId:this.docId,generation:this.connectionGeneration};this.binding=t,M(t).then(e=>{this.isCurrent(t)&&(this.view=e)}).catch(e=>{this.isCurrent(t)&&(this.error=o(e))})}updated(){let e=this.querySelector(`iframe`),t=this.view,n=this.binding;!this.allowScripts||!e||!t||!this.isCurrent(n)||this.sandboxHost||(this.releaseTheme=E(e,this.sandboxOrigin),this.sandboxHost=new k({frame:e,sandboxOrigin:this.sandboxOrigin,sandboxUrl:e.src,documentKey:`${n.docId}\0${n.generation}`,loadDocument:async()=>t.html,onLoaded:()=>this.postHostState(),onError:e=>this.fail(e),onReadyTimeout:()=>this.fail(Error(S(`board.widget.sandboxUnavailable`)))}))}fail(e){this.clearSandbox(),this.error=o(e)}postHostState(){let e=this.sandboxHost?.frame;e&&(D(e,this.sandboxOrigin),e.contentWindow?.postMessage({type:`openclaw:widget-chat-host`},this.sandboxOrigin))}render(){if(this.error)return p`<div class="board-widget__error" role="alert"> ${this.error} <button class="btn btn--small" @click=${()=>{this.clearView(),this.requestUpdate()}} > ${S(`common.retry`)} </button> </div>`;if(!this.view||!this.context)return p`<div role="status" style=${`min-height:${this.preferredHeight??420}px`}> ${S(`common.loading`)} </div>`;let e;try{this.allowScripts&&(e=T(this.view.sandboxUrl,this.view.sandboxPort,this.view.sandboxOrigin,this.context.gateway.connection.gatewayUrl,window.location.origin),this.sandboxOrigin=new URL(e).origin)}catch(e){return p`<div role="alert">${o(e)}</div>`}let t=this.contentHeight??this.preferredHeight;return v(this.sandboxGeneration,p`<iframe class="chat-tool-card__preview-frame" title=${this.title} src=${e??g} srcdoc=${this.allowScripts?g:this.view.html} sandbox=${this.allowScripts?`allow-scripts allow-same-origin allow-forms`:``} referrerpolicy="origin" style=${t?`height:${t}px;min-height:${t}px`:g} @error=${()=>this.sandboxHost?.handleFrameError()} ></iframe>`)}},r([t({context:b,subscribe:!0})],P.prototype,`context`,void 0),r([_()],P.prototype,`docId`,void 0),r([_()],P.prototype,`sessionKey`,void 0),r([_()],P.prototype,`title`,void 0),r([_({type:Number})],P.prototype,`preferredHeight`,void 0),r([_({type:Number})],P.prototype,`connectionGeneration`,void 0),r([h()],P.prototype,`view`,void 0),r([h()],P.prototype,`error`,void 0),r([h()],P.prototype,`contentHeight`,void 0),r([_({type:Boolean})],P.prototype,`allowScripts`,null),customElements.get(`openclaw-canvas-widget-view`)||customElements.define(`openclaw-canvas-widget-view`,P)})))()}F();export{P as OpenClawCanvasWidgetView}; //# sourceMappingURL=canvas-widget-view-CXTKpoEk.js.map