UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 7.87 kB
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./native-session-menu.runtime-Bhg9YfRh.js"])))=>i.map(i=>d[i]); import{n as e}from"./rolldown-runtime-8BhlS34s.js";import{Dl as t,Ec as n,Eu as r,Fs as i,Gl as a,Lc as o,Oc as s,Qs as c,Sl as l,Uc as u,Vl as d,Ys as f,_l as ee,js as te,ju as ne,lu as p,uu as m,wc as h}from"./control-ui-core-BncvAZjR.js";import{Ga as g,Ja as _,jo as re,mo as v}from"./control-ui-core-9qYjK5B2.js";import{Yr as y,_a as b,_t as ie,oa as x,sa as S}from"./ui-DuRgK6kL.js";import{C,M as w,_ as T,b as E,f as D,k as O,p as k,w as A}from"./config-runtime-BecGLziM.js";function j(e){let r=window,o=r.__OPENCLAW_NATIVE_CONVERSATION__,s=r.webkit?.messageHandlers?.openclawConversation;if(_()?.surface!==`conversation`||!b(o)||o.contract!==1||typeof s?.postMessage!=`function`)return null;let l={contract:1,documentId:crypto.randomUUID()},f=[`session-facts-v1`,`session-actions-v1`].filter(e=>Array.isArray(o.features)&&o.features.includes(e));r.__OPENCLAW_NATIVE_CONVERSATION_DOCUMENT__=l;let p=s.postMessage,g=p.bind(s),v=!1,S={visible:!0,active:!0},C,w,T=``,E=0,D=0,O=``,k=!1,A=Promise.resolve(),j=Promise.resolve(`accepted`),P=Promise.resolve(`rejected`),B=!1,H=new Set,U=new Set,W=new Set,G=()=>!v&&r.__OPENCLAW_NATIVE_CONVERSATION_DOCUMENT__===l&&r.webkit?.messageHandlers?.openclawConversation===s&&s.postMessage===p,K=(e,t,n=Date.now()+N)=>new Promise(r=>{let i=!1,a=new AbortController,o=e=>{i||(i=!0,clearTimeout(c),W.delete(s),a.abort(),r(e))},s=()=>o(t),c=setTimeout(s,Math.max(0,n-Date.now()));W.add(s),e(()=>!i&&G()&&Date.now()<n,a.signal).then(o,s)}),q=async e=>{if(!G())return`rejected`;try{let t=await g(e);return G()&&b(t)&&t.ok===!0?`accepted`:`rejected`}catch{return`rejected`}},J=(e,t,n=G)=>{let r=j;return j=K(async t=>(await r,t()&&n()?q(e):`timeout`),`timeout`,t),j},Y=(t,n)=>{if(!C||!G())return Promise.resolve(`rejected`);let{phase:r,lastErrorAuthReason:i}=e.gateway.snapshot,a=r===`connected`?`connected`:i?`signed-out`:r===`connecting`||r===`starting`||r===`reconnecting`?`connecting`:`offline`,o={...C,connection:a},s=JSON.stringify(o);if(s!==T||n&&B){T=s,B=!1;let e=J({...l,type:`state`,revision:++E,...o},t,n);P=e,e.then(t=>{P===e&&(B=t!==`accepted`)})}return P},ae=e=>{if(!G()||!f.includes(`session-facts-v1`))return;let t=V.safeParse(e?.filter(e=>e.agentId)),n={...l,type:`session-facts`,revision:D+1,sessions:t.success?t.data:null};new TextEncoder().encode(JSON.stringify(n)).length>65536&&(n.sessions=null);let r=JSON.stringify(n.sessions);r!==O&&(O=r,D++,J(n))},X=()=>{let n=e.router.getState().matches[0],r=n?.routeId===`chat`?n.data:void 0;if(n?.status===`success`&&r?.kind===`session`){let n=r.sessionKey,i={agentsList:e.agents.state.agentsList,hello:e.gateway.snapshot.hello,assistantAgentId:r.agentId??e.gateway.snapshot.assistantAgentId},o=d(i,n);if(!o)return;let s=e.sessions.presentation,l=s.result?.sessions.find(e=>a(i,n,e.key,e.agentId??(t(e.key)?s.agentId:void 0),o)),u=[...document.querySelectorAll(`openclaw-chat-page openclaw-chat-pane`)].find(e=>ee(e.sessionKey,n))?.runActivity,f=u?.client===e.gateway.snapshot.client&&u?.agentId===o&&u.working;C={context:{agentId:o,sessionKey:n},title:c(n,l),run:{active:f||(l?ie(l):!1)}}}if(!k&&C){let e=C.context;w&&(w.agentId!==e.agentId||w.sessionKey!==e.sessionKey)&&J({...l,type:`route-changed`,...e,reason:`other`}),w=e,Y()}},Z=t=>{let n=re(t.pathname,e.basePath);return!n||n===`chat`?!1:(J({...l,type:`open-dashboard`,path:t.pathname,...t.search?{search:t.search}:{}}).then(e=>{e!==`accepted`&&G()&&m({message:ne(`nativeConversation.openDashboardFailed`)})}),!0)},oe=async(t,n,r,i)=>{let o=F.safeParse(t);if(!o.success)return b(t)&&t.contract!==1?`unsupported`:`invalid-command`;let s=o.data;if(s.documentId!==l.documentId||!G())return`stale-document`;switch(s.type){case`navigate`:case`open-session-actions`:{let t=s.type===`open-session-actions`;if(t&&!f.includes(`session-actions-v1`))return`unsupported`;if(t&&(!S.visible||!S.active))return`unavailable`;let o=L.safeParse(s.payload);if(!o.success)return`invalid-command`;let c=u({context:e,face:`chat`,exactKey:!0,...o.data}).options;k=!0;try{await e.navigateAndWait(`chat`,c)}catch{return`navigate-rejected`}if(!n())return`navigate-timeout`;X();let l=()=>{let t=e.router.getState();return t.pendingMatches.length===0&&t.matches[0]?.routeId===`chat`&&t.matches[0]?.status===`success`&&b(t.matches[0]?.data)&&t.matches[0].data.kind===`session`&&C!==void 0&&a({agentsList:e.agents.state.agentsList,hello:e.gateway.snapshot.hello},C.context.sessionKey,o.data.sessionKey,o.data.agentId,C.context.agentId)};if(!l())return`navigate-rejected`;w=C?.context;let d=await Y(r,n);if(!n()||d===`timeout`)return`navigate-timeout`;if(d!==`accepted`||!l())return`navigate-rejected`;if(t){let e=document.querySelector(`openclaw-chat-page`);await e?.updateComplete;let t=e?.querySelector(`.chat-pane-cache__pane--active`);if(await t?.updateComplete,!n()||!l()||!t)return`unavailable`;let{openNativeSessionMenu:r}=await x(async()=>{let{openNativeSessionMenu:e}=await import(`./native-session-menu.runtime-Bhg9YfRh.js`);return{openNativeSessionMenu:e}},__vite__mapDeps([0]),import.meta.url);return await r({pane:t,signal:i,isCurrent:()=>n()&&l()})?void 0:`unavailable`}return}case`presentation`:{let e=R.safeParse(s.payload);if(!e.success)return`invalid-command`;S=e.data,U.forEach(e=>e());return}case`focus-composer`:{if(!z.safeParse(s.payload).success)return`invalid-command`;let e=document.querySelector(`openclaw-chat-pane.chat-pane-cache__pane--active .agent-chat__composer-combobox textarea`);if(!S.visible||!S.active||!e||e.disabled)return`unavailable`;e.focus({preventScroll:!0});return}default:return`unsupported`}},Q=e=>{let t=e instanceof CustomEvent?e.detail:null,n=I.safeParse(t);if(!n.success)return;let{documentId:r,requestId:i}=n.data,a=JSON.stringify([r,i]);if(H.has(a))return;H.add(a);let o=Date.now()+N,s=A;A=K(async(e,n)=>{if(await s,!e())return`navigate-timeout`;try{return await oe(t,e,o,n)}catch{return`command-failed`}},`navigate-timeout`,o).then(e=>{G()&&(K(()=>q({...l,type:`command-result`,documentId:r,requestId:i,ok:e===void 0,...e?{error:e}:{}}),`timeout`),k=!1,X())})},$=t=>{if(!i(t))return;let n=te(t);if(!n?.hasAttribute(`href`)||n.hasAttribute(`download`)||n.hasAttribute(`data-file-path`)||n.target&&n.target!==`_self`)return;let r=new URL(n.href,window.location.href);r.origin!==window.location.origin||e.basePath&&!r.pathname.startsWith(`${e.basePath}/`)||Z(r)&&t.preventDefault()};window.addEventListener(M,Q),document.addEventListener(`click`,$),J({...l,type:`ready`,surface:`conversation`,capabilities:[`navigate`,`presentation`,`focus-composer`,...f]}),document.addEventListener(n,X),document.addEventListener(h,X);let se=e.gateway.subscribe(X),ce=e.router.subscribe(X),le=e.sessions.subscribe(X);return X(),{supportsSessionActions:f.includes(`session-actions-v1`),get presentation(){return S},subscribe:e=>y(U,e),interceptNavigation:Z,publishSessionFacts:ae,dispose(){v=!0,W.forEach(e=>e()),document.removeEventListener(n,X),document.removeEventListener(h,X),se(),ce(),le(),U.clear(),window.removeEventListener(M,Q),document.removeEventListener(`click`,$),r.__OPENCLAW_NATIVE_CONVERSATION_DOCUMENT__===l&&delete r.__OPENCLAW_NATIVE_CONVERSATION_DOCUMENT__}}}var M,N,P,F,I,L,R,z,B,V;function H(){return(H=e((()=>{T(),v(),r(),f(),o(),l(),p(),s(),g(),S(),M=`openclaw:native-conversation-command`,N=15e3,P=O().min(1).refine(e=>e.trim()===e),F=A({contract:E(1),documentId:P,requestId:P,type:O(),payload:w()}),I=F.pick({documentId:!0,requestId:!0}),L=A({agentId:P,sessionKey:P}).strict(),R=A({visible:k(),active:k()}).strict(),z=A({}).strict(),B=P.refine(e=>new TextEncoder().encode(e).length<=4096),V=D(A({agentId:B,sessionKey:B,hasComposerDraft:k(),outboxAttentionCount:C().int().nonnegative().safe()}).strict()).max(64)})))()}H();export{j as createNativeConversationBridge};