Skip to content

Commit e813e83

Browse files
committed
[Platform] Refactor model size variant logic into parseModelName
Move the size variant resolution logic from getModel() into parseModelName() for better separation of concerns. The parseModelName() method is now non-static and returns a catalogKey for lookups while preserving the full model name. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 9b1b98f commit e813e83

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

src/platform/src/ModelCatalog/AbstractModelCatalog.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,16 @@ public function getModel(string $modelName): Model
3232
throw new InvalidArgumentException('Model name cannot be empty.');
3333
}
3434

35-
$parsed = self::parseModelName($modelName);
35+
$parsed = $this->parseModelName($modelName);
3636
$actualModelName = $parsed['name'];
37+
$catalogKey = $parsed['catalogKey'];
3738
$options = $parsed['options'];
3839

39-
// Try to find the model directly first
40-
if (!isset($this->models[$actualModelName])) {
41-
// If not found and contains a colon (e.g., "model:23b"), try the base model name
42-
if (str_contains($actualModelName, ':')) {
43-
$baseModelName = explode(':', $actualModelName, 2)[0];
44-
45-
if (isset($this->models[$baseModelName])) {
46-
// Use the base model's config but keep the full model name
47-
$modelConfig = $this->models[$baseModelName];
48-
$modelClass = $modelConfig['class'];
49-
50-
if (!class_exists($modelClass)) {
51-
throw new InvalidArgumentException(\sprintf('Model class "%s" does not exist.', $modelClass));
52-
}
53-
54-
$model = new $modelClass($actualModelName, $modelConfig['capabilities'], $options);
55-
if (!$model instanceof Model) {
56-
throw new InvalidArgumentException(\sprintf('Model class "%s" must extend "%s".', $modelClass, Model::class));
57-
}
58-
59-
return $model;
60-
}
61-
}
62-
40+
if (!isset($this->models[$catalogKey])) {
6341
throw new ModelNotFoundException(\sprintf('Model "%s" not found.', $actualModelName));
6442
}
6543

66-
$modelConfig = $this->models[$actualModelName];
44+
$modelConfig = $this->models[$catalogKey];
6745
$modelClass = $modelConfig['class'];
6846

6947
if (!class_exists($modelClass)) {
@@ -88,12 +66,13 @@ public function getModels(): array
8866

8967
/**
9068
* Extracts model name and options from a model name string that may contain query parameters.
69+
* Also resolves size variants (e.g., "model:23b") to their base model for catalog lookup.
9170
*
9271
* @param string $modelName The model name, potentially with query parameters (e.g., "model-name?param=value&other=123")
9372
*
94-
* @return array{name: string, options: array<string, mixed>} An array containing the model name and parsed options
73+
* @return array{name: string, catalogKey: string, options: array<string, mixed>} An array containing the model name, catalog lookup key, and parsed options
9574
*/
96-
protected static function parseModelName(string $modelName): array
75+
protected function parseModelName(string $modelName): array
9776
{
9877
$options = [];
9978
$actualModelName = $modelName;
@@ -110,8 +89,18 @@ protected static function parseModelName(string $modelName): array
11089
$options = self::convertNumericStrings($options);
11190
}
11291

92+
// Determine catalog key: try exact match first, then fall back to base model
93+
$catalogKey = $actualModelName;
94+
if (!isset($this->models[$actualModelName]) && str_contains($actualModelName, ':')) {
95+
$baseModelName = explode(':', $actualModelName, 2)[0];
96+
if (isset($this->models[$baseModelName])) {
97+
$catalogKey = $baseModelName;
98+
}
99+
}
100+
113101
return [
114102
'name' => $actualModelName,
103+
'catalogKey' => $catalogKey,
115104
'options' => $options,
116105
];
117106
}

0 commit comments

Comments
 (0)