Skip to content

Commit 656186b

Browse files
committed
Use ToolResult already in ToolboxInterface
1 parent 52e1d5a commit 656186b

File tree

12 files changed

+60
-57
lines changed

12 files changed

+60
-57
lines changed

src/agent/src/Toolbox/AgentProcessor.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ private function handleToolCallsCallback(Output $output): \Closure
9999

100100
$results = [];
101101
foreach ($toolCalls as $toolCall) {
102-
$result = $this->toolbox->execute($toolCall);
103-
$results[] = new ToolResult($toolCall, $result);
104-
$messages->add(Message::ofToolCall($toolCall, $this->resultConverter->convert($result)));
102+
$results[] = $toolResult = $this->toolbox->execute($toolCall);
103+
$messages->add(Message::ofToolCall($toolCall, $this->resultConverter->convert($toolResult)));
105104
}
106105

107106
$event = new ToolCallsExecuted(...$results);

src/agent/src/Toolbox/FaultTolerantToolbox.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@ public function getTools(): array
3333
return $this->innerToolbox->getTools();
3434
}
3535

36-
public function execute(ToolCall $toolCall): mixed
36+
public function execute(ToolCall $toolCall): ToolResult
3737
{
3838
try {
3939
return $this->innerToolbox->execute($toolCall);
4040
} catch (ToolExecutionExceptionInterface $e) {
41-
return $e->getToolCallResult();
41+
return new ToolResult($toolCall, $e->getToolCallResult());
4242
} catch (ToolNotFoundException) {
4343
$names = array_map(fn (Tool $metadata) => $metadata->getName(), $this->getTools());
4444

45-
return \sprintf('Tool "%s" was not found, please use one of these: %s', $toolCall->getName(), implode(', ', $names));
45+
return new ToolResult(
46+
$toolCall,
47+
\sprintf('Tool "%s" was not found, please use one of these: %s', $toolCall->getName(), implode(', ', $names))
48+
);
4649
}
4750
}
4851
}

src/agent/src/Toolbox/Toolbox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getTools(): array
7272
return $this->map = $map;
7373
}
7474

75-
public function execute(ToolCall $toolCall): mixed
75+
public function execute(ToolCall $toolCall): ToolResult
7676
{
7777
$metadata = $this->getMetadata($toolCall);
7878
$tool = $this->getExecutable($metadata);
@@ -93,7 +93,7 @@ public function execute(ToolCall $toolCall): mixed
9393
throw ToolExecutionException::executionFailed($toolCall, $e);
9494
}
9595

96-
return $result;
96+
return new ToolResult($toolCall, $result);
9797
}
9898

9999
private function getMetadata(ToolCall $toolCall): Tool

src/agent/src/Toolbox/ToolboxInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ public function getTools(): array;
3030
* @throws ToolExecutionExceptionInterface if the tool execution fails
3131
* @throws ToolNotFoundException if the tool is not found
3232
*/
33-
public function execute(ToolCall $toolCall): mixed;
33+
public function execute(ToolCall $toolCall): ToolResult;
3434
}

src/agent/tests/InputProcessor/SystemPromptInputProcessorTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\AI\Agent\Input;
1616
use Symfony\AI\Agent\InputProcessor\SystemPromptInputProcessor;
1717
use Symfony\AI\Agent\Toolbox\ToolboxInterface;
18+
use Symfony\AI\Agent\Toolbox\ToolResult;
1819
use Symfony\AI\Fixtures\Tool\ToolNoParams;
1920
use Symfony\AI\Fixtures\Tool\ToolRequiredParams;
2021
use Symfony\AI\Platform\Message\Content\File;
@@ -72,9 +73,9 @@ public function getTools(): array
7273
return [];
7374
}
7475

75-
public function execute(ToolCall $toolCall): mixed
76+
public function execute(ToolCall $toolCall): ToolResult
7677
{
77-
return null;
78+
return new ToolResult($toolCall, null);
7879
}
7980
},
8081
);
@@ -110,9 +111,9 @@ public function getTools(): array
110111
];
111112
}
112113

113-
public function execute(ToolCall $toolCall): mixed
114+
public function execute(ToolCall $toolCall): ToolResult
114115
{
115-
return null;
116+
return new ToolResult($toolCall, null);
116117
}
117118
},
118119
$this->getTranslator(),
@@ -153,9 +154,9 @@ public function getTools(): array
153154
];
154155
}
155156

156-
public function execute(ToolCall $toolCall): mixed
157+
public function execute(ToolCall $toolCall): ToolResult
157158
{
158-
return null;
159+
return new ToolResult($toolCall, null);
159160
}
160161
},
161162
);

src/agent/tests/Toolbox/AgentProcessorTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\AI\Agent\Output;
1818
use Symfony\AI\Agent\Toolbox\AgentProcessor;
1919
use Symfony\AI\Agent\Toolbox\ToolboxInterface;
20+
use Symfony\AI\Agent\Toolbox\ToolResult;
2021
use Symfony\AI\Platform\Message\AssistantMessage;
2122
use Symfony\AI\Platform\Message\MessageBag;
2223
use Symfony\AI\Platform\Message\ToolCallMessage;
@@ -72,12 +73,15 @@ public function testProcessInputWithRegisteredToolsButToolOverride()
7273

7374
public function testProcessOutputWithToolCallResponseKeepingMessages()
7475
{
76+
$toolCall = new ToolCall('id1', 'tool1', ['arg1' => 'value1']);
7577
$toolbox = $this->createMock(ToolboxInterface::class);
76-
$toolbox->expects($this->once())->method('execute')->willReturn('Test response');
78+
$toolbox
79+
->expects($this->once())
80+
->method('execute')
81+
->willReturn(new ToolResult($toolCall, 'Test response'));
7782

7883
$messageBag = new MessageBag();
79-
80-
$result = new ToolCallResult(new ToolCall('id1', 'tool1', ['arg1' => 'value1']));
84+
$result = new ToolCallResult($toolCall);
8185

8286
$agent = $this->createStub(AgentInterface::class);
8387

@@ -95,12 +99,15 @@ public function testProcessOutputWithToolCallResponseKeepingMessages()
9599

96100
public function testProcessOutputWithToolCallResponseForgettingMessages()
97101
{
102+
$toolCall = new ToolCall('id1', 'tool1', ['arg1' => 'value1']);
98103
$toolbox = $this->createMock(ToolboxInterface::class);
99-
$toolbox->expects($this->once())->method('execute')->willReturn('Test response');
104+
$toolbox
105+
->expects($this->once())
106+
->method('execute')
107+
->willReturn(new ToolResult($toolCall, 'Test response'));
100108

101109
$messageBag = new MessageBag();
102-
103-
$result = new ToolCallResult(new ToolCall('id1', 'tool1', ['arg1' => 'value1']));
110+
$result = new ToolCallResult($toolCall);
104111

105112
$agent = $this->createStub(AgentInterface::class);
106113

src/agent/tests/Toolbox/FaultTolerantToolboxTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\AI\Agent\Toolbox\Exception\ToolNotFoundException;
1818
use Symfony\AI\Agent\Toolbox\FaultTolerantToolbox;
1919
use Symfony\AI\Agent\Toolbox\ToolboxInterface;
20+
use Symfony\AI\Agent\Toolbox\ToolResult;
2021
use Symfony\AI\Fixtures\Tool\ToolNoParams;
2122
use Symfony\AI\Fixtures\Tool\ToolRequiredParams;
2223
use Symfony\AI\Platform\Result\ToolCall;
@@ -37,7 +38,7 @@ public function testFaultyToolExecution()
3738
$toolCall = new ToolCall('987654321', 'tool_foo');
3839
$actual = $faultTolerantToolbox->execute($toolCall);
3940

40-
$this->assertSame($expected, $actual);
41+
$this->assertSame($expected, $actual->getResult());
4142
}
4243

4344
public function testFaultyToolCall()
@@ -52,7 +53,7 @@ public function testFaultyToolCall()
5253
$toolCall = new ToolCall('123456789', 'tool_xyz');
5354
$actual = $faultTolerantToolbox->execute($toolCall);
5455

55-
$this->assertSame($expected, $actual);
56+
$this->assertSame($expected, $actual->getResult());
5657
}
5758

5859
public function testCustomToolExecutionException()
@@ -72,7 +73,7 @@ public function getToolCallResult(): array
7273
$toolCall = new ToolCall('123456789', 'tool_xyz');
7374
$actual = $faultTolerantToolbox->execute($toolCall);
7475

75-
$this->assertSame($expected, $actual);
76+
$this->assertSame($expected, $actual->getResult());
7677
}
7778

7879
private function createFaultyToolbox(\Closure $exceptionFactory): ToolboxInterface
@@ -93,7 +94,7 @@ public function getTools(): array
9394
];
9495
}
9596

96-
public function execute(ToolCall $toolCall): mixed
97+
public function execute(ToolCall $toolCall): ToolResult
9798
{
9899
throw ($this->exceptionFactory)($toolCall);
99100
}

src/agent/tests/Toolbox/ToolboxTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\AI\Agent\Toolbox\ToolFactory\ChainFactory;
2222
use Symfony\AI\Agent\Toolbox\ToolFactory\MemoryToolFactory;
2323
use Symfony\AI\Agent\Toolbox\ToolFactory\ReflectionToolFactory;
24+
use Symfony\AI\Agent\Toolbox\ToolResult;
2425
use Symfony\AI\Fixtures\Tool\ToolCustomException;
2526
use Symfony\AI\Fixtures\Tool\ToolDate;
2627
use Symfony\AI\Fixtures\Tool\ToolException;
@@ -180,9 +181,11 @@ public function testExecuteWithCustomException()
180181
#[DataProvider('executeProvider')]
181182
public function testExecute(string $expected, string $toolName, array $toolPayload = [])
182183
{
183-
$this->assertSame(
184-
$expected,
185-
$this->toolbox->execute(new ToolCall('call_1234', $toolName, $toolPayload)),
184+
$toolCall = new ToolCall('call_1234', $toolName, $toolPayload);
185+
186+
$this->assertEquals(
187+
new ToolResult($toolCall, $expected),
188+
$this->toolbox->execute($toolCall),
186189
);
187190
}
188191

@@ -244,7 +247,7 @@ public function testToolboxExecutionWithMemoryFactory()
244247
$toolbox = new Toolbox([new ToolNoAttribute1()], $memoryFactory);
245248
$result = $toolbox->execute(new ToolCall('call_1234', 'happy_birthday', ['name' => 'John', 'years' => 30]));
246249

247-
$this->assertSame('Happy Birthday, John! You are 30 years old.', $result);
250+
$this->assertSame('Happy Birthday, John! You are 30 years old.', $result->getResult());
248251
}
249252

250253
public function testToolboxMapWithOverrideViaChain()

src/ai-bundle/src/Profiler/DataCollector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\AI\AiBundle\Profiler;
1313

1414
use Symfony\AI\Agent\Toolbox\ToolboxInterface;
15-
use Symfony\AI\Platform\Model;
15+
use Symfony\AI\Agent\Toolbox\ToolResult;
1616
use Symfony\AI\Platform\Tool\Tool;
1717
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
1818
use Symfony\Component\HttpFoundation\Request;
@@ -23,7 +23,6 @@
2323
* @author Christopher Hertel <mail@christopher-hertel.de>
2424
*
2525
* @phpstan-import-type PlatformCallData from TraceablePlatform
26-
* @phpstan-import-type ToolCallData from TraceableToolbox
2726
*/
2827
final class DataCollector extends AbstractDataCollector implements LateDataCollectorInterface
2928
{
@@ -86,7 +85,7 @@ public function getTools(): array
8685
}
8786

8887
/**
89-
* @return ToolCallData[]
88+
* @return ToolResult[]
9089
*/
9190
public function getToolCalls(): array
9291
{

src/ai-bundle/src/Profiler/TraceableToolbox.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,16 @@
1212
namespace Symfony\AI\AiBundle\Profiler;
1313

1414
use Symfony\AI\Agent\Toolbox\ToolboxInterface;
15+
use Symfony\AI\Agent\Toolbox\ToolResult;
1516
use Symfony\AI\Platform\Result\ToolCall;
1617

1718
/**
1819
* @author Christopher Hertel <mail@christopher-hertel.de>
19-
*
20-
* @phpstan-type ToolCallData array{
21-
* call: ToolCall,
22-
* result: string,
23-
* }
2420
*/
2521
final class TraceableToolbox implements ToolboxInterface
2622
{
2723
/**
28-
* @var ToolCallData[]
24+
* @var ToolResult[]
2925
*/
3026
public array $calls = [];
3127

@@ -39,15 +35,8 @@ public function getTools(): array
3935
return $this->toolbox->getTools();
4036
}
4137

42-
public function execute(ToolCall $toolCall): mixed
38+
public function execute(ToolCall $toolCall): ToolResult
4339
{
44-
$result = $this->toolbox->execute($toolCall);
45-
46-
$this->calls[] = [
47-
'call' => $toolCall,
48-
'result' => $result,
49-
];
50-
51-
return $result;
40+
return $this->calls[] = $this->toolbox->execute($toolCall);
5241
}
5342
}

0 commit comments

Comments
 (0)