UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

90 lines (89 loc) • 3.24 kB
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs"; import { t as SESSION_ARCHIVE_ZSTD_SUFFIX } from "./transcript-file-names-mCC_xfFb.mjs"; import { n as resolvePreferredOpenClawTmpDir } from "./tmp-openclaw-dir-Bf0oU1Cn.mjs"; import { t as resolveZstdCodec } from "./zstd-codec-DyrkXXaa.mjs"; import fs from "node:fs"; import path from "node:path"; import { createHash, randomUUID } from "node:crypto"; import { walkDirectorySync } from "@openclaw/fs-safe/walk"; //#region src/config/sessions/archive-compression.ts const zstdCodec = resolveZstdCodec(); function encodeSessionArchiveContent(e) { const t = Buffer.from(e, "utf8"); if (t.length === 0) return { bytes: t, suffix: "" }; return { bytes: zstdCodec.compress(t), suffix: SESSION_ARCHIVE_ZSTD_SUFFIX }; } function readSessionArchiveContentSync(e, t) { if (!e.endsWith(".zst")) return fs.readFileSync(e, "utf8"); const n = fs.readFileSync(e); const r = t ?? n.length + 1048576; try { return zstdCodec.decompress(n, r).toString("utf8"); } catch (t) { if (t instanceof Error && hasErrnoCode(t, "ERR_BUFFER_TOO_LARGE")) t.message = `Session archive ${e} exceeds ${r} decompressed bytes; the archive is unchanged. Export it as uncompressed JSONL to read it.`; throw t; } } /** * Materializes a compressed archive as a plain JSONL cache file and returns * the readable path; plain archives pass through untouched. Source identity * validates cached bytes so downstream readers need not handle compression. */ function materializeSessionArchiveForRead(e) { if (!e.endsWith(".zst")) return e; const t = path.join(resolvePreferredOpenClawTmpDir(), "session-archive-read-cache"); const n = createHash("sha256").update(e).digest("hex").slice(0, 32); let r; try { r = fs.statSync(e); } catch (e) { removeMaterializedArchiveCacheEntries(t, n); throw e; } const i = path.join(t, `${n}-${r.size}-${Math.trunc(r.mtimeMs)}.jsonl`); sweepMaterializedArchiveCache(t); if (fs.existsSync(i)) return i; const a = readSessionArchiveContentSync(e); removeMaterializedArchiveCacheEntries(t, n, path.basename(i)); fs.mkdirSync(t, { recursive: true, mode: 448 }); const o = `${i}.${process.pid}.${randomUUID()}.tmp`; fs.writeFileSync(o, a, { encoding: "utf8", mode: 384 }); fs.renameSync(o, i); return i; } const MATERIALIZED_ARCHIVE_CACHE_TTL_MS = 864e5; let lastMaterializedArchiveCacheSweepMs = 0; function sweepMaterializedArchiveCache(e) { const t = Date.now(); if (t - lastMaterializedArchiveCacheSweepMs < MATERIALIZED_ARCHIVE_CACHE_TTL_MS / 24) return; lastMaterializedArchiveCacheSweepMs = t; for (const n of walkDirectorySync(e, { maxDepth: 1, symlinks: "include" }).entries) try { if (t - fs.statSync(n.path).mtimeMs > MATERIALIZED_ARCHIVE_CACHE_TTL_MS) fs.rmSync(n.path, { force: true }); } catch {} } function removeMaterializedArchiveCacheEntries(e, t, n) { for (const r of walkDirectorySync(e, { maxDepth: 1, symlinks: "include" }).entries) { if (!r.name.startsWith(`${t}-`) || r.name === n || r.name.endsWith(".tmp")) continue; fs.rmSync(r.path, { force: true }); } } //#endregion export { materializeSessionArchiveForRead as n, readSessionArchiveContentSync as r, encodeSessionArchiveContent as t };