enhance assistant batch responses

This commit is contained in:
ajnart committed 2026-08-14 11:32:10 +02:00
1 parent edf3f6cd84
commit 8d06201b41
10 files changed
+207 -38

No files matched your search

+35 -32
View File
@@ -10,47 +10,50 @@ export const iconsRouter = createTRPCRouter({
mcp: {
enabled: true,
description:
"Search for icons by name across all icon repositories. OPTIONAL: searchText (string to filter), limitPerGroup (number 1-500, default 12). Call with no arguments to browse all icons",
"Search for icons by name across all icon repositories. Returns matching icon names and exact image URLs; use those URLs directly for app icons and Markdown image previews. OPTIONAL: searchText (string to filter), limitPerGroup (number 1-500, default 12). Call with no arguments to browse all icons",
},
})
.input(iconsFindSchema)
.query(async ({ ctx, input }) => {
const term = normalizeImageName(input.searchText ?? "");
const repositories = await ctx.db.query.iconRepositories.findMany({
orderBy: (table, { asc, sql }) => [sql`CASE WHEN ${table.slug} = 'local' THEN 0 ELSE 1 END`, asc(table.slug)],
with: {
icons: {
columns: { id: true, name: true, url: true },
orderBy: (table, { asc, sql }) => [
sql`CASE WHEN ${table.name} LIKE '%.svg' THEN 0 ELSE 1 END`,
asc(table.name),
],
limit: term.length === 0 ? input.limitPerGroup : undefined,
const [repositories, countIcons] = await Promise.all([
ctx.db.query.iconRepositories.findMany({
orderBy: (table, { asc, sql }) => [sql`CASE WHEN ${table.slug} = 'local' THEN 0 ELSE 1 END`, asc(table.slug)],
with: {
icons: {
columns: { id: true, name: true, url: true },
orderBy: (table, { asc, sql }) => [
sql`CASE WHEN ${table.name} LIKE '%.svg' THEN 0 ELSE 1 END`,
asc(table.name),
],
limit: term.length === 0 ? input.limitPerGroup : undefined,
},
},
},
});
}),
ctx.db.$count(icons),
]);
const matchedRepositories = repositories.map((repository) => ({
...repository,
icons: repository.icons
.flatMap((icon) => {
const rank = getImageMatchRank(term, icon.name || icon.url);
return rank === null ? [] : [{ icon, rank }];
})
.toSorted((a, b) =>
a.rank !== b.rank
? a.rank - b.rank
: Number(b.icon.name.toLowerCase().endsWith(".svg")) -
Number(a.icon.name.toLowerCase().endsWith(".svg")) || a.icon.name.localeCompare(b.icon.name),
)
.slice(0, input.limitPerGroup)
.map(({ icon }) => icon),
}));
return {
icons:
term.length === 0
? repositories
: repositories.map((repository) => ({
...repository,
icons: repository.icons
.flatMap((icon) => {
const rank = getImageMatchRank(term, icon.name || icon.url);
return rank === null ? [] : [{ icon, rank }];
})
.toSorted((a, b) =>
a.rank !== b.rank
? a.rank - b.rank
: Number(b.icon.name.toLowerCase().endsWith(".svg")) -
Number(a.icon.name.toLowerCase().endsWith(".svg")) || a.icon.name.localeCompare(b.icon.name),
)
.slice(0, input.limitPerGroup)
.map(({ icon }) => icon),
})),
countIcons: await ctx.db.$count(icons),
term.length === 0 ? repositories : matchedRepositories.filter((repository) => repository.icons.length > 0),
countIcons,
};
}),
});
@@ -1,3 +1,5 @@
// @vitest-environment node
import { describe, expect, test } from "vitest";
import { iconRepositories, icons } from "@homarr/db/schema";
@@ -6,6 +8,73 @@ import { createDb } from "@homarr/db/test";
import { iconsRouter } from "../icons";
describe("findIcons", () => {
test("returns only repositories containing matches for a search", async () => {
const db = createDb();
await db.insert(iconRepositories).values([
{ id: "dashboard-icons", slug: "homarr-labs/dashboard-icons" },
{ id: "other-icons", slug: "example/other-icons" },
]);
await db.insert(icons).values([
{
id: "discord-svg",
name: "discord.svg",
url: "https://cdn.example.com/discord.svg",
checksum: "discord",
iconRepositoryId: "dashboard-icons",
},
{
id: "homarr-svg",
name: "homarr.svg",
url: "https://cdn.example.com/homarr.svg",
checksum: "homarr",
iconRepositoryId: "other-icons",
},
]);
const caller = iconsRouter.createCaller({ db, deviceType: undefined, session: null });
const result = await caller.findIcons({ searchText: "discord", limitPerGroup: 6 });
expect(result.countIcons).toBe(2);
expect(result.icons).toHaveLength(1);
expect(result.icons[0]).toMatchObject({
slug: "homarr-labs/dashboard-icons",
icons: [{ name: "discord.svg", url: "https://cdn.example.com/discord.svg" }],
});
});
test("keeps repositories when browsing without a search", async () => {
const db = createDb();
await db.insert(iconRepositories).values({ id: "empty", slug: "example/empty" });
const caller = iconsRouter.createCaller({ db, deviceType: undefined, session: null });
const result = await caller.findIcons({ limitPerGroup: 6 });
expect(result.icons).toMatchObject([{ slug: "example/empty", icons: [] }]);
});
test("finds the Homarr icon and returns its exact local repository URL", async () => {
const db = createDb();
await db.insert(iconRepositories).values({ id: "dashboard-icons", slug: "homarr-labs/dashboard-icons" });
await db.insert(icons).values({
id: "homarr-svg",
name: "homarr.svg",
url: "https://cdn.example.com/homarr.svg",
checksum: "homarr",
iconRepositoryId: "dashboard-icons",
});
const caller = iconsRouter.createCaller({ db, deviceType: undefined, session: null });
await expect(caller.findIcons({ searchText: "homarr" })).resolves.toMatchObject({
countIcons: 1,
icons: [
{
slug: "homarr-labs/dashboard-icons",
icons: [{ name: "homarr.svg", url: "https://cdn.example.com/homarr.svg" }],
},
],
});
});
test.each(["homeassistant", "home_assistant", "home assistant.png"])(
"matches normalized search %s before applying the group limit",
async (searchText) => {