openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
107 lines (106 loc) • 4.19 kB
JavaScript
import { a as lstatIfExists } from "./path-existence-Co7-GyVr.mjs";
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs";
import { a as canonicalPathFromExistingAncestor } from "./fs-safe-BbWvmdVn.mjs";
import "./path-existence-runtime-LOpU--Vt.mjs";
import "./file-access-runtime-BD2Fjfbw.mjs";
import { n as MIGRATION_REASON_TARGET_EXISTS, o as createMigrationItem } from "./migration-C73lp9ea.mjs";
import { a as listMarkdownFiles, n as CLAUDE_AUTO_MEMORY_MAX_SCAN_ENTRIES, t as CLAUDE_AUTO_MEMORY_MAX_FILES } from "./source-BhxkBFsC.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region extensions/migrate-claude/memory.ts
const MIGRATION_REASON_TARGET_NOT_REGULAR = "target is not a regular file";
function collectionSlug(e, t) {
return `${e.normalize("NFKD").replaceAll(/[^a-zA-Z0-9._-]+/gu, "-").replaceAll(/^[._-]+|[._-]+$/gu, "").slice(0, 72) || "project"}-${t}`;
}
async function resolveMemoryDestinationBoundary(e, t) {
return {
source: await fs.realpath(e),
workspace: await canonicalPathFromExistingAncestor(t)
};
}
async function assertSafeMemoryDestination(e, t) {
const n = await canonicalPathFromExistingAncestor(t);
if (!isPathInside(e.workspace, n)) throw new Error("Claude Code memory import destination must stay in the selected workspace.");
if (isPathInside(e.source, n) || isPathInside(n, e.source)) throw new Error("Claude Code auto-memory source and OpenClaw import destination must be separate directories.");
}
async function buildMemoryItems(e) {
const t = [];
if (e.includeInstructions !== false) for (const [n, r, i, a] of [
[
"workspace:CLAUDE.md",
e.source.projectMemoryPath,
"AGENTS.md",
"project CLAUDE.md"
],
[
"workspace:.claude/CLAUDE.md",
e.source.projectDotClaudeMemoryPath,
"AGENTS.md",
"project .claude/CLAUDE.md"
],
[
"memory:user-CLAUDE.md",
e.source.userMemoryPath,
"USER.md",
"user ~/.claude/CLAUDE.md"
]
]) {
const o = path.join(e.targets.workspaceDir, i);
if (!r) continue;
const s = (await lstatIfExists(o))?.isFile() === false;
t.push(createMigrationItem({
id: n,
kind: ["AGENTS.md", "USER.md"].includes(path.basename(o)) ? "workspace" : "memory",
action: "append",
source: r,
target: o,
status: s ? "conflict" : "planned",
reason: s ? MIGRATION_REASON_TARGET_NOT_REGULAR : void 0,
details: { sourceLabel: a }
}));
}
const n = [];
for (const t of e.source.autoMemorySources) {
const r = await resolveMemoryDestinationBoundary(t.path, e.targets.workspaceDir);
const i = path.join(e.targets.workspaceDir, "memory", "imports", "claude-code", collectionSlug(t.label, t.id));
await assertSafeMemoryDestination(r, i);
const a = CLAUDE_AUTO_MEMORY_MAX_FILES - n.length;
const o = await listMarkdownFiles(t.path, a);
if (o.truncatedBy) {
const e = o.truncatedBy === "files" ? `${CLAUDE_AUTO_MEMORY_MAX_FILES} Markdown files across all collections` : `${CLAUDE_AUTO_MEMORY_MAX_SCAN_ENTRIES} filesystem entries in one collection`;
throw new Error(`Claude Code auto-memory exceeds the safe import limit of ${e}. Narrow or split the source memory before importing.`);
}
const s = o.files;
for (const a of s) {
const o = path.join(t.path, a);
const c = path.join(i, a);
const l = await lstatIfExists(c);
const u = l !== void 0;
const d = u && !l.isFile();
if (!d) await assertSafeMemoryDestination(r, c);
const f = d || u && !e.overwrite;
n.push(createMigrationItem({
id: `memory:claude-auto:${t.id}:${a.replaceAll(path.sep, "/")}`,
kind: "memory",
action: "copy",
source: o,
target: c,
status: f ? "conflict" : "planned",
reason: d ? MIGRATION_REASON_TARGET_NOT_REGULAR : f ? MIGRATION_REASON_TARGET_EXISTS : void 0,
message: "Copy Claude Code auto-memory Markdown into the OpenClaw memory index.",
details: {
sourceType: "claude-auto-memory",
sourceLabel: "Claude Code auto-memory",
collectionId: t.id,
collectionLabel: t.label,
collectionFileCount: s.length,
relativePath: a.replaceAll(path.sep, "/")
}
}));
}
}
t.push(...n);
return t;
}
//#endregion
export { buildMemoryItems as t };