3131use Symfony \AI \Agent \Toolbox \ToolFactory \MemoryToolFactory ;
3232use Symfony \AI \AiBundle \DependencyInjection \ProcessorCompilerPass ;
3333use Symfony \AI \AiBundle \Exception \InvalidArgumentException ;
34+ use Symfony \AI \AiBundle \Profiler \TraceableChat ;
3435use Symfony \AI \AiBundle \Profiler \TraceableMessageStore ;
3536use Symfony \AI \AiBundle \Profiler \TraceablePlatform ;
3637use Symfony \AI \AiBundle \Profiler \TraceableToolbox ;
3738use Symfony \AI \AiBundle \Security \Attribute \IsGrantedTool ;
3839use Symfony \AI \Chat \Bridge \HttpFoundation \SessionStore ;
3940use Symfony \AI \Chat \Bridge \Meilisearch \MessageStore as MeilisearchMessageStore ;
4041use Symfony \AI \Chat \Bridge \Pogocache \MessageStore as PogocacheMessageStore ;
42+ use Symfony \AI \Chat \Chat ;
43+ use Symfony \AI \Chat \ChatInterface ;
4144use Symfony \AI \Chat \MessageStoreInterface ;
4245use Symfony \AI \Platform \Bridge \Anthropic \PlatformFactory as AnthropicPlatformFactory ;
4346use Symfony \AI \Platform \Bridge \Azure \OpenAi \PlatformFactory as AzureOpenAiPlatformFactory ;
@@ -182,12 +185,12 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
182185
183186 if ($ builder ->getParameter ('kernel.debug ' )) {
184187 foreach ($ messageStores as $ messageStore ) {
185- $ traceablePlatformDefinition = (new Definition (TraceableMessageStore::class))
188+ $ traceableMessageStoreDefinition = (new Definition (TraceableMessageStore::class))
186189 ->setDecoratedService ($ messageStore )
187190 ->setArguments ([new Reference ('.inner ' )])
188191 ->addTag ('ai.traceable_message_store ' );
189192 $ suffix = u ($ messageStore )->afterLast ('. ' )->toString ();
190- $ builder ->setDefinition ('ai.traceable_message_store. ' .$ suffix , $ traceablePlatformDefinition );
193+ $ builder ->setDefinition ('ai.traceable_message_store. ' .$ suffix , $ traceableMessageStoreDefinition );
191194 }
192195 }
193196
@@ -196,6 +199,27 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
196199 $ builder ->removeDefinition ('ai.command.drop_message_store ' );
197200 }
198201
202+ foreach ($ config ['chat ' ] ?? [] as $ name => $ chat ) {
203+ $ this ->processChatConfig ($ name , $ chat , $ builder );
204+ }
205+
206+ $ chats = array_keys ($ builder ->findTaggedServiceIds ('ai.chat ' ));
207+
208+ if (1 === \count ($ chats )) {
209+ $ builder ->setAlias (ChatInterface::class, reset ($ chats ));
210+ }
211+
212+ if ($ builder ->getParameter ('kernel.debug ' )) {
213+ foreach ($ chats as $ chat ) {
214+ $ traceableChatDefinition = (new Definition (TraceableChat::class))
215+ ->setDecoratedService ($ chat )
216+ ->setArguments ([new Reference ('.inner ' )])
217+ ->addTag ('ai.traceable_chat ' );
218+ $ suffix = u ($ chat )->afterLast ('. ' )->toString ();
219+ $ builder ->setDefinition ('ai.traceable_chat. ' .$ suffix , $ traceableChatDefinition );
220+ }
221+ }
222+
199223 foreach ($ config ['vectorizer ' ] ?? [] as $ vectorizerName => $ vectorizer ) {
200224 $ this ->processVectorizerConfig ($ vectorizerName , $ vectorizer , $ builder );
201225 }
@@ -1399,6 +1423,26 @@ private function processMessageStoreConfig(string $type, array $messageStores, C
13991423 }
14001424 }
14011425
1426+ /**
1427+ * @param array{
1428+ * agent: string,
1429+ * message_store: string,
1430+ * } $configuration
1431+ */
1432+ private function processChatConfig (string $ name , array $ configuration , ContainerBuilder $ container ): void
1433+ {
1434+ $ definition = new Definition (Chat::class);
1435+ $ definition
1436+ ->setArguments ([
1437+ new Reference ('ai.agent. ' .$ configuration ['agent ' ]),
1438+ new Reference ('ai.message_store. ' .$ configuration ['message_store ' ]),
1439+ ])
1440+ ->addTag ('ai.chat ' );
1441+
1442+ $ container ->setDefinition ('ai.chat. ' .$ name , $ definition );
1443+ $ container ->registerAliasForArgument ('ai.chat. ' .$ name , ChatInterface::class, $ name );
1444+ }
1445+
14021446 /**
14031447 * @param array<string, mixed> $config
14041448 */
0 commit comments