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
18 changes: 18 additions & 0 deletions src/agent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,24 @@ to skip the next LLM call by setting a result yourself::
}
});

Tool Call Lifecycle Events
~~~~~~~~~~~~~~~~~~~~~~~~~~

If you need to react more granularly to the lifecycle of individual tool calls, you can listen to the
``ToolCallArgumentsResolved``, ``ToolCallSucceeded`` and ``ToolCallFailed`` events. These are dispatched at different stages::

$eventDispatcher->addListener(ToolCallArgumentsResolved::class, function (ToolCallArgumentsResolved $event): void {
// Let the client know, that the tool $event->toolCall->name was executed
});

$eventDispatcher->addListener(ToolCallSucceeded::class, function (ToolCallSucceeded $event): void {
// Let the client know, that the tool $event->toolCall->name successfully returned the result $event->result
});

$eventDispatcher->addListener(ToolCallFailed::class, function (ToolCallFailed $event): void {
// Let the client know, that the tool $event->toolCall->name failed with the exception: $event->exception
});

Keeping Tool Messages
~~~~~~~~~~~~~~~~~~~~~

Expand Down
31 changes: 31 additions & 0 deletions src/agent/src/Toolbox/Event/ToolCallFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

namespace Symfony\AI\Agent\Toolbox\Event;

use Symfony\AI\Platform\Tool\Tool;

/**
* Dispatched after successfully invoking a tool.
*/
final readonly class ToolCallFailed
{
/**
* @param array<string, mixed> $arguments
*/
public function __construct(
public object $tool,
public Tool $metadata,
public array $arguments,
public \Throwable $exception,
) {
}
}
31 changes: 31 additions & 0 deletions src/agent/src/Toolbox/Event/ToolCallSucceeded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

namespace Symfony\AI\Agent\Toolbox\Event;

use Symfony\AI\Platform\Tool\Tool;

/**
* Dispatched after successfully invoking a tool.
*/
final readonly class ToolCallSucceeded
{
/**
* @param array<string, mixed> $arguments
*/
public function __construct(
public object $tool,
public Tool $metadata,
public array $arguments,
public mixed $result,
) {
}
}
6 changes: 5 additions & 1 deletion src/agent/src/Toolbox/Toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\AI\Agent\Toolbox\Event\ToolCallArgumentsResolved;
use Symfony\AI\Agent\Toolbox\Event\ToolCallFailed;
use Symfony\AI\Agent\Toolbox\Event\ToolCallSucceeded;
use Symfony\AI\Agent\Toolbox\Exception\ToolExecutionException;
use Symfony\AI\Agent\Toolbox\Exception\ToolExecutionExceptionInterface;
use Symfony\AI\Agent\Toolbox\Exception\ToolNotFoundException;
Expand Down Expand Up @@ -80,12 +82,14 @@ 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);
$this->eventDispatcher?->dispatch(new ToolCallSucceeded($tool, $metadata, $arguments, $result));
} catch (ToolExecutionExceptionInterface $e) {
$this->eventDispatcher?->dispatch(new ToolCallFailed($tool, $metadata, $arguments ?? [], $e));
throw $e;
} catch (\Throwable $e) {
$this->logger->warning(\sprintf('Failed to execute tool "%s".', $toolCall->name), ['exception' => $e]);
$this->eventDispatcher?->dispatch(new ToolCallFailed($tool, $metadata, $arguments ?? [], $e));
throw ToolExecutionException::executionFailed($toolCall, $e);
}

Expand Down
95 changes: 95 additions & 0 deletions src/agent/tests/Toolbox/ToolboxEventDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

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

namespace Symfony\AI\Agent\Tests\Toolbox;

use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Agent\Toolbox\Event\ToolCallArgumentsResolved;
use Symfony\AI\Agent\Toolbox\Event\ToolCallFailed;
use Symfony\AI\Agent\Toolbox\Event\ToolCallSucceeded;
use Symfony\AI\Agent\Toolbox\Toolbox;
use Symfony\AI\Fixtures\Tool\ToolCustomException;
use Symfony\AI\Fixtures\Tool\ToolException;
use Symfony\AI\Fixtures\Tool\ToolNoParams;
use Symfony\AI\Platform\Result\ToolCall;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final class ToolboxEventDispatcherTest extends TestCase
{
private Toolbox $toolbox;
private array $dispatchedEvents = [];

/**
* @throws Exception
*/
protected function setUp(): void
{
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher
->method('dispatch')
->willReturnCallback(function (object $event, ?string $eventName = null) {
$this->dispatchedEvents[] = $eventName ?? $event::class;

return $event;
});
$this->toolbox = new Toolbox([
new ToolNoParams(),
new ToolException(),
new ToolCustomException(),
], eventDispatcher: $dispatcher);
}

public function testExecuteWithUnknownTool()
{
try {
$this->toolbox->execute(new ToolCall('call_1234', 'foo_bar_baz'));
} catch (\Throwable) {
}
$this->assertEmpty($this->dispatchedEvents);
}

public function testExecuteWithToolExecutionException()
{
try {
$this->toolbox->execute(new ToolCall('call_1234', 'tool_exception'));
} catch (\Throwable) {
}
$this->assertEquals([
ToolCallArgumentsResolved::class,
ToolCallFailed::class,
], $this->dispatchedEvents);
}

public function testExecuteWithCustomExecutionException()
{
try {
$this->toolbox->execute(new ToolCall('call_1234', 'tool_custom_exception'));
} catch (\Throwable) {
}
$this->assertEquals([
ToolCallArgumentsResolved::class,
ToolCallFailed::class,
], $this->dispatchedEvents);
}

public function testExecuteSuccess()
{
try {
$this->toolbox->execute(new ToolCall('call_1234', 'tool_no_params'));
} catch (\Throwable) {
}
$this->assertEquals([
ToolCallArgumentsResolved::class,
ToolCallSucceeded::class,
], $this->dispatchedEvents);
}
}
Loading