UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 19.1 kB
import{Ut as e,ir as t}from"./git-operation.worker.js";import{a as n,i as r,n as i,o as a,r as o}from"./git-operation.worker-workspace-hash-memo-CMIph4rA.mjs";import{c as s,g as c,o as l,s as u,v as d}from"./git-operation.worker-workspace-manifest-DsRMcvMP.mjs";import{a as f,i as p,n as m,o as h,r as g,t as _}from"./git-operation.worker-workspace-manifest-comparison-E7MDmqwp.mjs";import v from"node:fs/promises";import{createHash as y}from"node:crypto";import{deserialize as b,serialize as x}from"node:v8";import S,{deflateRawSync as C,zstdCompressSync as w,zstdDecompressSync as T}from"node:zlib";function digest(e,...t){return`sha256:${y(`sha256`).update(JSON.stringify([`openclaw.local-workspace.${e}.v1`,...t])).digest(`hex`)}`}const E=digest(`empty`);function parentPath(e){let t=e.lastIndexOf(`/`);return t===-1?``:e.slice(0,t)}function localWorkspaceManifestRootReference(e,t){return digest(`root`,t,e.get(``).treeRef)}function createLocalWorkspaceManifestTree(e){let t={node:{path:``,type:`directory`},priority:``},n=/* @__PURE__ */ new Map([[``,t]]),r=/* @__PURE__ */ new Map;for(let t of e.toSorted((e,t)=>e.path<t.path?-1:+(e.path>t.path))){if(t.path===``)throw Error(`The workspace manifest root cannot be changed`);let e=parentPath(t.path),i=n.get(e);if(i?.node.type!==`directory`)throw Error(`Missing workspace manifest directory: ${e}`);let a={node:t,priority:digest(`priority`,t.path)},o=r.get(e)??[];for(;o.length&&o.at(-1).priority>a.priority;)a.left=o.pop();let s=o.at(-1);s?s.right=a:i.children=a,o.push(a),r.set(e,o),n.set(t.path,a)}let i=/* @__PURE__ */ new Map,finish=e=>{if(!e)return;let t=manifestRecord(e.node,finish(e.left),finish(e.right),finish(e.children));return i.set(e.node.path,t),t};return finish(t),i}function manifestRecord(e,t,n,r,i){let a=e.type===`directory`?digest(`directory`,e.path,r?.treeRef??E):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??E,a,n?.treeRef??E)}}function updateLocalWorkspaceManifestTree(e,t){let n=/* @__PURE__ */ new Map,r=/* @__PURE__ */ new Set,i=/* @__PURE__ */ new Map,recordAt=t=>t===null?void 0:e.get(t);function write(t,i,a,o){let 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);return t===void 0&&(t=digest(`priority`,e),i.set(e,t)),t}function precedes(e,t){let n=priority(e),r=priority(t);return n<r||n===r&&e<t}function insert(t,n){if(t===null)return write(n,null,null,null),n.path;let r=e.get(t);if(n.path===t){if(n.type!==`directory`&&r.children!==null)throw Error(`Cannot replace nonempty workspace directory: ${t}`);return write(n,r.left,r.right,r.children),t}if(n.path<t){let i=insert(r.left,n);if(precedes(i,t)){let n=e.get(i);return write(r.node,n.right,r.right,r.children),write(n.node,n.left,t,n.children),i}write(r.node,i,r.right,r.children)}else{let i=insert(r.right,n);if(precedes(i,t)){let n=e.get(i);return write(r.node,r.left,n.left,r.children),write(n.node,t,n.right,n.children),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)){let r=e.get(t);return write(r.node,r.left,merge(r.right,n),r.children),t}let r=e.get(n);return write(r.node,merge(t,r.left),r.right,r.children),n}function remove(t,i){if(t===null)return null;let a=e.get(t);if(i===t){if(a.children!==null)throw Error(`Cannot remove nonempty workspace directory: ${t}`);let i=merge(a.left,a.right);return e.delete(t),n.delete(t),r.add(t),i}return i<t?write(a.node,remove(a.left,i),a.right,a.children):write(a.node,a.left,remove(a.right,i),a.children),t}function refresh(t,n){let r=e.get(t);return write(r.node,n<t?refresh(r.left,n):r.left,n>t?refresh(r.right,n):r.right,r.children),t}e.has(``)||write({path:``,type:`directory`},null,null,null);let a=t.toSorted((e,t)=>{if(!!e.node!=!!t.node)return e.node?1:-1;let n=e.path<t.path?-1:+(e.path>t.path);return e.node?n:-n});for(let t of a){if(t.path===``)throw Error(`The workspace manifest root cannot be changed`);if(!t.node&&!e.has(t.path))continue;let n=parentPath(t.path),r=e.get(n);if(r?.node.type!==`directory`)throw Error(`Missing workspace manifest directory: ${n}`);let i=t.node?insert(r.children,t.node):remove(r.children,t.path);for(;;){let t=e.get(n);if(write(t.node,t.left,t.right,i),n===``)break;let r=parentPath(n);i=refresh(e.get(r).children,n),n=r}}return{records:[...n.values()],removed:[...r]}}function decodeLocalWorkspaceTree(e,t=!1){let n=/* @__PURE__ */ new Map;for(let r of e){let e=t?[JSON.parse(r)]:JSON.parse(r);for(let t of e)n.set(t.node.path,t)}return n}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 prepareLocalWorkspaceTreeUpdate(e,t,n,r){let i=/* @__PURE__ */ new Map;if(r){i.set(null,r);for(let t of[``,...n]){let n=e.get(t)?.children;n!=null&&i.set(t,e.get(n).treeRef)}}let 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){let i=t&&new Set(t),a=/* @__PURE__ */ new Set([``]);if(i===void 0)for(let t of e.values())t.children!==null&&a.add(t.children);else for(let t of i){if(t===``)continue;let n=t.slice(0,Math.max(0,t.lastIndexOf(`/`))),r=e.get(n)?.children;r!=null&&a.add(r)}let o=[];for(let t of a){let a=e.get(t);if(n?.has(a.treeRef))continue;let s=t===``?null:t.slice(0,Math.max(0,t.lastIndexOf(`/`))),c=i&&r?.get(s),l=[],u=[],d=[a];for(;d.length;){let t=d.pop();(!c||t===a||i.has(t.node.path))&&(l.push(t),t.children!==null&&u.push(e.get(t.children).treeRef));for(let n of[t.left,t.right])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){let i=encodeLocalWorkspaceTreePacks(e,t,n,r),a=[];for(let e=0;e<i.length;e+=256)a.push(C(x({packs:i.slice(e,e+256)})));return a}function prepareLocalWorkspaceSourcePathSet(e){return{digest:`sha256:${y(`sha256`).update(e).digest(`hex`)}`,compressed:w(Buffer.from(e))}}let D;function resolveZstdCodec(){return D??={compress:(e,t,n)=>t===void 0&&n===void 0?S.zstdCompressSync(e):S.zstdCompressSync(e,{params:{...t===void 0?{}:{[S.constants.ZSTD_c_compressionLevel]:t},...n===void 0?{}:{[S.constants.ZSTD_c_checksumFlag]:n}}}),decompress:(e,t)=>S.zstdDecompressSync(e,t===void 0?void 0:{maxOutputLength:t,chunkSize:Math.max(S.constants.Z_MIN_CHUNK,Math.min(S.constants.Z_DEFAULT_CHUNK,t+1))})}}const O=-1n;function filePaths(e){let t=[];for(let{node:n}of e.values())n.type===`file`&&t.push(n.path);return t.sort(),t}function prepareLocalWorkspaceFingerprints(e,t,n,r,i){let sourceDevice=e=>i&&e===i.snapshotDev?i.sourceDev:e,o=n===null||Buffer.isBuffer(n)?n:Buffer.from(n.buffer,n.byteOffset,n.byteLength),s=filePaths(e),c;if(n)try{let e=resolveZstdCodec().decompress(n,45+d*16);e.length===45+s.length*16&&e.readUInt8(0)===1&&e.subarray(9,41).equals(Buffer.from(t.slice(7),`hex`))&&e.readUInt32LE(41)===s.length&&(c=e)}catch{}let l=/* @__PURE__ */ new Map;for(let e=0;e<s.length;e++)l.set(s[e],45+e*16);let u=c?.readBigUInt64LE(1),f=/* @__PURE__ */ new Map,p=/* @__PURE__ */ new Map,m=c!==void 0,h=0;return{reuseFile(t,n,i){let o=l.get(t),s=e.get(t)?.node;if(!c||o===void 0||s?.type!==`file`)m=!1;else{if(sourceDevice(i.dev)===u&&i.ino===c.readBigUInt64LE(o)&&i.ctimeNs===c.readBigInt64LE(o+8)&&s.size===Number(i.size)&&a(i.ctimeNs,r))return f.set(t,n),s;m=!1}},observe(e,t,n){f.get(e)===t?h++:(m=!1,f.delete(e),p.set(e,{identity:t,reusable:n}))},encode(e,n,i){if(i&&!c)return null;if(n===t&&m&&h===s.length)return o;let d=s;if(n!==t){let t=s.filter(t=>e.get(t)?.node.type===`file`),n=[];for(let{node:t}of e.values())t.type===`file`&&!l.has(t.path)&&n.push(t.path);n.sort(),d=[];let r=0;for(let e of t){for(;r<n.length&&n[r]<e;)d.push(n[r++]);d.push(e)}for(;r<n.length;)d.push(n[r++])}let g=Buffer.alloc(45+d.length*16);g.writeUInt8(1,0),Buffer.from(n.slice(7),`hex`).copy(g,9),g.writeUInt32LE(d.length,41);let _=i?u:void 0;for(let e=0;e<d.length;e++){let t=d[e],n=45+e*16;if(f.has(t)||i&&l.has(t)&&!i.has(t)){let e=l.get(t);if(_??=u,_!==u)return null;c.copy(g,n,e,e+16);continue}let o=p.get(t);if(!o){if(i){g.writeBigInt64LE(O,n+8);continue}return null}let s=o.identity.split(`:`),m=sourceDevice(BigInt(s[1]));if(_??=m,m!==_)return null;let h=BigInt(s[5]);g.writeBigUInt64LE(BigInt(s[2]),n),g.writeBigInt64LE(o.reusable??a(h,r)?h:O,n+8)}return g.writeBigUInt64LE(_??0n,1),resolveZstdCodec().compress(g)}}}function applyWorkspaceSourceOverlay(e,t,n){let r=g(e),i=g(n),a=g(t),o=_(r,i),s=new Set([...o].filter(e=>i.get(e)?.type!==`directory`&&(i.has(e)||r.get(e)?.type!==`directory`)));for(let e of a.keys())(o.has(e)||m(s,e))&&a.delete(e);for(let e of o){let t=i.get(e);t&&a.set(e,t)}for(let e of a.keys())for(let t of[...c(e)].toReversed())a.set(t,{path:t,type:`directory`});return{version:1,baseCommit:n.baseCommit,entries:[...a.values()].filter(e=>e.type===`file`||e.type===`symlink`),directories:[...a.values()].flatMap(e=>e.type===`directory`?[e.path]:[])}}let k;function decodeUtf8(e){return Buffer.from(e.buffer,e.byteOffset,e.byteLength).toString(`utf8`)}function decodeManifestValue(e){return JSON.parse(decodeUtf8(e.input.payload))}async function withHashes(e,t){let a={contentHashCount:0,memoHitCount:0};if(!e)return{value:await n(t),hashes:[],metrics:a};let s=new Map(e.entries),c=new Map(s),l=await(e.owner===`worker`?o(c,t,a):r(c,t,a));return i(c),{value:l,hashes:[...c].filter(([e,t])=>s.get(e)!==t),metrics:a}}async function executeWorkspaceManifestComputation(n,r){switch(n.type){case`workspace.manifest.template`:{let{readActualWorkspaceManifestImpl:e}=await import(`./git-operation.worker-workspace-actual-manifest-Y3A72WaR.mjs`).then(e=>e.a),t=decodeManifestValue(n),r=t.previous,i=new Map(r?u(r.rawManifest,r.manifestRef).entries.map(e=>[e.path,e]):[]),a=new Set(r?.changedPaths),{rawManifest:o,manifestRef:s}=await e({...t,buildCacheInputs:new Set(t.buildCacheInputs)},r?(e,t,n)=>{let r=i.get(e);return!a.has(e)&&!/(?:^|\/)node_modules\//u.test(e)&&r?.type===`file`&&r.size===Number(n.size)&&r.mode===l(Number(n.mode&511n))?r:void 0}:void 0);return{rawManifest:o,manifestRef:s}}case`workspace.manifest.local`:{let{executeLocalWorkspaceManifestRead:t}=await import(`./git-operation.worker-local-workspace-manifest-scan-4gzGP_R5.mjs`),r=n.input.owner,i=k?.owner===r?k.records:void 0;k=void 0;let a=await e({type:`workspace.manifest.read`,input:{knownTreeRoot:i?.get(``)?.treeRef}}),[o,s]=b(T(a));o.records&&(i=decodeLocalWorkspaceTree(o.records,!o.row.tree_root_digest));let c=await t(o,s,i);return i?.has(``)&&o.row.tree_root_digest&&(s.kind===`scan`||s.kind===`probe`||s.kind===`materialize`||s.kind===`archive`)&&(k={owner:r,records:i}),c}case`workspace.manifest.base-refresh`:{let{readWorkspaceBaseRefreshTrees:e}=await import(`./git-operation.worker-workspace-base-refresh-trees-DNNaH-Ff.mjs`),t=n.input;return await withHashes(t.hashes,async()=>{let n=u(decodeUtf8(t.baseRaw),t.baseRef,t.buildCacheInputs),[r,i]=await e(t,n),a=applyWorkspaceSourceOverlay(r,n,i);a.buildCacheInputs=[.../* @__PURE__ */ new Set([...t.buildCacheInputs,...i.entries.map(e=>e.path)])];let o=g(r),c=g(a);for(let[e,r]of g(n))if(!o.has(e)&&!h(r,c.get(e)))throw Error(`The selected base conflicts with saved workspace data at ${e}; move that saved file before retrying ${t.action}.`);let l=s(a);return{rawManifest:l,manifestRef:`sha256:${y(`sha256`).update(l).digest(`hex`)}`,buildCacheInputs:a.buildCacheInputs}})}case`workspace.manifest.equal`:{let e=n.input,t=g(u(decodeUtf8(e.baseRaw),e.baseRef,e.buildCacheInputs)),r=g(u(decodeUtf8(e.currentRaw),e.currentRef,e.buildCacheInputs));return t.size===r.size&&[...t].every(([e,t])=>h(t,r.get(e)))}case`workspace.manifest.remote-capture`:{let{captureNodeWorkspaceManifestImpl:e}=await import(`./git-operation.worker-workspace-manifest-script.runtime-4XkY36cJ.mjs`);return await e(n.input,r)}case`workspace.manifest.nodes`:{let{localWorkspaceNode:e}=await import(`./git-operation.worker-workspace-reconcile-fs-2rlVLgX2.mjs`),r=decodeManifestValue(n);return await withHashes(r.hashes,async()=>{let n=await t({tasks:r.paths.map(t=>async()=>[t,await e(r.root,t)]),limit:4,errorMode:`stop`});if(n.hasError)throw n.firstError;return n.results})}case`workspace.manifest.result`:case`workspace.manifest.staged`:{let{loadStagedWorkerWorkspace:e,materializeStagedWorkerWorkspace:t}=await import(`./git-operation.worker-workspace-result-inventory.runtime-BcmHxqyi.mjs`),{nodes:r,...i}=await e(n.input.root,n.input.ref,n.input.buildCacheInputs,n.input.reconciliation),a=n.input.reconciliation;return(n.input.verifyContents||n.input.stagingRoot&&(!a||!a.alreadyAccepted&&a.baseManifestRef===i.baseManifestRef))&&await t(n.input.root,n.input.stagingRoot,i),n.type===`workspace.manifest.result`?{gitHistory:i.gitHistory,baseManifestRef:i.baseManifestRef,currentManifestRef:i.currentManifestRef,base:{baseCommit:i.base.baseCommit},current:{baseCommit:i.current.baseCommit},changed:i.changed,changedEntryCount:i.changedEntries.length}:{...i,reconciliation:f(i.base,i.current,r)}}case`workspace.manifest.stage-input`:{let{buildWorkspaceStageInput:e}=await import(`./git-operation.worker-workspace-result-preparation.runtime-BYrbIVgd.mjs`);return await e(n.input,r)}case`workspace.manifest.tree-input`:{let{buildWorkspaceTreeInput:e}=await import(`./git-operation.worker-workspace-result-preparation.runtime-BYrbIVgd.mjs`);return await e(decodeManifestValue(n),r)}case`workspace.manifest.capture`:case`workspace.manifest.snapshot`:case`workspace.manifest.document`:case`workspace.manifest.initial`:case`workspace.manifest.reference`:{let{readActualWorkspaceManifestImpl:e}=await import(`./git-operation.worker-workspace-actual-manifest-Y3A72WaR.mjs`).then(e=>e.a),t=decodeManifestValue(n),r=n.type===`workspace.manifest.initial`?createLocalWorkspaceManifestTree([]):void 0,i=r?prepareLocalWorkspaceFingerprints(r,r.get(``).treeRef,null,Date.now()):void 0,a=await withHashes(t.hashes,()=>e({...t,includePaths:t.includePaths&&new Set(t.includePaths),gitIgnore:t.gitIgnore&&{paths:new Set(t.gitIgnore.paths),retainedPaths:new Set(t.gitIgnore.retainedPaths)},buildCacheInputs:t.buildCacheInputs&&new Set(t.buildCacheInputs),preserveDirectories:t.preserveDirectories&&new Set(t.preserveDirectories)},void 0,i?.observe));if(i){let{manifest:e,rawManifest:t,manifestRef:n}=a.value,r=createLocalWorkspaceManifestTree([...e.entries,...(e.directories??[]).map(e=>({path:e,type:`directory`}))]);return{...a,value:{rawManifest:t,manifestRef:n,projectionFingerprints:i.encode(r,r.get(``).treeRef)}}}if(n.type===`workspace.manifest.document`){let{manifestRef:e,rawManifest:t}=a.value;return{...a,value:{manifestRef:e,rawManifest:t}}}return n.type===`workspace.manifest.reference`?{...a,value:a.value.manifestRef}:n.type===`workspace.manifest.snapshot`?a:{...a,value:{manifest:a.value.manifest,manifestRef:a.value.manifestRef}}}case`workspace.manifest.file`:{let{readWorkspaceFileSnapshotWithLimit:e}=await import(`./git-operation.worker-workspace-actual-manifest-Y3A72WaR.mjs`).then(e=>e.a),t=decodeManifestValue(n);return await withHashes(t.hashes,()=>e(t.path,t.maxBytes,t.root))}case`workspace.reconcile.temporary`:{let{join:e}=await import(`node:path`),{resolvePreferredOpenClawTmpDir:t}=await import(`./git-operation.worker.js`).then(e=>e.En);return e(t(),n.input.prefix)}case`workspace.reconcile.ownership`:{let{assertWorkspaceInputOwnership:e}=await import(`./git-operation.worker-workspace-reconcile-core-Ci1PG_NC.mjs`);return await e(n.input)}case`workspace.reconcile.recover`:{let{recoverWorkerWorkspaceReconciliation:e}=await import(`./git-operation.worker-workspace-reconcile-recovery-CG-sD8wp.mjs`);return await e({...n.input,journal:{...n.input.journal,basePack:await v.readFile(n.input.basePackPath)},assertCurrent:r})}case`workspace.reconcile.apply`:{let{root:e,appliedEntries:t,buildCacheInputs:i,patchPath:a}=n.input,{root:o}=await import(`./git-operation.worker-fs-safe-2XqdJR5K.mjs`).then(e=>e.n),{createStagedInputPathMatcher:s}=await import(`./git-operation.worker-staged-inputs-DZiTgX91.mjs`).then(e=>e.o),{prepareNonDirectoryTargets:c}=await import(`./git-operation.worker-workspace-reconcile-derived-paths-CuPCc3j9.mjs`),{applyWorkspacePatch:l}=await import(`./git-operation.worker-workspace-reconcile-recovery-CG-sD8wp.mjs`),{applyWorkspaceDirectoryChanges:u}=await import(`./git-operation.worker-workspace-reconcile-core-Ci1PG_NC.mjs`),d=s(await o(e));return await c(e,t,async e=>!!i?.has(e)||await d(e),r,i!==void 0),await l({root:e,patch:await v.readFile(a),assertCurrent:r}),await u({...n.input,assertCurrent:r})}case`workspace.reconcile.diff`:{let{readWorkspacePatch:e}=await import(`./git-operation.worker-workspace-reconcile-recovery-CG-sD8wp.mjs`);return await e(n.input.root,n.input.baseTree,n.input.currentTree)}case`workspace.reconcile.patch`:{let{createWorkspacePatch:e}=await import(`./git-operation.worker-workspace-reconcile-recovery-CG-sD8wp.mjs`);return await e(decodeManifestValue(n))}case`workspace.reconcile.preflight`:{let{preflightWorkspaceApplyImpl:e}=await import(`./git-operation.worker-workspace-reconcile-preflight-CX80AFmY.mjs`),t=decodeManifestValue(n);return await withHashes(t.hashes,()=>e(t))}case`workspace.manifest.paths`:{let e=n.input,t=u(decodeUtf8(e.raw),e.expectedRef,e.buildCacheInputs);return/* @__PURE__ */ new Set([...t.directories??[],...t.entries.map(e=>e.path)])}case`workspace.manifest.parse`:{let e=decodeUtf8(n.input.raw),t=n.input.expectedRef??`sha256:${y(`sha256`).update(e).digest(`hex`)}`;return{manifest:u(e,t),manifestRef:t}}case`workspace.manifest.overlay`:{let e=decodeManifestValue(n),t=s(applyWorkspaceSourceOverlay(e.source,e.prepared,e.incoming)),r=`sha256:${y(`sha256`).update(t).digest(`hex`)}`;return{manifest:u(t,r),manifestRef:r}}case`workspace.manifest.serialize`:{let e=decodeManifestValue(n),t=s(e.manifest);return{raw:t,manifestRef:`sha256:${y(`sha256`).update(t).digest(`hex`)}`}}case`workspace.manifest.pair`:{let e=decodeUtf8(n.input.baseRaw),t=decodeUtf8(n.input.currentRaw),r=u(e,n.input.baseRef,n.input.buildCacheInputs),i=u(t,n.input.currentRef,n.input.buildCacheInputs),a=p(r,i,!1);return{base:r,current:i,...a,paths:a.entries.map(e=>e.path)}}}throw Error(`Unsupported workspace computation`)}export{localWorkspaceDigestReference as a,prepareLocalWorkspaceTreeUpdate as c,updateLocalWorkspaceManifestTree as d,executeWorkspaceManifestComputation,localWorkspaceDigestBytes as i,createLocalWorkspaceManifestTree as l,resolveZstdCodec as n,prepareLocalWorkspaceSourcePathSet as o,decodeLocalWorkspaceTree as r,prepareLocalWorkspaceTreePacks as s,prepareLocalWorkspaceFingerprints as t,localWorkspaceManifestRootReference as u};