The chat page picks up to 12 tools from a large MCP catalog. It now splits unspaced Chinese into two-character terms instead of treating the whole question as one word. Terms are weighted by how rare they are in the catalog (IDF), and a tool named exactly in the question, hyphenated names included, is boosted to the top. The selection moves into lib/tool-selection.mjs with its own tests. page-chat goes to 0.1.12. Run at d10e93a62832e31ead84ddef438b615b13935e26 merged onto main1aab125bf, on a separate macOS machine (bun 1.4.2, sandbox HOME): - packages/chat `bun test`: 75 pass, 1 skip, 0 fail. - `bun scripts/check.mjs`: exit 0. - Root `bun test`: 21 fail and 4 errors in devin, zcode and window-warmup, none in chat. Main1aab125bfshows the same 21 and 4 in the same tests. The devin and zcode ones refuse a HOME that isn't from `mktemp -d`. - Broken by hand, one at a time: without the exact-name boost, "exact identifier outranks…" goes red. With IDF replaced by 1, "rare term outranks…" goes red. Without `-` in the Latin term regex, the exact-identifier test goes red. Version: page-chat is served from the dist branch, where the newest is 0.1.11.
28 lines
1.3 KiB
JavaScript
28 lines
1.3 KiB
JavaScript
// Keep schemas bounded, but do not treat a whole unspaced Chinese question
|
|
// as one word. Rare matching terms outrank boilerplate repeated in catalogs.
|
|
export function selectTools(all, query, limit = 12) {
|
|
const q = String(query).toLowerCase();
|
|
const terms = new Set(q.match(/[a-z0-9_-]+/g)?.filter(w => w.length >= 3) || []);
|
|
for (const run of q.match(/[\u4e00-\u9fff]+/g) || []) {
|
|
for (let i = 0; i < run.length - 1; i++) terms.add(run.slice(i, i + 2));
|
|
}
|
|
const rows = all.map(t => ({ t, hay: `${t.name} ${t.description || ""}`.toLowerCase(), score: 0 }));
|
|
for (const term of terms) {
|
|
const hits = rows.filter(r => r.hay.includes(term));
|
|
const weight = Math.log(1 + all.length / Math.max(1, hits.length));
|
|
for (const row of hits) row.score += weight;
|
|
}
|
|
for (const row of rows) {
|
|
// Explicit tool identifiers must win even over verbose descriptions.
|
|
const name = String(row.t.rawName || row.t.name).toLowerCase();
|
|
if (terms.has(name)) row.score += 1000;
|
|
}
|
|
const selected = rows.filter(r => r.score > 0).sort((a, b) => b.score - a.score)
|
|
.slice(0, limit).map(r => r.t);
|
|
if (selected.length < 3) {
|
|
const common = /^(get|list|search|read|fetch|query)_|(_search|_list|_get)$/;
|
|
selected.push(...all.filter(t => common.test(t.rawName) && !selected.includes(t)).slice(0, 8));
|
|
}
|
|
return selected.slice(0, limit);
|
|
}
|