litellm/ui/litellm-dashboard/src/utils/searchUtils.ts
ryan-crabbe-berri d9f7f9ea16 feat(ui): add search to Agent Hub tab and admin agents table
Ports the Model Hub search to the AI Hub Agent Hub tab and the admin
/agents toolbar as a client-side filter over agent name and description.
Extracts the hub search matching into utils/searchUtils and fixes the
public Model Hub rendering the whole catalog when a search matches
nothing (LIT-5230)
2026-09-01 11:46:58 -07:00

32 lines
1.2 KiB
TypeScript

type SearchField = string | null | undefined;
const normalizeTerm = (term: string): string => term.trim().toLowerCase();
export function matchesSearchTerm(term: string, fields: ReadonlyArray<SearchField>): boolean {
const needle = normalizeTerm(term);
if (needle === "") return true;
const haystacks = fields.filter((field): field is string => typeof field === "string").map((f) => f.toLowerCase());
if (haystacks.some((haystack) => haystack.includes(needle))) return true;
return needle.split(/\s+/).every((word) => haystacks.some((haystack) => haystack.includes(word)));
}
export function filterBySearchTerm<T>(
items: ReadonlyArray<T>,
term: string,
fields: (item: T) => ReadonlyArray<SearchField>,
): T[] {
return items.filter((item) => matchesSearchTerm(term, fields(item)));
}
export function rankBySearchRelevance<T>(items: ReadonlyArray<T>, term: string, name: (item: T) => string): T[] {
const needle = normalizeTerm(term);
if (needle === "") return [...items];
const score = (item: T): number => {
const candidate = name(item).toLowerCase();
return (candidate === needle ? 1000 : 0) + (candidate.startsWith(needle) ? 100 : 0) + (1000 - candidate.length);
};
return [...items].sort((a, b) => score(b) - score(a));
}