openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 1.91 kB
JavaScript
import{o as e}from"./transcript-path-guards-u0N7Z9_x.mjs";import{n as t}from"./transcript-tmp-openclaw-dir-m_279lEd.mjs";import"./transcript-artifacts-BcxHhf8q.mjs";import{t as n}from"./transcript-zstd-codec-Bwg8OR0e.mjs";import r from"node:path";import{createHash as i,randomUUID as a}from"node:crypto";import o from"node:fs";import{walkDirectorySync as s}from"@openclaw/fs-safe/walk";const c=n();function readSessionArchiveContentSync(t,n){if(!t.endsWith(`.zst`))return o.readFileSync(t,`utf8`);let r=o.readFileSync(t),i=n??r.length+1048576;try{return c.decompress(r,i).toString(`utf8`)}catch(n){throw n instanceof Error&&e(n,`ERR_BUFFER_TOO_LARGE`)&&(n.message=`Session archive ${t} exceeds ${i} decompressed bytes; the archive is unchanged. Export it as uncompressed JSONL to read it.`),n}}function materializeSessionArchiveForRead(e){if(!e.endsWith(`.zst`))return e;let n=r.join(t(),`session-archive-read-cache`),s=i(`sha256`).update(e).digest(`hex`).slice(0,32),c;try{c=o.statSync(e)}catch(e){throw removeMaterializedArchiveCacheEntries(n,s),e}let l=r.join(n,`${s}-${c.size}-${Math.trunc(c.mtimeMs)}.jsonl`);if(sweepMaterializedArchiveCache(n),o.existsSync(l))return l;let u=readSessionArchiveContentSync(e);removeMaterializedArchiveCacheEntries(n,s,r.basename(l)),o.mkdirSync(n,{recursive:!0,mode:448});let d=`${l}.${process.pid}.${a()}.tmp`;return o.writeFileSync(d,u,{encoding:`utf8`,mode:384}),o.renameSync(d,l),l}const l=864e5;let u=0;function sweepMaterializedArchiveCache(e){let t=Date.now();if(!(t-u<l/24)){u=t;for(let n of s(e,{maxDepth:1,symlinks:`include`}).entries)try{t-o.statSync(n.path).mtimeMs>l&&o.rmSync(n.path,{force:!0})}catch{}}}function removeMaterializedArchiveCacheEntries(e,t,n){for(let r of s(e,{maxDepth:1,symlinks:`include`}).entries)r.name.startsWith(`${t}-`)&&r.name!==n&&!r.name.endsWith(`.tmp`)&&o.rmSync(r.path,{force:!0})}export{materializeSessionArchiveForRead as t};