Skip to content

Commit 82710f2

Browse files
committed
feat(memory-wiki): add wiki search and get surfaces
1 parent 516a43f commit 82710f2

7 files changed

Lines changed: 430 additions & 3 deletions

File tree

‎extensions/memory-wiki/index.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ describe("memory-wiki plugin", () => {
2424

2525
await plugin.register(api);
2626

27-
expect(registerTool).toHaveBeenCalledTimes(1);
28-
expect(registerTool.mock.calls[0]?.[1]).toMatchObject({ name: "wiki_status" });
27+
expect(registerTool).toHaveBeenCalledTimes(3);
28+
expect(registerTool.mock.calls.map((call) => call[1]?.name)).toEqual([
29+
"wiki_status",
30+
"wiki_search",
31+
"wiki_get",
32+
]);
2933
expect(registerCli).toHaveBeenCalledTimes(1);
3034
expect(registerCli.mock.calls[0]?.[1]).toMatchObject({
3135
descriptors: [

‎extensions/memory-wiki/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { definePluginEntry } from "./api.js";
22
import { registerWikiCli } from "./src/cli.js";
33
import { memoryWikiConfigSchema, resolveMemoryWikiConfig } from "./src/config.js";
4-
import { createWikiStatusTool } from "./src/tool.js";
4+
import { createWikiGetTool, createWikiSearchTool, createWikiStatusTool } from "./src/tool.js";
55

66
export default definePluginEntry({
77
id: "memory-wiki",
@@ -12,6 +12,8 @@ export default definePluginEntry({
1212
const config = resolveMemoryWikiConfig(api.pluginConfig);
1313

1414
api.registerTool(createWikiStatusTool(config), { name: "wiki_status" });
15+
api.registerTool(createWikiSearchTool(config), { name: "wiki_search" });
16+
api.registerTool(createWikiGetTool(config), { name: "wiki_get" });
1517
api.registerCli(
1618
({ program }) => {
1719
registerWikiCli(program, config);

‎extensions/memory-wiki/skills/wiki-maintainer/SKILL.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ description: Maintain the OpenClaw memory wiki vault with deterministic pages, m
66
Use this skill when working inside a memory-wiki vault.
77

88
- Prefer `wiki_status` first when you need to understand the vault mode, path, or Obsidian CLI availability.
9+
- Use `wiki_search` to discover candidate pages, then `wiki_get` to inspect the exact page before editing or citing it.
910
- Use `openclaw wiki ingest`, `openclaw wiki compile`, and `openclaw wiki lint` as the default maintenance loop.
1011
- Keep generated sections inside managed markers. Do not overwrite human note blocks.
1112
- Treat raw sources, memory artifacts, and daily notes as evidence. Do not let wiki pages become the only source of truth for new claims.

‎extensions/memory-wiki/src/cli.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { MemoryWikiPluginConfig, ResolvedMemoryWikiConfig } from "./config.
44
import { resolveMemoryWikiConfig } from "./config.js";
55
import { ingestMemoryWikiSource } from "./ingest.js";
66
import { lintMemoryWikiVault } from "./lint.js";
7+
import { getMemoryWikiPage, searchMemoryWiki } from "./query.js";
78
import { renderMemoryWikiStatus, resolveMemoryWikiStatus } from "./status.js";
89
import { initializeMemoryWikiVault } from "./vault.js";
910

@@ -28,6 +29,17 @@ type WikiIngestCommandOptions = {
2829
title?: string;
2930
};
3031

32+
type WikiSearchCommandOptions = {
33+
json?: boolean;
34+
maxResults?: number;
35+
};
36+
37+
type WikiGetCommandOptions = {
38+
json?: boolean;
39+
from?: number;
40+
lines?: number;
41+
};
42+
3143
function writeOutput(output: string, writer: Pick<NodeJS.WriteStream, "write"> = process.stdout) {
3244
writer.write(output.endsWith("\n") ? output : `${output}\n`);
3345
}
@@ -103,6 +115,53 @@ export async function runWikiIngest(params: {
103115
return result;
104116
}
105117

118+
export async function runWikiSearch(params: {
119+
config: ResolvedMemoryWikiConfig;
120+
query: string;
121+
maxResults?: number;
122+
json?: boolean;
123+
stdout?: Pick<NodeJS.WriteStream, "write">;
124+
}) {
125+
const results = await searchMemoryWiki({
126+
config: params.config,
127+
query: params.query,
128+
maxResults: params.maxResults,
129+
});
130+
const summary = params.json
131+
? JSON.stringify(results, null, 2)
132+
: results.length === 0
133+
? "No wiki results."
134+
: results
135+
.map(
136+
(result, index) =>
137+
`${index + 1}. ${result.title} (${result.kind})\nPath: ${result.path}\nSnippet: ${result.snippet}`,
138+
)
139+
.join("\n\n");
140+
writeOutput(summary, params.stdout);
141+
return results;
142+
}
143+
144+
export async function runWikiGet(params: {
145+
config: ResolvedMemoryWikiConfig;
146+
lookup: string;
147+
fromLine?: number;
148+
lineCount?: number;
149+
json?: boolean;
150+
stdout?: Pick<NodeJS.WriteStream, "write">;
151+
}) {
152+
const result = await getMemoryWikiPage({
153+
config: params.config,
154+
lookup: params.lookup,
155+
fromLine: params.fromLine,
156+
lineCount: params.lineCount,
157+
});
158+
const summary = params.json
159+
? JSON.stringify(result, null, 2)
160+
: (result?.content ?? `Wiki page not found: ${params.lookup}`);
161+
writeOutput(summary, params.stdout);
162+
return result;
163+
}
164+
106165
export function registerWikiCli(program: Command, pluginConfig?: MemoryWikiPluginConfig) {
107166
const config = resolveMemoryWikiConfig(pluginConfig);
108167
const wiki = program.command("wiki").description("Inspect and initialize the memory wiki vault");
@@ -148,4 +207,36 @@ export function registerWikiCli(program: Command, pluginConfig?: MemoryWikiPlugi
148207
.action(async (inputPath: string, opts: WikiIngestCommandOptions) => {
149208
await runWikiIngest({ config, inputPath, title: opts.title, json: opts.json });
150209
});
210+
211+
wiki
212+
.command("search")
213+
.description("Search wiki pages")
214+
.argument("<query>", "Search query")
215+
.option("--max-results <n>", "Maximum results", (value: string) => Number(value))
216+
.option("--json", "Print JSON")
217+
.action(async (query: string, opts: WikiSearchCommandOptions) => {
218+
await runWikiSearch({
219+
config,
220+
query,
221+
maxResults: opts.maxResults,
222+
json: opts.json,
223+
});
224+
});
225+
226+
wiki
227+
.command("get")
228+
.description("Read a wiki page by id or relative path")
229+
.argument("<lookup>", "Relative path or page id")
230+
.option("--from <n>", "Start line", (value: string) => Number(value))
231+
.option("--lines <n>", "Number of lines", (value: string) => Number(value))
232+
.option("--json", "Print JSON")
233+
.action(async (lookup: string, opts: WikiGetCommandOptions) => {
234+
await runWikiGet({
235+
config,
236+
lookup,
237+
fromLine: opts.from,
238+
lineCount: opts.lines,
239+
json: opts.json,
240+
});
241+
});
151242
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import fs from "node:fs/promises";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { resolveMemoryWikiConfig } from "./config.js";
6+
import { renderWikiMarkdown } from "./markdown.js";
7+
import { getMemoryWikiPage, searchMemoryWiki } from "./query.js";
8+
import { initializeMemoryWikiVault } from "./vault.js";
9+
10+
const tempDirs: string[] = [];
11+
12+
afterEach(async () => {
13+
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
14+
});
15+
16+
describe("searchMemoryWiki", () => {
17+
it("finds pages by title and body", async () => {
18+
const rootDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-wiki-query-"));
19+
tempDirs.push(rootDir);
20+
const config = resolveMemoryWikiConfig(
21+
{ vault: { path: rootDir } },
22+
{ homedir: "/Users/tester" },
23+
);
24+
await initializeMemoryWikiVault(config);
25+
await fs.writeFile(
26+
path.join(rootDir, "sources", "alpha.md"),
27+
renderWikiMarkdown({
28+
frontmatter: { pageType: "source", id: "source.alpha", title: "Alpha Source" },
29+
body: "# Alpha Source\n\nalpha body text\n",
30+
}),
31+
"utf8",
32+
);
33+
34+
const results = await searchMemoryWiki({ config, query: "alpha" });
35+
36+
expect(results).toHaveLength(1);
37+
expect(results[0]?.path).toBe("sources/alpha.md");
38+
});
39+
});
40+
41+
describe("getMemoryWikiPage", () => {
42+
it("reads pages by relative path and slices line ranges", async () => {
43+
const rootDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-wiki-query-"));
44+
tempDirs.push(rootDir);
45+
const config = resolveMemoryWikiConfig(
46+
{ vault: { path: rootDir } },
47+
{ homedir: "/Users/tester" },
48+
);
49+
await initializeMemoryWikiVault(config);
50+
await fs.writeFile(
51+
path.join(rootDir, "sources", "alpha.md"),
52+
renderWikiMarkdown({
53+
frontmatter: { pageType: "source", id: "source.alpha", title: "Alpha Source" },
54+
body: "# Alpha Source\n\nline one\nline two\nline three\n",
55+
}),
56+
"utf8",
57+
);
58+
59+
const result = await getMemoryWikiPage({
60+
config,
61+
lookup: "sources/alpha.md",
62+
fromLine: 4,
63+
lineCount: 2,
64+
});
65+
66+
expect(result?.path).toBe("sources/alpha.md");
67+
expect(result?.content).toContain("line one");
68+
expect(result?.content).toContain("line two");
69+
expect(result?.content).not.toContain("line three");
70+
});
71+
});

0 commit comments

Comments
 (0)