Refactor the schema tool array to use normalizeSchemaProperties method (#223)
This commit is contained in:
parent
ab4f686e18
commit
e485fde727
1 changed files with 21 additions and 8 deletions
|
|
@ -84,27 +84,24 @@ class Tool implements \JsonSerializable
|
||||||
if (!isset($data['inputSchema']['type']) || 'object' !== $data['inputSchema']['type']) {
|
if (!isset($data['inputSchema']['type']) || 'object' !== $data['inputSchema']['type']) {
|
||||||
throw new InvalidArgumentException('Tool inputSchema must be of type "object".');
|
throw new InvalidArgumentException('Tool inputSchema must be of type "object".');
|
||||||
}
|
}
|
||||||
if (isset($data['inputSchema']['properties']) && \is_array($data['inputSchema']['properties']) && empty($data['inputSchema']['properties'])) {
|
$inputSchema = self::normalizeSchemaProperties($data['inputSchema']);
|
||||||
$data['inputSchema']['properties'] = new \stdClass();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$outputSchema = null;
|
||||||
if (isset($data['outputSchema']) && \is_array($data['outputSchema'])) {
|
if (isset($data['outputSchema']) && \is_array($data['outputSchema'])) {
|
||||||
if (!isset($data['outputSchema']['type']) || 'object' !== $data['outputSchema']['type']) {
|
if (!isset($data['outputSchema']['type']) || 'object' !== $data['outputSchema']['type']) {
|
||||||
throw new InvalidArgumentException('Tool outputSchema must be of type "object".');
|
throw new InvalidArgumentException('Tool outputSchema must be of type "object".');
|
||||||
}
|
}
|
||||||
if (isset($data['outputSchema']['properties']) && \is_array($data['outputSchema']['properties']) && empty($data['outputSchema']['properties'])) {
|
$outputSchema = self::normalizeSchemaProperties($data['outputSchema']);
|
||||||
$data['outputSchema']['properties'] = new \stdClass();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new self(
|
return new self(
|
||||||
$data['name'],
|
$data['name'],
|
||||||
$data['inputSchema'],
|
$inputSchema,
|
||||||
isset($data['description']) && \is_string($data['description']) ? $data['description'] : null,
|
isset($data['description']) && \is_string($data['description']) ? $data['description'] : null,
|
||||||
isset($data['annotations']) && \is_array($data['annotations']) ? ToolAnnotations::fromArray($data['annotations']) : null,
|
isset($data['annotations']) && \is_array($data['annotations']) ? ToolAnnotations::fromArray($data['annotations']) : null,
|
||||||
isset($data['icons']) && \is_array($data['icons']) ? array_map(Icon::fromArray(...), $data['icons']) : null,
|
isset($data['icons']) && \is_array($data['icons']) ? array_map(Icon::fromArray(...), $data['icons']) : null,
|
||||||
isset($data['_meta']) && \is_array($data['_meta']) ? $data['_meta'] : null,
|
isset($data['_meta']) && \is_array($data['_meta']) ? $data['_meta'] : null,
|
||||||
isset($data['outputSchema']) && \is_array($data['outputSchema']) ? $data['outputSchema'] : null,
|
$outputSchema,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -143,4 +140,20 @@ class Tool implements \JsonSerializable
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize schema properties: convert an empty properties array to stdClass.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $schema
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private static function normalizeSchemaProperties(array $schema): array
|
||||||
|
{
|
||||||
|
if (isset($schema['properties']) && \is_array($schema['properties']) && empty($schema['properties'])) {
|
||||||
|
$schema['properties'] = new \stdClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $schema;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue