|
| 1 | +/** |
| 2 | + * MiniSearch (BM25) ranking over Markdown sections — same strategy as docker/search/knowledge-rank.mjs. |
| 3 | + */ |
| 4 | +import MiniSearch from "minisearch"; |
| 5 | + |
| 6 | +export interface CorpusDocument { |
| 7 | + file: string; |
| 8 | + content: string; |
| 9 | +} |
| 10 | + |
| 11 | +export interface RankedCorpusChunk { |
| 12 | + file: string; |
| 13 | + score: number; |
| 14 | + section: string; |
| 15 | + snippet: string; |
| 16 | +} |
| 17 | + |
| 18 | +const MAX_RESULTS = 5; |
| 19 | +const MIN_TOKEN_LENGTH = 2; |
| 20 | +const STOP_WORDS = new Set([ |
| 21 | + "a", |
| 22 | + "an", |
| 23 | + "and", |
| 24 | + "are", |
| 25 | + "for", |
| 26 | + "how", |
| 27 | + "is", |
| 28 | + "into", |
| 29 | + "that", |
| 30 | + "the", |
| 31 | + "this", |
| 32 | + "what", |
| 33 | + "with", |
| 34 | +]); |
| 35 | + |
| 36 | +function unicodeTokenize(text: string): string[] { |
| 37 | + return text |
| 38 | + .toLowerCase() |
| 39 | + .split(/[^\p{L}\p{N}]+/u) |
| 40 | + .filter((t) => t.length > 0); |
| 41 | +} |
| 42 | + |
| 43 | +/** Exported for tests and snippet logic aligned with legacy ranker. */ |
| 44 | +export function tokenizeRankTerms(query: string): string[] { |
| 45 | + return unicodeTokenize(query).filter( |
| 46 | + (token) => token.length >= MIN_TOKEN_LENGTH && !STOP_WORDS.has(token), |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +export function splitIntoSections( |
| 51 | + relPath: string, |
| 52 | + content: string, |
| 53 | +): ReadonlyArray<{ file: string; section: string; content: string }> { |
| 54 | + const lines = content.split(/\r?\n/); |
| 55 | + const out: { file: string; section: string; content: string }[] = []; |
| 56 | + let sectionTitle = "Overview"; |
| 57 | + const buf: string[] = []; |
| 58 | + const flush = () => { |
| 59 | + const text = buf.join("\n").trim(); |
| 60 | + if (text.length > 0) { |
| 61 | + out.push({ file: relPath, section: sectionTitle, content: text }); |
| 62 | + } |
| 63 | + buf.length = 0; |
| 64 | + }; |
| 65 | + for (const line of lines) { |
| 66 | + const m = /^#{1,6}\s+(.+)$/.exec(line); |
| 67 | + if (m) { |
| 68 | + flush(); |
| 69 | + sectionTitle = m[1].trim(); |
| 70 | + continue; |
| 71 | + } |
| 72 | + buf.push(line); |
| 73 | + } |
| 74 | + flush(); |
| 75 | + return out; |
| 76 | +} |
| 77 | + |
| 78 | +function extractSnippet(content: string, matchIndex: number): string { |
| 79 | + const windowStart = Math.max(0, matchIndex - 120); |
| 80 | + const windowEnd = Math.min(content.length, matchIndex + 220); |
| 81 | + const rawSnippet = content |
| 82 | + .slice(windowStart, windowEnd) |
| 83 | + .replace(/\s+/g, " ") |
| 84 | + .trim(); |
| 85 | + if (rawSnippet.length <= 220) { |
| 86 | + return rawSnippet; |
| 87 | + } |
| 88 | + return `${rawSnippet.slice(0, 217)}...`; |
| 89 | +} |
| 90 | + |
| 91 | +function snippetFromContent(content: string, query: string): string { |
| 92 | + const terms = tokenizeRankTerms(query); |
| 93 | + if (terms.length === 0) { |
| 94 | + const fb = unicodeTokenize(query).filter((t) => t.length >= 1); |
| 95 | + for (const t of fb) { |
| 96 | + const i = content.toLowerCase().indexOf(t); |
| 97 | + if (i !== -1) { |
| 98 | + return extractSnippet(content, i); |
| 99 | + } |
| 100 | + } |
| 101 | + return extractSnippet(content, 0); |
| 102 | + } |
| 103 | + const lower = content.toLowerCase(); |
| 104 | + let best = -1; |
| 105 | + for (const t of terms) { |
| 106 | + const i = lower.indexOf(t); |
| 107 | + if (i !== -1 && (best === -1 || i < best)) { |
| 108 | + best = i; |
| 109 | + } |
| 110 | + } |
| 111 | + const idx = best === -1 ? 0 : best; |
| 112 | + return extractSnippet(content, idx); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Rank corpus slices with MiniSearch (per-heading chunks). Rebuilds index each call — fine for MCP corpus sizes. |
| 117 | + */ |
| 118 | +export function rankCorpusWithMiniSearch( |
| 119 | + query: string, |
| 120 | + documents: ReadonlyArray<CorpusDocument>, |
| 121 | +): ReadonlyArray<RankedCorpusChunk> { |
| 122 | + const rows: { id: number; file: string; section: string; content: string }[] = []; |
| 123 | + let id = 0; |
| 124 | + for (const doc of documents) { |
| 125 | + for (const sec of splitIntoSections(doc.file, doc.content)) { |
| 126 | + rows.push({ id: id++, file: sec.file, section: sec.section, content: sec.content }); |
| 127 | + } |
| 128 | + } |
| 129 | + if (rows.length === 0) { |
| 130 | + return []; |
| 131 | + } |
| 132 | + |
| 133 | + const mini = new MiniSearch({ |
| 134 | + fields: ["content", "section", "file"], |
| 135 | + storeFields: ["file", "section", "content"], |
| 136 | + idField: "id", |
| 137 | + tokenize: (string) => unicodeTokenize(string).filter((t) => t.length >= 1), |
| 138 | + }); |
| 139 | + mini.addAll(rows); |
| 140 | + |
| 141 | + const hits = mini.search(query, { |
| 142 | + prefix: true, |
| 143 | + fuzzy: 0.12, |
| 144 | + boost: { section: 2.2, file: 1.65, content: 1 }, |
| 145 | + }); |
| 146 | + |
| 147 | + const byId = new Map(rows.map((r) => [r.id, r])); |
| 148 | + const out: RankedCorpusChunk[] = []; |
| 149 | + for (const h of hits.slice(0, MAX_RESULTS)) { |
| 150 | + const hid = h.id as number; |
| 151 | + const stored = byId.get(hid); |
| 152 | + if (!stored) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + out.push({ |
| 156 | + file: stored.file, |
| 157 | + score: h.score, |
| 158 | + section: stored.section, |
| 159 | + snippet: snippetFromContent(stored.content, query), |
| 160 | + }); |
| 161 | + } |
| 162 | + return out; |
| 163 | +} |
0 commit comments