openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.38 kB
JavaScript
import{a as e,i as t,r as n}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import{o as r}from"./openclaw-state-read-openclaw-state-db-schema-helpers-BkqUYEFY.mjs";import"./openclaw-state-read-openclaw-state-feature-schema-BPVIIoAx.mjs";import{t as i}from"./openclaw-state-read-zstd-codec-Bwg8OR0e.mjs";import{a,i as o,r as s,t as c}from"./openclaw-state-read-local-workspace-cas-ZEBQPaPC.mjs";import{serialize as l}from"node:v8";import{deflateRawSync as u}from"node:zlib";const d=`local_workspace_tree_packs`,f=`local_workspace_source_path_sets`,query=t=>e(t);function readLocalWorkspaceSourcePaths(e,n){let r=t(e,query(e).selectFrom(f).select(`paths_zstd`).where(`path_digest`,`=`,Buffer.from(n)));if(!r)throw Error(`Local workspace source path set is missing`);return c(r.paths_zstd)}function readLocalWorkspaceTree(e,t,r){let i=[],a=[o(t)];for(;a.length;){let t=[];for(let c=0;c<a.length;c+=8192){let l=a.slice(c,c+8192),u=n(e,query(e).selectFrom(d).select([`tree_digest`,`children_json`]).select(e=>r?.referencesOnly?e.val(null).as(`records_json`):e.ref(`records_json`).as(`records_json`)).where(`tree_digest`,`in`,l.map(s))).rows;if(u.length!==l.length&&!r?.allowMissing)throw Error(`Local workspace baseline nodes are missing`);for(let e of u){i.push(r?.referencesOnly?o(e.tree_digest):e.records_json);for(let n of JSON.parse(e.children_json))t.push(n)}}a=t}return i}function readUnreachableWorkspaceContent(e,t,n){let r=[...e].toSorted(),i=[],a=/* @__PURE__ */ new Uint8Array;for(let e=0;;e+=256){let o=r.slice(e,e+256).map(s),c=e+256<r.length?o.at(-1):void 0;for(;;){let e=n(a,c,o);for(let n of e)n.sweep_epoch<t-1&&i.push(n.digest);if(e.length<256)break;a=e.at(-1).digest}if(c===void 0)return i;a=c}}function readLocalWorkspaceSweepCandidates(e,t){if(!r(e,d))return{packs:[],paths:[]};let i=/* @__PURE__ */ new Set,a=/* @__PURE__ */ new Set,c;for(;;){let t=n(e,query(e).selectFrom(`local_workspace_projections`).select([`worktree_id`,`tree_root_digest`,`source_paths_digest`,`checkpoint_json`]).$if(c!==void 0,e=>e.where(`worktree_id`,`>`,c)).orderBy(`worktree_id`).limit(256)).rows;for(let e of t)if(e.tree_root_digest&&i.add(o(e.tree_root_digest)),e.source_paths_digest&&a.add(o(e.source_paths_digest)),e.checkpoint_json){let t=JSON.parse(e.checkpoint_json),n=t.kind===`rejected`?t.checkpoint?JSON.parse(t.checkpoint):void 0:t;n?.treeRoot&&i.add(n.treeRoot),n?.previousTreeRoot&&i.add(n.previousTreeRoot)}if(t.length<256)break;c=t.at(-1).worktree_id}let l=i.values(),u=0;for(;u<i.size;){let t=[],r=Math.min(256,i.size-u);for(let e=0;e<r;e++)t.push(l.next().value);u+=r;let a=n(e,query(e).selectFrom(d).select(`children_json`).where(`tree_digest`,`in`,t.map(s))).rows;if(a.length!==t.length)throw Error(`Local workspace baseline nodes are missing`);for(let e of a)for(let t of JSON.parse(e.children_json))i.add(t)}return{packs:readUnreachableWorkspaceContent(i,t,(t,r,i)=>n(e,query(e).selectFrom(d).select([`tree_digest as digest`,`sweep_epoch`]).where(`tree_digest`,`>`,t).$if(r!==void 0,e=>e.where(`tree_digest`,`<=`,r)).$if(i.length>0,e=>e.where(`tree_digest`,`not in`,i)).orderBy(`tree_digest`).limit(256)).rows),paths:readUnreachableWorkspaceContent(a,t,(t,r,i)=>n(e,query(e).selectFrom(f).select([`path_digest as digest`,`sweep_epoch`]).where(`path_digest`,`>`,t).$if(r!==void 0,e=>e.where(`path_digest`,`<=`,r)).$if(i.length>0,e=>e.where(`path_digest`,`not in`,i)).orderBy(`path_digest`).limit(256)).rows)}}function localWorkspaceFingerprintPatch(e){let t={};if(e){let{target:n,blob:r,oppositeBlob:i}=e;for(let[e,a]of[[n,r],[n===`canonical`?`projection`:`canonical`,i]])if(a){let n=Buffer.from(a,`base64`);t[`${e}_fingerprints`]=new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}}return t}function prepareLocalWorkspaceUpdate(e,t=[]){let n=e.manifest,r={...e.patch,...n?{baseline_ref:n.resultingRoot,baseline_json:null}:{},...n?.treeRoot?{tree_root_digest:s(n.treeRoot)}:{},...localWorkspaceFingerprintPatch(n?.fingerprints)},o=r;if(typeof r.source_paths_json==`string`){let e=a(r.source_paths_json);t.push(u(l({paths:e}))),r.source_paths_digest=s(e.digest),o={...r,source_paths_json:`[]`}}return{revision:e.revision,patch:r,rejectedResult:e.rejectedResult,mutation:i().compress(l({...e,patch:o}),1),preparations:t}}export{readLocalWorkspaceTree as i,readLocalWorkspaceSourcePaths as n,readLocalWorkspaceSweepCandidates as r,prepareLocalWorkspaceUpdate as t};