Skip to content

Commit 1ff951c

Browse files
committed
ref
1 parent 39e2ef3 commit 1ff951c

File tree

15 files changed

+119
-49
lines changed

15 files changed

+119
-49
lines changed

examples/misc/persistent-chat-double-agent.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727

2828
$store = new InMemoryStore();
2929

30-
$firstChat = new Chat($firstAgent, $store);
31-
$secondChat = new Chat($secondAgent, $store);
30+
$firstChat = new Chat($firstAgent, $store, '_first_chat');
31+
$secondChat = new Chat($secondAgent, $store, '_second_chat');
3232

33-
$firstChat->initiate(new MessageBag(
33+
$firstChat->setup(new MessageBag(
3434
Message::forSystem('You are a helpful assistant. You only answer with short sentences.'),
3535
));
36-
$secondChat->initiate(new MessageBag(
36+
$secondChat->setup(new MessageBag(
3737
Message::forSystem('You are a helpful assistant. You only answer with short sentences.'),
3838
));
3939

examples/misc/persistent-chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
Message::forSystem('You are a helpful assistant. You only answer with short sentences.'),
3030
);
3131

32-
$chat->initiate($messages);
32+
$chat->setup($messages);
3333
$chat->submit(Message::ofUser('My name is Christopher.'));
3434
$message = $chat->submit(Message::ofUser('What is my name?'));
3535

src/agent/src/Chat.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,28 @@
1717
use Symfony\AI\Platform\Message\MessageBagInterface;
1818
use Symfony\AI\Platform\Message\UserMessage;
1919
use Symfony\AI\Platform\Result\TextResult;
20-
use Symfony\Component\Uid\AbstractUid;
21-
use Symfony\Component\Uid\TimeBasedUidInterface;
2220

2321
/**
2422
* @author Christopher Hertel <[email protected]>
2523
*/
2624
final readonly class Chat implements ChatInterface
2725
{
28-
private AbstractUid&TimeBasedUidInterface $currentMessageBag;
29-
3026
public function __construct(
3127
private AgentInterface $agent,
3228
private MessageStoreInterface $store,
29+
private string $storageKey = '_messages',
3330
) {
3431
}
3532

36-
public function initiate(MessageBagInterface $messages): void
33+
public function setup(MessageBagInterface $messages): void
3734
{
3835
$this->store->clear();
39-
$this->store->save($messages);
40-
41-
$this->currentMessageBag = $messages->getId();
36+
$this->store->save($this->storageKey, $messages);
4237
}
4338

4439
public function submit(UserMessage $message): AssistantMessage
4540
{
46-
$messagesBag = $this->store->load($this->currentMessageBag);
41+
$messagesBag = $this->store->load($this->storageKey);
4742

4843
$messagesBag->add($message);
4944
$result = $this->agent->call($messagesBag);
@@ -53,7 +48,7 @@ public function submit(UserMessage $message): AssistantMessage
5348
$assistantMessage = Message::ofAssistant($result->getContent());
5449
$messagesBag->add($assistantMessage);
5550

56-
$this->store->save($messagesBag);
51+
$this->store->save($this->storageKey, $messagesBag);
5752

5853
return $assistantMessage;
5954
}

src/agent/src/Chat/MessageStore/CacheStore.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use Symfony\AI\Agent\Exception\RuntimeException;
1717
use Symfony\AI\Platform\Message\MessageBag;
1818
use Symfony\AI\Platform\Message\MessageBagInterface;
19-
use Symfony\Component\Uid\AbstractUid;
20-
use Symfony\Component\Uid\TimeBasedUidInterface;
2119

2220
final readonly class CacheStore implements MessageStoreInterface
2321
{
@@ -30,19 +28,19 @@ public function __construct(
3028
}
3129
}
3230

33-
public function save(MessageBagInterface $messages): void
31+
public function save(string $storageKey, MessageBagInterface $messages): void
3432
{
35-
$item = $this->cache->getItem($messages->getId()->toRfc4122());
33+
$item = $this->cache->getItem($storageKey);
3634

3735
$item->set($messages);
3836
$item->expiresAfter($this->ttl);
3937

4038
$this->cache->save($item);
4139
}
4240

43-
public function load(AbstractUid&TimeBasedUidInterface $id): MessageBagInterface
41+
public function load(string $storageKey): MessageBagInterface
4442
{
45-
$item = $this->cache->getItem($id->toRfc4122());
43+
$item = $this->cache->getItem($storageKey);
4644

4745
return $item->isHit() ? $item->get() : new MessageBag();
4846
}

src/agent/src/Chat/MessageStore/InMemoryStore.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use Symfony\AI\Agent\Chat\MessageStoreInterface;
1515
use Symfony\AI\Platform\Message\MessageBag;
1616
use Symfony\AI\Platform\Message\MessageBagInterface;
17-
use Symfony\Component\Uid\AbstractUid;
18-
use Symfony\Component\Uid\TimeBasedUidInterface;
1917

2018
final class InMemoryStore implements MessageStoreInterface
2119
{
@@ -24,14 +22,14 @@ final class InMemoryStore implements MessageStoreInterface
2422
*/
2523
private array $messageBags;
2624

27-
public function save(MessageBagInterface $messages): void
25+
public function save(MessageBagInterface $messages, ?string $key = null): void
2826
{
29-
$this->messageBags[$messages->getId()->toRfc4122()] = $messages;
27+
$this->messageBags[$key ?? $this->key] = $messages;
3028
}
3129

32-
public function load(AbstractUid&TimeBasedUidInterface $id): MessageBagInterface
30+
public function load(string $storageKey): MessageBagInterface
3331
{
34-
return $this->messageBags[$id->toRfc4122()] ?? new MessageBag();
32+
return $this->messageBags[$storageKey] ?? new MessageBag();
3533
}
3634

3735
public function clear(): void

src/agent/src/Chat/MessageStore/SessionStore.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
use Symfony\AI\Platform\Message\MessageBagInterface;
1818
use Symfony\Component\HttpFoundation\RequestStack;
1919
use Symfony\Component\HttpFoundation\Session\SessionInterface;
20-
use Symfony\Component\Uid\AbstractUid;
21-
use Symfony\Component\Uid\TimeBasedUidInterface;
2220

2321
final readonly class SessionStore implements MessageStoreInterface
2422
{
@@ -34,14 +32,14 @@ public function __construct(
3432
$this->session = $requestStack->getSession();
3533
}
3634

37-
public function save(MessageBagInterface $messages): void
35+
public function save(string $storageKey, MessageBagInterface $messages): void
3836
{
39-
$this->session->set($messages->getId()->toRfc4122(), $messages);
37+
$this->session->set($storageKey, $messages);
4038
}
4139

42-
public function load(AbstractUid&TimeBasedUidInterface $id): MessageBagInterface
40+
public function load(string $storageKey): MessageBagInterface
4341
{
44-
return $this->session->get($id->toRfc4122(), new MessageBag());
42+
return $this->session->get($storageKey, new MessageBag());
4543
}
4644

4745
public function clear(): void

src/agent/src/Chat/MessageStoreInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
namespace Symfony\AI\Agent\Chat;
1313

1414
use Symfony\AI\Platform\Message\MessageBagInterface;
15-
use Symfony\Component\Uid\AbstractUid;
16-
use Symfony\Component\Uid\TimeBasedUidInterface;
1715

1816
interface MessageStoreInterface
1917
{
20-
public function save(MessageBagInterface $messages): void;
18+
public function save(MessageBagInterface $messages, ?string $key = null): void;
2119

22-
public function load(AbstractUid&TimeBasedUidInterface $id): MessageBagInterface;
20+
public function load(?string $key = null): MessageBagInterface;
2321

24-
public function clear(): void;
22+
public function clear(?string $key = null): void;
23+
24+
public function setKey(string $key): void;
2525
}

src/agent/src/ChatInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
interface ChatInterface
2020
{
21-
public function initiate(MessageBagInterface $messages): void;
21+
public function setup(MessageBagInterface $messages): void;
2222

2323
/**
2424
* @throws ExceptionInterface When the chat submission fails due to agent errors

src/agent/tests/AgentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,10 @@ public function testDoubleAgentCanUseSameMessageStore()
418418
Message::forSystem('You are a helpful assistant. You only answer with short sentences.'),
419419
);
420420

421-
$firstChat->initiate($messages);
421+
$firstChat->setup($messages);
422422
$firstChat->submit(new UserMessage(new Text('Hello')));
423423

424-
$secondChat->initiate($messages);
424+
$secondChat->setup($messages);
425425
$secondChat->submit(new UserMessage(new Text('Hello')));
426426
$secondChat->submit(new UserMessage(new Text('Hello there')));
427427

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Agent\Tests\Chat;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\UsesClass;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\AI\Agent\Chat\MessageStore\CacheStore;
18+
use Symfony\AI\Platform\Message\Message;
19+
use Symfony\AI\Platform\Message\MessageBag;
20+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
21+
22+
#[CoversClass(CacheStore::class)]
23+
#[UsesClass(MessageBag::class)]
24+
#[UsesClass(Message::class)]
25+
final class CacheStoreTest extends TestCase
26+
{
27+
public function testItCanStore()
28+
{
29+
$messageBag = new MessageBag();
30+
$messageBag->add(Message::ofUser('Hello'));
31+
32+
$store = new CacheStore(new ArrayAdapter());
33+
$store->save('foo', $messageBag);
34+
35+
$this->assertCount(1, $store->load($messageBag->getId()));
36+
}
37+
38+
public function testItCanStoreMultipleMessageBags()
39+
{
40+
$firstMessageBag = new MessageBag();
41+
$firstMessageBag->add(Message::ofUser('Hello'));
42+
43+
$secondMessageBag = new MessageBag();
44+
45+
$store = new CacheStore(new ArrayAdapter());
46+
$store->save('foo', $firstMessageBag);
47+
$store->save('foo', $secondMessageBag);
48+
49+
$this->assertCount(1, $store->load($firstMessageBag->getId()));
50+
$this->assertCount(0, $store->load($secondMessageBag->getId()));
51+
}
52+
53+
public function testItCanClear()
54+
{
55+
$firstMessageBag = new MessageBag();
56+
$firstMessageBag->add(Message::ofUser('Hello'));
57+
58+
$secondMessageBag = new MessageBag();
59+
60+
$store = new CacheStore(new ArrayAdapter());
61+
$store->save('foo', $firstMessageBag);
62+
$store->save('foo', $secondMessageBag);
63+
64+
$this->assertCount(1, $store->load($firstMessageBag->getId()));
65+
$this->assertCount(0, $store->load($secondMessageBag->getId()));
66+
67+
$store->clear();
68+
69+
$this->assertCount(0, $store->load($firstMessageBag->getId()));
70+
$this->assertCount(0, $store->load($secondMessageBag->getId()));
71+
}
72+
}

0 commit comments

Comments
 (0)