Skip to content

Commit 060a7cf

Browse files
patrickbrouwerssixlive
authored andcommitted
(feat) any of schema support
1 parent 874b858 commit 060a7cf

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

src/Relay.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Facades\Cache;
88
use Prism\Prism\Contracts\Schema;
9+
use Prism\Prism\Schema\AnyOfSchema;
910
use Prism\Prism\Schema\ArraySchema;
1011
use Prism\Prism\Schema\BooleanSchema;
1112
use Prism\Prism\Schema\EnumSchema;
@@ -326,7 +327,7 @@ protected function getSchemeParameters(array $properties, string $definitionName
326327
$parameters = [];
327328
foreach ($properties as $name => $property) {
328329

329-
$parameter = $this->getSchemeParameter($name, $property, $definitionName);
330+
$parameter = $this->getSchemeParameter((string) $name, $property, $definitionName);
330331
if ($parameter instanceof Schema) {
331332
$parameters[] = $parameter;
332333
}
@@ -348,6 +349,14 @@ protected function getSchemeParameter(string $name, array $property, string $def
348349

349350
$type = data_get($property, 'type');
350351
$description = $this->getParameterDescription($name, $property, $definitionName);
352+
353+
if ($type === null && isset($property['anyOf'])) {
354+
$type = 'anyOf';
355+
$itemsSchema = $this->getSchemeParameters(data_get($property, 'anyOf', []), $definitionName);
356+
357+
return new AnyOfSchema($itemsSchema, $name, $description);
358+
}
359+
351360
$itemsSchema = $this->getSchemeParameter('', data_get($property, 'items', []), $definitionName);
352361

353362
return match ($type) {

tests/TestDoubles/RelayFake.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ protected function fetchToolDefinitions(): array
182182
'required' => ['script'],
183183
],
184184
],
185+
[
186+
'name' => 'union_tool',
187+
'description' => 'A tool with union parameter',
188+
'inputSchema' => [
189+
'type' => 'object',
190+
'properties' => [
191+
'nameOrId' => [
192+
'anyOf' => [
193+
[
194+
'type' => 'string',
195+
'description' => 'Name',
196+
],
197+
[
198+
'type' => 'number',
199+
'description' => 'ID',
200+
],
201+
],
202+
],
203+
],
204+
'required' => ['nameOrId'],
205+
],
206+
],
185207
];
186208
}
187209

tests/Unit/RelayTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tests\Unit;
66

77
use Illuminate\Support\Facades\Cache;
8+
use Prism\Prism\Schema\AnyOfSchema;
89
use Prism\Prism\Tool;
910
use Prism\Relay\Exceptions\ServerConfigurationException;
1011
use Prism\Relay\Exceptions\ToolDefinitionException;
@@ -86,7 +87,7 @@
8687
$tools = $relay->tools();
8788

8889
// Test we have the tools we expect
89-
expect($tools)->toHaveCount(6);
90+
expect($tools)->toHaveCount(7);
9091
});
9192

9293
it('handles different parameter types correctly in tools', function (): void {
@@ -128,3 +129,29 @@
128129
expect($tools)->toBeArray()
129130
->and($tools !== [])->toBeTrue();
130131
});
132+
133+
it('supports mapping any of schemas', function (): void {
134+
$relay = new RelayFake($this->serverName);
135+
136+
// Call tools() to create handlers
137+
$tools = $relay->tools();
138+
139+
$tool = $tools[6];
140+
$anyOf = $tool->parameters()['nameOrId'];
141+
142+
expect($anyOf)
143+
->toBeInstanceOf(AnyOfSchema::class)
144+
->and($anyOf->toArray())->toBe([
145+
'anyOf' => [
146+
[
147+
'description' => 'Name',
148+
'type' => 'string',
149+
],
150+
[
151+
'description' => 'ID',
152+
'type' => 'number',
153+
],
154+
],
155+
'description' => 'Parameter nameOrId for union_tool',
156+
]);
157+
});

0 commit comments

Comments
 (0)