Skip to content

Commit 14ebe59

Browse files
fix(web): model picker search ties no longer favor shorter version names
Matches in the same tier now score equally regardless of where in the name they land, so the catalog's newest-first order decides ties (GPT-6.1 Sol no longer ranks below the legacy GPT-6 Sol). The exact-name check also ignores extra whitespace in the query. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d9efbc3 commit 14ebe59

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎apps/web/src/components/chat/modelPickerSearch.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ describe("rankModelPickerSearch", () => {
287287
expect(search("Grok 4.6")[0]).toBe("Grok 4.6");
288288
});
289289

290+
it("matches an exact name regardless of extra whitespace in the query", () => {
291+
expect(search(" claude opus 5 ")[0]).toBe("Claude Opus 5");
292+
});
293+
290294
it("ranks an exact name above a favorite partial match", () => {
291295
const models = catalog.map((model) =>
292296
model.name === "Claude Opus 5.5" ? { ...model, isFavorite: true } : model,

‎apps/web/src/components/chat/modelPickerSearch.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ function scoreModelPickerSearchToken(
4343
boundaryBase: fieldBase + 4,
4444
includesBase: fieldBase + 6,
4545
...(token.length >= 3 ? { fuzzyBase: fieldBase + 100 } : {}),
46-
ignoreLength: true,
46+
ignorePosition: true,
4747
});
4848
}
4949

50+
function normalizeModelName(name: string): string {
51+
return normalizeSearchQuery(name).replace(/\s+/gu, " ");
52+
}
53+
5054
export function buildModelPickerSearchText(model: ModelPickerSearchableModel): string {
5155
return normalizeSearchQuery(
5256
[model.name, model.shortName, model.subProvider, model.driverKind, model.providerDisplayName]
@@ -59,16 +63,18 @@ export function scoreModelPickerSearch(
5963
model: ModelPickerSearchableModel,
6064
query: string,
6165
): number | null {
62-
const normalizedQuery = normalizeSearchQuery(query);
63-
const tokens = normalizedQuery.split(/\s+/u).filter((token) => token.length > 0);
66+
const tokens = normalizeSearchQuery(query)
67+
.split(/\s+/u)
68+
.filter((token) => token.length > 0);
6469

6570
if (tokens.length === 0) {
6671
return 0;
6772
}
6873

74+
const normalizedQuery = tokens.join(" ");
6975
if (
70-
normalizedQuery === normalizeSearchQuery(model.name) ||
71-
normalizedQuery === normalizeSearchQuery(model.shortName ?? "")
76+
normalizedQuery === normalizeModelName(model.name) ||
77+
normalizedQuery === normalizeModelName(model.shortName ?? "")
7278
) {
7379
return MODEL_PICKER_EXACT_NAME_SCORE;
7480
}

‎packages/shared/src/searchRanking.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function normalizeSearchQuery(
2222
export function scoreSubsequenceMatch(
2323
value: string,
2424
query: string,
25-
options?: { ignoreLength?: boolean },
25+
options?: { ignorePosition?: boolean },
2626
): number | null {
2727
if (!query) return 0;
2828

@@ -47,7 +47,10 @@ export function scoreSubsequenceMatch(
4747
queryIndex += 1;
4848
if (queryIndex === query.length) {
4949
const spanPenalty = valueIndex - firstMatchIndex + 1 - query.length;
50-
const lengthPenalty = options?.ignoreLength ? 0 : Math.min(64, value.length - query.length);
50+
if (options?.ignorePosition) {
51+
return gapPenalty * 3 + spanPenalty;
52+
}
53+
const lengthPenalty = Math.min(64, value.length - query.length);
5154
return firstMatchIndex * 2 + gapPenalty * 3 + spanPenalty + lengthPenalty;
5255
}
5356
}
@@ -96,10 +99,15 @@ export function scoreQueryMatch(input: {
9699
includesBase?: number;
97100
fuzzyBase?: number;
98101
boundaryMarkers?: readonly string[];
99-
ignoreLength?: boolean;
102+
/**
103+
* Score by match tier alone, ignoring where the match lands and how long the
104+
* value is, so callers can keep their own order among equal matches.
105+
*/
106+
ignorePosition?: boolean;
100107
}): number | null {
101108
const { value, query } = input;
102-
const penalty = input.ignoreLength ? 0 : lengthPenalty(value, query);
109+
const ignorePosition = input.ignorePosition === true;
110+
const penalty = ignorePosition ? 0 : lengthPenalty(value, query);
103111

104112
if (!value || !query) {
105113
return null;
@@ -120,21 +128,19 @@ export function scoreQueryMatch(input: {
120128
input.boundaryMarkers ?? [" ", "-", "_", "/"],
121129
);
122130
if (boundaryIndex !== null) {
123-
return input.boundaryBase + boundaryIndex * 2 + penalty;
131+
return input.boundaryBase + (ignorePosition ? 0 : boundaryIndex * 2) + penalty;
124132
}
125133
}
126134

127135
if (input.includesBase !== undefined) {
128136
const includesIndex = value.indexOf(query);
129137
if (includesIndex !== -1) {
130-
return input.includesBase + includesIndex * 2 + penalty;
138+
return input.includesBase + (ignorePosition ? 0 : includesIndex * 2) + penalty;
131139
}
132140
}
133141

134142
if (input.fuzzyBase !== undefined) {
135-
const fuzzyScore = scoreSubsequenceMatch(value, query, {
136-
ignoreLength: input.ignoreLength === true,
137-
});
143+
const fuzzyScore = scoreSubsequenceMatch(value, query, { ignorePosition });
138144
if (fuzzyScore !== null) {
139145
return input.fuzzyBase + fuzzyScore;
140146
}

0 commit comments

Comments
 (0)