UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 3.94 kB
import{a as e,i as t,r as n}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{o as r}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import"./openclaw-state-read-openclaw-state-feature-schema-BaBHFVL8.mjs";import{serialize as i}from"node:v8";import{deflateRawSync as a,zstdDecompressSync as o}from"node:zlib";import"node:crypto";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 encodeLocalWorkspaceTreePacks(e,t){let n=/* @__PURE__ */ new Set([``]);if(t===void 0)for(let t of e.values())t.children!==null&&n.add(t.children);else for(let r of t){if(r===``)continue;let t=r.slice(0,Math.max(0,r.lastIndexOf(`/`))),i=e.get(t)?.children;i!=null&&n.add(i)}return[...n].map(t=>{let n=e.get(t),r=[],i=[],a=[n];for(;a.length;){let t=a.pop();r.push(t),t.children!==null&&i.push(e.get(t.children).treeRef);for(let n of[t.left,t.right])n!==null&&a.push(e.get(n))}return{treeRef:n.treeRef,records:r,children:i}})}function prepareLocalWorkspaceTreePacks(e,t){let n=encodeLocalWorkspaceTreePacks(e,t),r=[];for(let e=0;e<n.length;e+=256)r.push(a(i({packs:n.slice(e,e+256)})));return r}function decodeLocalWorkspaceSourcePathSet(e){return o(e).toString(`utf8`)}const s=`local_workspace_tree_packs`,c=`local_workspace_source_path_sets`,query=t=>e(t);function readLocalWorkspaceSourcePaths(e,n){let r=t(e,query(e).selectFrom(c).select(`paths_zstd`).where(`path_digest`,`=`,Buffer.from(n)));if(!r)throw Error(`Local workspace source path set is missing`);return decodeLocalWorkspaceSourcePathSet(r.paths_zstd)}function readLocalWorkspaceTree(e,t,r){let i=/* @__PURE__ */ new Map,a=[localWorkspaceDigestReference(t)];for(;a.length;){let t=[];for(let o=0;o<a.length;o+=8192){let c=a.slice(o,o+8192),l=n(e,query(e).selectFrom(s).select([`records_json`,`children_json`]).where(`tree_digest`,`in`,c.map(localWorkspaceDigestBytes))).rows;if(l.length!==c.length&&!r?.allowMissing)throw Error(`Local workspace baseline nodes are missing`);for(let e of l){let n=JSON.parse(e.records_json);for(let e of n)i.set(e.node.path,e);for(let n of JSON.parse(e.children_json))t.push(n)}}a=t}return i}function readLocalWorkspaceSweepCandidates(e,t){let i={packs:[],paths:[]};if(!r(e,s))return i;let a=/* @__PURE__ */ new Set,o=/* @__PURE__ */ new Set,l=/* @__PURE__ */ new Set;for(let t of n(e,query(e).selectFrom(`local_workspace_projections`).select([`tree_root_digest`,`source_paths_digest`,`checkpoint_json`])).rows)if(t.tree_root_digest&&l.add(localWorkspaceDigestReference(t.tree_root_digest)),t.source_paths_digest&&o.add(localWorkspaceDigestReference(t.source_paths_digest)),t.checkpoint_json){let e=JSON.parse(t.checkpoint_json);e.treeRoot&&l.add(e.treeRoot),e.previousTreeRoot&&l.add(e.previousTreeRoot)}let u=new Set(l);for(;u.size;){let t=[...u].slice(0,256),r=n(e,query(e).selectFrom(s).select([`tree_digest`,`children_json`]).where(`tree_digest`,`in`,t.map(localWorkspaceDigestBytes))).rows;if(r.length!==t.length)throw Error(`Local workspace baseline nodes are missing`);for(let e of r){let t=localWorkspaceDigestReference(e.tree_digest);a.add(t),u.delete(t);for(let t of JSON.parse(e.children_json))a.has(t)||u.add(t)}}for(let r of n(e,query(e).selectFrom(s).select(`tree_digest`).where(`sweep_epoch`,`<`,t-1)).rows)a.has(localWorkspaceDigestReference(r.tree_digest))||i.packs.push(r.tree_digest);for(let r of n(e,query(e).selectFrom(c).select(`path_digest`).where(`sweep_epoch`,`<`,t-1)).rows)o.has(localWorkspaceDigestReference(r.path_digest))||i.paths.push(r.path_digest);return i}export{localWorkspaceDigestReference as a,localWorkspaceDigestBytes as i,readLocalWorkspaceSweepCandidates as n,prepareLocalWorkspaceTreePacks as o,readLocalWorkspaceTree as r,readLocalWorkspaceSourcePaths as t};