openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.87 kB
JavaScript
import{u as isStringArray}from"./record-coerce-BTbqdJ3-.mjs";import{s as requireGitBuffer}from"./git-2P0YMMKQ.mjs";import{o as splitNullBuffer}from"./git-path-inventory-CgWMY8bC.mjs";import{t as workspacePathAncestors}from"./workspace-path-ancestors-BgOePO1n.mjs";import{s as buildCacheInputPaths}from"./workspace-path-exclusions-Cmi1311P.mjs";import{u as serializeWorkerWorkspaceManifest}from"./workspace-manifest-BE9NudAI.mjs";import{createHash}from"node:crypto";import{isUtf8}from"node:buffer";
//!src/gateway/worker-environments/local-workspace-inventory.ts
function decodeLocalWorkspaceGitPaths(e){if(!isUtf8(e))throw new Error(`Local sandbox source paths must be UTF-8; rename the invalid Git path before retrying`);return e.toString(`utf8`).split(`\0`).filter(Boolean)}function parseLocalWorkspaceSourcePaths(e){const t=JSON.parse(e);if(!isStringArray(t))throw new Error(`Invalid local workspace source admission`);return t}async function readLocalWorkspaceGitExclusions(e,t,n){const r=await requireGitBuffer(e,[`--work-tree=${t}`,`ls-files`,`--full-name`,`--others`,`--ignored`,`--exclude-standard`,`--directory`,`-z`]);return{paths:new Set(decodeLocalWorkspaceGitPaths(r)),retainedPaths:n}}async function admitLocalWorkspaceSourcePaths(e){const t=await requireGitBuffer(e.root,[`ls-files`,`--cached`,`--others`,`--exclude-standard`,`-z`],{signal:e.signal,beforeRun:e.assertCurrent});e.assertCurrent();return JSON.stringify([...new Set(decodeLocalWorkspaceGitPaths(t))])}async function selectLocalWorkspaceCanonicalPaths(e){e.signal?.throwIfAborted();e.assertCurrent();let t;if(e.preparedPaths)t=new Set(e.preparedPaths);else{const n=await requireGitBuffer(e.root,[`ls-files`,`--cached`,`-z`],{signal:e.signal,beforeRun:e.assertCurrent});e.assertCurrent();t=new Set(e.admittedPaths);for(const e of decodeLocalWorkspaceGitPaths(n))t.add(e)}if(e.baselinePaths)for(const n of e.baselinePaths)t.add(n);for(const e of t){const n=e.lastIndexOf(`/`);if(n!==-1)t.add(e.slice(0,n))}return t}async function prepareLocalWorkspaceBuildInputs(e){const{worktree:t,projection:n,signal:r,assertCurrent:i}=e;const a=parseLocalWorkspaceSourcePaths(n.source_paths_json);const o=t.removedAt===void 0?await selectLocalWorkspaceCanonicalPaths({root:t.path,admittedPaths:a,signal:r,assertCurrent:i}):new Set(a);const s=buildCacheInputPaths(o);if(t.snapshotRef&&n.baseline_ref){const e=await requireGitBuffer(t.repoRoot,[`ls-tree`,`-r`,`-z`,`--name-only`,t.snapshotRef],{signal:r,beforeRun:i});i();for(const t of buildCacheInputPaths(decodeLocalWorkspaceGitPaths(e))){s.add(t);o.add(t)}}if(s.size){const t=a.length;const n=new Set(a);for(const e of s)if(!n.has(e))a.push(e);if(a.length>t)await e.retainSourcePaths(JSON.stringify(a))}return{paths:o,retained:[...s]}}async function selectArchiveManifest(e){const{signal:t,assertCurrent:n}=e;if(!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/u.test(e.snapshotCommit))throw new Error(`Invalid archive snapshot commit`);const r=await requireGitBuffer(e.root,[`ls-tree`,`-r`,`-z`,`--name-only`,e.snapshotCommit],{signal:t,beforeRun:n,maxOutputBytes:67108864});n();const i=new Set(splitNullBuffer(r).map(e=>e.toString(`hex`)));const a=e.manifest.entries.filter(e=>i.has(Buffer.from(e.path).toString(`hex`)));const o=/* @__PURE__ */ new Set;for(const e of a)for(const t of workspacePathAncestors(e.path))o.add(t);if(a.length===e.manifest.entries.length&&o.size===(e.manifest.directories?.length??0)&&e.manifest.directories?.every(e=>o.has(e))!==false)return e.manifest;return{...e.manifest,entries:a,directories:[...o].toSorted()}}function localWorkspaceManifestDocument(e,t,n,r=`current`){const i={version:1,baseCommit:t,entries:[],directories:[],...n?{buildCacheInputs:n}:{}};for(const t of e)if(t.type===`directory`)i.directories.push(t.path);else i.entries.push(t);return archiveDocument(i,r)}function archiveDocument(e,t=`current`){const n=serializeWorkerWorkspaceManifest(e,t);return{rawManifest:n,manifestRef:`sha256:`+createHash(`sha256`).update(n).digest(`hex`)}}async function captureLocalWorkspaceArchiveBaseline(e){return archiveDocument(await selectArchiveManifest(e))}async function prepareLocalWorkspaceArchive(e){const t={version:1,baseCommit:e.baseCommit,buildCacheInputs:e.buildCacheInputs,entries:[],directories:[]};for(const{node:n}of e.records.values())if(n.type!==`directory`)t.entries.push(n);else if(n.path!==``)t.directories.push(n.path);const n=await selectArchiveManifest({...e,manifest:t,assertCurrent:()=>void 0});return n===t?{kind:`archive`}:{kind:`archive`,overlay:{base:archiveDocument(n),accepted:archiveDocument(t)}}}export{prepareLocalWorkspaceArchive as a,selectLocalWorkspaceCanonicalPaths as c,parseLocalWorkspaceSourcePaths as i,captureLocalWorkspaceArchiveBaseline as n,prepareLocalWorkspaceBuildInputs as o,localWorkspaceManifestDocument as r,readLocalWorkspaceGitExclusions as s,admitLocalWorkspaceSourcePaths as t};