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.
42 lines
2.2 KiB
JavaScript
42 lines
2.2 KiB
JavaScript
import { expect, test } from "bun:test";
|
|
import { selectTools } from "./lib/tool-selection.mjs";
|
|
|
|
const tool = (name, description) => ({name: `example_${name}`, rawName: name, description});
|
|
const historical = tool("get_historical_patent", "查询企业历史专利信息");
|
|
const current = tool("get_current_patent", "查询企业当前专利信息");
|
|
const catalog = [...Array.from({length: 184}, (_, i) => tool(`get_record_${i}`, "查询企业基本信息")), current, historical];
|
|
|
|
test("unspaced Chinese recalls historical tools from a 186 tool catalog", () => {
|
|
const selected = selectTools(catalog, "请查询示例科技股份有限公司的历史专利");
|
|
expect(selected[0]).toBe(historical);
|
|
expect(selected.length).toBeLessThanOrEqual(12);
|
|
});
|
|
test("current and historical requests rank differently", () => {
|
|
expect(selectTools(catalog, "查询企业当前专利")[0]).toBe(current);
|
|
});
|
|
test("explicit raw identifier wins over boilerplate", () => {
|
|
expect(selectTools(catalog, "只调用 get_historical_patent 查询企业基本信息")[0]).toBe(historical);
|
|
});
|
|
test("English keywords still select matching tools", () => {
|
|
const wanted = tool("search_issues", "Search project issues");
|
|
expect(selectTools([...catalog, wanted], "search project issues")[0]).toBe(wanted);
|
|
});
|
|
test("empty query preserves bounded fallback without duplicate tools", () => {
|
|
const selected = selectTools(catalog, "");
|
|
expect(selected.length).toBe(8);
|
|
expect(new Set(selected).size).toBe(selected.length);
|
|
expect(selectTools([], "历史专利")).toEqual([]);
|
|
});
|
|
|
|
// Adversarial fixtures: each assertion fails if its scoring rule is removed.
|
|
test("exact identifier outranks a description matching more query terms", () => {
|
|
const exact = tool("get-patent", "Read patents");
|
|
const distractor = tool("search_records", "get-patent company historical records");
|
|
expect(selectTools([distractor, exact], "get-patent company historical records", 1)).toEqual([exact]);
|
|
});
|
|
test("rare term outranks two widespread boilerplate terms", () => {
|
|
const common = Array.from({length: 30}, (_, i) => tool(`get_common_${i}`, "company records"));
|
|
const rare = tool("get_special", "patent");
|
|
expect(selectTools([...common, rare], "company records patent", 1)).toEqual([rare]);
|
|
});
|