UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.97 kB
import{a as executeSqliteQueryTakeFirstSync,i as executeSqliteQuerySync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{a as tableExists}from"./openclaw-state-db-schema-helpers-rYTimzjJ.mjs";import{S as getOpenClawStateFeatureSchemaSql}from"./openclaw-state-db-schema-additive-DDOLEjcV.mjs";import{n as localWorkspaceDigestBytes,r as localWorkspaceDigestReference,t as decodeLocalWorkspaceTree}from"./local-workspace-cas-BX5-hDAy.mjs"; //!src/gateway/worker-environments/local-workspace-cas.kernel.ts const packs=`local_workspace_tree_packs`;const pathSets=`local_workspace_source_path_sets`;const epochKey=`workspace.manifest.sweepEpoch`;const pendingSweepKey=`workspace.manifest.pendingSweepEpoch`;const query=e=>getNodeSqliteKysely(e);function currentLocalWorkspaceSweepEpoch(e){if(!tableExists(e,`config_machine_state`))return 0;const t=executeSqliteQueryTakeFirstSync(e,query(e).selectFrom(`config_machine_state`).select(`value_json`).where(`state_key`,`=`,epochKey));return t?Number(t.value_json):0}function prepareLocalWorkspaceContentInDatabase(e,t){if(!tableExists(e,packs))e.exec(getOpenClawStateFeatureSchemaSql(`local_workspace_projections`));const n=currentLocalWorkspaceSweepEpoch(e);if(t.packs?.length){if(t.packs.length>256)throw new Error(`Local workspace pack preparation exceeds its batch limit`);const r=t.packs.map(t=>{let r=t.records;let i=t.children;if(t.baseTreeRef){const n=executeSqliteQueryTakeFirstSync(e,query(e).selectFrom(packs).select([`records_json`,`children_json`]).where(`tree_digest`,`=`,Buffer.from(localWorkspaceDigestBytes(t.baseTreeRef))));if(!n)throw new Error(`Local workspace baseline nodes are missing`);const a=decodeLocalWorkspaceTree([n.records_json]);const o=/* @__PURE__ */ new Map;const s=JSON.parse(n.children_json);let c=0;for(const e of a.values())if(e.children!==null)o.set(e.node.path,s[c++]);c=0;for(const e of r){a.set(e.node.path,e);if(e.children!==null)o.set(e.node.path,i[c++])}const l=[r[0]];r=[];i=[];while(l.length){const e=l.pop();r.push(e);if(e.children!==null)i.push(o.get(e.node.path));for(const t of[e.left,e.right])if(t!==null)l.push(a.get(t))}}return{tree_digest:localWorkspaceDigestBytes(t.treeRef),records_json:JSON.stringify(r),children_json:JSON.stringify(i),sweep_epoch:n}});const i=executeSqliteQuerySync(e,query(e).selectFrom(packs).select([`tree_digest`,`sweep_epoch`]).where(`tree_digest`,`in`,r.map(e=>e.tree_digest))).rows;const a=new Set(i.map(e=>localWorkspaceDigestReference(e.tree_digest)));const o=r.filter(e=>!a.has(localWorkspaceDigestReference(e.tree_digest)));if(o.length)executeSqliteQuerySync(e,query(e).insertInto(packs).values(o).onConflict(e=>e.column(`tree_digest`).doUpdateSet(e=>({sweep_epoch:e.case().when(`${packs}.sweep_epoch`,`<`,n).then(n).else(e.ref(`${packs}.sweep_epoch`)).end()}))));const s=i.filter(e=>e.sweep_epoch<n).map(e=>e.tree_digest);if(s.length)executeSqliteQuerySync(e,query(e).updateTable(packs).set({sweep_epoch:n}).where(`tree_digest`,`in`,s).where(`sweep_epoch`,`<`,n))}if(t.paths){const r=localWorkspaceDigestBytes(t.paths.digest);executeSqliteQuerySync(e,query(e).insertInto(pathSets).values({path_digest:r,paths_zstd:Buffer.from(t.paths.compressed),sweep_epoch:n}).onConflict(e=>e.column(`path_digest`).doUpdateSet(e=>({sweep_epoch:e.case().when(`${pathSets}.sweep_epoch`,`<`,n).then(n).else(e.ref(`${pathSets}.sweep_epoch`)).end()}))))}return n}function requestLocalWorkspaceSweepInDatabase(e,t=currentLocalWorkspaceSweepEpoch(e)){executeSqliteQuerySync(e,query(e).insertInto(`config_machine_state`).values({state_key:pendingSweepKey,value_json:String(t+2),updated_at_ms:Date.now()}).onConflict(e=>e.column(`state_key`).doUpdateSet({value_json:String(t+2),updated_at_ms:Date.now()}).where(`config_machine_state.value_json`,`!=`,String(t+2))))}function finishLocalWorkspaceSweepInDatabase(e,t){executeSqliteQuerySync(e,query(e).updateTable(`config_machine_state`).set({value_json:`0`,updated_at_ms:Date.now()}).where(`state_key`,`=`,pendingSweepKey).where(e=>e(e.cast(`value_json`,`integer`),`<=`,t)))}function beginLocalWorkspaceSweepInDatabase(e){const t=executeSqliteQueryTakeFirstSync(e,query(e).selectFrom(`config_machine_state`).select(`value_json`).where(`state_key`,`=`,pendingSweepKey));if(t?.value_json===`0`)return null;if(!t)requestLocalWorkspaceSweepInDatabase(e);const n=currentLocalWorkspaceSweepEpoch(e)+1;executeSqliteQuerySync(e,query(e).insertInto(`config_machine_state`).values({state_key:epochKey,value_json:String(n),updated_at_ms:Date.now()}).onConflict(e=>e.column(`state_key`).doUpdateSet({value_json:String(n),updated_at_ms:Date.now()})));return n}function deleteLocalWorkspaceSweepBatchInDatabase(e,t,n){if(n.packs.length>256||n.paths.length>256)throw new Error(`Local workspace sweep exceeds its batch limit`);if(n.packs.length)executeSqliteQuerySync(e,query(e).deleteFrom(packs).where(`tree_digest`,`in`,n.packs.map(e=>Buffer.from(e))).where(`sweep_epoch`,`<`,t-1));if(n.paths.length)executeSqliteQuerySync(e,query(e).deleteFrom(pathSets).where(`path_digest`,`in`,n.paths.map(e=>Buffer.from(e))).where(`sweep_epoch`,`<`,t-1))}function pruneLocalWorkspaceLegacyBatchInDatabase(e,t){if(!executeSqliteQueryTakeFirstSync(e,query(e).selectFrom(`local_workspace_projections`).select(`tree_root_digest`).where(`worktree_id`,`=`,t))?.tree_root_digest)return false;const n=executeSqliteQuerySync(e,query(e).selectFrom(`local_workspace_manifest_nodes`).select(`path`).where(`worktree_id`,`=`,t).limit(256)).rows;if(!n.length)return false;executeSqliteQuerySync(e,query(e).deleteFrom(`local_workspace_manifest_nodes`).where(`worktree_id`,`=`,t).where(`path`,`in`,n.map(e=>e.path)));return n.length===256}export{prepareLocalWorkspaceContentInDatabase as a,finishLocalWorkspaceSweepInDatabase as i,currentLocalWorkspaceSweepEpoch as n,pruneLocalWorkspaceLegacyBatchInDatabase as o,deleteLocalWorkspaceSweepBatchInDatabase as r,requestLocalWorkspaceSweepInDatabase as s,beginLocalWorkspaceSweepInDatabase as t};