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

@@ -21,7 +21,7 @@ import {
parseModelPickerLegacySectionKey,
parseModelPickerModelKey,
} from "./modelPickerKeys";
import { buildModelPickerSearchText, scoreModelPickerSearch } from "./modelPickerSearch";
import { rankModelPickerSearch } from "./modelPickerSearch";
import {
Combobox,
ComboboxEmpty,
@@ -451,77 +451,20 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
let result = flatModels;
// Apply tokenized fuzzy search across the combined provider/model search fields.
// When searching, we only respect locked provider (by driver kind),
// ignoring sidebar selection so account-scoped searches can find a
// model before the user chooses a specific instance rail item.
if (searchQuery.trim()) {
const rankedMatches = result
.map((model) => ({
model,
score: scoreModelPickerSearch(
{
name: model.name,
...(model.shortName ? { shortName: model.shortName } : {}),
...(model.subProvider ? { subProvider: model.subProvider } : {}),
driverKind: model.driverKind,
providerDisplayName: model.instanceDisplayName,
isFavorite: favoritesSet.has(providerModelKey(model.instanceId, model.slug)),
},
searchQuery,
),
isFavorite: favoritesSet.has(providerModelKey(model.instanceId, model.slug)),
tieBreaker: buildModelPickerSearchText({
name: model.name,
...(model.shortName ? { shortName: model.shortName } : {}),
...(model.subProvider ? { subProvider: model.subProvider } : {}),
driverKind: model.driverKind,
providerDisplayName: model.instanceDisplayName,
}),
}))
.filter(
(
rankedModel,
): rankedModel is {
model: ModelPickerItem;
score: number;
isFavorite: boolean;
tieBreaker: string;
} => rankedModel.score !== null,
);
// When searching, we only respect locked provider (by driver kind),
// ignoring sidebar selection so account-scoped searches can find a
// model before the user chooses a specific instance rail item.
if (props.lockedProvider !== null) {
const lockedProviderMatches: Array<(typeof rankedMatches)[number]> = [];
for (const rankedModel of rankedMatches) {
if (matchesLockedProvider(rankedModel.model)) {
lockedProviderMatches.push(rankedModel);
}
}
return lockedProviderMatches
.toSorted((a, b) => {
const scoreDelta = a.score - b.score;
if (scoreDelta !== 0) {
return scoreDelta;
}
if (a.isFavorite !== b.isFavorite) {
return a.isFavorite ? -1 : 1;
}
return a.tieBreaker.localeCompare(b.tieBreaker);
})
.map((rankedModel) => rankedModel.model);
}
return rankedMatches
.toSorted((a, b) => {
const scoreDelta = a.score - b.score;
if (scoreDelta !== 0) {
return scoreDelta;
}
if (a.isFavorite !== b.isFavorite) {
return a.isFavorite ? -1 : 1;
}
return a.tieBreaker.localeCompare(b.tieBreaker);
})
.map((rankedModel) => rankedModel.model);
const candidates =
props.lockedProvider !== null ? result.filter((m) => matchesLockedProvider(m)) : result;
return rankModelPickerSearch(candidates, searchQuery, (model) => ({
name: model.name,
...(model.shortName ? { shortName: model.shortName } : {}),
...(model.subProvider ? { subProvider: model.subProvider } : {}),
driverKind: model.driverKind,
providerDisplayName: model.instanceDisplayName,
isFavorite: favoritesSet.has(providerModelKey(model.instanceId, model.slug)),
}));
}
if (props.lockedProvider !== null) {
@@ -1,6 +1,10 @@
import { describe, expect, it } from "vite-plus/test";
import { buildModelPickerSearchText, scoreModelPickerSearch } from "./modelPickerSearch";
import {
buildModelPickerSearchText,
rankModelPickerSearch,
scoreModelPickerSearch,
} from "./modelPickerSearch";
describe("buildModelPickerSearchText", () => {
it("builds provider-agnostic search text from generic fields", () => {
@@ -129,3 +133,187 @@ describe("scoreModelPickerSearch", () => {
).not.toBeNull();
});
});
type CatalogModel = {
driverKind: string;
providerDisplayName: string;
name: string;
subProvider?: string;
isFavorite?: boolean;
};
function instanceModels(
driverKind: string,
providerDisplayName: string,
names: ReadonlyArray<string>,
): CatalogModel[] {
return names.map((name) => ({ driverKind, providerDisplayName, name }));
}
// Each provider lists its models newest first, as the picker receives them.
// Claude, Codex, and Antigravity mirror apps/server/src/provider/model-manifest.json;
// Grok and OpenCode report their catalogs at runtime.
const catalog: CatalogModel[] = [
...instanceModels("claudeAgent", "Claude", [
"Claude Opus 5.5",
"Claude Sonnet 5.5",
"Claude Fable 5.1",
"Claude Fable 5",
"Claude Opus 5",
"Claude Opus 4.8",
"Claude Opus 4.7",
"Claude Opus 4.6",
"Claude Opus 4.5",
"Claude Sonnet 5",
"Claude Sonnet 4.6",
"Claude Haiku 5.5",
"Claude Haiku 4.5",
]),
...instanceModels("codex", "Codex", [
"GPT-6.1 Sol",
"GPT-6 Astra",
"GPT-6 Luna",
"GPT-6 Sol",
"GPT-5.6 Sol",
"GPT-5.6 Terra",
"GPT-5.6 Luna",
"GPT-5.5",
]),
...instanceModels("grok", "Grok", ["Grok 4.7", "Grok 4.6", "Grok 4.5"]),
...instanceModels("antigravity", "Antigravity", [
"Gemini 3.8 Flash (High)",
"Gemini 3.8 Flash (Medium)",
"Gemini 3.8 Flash (Low)",
"Gemini 3.7 Flash (High)",
"Gemini 3.7 Flash (Medium)",
"Gemini 3.7 Flash (Low)",
"Gemini 3.6 Flash (High)",
"Gemini 3.6 Flash (Medium)",
"Gemini 3.6 Flash (Low)",
"Gemini 3.1 Pro (High)",
"Gemini 3.1 Pro (Low)",
]),
{
driverKind: "opencode",
providerDisplayName: "OpenCode",
name: "Kimi K3",
subProvider: "Moonshot",
},
{
driverKind: "opencode",
providerDisplayName: "OpenCode",
name: "Kimi K2.5",
subProvider: "Moonshot",
},
{
driverKind: "opencode",
providerDisplayName: "OpenCode",
name: "Kimi K2",
subProvider: "Moonshot",
},
];
function search(query: string, models: ReadonlyArray<CatalogModel> = catalog): string[] {
return rankModelPickerSearch(models, query, (model) => model).map((model) => model.name);
}
describe("rankModelPickerSearch", () => {
it.each([
[
"opus",
[
"Claude Opus 5.5",
"Claude Opus 5",
"Claude Opus 4.8",
"Claude Opus 4.7",
"Claude Opus 4.6",
"Claude Opus 4.5",
],
],
["sonnet", ["Claude Sonnet 5.5", "Claude Sonnet 5", "Claude Sonnet 4.6"]],
["fable", ["Claude Fable 5.1", "Claude Fable 5"]],
["haiku", ["Claude Haiku 5.5", "Claude Haiku 4.5"]],
[
"gpt",
[
"GPT-6.1 Sol",
"GPT-6 Astra",
"GPT-6 Luna",
"GPT-6 Sol",
"GPT-5.6 Sol",
"GPT-5.6 Terra",
"GPT-5.6 Luna",
"GPT-5.5",
],
],
["sol", ["GPT-6.1 Sol", "GPT-6 Sol", "GPT-5.6 Sol"]],
["luna", ["GPT-6 Luna", "GPT-5.6 Luna"]],
["grok", ["Grok 4.7", "Grok 4.6", "Grok 4.5"]],
[
"flash",
[
"Gemini 3.8 Flash (High)",
"Gemini 3.8 Flash (Medium)",
"Gemini 3.8 Flash (Low)",
"Gemini 3.7 Flash (High)",
"Gemini 3.7 Flash (Medium)",
"Gemini 3.7 Flash (Low)",
"Gemini 3.6 Flash (High)",
"Gemini 3.6 Flash (Medium)",
"Gemini 3.6 Flash (Low)",
],
],
["kimi", ["Kimi K3", "Kimi K2.5", "Kimi K2"]],
])("lists equally good matches for %j newest first", (query, expected) => {
// Loose fuzzy matches from other families may follow; they rank last.
expect(search(query).slice(0, expected.length)).toEqual(expected);
});
it("keeps newest first when a version token narrows the family", () => {
expect(search("opus 4")).toEqual([
"Claude Opus 4.8",
"Claude Opus 4.7",
"Claude Opus 4.6",
"Claude Opus 4.5",
]);
// "5" only appears mid-version in 4.5, a weaker match.
expect(search("opus 5")).toEqual(["Claude Opus 5.5", "Claude Opus 5", "Claude Opus 4.5"]);
expect(search("opus 5.")).toEqual(["Claude Opus 5.5"]);
});
it("still ranks an exact name above newer models that extend it", () => {
expect(search("gpt-6 sol").slice(0, 2)).toEqual(["GPT-6 Sol", "GPT-6.1 Sol"]);
expect(search("claude opus 5").slice(0, 2)).toEqual(["Claude Opus 5", "Claude Opus 5.5"]);
expect(search("Grok 4.6")[0]).toBe("Grok 4.6");
});
it("matches an exact name regardless of extra whitespace in the query", () => {
expect(search(" claude opus 5 ")[0]).toBe("Claude Opus 5");
});
it("ranks an exact name above a favorite partial match", () => {
const models = catalog.map((model) =>
model.name === "Claude Opus 5.5" ? { ...model, isFavorite: true } : model,
);
expect(search("claude opus 5", models)[0]).toBe("Claude Opus 5");
});
it("matches the provider when the name does not", () => {
expect(search("moonshot")).toEqual(["Kimi K3", "Kimi K2.5", "Kimi K2"]);
});
it("ranks a favorite above newer models in the same family", () => {
const models = catalog.map((model) =>
model.name === "Claude Opus 4.5" ? { ...model, isFavorite: true } : model,
);
expect(search("opus", models).slice(0, 3)).toEqual([
"Claude Opus 4.5",
"Claude Opus 5.5",
"Claude Opus 5",
]);
});
it("follows the order it is given, including a user's custom model order", () => {
expect(search("grok", catalog.toReversed())).toEqual(["Grok 4.5", "Grok 4.6", "Grok 4.7"]);
});
});
@@ -16,6 +16,8 @@ type ModelPickerSearchableModel = {
};
const MODEL_PICKER_FAVORITE_SCORE_BOOST = 24;
// Far below any token-scored match, favorites included.
const MODEL_PICKER_EXACT_NAME_SCORE = -1000;
function getModelPickerSearchFields(model: ModelPickerSearchableModel): string[] {
return [
@@ -41,9 +43,14 @@ function scoreModelPickerSearchToken(
boundaryBase: fieldBase + 4,
includesBase: fieldBase + 6,
...(token.length >= 3 ? { fuzzyBase: fieldBase + 100 } : {}),
ignorePosition: true,
});
}
function normalizeModelName(name: string): string {
return normalizeSearchQuery(name).replace(/\s+/gu, " ");
}
export function buildModelPickerSearchText(model: ModelPickerSearchableModel): string {
return normalizeSearchQuery(
[model.name, model.shortName, model.subProvider, model.driverKind, model.providerDisplayName]
@@ -64,6 +71,14 @@ export function scoreModelPickerSearch(
return 0;
}
const normalizedQuery = tokens.join(" ");
if (
normalizedQuery === normalizeModelName(model.name) ||
normalizedQuery === normalizeModelName(model.shortName ?? "")
) {
return MODEL_PICKER_EXACT_NAME_SCORE;
}
const fields = getModelPickerSearchFields(model);
let score = 0;
@@ -85,3 +100,38 @@ export function scoreModelPickerSearch(
return model.isFavorite ? score - MODEL_PICKER_FAVORITE_SCORE_BOOST : score;
}
/**
* Filters `items` to those matching `query`, best match first. Equal matches
* keep their order in `items`, which is the provider's own newest-first order.
*/
export function rankModelPickerSearch<T>(
items: ReadonlyArray<T>,
query: string,
toSearchable: (item: T) => ModelPickerSearchableModel,
): T[] {
const ranked: Array<{
item: T;
score: number;
isFavorite: boolean;
order: number;
}> = [];
items.forEach((item, order) => {
const searchable = toSearchable(item);
const score = scoreModelPickerSearch(searchable, query);
if (score !== null) {
ranked.push({
item,
score,
isFavorite: searchable.isFavorite === true,
order,
});
}
});
return ranked
.toSorted(
(a, b) =>
a.score - b.score || Number(b.isFavorite) - Number(a.isFavorite) || a.order - b.order,
)
.map((entry) => entry.item);
}
+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;
}