Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): rank icon search by keyword coverage instead of requiring all

Elian Doranb7b1c6f3332b
diff --git a/apps/server/src/services/llm/tools/icon_tools.spec.ts b/apps/server/src/services/llm/tools/icon_tools.spec.ts
index 5fde9b0c65..01bf48e85e 100644
--- a/apps/server/src/services/llm/tools/icon_tools.spec.ts
+++ b/apps/server/src/services/llm/tools/icon_tools.spec.ts
@@ -35,6 +35,12 @@ describe("icon_tools", () => {
expect(result.totalResults).toBeGreaterThan(3);
});
+ it("still matches a descriptive query where no icon carries every keyword", () => {
+ const result = getTool("search_icons").execute({ query: "building landmark monument" }) as SearchResult;
+ expect(result.totalResults).toBeGreaterThan(0);
+ expect(result.results[0].iconClass).toMatch(/building/);
+ });
+
it("returns no results for an unmatched keyword", () => {
const result = getTool("search_icons").execute({ query: "xyzzyplugh" }) as SearchResult;
expect(result).toEqual({ totalResults: 0, results: [] });
@@ -49,19 +55,21 @@ describe("icon_tools", () => {
});
describe("scoreIcon", () => {
- it("ranks exact term and segment matches above substring matches and rejects non-matches", () => {
+ it("ranks exact term and segment matches above substring matches and counts keyword coverage", () => {
// Exact term match.
- expect(scoreIcon("bx-rocket", ["rocket"], ["rocket"])).toBe(2);
+ expect(scoreIcon("bx-rocket", ["rocket"], ["rocket"])).toEqual({ matched: 1, quality: 2 });
// Exact hyphen-segment match.
- expect(scoreIcon("bx-check-circle", ["check-circle"], ["circle"])).toBe(2);
+ expect(scoreIcon("bx-check-circle", ["check-circle"], ["circle"])).toEqual({ matched: 1, quality: 2 });
// Substring-only match scores lower.
- expect(scoreIcon("bx-checkbox", ["checkbox"], ["check"])).toBe(1);
+ expect(scoreIcon("bx-checkbox", ["checkbox"], ["check"])).toEqual({ matched: 1, quality: 1 });
// Match on the icon name itself.
- expect(scoreIcon("bx-rocket", ["launch"], ["rocket"])).toBe(1);
- // Every keyword must match.
- expect(scoreIcon("bx-rocket", ["rocket"], ["rocket", "banana"])).toBe(0);
+ expect(scoreIcon("bx-rocket", ["launch"], ["rocket"])).toEqual({ matched: 1, quality: 1 });
+ // An unmatched keyword no longer rejects the icon, it just doesn't count.
+ expect(scoreIcon("bx-rocket", ["rocket"], ["rocket", "banana"])).toEqual({ matched: 1, quality: 2 });
+ // No keyword matched at all.
+ expect(scoreIcon("bx-rocket", ["rocket"], ["banana"])).toEqual({ matched: 0, quality: 0 });
// Scores accumulate across keywords.
- expect(scoreIcon("bx-check-circle", ["check-circle"], ["check", "circle"])).toBe(4);
+ expect(scoreIcon("bx-check-circle", ["check-circle"], ["check", "circle"])).toEqual({ matched: 2, quality: 4 });
});
});
});
diff --git a/apps/server/src/services/llm/tools/icon_tools.ts b/apps/server/src/services/llm/tools/icon_tools.ts
index f796f76e5c..1a2859e4e7 100644
--- a/apps/server/src/services/llm/tools/icon_tools.ts
+++ b/apps/server/src/services/llm/tools/icon_tools.ts
@@ -18,7 +18,7 @@ export const iconTools = defineTools({
"ALWAYS use this instead of prepending an emoji to a note's title."
].join(" "),
inputSchema: z.object({
- query: z.string().describe("One or more keywords describing the desired icon, e.g. 'rocket' or 'check circle'"),
+ query: z.string().describe("One or more keywords describing the desired icon, e.g. 'rocket' or 'building landmark monument'. Keywords are matched individually, so listing several synonyms is safe: icons matching more of them rank first."),
limit: z.number().optional().describe("Maximum number of icons to return. Defaults to 20.")
}),
execute: ({ query, limit = DEFAULT_LIMIT }) => {
@@ -27,19 +27,19 @@ export const iconTools = defineTools({
return { error: "Query must not be empty" };
}
- const matches: { iconClass: string; terms: string[]; score: number }[] = [];
+ const matches: { iconClass: string; terms: string[]; score: IconScore }[] = [];
for (const pack of icon_packs.getIconPacks()) {
for (const [name, icon] of Object.entries(pack.manifest.icons)) {
if (!name || !icon?.terms) {
continue;
}
const score = scoreIcon(name, icon.terms, keywords);
- if (score > 0) {
+ if (score.matched > 0) {
matches.push({ iconClass: `${pack.prefix} ${name}`, terms: icon.terms, score });
}
}
}
- matches.sort((a, b) => b.score - a.score);
+ matches.sort((a, b) => b.score.matched - a.score.matched || b.score.quality - a.score.quality);
return {
totalResults: matches.length,
@@ -49,21 +49,33 @@ export const iconTools = defineTools({
}
});
+export interface IconScore {
+ /** How many of the keywords matched the icon at all. Primary ranking signal. */
+ matched: number;
+ /** How well those keywords matched: 2 per exact match, 1 per substring match. */
+ quality: number;
+}
+
/**
- * Score an icon against the search keywords. Every keyword must match the icon
- * name or one of its terms, otherwise the icon is rejected (score 0). Exact
- * matches on a term or on a hyphen-separated segment of a term ("circle" in
- * "check-circle") rank above plain substring matches.
+ * Score an icon against the search keywords. A keyword matches if it occurs in
+ * the icon name or one of its terms; exact matches on a term or on a
+ * hyphen-separated segment of a term ("circle" in "check-circle") rank above
+ * plain substring matches.
+ *
+ * Keywords are scored independently rather than required all at once, because
+ * callers (an LLM in particular) tend to describe an icon with several loosely
+ * related words — "building landmark monument" — where no single icon carries
+ * every one of them. Icons matching more keywords still sort first.
*/
-export function scoreIcon(name: string, terms: string[], keywords: string[]): number {
- let score = 0;
+export function scoreIcon(name: string, terms: string[], keywords: string[]): IconScore {
+ const score: IconScore = { matched: 0, quality: 0 };
for (const keyword of keywords) {
if (terms.some((term) => term === keyword || term.split("-").includes(keyword))) {
- score += 2;
+ score.matched++;
+ score.quality += 2;
} else if (terms.some((term) => term.includes(keyword)) || name.includes(keyword)) {
- score += 1;
- } else {
- return 0;
+ score.matched++;
+ score.quality += 1;
}
}
return score;