decocms
Version:
Deco CMS — Self-hostable MCP Gateway for managing AI connections and tools
3 lines (2 loc) • 5.87 kB
JavaScript
import{r as a,c as he,j as G,a6 as Ee,q as ve,K as C,cL as J}from"./index-DoEtSiIe.js";import{h as pe,u as me}from"./chat-context-vJ6R-Clt.js";import{b as Se}from"./sandbox-url-D3mC8qhT.js";import{b as ge}from"./repository-binding-BAQCDq-W.js";import{e as ye}from"./backoff-CrEUbyZT.js";function W(t){return t==="installing"||t==="install-failed"||t==="starting"||t==="start-failed"||t==="running"||t==="crashed"}const be={phase:null,lifecycle:{phase:"idle"},status:{state:"running"},branch:{kind:"unknown"},notFound:!1,scripts:[],activeProcesses:[],getBuffer:()=>"",hasData:()=>!1,subscribeChunks:()=>()=>{},subscribeReload:()=>()=>{}},Z=a.createContext(be),z=16384;class De{data="";append(e){this.data+=e,this.data.length>z&&(this.data=this.data.slice(this.data.length-z))}get(){return this.data}clear(){this.data=""}}const ke=1e3,Re=3e4,Te=3e3,X=["lifecycle","status","tasks","scripts","branch","reload","file-changed"],I="log";function xe(t){if(!t)return null;try{return new URL("/_sandbox/events",t).href}catch{return null}}function Ce(t){return t===404}function Le(t){return/(?:^|[\s;&|(])deno\s+task\b/.test(t)}async function _e(t){try{const e=await fetch(t,{headers:{Accept:"text/event-stream"},cache:"no-store",signal:AbortSignal.timeout(Te)});return await e.body?.cancel().catch(()=>{}),Ce(e.status)}catch{return!1}}const ee=pe(Z);function Me(t){const e=he.c(10);let i,f,c;e[0]!==t?({resetKey:c,children:i,...f}=t,e[0]=t,e[1]=i,e[2]=f,e[3]=c):(i=e[1],f=e[2],c=e[3]);let o;e[4]!==f||e[5]!==c?(o=G.jsx(we,{...f},c),e[4]=f,e[5]=c,e[6]=o):o=e[6];let h;return e[7]!==i||e[8]!==o?(h=G.jsx(ee.Host,{source:o,children:i}),e[7]=i,e[8]=o,e[9]=h):h=e[9],h}function we(t){return ee.usePublish(Pe(t)),null}function Pe({virtualMcpId:t,branch:e,previewUrl:i=null,enabled:f=!0}){const{org:c}=Ee(),o=ve(),h=me()?.taskId??null,[ne,L]=a.useState(null),[se,_]=a.useState({phase:"idle"}),[re,w]=a.useState({state:"running"}),[ae,P]=a.useState({kind:"unknown"}),[ce,O]=a.useState(!1),[oe,N]=a.useState([]),[ue,B]=a.useState([]),[,ie]=a.useState(0),m=a.useRef(new Map),K=a.useRef(new Set),k=a.useRef(new Set),A=a.useRef(null),R=xe(i),Q=ge(t)?.metadata?.runtime?.selected==="deno",le=l=>{let d=m.current.get(l);return d||(d=new De,m.current.set(l,d)),d};return a.useEffect(()=>{if(L(null),_({phase:"idle"}),w({state:"running"}),P({kind:"unknown"}),A.current=null,O(!1),N([]),B([]),m.current.clear(),!t||!e||!f)return;const l=Se({orgSlug:c.slug,virtualMcpId:t,branch:e,threadId:h??null},"events");let d=!1,S=0,T=null,g=null,y=null,v=null,E=null,x=null,V=!1;const F=()=>{g&&clearTimeout(g),g=setTimeout(()=>{g=null,o.invalidateQueries({queryKey:C.liveMeta(c.slug,t,e)})},1e3)},de=n=>{try{const s=JSON.parse(n.data);L(s),s.kind!=="failed"&&(O(!1),S=0),s.kind==="ready"&&H()}catch(s){console.warn("[vm-events] bad phase payload",s)}},$=()=>{O(!0),L(null),_({phase:"idle"}),w({state:"running"}),P({kind:"unknown"}),N([]),B([]),m.current.clear()},j=n=>{try{const s=JSON.parse(n.data);if(typeof s.data!="string")return;S=0;const u=s.data.replace(/\r?\n/g,`\r
`);le(s.source).append(u);for(const r of K.current)try{r(s.source,u)}catch{}ie(r=>r+1)}catch{}},q=n=>{const s=n.type;try{const u=JSON.parse(n.data);switch(S=0,s){case"lifecycle":{const r=u;_(r.state);const b=`${c.slug}/${t}/${e}`;if(W(r.state.phase)&&!(x&&W(x))){J(o,c.id);const D=C.decofile(b);o.getQueryData(D)===void 0&&o.invalidateQueries({queryKey:D})}r.state.phase==="running"&&x!=="running"&&(J(o,c.id),o.invalidateQueries({queryKey:C.decofile(b)}),F()),x=r.state.phase;const p=r.state.phase==="running"?r.state.port:null,U=A.current;if(A.current=p,U!==null&&p!==null&&U!==p)for(const D of k.current)try{D()}catch{}return}case"status":w(u);return;case"tasks":{const r=u;V=r.active.some(p=>Le(p.command));const b=r.active.map(p=>p.logName??"").filter(Boolean);B(b);return}case"scripts":N(u.scripts);return;case"branch":{const r=u.meta;P(r);return}case"reload":for(const r of k.current)try{r()}catch{}return;case"file-changed":{const{path:r}=u,b=`${c.slug}/${t}/${e}`;if(r.startsWith(".deco/")||r.includes("/.deco/")){if(!(x==="running"))return;r.endsWith(".deco/meta.gen.json")&&F(),y&&clearTimeout(y),y=setTimeout(()=>{if(y=null,o.invalidateQueries({queryKey:C.decofile(b)}),Q||V)for(const D of k.current)try{D()}catch{}},500)}else F();return}}}catch{}};function M(){if(d)return;const n=new EventSource(l);v=n,n.onopen=()=>{},n.onerror=()=>{v===n&&n.readyState===EventSource.CLOSED&&Y("studio")},n.addEventListener("phase",de),n.addEventListener("gone",$),n.addEventListener(I,j);for(const s of X)n.addEventListener(s,q)}function H(){if(d||!R||E)return;const n=new EventSource(R);E=n;let s=!1;n.onopen=()=>{if(d||E!==n)return;s=!0,S=0;const u=v;v=null,u?.close()},n.onerror=()=>{E===n&&n.readyState===EventSource.CLOSED&&fe(n,s)},n.addEventListener(I,j);for(const u of X)n.addEventListener(u,q)}async function fe(n,s){const u=R?await _e(R):!1;if(!(d||E!==n)){if(n.close(),E=null,u){$(),v||M();return}s&&Y("direct")}}function Y(n){if(d||T)return;const s=ye(Re,ke,S,2,0);S++,T=setTimeout(()=>{if(T=null,!d){if(n==="direct"){E?.close(),E=null,H();return}v?.close(),v=null,M()}},s)}return M(),()=>{d=!0,v?.close(),E?.close(),T&&clearTimeout(T),g&&clearTimeout(g),y&&clearTimeout(y)}},[t,e,c.slug,c.id,f,h,R,o,Q]),{phase:ne,lifecycle:se,status:re,branch:ae,notFound:ce,scripts:oe,activeProcesses:ue,getBuffer:l=>m.current.get(l)?.get()??"",hasData:l=>(m.current.get(l)?.get().length??0)>0,subscribeChunks:l=>(K.current.add(l),()=>{K.current.delete(l)}),subscribeReload:l=>(k.current.add(l),()=>{k.current.delete(l)})}}function te(){return a.use(Z)}function Ue(t){const{subscribeChunks:e}=te(),i=a.useRef(t);i.current=t,a.useEffect(()=>e((o,h)=>{i.current?.(o,h)}),[e])}function Qe(t){const{subscribeReload:e}=te(),i=a.useRef(t);i.current=t,a.useEffect(()=>e(()=>{i.current?.()}),[e])}export{Me as S,Ue as a,Qe as b,W as i,te as u};