Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/platform/src/ModelCatalog/AbstractModelCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function parseModelName(string $modelName): array

parse_str($queryString, $options);

$options = self::convertNumericStrings($options);
$options = self::convertScalarStrings($options);
}

// Determine catalog key: try exact match first, then fall back to base model
Expand All @@ -110,13 +110,17 @@ protected function parseModelName(string $modelName): array
*
* @param array<string, mixed> $data The array to process
*
* @return array<string, mixed> The array with numeric strings converted to appropriate numeric types
* @return array<string, mixed> The array with numeric and boolean-like strings converted to appropriate numeric/boolean types
*/
private static function convertNumericStrings(array $data): array
private static function convertScalarStrings(array $data): array
{
foreach ($data as $key => $value) {
if (\is_array($value)) {
$data[$key] = self::convertNumericStrings($value);
$data[$key] = self::convertScalarStrings($value);
} elseif ('true' === $value) {
$data[$key] = true;
} elseif ('false' === $value) {
$data[$key] = false;
} elseif (is_numeric($value) && \is_string($value)) {
// Convert to int if it's a whole number, otherwise to float
$data[$key] = str_contains($value, '.') ? (float) $value : (int) $value;
Expand Down
35 changes: 34 additions & 1 deletion src/platform/tests/ModelCatalog/AbstractModelCatalogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ public function testGetModelWithIntegerQueryParameter()
$this->assertSame(500, $options['max_tokens']);
}

public function testGetModelWithBooleanQueryParameters()
{
$catalog = $this->createTestCatalog();
$model = $catalog->getModel('test-model?think=true&stream=false');

$this->assertSame('test-model', $model->getName());
$options = $model->getOptions();
$this->assertArrayHasKey('think', $options);
$this->assertIsBool($options['think']);
$this->assertTrue($options['think']);
$this->assertArrayHasKey('stream', $options);
$this->assertIsBool($options['stream']);
$this->assertFalse($options['stream']);
}

public function testGetModelWithMultipleQueryParameters()
{
$catalog = $this->createTestCatalog();
Expand All @@ -66,7 +81,8 @@ public function testGetModelWithMultipleQueryParameters()
$this->assertSame(0.7, $options['temperature']);

$this->assertArrayHasKey('stream', $options);
$this->assertSame('true', $options['stream']);
$this->assertIsBool($options['stream']);
$this->assertTrue($options['stream']);
}

public function testGetModelWithNestedArrayQueryParameters()
Expand Down Expand Up @@ -125,6 +141,23 @@ public function testNumericStringsAreConvertedRecursively()
$this->assertIsInt($options['a']['e']);
}

public function testBooleanStringsAreConvertedRecursively()
{
$catalog = $this->createTestCatalog();
$model = $catalog->getModel('test-model?a[b][c]=true&a[b][d]=text&a[e]=false');

$options = $model->getOptions();

$this->assertIsArray($options['a']);
$this->assertIsArray($options['a']['b']);
$this->assertIsBool($options['a']['b']['c']);
$this->assertTrue($options['a']['b']['c']);
$this->assertIsString($options['a']['b']['d']);
$this->assertSame('text', $options['a']['b']['d']);
$this->assertIsBool($options['a']['e']);
$this->assertFalse($options['a']['e']);
}

private function createTestCatalog(): AbstractModelCatalog
{
return new class extends AbstractModelCatalog {
Expand Down