@monoes/monograph 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/storage/fts-store.ts
CHANGED
|
@@ -1,5 +1,63 @@
|
|
|
1
1
|
import type Database from 'better-sqlite3';
|
|
2
2
|
|
|
3
|
+
// ── Intent-to-identifier extraction ──────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
const STOPWORDS = new Set([
|
|
6
|
+
'a','an','the','is','are','was','were','be','been','being','have','has','had',
|
|
7
|
+
'do','does','did','will','would','shall','should','may','might','must','can',
|
|
8
|
+
'could','am','it','its','i','me','my','we','our','you','your','he','she',
|
|
9
|
+
'they','them','their','this','that','these','those','of','in','to','for',
|
|
10
|
+
'with','on','at','from','by','about','as','into','through','during','before',
|
|
11
|
+
'after','above','below','between','out','off','over','under','again','then',
|
|
12
|
+
'once','here','there','when','where','why','how','all','both','each','every',
|
|
13
|
+
'few','more','most','other','some','such','no','nor','not','only','own',
|
|
14
|
+
'same','so','than','too','very','just','because','but','and','or','if',
|
|
15
|
+
'while','what','which','who','whom','up','down','get','set','add','use',
|
|
16
|
+
'find','show','make','let','want','need','like','look','also','new','old',
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Extract code-relevant search terms from a natural-language query.
|
|
21
|
+
* Strips stopwords, splits camelCase/PascalCase/snake_case, and strips
|
|
22
|
+
* file extensions (so "CLAUDE.md" also tries "CLAUDE").
|
|
23
|
+
* Returns an empty array when the query is already a bare identifier.
|
|
24
|
+
*/
|
|
25
|
+
export function extractSearchTerms(query: string): string[] {
|
|
26
|
+
const words = query.split(/[\s,;:!?()\[\]{}'"]+/).filter(Boolean);
|
|
27
|
+
// If query is a single token or all tokens are code-like, it's already an identifier query
|
|
28
|
+
if (words.length <= 1) return [];
|
|
29
|
+
const hasStopword = words.some(w => STOPWORDS.has(w.toLowerCase()));
|
|
30
|
+
if (!hasStopword && words.length <= 3) return [];
|
|
31
|
+
|
|
32
|
+
const terms = new Set<string>();
|
|
33
|
+
for (const word of words) {
|
|
34
|
+
const lower = word.toLowerCase();
|
|
35
|
+
if (STOPWORDS.has(lower)) continue;
|
|
36
|
+
if (word.length < 3) continue;
|
|
37
|
+
|
|
38
|
+
// Strip file extensions: "CLAUDE.md" → also add "CLAUDE"
|
|
39
|
+
const dotIdx = word.lastIndexOf('.');
|
|
40
|
+
if (dotIdx > 0 && dotIdx < word.length - 1) {
|
|
41
|
+
terms.add(word.slice(0, dotIdx));
|
|
42
|
+
terms.add(word);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Split camelCase/PascalCase: "ExtensionBridge" → "Extension", "Bridge"
|
|
46
|
+
const camelParts = word.replace(/([a-z])([A-Z])/g, '$1 $2')
|
|
47
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
|
|
48
|
+
.split(/[\s_-]+/)
|
|
49
|
+
.filter(p => p.length >= 3 && !STOPWORDS.has(p.toLowerCase()));
|
|
50
|
+
if (camelParts.length > 1) {
|
|
51
|
+
for (const part of camelParts) terms.add(part);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Keep the original word
|
|
55
|
+
terms.add(word);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return Array.from(terms);
|
|
59
|
+
}
|
|
60
|
+
|
|
3
61
|
export interface FtsResult {
|
|
4
62
|
id: string;
|
|
5
63
|
name: string;
|
|
@@ -71,6 +129,63 @@ export function ftsSearch(
|
|
|
71
129
|
// FTS MATCH can throw on malformed queries; fall through to LIKE path
|
|
72
130
|
}
|
|
73
131
|
|
|
132
|
+
// ── Intent-extraction fallback: when the raw query looks like natural language
|
|
133
|
+
// and FTS returned nothing, extract code-relevant tokens and retry FTS. ──────
|
|
134
|
+
if (matchRows.length === 0) {
|
|
135
|
+
const extracted = extractSearchTerms(safeQuery);
|
|
136
|
+
if (extracted.length > 0) {
|
|
137
|
+
// Try each extracted term individually via FTS, then merge by best rank
|
|
138
|
+
const termFtsQuery = extracted.map(quoteFtsTerm).join(' OR ');
|
|
139
|
+
let termSql = `
|
|
140
|
+
SELECT n.id, n.name, n.norm_label, n.file_path, n.label,
|
|
141
|
+
n.start_line, n.end_line, nodes_fts.rank
|
|
142
|
+
FROM nodes_fts
|
|
143
|
+
JOIN nodes n ON n.rowid = nodes_fts.rowid
|
|
144
|
+
WHERE nodes_fts MATCH ?
|
|
145
|
+
`;
|
|
146
|
+
const termParams: unknown[] = [termFtsQuery];
|
|
147
|
+
if (label) {
|
|
148
|
+
termSql += ' AND n.label = ?';
|
|
149
|
+
termParams.push(label);
|
|
150
|
+
}
|
|
151
|
+
termSql += ' ORDER BY nodes_fts.rank LIMIT ?';
|
|
152
|
+
termParams.push(limit * 2);
|
|
153
|
+
try {
|
|
154
|
+
matchRows = db.prepare(termSql).all(...(termParams as [string, ...unknown[]])) as Record<string, unknown>[];
|
|
155
|
+
} catch { /* FTS MATCH can throw on malformed queries */ }
|
|
156
|
+
|
|
157
|
+
// Also try individual LIKE for each extracted term to catch camelCase substrings
|
|
158
|
+
if (matchRows.length < limit) {
|
|
159
|
+
const seenIds = new Set(matchRows.map((r) => r.id as string));
|
|
160
|
+
for (const term of extracted) {
|
|
161
|
+
if (matchRows.length >= limit) break;
|
|
162
|
+
const escapedTerm = term.replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_');
|
|
163
|
+
const pat = `%${escapedTerm}%`;
|
|
164
|
+
let termLikeSql = `
|
|
165
|
+
SELECT n.id, n.name, n.norm_label, n.file_path, n.label,
|
|
166
|
+
n.start_line, n.end_line, 0 AS rank
|
|
167
|
+
FROM nodes n
|
|
168
|
+
WHERE (n.name LIKE ? ESCAPE '\\' OR n.norm_label LIKE ? ESCAPE '\\')
|
|
169
|
+
`;
|
|
170
|
+
const termLikeParams: unknown[] = [pat, pat];
|
|
171
|
+
if (label) {
|
|
172
|
+
termLikeSql += ' AND n.label = ?';
|
|
173
|
+
termLikeParams.push(label);
|
|
174
|
+
}
|
|
175
|
+
termLikeSql += ' LIMIT ?';
|
|
176
|
+
termLikeParams.push(limit);
|
|
177
|
+
const likeRows = db.prepare(termLikeSql).all(...(termLikeParams as [string, ...unknown[]])) as Record<string, unknown>[];
|
|
178
|
+
for (const r of likeRows) {
|
|
179
|
+
if (!seenIds.has(r.id as string)) {
|
|
180
|
+
matchRows.push(r);
|
|
181
|
+
seenIds.add(r.id as string);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
74
189
|
// Run the LIKE fallback whenever MATCH threw (malformed/boolean-keyword query) or
|
|
75
190
|
// returned zero rows — not just for short (≤2 char) queries. Short queries need it
|
|
76
191
|
// because trigram requires ≥3 characters to fire; longer queries need it whenever
|
|
@@ -79,13 +194,24 @@ export function ftsSearch(
|
|
|
79
194
|
if (safeQuery.length <= 2 || matchRows.length === 0) {
|
|
80
195
|
const escapedQuery = safeQuery.replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_');
|
|
81
196
|
const likePattern = `%${escapedQuery}%`;
|
|
197
|
+
|
|
198
|
+
// Also try without file extension: "CLAUDE.md" → "CLAUDE"
|
|
199
|
+
const dotIdx = safeQuery.lastIndexOf('.');
|
|
200
|
+
const strippedLikePattern = (dotIdx > 0 && dotIdx < safeQuery.length - 1)
|
|
201
|
+
? `%${safeQuery.slice(0, dotIdx).replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_')}%`
|
|
202
|
+
: null;
|
|
203
|
+
|
|
82
204
|
let likeSql = `
|
|
83
205
|
SELECT n.id, n.name, n.norm_label, n.file_path, n.label,
|
|
84
206
|
n.start_line, n.end_line, 0 AS rank
|
|
85
207
|
FROM nodes n
|
|
86
|
-
WHERE (n.name LIKE ? ESCAPE '\\' OR n.norm_label LIKE ? ESCAPE '\\' OR n.file_path LIKE ? ESCAPE '\\'
|
|
87
|
-
`;
|
|
208
|
+
WHERE (n.name LIKE ? ESCAPE '\\' OR n.norm_label LIKE ? ESCAPE '\\' OR n.file_path LIKE ? ESCAPE '\\'`;
|
|
88
209
|
const likeParams: unknown[] = [likePattern, likePattern, likePattern];
|
|
210
|
+
if (strippedLikePattern) {
|
|
211
|
+
likeSql += ` OR n.name LIKE ? ESCAPE '\\'`;
|
|
212
|
+
likeParams.push(strippedLikePattern);
|
|
213
|
+
}
|
|
214
|
+
likeSql += ')';
|
|
89
215
|
if (label) {
|
|
90
216
|
likeSql += ' AND n.label = ?';
|
|
91
217
|
likeParams.push(label);
|