|
31 | 31 | use Symfony\AI\Agent\Toolbox\ToolFactory\MemoryToolFactory; |
32 | 32 | use Symfony\AI\AiBundle\DependencyInjection\ProcessorCompilerPass; |
33 | 33 | use Symfony\AI\AiBundle\Exception\InvalidArgumentException; |
| 34 | +use Symfony\AI\AiBundle\Profiler\TraceableChat; |
| 35 | +use Symfony\AI\AiBundle\Profiler\TraceableMessageStore; |
34 | 36 | use Symfony\AI\AiBundle\Profiler\TraceablePlatform; |
35 | 37 | use Symfony\AI\AiBundle\Profiler\TraceableToolbox; |
36 | 38 | use Symfony\AI\AiBundle\Security\Attribute\IsGrantedTool; |
37 | 39 | use Symfony\AI\Chat\Bridge\HttpFoundation\SessionStore; |
38 | 40 | use Symfony\AI\Chat\Bridge\Meilisearch\MessageStore as MeilisearchMessageStore; |
39 | 41 | use Symfony\AI\Chat\Bridge\Pogocache\MessageStore as PogocacheMessageStore; |
40 | 42 | use Symfony\AI\Chat\Bridge\Redis\MessageStore as RedisMessageStore; |
| 43 | +use Symfony\AI\Chat\Chat; |
| 44 | +use Symfony\AI\Chat\ChatInterface; |
41 | 45 | use Symfony\AI\Chat\MessageStoreInterface; |
42 | 46 | use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory as AnthropicPlatformFactory; |
43 | 47 | use Symfony\AI\Platform\Bridge\Azure\OpenAi\PlatformFactory as AzureOpenAiPlatformFactory; |
@@ -181,11 +185,49 @@ public function loadExtension(array $config, ContainerConfigurator $container, C |
181 | 185 | $builder->setAlias(MessageStoreInterface::class, reset($messageStores)); |
182 | 186 | } |
183 | 187 |
|
| 188 | + if ($builder->getParameter('kernel.debug')) { |
| 189 | + foreach ($messageStores as $messageStore) { |
| 190 | + $traceableMessageStoreDefinition = (new Definition(TraceableMessageStore::class)) |
| 191 | + ->setDecoratedService($messageStore) |
| 192 | + ->setArguments([ |
| 193 | + new Reference('.inner'), |
| 194 | + new Reference(ClockInterface::class), |
| 195 | + ]) |
| 196 | + ->addTag('ai.traceable_message_store'); |
| 197 | + $suffix = u($messageStore)->afterLast('.')->toString(); |
| 198 | + $builder->setDefinition('ai.traceable_message_store.'.$suffix, $traceableMessageStoreDefinition); |
| 199 | + } |
| 200 | + } |
| 201 | + |
184 | 202 | if ([] === $messageStores) { |
185 | 203 | $builder->removeDefinition('ai.command.setup_message_store'); |
186 | 204 | $builder->removeDefinition('ai.command.drop_message_store'); |
187 | 205 | } |
188 | 206 |
|
| 207 | + foreach ($config['chat'] ?? [] as $name => $chat) { |
| 208 | + $this->processChatConfig($name, $chat, $builder); |
| 209 | + } |
| 210 | + |
| 211 | + $chats = array_keys($builder->findTaggedServiceIds('ai.chat')); |
| 212 | + |
| 213 | + if (1 === \count($chats)) { |
| 214 | + $builder->setAlias(ChatInterface::class, reset($chats)); |
| 215 | + } |
| 216 | + |
| 217 | + if ($builder->getParameter('kernel.debug')) { |
| 218 | + foreach ($chats as $chat) { |
| 219 | + $traceableChatDefinition = (new Definition(TraceableChat::class)) |
| 220 | + ->setDecoratedService($chat) |
| 221 | + ->setArguments([ |
| 222 | + new Reference('.inner'), |
| 223 | + new Reference(ClockInterface::class), |
| 224 | + ]) |
| 225 | + ->addTag('ai.traceable_chat'); |
| 226 | + $suffix = u($chat)->afterLast('.')->toString(); |
| 227 | + $builder->setDefinition('ai.traceable_chat.'.$suffix, $traceableChatDefinition); |
| 228 | + } |
| 229 | + } |
| 230 | + |
189 | 231 | foreach ($config['vectorizer'] ?? [] as $vectorizerName => $vectorizer) { |
190 | 232 | $this->processVectorizerConfig($vectorizerName, $vectorizer, $builder); |
191 | 233 | } |
@@ -1475,6 +1517,26 @@ private function processMessageStoreConfig(string $type, array $messageStores, C |
1475 | 1517 | } |
1476 | 1518 | } |
1477 | 1519 |
|
| 1520 | + /** |
| 1521 | + * @param array{ |
| 1522 | + * agent: string, |
| 1523 | + * message_store: string, |
| 1524 | + * } $configuration |
| 1525 | + */ |
| 1526 | + private function processChatConfig(string $name, array $configuration, ContainerBuilder $container): void |
| 1527 | + { |
| 1528 | + $definition = new Definition(Chat::class); |
| 1529 | + $definition |
| 1530 | + ->setArguments([ |
| 1531 | + new Reference($configuration['agent']), |
| 1532 | + new Reference($configuration['message_store']), |
| 1533 | + ]) |
| 1534 | + ->addTag('ai.chat'); |
| 1535 | + |
| 1536 | + $container->setDefinition('ai.chat.'.$name, $definition); |
| 1537 | + $container->registerAliasForArgument('ai.chat.'.$name, ChatInterface::class, $name); |
| 1538 | + } |
| 1539 | + |
1478 | 1540 | /** |
1479 | 1541 | * @param array<string, mixed> $config |
1480 | 1542 | */ |
|
0 commit comments