decocms
Version:
Deco CMS — Self-hostable MCP Gateway for managing AI connections and tools
2 lines (1 loc) • 6.31 kB
JavaScript
import{f$ as q,eE as B,g0 as U,c as J,r as Y,fR as P,fS as M,fX as K,fT as N,K as V,fU as X,fW as z,fV as G,fP as R}from"./index-DoEtSiIe.js";import{ax as Z,au as x,aF as ee,av as te,aw as C,aJ as ne,ay as re,aA as se}from"./site-editor-toast-B-FDGvtN.js";import{a5 as F,ak as oe,a8 as ie,a4 as ae}from"./blocks-tab-states-CTy1iAF5.js";async function ce(e,t){return e.inDraft?e.value===void 0?B:U(e.value):await q(null,e.value,t)??null}class Ce{constructor(t){this.now=t,this.loaded=t()}now;copies=new Map;generations=new Map;loaded;settle(){this.loaded||(this.loaded=this.now())}get(t){const n=this.copies.get(t);if(n)return n;if(this.loaded)return{value:this.loaded.blocks[t],inDraft:this.loaded.versionOf(t)!==null}}generation(t){return this.generations.get(t)??0}edited(t){return{generation:this.generation(t),copy:this.get(t)}}guardOf(t,n){return n&&n.generation!==this.generation(t)?n.copy:this.get(t)}hold(t){this.generations.set(t,this.generation(t)+1);const n=this.now();!n||!Object.hasOwn(n.blocks,t)||this.copies.set(t,{value:n.blocks[t],inDraft:n.versionOf(t)!==null})}saved(t,n){this.copies.set(t,{value:n,inDraft:!0})}}const g=new Map,k=new Set,E=new Map;function ke(e){return`${e.orgSlug}/${e.virtualMcpId}/${e.branch}`}function Ee(e,t){const n=new Map(g.get(e)??E);n.set(t,(n.get(t)??0)+1),g.set(e,n);for(const s of k)s()}function le(e){return k.add(e),()=>k.delete(e)}const A=new WeakMap;function fe(e){let t=A.get(e);return t||(t=n=>e.get(n)??0,A.set(e,t)),t}function De(e){const t=J.c(5);let n,s;t[0]!==e?(n=()=>g.get(e)??E,s=()=>g.get(e)??E,t[0]=e,t[1]=n,t[2]=s):(n=t[1],s=t[2]);const r=Y.useSyncExternalStore(le,n,s);let o;return t[3]!==r?(o=fe(r),t[3]=r,t[4]=o):o=t[4],o}class p extends Error{constructor(t){super("kept their version"),this.deleted=t,this.name="HostedKeptTheirsError"}deleted}const ue=3;function de(e,t,n,s){const r={},o={},i={};for(const a of e)t&&Object.hasOwn(t,a)&&(r[a]=t[a]),Object.hasOwn(n.writes,a)&&(o[a]=n.writes[a]),Object.hasOwn(s,a)&&(i[a]=s[a]);return X({ancestor:r,mine:o,theirs:i})}function he(e,t,n){const s={},r=[];for(const o of e){const i=t[o];_(i)?s[o]=i:Object.hasOwn(n,o)&&r.push(o)}return{writes:s,deletes:r}}function we(e,t){for(const[n,s]of Object.entries(e.writes))if(!Object.hasOwn(t,n)||R(t[n])!==R(s))return!1;return e.deletes.every(n=>!Object.hasOwn(t,n))}function Oe(e,t){if(!_(t))return null;if(F(e,t))return oe(t);const n=typeof t.name=="string"?t.name.trim():"";return n&&n!==e?n:I(e)}function I(e){const t=F(e);if(t)return ie(t);const n=e.split("/").pop()??e;return ae(n).trim()||n}function $(e,...t){for(const n of t){const s=Oe(e,n);if(s)return s}return I(e)}function H(e,t,n){return Object.keys(e.writes).some(r=>!t||!Object.hasOwn(t,r))?e.deletes.filter(r=>!Object.hasOwn(e.writes,r)&&n(r)):[]}async function T(e,t,n){const s=Object.keys(e.writes).filter(i=>!t||!Object.hasOwn(t,i)).map(i=>e.writes[i]),r=n[0];if(await N({kind:"theyDeleted",label:$(r,t?.[r],...s)})==="theirs")throw new p(!0);return{writes:e.writes,deletes:e.deletes.filter(i=>!n.includes(i))}}function W(e){if(e.pointers.length!==1)return null;const t=e.pointers[0],n=t.split("/");if(n.length!==2)return null;const s=n[1].replaceAll("~1","/").replaceAll("~0","~"),r=e.mine[s],o=e.theirs[s];return r===void 0==(o===void 0)?null:{pointer:t,conflict:{kind:o===void 0?"theyDeleted":"youDeleted",label:$(s,r,o,e.ancestor[s])}}}async function pe(e,t,n,s){const r=W(e);if(r)return{[r.pointer]:await N(r.conflict)};const o=z(e,t);return G(e,s?o.map(s):o,n?.author?{...n,author:{...n.author,keyed:!0}}:n)}function Se(e){const t=e.split("/")[1];return t?t.replaceAll("~1","/").replaceAll("~0","~"):null}function ge(e){const t=Object.values(e);return t.length>0&&t.every(n=>n==="theirs")}async function ve(e,t,n){const s=o=>se(e.queryClient,e.cacheKey,o,e.backend);if(t.some(o=>s(o)===void 0))try{await te(e.queryClient,e.backend,e.params,e.cacheKey)}catch{throw new C}const r={};for(const o of t){const i=s(o);if(i===void 0)throw new C;const a=n&&Object.hasOwn(n,o)?await q(i,n[o],P(e.queryClient,e.cacheKey,o)):i;if(a===void 0)throw new C;r[o]=a}return r}async function me(e,t,n,s){if(s&&w(e,t,r=>Object.hasOwn(s,r)?{value:s[r]}:null),!ne(e.queryClient,e.backend,e.cacheKey,n))for(const r of t)await re(e.queryClient,e.backend,e.params,e.cacheKey,r,n);return e.queryClient.getQueryData(V.decofile(e.cacheKey))??{}}async function Me(e,t,n,s){let r=t;const o=s?Object.keys(s):[];let i=o.length===0?n:{...n,...Object.fromEntries(o.map(a=>[a,s[a].value]))};for(let a=0;;a++){const v=r.deletes.filter(l=>!Object.hasOwn(r.writes,l)),d=[...Object.keys(r.writes),...v],O=await ve(e,d,a===0?i:void 0);if(a===0)for(const l of o)Object.hasOwn(O,l)&&(O[l]=await ce(s[l],P(e.queryClient,e.cacheKey,l)));const m=H(r,i,l=>O[l]===B);if(m.length>0){try{if(!M())throw new p(!0);r=await T(r,i,m)}catch(l){throw w(e,d,c=>m.includes(c)||!i||!Object.hasOwn(i,c)?null:{value:i[c]}),l}continue}try{const l=await Z(e.queryClient,e.backend,e.params,e.cacheKey,{set:r.writes,...v.length>0?{delete:v}:{}},O);return w(e,d,c=>Object.hasOwn(r.writes,c)?{value:r.writes[c]}:null),{...l,change:r}}catch(l){if(!x(l))throw l;const c=await me(e,d,l,i);if(a+1>=ue)throw new ee;if(!M())throw l;const D=Object.keys(r.writes).filter(u=>!i||!Object.hasOwn(i,u)),L=r;w(e,D,u=>({value:L.writes[u]}));const S=()=>w(e,D,u=>Object.hasOwn(c,u)?{value:c[u]}:null);try{const u=H(r,i,f=>!!i&&Object.hasOwn(i,f)&&!Object.hasOwn(c,f));if(u.length>0){r=await T(r,i,u),i=Object.fromEntries(d.filter(f=>Object.hasOwn(c,f)).map(f=>[f,c[f]]));continue}const h=de(d,i,r,c),b=h.pointers.length===0?{}:await pe(h),y=W(h);if(y&&b[y.pointer]==="theirs")throw new p(y.conflict.kind==="theyDeleted");const Q=h.pointers.length===0?h.merged:K(h,b),j=he(d,Q,c);if(we(j,c)){if(ge(b))throw new p(d.some(f=>!Object.hasOwn(c,f)));return S(),{revision:"",versions:{},change:j}}r=j,i=Object.fromEntries(d.filter(f=>Object.hasOwn(c,f)).map(f=>[f,c[f]]))}catch(u){throw S(),u}}}}function w(e,t,n){e.queryClient.setQueryData(V.decofile(e.cacheKey),s=>{if(!s)return s;const r={...s};for(const o of t){const i=n(o);i?r[o]=i.value:delete r[o]}return r})}function _(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}export{p as H,ve as a,Ee as b,ke as c,ue as d,$ as e,ge as f,Ce as g,ce as h,Me as i,pe as j,Se as k,he as l,we as m,de as p,De as u};