Skip to content

Commit aab79a2

Browse files
committed
improv: Better default, preserve tool msgs
1 parent 0f11b4f commit aab79a2

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

docs/components/agent.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ If you need to react more granularly to the lifecycle of individual tool calls,
405405
// Let the client know, that the tool $event->toolCall->name failed with the exception: $event->exception
406406
});
407407

408-
Keeping Tool Messages
409-
~~~~~~~~~~~~~~~~~~~~~
408+
Excluding Tool Messages from :class:`Symfony\\AI\\Platform\\Message\\MessageBag`
409+
~~~~~~~~~~~~~~~~~~~~------------------------------------------------------------
410410

411-
Sometimes you might wish to keep the tool messages (:class:`Symfony\\AI\\Platform\\Message\\AssistantMessage` containing the ``toolCalls`` and :class:`Symfony\\AI\\Platform\\Message\\ToolCallMessage`
412-
containing the result) in the context. Enable the ``keepToolMessages`` flag of the toolbox' :class:`Symfony\\AI\\Agent\\Toolbox\\AgentProcessor`
413-
to ensure those messages will be added to your :class:`Symfony\\AI\\Platform\\Message\\MessageBag`::
411+
Sometimes you might wish to exclude the tool messages (:class:`Symfony\\AI\\Platform\\Message\\AssistantMessage` containing the ``toolCalls`` and :class:`Symfony\\AI\\Platform\\Message\\ToolCallMessage`
412+
containing the result) in the context. Enable the ``excludeToolMessages`` flag of the toolbox' :class:`Symfony\\AI\\Agent\\Toolbox\\AgentProcessor`
413+
to ensure those messages will be removed from your :class:`Symfony\\AI\\Platform\\Message\\MessageBag`::
414414

415415
use Symfony\AI\Agent\Toolbox\AgentProcessor;
416416
use Symfony\AI\Agent\Toolbox\Toolbox;
@@ -428,11 +428,11 @@ to ensure those messages will be added to your :class:`Symfony\\AI\\Platform\\Me
428428
$tool = new MyTool();
429429

430430
$toolbox = new Toolbox([$tool]);
431-
$toolProcessor = new AgentProcessor($toolbox, keepToolMessages: true);
431+
$toolProcessor = new AgentProcessor($toolbox, excludeToolMessages: true);
432432

433433
$agent = new Agent($platform, $model, inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
434434
$result = $agent->call($messages);
435-
// $messages will now include the tool messages
435+
// $messages will now exclude the tool messages
436436

437437
Code Examples (with built-in tools)
438438
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/agent/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* [BC BREAK] Rename `Symfony\AI\Agent\Toolbox\Tool\Agent` to `Symfony\AI\Agent\Toolbox\Tool\Subagent`
88
* Add `MetaDataAwareTrait` to `MockResponse`, the metadata will also be set on the returned `TextResult` when calling the `toResult` function
9+
* [BC Break] Refactor Agent Processor `keepToolMessages` to `excludeToolMessages` and change default behaviour to preserve tool messages.
910

1011
0.3
1112
---

src/agent/src/Toolbox/AgentProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct(
5050
private readonly ToolboxInterface $toolbox,
5151
private readonly ToolResultConverter $resultConverter = new ToolResultConverter(),
5252
private readonly ?EventDispatcherInterface $eventDispatcher = null,
53-
private readonly bool $keepToolMessages = false,
53+
private readonly bool $excludeToolMessages = false,
5454
private readonly bool $includeSources = false,
5555
private readonly ?int $maxToolCalls = null,
5656
) {
@@ -104,7 +104,7 @@ private function isFlatStringArray(array $tools): bool
104104
private function handleToolCallsCallback(Output $output, ToolCallResult $result, ?AssistantMessage $streamedAssistantResponse = null): ResultInterface
105105
{
106106
++$this->nestingLevel;
107-
$messages = $this->keepToolMessages ? $output->getMessageBag() : clone $output->getMessageBag();
107+
$messages = $this->excludeToolMessages ? clone $output->getMessageBag() : $output->getMessageBag();
108108

109109
if (null !== $streamedAssistantResponse && '' !== $streamedAssistantResponse->getContent()) {
110110
$messages->add($streamedAssistantResponse);

src/agent/tests/Toolbox/AgentProcessorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function testProcessOutputWithToolCallResponseKeepingMessages()
9797

9898
$agent = $this->createStub(AgentInterface::class);
9999

100-
$processor = new AgentProcessor($toolbox, keepToolMessages: true);
100+
$processor = new AgentProcessor($toolbox, excludeToolMessages: false);
101101
$processor->setAgent($agent);
102102

103103
$output = new Output('gpt-4', $result, $messageBag);
@@ -123,7 +123,7 @@ public function testProcessOutputWithToolCallResponseForgettingMessages()
123123

124124
$agent = $this->createStub(AgentInterface::class);
125125

126-
$processor = new AgentProcessor($toolbox, keepToolMessages: false);
126+
$processor = new AgentProcessor($toolbox, excludeToolMessages: true);
127127
$processor->setAgent($agent);
128128

129129
$output = new Output('gpt-4', $result, $messageBag);

0 commit comments

Comments
 (0)