claude-mem-lite 4.0.4 → 5.1.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.
Files changed (64) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +18 -72
  4. package/README.zh-CN.md +24 -74
  5. package/adopt-content.mjs +0 -1
  6. package/cli/common.mjs +0 -17
  7. package/cli.mjs +19 -3
  8. package/commands/mem.md +3 -3
  9. package/commands/update.md +5 -12
  10. package/format-utils.mjs +0 -21
  11. package/haiku-client.mjs +36 -5
  12. package/hash-utils.mjs +21 -0
  13. package/hook-episode.mjs +7 -1
  14. package/hook-optimize.mjs +103 -50
  15. package/hook-shared.mjs +8 -2
  16. package/hook-update.mjs +56 -14
  17. package/hook.mjs +21 -29
  18. package/hooks/hooks.json +0 -10
  19. package/install.mjs +100 -678
  20. package/lib/atomic-write.mjs +16 -0
  21. package/lib/deferred-work.mjs +7 -3
  22. package/lib/doctor-drift.mjs +0 -1
  23. package/lib/frontmatter.mjs +1 -2
  24. package/lib/get-core.mjs +27 -1
  25. package/lib/hook-prune.mjs +133 -0
  26. package/lib/hook-stdin.mjs +1 -1
  27. package/lib/hook-telemetry.mjs +1 -1
  28. package/lib/install-shape.mjs +45 -4
  29. package/lib/maintain-core.mjs +9 -0
  30. package/lib/metrics.mjs +2 -2
  31. package/lib/proc-lock.mjs +111 -13
  32. package/lib/proxy-fetch.mjs +26 -2
  33. package/lib/scrub-record.mjs +8 -0
  34. package/lib/shard-gc.mjs +11 -7
  35. package/lib/tmp-fixture-sweep.mjs +9 -0
  36. package/mem-cli.mjs +46 -434
  37. package/nlp.mjs +1 -1
  38. package/npm-shrinkwrap.json +6 -2
  39. package/package.json +9 -16
  40. package/project-utils.mjs +43 -15
  41. package/schema.mjs +112 -50
  42. package/scripts/hook-launcher.mjs +9 -3
  43. package/scripts/prompt-search-utils.mjs +0 -30
  44. package/scripts/setup.sh +5 -0
  45. package/scripts/user-prompt-search.js +3 -125
  46. package/secret-scrub.mjs +32 -3
  47. package/server.mjs +19 -451
  48. package/source-files.mjs +12 -29
  49. package/synonyms.mjs +2 -1
  50. package/tool-schemas.mjs +23 -91
  51. package/utils.mjs +5 -13
  52. package/commands/tools.md +0 -67
  53. package/install-metadata.mjs +0 -2193
  54. package/lib/registry-core.mjs +0 -264
  55. package/registry/preinstalled.json +0 -2419
  56. package/registry-enricher.mjs +0 -124
  57. package/registry-github.mjs +0 -86
  58. package/registry-importer.mjs +0 -569
  59. package/registry-recommend.mjs +0 -503
  60. package/registry-retriever.mjs +0 -611
  61. package/registry-scanner.mjs +0 -261
  62. package/registry.mjs +0 -665
  63. package/resource-discovery.mjs +0 -199
  64. package/scripts/pre-skill-bridge.js +0 -146
@@ -1,611 +0,0 @@
1
- // claude-mem-lite: Resource retriever — FTS5 search + composite scoring
2
- // Tier 2 of the 3-tier dispatch intelligence architecture
3
-
4
- import { debugCatch } from './utils.mjs';
5
- import { BASE_STOP_WORDS } from './stop-words.mjs';
6
- import { DISPATCH_SYNONYMS } from './synonyms.mjs';
7
-
8
- // Re-export for backward compatibility
9
- export { DISPATCH_SYNONYMS };
10
-
11
- // ─── CJK Tokenization ───────────────────────────────────────────────────────
12
- // Chinese text has no word boundaries (no spaces between words).
13
- // Two-layer extraction:
14
- // 1. DISPATCH_SYNONYMS CJK keys → synonym-expanded via expandToken()
15
- // 2. CJK_INTENT_MAP → inject English equivalents for FTS5 matching
16
-
17
- const CJK_INTENT_MAP = {
18
- // test
19
- 测试: 'test',
20
- 写测试: 'test',
21
- 单测: 'test',
22
- 单元测试: 'test',
23
- 用例: 'test',
24
- 覆盖率: 'coverage',
25
- // fix/debug — synced with extractIntent CJK patterns
26
- 修复: 'fix',
27
- 调试: 'debug',
28
- 排错: 'debug',
29
- 报错: 'error',
30
- 出错: 'error',
31
- 修bug: 'fix',
32
- 改bug: 'fix',
33
- 找bug: 'debug',
34
- 有bug: 'fix',
35
- 有问题: 'fix',
36
- 不工作: 'fix',
37
- 跑不起来: 'fix',
38
- 不能用: 'fix',
39
- 挂了: 'crash',
40
- 崩溃: 'crash',
41
- // commit
42
- 提交: 'commit',
43
- 推送: 'push',
44
- 上传: 'push',
45
- // deploy
46
- 部署: 'deploy',
47
- 上线: 'deploy',
48
- 发布: 'release',
49
- 回滚: 'rollback',
50
- // review
51
- 审查: 'review',
52
- 审核: 'review',
53
- 评审: 'review',
54
- 代码审查: 'review',
55
- 代码审核: 'review',
56
- 看看代码: 'review',
57
- // clean
58
- 重构: 'refactor',
59
- 清理: 'clean',
60
- 整理: 'clean',
61
- 简化: 'simplify',
62
- 太烂: 'refactor',
63
- 乱七八糟: 'refactor',
64
- 看不懂: 'refactor',
65
- // performance
66
- 优化: 'optimize',
67
- 性能: 'performance',
68
- 卡顿: 'performance',
69
- 太慢: 'performance',
70
- 耗时: 'performance',
71
- 慢死了: 'performance',
72
- 好慢: 'performance',
73
- 缓存: 'cache',
74
- // security
75
- 安全: 'security',
76
- 漏洞: 'vulnerability',
77
- 鉴权: 'auth',
78
- 认证: 'auth',
79
- 授权: 'auth',
80
- 权限: 'auth',
81
- 泄露: 'security',
82
- 暴露: 'security',
83
- 不安全: 'vulnerability',
84
- // lint
85
- 格式化: 'format',
86
- 代码风格: 'lint',
87
- 代码规范: 'lint',
88
- 类型检查: 'typecheck',
89
- // design
90
- 设计: 'design',
91
- 界面: 'ui',
92
- 前端: 'frontend',
93
- 样式: 'css',
94
- 页面: 'frontend',
95
- 组件: 'component',
96
- 布局: 'layout',
97
- // build
98
- 构建: 'build',
99
- 编译: 'compile',
100
- 打包: 'bundle',
101
- 依赖: 'dependency',
102
- // doc
103
- 文档: 'documentation',
104
- 写文档: 'documentation',
105
- 文档化: 'documentation',
106
- 注释: 'comment',
107
- // infra
108
- 容器: 'docker',
109
- 服务器: 'server',
110
- 运维: 'devops',
111
- 集群: 'cluster',
112
- 监控: 'monitoring',
113
- 配置: 'config',
114
- 日志: 'logging',
115
- // db
116
- 数据库: 'database',
117
- 建表: 'database',
118
- 索引: 'database',
119
- 迁移: 'migration',
120
- 查询慢: 'performance',
121
- // api
122
- 接口: 'api',
123
- 路由: 'route',
124
- // plan
125
- 规划: 'planning',
126
- 架构: 'architecture',
127
- 方案: 'plan',
128
- 设计方案: 'architecture',
129
- // search — only web/info search, NOT code search (grep/find)
130
- 联网搜索: 'search',
131
- 网上搜索: 'search',
132
- 查资料: 'search',
133
- 找资料: 'search',
134
- 搜索最新: 'search',
135
- 搜索资料: 'search',
136
- 搜索文档: 'search',
137
- };
138
-
139
- // Merge all CJK keys from both maps, longest-first to avoid partial matches
140
- const ALL_CJK_KEYS = [
141
- ...new Set([
142
- ...Object.keys(DISPATCH_SYNONYMS).filter((k) => /[\u4e00-\u9fff\u3400-\u4dbf]/.test(k)),
143
- ...Object.keys(CJK_INTENT_MAP),
144
- ]),
145
- ].sort((a, b) => b.length - a.length);
146
-
147
- function extractCJKTokens(text) {
148
- const found = [];
149
- const seen = new Set();
150
- for (const key of ALL_CJK_KEYS) {
151
- if (text.includes(key)) {
152
- if (!seen.has(key)) {
153
- seen.add(key);
154
- found.push(key);
155
- }
156
- // Also inject English equivalent for direct FTS5 matching
157
- const en = CJK_INTENT_MAP[key];
158
- if (en && !seen.has(en)) {
159
- seen.add(en);
160
- found.push(en);
161
- }
162
- }
163
- }
164
- return found;
165
- }
166
-
167
- /**
168
- * English intent equivalents of any CJK_INTENT_MAP phrase present in `text` (deduped).
169
- * Shared primitive so the skill-recommendation gate's intent match (registry-recommend.mjs)
170
- * can bridge pure-中文 prompts to English intent_tags — Chinese has no word boundaries, so a
171
- * phrase like "写测试" never token-matches the English tag "test" without this. Latin in keys
172
- * ("修bug") is lowercased on the haystack so case can't miss; CJK is case-invariant.
173
- */
174
- export function cjkIntentTokens(text) {
175
- const s = String(text || '').toLowerCase();
176
- const out = [];
177
- const seen = new Set();
178
- for (const [key, en] of Object.entries(CJK_INTENT_MAP)) {
179
- if (s.includes(key) && !seen.has(en)) {
180
- seen.add(en);
181
- out.push(en);
182
- }
183
- }
184
- return out;
185
- }
186
-
187
- // ─── Query Building ──────────────────────────────────────────────────────────
188
-
189
- /**
190
- * Expand a single token with synonyms for FTS5.
191
- * @param {string} token Input token
192
- * @returns {string} FTS5 OR group or bare token
193
- */
194
- const MAX_SYNONYM_EXPANSION = 8;
195
-
196
- function expandToken(token) {
197
- const lower = token.toLowerCase();
198
- const synonyms = DISPATCH_SYNONYMS[lower];
199
- // CJK characters: pass through unquoted — FTS5 unicode61 tokenizer handles them natively.
200
- // Quoting CJK tokens can interfere with tokenization.
201
- const isSafe = (t) => /^[a-zA-Z0-9]+$/.test(t) || /[\u4e00-\u9fff\u3400-\u4dbf]/.test(t);
202
- if (!synonyms || synonyms.length === 0) {
203
- return isSafe(token) ? token : `"${token.replace(/"/g, '""')}"`;
204
- }
205
- // Cap synonym expansion to prevent BM25 precision dilution from overly broad OR groups
206
- const capped = synonyms.slice(0, MAX_SYNONYM_EXPANSION);
207
- const parts = [token, ...capped].map((t) => (isSafe(t) ? t : `"${t.replace(/"/g, '""')}"`));
208
- return `(${parts.join(' OR ')})`;
209
- }
210
-
211
- /**
212
- * Build enhanced FTS5 query from context signals.
213
- * Expands synonyms and joins with OR for broad matching.
214
- * @param {object} signals Context signals from Tier 1
215
- * @returns {string|null} FTS5 query string or null
216
- */
217
- export function buildEnhancedQuery(signals) {
218
- const parts = [];
219
-
220
- // Column-targeted: route primary intent to intent_tags column (highest signal)
221
- if (signals.primaryIntent) {
222
- const expanded = expandToken(signals.primaryIntent.toLowerCase());
223
- parts.push(`intent_tags:${expanded}`);
224
- }
225
-
226
- // Secondary intents → also column-targeted to intent_tags (not general query).
227
- // Previously these were general tokens that matched trigger_patterns (BM25 weight 5.0),
228
- // causing secondary domain words (e.g. "api" in "Write documentation for the API module")
229
- // to overpower the primary intent. Column-targeting keeps intent signal in the right lane.
230
- //
231
- // Note: signals.action (tool type: edit/bash/write) is NOT included — it's metadata
232
- // about the tool being used, not what the user needs help with.
233
- const generalTokens = new Set();
234
- if (signals.intent) {
235
- const intents = signals.intent.split(/[\s,]+/).filter(Boolean);
236
- // Secondary intents: column-targeted but WITHOUT synonym expansion.
237
- // This gives primary intent (expanded) higher BM25 weight than secondary intents (single token).
238
- for (const t of intents.slice(signals.primaryIntent ? 1 : 0)) {
239
- parts.push(`intent_tags:${t.toLowerCase()}`);
240
- }
241
- }
242
- if (signals.errorDomain) {
243
- for (const t of signals.errorDomain.split(/[\s,]+/).filter(Boolean)) {
244
- generalTokens.add(t.toLowerCase());
245
- }
246
- }
247
-
248
- // Column-targeted: route tech stack to domain_tags column
249
- if (signals.techStack) {
250
- for (const t of signals.techStack.split(/[\s,]+/).filter(Boolean)) {
251
- parts.push(`domain_tags:${expandToken(t.toLowerCase())}`);
252
- }
253
- }
254
-
255
- // Raw keywords from prompt: domain-specific terms not captured by intent patterns.
256
- // Added as column-targeted intent_tags + literal general match (no synonym expansion).
257
- // Synonym expansion is harmful for rawKeywords: "database" expanding to ORM/SQL terms
258
- // would dilute BM25 precision. Literal matching is sufficient — "seo" matches "seo"
259
- // directly across name, intent_tags, capability_summary, trigger_patterns.
260
- if (signals.rawKeywords?.length > 0) {
261
- const isSafe = (t) => /^[a-zA-Z0-9]+$/.test(t) || /[\u4e00-\u9fff\u3400-\u4dbf]/.test(t);
262
- for (const kw of signals.rawKeywords) {
263
- const safeKw = isSafe(kw) ? kw : `"${kw.replace(/"/g, '""')}"`;
264
- parts.push(`intent_tags:${safeKw}`);
265
- parts.push(safeKw);
266
- }
267
- }
268
-
269
- // Add general tokens (expanded with synonyms)
270
- for (const t of generalTokens) {
271
- parts.push(expandToken(t));
272
- }
273
-
274
- if (parts.length === 0) return null;
275
- return parts.join(' OR ');
276
- }
277
-
278
- /**
279
- * Build FTS5 query from raw text (user prompt, tool description).
280
- * Tokenizes, filters stop words, expands synonyms.
281
- * @param {string} text Raw text input
282
- * @returns {string|null} FTS5 query string or null
283
- */
284
- const TEXT_QUERY_STOP_WORDS = new Set([
285
- ...BASE_STOP_WORDS,
286
- // CJK stop words (particles, pronouns, common verbs)
287
- '的',
288
- '了',
289
- '是',
290
- '在',
291
- '我',
292
- '有',
293
- '和',
294
- '就',
295
- '不',
296
- '人',
297
- '都',
298
- '一',
299
- '一个',
300
- '上',
301
- '也',
302
- '这',
303
- '那',
304
- '你',
305
- '他',
306
- '她',
307
- '它',
308
- '们',
309
- '把',
310
- '让',
311
- '给',
312
- '用',
313
- '来',
314
- '去',
315
- '做',
316
- '说',
317
- '要',
318
- '会',
319
- '能',
320
- '帮',
321
- '帮我',
322
- '请',
323
- '下',
324
- '吧',
325
- ]);
326
-
327
- export function buildQueryFromText(text) {
328
- if (!text || typeof text !== 'string') return null;
329
-
330
- const cleaned = text.replace(/[{}()[\]^~*:@#$%&"\\-]/g, ' ').trim();
331
-
332
- // Extract CJK compound words before whitespace split (Chinese has no spaces)
333
- const cjkTokens = extractCJKTokens(cleaned);
334
-
335
- // Extract embedded English words from mixed CJK/Latin text.
336
- // Handles "用seo技能检查下网站的seo优化问题" → extracts "seo".
337
- // Whitespace split fails here because CJK text has no spaces.
338
- const embeddedEnTokens = (cleaned.match(/[a-zA-Z]{2,}/g) || []).map((w) => w.toLowerCase());
339
-
340
- const wsTokens = cleaned
341
- .split(/\s+/)
342
- .filter((t) => t.length > 1 && !TEXT_QUERY_STOP_WORDS.has(t.toLowerCase()) && !/^\d+$/.test(t));
343
-
344
- // Merge: CJK tokens first (high signal), then embedded English, then whitespace tokens, deduplicated
345
- const seen = new Set();
346
- const tokens = [];
347
- for (const t of [...cjkTokens, ...embeddedEnTokens, ...wsTokens]) {
348
- const key = t.toLowerCase();
349
- if (!seen.has(key) && !TEXT_QUERY_STOP_WORDS.has(key)) {
350
- seen.add(key);
351
- tokens.push(t);
352
- }
353
- }
354
- tokens.splice(8); // Limit to 8 most relevant tokens
355
-
356
- if (tokens.length === 0) return null;
357
-
358
- const expanded = tokens.map((t) => expandToken(t));
359
- return expanded.join(' OR ');
360
- }
361
-
362
- // ─── Project Domain Filtering ─────────────────────────────────────────────────
363
-
364
- // Platform/language tags that indicate a resource is technology-specific.
365
- // Only resources with these tags AND no overlap with project domains get filtered out.
366
- // Resources with only functional tags (testing, quality, review) always pass.
367
- const TECHNOLOGY_TAGS = new Set([
368
- 'javascript',
369
- 'typescript',
370
- 'node',
371
- 'react',
372
- 'vue',
373
- 'svelte',
374
- 'angular',
375
- 'python',
376
- 'django',
377
- 'flask',
378
- 'fastapi',
379
- 'rust',
380
- 'go',
381
- 'java',
382
- 'kotlin',
383
- 'ruby',
384
- 'php',
385
- 'swift',
386
- 'dart',
387
- 'flutter',
388
- 'ios',
389
- 'macos',
390
- 'android',
391
- 'cpp',
392
- 'c',
393
- 'csharp',
394
- 'dotnet',
395
- 'aspnet',
396
- 'elixir',
397
- 'erlang',
398
- 'lua',
399
- 'zig',
400
- 'solidity',
401
- 'html',
402
- 'css',
403
- 'frontend',
404
- 'backend',
405
- 'browser',
406
- 'web',
407
- 'playwright',
408
- ]);
409
-
410
- /**
411
- * Post-filter FTS5 results by project domain overlap.
412
- * - Resources with empty domain_tags (universal) always pass.
413
- * - Resources with only functional tags (no technology-specific tags) always pass.
414
- * - Resources with technology tags must overlap with project domains or tech_stack.
415
- * @param {object[]} results FTS5 results
416
- * @param {string[]} projectDomains Detected project domains
417
- * @returns {object[]} Filtered results
418
- */
419
- export function filterByProjectDomain(results, projectDomains) {
420
- if (!projectDomains || projectDomains.length === 0) return results;
421
- const domainSet = new Set(projectDomains.map((d) => d.toLowerCase()));
422
- return results.filter((r) => {
423
- // Universal: no domain_tags
424
- if (!r.domain_tags || r.domain_tags.trim() === '') return true;
425
-
426
- const tags = r.domain_tags
427
- .split(/[\s,]+/)
428
- .map((t) => t.trim().toLowerCase())
429
- .filter(Boolean);
430
-
431
- // Check if any tag is a technology tag
432
- const hasTechTag = tags.some((t) => TECHNOLOGY_TAGS.has(t));
433
- if (!hasTechTag) return true; // Only functional tags — always pass
434
-
435
- // Has tech tags — check overlap with project domains
436
- if (tags.some((t) => domainSet.has(t))) return true;
437
-
438
- // Also check tech_stack column (broader tech info)
439
- if (r.tech_stack) {
440
- const techTags = r.tech_stack
441
- .split(/[\s,]+/)
442
- .map((t) => t.trim().toLowerCase())
443
- .filter(Boolean);
444
- if (techTags.some((t) => domainSet.has(t))) return true;
445
- }
446
-
447
- return false;
448
- });
449
- }
450
-
451
- // ─── FTS5 Retrieval ──────────────────────────────────────────────────────────
452
-
453
- // BM25 weights (8 columns, positional — must match FTS5 column order in registry.mjs):
454
- // trigger_patterns(3), keywords(3), capability_summary(3),
455
- // intent_tags(2), use_cases(2), domain_tags(1), tech_stack(1), name(1)
456
- //
457
- // Composite ranking formula:
458
- // 40% BM25 text relevance
459
- // Quality-tier bonus: bounded additive (installed -0.15, verified -0.075). Was a
460
- // MULTIPLIER on the BM25 term, which scaled the magnitude of a variable, unbounded,
461
- // NEGATIVE signal — letting a weakly-matching installed resource (×3) outrank a
462
- // strongly-matching community one. Additive keeps tier a promotion, not an override.
463
- // 15% Star popularity (saturation normalization — diminishing returns after ~500 stars)
464
- // 15% Success rate (Laplace smoothing — Beta prior α=1, β=1 for small-sample robustness)
465
- // 10% Adoption rate (Laplace smoothing)
466
- // 10% Cold start exploration bonus (UCB1-inspired — decays as recommend_count grows)
467
- // -10% Negative feedback penalty (zombie recommendations: high recommend, near-zero adopt)
468
-
469
- // Time-windowed behavioral signals: blend all-time rates (stability) with recent 30-day rates (freshness).
470
- // recent_* subqueries return NULL when no recent invocations:
471
- // COUNT(*)=0 → SUM(...)=NULL → (NULL+1.0)/(0+2.0) = NULL → COALESCE falls back to all-time only.
472
- //
473
- // Sign convention: bm25() returns NEGATIVE (more negative = more relevant).
474
- // We keep the negative direction and SUBTRACT positive behavioral signals to make
475
- // better resources more negative. ORDER BY ... ASC puts most negative (best) first.
476
- // Composite score expression (shared between SELECT and ORDER BY)
477
- // Sign convention: more negative = better. BM25 is negative, behavioral signals are subtracted.
478
- const COMPOSITE_EXPR = `(
479
- bm25(resources_fts, 3.0, 3.0, 3.0, 2.0, 2.0, 1.0, 1.0, 1.0) * 0.4
480
- - CASE COALESCE(r.quality_tier, 'community')
481
- WHEN 'installed' THEN 0.15
482
- WHEN 'verified' THEN 0.075
483
- ELSE 0
484
- END
485
- - COALESCE(r.repo_stars * 1.0 / (r.repo_stars + 100.0), 0) * 0.15
486
- - (
487
- (COALESCE(r.success_count, 0) + 1.0) / (COALESCE(r.recommend_count, 0) + 2.0) * 0.5
488
- + COALESCE(
489
- (SELECT (SUM(CASE WHEN i.outcome='success' THEN 1 ELSE 0 END) + 1.0)
490
- / (COUNT(*) + 2.0)
491
- FROM invocations i WHERE i.resource_id = r.id
492
- AND i.created_at > datetime('now', '-30 days')),
493
- (COALESCE(r.success_count, 0) + 1.0) / (COALESCE(r.recommend_count, 0) + 2.0)
494
- ) * 0.5
495
- ) * 0.15
496
- - (
497
- (COALESCE(r.weighted_adopt_sum, 0) + 1.0) / (COALESCE(r.recommend_count, 0) + 2.0) * 0.5
498
- + COALESCE(
499
- (SELECT (SUM(COALESCE(i.score, 0)) + 1.0)
500
- / (COUNT(*) + 2.0)
501
- FROM invocations i WHERE i.resource_id = r.id
502
- AND i.created_at > datetime('now', '-30 days')),
503
- (COALESCE(r.weighted_adopt_sum, 0) + 1.0) / (COALESCE(r.recommend_count, 0) + 2.0)
504
- ) * 0.5
505
- ) * 0.10
506
- - CASE WHEN COALESCE(r.recommend_count, 0) <= 5
507
- THEN 0.10 * (1.0 - COALESCE(r.recommend_count, 0) * 1.0 / 5.0)
508
- ELSE 0 END
509
- + CASE WHEN COALESCE(r.recommend_count, 0) > 5
510
- AND (COALESCE(r.adopt_count, 0) + 1.0) / (COALESCE(r.recommend_count, 0) + 2.0) < 0.1
511
- THEN 0.10
512
- ELSE 0 END
513
- - CASE WHEN r.source = 'user' THEN 0.15 ELSE 0 END
514
- )`;
515
-
516
- const SEARCH_SQL = `
517
- SELECT *, composite_score FROM (
518
- SELECT r.*,
519
- bm25(resources_fts, 3.0, 3.0, 3.0, 2.0, 2.0, 1.0, 1.0, 1.0) AS relevance,
520
- ${COMPOSITE_EXPR} AS composite_score
521
- FROM resources_fts
522
- JOIN resources r ON r.id = resources_fts.rowid
523
- WHERE resources_fts MATCH ?
524
- AND r.status = 'active'
525
- ) sub
526
- ORDER BY composite_score ASC, id ASC
527
- LIMIT ?
528
- `;
529
-
530
- const SEARCH_BY_TYPE_SQL = `
531
- SELECT *, composite_score FROM (
532
- SELECT r.*,
533
- bm25(resources_fts, 3.0, 3.0, 3.0, 2.0, 2.0, 1.0, 1.0, 1.0) AS relevance,
534
- ${COMPOSITE_EXPR} AS composite_score
535
- FROM resources_fts
536
- JOIN resources r ON r.id = resources_fts.rowid
537
- WHERE resources_fts MATCH ?
538
- AND r.status = 'active'
539
- AND r.type = ?
540
- ) sub
541
- ORDER BY composite_score ASC, id ASC
542
- LIMIT ?
543
- `;
544
-
545
- /**
546
- * Search for resources using FTS5 with composite scoring.
547
- * When projectDomains is provided, fetches extra results internally to allow
548
- * headroom after domain filtering, then slices to requested limit.
549
- * @param {Database} db Registry database
550
- * @param {string} query FTS5 query string (already expanded)
551
- * @param {object} [opts] Options
552
- * @param {'skill'|'agent'} [opts.type] Filter by type
553
- * @param {number} [opts.limit=3] Max results
554
- * @param {string[]} [opts.projectDomains] Project domains for post-filtering
555
- * @returns {object[]} Array of matching resources with relevance scores
556
- */
557
- export function retrieveResources(db, query, { type, limit = 3, projectDomains } = {}) {
558
- if (!query) return [];
559
-
560
- // Fetch extra when domain filtering is active to ensure enough results after filtering
561
- const fetchLimit = projectDomains && projectDomains.length > 0 ? Math.max(limit * 3, 10) : limit;
562
-
563
- try {
564
- let results;
565
- if (type) {
566
- results = db.prepare(SEARCH_BY_TYPE_SQL).all(query, type, fetchLimit);
567
- } else {
568
- results = db.prepare(SEARCH_SQL).all(query, fetchLimit);
569
- }
570
- if (projectDomains && projectDomains.length > 0) {
571
- results = filterByProjectDomain(results, projectDomains);
572
- }
573
- return results.slice(0, limit);
574
- } catch (e) {
575
- // FTS5 query syntax error — try simpler query
576
- debugCatch(e, 'retrieveResources');
577
- try {
578
- const simpleQuery = query
579
- .replace(/[()]/g, '')
580
- .split(/\s+OR\s+/)
581
- .slice(0, 3)
582
- .join(' OR ');
583
- let results;
584
- if (type) {
585
- results = db.prepare(SEARCH_BY_TYPE_SQL).all(simpleQuery, type, fetchLimit);
586
- } else {
587
- results = db.prepare(SEARCH_SQL).all(simpleQuery, fetchLimit);
588
- }
589
- if (projectDomains && projectDomains.length > 0) {
590
- results = filterByProjectDomain(results, projectDomains);
591
- }
592
- return results.slice(0, limit);
593
- } catch {
594
- return [];
595
- }
596
- }
597
- }
598
-
599
- /**
600
- * Search for resources using raw text (builds query automatically).
601
- * Convenience wrapper combining buildQueryFromText + retrieveResources.
602
- * @param {Database} db Registry database
603
- * @param {string} text Raw search text
604
- * @param {object} [opts] Options passed to retrieveResources
605
- * @returns {object[]} Matching resources
606
- */
607
- export function searchResources(db, text, opts) {
608
- const query = buildQueryFromText(text);
609
- if (!query) return [];
610
- return retrieveResources(db, query, opts);
611
- }