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
13 changes: 11 additions & 2 deletions src/Relay.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Facades\Cache;
use Prism\Prism\Contracts\Schema;
use Prism\Prism\Schema\AnyOfSchema;
use Prism\Prism\Schema\ArraySchema;
use Prism\Prism\Schema\BooleanSchema;
use Prism\Prism\Schema\EnumSchema;
Expand Down Expand Up @@ -326,7 +327,7 @@ protected function getSchemeParameters(array $properties, string $definitionName
$parameters = [];
foreach ($properties as $name => $property) {

$parameter = $this->getSchemeParameter($name, $property, $definitionName);
$parameter = $this->getSchemeParameter((string) $name, $property, $definitionName);
if ($parameter instanceof Schema) {
$parameters[] = $parameter;
}
Expand All @@ -340,14 +341,22 @@ protected function getSchemeParameters(array $properties, string $definitionName
*
* @throws RelayException
*/
protected function getSchemeParameter(string $name, array $property, string $definitionName): Schema|null
protected function getSchemeParameter(string $name, array $property, string $definitionName): ?Schema
{
if ($property === []) {
return null;
}

$type = data_get($property, 'type');
$description = $this->getParameterDescription($name, $property, $definitionName);

if ($type === null && isset($property['anyOf'])) {
$type = 'anyOf';
$itemsSchema = $this->getSchemeParameters(data_get($property, 'anyOf', []), $definitionName);

return new AnyOfSchema($itemsSchema, $name, $description);
}

$itemsSchema = $this->getSchemeParameter('', data_get($property, 'items', []), $definitionName);

return match ($type) {
Expand Down
22 changes: 22 additions & 0 deletions tests/TestDoubles/RelayFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ protected function fetchToolDefinitions(): array
'required' => ['script'],
],
],
[
'name' => 'union_tool',
'description' => 'A tool with union parameter',
'inputSchema' => [
'type' => 'object',
'properties' => [
'nameOrId' => [
'anyOf' => [
[
'type' => 'string',
'description' => 'Name',
],
[
'type' => 'number',
'description' => 'ID',
],
],
],
],
'required' => ['nameOrId'],
],
],
];
}

Expand Down
29 changes: 28 additions & 1 deletion tests/Unit/RelayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Unit;

use Illuminate\Support\Facades\Cache;
use Prism\Prism\Schema\AnyOfSchema;
use Prism\Prism\Tool;
use Prism\Relay\Exceptions\ServerConfigurationException;
use Prism\Relay\Exceptions\ToolDefinitionException;
Expand Down Expand Up @@ -86,7 +87,7 @@
$tools = $relay->tools();

// Test we have the tools we expect
expect($tools)->toHaveCount(6);
expect($tools)->toHaveCount(7);
});

it('handles different parameter types correctly in tools', function (): void {
Expand Down Expand Up @@ -128,3 +129,29 @@
expect($tools)->toBeArray()
->and($tools !== [])->toBeTrue();
});

it('supports mapping any of schemas', function (): void {
$relay = new RelayFake($this->serverName);

// Call tools() to create handlers
$tools = $relay->tools();

$tool = $tools[6];
$anyOf = $tool->parameters()['nameOrId'];

expect($anyOf)
->toBeInstanceOf(AnyOfSchema::class)
->and($anyOf->toArray())->toBe([
'anyOf' => [
[
'description' => 'Name',
'type' => 'string',
],
[
'description' => 'ID',
'type' => 'number',
],
],
'description' => 'Parameter nameOrId for union_tool',
]);
});
Loading