UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

43 lines (42 loc) 1.87 kB
import "./fs-safe-aqmM_n6V.js"; import { t as appendRegularFile } from "./regular-file-BD2zl6_l.js"; import path from "node:path"; import fs from "node:fs/promises"; //#region src/memory-host-sdk/events.ts /** Workspace-relative JSONL audit log for memory recall, promotion, and dream events. */ const MEMORY_HOST_EVENT_LOG_RELATIVE_PATH = path.join("memory", ".dreams", "events.jsonl"); /** Resolve the event log path inside a workspace without touching the filesystem. */ function resolveMemoryHostEventLogPath(workspaceDir) { return path.join(workspaceDir, MEMORY_HOST_EVENT_LOG_RELATIVE_PATH); } /** Append one memory host event, creating the dreams directory with symlink-safe writes. */ async function appendMemoryHostEvent(workspaceDir, event) { const eventLogPath = resolveMemoryHostEventLogPath(workspaceDir); await fs.mkdir(path.dirname(eventLogPath), { recursive: true }); await appendRegularFile({ filePath: eventLogPath, content: `${JSON.stringify(event)}\n`, rejectSymlinkParents: true }); } /** Read recent memory host events, ignoring corrupt JSONL lines left by partial writes. */ async function readMemoryHostEvents(params) { const eventLogPath = resolveMemoryHostEventLogPath(params.workspaceDir); const raw = await fs.readFile(eventLogPath, "utf8").catch((err) => { if (err?.code === "ENOENT") return ""; throw err; }); if (!raw.trim()) return []; const events = raw.split("\n").map((line) => line.trim()).filter(Boolean).flatMap((line) => { try { return [JSON.parse(line)]; } catch { return []; } }); if (!Number.isFinite(params.limit)) return events; const limit = Math.max(0, Math.floor(params.limit)); return limit === 0 ? [] : events.slice(-limit); } //#endregion export { resolveMemoryHostEventLogPath as i, appendMemoryHostEvent as n, readMemoryHostEvents as r, MEMORY_HOST_EVENT_LOG_RELATIVE_PATH as t };