Skip to content

Commit 4660ee0

Browse files
committed
Sort model names when loading models-dev.json
1 parent 9549919 commit 4660ee0

File tree

5 files changed

+97
-63
lines changed

5 files changed

+97
-63
lines changed

src/platform/src/Bridge/ModelsDev/DataLoader.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ public static function load(?string $dataPath = null): array
5555
throw new RuntimeException('Invalid models.dev API data.');
5656
}
5757

58-
self::$cachedData = $data;
58+
// Sort providers alphabetically and sort models within each provider by release date
59+
$sortedData = self::sortData($data);
60+
61+
self::$cachedData = $sortedData;
5962
self::$cachedPath = $dataPath;
6063

61-
return $data;
64+
return $sortedData;
6265
}
6366

6467
/**
@@ -71,4 +74,38 @@ public static function clearCache(): void
7174
self::$cachedData = null;
7275
self::$cachedPath = null;
7376
}
77+
78+
/**
79+
* Sort providers alphabetically and models by release date (newest first).
80+
*
81+
* @param array<string, array<string, mixed>> $data
82+
*
83+
* @return array<string, array<string, mixed>>
84+
*/
85+
private static function sortData(array $data): array
86+
{
87+
// Sort providers alphabetically
88+
ksort($data);
89+
90+
// Sort models within each provider by release date (newest first)
91+
foreach ($data as $providerId => &$provider) {
92+
if (isset($provider['models']) && \is_array($provider['models'])) {
93+
// Convert models to array with keys preserved
94+
$models = $provider['models'];
95+
96+
// Sort models by release_date descending
97+
uasort($models, static function ($a, $b) {
98+
$dateA = $a['release_date'] ?? '1970-01-01';
99+
$dateB = $b['release_date'] ?? '1970-01-01';
100+
101+
// Compare dates in descending order (newer dates first)
102+
return strcmp($dateB, $dateA);
103+
});
104+
105+
$provider['models'] = $models;
106+
}
107+
}
108+
109+
return $data;
110+
}
74111
}

src/platform/src/Bridge/ModelsDev/ModelResolver.php

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* Accepted input formats (resolved in that order):
2020
*
21-
* * "provider:model": Explicit form like "anthropic:claude-opus-4-5"
21+
* * "provider::model": Explicit form like "anthropic::claude-opus-4-5"
2222
*
2323
* * "provider": Provider-only, returns the first non-deprecated model listed
2424
* in that provider's catalog.
@@ -51,15 +51,15 @@ public function __construct(
5151
/**
5252
* Resolves a model specification to a (provider, modelId) pair.
5353
*
54-
* @return array{provider: string, modelId: string}
54+
* @return array{provider: string, model_id: string}
5555
*
5656
* @throws InvalidArgumentException when the provider or model cannot be determined
5757
*/
5858
public function resolve(string $modelSpec): array
5959
{
60-
// "provider:model"
61-
if (str_contains($modelSpec, ':')) {
62-
[$provider, $modelId] = explode(':', $modelSpec, 2);
60+
// "provider::model"
61+
if (str_contains($modelSpec, '::')) {
62+
[$provider, $modelId] = explode('::', $modelSpec, 2);
6363

6464
if ('' === $provider) {
6565
throw new InvalidArgumentException(\sprintf('Invalid model specification "%s": provider part is empty.', $modelSpec));
@@ -71,7 +71,7 @@ public function resolve(string $modelSpec): array
7171
return ['provider' => $provider, 'model_id' => $modelId];
7272
}
7373

74-
// "provider"
74+
// "provider"
7575
if ($this->registry->has($modelSpec)) {
7676
return ['provider' => $modelSpec, 'model_id' => $this->resolveFirstModel($modelSpec)];
7777
}
@@ -87,12 +87,12 @@ public function resolve(string $modelSpec): array
8787
*/
8888
private function resolveFirstModel(string $provider): string
8989
{
90-
if (null === $catalog = $this->getCatalog($provider)) {
91-
throw new InvalidArgumentException(\sprintf('No model catalog found for provider "%s"; use "provider:model" format.', $provider));
90+
if (null === $catalog = $this->registry->getCatalog($provider)) {
91+
throw new InvalidArgumentException(\sprintf('No model catalog found for provider "%s"; use "provider::model" format.', $provider));
9292
}
9393

9494
if ([] === $models = array_keys($catalog->getModels())) {
95-
throw new InvalidArgumentException(\sprintf('No models found for provider "%s"; use "provider:model" format.', $provider));
95+
throw new InvalidArgumentException(\sprintf('No models found for provider "%s"; use "provider::model" format.', $provider));
9696
}
9797

9898
return $models[0];
@@ -109,12 +109,7 @@ private function findProviderForModel(string $modelId): string
109109
{
110110
// Check canonical providers first for deterministic resolution
111111
foreach (self::CANONICAL_PROVIDERS as $providerId) {
112-
if (!$this->registry->has($providerId)) {
113-
continue;
114-
}
115-
116-
$catalog = $this->getCatalog($providerId);
117-
if (null !== $catalog && isset($catalog->getModels()[$modelId])) {
112+
if (isset($this->registry->getCatalog($providerId)?->getModels()[$modelId])) {
118113
return $providerId;
119114
}
120115
}
@@ -125,21 +120,11 @@ private function findProviderForModel(string $modelId): string
125120
continue; // already checked
126121
}
127122

128-
$catalog = $this->getCatalog($providerId);
129-
if (null !== $catalog && isset($catalog->getModels()[$modelId])) {
123+
if (isset($this->registry->getCatalog($providerId)?->getModels()[$modelId])) {
130124
return $providerId;
131125
}
132126
}
133127

134-
throw new InvalidArgumentException(\sprintf('Cannot determine provider for model "%s"; Use "provider:model" format to specify it explicitly.', $modelId));
135-
}
136-
137-
private function getCatalog(string $provider): ?ModelCatalog
138-
{
139-
try {
140-
return new ModelCatalog($provider, $this->dataPath);
141-
} catch (\Throwable) {
142-
return null;
143-
}
128+
throw new InvalidArgumentException(\sprintf('Cannot determine provider for model "%s"; Use "provider::model" format to specify it explicitly.', $modelId));
144129
}
145130
}

src/platform/src/Bridge/ModelsDev/ProviderRegistry.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ final class ProviderRegistry
2525
*/
2626
private readonly array $providers;
2727

28-
public function __construct(?string $dataPath = null)
29-
{
28+
public function __construct(
29+
private ?string $dataPath = null,
30+
) {
3031
$providers = [];
3132
foreach (DataLoader::load($dataPath) as $providerId => $providerData) {
3233
$providers[$providerId] = [
@@ -59,6 +60,15 @@ public function getProviderName(string $providerId): string
5960
return $this->providers[$providerId]['name'];
6061
}
6162

63+
public function getCatalog(string $providerId): ?ModelCatalog
64+
{
65+
if (!isset($this->providers[$providerId])) {
66+
return null;
67+
}
68+
69+
return new ModelCatalog($providerId, $this->dataPath);
70+
}
71+
6272
public function has(string $providerId): bool
6373
{
6474
return isset($this->providers[$providerId]);

0 commit comments

Comments
 (0)