mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-26 07:57:40 +00:00
feat(memory-wiki): restore llm wiki stack
This commit is contained in:
1
src/plugin-sdk/memory-core-host-events.ts
Normal file
1
src/plugin-sdk/memory-core-host-events.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "../memory-host-sdk/events.js";
|
||||
@@ -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";
|
||||
|
||||
@@ -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,
|
||||
|
||||
1
src/plugin-sdk/memory-host-core.ts
Normal file
1
src/plugin-sdk/memory-host-core.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./memory-core-host-runtime-core.js";
|
||||
60
src/plugin-sdk/memory-host-events.test.ts
Normal file
60
src/plugin-sdk/memory-host-events.test.ts
Normal 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");
|
||||
});
|
||||
});
|
||||
1
src/plugin-sdk/memory-host-events.ts
Normal file
1
src/plugin-sdk/memory-host-events.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "../memory-host-sdk/events.js";
|
||||
1
src/plugin-sdk/memory-host-files.ts
Normal file
1
src/plugin-sdk/memory-host-files.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./memory-core-host-runtime-files.js";
|
||||
50
src/plugin-sdk/memory-host-markdown.test.ts
Normal file
50
src/plugin-sdk/memory-host-markdown.test.ts
Normal 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");
|
||||
});
|
||||
});
|
||||
34
src/plugin-sdk/memory-host-markdown.ts
Normal file
34
src/plugin-sdk/memory-host-markdown.ts
Normal 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`;
|
||||
}
|
||||
5
src/plugin-sdk/memory-host-search.runtime.ts
Normal file
5
src/plugin-sdk/memory-host-search.runtime.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export {
|
||||
closeActiveMemorySearchManagers,
|
||||
getActiveMemorySearchManager,
|
||||
resolveActiveMemoryBackendConfig,
|
||||
} from "../plugins/memory-runtime.js";
|
||||
42
src/plugin-sdk/memory-host-search.test.ts
Normal file
42
src/plugin-sdk/memory-host-search.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
29
src/plugin-sdk/memory-host-search.ts
Normal file
29
src/plugin-sdk/memory-host-search.ts
Normal 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);
|
||||
}
|
||||
1
src/plugin-sdk/memory-host-status.ts
Normal file
1
src/plugin-sdk/memory-host-status.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./memory-core-host-status.js";
|
||||
Reference in New Issue
Block a user