feat(memory-wiki): restore llm wiki stack

This commit is contained in:
Vincent Koc
2026-04-06 04:56:29 +01:00
parent 9fc2a9feeb
commit 5716d83336
84 changed files with 8740 additions and 144 deletions

View File

@@ -0,0 +1 @@
export * from "../memory-host-sdk/events.js";

View File

@@ -1 +1,8 @@
export * from "../memory-host-sdk/runtime-core.js";
export type {
MemoryCorpusGetResult,
MemoryCorpusSearchResult,
MemoryCorpusSupplement,
MemoryCorpusSupplementRegistration,
} from "../plugins/memory-state.js";
export { listMemoryCorpusSupplements } from "../plugins/memory-state.js";

View File

@@ -46,6 +46,12 @@ export {
withProgress,
withProgressTotals,
} from "./memory-core-host-runtime-cli.js";
export {
appendMemoryHostEvent,
readMemoryHostEvents,
resolveMemoryHostEventLogPath,
} from "./memory-core-host-events.js";
export type { MemoryHostEvent } from "./memory-core-host-events.js";
export {
resolveMemoryCorePluginConfig,
formatMemoryDreamingDay,

View File

@@ -0,0 +1 @@
export * from "./memory-core-host-runtime-core.js";

View File

@@ -0,0 +1,60 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
appendMemoryHostEvent,
readMemoryHostEvents,
resolveMemoryHostEventLogPath,
} from "./memory-host-events.js";
const tempDirs: string[] = [];
afterEach(async () => {
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
});
describe("memory host event journal helpers", () => {
it("appends and reads typed workspace events", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-host-events-"));
tempDirs.push(workspaceDir);
await appendMemoryHostEvent(workspaceDir, {
type: "memory.recall.recorded",
timestamp: "2026-04-05T12:00:00.000Z",
query: "glacier backup",
resultCount: 1,
results: [
{
path: "memory/2026-04-05.md",
startLine: 1,
endLine: 3,
score: 0.9,
},
],
});
await appendMemoryHostEvent(workspaceDir, {
type: "memory.dream.completed",
timestamp: "2026-04-05T13:00:00.000Z",
phase: "light",
lineCount: 4,
storageMode: "both",
inlinePath: path.join(workspaceDir, "memory", "2026-04-05.md"),
reportPath: path.join(workspaceDir, "memory", "dreaming", "light", "2026-04-05.md"),
});
const eventLogPath = resolveMemoryHostEventLogPath(workspaceDir);
await expect(fs.readFile(eventLogPath, "utf8")).resolves.toContain(
'"type":"memory.recall.recorded"',
);
const events = await readMemoryHostEvents({ workspaceDir });
const tail = await readMemoryHostEvents({ workspaceDir, limit: 1 });
expect(events).toHaveLength(2);
expect(events[0]?.type).toBe("memory.recall.recorded");
expect(events[1]?.type).toBe("memory.dream.completed");
expect(tail).toHaveLength(1);
expect(tail[0]?.type).toBe("memory.dream.completed");
});
});

View File

@@ -0,0 +1 @@
export * from "../memory-host-sdk/events.js";

View File

@@ -0,0 +1 @@
export * from "./memory-core-host-runtime-files.js";

View File

@@ -0,0 +1,50 @@
import { describe, expect, it } from "vitest";
import { replaceManagedMarkdownBlock, withTrailingNewline } from "./memory-host-markdown.js";
describe("withTrailingNewline", () => {
it("preserves trailing newlines", () => {
expect(withTrailingNewline("hello\n")).toBe("hello\n");
});
it("adds a trailing newline when missing", () => {
expect(withTrailingNewline("hello")).toBe("hello\n");
});
});
describe("replaceManagedMarkdownBlock", () => {
it("appends a managed block when missing", () => {
expect(
replaceManagedMarkdownBlock({
original: "# Title\n",
heading: "## Generated",
startMarker: "<!-- start -->",
endMarker: "<!-- end -->",
body: "- first",
}),
).toBe("# Title\n\n## Generated\n<!-- start -->\n- first\n<!-- end -->\n");
});
it("replaces an existing managed block in place", () => {
expect(
replaceManagedMarkdownBlock({
original:
"# Title\n\n## Generated\n<!-- start -->\n- old\n<!-- end -->\n\n## Notes\nkept\n",
heading: "## Generated",
startMarker: "<!-- start -->",
endMarker: "<!-- end -->",
body: "- new",
}),
).toBe("# Title\n\n## Generated\n<!-- start -->\n- new\n<!-- end -->\n\n## Notes\nkept\n");
});
it("supports headingless blocks", () => {
expect(
replaceManagedMarkdownBlock({
original: "alpha\n",
startMarker: "<!-- start -->",
endMarker: "<!-- end -->",
body: "beta",
}),
).toBe("alpha\n\n<!-- start -->\nbeta\n<!-- end -->\n");
});
});

View File

@@ -0,0 +1,34 @@
export type ManagedMarkdownBlockParams = {
original: string;
body: string;
startMarker: string;
endMarker: string;
heading?: string;
};
function escapeRegex(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
export function withTrailingNewline(content: string): string {
return content.endsWith("\n") ? content : `${content}\n`;
}
export function replaceManagedMarkdownBlock(params: ManagedMarkdownBlockParams): string {
const headingPrefix = params.heading ? `${params.heading}\n` : "";
const managedBlock = `${headingPrefix}${params.startMarker}\n${params.body}\n${params.endMarker}`;
const existingPattern = new RegExp(
`${params.heading ? `${escapeRegex(params.heading)}\\n` : ""}${escapeRegex(params.startMarker)}[\\s\\S]*?${escapeRegex(params.endMarker)}`,
"m",
);
if (existingPattern.test(params.original)) {
return params.original.replace(existingPattern, managedBlock);
}
const trimmed = params.original.trimEnd();
if (trimmed.length === 0) {
return `${managedBlock}\n`;
}
return `${trimmed}\n\n${managedBlock}\n`;
}

View File

@@ -0,0 +1,5 @@
export {
closeActiveMemorySearchManagers,
getActiveMemorySearchManager,
resolveActiveMemoryBackendConfig,
} from "../plugins/memory-runtime.js";

View File

@@ -0,0 +1,42 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import {
closeActiveMemorySearchManagers,
getActiveMemorySearchManager,
} from "./memory-host-search.js";
const { closeActiveMemorySearchManagersMock, getActiveMemorySearchManagerMock } = vi.hoisted(
() => ({
closeActiveMemorySearchManagersMock: vi.fn(),
getActiveMemorySearchManagerMock: vi.fn(),
}),
);
vi.mock("./memory-host-search.runtime.js", () => ({
closeActiveMemorySearchManagers: closeActiveMemorySearchManagersMock,
getActiveMemorySearchManager: getActiveMemorySearchManagerMock,
}));
describe("memory-host-search facade", () => {
beforeEach(() => {
closeActiveMemorySearchManagersMock.mockReset();
getActiveMemorySearchManagerMock.mockReset();
});
it("delegates active manager lookup to the lazy runtime module", async () => {
const cfg = { agents: { list: [{ id: "main", default: true }] } } as OpenClawConfig;
const expected = { manager: null, error: "unavailable" };
getActiveMemorySearchManagerMock.mockResolvedValue(expected);
await expect(getActiveMemorySearchManager({ cfg, agentId: "main" })).resolves.toEqual(expected);
expect(getActiveMemorySearchManagerMock).toHaveBeenCalledWith({ cfg, agentId: "main" });
});
it("delegates runtime cleanup to the lazy runtime module", async () => {
const cfg = { agents: { list: [{ id: "main", default: true }] } } as OpenClawConfig;
await closeActiveMemorySearchManagers(cfg);
expect(closeActiveMemorySearchManagersMock).toHaveBeenCalledWith(cfg);
});
});

View File

@@ -0,0 +1,29 @@
import type { OpenClawConfig } from "../config/config.js";
import type { RegisteredMemorySearchManager } from "../plugins/memory-state.js";
type ActiveMemorySearchPurpose = "default" | "status";
export type ActiveMemorySearchManagerResult = {
manager: RegisteredMemorySearchManager | null;
error?: string;
};
type MemoryHostSearchRuntimeModule = typeof import("./memory-host-search.runtime.js");
async function loadMemoryHostSearchRuntime(): Promise<MemoryHostSearchRuntimeModule> {
return await import("./memory-host-search.runtime.js");
}
export async function getActiveMemorySearchManager(params: {
cfg: OpenClawConfig;
agentId: string;
purpose?: ActiveMemorySearchPurpose;
}): Promise<ActiveMemorySearchManagerResult> {
const runtime = await loadMemoryHostSearchRuntime();
return await runtime.getActiveMemorySearchManager(params);
}
export async function closeActiveMemorySearchManagers(cfg?: OpenClawConfig): Promise<void> {
const runtime = await loadMemoryHostSearchRuntime();
await runtime.closeActiveMemorySearchManagers(cfg);
}

View File

@@ -0,0 +1 @@
export * from "./memory-core-host-status.js";