Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public static function notFoundForToolCall(ToolCall $toolCall): self

public static function notFoundForReference(ExecutionReference $reference): self
{
return new self(\sprintf('Tool not found for reference: %s::%s.', $reference->class, $reference->method));
return new self(\sprintf('Tool not found for reference: %s::%s.', $reference->getClass(), $reference->getMethod()));
}
}
2 changes: 1 addition & 1 deletion src/agent/src/Toolbox/ToolCallArgumentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
*/
public function resolveArguments(Tool $metadata, ToolCall $toolCall): array
{
$method = new \ReflectionMethod($metadata->reference->class, $metadata->reference->method);
$method = new \ReflectionMethod($metadata->reference->getClass(), $metadata->reference->getMethod());

/** @var array<string, \ReflectionParameter> $parameters */
$parameters = array_column($method->getParameters(), null, 'name');
Expand Down
5 changes: 3 additions & 2 deletions src/agent/src/Toolbox/Toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function execute(ToolCall $toolCall): mixed

$arguments = $this->argumentResolver->resolveArguments($metadata, $toolCall);
$this->eventDispatcher?->dispatch(new ToolCallArgumentsResolved($tool, $metadata, $arguments));
$result = $tool->{$metadata->reference->method}(...$arguments);
$result = $tool->{$metadata->reference->getMethod()}(...$arguments);
$this->eventDispatcher?->dispatch(new ToolCallSucceeded($tool, $metadata, $arguments, $result));
} catch (ToolExecutionExceptionInterface $e) {
$this->eventDispatcher?->dispatch(new ToolCallFailed($tool, $metadata, $arguments ?? [], $e));
Expand All @@ -109,8 +109,9 @@ private function getMetadata(ToolCall $toolCall): Tool

private function getExecutable(Tool $metadata): object
{
$className = $metadata->reference->getClass();
foreach ($this->tools as $tool) {
if ($tool instanceof $metadata->reference->class) {
if ($tool instanceof $className) {
return $tool;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testTestGetMetadataOverwrite()
$this->assertCount(1, $metadata);
$this->assertSame('optional_param', $metadata[0]->name);
$this->assertSame('Tool with optional param', $metadata[0]->description);
$this->assertSame('bar', $metadata[0]->reference->method);
$this->assertSame('bar', $metadata[0]->reference->getMethod());
}

public function testTestGetMetadataWithAttributeDoubleHit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testGetMetadataWithDistinctToolPerClass()
$this->assertInstanceOf(Tool::class, $metadata[0]);
$this->assertSame('happy_birthday', $metadata[0]->name);
$this->assertSame('Generates birthday message', $metadata[0]->description);
$this->assertSame('__invoke', $metadata[0]->reference->method);
$this->assertSame('__invoke', $metadata[0]->reference->getMethod());

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

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

$expectedParams = [
'type' => 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ className: ToolMultiple::class,

private function assertToolConfiguration(Tool $metadata, string $className, string $name, string $description, string $method, array $parameters): void
{
$this->assertSame($className, $metadata->reference->class);
$this->assertSame($method, $metadata->reference->method);
$this->assertSame($className, $metadata->reference->getClass());
$this->assertSame($method, $metadata->reference->getMethod());
$this->assertSame($name, $metadata->name);
$this->assertSame($description, $metadata->description);
$this->assertSame($parameters, $metadata->parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __invoke(ToolCallArgumentsResolved $event): void
{
$tool = $event->tool;
$class = new \ReflectionClass($tool);
$method = $class->getMethod($event->metadata->reference->method);
$method = $class->getMethod($event->metadata->reference->getMethod());
$classAttributes = $class->getAttributes(IsGrantedTool::class);
$methodAttributes = $method->getAttributes(IsGrantedTool::class);

Expand Down
14 changes: 12 additions & 2 deletions src/platform/src/Tool/ExecutionReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@
final class ExecutionReference
{
public function __construct(
public string $class,
public string $method = '__invoke',
private string $class,
private string $method = '__invoke',
) {
}

public function getClass(): string
{
return $this->class;
}

public function getMethod(): string
{
return $this->method;
}
}
55 changes: 55 additions & 0 deletions src/platform/tests/Tool/ExecutionReferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Tests\Tool;

use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Tool\ExecutionReference;

final class ExecutionReferenceTest extends TestCase
{
public function testGetClass()
{
$reference = new ExecutionReference('MyClass');

$this->assertSame('MyClass', $reference->getClass());
}

public function testGetMethod()
{
$reference = new ExecutionReference('MyClass', 'myMethod');

$this->assertSame('myMethod', $reference->getMethod());
}

public function testGetMethodReturnsDefaultInvokeMethod()
{
$reference = new ExecutionReference('MyClass');

$this->assertSame('__invoke', $reference->getMethod());
}

public function testConstructorWithClassAndMethod()
{
$reference = new ExecutionReference('MyClass', 'execute');

$this->assertSame('MyClass', $reference->getClass());
$this->assertSame('execute', $reference->getMethod());
}

public function testConstructorWithOnlyClass()
{
$reference = new ExecutionReference('AnotherClass');

$this->assertSame('AnotherClass', $reference->getClass());
$this->assertSame('__invoke', $reference->getMethod());
}
}
Loading