UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

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