Skip to content

Commit 6bbb878

Browse files
Guikingonechr-hertel
authored andcommitted
[AIBundle] Improve injection aliases
1 parent 11ef98f commit 6bbb878

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/ai-bundle/src/AiBundle.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
use Symfony\AI\AiBundle\Security\Attribute\IsGrantedTool;
2828
use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory as AnthropicPlatformFactory;
2929
use Symfony\AI\Platform\Bridge\Azure\OpenAi\PlatformFactory as AzureOpenAiPlatformFactory;
30+
use Symfony\AI\Platform\Bridge\Cerebras\PlatformFactory as CerebrasPlatformFactory;
3031
use Symfony\AI\Platform\Bridge\Gemini\PlatformFactory as GeminiPlatformFactory;
3132
use Symfony\AI\Platform\Bridge\LmStudio\PlatformFactory as LmStudioPlatformFactory;
3233
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory as MistralPlatformFactory;
3334
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory as OllamaPlatformFactory;
3435
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory as OpenAiPlatformFactory;
3536
use Symfony\AI\Platform\Bridge\OpenRouter\PlatformFactory as OpenRouterPlatformFactory;
36-
use Symfony\AI\Platform\Bridge\Cerebras\PlatformFactory as CerebrasPlatformFactory;
3737
use Symfony\AI\Platform\Model;
3838
use Symfony\AI\Platform\ModelClientInterface;
3939
use Symfony\AI\Platform\Platform;
@@ -56,6 +56,7 @@
5656
use Symfony\AI\Store\StoreInterface;
5757
use Symfony\AI\Store\VectorStoreInterface;
5858
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
59+
use Symfony\Component\DependencyInjection\Attribute\Target;
5960
use Symfony\Component\DependencyInjection\ChildDefinition;
6061
use Symfony\Component\DependencyInjection\ContainerBuilder;
6162
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -107,9 +108,6 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
107108
foreach ($config['agent'] as $agentName => $agent) {
108109
$this->processAgentConfig($agentName, $agent, $builder);
109110
}
110-
if (1 === \count($config['agent']) && isset($agentName)) {
111-
$builder->setAlias(AgentInterface::class, 'ai.agent.'.$agentName);
112-
}
113111

114112
foreach ($config['store'] ?? [] as $type => $store) {
115113
$this->processStoreConfig($type, $store, $builder);
@@ -460,6 +458,7 @@ private function processAgentConfig(string $name, array $config, ContainerBuilde
460458
;
461459

462460
$container->setDefinition('ai.agent.'.$name, $agentDefinition);
461+
$container->registerAliasForArgument('ai.agent.'.$name, AgentInterface::class, (new Target($name.'Agent'))->getParsedName());
463462
}
464463

465464
/**

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ public function testExtensionLoadDoesNotThrow()
3030
$this->buildContainer($this->getFullConfig());
3131
}
3232

33+
public function testInjectionAgentAliasIsRegistered()
34+
{
35+
$container = $this->buildContainer([
36+
'ai' => [
37+
'agent' => [
38+
'my_agent' => [
39+
'model' => ['class' => 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'],
40+
],
41+
],
42+
],
43+
]);
44+
45+
$this->assertTrue($container->hasAlias('Symfony\AI\Agent\AgentInterface $myAgentAgent'));
46+
}
47+
3348
#[TestWith([true], 'enabled')]
3449
#[TestWith([false], 'disabled')]
3550
public function testFaultTolerantAgentSpecificToolbox(bool $enabled)

0 commit comments

Comments
 (0)