UNPKG

decocms

Version:

Deco CMS — Self-hostable MCP Gateway for managing AI connections and tools

2 lines (1 loc) • 7.07 kB
import{c as O,q as $,i as S,K as b,eO as v,cM as A}from"./index-ztdGKvBm.js";const m={MethodNotFound:-32601,InvalidParams:-32602,InternalError:-32603,NotFound:-32001,Conflict:-32002,InvalidBlock:-32003,ReadOnly:-32005,Unsupported:-32006,LimitExceeded:-32007,Unavailable:-32008,Unauthorized:-32010};class f extends Error{code;data;constructor(n,o,e){super(o),this.name="ContentProtocolError",this.code=n,this.data=e}toJSON(){return this.data===void 0?{code:this.code,message:this.message}:{code:this.code,message:this.message,data:this.data}}static from(n){return new f(n.code,n.message,n.data)}}const g="deco-content",E={major:1},k=1;async function P(t){let n=new Uint8Array(await t.arrayBuffer());if(n[0]===31&&n[1]===139){const o=new Blob([n]).stream().pipeThrough(new DecompressionStream("gzip"));n=new Uint8Array(await new Response(o).arrayBuffer())}return JSON.parse(new TextDecoder().decode(n))}const C={major:E.major,minor:k},D=`the endpoint speaks an older ${g} than this client needs (${C.major}.${C.minor} or newer): update @decocms/blocks in the site (8.2.0 or newer) and start deco serve again`;function w(t,n,o){const e={reason:n,required:{...C}};if(o){e.actual={protocol:o.protocol,major:o.version?.major,minor:o.version?.minor};const r=o.server;typeof r?.name=="string"&&typeof r.version=="string"&&(e.server={name:r.name,version:r.version})}return new f(m.Unsupported,t,e)}function L(t){const n=t??{},{protocol:o,version:e}=n;if(o!==g)throw w(`the endpoint speaks ${JSON.stringify(o)}, not ${g}`,"protocol",n);if(e?.major!==E.major)throw w(`the endpoint speaks ${g} ${String(e?.major)}.x; this client speaks ${E.major}.x`,"major",n);if(typeof e.minor!="number"||e.minor<k)throw w(D,"outdated",n);return t}function j(t,n){return n.code===m.MethodNotFound&&t.startsWith("changes.")?w(D,"outdated"):f.from(n)}function x(t){const n=t.fetch??(s=>fetch(s));let o=1;async function e(s){const c={"content-type":"application/json",accept:"application/json",...t.headers},a=await n(new Request(t.endpoint,{method:"POST",headers:c,body:JSON.stringify(s)}));let i;try{i=await P(a)}catch{throw new f(m.Unavailable,`the endpoint answered HTTP ${a.status} without a JSON-RPC body`)}if(!a.ok||!Array.isArray(i)&&i?.id===null){const l=i?.error;throw l?f.from(l):new f(m.Unavailable,`the endpoint answered HTTP ${a.status}`)}return i}const r=async(s,c)=>{const a=o++,i=await e({jsonrpc:"2.0",id:a,method:s,params:c??{}});if(i.error)throw j(s,i.error);return i.result};return{call:r,describe:async()=>L(await r("describe")),schemaGet:s=>r("schema.get",s),blocksList:s=>r("blocks.list",s),blocksApply:s=>r("blocks.apply",s),changesList:s=>r("changes.list",s),changesGet:s=>r("changes.get",s),changesDiscard:s=>r("changes.discard",s),async batch(s){const c=s.map(()=>o++),a=await e(s.map((l,d)=>({jsonrpc:"2.0",id:c[d],method:l.method,params:l.params??{}}))),i=new Map(a.map(l=>[l.id,l]));return c.map((l,d)=>{const u=i.get(l);return u?u.error?{ok:!1,error:j(s[d].method,u.error)}:{ok:!0,result:u.result}:{ok:!1,error:new f(m.InternalError,"no response for this call")}})}}}function F(t){return t&&!t.edited?{ifNoneMatch:t.revision}:{}}const I=20,R=5,T=500;function z(t){return t instanceof f?t.code===m.MethodNotFound?!0:t.code===m.Unsupported&&t.data?.reason==="outdated":!1}function _(t,n,o=t){return o&&o!==t?o:n.notModified?t?{...t,base:n.base}:null:{revision:n.revision,base:n.base,changes:n.changes}}async function q(t,n,o){const e=b.contentChanges(o),r=t.getQueryData(e),s=await n.changesList(F(r)),c=_(r,s,t.getQueryData(e));if(c)return c;const a=await n.changesList({}),i=_(void 0,a,t.getQueryData(e));if(!i)throw new f(m.InternalError,"changes.list returned notModified without a cached list");return i}async function W(t,n,o){const r=await x({endpoint:new URL(`/api/${t}/decofile/${encodeURIComponent(n)}/${encodeURIComponent(o)}/rpc`,window.location.origin).href}).changesList();return r.notModified?0:Object.keys(r.changes).length}async function M(t,n){const o=[];for(let s=0;s<n.length;s+=I)o.push(n.slice(s,s+I));const e=Object.create(null),r=[];for(let s=0;s<o.length;s+=R){const c=await t.batch(o.slice(s,s+R).map(a=>({method:"changes.get",params:{names:a}})));for(const a of c){if(!a.ok)throw a.error;const i=a.result;Object.assign(e,i.entries),r.push(...i.diagnostics)}}return{entries:e,diagnostics:r}}async function X(t,n){let o={revision:"",versions:{}};for(let e=0;e<n.length;e+=T){const r=n.slice(e,e+T),s=await t.changesDiscard({names:r.map(c=>c.name),ifMatch:Object.fromEntries(r.map(c=>[c.name,c.version]))});o={revision:s.revision,versions:{...o.versions,...s.versions}}}return o}function Z(t){return t instanceof f&&t.code===m.Conflict}function N(t){return t?.kind==="protocol"?t:null}function K(t,n,o){const e=O.c(14);let r;e[0]!==o?(r={},e[0]=o,e[1]=r):r=e[1];const s=r,c=$();let a;e[2]!==t?(a=N(t),e[2]=t,e[3]=a):a=e[3];const i=a,l=!!i&&!!n&&s.enabled!==!1;let d;e[4]!==n?(d=b.contentChanges(n??""),e[4]=n,e[5]=d):d=e[5];let u;e[6]!==n||e[7]!==l||e[8]!==i||e[9]!==c?(u=l?()=>q(c,i.client,n):v,e[6]=n,e[7]=l,e[8]=i,e[9]=c,e[10]=u):u=e[10];let p;return e[11]!==d||e[12]!==u?(p={queryKey:d,queryFn:u,staleTime:0},e[11]=d,e[12]=u,e[13]=p):p=e[13],S(p)}function G(t){if(!t)return"";const n=Object.entries(t.changes).sort(([o],[e])=>o<e?-1:o>e?1:0).map(([o,e])=>[o,e.kind,e.version,e.baseVersion]);return JSON.stringify([t.revision,t.base.revision,t.base.resolvedRef,n])}function ee(t,n,o){const e=O.c(14);let r;e[0]!==t?(r=N(t),e[0]=t,e[1]=r):r=e[1];const s=r;let c;e[2]!==o?(c=o?Object.keys(o.changes):[],e[2]=o,e[3]=c):c=e[3];const a=c,i=!!s&&!!n&&!!o&&a.length>0;let l;e[4]!==n||e[5]!==o?(l=b.contentChangeSides(n??"",G(o)),e[4]=n,e[5]=o,e[6]=l):l=e[6];let d;e[7]!==i||e[8]!==a||e[9]!==s?(d=i?()=>M(s.client,a):v,e[7]=i,e[8]=a,e[9]=s,e[10]=d):d=e[10];let u;return e[11]!==l||e[12]!==d?(u={queryKey:l,queryFn:d,staleTime:Number.POSITIVE_INFINITY},e[11]=l,e[12]=d,e[13]=u):u=e[13],S(u)}function te(t,n,o){const e=O.c(16);let r;e[0]!==t?(r=N(t),e[0]=t,e[1]=r):r=e[1];const s=r;let c,a,i,l;if(e[2]!==n||e[3]!==o?.changes||e[4]!==s){const p=Object.entries(o?.changes??{}).filter(B).map(H).sort(V);a=p.map(Q);const h=JSON.stringify(p.map(J));c=!!s&&!!n&&a.length>0,l=S,i=b.contentDeletedCopies(n??"",h),e[2]=n,e[3]=o?.changes,e[4]=s,e[5]=c,e[6]=a,e[7]=i,e[8]=l}else c=e[5],a=e[6],i=e[7],l=e[8];let d;e[9]!==c||e[10]!==a||e[11]!==s?(d=c?async()=>{const p=await M(s.client,a),h=Object.create(null);for(const[U,y]of Object.entries(p.entries))h[U]=y.before&&typeof y.before=="object"?y.before:null;return h}:v,e[9]=c,e[10]=a,e[11]=s,e[12]=d):d=e[12];let u;return e[13]!==i||e[14]!==d?(u={queryKey:i,queryFn:d,staleTime:Number.POSITIVE_INFINITY,placeholderData:A},e[13]=i,e[14]=d,e[15]=u):u=e[15],l(u)}function J(t){return[t.name,t.base]}function Q(t){return t.name}function V(t,n){return t.name<n.name?-1:t.name>n.name?1:0}function H(t){const[n,o]=t;return{name:n,base:o.baseVersion??""}}function B(t){const[,n]=t;return n.kind==="delete"}export{f as C,m as E,g as P,te as a,L as b,x as c,F as d,_ as e,ee as f,X as g,Z as h,z as i,W as j,q as r,K as u};