Skip to content

Commit 5c3209a

Browse files
committed
[AiBundle] Register fault tolerant default toolbox
1 parent 29b2ca6 commit 5c3209a

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/ai-bundle/src/AiBundle.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,9 @@ private function processAgentConfig(string $name, array $config, ContainerBuilde
356356
$container->setDefinition('ai.toolbox.'.$name, $toolboxDefinition);
357357

358358
if ($config['fault_tolerant_toolbox']) {
359-
$faultTolerantToolboxDefinition = (new Definition('ai.fault_tolerant_toolbox.'.$name))
360-
->setClass(FaultTolerantToolbox::class)
359+
$container->setDefinition('ai.fault_tolerant_toolbox.'.$name, new Definition(FaultTolerantToolbox::class))
361360
->setArguments([new Reference('.inner')])
362361
->setDecoratedService('ai.toolbox.'.$name);
363-
$container->setDefinition('ai.fault_tolerant_toolbox.'.$name, $faultTolerantToolboxDefinition);
364362
}
365363

366364
if ($container->getParameter('kernel.debug')) {
@@ -379,6 +377,12 @@ private function processAgentConfig(string $name, array $config, ContainerBuilde
379377
$inputProcessors[] = new Reference('ai.tool.agent_processor.'.$name);
380378
$outputProcessors[] = new Reference('ai.tool.agent_processor.'.$name);
381379
} else {
380+
if ($config['fault_tolerant_toolbox'] && !$container->hasDefinition('ai.fault_tolerant_toolbox')) {
381+
$container->setDefinition('ai.fault_tolerant_toolbox', new Definition(FaultTolerantToolbox::class))
382+
->setArguments([new Reference('.inner')])
383+
->setDecoratedService('ai.toolbox');
384+
}
385+
382386
$inputProcessors[] = new Reference('ai.tool.agent_processor');
383387
$outputProcessors[] = new Reference('ai.tool.agent_processor');
384388
}

src/ai-bundle/tests/DependencyInjection/AiBundleTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\Attributes\CoversClass;
1515
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
16+
use PHPUnit\Framework\Attributes\TestWith;
1617
use PHPUnit\Framework\Attributes\UsesClass;
1718
use PHPUnit\Framework\TestCase;
1819
use Symfony\AI\AiBundle\AiBundle;
@@ -29,6 +30,46 @@ public function testExtensionLoadDoesNotThrow()
2930
$this->buildContainer($this->getFullConfig());
3031
}
3132

33+
#[TestWith([true], 'enabled')]
34+
#[TestWith([false], 'disabled')]
35+
public function testFaultTolerantAgentSpecificToolbox(bool $enabled)
36+
{
37+
$container = $this->buildContainer([
38+
'ai' => [
39+
'agent' => [
40+
'my_agent' => [
41+
'model' => ['class' => 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'],
42+
'tools' => [
43+
['service' => 'some_service', 'description' => 'Some tool'],
44+
],
45+
'fault_tolerant_toolbox' => $enabled,
46+
],
47+
],
48+
],
49+
]);
50+
51+
$this->assertSame($enabled, $container->hasDefinition('ai.fault_tolerant_toolbox.my_agent'));
52+
}
53+
54+
#[TestWith([true], 'enabled')]
55+
#[TestWith([false], 'disabled')]
56+
public function testFaultTolerantDefaultToolbox(bool $enabled)
57+
{
58+
$container = $this->buildContainer([
59+
'ai' => [
60+
'agent' => [
61+
'my_agent' => [
62+
'model' => ['class' => 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'],
63+
'tools' => true,
64+
'fault_tolerant_toolbox' => $enabled,
65+
],
66+
],
67+
],
68+
]);
69+
70+
$this->assertSame($enabled, $container->hasDefinition('ai.fault_tolerant_toolbox'));
71+
}
72+
3273
public function testAgentsCanBeRegisteredAsTools()
3374
{
3475
$container = $this->buildContainer([

0 commit comments

Comments
 (0)