Skip to content

Commit 0f11b4f

Browse files
committed
bug #1560 [Agent][Chat] Add metadata to AssistantMessage (janssensglenn)
This PR was squashed before being merged into the main branch. Discussion ---------- [Agent][Chat] Add `metadata` to `AssistantMessage` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | Fix #1558 | License | MIT This PR adds the sources of a TextResult to the metadata of the AssistantMessage within the [Chat](https://github.com/symfony/ai-chat) component. Commits ------- 337f0f3 [Agent][Chat] Add `metadata` to `AssistantMessage`
2 parents 75746d6 + 337f0f3 commit 0f11b4f

File tree

6 files changed

+41
-34
lines changed

6 files changed

+41
-34
lines changed

src/agent/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* [BC BREAK] Rename `Symfony\AI\Agent\Toolbox\Tool\Agent` to `Symfony\AI\Agent\Toolbox\Tool\Subagent`
8+
* Add `MetaDataAwareTrait` to `MockResponse`, the metadata will also be set on the returned `TextResult` when calling the `toResult` function
89

910
0.3
1011
---

src/agent/src/MockResponse.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\AI\Agent;
1313

14+
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
1415
use Symfony\AI\Platform\Result\ResultInterface;
1516
use Symfony\AI\Platform\Result\TextResult;
1617

@@ -24,14 +25,19 @@
2425
*/
2526
final class MockResponse
2627
{
28+
use MetadataAwareTrait;
29+
2730
public function __construct(
2831
private readonly string $content = '',
2932
) {
3033
}
3134

3235
public function toResult(): ResultInterface
3336
{
34-
return new TextResult($this->content);
37+
$result = new TextResult($this->content);
38+
$result->getMetadata()->merge($this->getMetadata());
39+
40+
return $result;
3541
}
3642

3743
public function getContent(): string

src/agent/tests/MockResponseTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ public function testConstructorWithEmptyContent()
3434
public function testToResult()
3535
{
3636
$response = new MockResponse('Response content');
37+
$response->getMetadata()->add('sources', ['https://example.com']);
38+
3739
$result = $response->toResult();
3840

3941
$this->assertInstanceOf(TextResult::class, $result);
4042
$this->assertSame('Response content', $result->getContent());
43+
$this->assertEquals($response->getMetadata(), $result->getMetadata());
4144
}
4245

4346
public function testCreate()

src/chat/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ CHANGELOG
55
---
66

77
* Add the component
8+
* Add `metadata` from `TextResult` to `AssistantMessage`

src/chat/src/Chat.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function submit(UserMessage $message): AssistantMessage
4545
\assert($result instanceof TextResult);
4646

4747
$assistantMessage = Message::ofAssistant($result->getContent());
48+
$assistantMessage->getMetadata()->merge($result->getMetadata());
4849
$messages->add($assistantMessage);
4950

5051
$this->store->save($messages);

src/chat/tests/ChatTest.php

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,24 @@
1111

1212
namespace Symfony\AI\Chat\Tests;
1313

14-
use PHPUnit\Framework\MockObject\MockObject;
1514
use PHPUnit\Framework\TestCase;
16-
use Symfony\AI\Agent\AgentInterface;
15+
use Symfony\AI\Agent\MockAgent;
16+
use Symfony\AI\Agent\MockResponse;
1717
use Symfony\AI\Chat\Chat;
1818
use Symfony\AI\Chat\InMemory\Store as InMemoryStore;
1919
use Symfony\AI\Platform\Message\AssistantMessage;
2020
use Symfony\AI\Platform\Message\Message;
2121
use Symfony\AI\Platform\Message\MessageBag;
22-
use Symfony\AI\Platform\Result\TextResult;
2322

2423
final class ChatTest extends TestCase
2524
{
26-
private AgentInterface&MockObject $agent;
25+
private MockAgent $agent;
2726
private InMemoryStore $store;
2827
private Chat $chat;
2928

3029
protected function setUp(): void
3130
{
32-
$this->agent = $this->createMock(AgentInterface::class);
31+
$this->agent = new MockAgent();
3332
$this->store = new InMemoryStore();
3433
$this->chat = new Chat($this->agent, $this->store);
3534
}
@@ -41,29 +40,30 @@ public function testItInitiatesChatByClearingAndSavingMessages()
4140
$this->chat->initiate($messages);
4241

4342
$this->assertCount(0, $this->store->load());
43+
44+
$this->agent->assertNotCalled();
4445
}
4546

4647
public function testItSubmitsUserMessageAndReturnsAssistantMessage()
4748
{
48-
$userMessage = Message::ofUser('Hello, how are you?');
49+
$userMessage = Message::ofUser($userPrompt = 'Hello, how are you?');
4950
$assistantContent = 'I am doing well, thank you!';
51+
$assistantSources = ['https://example.com'];
5052

51-
$textResult = new TextResult($assistantContent);
52-
53-
$this->agent->expects($this->once())
54-
->method('call')
55-
->with($this->callback(static function (MessageBag $messages) use ($userMessage) {
56-
$messagesArray = $messages->getMessages();
53+
$response = new MockResponse($assistantContent);
54+
$response->getMetadata()->add('sources', $assistantSources);
5755

58-
return end($messagesArray) === $userMessage;
59-
}))
60-
->willReturn($textResult);
56+
$this->agent->addResponse($userPrompt, $response);
6157

6258
$result = $this->chat->submit($userMessage);
6359

6460
$this->assertInstanceOf(AssistantMessage::class, $result);
6561
$this->assertSame($assistantContent, $result->getContent());
62+
$this->assertSame($assistantSources, $result->getMetadata()->get('sources', []));
6663
$this->assertCount(2, $this->store->load());
64+
65+
$this->agent->assertCallCount(1);
66+
$this->agent->assertCalledWith($userPrompt);
6767
}
6868

6969
public function testItAppendsMessagesToExistingConversation()
@@ -75,42 +75,37 @@ public function testItAppendsMessagesToExistingConversation()
7575
$existingMessages->add($existingUserMessage);
7676
$existingMessages->add($existingAssistantMessage);
7777

78-
$newUserMessage = Message::ofUser('Can you help with programming?');
79-
$newAssistantContent = 'Yes, I can help with programming!';
78+
$this->store->save($existingMessages);
8079

81-
$textResult = new TextResult($newAssistantContent);
80+
$newUserMessage = Message::ofUser($newUserPrompt = 'Can you help with programming?');
81+
$newAssistantContent = 'Yes, I can help with programming!';
8282

83-
$this->agent->expects($this->once())
84-
->method('call')
85-
->willReturn($textResult);
83+
$this->agent->addResponse($newUserPrompt, $newAssistantContent);
8684

8785
$result = $this->chat->submit($newUserMessage);
8886

8987
$this->assertInstanceOf(AssistantMessage::class, $result);
9088
$this->assertSame($newAssistantContent, $result->getContent());
91-
$this->assertCount(2, $this->store->load());
89+
$this->assertCount(4, $this->store->load());
90+
91+
$this->agent->assertCallCount(1);
92+
$this->agent->assertCalledWith($newUserPrompt);
9293
}
9394

9495
public function testItHandlesEmptyMessageStore()
9596
{
96-
$userMessage = Message::ofUser('First message');
97+
$userMessage = Message::ofUser($userPrompt = 'First message');
9798
$assistantContent = 'First response';
9899

99-
$textResult = new TextResult($assistantContent);
100-
101-
$this->agent->expects($this->once())
102-
->method('call')
103-
->with($this->callback(static function (MessageBag $messages) {
104-
$messagesArray = $messages->getMessages();
105-
106-
return 1 === \count($messagesArray);
107-
}))
108-
->willReturn($textResult);
100+
$this->agent->addResponse($userPrompt, $assistantContent);
109101

110102
$result = $this->chat->submit($userMessage);
111103

112104
$this->assertInstanceOf(AssistantMessage::class, $result);
113105
$this->assertSame($assistantContent, $result->getContent());
114106
$this->assertCount(2, $this->store->load());
107+
108+
$this->agent->assertCallCount(1);
109+
$this->agent->assertCalledWith($userPrompt);
115110
}
116111
}

0 commit comments

Comments
 (0)