-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
162 lines (145 loc) · 5.93 KB
/
index.ts
File metadata and controls
162 lines (145 loc) · 5.93 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { execSync } from "node:child_process"
import type { OpenClawPluginApi } from "openclaw/plugin-sdk"
import { BmClient } from "./bm-client.ts"
import { registerCli } from "./commands/cli.ts"
import { registerSkillCommands } from "./commands/skills.ts"
import { registerCommands } from "./commands/slash.ts"
import {
basicMemoryConfigSchema,
parseConfig,
resolveProjectPath,
} from "./config.ts"
import { BasicMemoryContextEngine } from "./context-engine/basic-memory-context-engine.ts"
import { initLogger, log } from "./logger.ts"
import { CONVERSATION_SCHEMA_CONTENT } from "./schema/conversation-schema.ts"
import { TASK_SCHEMA_CONTENT } from "./schema/task-schema.ts"
import { registerContextTool } from "./tools/build-context.ts"
import { registerDeleteTool } from "./tools/delete-note.ts"
import { registerEditTool } from "./tools/edit-note.ts"
import { registerProjectListTool } from "./tools/list-memory-projects.ts"
import { registerWorkspaceListTool } from "./tools/list-workspaces.ts"
import {
registerMemoryProvider,
setWorkspaceDir,
} from "./tools/memory-provider.ts"
import { registerMoveTool } from "./tools/move-note.ts"
import { registerReadTool } from "./tools/read-note.ts"
import { registerSchemaDiffTool } from "./tools/schema-diff.ts"
import { registerSchemaInferTool } from "./tools/schema-infer.ts"
import { registerSchemaValidateTool } from "./tools/schema-validate.ts"
import { registerSearchTool } from "./tools/search-notes.ts"
import { registerWriteTool } from "./tools/write-note.ts"
const BASIC_MEMORY_RELEASE_TAG = "v0.20.2"
export default {
id: "openclaw-basic-memory",
name: "Basic Memory",
description:
"Local-first knowledge graph for OpenClaw — persistent memory with graph search and composited memory_search",
kind: "memory" as const,
configSchema: basicMemoryConfigSchema,
register(api: OpenClawPluginApi) {
const cfg = parseConfig(api.pluginConfig)
initLogger(api.logger, cfg.debug)
log.info(
`project=${cfg.project} memoryDir=${cfg.memoryDir} memoryFile=${cfg.memoryFile}`,
)
const client = new BmClient(cfg.bmPath, cfg.project)
// --- BM Tools (always registered) ---
registerSearchTool(api, client)
registerProjectListTool(api, client)
registerWorkspaceListTool(api, client)
registerReadTool(api, client)
registerWriteTool(api, client)
registerEditTool(api, client)
registerContextTool(api, client)
registerDeleteTool(api, client)
registerMoveTool(api, client)
registerSchemaValidateTool(api, client)
registerSchemaInferTool(api, client)
registerSchemaDiffTool(api, client)
// --- Composited memory_search + memory_get (always registered) ---
registerMemoryProvider(api, client, cfg)
log.info("registered composited memory_search + memory_get")
api.registerContextEngine(
"openclaw-basic-memory",
() => new BasicMemoryContextEngine(client, cfg),
)
log.info("registered Basic Memory context engine")
// --- Commands ---
registerCommands(api, client)
registerSkillCommands(api)
registerCli(api, client, cfg)
// --- Service lifecycle ---
api.registerService({
id: "openclaw-basic-memory",
start: async (ctx: { config?: unknown; workspaceDir?: string }) => {
log.info("starting...")
// Auto-install bm CLI if not found
const bmBin = cfg.bmPath || "bm"
try {
execSync(`command -v ${bmBin}`, { stdio: "ignore" })
} catch {
log.info("bm CLI not found on PATH — attempting auto-install...")
try {
execSync("command -v uv", { stdio: "ignore" })
log.info(
"installing basic-memory via uv (this may take a minute)...",
)
const result = execSync(
`uv tool install "basic-memory @ git+https://github.com/basicmachines-co/basic-memory.git@${BASIC_MEMORY_RELEASE_TAG}" --force`,
{ encoding: "utf-8", timeout: 120_000, stdio: "pipe" },
)
log.info(
`basic-memory installed: ${result.trim().split("\n").pop()}`,
)
// Verify it worked
try {
execSync(`command -v ${bmBin}`, { stdio: "ignore" })
log.info("bm CLI now available on PATH")
} catch {
log.error(
"bm installed but not found on PATH. You may need to add uv's bin directory to your PATH (typically ~/.local/bin).",
)
}
} catch (_uvErr) {
log.error(
"Cannot auto-install basic-memory: uv not found. " +
"Install uv first (brew install uv, or curl -LsSf https://astral.sh/uv/install.sh | sh), " +
"then restart the gateway.",
)
}
}
const workspace = ctx.workspaceDir ?? process.cwd()
const projectPath = resolveProjectPath(cfg.projectPath, workspace)
cfg.projectPath = projectPath
await client.start({ cwd: workspace })
await client.ensureProject(projectPath)
log.debug(`project "${cfg.project}" at ${projectPath}`)
// Seed schemas if not already present
for (const [name, content] of [
["Task", TASK_SCHEMA_CONTENT],
["Conversation", CONVERSATION_SCHEMA_CONTENT],
] as const) {
try {
await client.readNote(`schema/${name}`)
log.debug(`${name} schema already exists, skipping seed`)
} catch {
try {
await client.writeNote(name, content, "schema")
log.debug(`seeded ${name} schema note`)
} catch (err) {
log.debug(`${name} schema seed failed (non-fatal)`, err)
}
}
}
setWorkspaceDir(workspace)
log.info("connected — BM MCP stdio session running")
},
stop: async () => {
log.info("stopping BM MCP session...")
await client.stop()
log.info("stopped")
},
})
},
}