Skip to content

Commit 02f20b4

Browse files
committed
feature #738 [Platform] Make Tool properties private with getters (OskarStark)
This PR was squashed before being merged into the main branch. Discussion ---------- [Platform] Make `Tool` properties private with getters | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | -- | License | MIT Commits ------- 4767102 [Platform] Make `Tool` properties private with getters
2 parents 7e666ea + 4767102 commit 02f20b4

File tree

17 files changed

+144
-51
lines changed

17 files changed

+144
-51
lines changed

src/agent/src/InputProcessor/SystemPromptInputProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function processInput(Input $input): void
6868

6969
$tools = implode(\PHP_EOL.\PHP_EOL, array_map(
7070
fn (Tool $tool) => <<<TOOL
71-
## {$tool->name}
72-
{$tool->description}
71+
## {$tool->getName()}
72+
{$tool->getDescription()}
7373
TOOL,
7474
$this->toolbox->getTools()
7575
));

src/agent/src/Toolbox/AgentProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function processInput(Input $input): void
5252
$options = $input->getOptions();
5353
// only filter tool map if list of strings is provided as option
5454
if (isset($options['tools']) && $this->isFlatStringArray($options['tools'])) {
55-
$toolMap = array_values(array_filter($toolMap, fn (Tool $tool) => \in_array($tool->name, $options['tools'], true)));
55+
$toolMap = array_values(array_filter($toolMap, fn (Tool $tool) => \in_array($tool->getName(), $options['tools'], true)));
5656
}
5757

5858
$options['tools'] = $toolMap;

src/agent/src/Toolbox/FaultTolerantToolbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function execute(ToolCall $toolCall): mixed
4040
} catch (ToolExecutionExceptionInterface $e) {
4141
return $e->getToolCallResult();
4242
} catch (ToolNotFoundException) {
43-
$names = array_map(fn (Tool $metadata) => $metadata->name, $this->getTools());
43+
$names = array_map(fn (Tool $metadata) => $metadata->getName(), $this->getTools());
4444

4545
return \sprintf('Tool "%s" was not found, please use one of these: %s', $toolCall->getName(), implode(', ', $names));
4646
}

src/agent/src/Toolbox/ToolCallArgumentResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343
*/
4444
public function resolveArguments(Tool $metadata, ToolCall $toolCall): array
4545
{
46-
$method = new \ReflectionMethod($metadata->reference->getClass(), $metadata->reference->getMethod());
46+
$method = new \ReflectionMethod($metadata->getReference()->getClass(), $metadata->getReference()->getMethod());
4747

4848
/** @var array<string, \ReflectionParameter> $parameters */
4949
$parameters = array_column($method->getParameters(), null, 'name');

src/agent/src/Toolbox/Toolbox.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function execute(ToolCall $toolCall): mixed
8282

8383
$arguments = $this->argumentResolver->resolveArguments($metadata, $toolCall);
8484
$this->eventDispatcher?->dispatch(new ToolCallArgumentsResolved($tool, $metadata, $arguments));
85-
$result = $tool->{$metadata->reference->getMethod()}(...$arguments);
85+
$result = $tool->{$metadata->getReference()->getMethod()}(...$arguments);
8686
$this->eventDispatcher?->dispatch(new ToolCallSucceeded($tool, $metadata, $arguments, $result));
8787
} catch (ToolExecutionExceptionInterface $e) {
8888
$this->eventDispatcher?->dispatch(new ToolCallFailed($tool, $metadata, $arguments ?? [], $e));
@@ -99,7 +99,7 @@ public function execute(ToolCall $toolCall): mixed
9999
private function getMetadata(ToolCall $toolCall): Tool
100100
{
101101
foreach ($this->getTools() as $metadata) {
102-
if ($metadata->name === $toolCall->getName()) {
102+
if ($metadata->getName() === $toolCall->getName()) {
103103
return $metadata;
104104
}
105105
}
@@ -109,13 +109,13 @@ private function getMetadata(ToolCall $toolCall): Tool
109109

110110
private function getExecutable(Tool $metadata): object
111111
{
112-
$className = $metadata->reference->getClass();
112+
$className = $metadata->getReference()->getClass();
113113
foreach ($this->tools as $tool) {
114114
if ($tool instanceof $className) {
115115
return $tool;
116116
}
117117
}
118118

119-
throw ToolNotFoundException::notFoundForReference($metadata->reference);
119+
throw ToolNotFoundException::notFoundForReference($metadata->getReference());
120120
}
121121
}

src/agent/tests/Toolbox/MetadataFactory/ChainFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public function testTestGetMetadataOverwrite()
6565
$metadata = iterator_to_array($this->factory->getTool(ToolOptionalParam::class));
6666

6767
$this->assertCount(1, $metadata);
68-
$this->assertSame('optional_param', $metadata[0]->name);
69-
$this->assertSame('Tool with optional param', $metadata[0]->description);
70-
$this->assertSame('bar', $metadata[0]->reference->getMethod());
68+
$this->assertSame('optional_param', $metadata[0]->getName());
69+
$this->assertSame('Tool with optional param', $metadata[0]->getDescription());
70+
$this->assertSame('bar', $metadata[0]->getReference()->getMethod());
7171
}
7272

7373
public function testTestGetMetadataWithAttributeDoubleHit()

src/agent/tests/Toolbox/MetadataFactory/MemoryFactoryTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public function testGetMetadataWithDistinctToolPerClass()
3939

4040
$this->assertCount(1, $metadata);
4141
$this->assertInstanceOf(Tool::class, $metadata[0]);
42-
$this->assertSame('happy_birthday', $metadata[0]->name);
43-
$this->assertSame('Generates birthday message', $metadata[0]->description);
44-
$this->assertSame('__invoke', $metadata[0]->reference->getMethod());
42+
$this->assertSame('happy_birthday', $metadata[0]->getName());
43+
$this->assertSame('Generates birthday message', $metadata[0]->getDescription());
44+
$this->assertSame('__invoke', $metadata[0]->getReference()->getMethod());
4545

4646
$expectedParams = [
4747
'type' => 'object',
@@ -53,7 +53,7 @@ public function testGetMetadataWithDistinctToolPerClass()
5353
'additionalProperties' => false,
5454
];
5555

56-
$this->assertSame($expectedParams, $metadata[0]->parameters);
56+
$this->assertSame($expectedParams, $metadata[0]->getParameters());
5757
}
5858

5959
public function testGetMetadataWithMultipleToolsInClass()
@@ -66,9 +66,9 @@ public function testGetMetadataWithMultipleToolsInClass()
6666

6767
$this->assertCount(2, $metadata);
6868
$this->assertInstanceOf(Tool::class, $metadata[0]);
69-
$this->assertSame('checkout', $metadata[0]->name);
70-
$this->assertSame('Buys a number of items per product', $metadata[0]->description);
71-
$this->assertSame('buy', $metadata[0]->reference->getMethod());
69+
$this->assertSame('checkout', $metadata[0]->getName());
70+
$this->assertSame('Buys a number of items per product', $metadata[0]->getDescription());
71+
$this->assertSame('buy', $metadata[0]->getReference()->getMethod());
7272

7373
$expectedParams = [
7474
'type' => 'object',
@@ -79,12 +79,12 @@ public function testGetMetadataWithMultipleToolsInClass()
7979
'required' => ['id', 'amount'],
8080
'additionalProperties' => false,
8181
];
82-
$this->assertSame($expectedParams, $metadata[0]->parameters);
82+
$this->assertSame($expectedParams, $metadata[0]->getParameters());
8383

8484
$this->assertInstanceOf(Tool::class, $metadata[1]);
85-
$this->assertSame('cancel', $metadata[1]->name);
86-
$this->assertSame('Cancels an order', $metadata[1]->description);
87-
$this->assertSame('cancel', $metadata[1]->reference->getMethod());
85+
$this->assertSame('cancel', $metadata[1]->getName());
86+
$this->assertSame('Cancels an order', $metadata[1]->getDescription());
87+
$this->assertSame('cancel', $metadata[1]->getReference()->getMethod());
8888

8989
$expectedParams = [
9090
'type' => 'object',
@@ -94,6 +94,6 @@ public function testGetMetadataWithMultipleToolsInClass()
9494
'required' => ['orderId'],
9595
'additionalProperties' => false,
9696
];
97-
$this->assertSame($expectedParams, $metadata[1]->parameters);
97+
$this->assertSame($expectedParams, $metadata[1]->getParameters());
9898
}
9999
}

src/agent/tests/Toolbox/MetadataFactory/ReflectionFactoryTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ className: ToolMultiple::class,
127127

128128
private function assertToolConfiguration(Tool $metadata, string $className, string $name, string $description, string $method, array $parameters): void
129129
{
130-
$this->assertSame($className, $metadata->reference->getClass());
131-
$this->assertSame($method, $metadata->reference->getMethod());
132-
$this->assertSame($name, $metadata->name);
133-
$this->assertSame($description, $metadata->description);
134-
$this->assertSame($parameters, $metadata->parameters);
130+
$this->assertSame($className, $metadata->getReference()->getClass());
131+
$this->assertSame($method, $metadata->getReference()->getMethod());
132+
$this->assertSame($name, $metadata->getName());
133+
$this->assertSame($description, $metadata->getDescription());
134+
$this->assertSame($parameters, $metadata->getParameters());
135135
}
136136
}

src/ai-bundle/src/Security/EventListener/IsGrantedToolAttributeListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __invoke(ToolCallArgumentsResolved $event): void
3737
{
3838
$tool = $event->tool;
3939
$class = new \ReflectionClass($tool);
40-
$method = $class->getMethod($event->metadata->reference->getMethod());
40+
$method = $class->getMethod($event->metadata->getReference()->getMethod());
4141
$classAttributes = $class->getAttributes(IsGrantedTool::class);
4242
$methodAttributes = $method->getAttributes(IsGrantedTool::class);
4343

src/ai-bundle/tests/Security/IsGrantedToolAttributeListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testItWillThrowWhenNotGranted(object $tool, Tool $metadata)
3939
$this->authChecker->expects($this->once())->method('isGranted')->willReturn(false);
4040

4141
$this->expectException(AccessDeniedException::class);
42-
$this->expectExceptionMessage(\sprintf('No access to %s tool.', $metadata->name));
42+
$this->expectExceptionMessage(\sprintf('No access to %s tool.', $metadata->getName()));
4343
$this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, []));
4444
}
4545

0 commit comments

Comments
 (0)