-
Notifications
You must be signed in to change notification settings - Fork 3.7k
improvement(models): sort model dropdown by latest release date within each provider #5099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| getBaseModelProviders, | ||
| orderModelIdsByReleaseDate, | ||
| PROVIDER_DEFINITIONS, | ||
| } from '@/providers/models' | ||
|
|
||
| /** Maps a lowercased model ID to its provider's index in the catalog. */ | ||
| const PROVIDER_INDEX_BY_MODEL = new Map<string, number>() | ||
| /** Maps a lowercased model ID to its release time (ms), or null when undated. */ | ||
| const RELEASE_TIME_BY_MODEL = new Map<string, number | null>() | ||
| for (const [providerIndex, provider] of Object.values(PROVIDER_DEFINITIONS).entries()) { | ||
| for (const model of provider.models) { | ||
| const id = model.id.toLowerCase() | ||
| PROVIDER_INDEX_BY_MODEL.set(id, providerIndex) | ||
| RELEASE_TIME_BY_MODEL.set(id, model.releaseDate ? Date.parse(model.releaseDate) : null) | ||
| } | ||
| } | ||
|
|
||
| describe('orderModelIdsByReleaseDate', () => { | ||
| it('keeps provider grouping order intact', () => { | ||
| const ordered = orderModelIdsByReleaseDate(Object.keys(getBaseModelProviders())) | ||
| let lastProviderIndex = -1 | ||
| const seenProviders = new Set<number>() | ||
| for (const id of ordered) { | ||
| const providerIndex = PROVIDER_INDEX_BY_MODEL.get(id.toLowerCase()) | ||
| expect(providerIndex).toBeDefined() | ||
| // A provider's models must form one contiguous run: once we leave a provider | ||
| // we never return to it. | ||
| if (providerIndex !== lastProviderIndex) { | ||
| expect(seenProviders.has(providerIndex as number)).toBe(false) | ||
| seenProviders.add(providerIndex as number) | ||
| lastProviderIndex = providerIndex as number | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| it('sorts models within a provider newest-first by release date', () => { | ||
| const ordered = orderModelIdsByReleaseDate(Object.keys(getBaseModelProviders())) | ||
| for (let i = 1; i < ordered.length; i++) { | ||
| const prev = ordered[i - 1].toLowerCase() | ||
| const curr = ordered[i].toLowerCase() | ||
| if (PROVIDER_INDEX_BY_MODEL.get(prev) !== PROVIDER_INDEX_BY_MODEL.get(curr)) continue | ||
|
|
||
| const prevTime = RELEASE_TIME_BY_MODEL.get(prev) | ||
| const currTime = RELEASE_TIME_BY_MODEL.get(curr) | ||
| // Dated models precede undated ones; among dated models, newer precedes older. | ||
| if (prevTime == null) { | ||
| expect(currTime).toBeNull() | ||
| } else if (currTime != null) { | ||
| expect(prevTime).toBeGreaterThanOrEqual(currTime) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| it('places unknown model IDs last, preserving their input order', () => { | ||
| const known = Object.keys(getBaseModelProviders())[0] | ||
| const ordered = orderModelIdsByReleaseDate(['mystery-a', known, 'mystery-b']) | ||
| expect(ordered[0]).toBe(known) | ||
| expect(ordered.slice(1)).toEqual(['mystery-a', 'mystery-b']) | ||
| }) | ||
|
|
||
| it('is case-insensitive when matching catalog IDs', () => { | ||
| const id = Object.keys(getBaseModelProviders())[0] | ||
| const ordered = orderModelIdsByReleaseDate([id.toUpperCase()]) | ||
| expect(ordered).toEqual([id.toUpperCase()]) | ||
| }) | ||
|
|
||
| it('returns an empty array for empty input', () => { | ||
| expect(orderModelIdsByReleaseDate([])).toEqual([]) | ||
| }) | ||
|
|
||
| it('does not add or drop any IDs', () => { | ||
| const input = Object.keys(getBaseModelProviders()) | ||
| const ordered = orderModelIdsByReleaseDate(input) | ||
| expect([...ordered].sort()).toEqual([...input].sort()) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.