UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 6.46 kB
import{serialize as e}from"node:v8";import{deflateRawSync as t,zstdCompressSync as n,zstdDecompressSync as r}from"node:zlib";import{createHash as i}from"node:crypto";function digest(e,...t){return`sha256:${i(`sha256`).update(JSON.stringify([`openclaw.local-workspace.${e}.v1`,...t])).digest(`hex`)}`}const a=digest(`empty`);function parentPath(e){let t=e.lastIndexOf(`/`);return t===-1?``:e.slice(0,t)}function localWorkspaceManifestRootReference(e,t){return digest(`root`,t,e.get(``).treeRef)}function createLocalWorkspaceManifestTree(e){let t={node:{path:``,type:`directory`},priority:``},n=/* @__PURE__ */ new Map([[``,t]]),r=/* @__PURE__ */ new Map;for(let t of e.toSorted((e,t)=>e.path<t.path?-1:+(e.path>t.path))){if(t.path===``)throw Error(`The workspace manifest root cannot be changed`);let e=parentPath(t.path),i=n.get(e);if(i?.node.type!==`directory`)throw Error(`Missing workspace manifest directory: ${e}`);let a={node:t,priority:digest(`priority`,t.path)},o=r.get(e)??[];for(;o.length&&o.at(-1).priority>a.priority;)a.left=o.pop();let s=o.at(-1);s?s.right=a:i.children=a,o.push(a),r.set(e,o),n.set(t.path,a)}let i=/* @__PURE__ */ new Map,finish=e=>{if(!e)return;let t=manifestRecord(e.node,finish(e.left),finish(e.right),finish(e.children));return i.set(e.node.path,t),t};return finish(t),i}function manifestRecord(e,t,n,r,i){let o=e.type===`directory`?digest(`directory`,e.path,r?.treeRef??a):i?.node===e?i.contentRef:e.type===`file`?digest(`file`,e.path,e.mode,e.size,e.sha256):digest(`symlink`,e.path,e.mode,e.target);return{node:e,left:t?.node.path??null,right:n?.node.path??null,children:r?.node.path??null,contentRef:o,treeRef:digest(`siblings`,t?.treeRef??a,o,n?.treeRef??a)}}function updateLocalWorkspaceManifestTree(e,t){let n=/* @__PURE__ */ new Map,r=/* @__PURE__ */ new Set,i=/* @__PURE__ */ new Map,recordAt=t=>t===null?void 0:e.get(t);function write(t,i,a,o){let s=manifestRecord(t,recordAt(i),recordAt(a),recordAt(o),e.get(t.path));e.set(t.path,s),n.set(t.path,s),r.delete(t.path)}function priority(e){let t=i.get(e);return t===void 0&&(t=digest(`priority`,e),i.set(e,t)),t}function precedes(e,t){let n=priority(e),r=priority(t);return n<r||n===r&&e<t}function insert(t,n){if(t===null)return write(n,null,null,null),n.path;let r=e.get(t);if(n.path===t){if(n.type!==`directory`&&r.children!==null)throw Error(`Cannot replace nonempty workspace directory: ${t}`);return write(n,r.left,r.right,r.children),t}if(n.path<t){let i=insert(r.left,n);if(precedes(i,t)){let n=e.get(i);return write(r.node,n.right,r.right,r.children),write(n.node,n.left,t,n.children),i}write(r.node,i,r.right,r.children)}else{let i=insert(r.right,n);if(precedes(i,t)){let n=e.get(i);return write(r.node,r.left,n.left,r.children),write(n.node,t,n.right,n.children),i}write(r.node,r.left,i,r.children)}return t}function merge(t,n){if(t===null||n===null)return t??n;if(precedes(t,n)){let r=e.get(t);return write(r.node,r.left,merge(r.right,n),r.children),t}let r=e.get(n);return write(r.node,merge(t,r.left),r.right,r.children),n}function remove(t,i){if(t===null)return null;let a=e.get(t);if(i===t){if(a.children!==null)throw Error(`Cannot remove nonempty workspace directory: ${t}`);let i=merge(a.left,a.right);return e.delete(t),n.delete(t),r.add(t),i}return i<t?write(a.node,remove(a.left,i),a.right,a.children):write(a.node,a.left,remove(a.right,i),a.children),t}function refresh(t,n){let r=e.get(t);return write(r.node,n<t?refresh(r.left,n):r.left,n>t?refresh(r.right,n):r.right,r.children),t}e.has(``)||write({path:``,type:`directory`},null,null,null);let a=t.toSorted((e,t)=>{if(!!e.node!=!!t.node)return e.node?1:-1;let n=e.path<t.path?-1:+(e.path>t.path);return e.node?n:-n});for(let t of a){if(t.path===``)throw Error(`The workspace manifest root cannot be changed`);if(!t.node&&!e.has(t.path))continue;let n=parentPath(t.path),r=e.get(n);if(r?.node.type!==`directory`)throw Error(`Missing workspace manifest directory: ${n}`);let i=t.node?insert(r.children,t.node):remove(r.children,t.path);for(;;){let t=e.get(n);if(write(t.node,t.left,t.right,i),n===``)break;let r=parentPath(n);i=refresh(e.get(r).children,n),n=r}}return{records:[...n.values()],removed:[...r]}}function decodeLocalWorkspaceTree(e,t=!1){let n=/* @__PURE__ */ new Map;for(let r of e){let e=t?[JSON.parse(r)]:JSON.parse(r);for(let t of e)n.set(t.node.path,t)}return n}function localWorkspaceDigestBytes(e){if(!/^sha256:[a-f0-9]{64}$/u.test(e))throw Error(`Invalid local workspace tree digest`);return Uint8Array.from(Buffer.from(e.slice(7),`hex`))}function localWorkspaceDigestReference(e){return`sha256:${Buffer.from(e).toString(`hex`)}`}function prepareLocalWorkspaceTreeUpdate(e,t,n,r){let i=/* @__PURE__ */ new Map;if(r){i.set(null,r);for(let t of[``,...n]){let n=e.get(t)?.children;n!=null&&i.set(t,e.get(n).treeRef)}}let a=updateLocalWorkspaceManifestTree(e,t);return e.get(``).treeRef===r?[]:prepareLocalWorkspaceTreePacks(e,r?[...a.records.map(({node:e})=>e.path),...a.removed]:void 0,void 0,i)}function encodeLocalWorkspaceTreePacks(e,t,n,r){let i=t&&new Set(t),a=/* @__PURE__ */ new Set([``]);if(i===void 0)for(let t of e.values())t.children!==null&&a.add(t.children);else for(let t of i){if(t===``)continue;let n=t.slice(0,Math.max(0,t.lastIndexOf(`/`))),r=e.get(n)?.children;r!=null&&a.add(r)}let o=[];for(let t of a){let a=e.get(t);if(n?.has(a.treeRef))continue;let s=t===``?null:t.slice(0,Math.max(0,t.lastIndexOf(`/`))),c=i&&r?.get(s),l=[],u=[],d=[a];for(;d.length;){let t=d.pop();(!c||t===a||i.has(t.node.path))&&(l.push(t),t.children!==null&&u.push(e.get(t.children).treeRef));for(let n of[t.left,t.right])n!==null&&d.push(e.get(n))}o.push({treeRef:a.treeRef,baseTreeRef:c,records:l,children:u})}return o}function prepareLocalWorkspaceTreePacks(n,r,i,a){let o=encodeLocalWorkspaceTreePacks(n,r,i,a),s=[];for(let n=0;n<o.length;n+=256)s.push(t(e({packs:o.slice(n,n+256)})));return s}function prepareLocalWorkspaceSourcePathSet(e){return{digest:`sha256:${i(`sha256`).update(e).digest(`hex`)}`,compressed:n(Buffer.from(e))}}function decodeLocalWorkspaceSourcePathSet(e){return r(e).toString(`utf8`)}export{prepareLocalWorkspaceSourcePathSet as a,createLocalWorkspaceManifestTree as c,localWorkspaceDigestReference as i,localWorkspaceManifestRootReference as l,decodeLocalWorkspaceTree as n,prepareLocalWorkspaceTreePacks as o,localWorkspaceDigestBytes as r,prepareLocalWorkspaceTreeUpdate as s,decodeLocalWorkspaceSourcePathSet as t,updateLocalWorkspaceManifestTree as u};