-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslash.ts
More file actions
105 lines (92 loc) · 3.04 KB
/
slash.ts
File metadata and controls
105 lines (92 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { execSync } from "node:child_process"
import { dirname, resolve } from "node:path"
import { fileURLToPath } from "node:url"
import type { OpenClawPluginApi } from "openclaw/plugin-sdk"
import type { BmClient } from "../bm-client.ts"
import { log } from "../logger.ts"
const __dirname = dirname(fileURLToPath(import.meta.url))
export function registerCommands(
api: OpenClawPluginApi,
client: BmClient,
): void {
api.registerCommand({
name: "bm-setup",
description: "Install or update the Basic Memory CLI (requires uv)",
requireAuth: true,
handler: async () => {
const scriptPath = resolve(__dirname, "..", "scripts", "setup-bm.sh")
log.info(`/bm-setup: running ${scriptPath}`)
try {
const output = execSync(`bash "${scriptPath}"`, {
encoding: "utf-8",
timeout: 180_000,
stdio: "pipe",
env: { ...process.env },
})
return { text: output.trim() }
} catch (err: unknown) {
const execErr = err as { stderr?: string; stdout?: string }
const detail = execErr.stderr || execErr.stdout || String(err)
log.error("/bm-setup failed", err)
return {
text: `Setup failed:\n${detail.trim()}`,
}
}
},
})
api.registerCommand({
name: "remember",
description: "Save something to the Basic Memory knowledge graph",
acceptsArgs: true,
requireAuth: true,
handler: async (ctx: { args?: string }) => {
const text = ctx.args?.trim()
if (!text) {
return { text: "Usage: /remember <text to save as a note>" }
}
log.debug(`/remember: "${text.slice(0, 50)}"`)
try {
const title = text.length > 60 ? text.slice(0, 60) : text
await client.writeNote(title, text, "agent/memories")
const preview = text.length > 60 ? `${text.slice(0, 60)}...` : text
return { text: `Remembered: "${preview}"` }
} catch (err) {
log.error("/remember failed", err)
return {
text: "Failed to save memory. Is Basic Memory running?",
}
}
},
})
api.registerCommand({
name: "recall",
description: "Search the Basic Memory knowledge graph",
acceptsArgs: true,
requireAuth: true,
handler: async (ctx: { args?: string }) => {
const query = ctx.args?.trim()
if (!query) {
return { text: "Usage: /recall <search query>" }
}
log.debug(`/recall: "${query}"`)
try {
const results = await client.search(query, 5)
if (results.length === 0) {
return { text: `No notes found for: "${query}"` }
}
const lines = results.map((r, i) => {
const score = r.score ? ` (${(r.score * 100).toFixed(0)}%)` : ""
return `${i + 1}. ${r.title}${score}`
})
return {
text: `Found ${results.length} notes:\n\n${lines.join("\n")}`,
}
} catch (err) {
log.error("/recall failed", err)
return {
text: "Failed to search. Is Basic Memory running?",
}
}
},
})
}