diff --git a/src/shared/native-chat-session-option-state.test.ts b/src/shared/native-chat-session-option-state.test.ts index f2827207bf7..0cd0af94d80 100644 --- a/src/shared/native-chat-session-option-state.test.ts +++ b/src/shared/native-chat-session-option-state.test.ts @@ -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') diff --git a/src/shared/native-chat-session-option-state.ts b/src/shared/native-chat-session-option-state.ts index 5d070969775..14eddfa7ff9 100644 --- a/src/shared/native-chat-session-option-state.ts +++ b/src/shared/native-chat-session-option-state.ts @@ -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 }