UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines 11.5 kB
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./ghostty-web-BGvE7Q2R.js","./rolldown-runtime-DkW27tQK.js","./control-ui-foundation-Cydpkyqa.js","./control-ui-core-Rwzhj6nO.js","./lit-runtime-CV_MH99K.js","./control-ui-core-YphAl4aM.js","./control-ui-core-vvcQQV2m.js","./gateway-runtime-Bqqcp9-I.js","./control-ui-core-ClxHIvNM.css"])))=>i.map(i=>d[i]); import{n as e}from"./rolldown-runtime-DkW27tQK.js";import{ii as t,ri as n}from"./control-ui-foundation-Cydpkyqa.js";function r(e){if(!Number.isSafeInteger(e.columns)||!Number.isSafeInteger(e.rows)||e.columns<1||e.rows<1||e.columns>65535||e.rows>65535)throw new i(`invalid_terminal_size`,`terminal size must be integer columns and rows between 1 and 65535`);return e}var i;function a(){return(a=e((()=>{i=class extends Error{code;cause;constructor(e,t,n){super(t),this.name=`LibterminalError`,this.code=e,this.cause=n?.cause}}})))()}function o(e,t){let n=e.sessionId??``,r=p.encode(n),i=e.payload??new Uint8Array,a=t?.maxSessionIdBytes??4096,o=t?.maxFrameBytes??1048576;if(r.byteLength>a)throw l(`session id exceeds ${a} bytes`);if(!u(e.type))throw l(`terminal frame message type ${String(e.type)} is unsupported`);let s=8+r.length+4+i.length;if(s>o)throw l(`terminal frame exceeds ${o} bytes`);let c=new Uint8Array(s),f=new DataView(c.buffer,c.byteOffset,c.byteLength),m=0;return f.setUint16(m,d,!0),m+=2,f.setUint8(m,2),m+=1,f.setUint8(m,e.type),m+=1,f.setUint32(m,r.length,!0),m+=4,c.set(r,m),m+=r.length,f.setUint32(m,i.length,!0),m+=4,c.set(i,m),c}function s(e,t){let n=t?.maxFrameBytes??1048576,r=t?.maxSessionIdBytes??4096;if(e.byteLength>n)throw l(`terminal frame exceeds ${n} bytes`);if(e.byteLength<12)throw l(`terminal frame is shorter than its fixed header`);let a=new DataView(e.buffer,e.byteOffset,e.byteLength),o=0;if(a.getUint16(o,!0)!==22851)throw l(`terminal frame has invalid magic`);o+=2;let s=a.getUint8(o);if(s!==2)throw new i(`unsupported_protocol`,`terminal protocol version ${s} is unsupported`);o+=1;let c=a.getUint8(o);if(!u(c))throw l(`terminal frame message type ${c} is unsupported`);o+=1;let d=a.getUint32(o,!0);if(o+=4,d>r)throw l(`session id exceeds ${r} bytes`);if(o+d+4>e.byteLength)throw l(`terminal frame session id length exceeds frame length`);let f;try{f=m.decode(e.subarray(o,o+d))}catch(e){throw new i(`invalid_frame`,`terminal frame session id is not UTF-8`,{cause:e})}o+=d;let p=a.getUint32(o,!0);if(o+=4,o+p!==e.byteLength)throw l(`terminal frame payload length does not match frame length`);return{type:c,sessionId:f,payload:e.subarray(o,o+p)}}function c(e,t){try{return s(e,t)}catch{return null}}function l(e){return new i(`invalid_frame`,e)}function u(e){return h.has(e)}var d,f,p,m,h;function g(){return(g=e((()=>{a(),d=22851,f={Hello:1,Welcome:2,Subscribe:10,Unsubscribe:11,Output:20,Snapshot:21,Event:22,Error:23,Input:30,Key:31,Resize:32,Stop:33,ControlRequest:50,ControlDecision:51,ControlGranted:52,ControlRevoked:53,Ping:60,Pong:61,Ack:62},p=new TextEncoder,m=new TextDecoder(`utf-8`,{fatal:!0}),h=new Set(Object.values(f))})))()}function _(e,t){for(let n=t;n<e.length;n+=1){if(e[n]===N)return{kind:`terminator`,index:n,terminator:N};if(e[n]===P||e[n]===F)return{kind:`cancel`,index:n};if(e[n]===M&&e[n+1]===`\\`)return{kind:`terminator`,index:n,terminator:I};if(e[n]===M&&e[n+1]===`]`)return{kind:`restart`,index:n}}return null}function v(e,t,n){let r=/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/iu.exec(t);if(!r)return null;let[,i,a,o]=r;return`${M}]${e};rgb:${i}${i}/${a}${a}/${o}${o}${n}`}function y(e){let t=``,n=!1,r=[],i=()=>{if(n)return;n=!0;let t=0;try{for(;t<r.length;)e.reply(r[t]),t+=1}finally{r.splice(0,t),n=!1}},a=(n,a)=>{for(t+=n;t.length>0;){let n=t.indexOf(`${M}]`);if(n===-1){t=t.endsWith(M)?M:``;break}n>0&&(t=t.slice(n));let i=z.find(({sequence:e})=>t.startsWith(e));if(!i){if(z.some(({sequence:e})=>e.startsWith(t)))break;t=t.slice(1);continue}let o=_(t,i.sequence.length);if(!o){if(t.length<=L)break;t=t.slice(1);continue}if(o.kind===`restart`){t=t.slice(o.index);continue}if(o.kind===`cancel`){t=t.slice(o.index+1);continue}if(a){let n=e.getColors(),a=i.slot,s=t.slice(i.sequence.length,o.index);for(let e of s.split(`;`)){if(e){let t=R.find(e=>e.slot===a);if(e===`?`&&t){let e=v(a,n[t.color],o.terminator);e&&r.push(e)}}a+=1}}t=t.slice(o.index+o.terminator.length)}a&&i()};return{observe(e){a(e,!0)},primeFromReplay(e){t=``,a(e,!1)}}}async function b(e){if(e?.module)return T(e.module,e.wasmUrl);let t=e?.wasmUrl??`<default>`,r=V.get(t);r||(r=n(()=>import(`./ghostty-web-BGvE7Q2R.js`).then(t=>T(t,e?.wasmUrl)),__vite__mapDeps([0,1,2,3,4,5,6,7,8]),import.meta.url),V.set(t,r));try{return await r}catch(e){throw V.delete(t),new i(`ghostty_unavailable`,`failed to load Ghostty browser runtime`,{cause:e})}}async function x(e){e.onStatus?.({state:`loading`}),D(e.signal);let t,n;try{let r=e.runtime??await b(e.runtimeOptions);D(e.signal);let i=e.readOnly??!0;t=new r.Terminal({...e.terminalOptions,disableStdin:i,ghostty:r.ghostty});let a=new r.FitAddon;return t.loadAddon(a),t.open(e.parent),n=new G(t,a,i,e),n.start(),e.onStatus?.({state:`ready`}),n}catch(r){throw n?n.dispose():t?.dispose(),O(e.signal,r)||(e.onStatus?.({state:`error`,error:r}),r instanceof i)?r:new i(`ghostty_unavailable`,`failed to create Ghostty browser terminal`,{cause:r})}}async function S(e,t,n){let r=t[Symbol.asyncIterator](),i=E(n),a=!1,o=!1,s;try{for(;;){let t=i?await Promise.race([r.next(),i.promise]):await r.next();if(t===K||t.done){a=t!==K;return}e.write(t.value)}}catch(e){o=!0,s=e}finally{if(i?.dispose(),!a){let e=r.return?.();if(n?.aborted)Promise.resolve(e).catch(A);else try{await e}catch(e){o||(o=!0,s=e)}}}if(o)throw s}function C(e){return new WebSocket(e)}async function w(e){return e instanceof Uint8Array?e.slice():e instanceof ArrayBuffer?new Uint8Array(e):ArrayBuffer.isView(e)?new Uint8Array(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)):typeof Blob<`u`&&e instanceof Blob||j(e)?new Uint8Array(await e.arrayBuffer()):B.encode(String(e))}async function T(e,t){try{return{ghostty:await e.Ghostty.load(t),Terminal:e.Terminal,FitAddon:e.FitAddon}}catch(e){throw new i(`ghostty_unavailable`,`failed to load Ghostty WASM`,{cause:e})}}function E(e){if(!e)return;if(e.aborted)return{promise:Promise.resolve(K),dispose:()=>void 0};let t=A,n=new Promise(e=>{t=e}),r=()=>t(K);return e.addEventListener(`abort`,r,{once:!0}),{promise:n,dispose:()=>e.removeEventListener(`abort`,r)}}function D(e){if(e?.aborted)throw e.reason??new DOMException(`The operation was aborted`,`AbortError`)}function O(e,t){return e?.aborted?t===e.reason||e.reason===void 0&&t instanceof DOMException&&t.name===`AbortError`:!1}function k(e,t){if(!t)return{signal:e,dispose:()=>void 0};let n=new AbortController,r=()=>n.abort(e.reason),i=()=>n.abort(t.reason);return e.aborted?r():t.aborted?i():(e.addEventListener(`abort`,r,{once:!0}),t.addEventListener(`abort`,i,{once:!0})),{signal:n.signal,dispose:()=>{e.removeEventListener(`abort`,r),t.removeEventListener(`abort`,i)}}}function A(){}function j(e){return typeof e==`object`&&!!e&&`arrayBuffer`in e&&typeof e.arrayBuffer==`function`}var M,N,P,F,I,L,R,z,B,V,H,U,W,G,K;function q(){return(q=e((()=>{a(),g(),t(),M=`\x1B`,N=`\x07`,P=``,F=``,I=`${M}\\`,L=1024,R=[{color:`foreground`,slot:10},{color:`background`,slot:11},{color:`cursor`,slot:12}],z=R.map(({slot:e})=>({sequence:`${M}]${e};`,slot:e})),B=new TextEncoder,V=new Map,H=2,U=1,W=class{options;socket;reconnectTimer;closedByCaller=!1;constructor(e){this.options=e}get isOpen(){return this.socket?.readyState===U}connect(){if(this.closedByCaller=!1,this.socket&&this.socket.readyState<H)return;this.clearReconnectTimer();let e;try{e=(this.options.socketFactory??C)(this.resolveUrl())}catch(e){this.reportError(e),this.scheduleReconnect();return}this.socket=e;let t=Promise.resolve();e.binaryType=`arraybuffer`,e.addEventListener(`open`,this.handleOpen(e)),e.addEventListener(`message`,this.handleMessage(e,e=>{t=t.catch(A).then(e).catch(e=>this.reportError(e))})),e.addEventListener(`close`,this.handleClose(e)),e.addEventListener(`error`,this.handleError(e))}send(e){let t=this.socket;if(!t||t.readyState!==U)return!1;try{return t.send(o(e,this.options.frameLimits)),!0}catch(e){return this.reportError(e),!1}}close(e=1e3,t=`terminal hub closed`){this.closedByCaller=!0,this.clearReconnectTimer();let n=this.socket;if(!(!n||n.readyState>=H))try{n.close(e,t)}catch(e){this.socket=void 0,this.reportError(e)}}handleOpen(e){return()=>{this.socket===e&&(this.send({type:f.Hello}),this.notify(()=>this.options.onOpen?.()))}}handleMessage(e,t){return n=>{t(async()=>{if(this.socket!==e)return;let t=c(await w(n.data),this.options.frameLimits);t&&this.socket===e&&this.notify(()=>this.options.onFrame?.(t))})}}handleClose(e){return t=>{this.socket===e&&(this.socket=void 0,this.notify(()=>this.options.onClose?.(t)),this.scheduleReconnect())}}handleError(e){return()=>{this.socket===e&&this.reportError()}}resolveUrl(){return typeof this.options.url==`function`?this.options.url():this.options.url}scheduleReconnect(){this.closedByCaller||this.reconnectTimer||!this.shouldReconnect()||(this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=void 0,this.connect()},this.options.reconnectDelayMs??1500))}shouldReconnect(){try{return!!this.options.shouldReconnect?.()}catch(e){return this.reportError(e),!1}}clearReconnectTimer(){this.reconnectTimer&&=(clearTimeout(this.reconnectTimer),void 0)}notify(e){try{e()}catch(e){this.reportError(e)}}reportError(e){try{this.options.onError?.(e)}catch{}}},G=class{terminal;fitAddon;options;inputSubscription;resizeSubscription;abortListener;disposed=!1;currentReadOnly;disposeController=new AbortController;constructor(e,t,n,r){this.terminal=e,this.fitAddon=t,this.currentReadOnly=n,this.options=r}get readOnly(){return this.currentReadOnly}start(){this.inputSubscription=this.terminal.onData(e=>{this.currentReadOnly||this.options.onData?.(B.encode(e))}),this.resizeSubscription=this.terminal.onResize(({cols:e,rows:t})=>{this.options.onResize?.({columns:e,rows:t})}),this.options.size?this.resize(this.options.size):this.options.autoFit!==!1&&this.fitAddon.fit(),this.options.autoFit!==!1&&this.fitAddon.observeResize();let e=this.options.signal;e&&(this.abortListener=()=>this.dispose(),e.addEventListener(`abort`,this.abortListener,{once:!0}),e.aborted&&this.abortListener())}write(e){this.assertOpen(),this.terminal.write(e)}resize(e){this.assertOpen(),r(e),this.terminal.resize(e.columns,e.rows)}fit(){this.assertOpen(),this.fitAddon.fit()}setReadOnly(e){this.assertOpen(),this.currentReadOnly=e,this.terminal.options.disableStdin=e}async attach(e,t){this.assertOpen();let n=k(this.disposeController.signal,t??this.options.signal);try{await S(this.terminal,e,n.signal),this.options.onStatus?.({state:`ended`})}catch(e){throw this.options.onStatus?.({state:`error`,error:e}),e}finally{n.dispose()}}dispose(){this.disposed||(this.disposed=!0,this.disposeController.abort(`terminal disposed`),this.abortListener&&=(this.options.signal?.removeEventListener(`abort`,this.abortListener),void 0),this.inputSubscription?.dispose(),this.resizeSubscription?.dispose(),this.fitAddon.dispose(),this.terminal.dispose())}assertOpen(){if(this.disposed)throw new i(`transport_closed`,`Ghostty browser terminal is disposed`)}},K=Symbol(`aborted`)})))()}q();export{W as TerminalHubClient,S as attachTerminalStream,x as createGhosttyTerminal,y as createTerminalDefaultColorQueryResponder,b as loadGhosttyRuntime}; //# sourceMappingURL=browser-CZlUTVHo.js.map