perf: select matching model IDs without sorting (#20334)

Co-authored-by: Orca Worker <orca-worker@localhost>
This commit is contained in:
OrcaWin
2026-09-12 18:11:11 -07:00
committed by GitHub
co-authored by Orca Worker
parent 91ecd32711
commit 13c0c8b535
2 changed files with 19 additions and 4 deletions
@@ -48,6 +48,15 @@ describe('applyNativeChatReportedSessionOptions', () => {
})
describe('matchNativeChatCatalogModelId', () => {
it('prefers the longest contained id and keeps catalog order for ties', () => {
const catalog = {
...CLAUDE_SESSION_OPTION_CATALOG,
models: ['a', 'abc', 'xyz'].map((id) => ({ id, label: id, options: [] }))
}
expect(matchNativeChatCatalogModelId(catalog, 'provider-xyz-abc')).toBe('abc')
expect(catalog.models.map((model) => model.id)).toEqual(['a', 'abc', 'xyz'])
})
it('matches exact ids, labels, and provider-id containment', () => {
expect(matchNativeChatCatalogModelId(CLAUDE_SESSION_OPTION_CATALOG, 'sonnet')).toBe('sonnet')
expect(matchNativeChatCatalogModelId(CLAUDE_SESSION_OPTION_CATALOG, 'Sonnet 5')).toBe('sonnet')
+10 -4
View File
@@ -167,8 +167,14 @@ export function matchNativeChatCatalogModelId(
if (byLabel) {
return byLabel.id
}
const containing = [...catalog.models]
.sort((left, right) => right.id.length - left.id.length)
.find((model) => normalized.includes(model.id.toLowerCase()))
return containing?.id ?? null
let containingId: string | null = null
for (const model of catalog.models) {
if (
(containingId === null || model.id.length > containingId.length) &&
normalized.includes(model.id.toLowerCase())
) {
containingId = model.id
}
}
return containingId
}