Skip to content

Commit a1f26a4

Browse files
committed
feature #739 [Platform] Make ExecutionReference properties private with getters (OskarStark)
This PR was merged into the main branch. Discussion ---------- [Platform] Make `ExecutionReference` properties private with getters | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | -- | License | MIT This change improves encapsulation by making the class and method properties private and providing public getter methods. Commits ------- ef6d7c7 [Platform][Tool] Make ExecutionReference properties private with getters
2 parents 09afb94 + ef6d7c7 commit a1f26a4

File tree

9 files changed

+79
-13
lines changed

9 files changed

+79
-13
lines changed

src/agent/src/Toolbox/Exception/ToolNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public static function notFoundForToolCall(ToolCall $toolCall): self
3131

3232
public static function notFoundForReference(ExecutionReference $reference): self
3333
{
34-
return new self(\sprintf('Tool not found for reference: %s::%s.', $reference->class, $reference->method));
34+
return new self(\sprintf('Tool not found for reference: %s::%s.', $reference->getClass(), $reference->getMethod()));
3535
}
3636
}

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->class, $metadata->reference->method);
46+
$method = new \ReflectionMethod($metadata->reference->getClass(), $metadata->reference->getMethod());
4747

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

src/agent/src/Toolbox/Toolbox.php

Lines changed: 3 additions & 2 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->method}(...$arguments);
85+
$result = $tool->{$metadata->reference->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));
@@ -109,8 +109,9 @@ private function getMetadata(ToolCall $toolCall): Tool
109109

110110
private function getExecutable(Tool $metadata): object
111111
{
112+
$className = $metadata->reference->getClass();
112113
foreach ($this->tools as $tool) {
113-
if ($tool instanceof $metadata->reference->class) {
114+
if ($tool instanceof $className) {
114115
return $tool;
115116
}
116117
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testTestGetMetadataOverwrite()
6767
$this->assertCount(1, $metadata);
6868
$this->assertSame('optional_param', $metadata[0]->name);
6969
$this->assertSame('Tool with optional param', $metadata[0]->description);
70-
$this->assertSame('bar', $metadata[0]->reference->method);
70+
$this->assertSame('bar', $metadata[0]->reference->getMethod());
7171
}
7272

7373
public function testTestGetMetadataWithAttributeDoubleHit()

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testGetMetadataWithDistinctToolPerClass()
4141
$this->assertInstanceOf(Tool::class, $metadata[0]);
4242
$this->assertSame('happy_birthday', $metadata[0]->name);
4343
$this->assertSame('Generates birthday message', $metadata[0]->description);
44-
$this->assertSame('__invoke', $metadata[0]->reference->method);
44+
$this->assertSame('__invoke', $metadata[0]->reference->getMethod());
4545

4646
$expectedParams = [
4747
'type' => 'object',
@@ -68,7 +68,7 @@ public function testGetMetadataWithMultipleToolsInClass()
6868
$this->assertInstanceOf(Tool::class, $metadata[0]);
6969
$this->assertSame('checkout', $metadata[0]->name);
7070
$this->assertSame('Buys a number of items per product', $metadata[0]->description);
71-
$this->assertSame('buy', $metadata[0]->reference->method);
71+
$this->assertSame('buy', $metadata[0]->reference->getMethod());
7272

7373
$expectedParams = [
7474
'type' => 'object',
@@ -84,7 +84,7 @@ public function testGetMetadataWithMultipleToolsInClass()
8484
$this->assertInstanceOf(Tool::class, $metadata[1]);
8585
$this->assertSame('cancel', $metadata[1]->name);
8686
$this->assertSame('Cancels an order', $metadata[1]->description);
87-
$this->assertSame('cancel', $metadata[1]->reference->method);
87+
$this->assertSame('cancel', $metadata[1]->reference->getMethod());
8888

8989
$expectedParams = [
9090
'type' => 'object',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ 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->class);
131-
$this->assertSame($method, $metadata->reference->method);
130+
$this->assertSame($className, $metadata->reference->getClass());
131+
$this->assertSame($method, $metadata->reference->getMethod());
132132
$this->assertSame($name, $metadata->name);
133133
$this->assertSame($description, $metadata->description);
134134
$this->assertSame($parameters, $metadata->parameters);

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->method);
40+
$method = $class->getMethod($event->metadata->reference->getMethod());
4141
$classAttributes = $class->getAttributes(IsGrantedTool::class);
4242
$methodAttributes = $method->getAttributes(IsGrantedTool::class);
4343

src/platform/src/Tool/ExecutionReference.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@
1717
final class ExecutionReference
1818
{
1919
public function __construct(
20-
public string $class,
21-
public string $method = '__invoke',
20+
private string $class,
21+
private string $method = '__invoke',
2222
) {
2323
}
24+
25+
public function getClass(): string
26+
{
27+
return $this->class;
28+
}
29+
30+
public function getMethod(): string
31+
{
32+
return $this->method;
33+
}
2434
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Tests\Tool;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\AI\Platform\Tool\ExecutionReference;
16+
17+
final class ExecutionReferenceTest extends TestCase
18+
{
19+
public function testGetClass()
20+
{
21+
$reference = new ExecutionReference('MyClass');
22+
23+
$this->assertSame('MyClass', $reference->getClass());
24+
}
25+
26+
public function testGetMethod()
27+
{
28+
$reference = new ExecutionReference('MyClass', 'myMethod');
29+
30+
$this->assertSame('myMethod', $reference->getMethod());
31+
}
32+
33+
public function testGetMethodReturnsDefaultInvokeMethod()
34+
{
35+
$reference = new ExecutionReference('MyClass');
36+
37+
$this->assertSame('__invoke', $reference->getMethod());
38+
}
39+
40+
public function testConstructorWithClassAndMethod()
41+
{
42+
$reference = new ExecutionReference('MyClass', 'execute');
43+
44+
$this->assertSame('MyClass', $reference->getClass());
45+
$this->assertSame('execute', $reference->getMethod());
46+
}
47+
48+
public function testConstructorWithOnlyClass()
49+
{
50+
$reference = new ExecutionReference('AnotherClass');
51+
52+
$this->assertSame('AnotherClass', $reference->getClass());
53+
$this->assertSame('__invoke', $reference->getMethod());
54+
}
55+
}

0 commit comments

Comments
 (0)