Skip to content

Commit aba1562

Browse files
committed
ref
1 parent 76c9c26 commit aba1562

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

src/ai-bundle/config/options.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,9 @@
469469
->arrayPrototype()
470470
->children()
471471
->stringNode('service')->cannotBeEmpty()->defaultValue('cache.app')->end()
472-
->stringNode('cache_key')->end()
472+
->stringNode('cache_key')
473+
->info('The name of the store will be used if the key is not set')
474+
->end()
473475
->stringNode('strategy')->end()
474476
->end()
475477
->end()
@@ -755,7 +757,10 @@
755757
->arrayPrototype()
756758
->children()
757759
->stringNode('service')->cannotBeEmpty()->defaultValue('cache.app')->end()
758-
->stringNode('key')->end()
760+
->stringNode('key')
761+
->info('The name of the message store will be used if the key is not set')
762+
->end()
763+
->integerNode('ttl')->end()
759764
->end()
760765
->end()
761766
->end()

src/ai-bundle/src/AiBundle.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Symfony\AI\AiBundle\Profiler\TraceableToolbox;
3636
use Symfony\AI\AiBundle\Security\Attribute\IsGrantedTool;
3737
use Symfony\AI\Chat\Bridge\HttpFoundation\SessionStore;
38+
use Symfony\AI\Chat\Bridge\Local\CacheStore as CacheMessageStore;
3839
use Symfony\AI\Chat\Bridge\Meilisearch\MessageStore as MeilisearchMessageStore;
3940
use Symfony\AI\Chat\Bridge\Pogocache\MessageStore as PogocacheMessageStore;
4041
use Symfony\AI\Chat\Bridge\Redis\MessageStore as RedisMessageStore;
@@ -811,10 +812,6 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
811812
new Definition(DistanceCalculator::class),
812813
];
813814

814-
if (\array_key_exists('cache_key', $store) && null !== $store['cache_key']) {
815-
$arguments[2] = $store['cache_key'];
816-
}
817-
818815
if (\array_key_exists('strategy', $store) && null !== $store['strategy']) {
819816
if (!$container->hasDefinition('ai.store.distance_calculator.'.$name)) {
820817
$distanceCalculatorDefinition = new Definition(DistanceCalculator::class);
@@ -826,10 +823,14 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
826823
$arguments[1] = new Reference('ai.store.distance_calculator.'.$name);
827824
}
828825

826+
$arguments[2] = \array_key_exists('cache_key', $store) && null !== $store['cache_key']
827+
? $store['cache_key']
828+
: $name;
829+
829830
$definition = new Definition(CacheStore::class);
830831
$definition
831-
->addTag('ai.store')
832-
->setArguments($arguments);
832+
->setArguments($arguments)
833+
->addTag('ai.store');
833834

834835
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
835836
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1364,11 +1365,20 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
13641365
*/
13651366
private function processMessageStoreConfig(string $type, array $messageStores, ContainerBuilder $container): void
13661367
{
1367-
if ('memory' === $type) {
1368+
if ('cache' === $type) {
13681369
foreach ($messageStores as $name => $messageStore) {
1369-
$definition = new Definition(InMemoryStore::class);
1370+
$arguments = [
1371+
new Reference($messageStore['service']),
1372+
$messageStore['key'] ?? $name,
1373+
];
1374+
1375+
if (\array_key_exists('ttl', $messageStore)) {
1376+
$arguments[2] = $messageStore['ttl'];
1377+
}
1378+
1379+
$definition = new Definition(CacheMessageStore::class);
13701380
$definition
1371-
->setArgument(0, $messageStore['identifier'])
1381+
->setArguments($arguments)
13721382
->addTag('ai.message_store');
13731383

13741384
$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
@@ -1377,19 +1387,11 @@ private function processMessageStoreConfig(string $type, array $messageStores, C
13771387
}
13781388
}
13791389

1380-
if ('cache' === $type) {
1390+
if ('memory' === $type) {
13811391
foreach ($messageStores as $name => $messageStore) {
1382-
$arguments = [
1383-
new Reference($messageStore['service']),
1384-
];
1385-
1386-
if (\array_key_exists('key', $messageStore)) {
1387-
$arguments['key'] = $messageStore['key'];
1388-
}
1389-
1390-
$definition = new Definition(CacheStore::class);
1392+
$definition = new Definition(InMemoryStore::class);
13911393
$definition
1392-
->setArguments($arguments)
1394+
->setArgument(0, $messageStore['identifier'])
13931395
->addTag('ai.message_store');
13941396

13951397
$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,6 +2757,53 @@ public function testVectorizerModelBooleanOptionsArePreserved()
27572757
$this->assertSame('text-embedding-3-small?normalize=false&cache=true&nested%5Bbool%5D=false', $vectorizerDefinition->getArgument(1));
27582758
}
27592759

2760+
public function testCacheMessageStoreCanBeConfiguredWithCustomKey()
2761+
{
2762+
$container = $this->buildContainer([
2763+
'ai' => [
2764+
'message_store' => [
2765+
'cache' => [
2766+
'custom' => [
2767+
'service' => 'cache.app',
2768+
'key' => 'custom',
2769+
],
2770+
],
2771+
],
2772+
],
2773+
]);
2774+
2775+
$cacheMessageStoreDefinition = $container->getDefinition('ai.message_store.cache.custom');
2776+
2777+
$this->assertInstanceOf(Reference::class, $cacheMessageStoreDefinition->getArgument(0));
2778+
$this->assertSame('cache.app', (string) $cacheMessageStoreDefinition->getArgument(0));
2779+
2780+
$this->assertSame('custom', (string) $cacheMessageStoreDefinition->getArgument(1));
2781+
}
2782+
2783+
public function testCacheMessageStoreCanBeConfiguredWithCustomTtl()
2784+
{
2785+
$container = $this->buildContainer([
2786+
'ai' => [
2787+
'message_store' => [
2788+
'cache' => [
2789+
'custom' => [
2790+
'service' => 'cache.app',
2791+
'ttl' => 3600,
2792+
],
2793+
],
2794+
],
2795+
],
2796+
]);
2797+
2798+
$cacheMessageStoreDefinition = $container->getDefinition('ai.message_store.cache.custom');
2799+
2800+
$this->assertInstanceOf(Reference::class, $cacheMessageStoreDefinition->getArgument(0));
2801+
$this->assertSame('cache.app', (string) $cacheMessageStoreDefinition->getArgument(0));
2802+
2803+
$this->assertSame('custom', (string) $cacheMessageStoreDefinition->getArgument(1));
2804+
$this->assertSame(3600, (int) $cacheMessageStoreDefinition->getArgument(2));
2805+
}
2806+
27602807
private function buildContainer(array $configuration): ContainerBuilder
27612808
{
27622809
$container = new ContainerBuilder();

src/chat/src/Bridge/Local/CacheStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class CacheStore implements ManagedStoreInterface, MessageStoreInterface
2424
{
2525
public function __construct(
2626
private readonly CacheItemPoolInterface $cache,
27-
private readonly string $cacheKey,
27+
private readonly string $cacheKey = '_message_store_cache',
2828
private readonly int $ttl = 86400,
2929
) {
3030
if (!interface_exists(CacheItemPoolInterface::class)) {

0 commit comments

Comments
 (0)