fix(web): model picker search lists newest models first (#18494)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-15 16:37:44 -04:00
1 parent 4036e18597
commit ede3f2d6ba
4 files changed
+272 -77

No files matched your search

+19 -5
View File
@@ -19,7 +19,11 @@ export function normalizeSearchQuery(
: trimmed.toLowerCase();
}
export function scoreSubsequenceMatch(value: string, query: string): number | null {
export function scoreSubsequenceMatch(
value: string,
query: string,
options?: { ignorePosition?: boolean },
): number | null {
if (!query) return 0;
let queryIndex = 0;
@@ -43,6 +47,9 @@ export function scoreSubsequenceMatch(value: string, query: string): number | nu
queryIndex += 1;
if (queryIndex === query.length) {
const spanPenalty = valueIndex - firstMatchIndex + 1 - query.length;
if (options?.ignorePosition) {
return gapPenalty * 3 + spanPenalty;
}
const lengthPenalty = Math.min(64, value.length - query.length);
return firstMatchIndex * 2 + gapPenalty * 3 + spanPenalty + lengthPenalty;
}
@@ -92,8 +99,15 @@ export function scoreQueryMatch(input: {
includesBase?: number;
fuzzyBase?: number;
boundaryMarkers?: readonly string[];
/**
* Score by match tier alone, ignoring where the match lands and how long the
* value is, so callers can keep their own order among equal matches.
*/
ignorePosition?: boolean;
}): number | null {
const { value, query } = input;
const ignorePosition = input.ignorePosition === true;
const penalty = ignorePosition ? 0 : lengthPenalty(value, query);
if (!value || !query) {
return null;
@@ -104,7 +118,7 @@ export function scoreQueryMatch(input: {
}
if (input.prefixBase !== undefined && value.startsWith(query)) {
return input.prefixBase + lengthPenalty(value, query);
return input.prefixBase + penalty;
}
if (input.boundaryBase !== undefined) {
@@ -114,19 +128,19 @@ export function scoreQueryMatch(input: {
input.boundaryMarkers ?? [" ", "-", "_", "/"],
);
if (boundaryIndex !== null) {
return input.boundaryBase + boundaryIndex * 2 + lengthPenalty(value, query);
return input.boundaryBase + (ignorePosition ? 0 : boundaryIndex * 2) + penalty;
}
}
if (input.includesBase !== undefined) {
const includesIndex = value.indexOf(query);
if (includesIndex !== -1) {
return input.includesBase + includesIndex * 2 + lengthPenalty(value, query);
return input.includesBase + (ignorePosition ? 0 : includesIndex * 2) + penalty;
}
}
if (input.fuzzyBase !== undefined) {
const fuzzyScore = scoreSubsequenceMatch(value, query);
const fuzzyScore = scoreSubsequenceMatch(value, query, { ignorePosition });
if (fuzzyScore !== null) {
return input.fuzzyBase + fuzzyScore;
}