From 8622ef2264cca3a6f862151502b02eecd985059a Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Thu, 11 Sep 2025 07:32:41 +0200 Subject: [PATCH] [AiBundle] Set agent name from configuration key Ensure agents have their name properly set from the configuration key. The Agent constructor's name parameter (argument 4) and the ai.agent tag both use the configuration key as the agent name. This allows Agent.getName() to return the configuration key and enables proper service discovery through tagged locators (e.g., ChatCommand). --- src/ai-bundle/src/AiBundle.php | 3 ++- .../DependencyInjection/AiBundleTest.php | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ai-bundle/src/AiBundle.php b/src/ai-bundle/src/AiBundle.php index f52212ec2..72bba0c20 100644 --- a/src/ai-bundle/src/AiBundle.php +++ b/src/ai-bundle/src/AiBundle.php @@ -600,7 +600,8 @@ private function processAgentConfig(string $name, array $config, ContainerBuilde $agentDefinition ->setArgument(2, []) // placeholder until ProcessorCompilerPass process. ->setArgument(3, []) // placeholder until ProcessorCompilerPass process. - ->setArgument(4, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)) + ->setArgument(4, $name) + ->setArgument(5, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)) ; $container->setDefinition($agentId, $agentDefinition); diff --git a/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php b/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php index 35e340b01..ae2142724 100644 --- a/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php +++ b/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php @@ -108,6 +108,33 @@ public function testAgentHasTag() $this->assertArrayHasKey('ai.agent.my_agent', $container->findTaggedServiceIds('ai.agent')); } + public function testAgentNameIsSetFromConfigKey() + { + $container = $this->buildContainer([ + 'ai' => [ + 'agent' => [ + 'my_custom_agent' => [ + 'model' => ['class' => 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'], + ], + ], + ], + ]); + + $this->assertTrue($container->hasDefinition('ai.agent.my_custom_agent')); + + $agentDefinition = $container->getDefinition('ai.agent.my_custom_agent'); + $arguments = $agentDefinition->getArguments(); + + // The 5th argument (index 4) should be the config key as agent name + $this->assertArrayHasKey(4, $arguments, 'Agent definition should have argument at index 4 for name'); + $this->assertSame('my_custom_agent', $arguments[4]); + + // Check that the tag uses the config key as name + $tags = $agentDefinition->getTag('ai.agent'); + $this->assertNotEmpty($tags, 'Agent should have ai.agent tag'); + $this->assertSame('my_custom_agent', $tags[0]['name'], 'Agent tag should use config key as name'); + } + #[TestWith([true], 'enabled')] #[TestWith([false], 'disabled')] public function testFaultTolerantAgentSpecificToolbox(bool $enabled)