-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch-notes.ts
More file actions
144 lines (138 loc) · 4.22 KB
/
search-notes.ts
File metadata and controls
144 lines (138 loc) · 4.22 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
import { Type } from "@sinclair/typebox"
import type { OpenClawPluginApi } from "openclaw/plugin-sdk"
import type { BmClient } from "../bm-client.ts"
import { log } from "../logger.ts"
export function registerSearchTool(
api: OpenClawPluginApi,
client: BmClient,
): void {
api.registerTool(
{
name: "search_notes",
label: "Knowledge Search",
description:
"Search the Basic Memory knowledge graph for relevant notes, concepts, and connections. " +
"Returns matching notes with titles, content previews, and relevance scores. " +
"Optionally filter by frontmatter metadata fields, tags, or status.",
parameters: Type.Object({
query: Type.String({ description: "Search query" }),
limit: Type.Optional(
Type.Number({ description: "Max results (default: 10)" }),
),
project: Type.Optional(
Type.String({
description: "Target project name (defaults to current project)",
}),
),
metadata_filters: Type.Optional(
Type.Object(
{},
{
additionalProperties: true,
description:
"Filter by frontmatter fields. Supports equality, $in, $gt/$gte/$lt/$lte, $between, and array-contains operators.",
},
),
),
tags: Type.Optional(
Type.Array(Type.String(), {
description: "Filter by frontmatter tags (all must match)",
}),
),
status: Type.Optional(
Type.String({
description: "Filter by frontmatter status field",
}),
),
}),
async execute(
_toolCallId: string,
params: {
query: string
limit?: number
project?: string
metadata_filters?: Record<string, unknown>
tags?: string[]
status?: string
},
) {
const limit = params.limit ?? 10
log.debug(
`search_notes: query="${params.query}" limit=${limit} project="${params.project ?? "default"}"`,
)
const metadata =
params.metadata_filters || params.tags || params.status
? {
filters: params.metadata_filters,
tags: params.tags,
status: params.status,
}
: undefined
try {
const results = await client.search(
params.query,
limit,
params.project,
metadata,
)
if (results.length === 0) {
return {
content: [
{
type: "text" as const,
text: "No matching notes found in the knowledge graph.",
},
],
details: {
count: 0,
results: [],
},
}
}
const text = results
.map((r, i) => {
const score = r.score ? ` (${(r.score * 100).toFixed(0)}%)` : ""
const content = r.content ?? ""
const preview =
content.length > 200 ? `${content.slice(0, 200)}...` : content
return `${i + 1}. **${r.title}**${score}\n ${preview}`
})
.join("\n\n")
return {
content: [
{
type: "text" as const,
text: `Found ${results.length} notes:\n\n${text}`,
},
],
details: {
count: results.length,
results: results.map((r) => ({
title: r.title,
permalink: r.permalink,
score: r.score,
})),
},
}
} catch (err) {
log.error("search_notes failed", err)
return {
content: [
{
type: "text" as const,
text: "Search failed. Is Basic Memory running? Check logs for details.",
},
],
details: {
count: 0,
results: [],
query: params.query,
error: "search_notes_failed",
},
}
}
},
},
{ name: "search_notes" },
)
}