openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
132 lines (131 loc) • 4.9 kB
JavaScript
import { i as extractErrorCode } from "./error-coercion-D_-xJ90S.js";
import { n as replaceFileAtomic } from "./replace-file-BAJ-TWzD.js";
import { y as formatMemoryDreamingDay } from "./dreaming-DmJZL5-O.js";
import "./error-runtime-Bz9Tw57Z.js";
import "./security-runtime-Ckf0kc0h.js";
import "./memory-core-host-status-DAqa4uMk.js";
import { t as appendMemoryHostEvent } from "./memory-host-events-DVbON-YI.js";
import { n as withTrailingNewline, t as replaceManagedMarkdownBlock } from "./memory-host-markdown-mHNl3RAL.js";
import { n as resolveMemoryCoreTimestamp, t as resolveMemoryCoreNowMs } from "./time-BhFVUM0b.js";
import { c as updateDeepDreamsFile } from "./dreaming-dreams-file-DQfkc_tk.js";
import path from "node:path";
import fs from "node:fs/promises";
//#region extensions/memory-core/src/dreaming-markdown.ts
const DAILY_PHASE_HEADINGS = {
light: "## Light Sleep",
rem: "## REM Sleep"
};
const DAILY_PHASE_LABELS = {
light: "light",
rem: "rem"
};
function resolvePhaseMarkers(phase) {
const label = DAILY_PHASE_LABELS[phase];
return {
start: `<!-- openclaw:dreaming:${label}:start -->`,
end: `<!-- openclaw:dreaming:${label}:end -->`
};
}
function resolveDailyMemoryPath(workspaceDir, epochMs, timezone) {
const isoDay = formatMemoryDreamingDay(epochMs, timezone);
return path.join(workspaceDir, "memory", `${isoDay}.md`);
}
function resolveSeparateReportPath(workspaceDir, phase, epochMs, timezone) {
const isoDay = formatMemoryDreamingDay(epochMs, timezone);
return path.join(workspaceDir, "memory", "dreaming", phase, `${isoDay}.md`);
}
function shouldWriteInline(storage) {
return storage.mode === "inline" || storage.mode === "both";
}
function shouldWriteSeparate(storage) {
return storage.mode === "separate" || storage.mode === "both" || storage.separateReports;
}
async function replaceDreamingMarkdownFile(filePath, content) {
const directoryPath = path.dirname(filePath);
await fs.mkdir(directoryPath, { recursive: true });
const dirMode = (await fs.stat(directoryPath)).mode & 4095;
await replaceFileAtomic({
filePath,
content,
dirMode,
mode: 384,
preserveExistingMode: true,
tempPrefix: `${path.basename(filePath)}.dreaming`,
syncTempFile: true,
syncParentDir: true,
throwOnCleanupError: true
});
}
async function writeDailyDreamingPhaseBlock(params) {
const nowMs = resolveMemoryCoreNowMs(params.nowMs);
const body = params.bodyLines.length > 0 ? params.bodyLines.join("\n") : "- No notable updates.";
let inlinePath;
let reportPath;
if (shouldWriteInline(params.storage)) {
inlinePath = resolveDailyMemoryPath(params.workspaceDir, nowMs, params.timezone);
await fs.mkdir(path.dirname(inlinePath), { recursive: true });
const original = await fs.readFile(inlinePath, "utf-8").catch((err) => {
if (extractErrorCode(err) === "ENOENT") return "";
throw err;
});
const markers = resolvePhaseMarkers(params.phase);
const updated = replaceManagedMarkdownBlock({
original,
heading: DAILY_PHASE_HEADINGS[params.phase],
startMarker: markers.start,
endMarker: markers.end,
body
});
await replaceDreamingMarkdownFile(inlinePath, withTrailingNewline(updated));
}
if (shouldWriteSeparate(params.storage)) {
reportPath = resolveSeparateReportPath(params.workspaceDir, params.phase, nowMs, params.timezone);
const report = [
`# ${params.phase === "light" ? "Light Sleep" : "REM Sleep"}`,
"",
body,
""
].join("\n");
await replaceDreamingMarkdownFile(reportPath, report);
}
await appendMemoryHostEvent(params.workspaceDir, {
type: "memory.dream.completed",
timestamp: resolveMemoryCoreTimestamp(nowMs),
phase: params.phase,
outcome: "completed",
...inlinePath ? { inlinePath } : {},
...reportPath ? { reportPath } : {},
lineCount: params.bodyLines.length,
storageMode: params.storage.mode
});
return {
...inlinePath ? { inlinePath } : {},
...reportPath ? { reportPath } : {}
};
}
async function writeDeepDreamingReport(params) {
const nowMs = resolveMemoryCoreNowMs(params.nowMs);
const body = params.bodyLines.length > 0 ? params.bodyLines.join("\n") : "- No durable changes.";
const inlinePath = await updateDeepDreamsFile({
workspaceDir: params.workspaceDir,
bodyLines: params.bodyLines
});
let reportPath;
if (shouldWriteSeparate(params.storage)) {
reportPath = resolveSeparateReportPath(params.workspaceDir, "deep", nowMs, params.timezone);
await replaceDreamingMarkdownFile(reportPath, `# Deep Sleep\n\n${body}\n`);
}
await appendMemoryHostEvent(params.workspaceDir, {
type: "memory.dream.completed",
timestamp: resolveMemoryCoreTimestamp(nowMs),
phase: "deep",
outcome: "completed",
inlinePath,
...reportPath ? { reportPath } : {},
lineCount: params.bodyLines.length,
storageMode: params.storage.mode
});
return reportPath;
}
//#endregion
export { writeDeepDreamingReport as n, writeDailyDreamingPhaseBlock as t };