Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,15 @@ protected function prepareToolsParam(array $functionDeclarations): array
{
$tools = [];
foreach ($functionDeclarations as $functionDeclaration) {
$function = $functionDeclaration->toArray();
$parameters = $functionDeclaration->getJsonSerializableParameters();
if ($parameters !== null) {
$function[FunctionDeclaration::KEY_PARAMETERS] = $parameters;
}

$tools[] = [
'type' => 'function',
'function' => $functionDeclaration->toArray(),
'function' => $function,
];
}

Expand Down
85 changes: 85 additions & 0 deletions src/Tools/DTO/FunctionDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ public function getParameters(): ?array
return $this->parameters;
}

/**
* Gets the function parameters schema in a JSON-serializable form.
*
* JSON Schema object-map fields such as properties must encode as JSON
* objects even when empty. PHP arrays cannot preserve that distinction
* without casting the empty map before serialization.
*
* @since 0.1.0
*
* @return array<string, mixed>|\stdClass|null The JSON-serializable parameters schema.
*/
public function getJsonSerializableParameters()
{
if ($this->parameters === null) {
return null;
}

/** @var array<string, mixed>|\stdClass $parameters */
$parameters = $this->prepareJsonSchemaObjectMaps($this->parameters, self::KEY_PARAMETERS);
return $parameters;
}

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -143,6 +165,69 @@ public function toArray(): array
return $data;
}

/**
* {@inheritDoc}
*
* @since 0.1.0
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$data = $this->toArray();

if ($this->parameters !== null) {
$data[self::KEY_PARAMETERS] = $this->getJsonSerializableParameters();
}

return $data;
}

/**
* Recursively prepares JSON Schema object-map fields for JSON serialization.
*
* @since 0.1.0
*
* @param mixed $value The value to prepare.
* @param string|null $key The current JSON Schema key, if available.
* @return mixed The prepared value.
*/
private function prepareJsonSchemaObjectMaps($value, ?string $key = null)
{
if (!is_array($value)) {
return $value;
}

if ($value === [] && $this->isJsonSchemaObjectMapKey($key)) {
return new \stdClass();
}

foreach ($value as $childKey => $childValue) {
$value[$childKey] = $this->prepareJsonSchemaObjectMaps(
$childValue,
is_string($childKey) ? $childKey : null
);
}

return $value;
}

/**
* Checks whether the given JSON Schema key represents an object map.
*
* @since 0.1.0
*
* @param string|null $key The JSON Schema key.
* @return bool True if the key represents an object map, false otherwise.
*/
private function isJsonSchemaObjectMapKey(?string $key): bool
{
return in_array(
$key,
[self::KEY_PARAMETERS, 'properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas'],
true
);
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,35 @@ public function testPrepareToolsParam(): void
$this->assertEquals($functionDeclaration2->toArray(), $prepared[1]['function']);
}

/**
* Tests prepareToolsParam() preserves empty JSON Schema object maps.
*
* @return void
*/
public function testPrepareToolsParamPreservesEmptySchemaObjectMaps(): void
{
$functionDeclaration = new FunctionDeclaration(
'inspect_object',
'Inspects an object with optional nested metadata',
[
'type' => 'object',
'properties' => [
'metadata' => [
'type' => 'object',
'properties' => [],
],
],
]
);
$model = $this->createModel();

$prepared = $model->exposePrepareToolsParam([$functionDeclaration]);
$json = json_encode($prepared, JSON_THROW_ON_ERROR);

$this->assertStringContainsString('"properties":{', $json);
$this->assertStringNotContainsString('"properties":[]', $json);
}

/**
* Tests prepareResponseFormatParam() with null schema.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/Tools/DTO/FunctionDeclarationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function testCreateWithoutParameters(): void
$this->assertEquals($name, $declaration->getName());
$this->assertEquals($description, $declaration->getDescription());
$this->assertNull($declaration->getParameters());
$this->assertNull($declaration->getJsonSerializableParameters());
}

/**
Expand Down Expand Up @@ -206,6 +207,33 @@ public function testToArrayWithParameters(): void
);
}

/**
* Tests JSON serialization with empty JSON Schema object maps.
*
* @return void
*/
public function testJsonSerializationPreservesEmptySchemaObjectMaps(): void
{
$declaration = new FunctionDeclaration(
'inspectObject',
'Inspects an object with optional nested metadata',
[
'type' => 'object',
'properties' => [
'metadata' => [
'type' => 'object',
'properties' => [],
],
],
]
);

$json = json_encode($declaration, JSON_THROW_ON_ERROR);

$this->assertStringContainsString('"properties":{', $json);
$this->assertStringNotContainsString('"properties":[]', $json);
}

/**
* Tests array transformation without parameters.
*
Expand Down
Loading