openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
107 lines (106 loc) • 3.92 kB
JavaScript
import { a as extractErrorCode } from "./error-coercion-hDrHOSGy.mjs";
import { _ as formatMemoryDreamingDay } from "./dreaming-C72ZWZJr.mjs";
import { r as replaceFileAtomic } from "./security-runtime-C-zE_Zy_.mjs";
import "./error-runtime-BM740gJj.mjs";
import { c as readWorkspaceText, n as getMemoryWorkspaceMaintenance } from "./memory-workspace-files-Ekb7DRdf.mjs";
import "./memory-core-host-status-B88S-xY5.mjs";
import { t as appendMemoryHostEvent } from "./memory-host-events-BbYpZpdA.mjs";
import { n as withTrailingNewline, t as replaceManagedMarkdownBlock } from "./memory-host-markdown-DAu07cV4.mjs";
import { n as resolveMemoryCoreTimestamp, t as resolveMemoryCoreNowMs } from "./time-C9K_N17D.mjs";
import { l as updateDeepDreamsFile } from "./dreaming-dreams-file-Cbm7FmRe.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region extensions/memory-core/src/dreaming-markdown.ts
const PHASE_TITLES = {
light: "Light Sleep",
rem: "REM Sleep",
deep: "Deep Sleep"
};
function resolveDailyMemoryPath(e, t, n) {
const r = formatMemoryDreamingDay(t, n);
return path.join(e, "memory", `${r}.md`);
}
function resolveSeparateReportPath(e, t, n, r) {
const i = formatMemoryDreamingDay(n, r);
return path.join(e, "memory", "dreaming", t, `${i}.md`);
}
function shouldWriteInline(e) {
return e.mode === "inline" || e.mode === "both";
}
function shouldWriteSeparate(e) {
return e.mode === "separate" || e.mode === "both" || e.separateReports;
}
async function replaceDreamingMarkdownFile(e, t, n) {
const r = n ? getMemoryWorkspaceMaintenance(n) : void 0;
if (r) return await r.replaceReport(e, t);
const i = path.dirname(e);
await fs.mkdir(i, { recursive: true });
const a = (await fs.stat(i)).mode & 4095;
await replaceFileAtomic({
filePath: e,
content: t,
dirMode: a,
mode: 384,
preserveExistingMode: true,
tempPrefix: `${path.basename(e)}.dreaming`,
syncTempFile: true,
syncParentDir: true,
throwOnCleanupError: true
});
}
async function writeDreamingReport(e) {
const t = resolveMemoryCoreNowMs(e.nowMs);
const n = e.bodyLines.length > 0 ? e.bodyLines.join("\n") : e.phase === "deep" ? "- No durable changes." : "- No notable updates.";
let r;
let i;
if (e.phase === "deep") {
if (e.hasContent) r = await updateDeepDreamsFile(e);
} else if (shouldWriteInline(e.storage)) {
const i = resolveDailyMemoryPath(e.workspaceDir, t, e.timezone);
const a = await readWorkspaceText(e.workspaceDir, i).catch((e) => {
if (extractErrorCode(e) === "ENOENT") return;
throw e;
});
if (e.hasContent || a !== void 0) {
r = i;
const t = replaceManagedMarkdownBlock({
original: a ?? "",
heading: `## ${PHASE_TITLES[e.phase]}`,
startMarker: `<!-- openclaw:dreaming:${e.phase}:start -->`,
endMarker: `<!-- openclaw:dreaming:${e.phase}:end -->`,
body: n
});
await replaceDreamingMarkdownFile(r, withTrailingNewline(t), e.workspaceDir);
}
}
if (e.hasContent && shouldWriteSeparate(e.storage)) {
i = resolveSeparateReportPath(e.workspaceDir, e.phase, t, e.timezone);
const r = `# ${PHASE_TITLES[e.phase]}\n\n${n}\n`;
await replaceDreamingMarkdownFile(i, r, e.workspaceDir);
}
await appendMemoryHostEvent(e.workspaceDir, {
type: "memory.dream.completed",
timestamp: resolveMemoryCoreTimestamp(t),
phase: e.phase,
outcome: "completed",
...e.phase === "deep" || r ? { inlinePath: r } : {},
...i ? { reportPath: i } : {},
lineCount: e.bodyLines.length,
storageMode: e.storage.mode
});
return {
...r ? { inlinePath: r } : {},
...i ? { reportPath: i } : {}
};
}
async function writeDailyDreamingPhaseBlock(e) {
return await writeDreamingReport(e);
}
async function writeDeepDreamingReport(e) {
return (await writeDreamingReport({
...e,
phase: "deep"
})).reportPath;
}
//#endregion
export { writeDailyDreamingPhaseBlock as n, writeDeepDreamingReport as r, replaceDreamingMarkdownFile as t };