-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.ts
More file actions
176 lines (157 loc) · 5.9 KB
/
cli.ts
File metadata and controls
176 lines (157 loc) · 5.9 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry"
import type { BmClient } from "../bm-client.ts"
import type { BasicMemoryConfig } from "../config.ts"
import { log } from "../logger.ts"
export function registerCli(
api: OpenClawPluginApi,
client: BmClient,
cfg: BasicMemoryConfig,
): void {
api.registerCli(
// biome-ignore lint/suspicious/noExplicitAny: openclaw SDK does not ship types
({ program }: { program: any }) => {
const cmd = program
.command("basic-memory")
.description("Basic Memory knowledge graph commands")
cmd
.command("search")
.argument("<query>", "Search query")
.option("--limit <n>", "Max results", "10")
.action(async (query: string, opts: { limit: string }) => {
const limit = Number.parseInt(opts.limit, 10) || 10
log.debug(`cli search: query="${query}" limit=${limit}`)
const results = await client.search(query, limit)
if (results.length === 0) {
console.log("No notes found.")
return
}
for (const r of results) {
const score = r.score ? ` (${(r.score * 100).toFixed(0)}%)` : ""
console.log(`- ${r.title}${score}`)
if (r.content) {
const preview =
r.content.length > 100
? `${r.content.slice(0, 100)}...`
: r.content
console.log(` ${preview}`)
}
}
})
cmd
.command("read")
.argument("<identifier>", "Note title, permalink, or memory:// URL")
.option("--raw", "Return raw markdown including frontmatter", false)
.action(async (identifier: string, opts: { raw?: boolean }) => {
log.debug(`cli read: identifier="${identifier}"`)
const note = await client.readNote(identifier, {
includeFrontmatter: opts.raw === true,
})
console.log(`# ${note.title}`)
console.log(`permalink: ${note.permalink}`)
console.log(`file: ${note.file_path}`)
console.log("")
console.log(note.content)
})
cmd
.command("edit")
.argument("<identifier>", "Note title, permalink, or memory:// URL")
.requiredOption(
"--operation <operation>",
"Edit operation: append|prepend|find_replace|replace_section",
)
.requiredOption("--content <content>", "Edit content")
.option("--find-text <text>", "Text to find for find_replace")
.option("--section <heading>", "Section heading for replace_section")
.option(
"--expected-replacements <n>",
"Expected replacement count for find_replace",
"1",
)
.action(
async (
identifier: string,
opts: {
operation:
| "append"
| "prepend"
| "find_replace"
| "replace_section"
content: string
findText?: string
section?: string
expectedReplacements: string
},
) => {
const expectedReplacements =
Number.parseInt(opts.expectedReplacements, 10) || 1
log.debug(
`cli edit: identifier="${identifier}" op=${opts.operation} expected_replacements=${expectedReplacements}`,
)
const result = await client.editNote(
identifier,
opts.operation,
opts.content,
{
find_text: opts.findText,
section: opts.section,
expected_replacements: expectedReplacements,
},
)
console.log(`Edited: ${result.title}`)
console.log(`permalink: ${result.permalink}`)
console.log(`file: ${result.file_path}`)
console.log(`operation: ${result.operation}`)
if (result.checksum) {
console.log(`checksum: ${result.checksum}`)
}
},
)
cmd
.command("context")
.argument("<url>", "Memory URL to navigate")
.option("--depth <n>", "Relation hops to follow", "1")
.action(async (url: string, opts: { depth: string }) => {
const depth = Number.parseInt(opts.depth, 10) || 1
log.debug(`cli context: url="${url}" depth=${depth}`)
const ctx = await client.buildContext(url, depth)
if (!ctx.results || ctx.results.length === 0) {
console.log(`No context found for "${url}".`)
return
}
for (const result of ctx.results) {
console.log(`## ${result.primary_result.title}`)
console.log(result.primary_result.content)
console.log("")
}
})
cmd
.command("recent")
.option("--timeframe <t>", "Timeframe (e.g. 24h, 7d)", "24h")
.action(async (opts: { timeframe: string }) => {
log.debug(`cli recent: timeframe="${opts.timeframe}"`)
const results = await client.recentActivity(opts.timeframe)
if (results.length === 0) {
console.log("No recent activity.")
return
}
for (const r of results) {
console.log(`- ${r.title} (${r.permalink})`)
}
})
cmd
.command("status")
.description("Show plugin status")
.action(() => {
console.log(`Project: ${cfg.project}`)
console.log(`Project path: ${cfg.projectPath}`)
console.log(`BM CLI: ${cfg.bmPath}`)
console.log(`Memory dir: ${cfg.memoryDir}`)
console.log(`Memory file: ${cfg.memoryFile}`)
console.log(`Auto-capture: ${cfg.autoCapture}`)
console.log(`Cloud: ${cfg.cloud ? cfg.cloud.url : "disabled"}`)
console.log(`Debug: ${cfg.debug}`)
})
},
{ commands: ["basic-memory"] },
)
}