Skip to content

Commit a4747cb

Browse files
committed
feature #758 [Platform] Introduce getters and metadata to message classes (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Platform] Introduce getters and metadata to message classes | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | | License | MIT Demotes the properties of message and content classes from public to private, therefore introduces getters and adds metadata to messages. Commits ------- 4de652b Introduce getters and metadata to message classes
2 parents a23b6aa + 4de652b commit a4747cb

File tree

61 files changed

+250
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+250
-172
lines changed

demo/tests/Blog/ChatTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function testLoadMessagesReturnsDefaultSystemMessage()
3737

3838
$systemMessage = $messages->getMessages()[0];
3939
$this->assertInstanceOf(SystemMessage::class, $systemMessage);
40-
$this->assertStringContainsString('helpful assistant', $systemMessage->content);
41-
$this->assertStringContainsString('similarity_search', $systemMessage->content);
40+
$this->assertStringContainsString('helpful assistant', $systemMessage->getContent());
41+
$this->assertStringContainsString('similarity_search', $systemMessage->getContent());
4242
}
4343

4444
public function testSubmitMessageAddsUserMessageAndAgentResponse()
@@ -65,12 +65,12 @@ public function testSubmitMessageAddsUserMessageAndAgentResponse()
6565
// Check user message
6666
$userMessage = $messageList[1];
6767
$this->assertInstanceOf(UserMessage::class, $userMessage);
68-
$this->assertSame('What is Symfony?', $userMessage->content[0]->text);
68+
$this->assertSame('What is Symfony?', $userMessage->getContent()[0]->getText());
6969

7070
// Check assistant message
7171
$assistantMessage = $messageList[2];
7272
$this->assertInstanceOf(AssistantMessage::class, $assistantMessage);
73-
$this->assertSame('Symfony is a PHP web framework for building web applications and APIs.', $assistantMessage->content);
73+
$this->assertSame('Symfony is a PHP web framework for building web applications and APIs.', $assistantMessage->getContent());
7474
}
7575

7676
public function testSubmitMessageWithUnknownQueryUsesDefaultResponse()
@@ -91,7 +91,7 @@ public function testSubmitMessageWithUnknownQueryUsesDefaultResponse()
9191

9292
// Check assistant used default response
9393
$assistantMessage = $messageList[2];
94-
$this->assertSame('I can help you with Symfony-related questions!', $assistantMessage->content);
94+
$this->assertSame('I can help you with Symfony-related questions!', $assistantMessage->getContent());
9595
}
9696

9797
public function testMultipleMessagesAreTrackedCorrectly()
@@ -169,10 +169,10 @@ public function testAgentReceivesFullConversationHistory()
169169
$this->assertCount(5, $messages);
170170

171171
// Verify the conversation flow (with 5 messages)
172-
$this->assertStringContainsString('helpful assistant', $messages[0]->content); // system
173-
$this->assertSame('What is Symfony?', $messages[1]->content[0]->text); // user1
174-
$this->assertSame('Symfony is a PHP web framework for building web applications and APIs.', $messages[2]->content); // assistant1
175-
$this->assertSame('Tell me more', $messages[3]->content[0]->text); // user2
172+
$this->assertStringContainsString('helpful assistant', $messages[0]->getContent()); // system
173+
$this->assertSame('What is Symfony?', $messages[1]->getContent()[0]->getText()); // user1
174+
$this->assertSame('Symfony is a PHP web framework for building web applications and APIs.', $messages[2]->getContent()); // assistant1
175+
$this->assertSame('Tell me more', $messages[3]->getContent()[0]->getText()); // user2
176176
// The 5th message appears to be the previous assistant response or another system message
177177
}
178178

examples/chat/persistent-chat-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535
$chat->submit(Message::ofUser('My name is Christopher.'));
3636
$message = $chat->submit(Message::ofUser('What is my name?'));
3737

38-
echo $message->content.\PHP_EOL;
38+
echo $message->getContent().\PHP_EOL;

examples/chat/persistent-chat-session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@
4444
$chat->submit(Message::ofUser('My name is Christopher.'));
4545
$message = $chat->submit(Message::ofUser('What is my name?'));
4646

47-
echo $message->content.\PHP_EOL;
47+
echo $message->getContent().\PHP_EOL;

examples/chat/persistent-chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
$chat->submit(Message::ofUser('My name is Christopher.'));
3232
$message = $chat->submit(Message::ofUser('What is my name?'));
3333

34-
echo $message->content.\PHP_EOL;
34+
echo $message->getContent().\PHP_EOL;

src/agent/src/Memory/EmbeddingProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function load(Input $input): array
4343
}
4444

4545
$userMessageTextContent = array_filter(
46-
$userMessage->content,
46+
$userMessage->getContent(),
4747
static fn (ContentInterface $content): bool => $content instanceof Text,
4848
);
4949

@@ -53,7 +53,7 @@ public function load(Input $input): array
5353

5454
$userMessageTextContent = array_shift($userMessageTextContent);
5555

56-
$vectors = $this->platform->invoke($this->model->getName(), $userMessageTextContent->text)->asVectors();
56+
$vectors = $this->platform->invoke($this->model->getName(), $userMessageTextContent->getText())->asVectors();
5757
$foundEmbeddingContent = $this->vectorStore->query($vectors[0]);
5858
if (0 === \count($foundEmbeddingContent)) {
5959
return [];

src/agent/src/Memory/MemoryInputProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function processInput(Input $input): void
6969
return;
7070
}
7171

72-
$systemMessage = $input->getMessageBag()->getSystemMessage()->content ?? '';
72+
$systemMessage = $input->getMessageBag()->getSystemMessage()?->getContent() ?? '';
7373

7474
$combinedMessage = self::MEMORY_PROMPT_MESSAGE.$memory;
7575
if ('' !== $systemMessage) {

src/agent/src/MockAgent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public function call(MessageBag $messages, array $options = []): ResultInterface
6262
$content = '';
6363

6464
if ($lastMessage instanceof UserMessage) {
65-
foreach ($lastMessage->content as $messageContent) {
65+
foreach ($lastMessage->getContent() as $messageContent) {
6666
if ($messageContent instanceof Text) {
67-
$content .= $messageContent->text;
67+
$content .= $messageContent->getText();
6868
}
6969
}
7070
}

src/agent/src/Toolbox/AgentProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private function handleToolCallsCallback(Output $output): \Closure
8989
return function (ToolCallResult $result, ?AssistantMessage $streamedAssistantResponse = null) use ($output): ResultInterface {
9090
$messages = $this->keepToolMessages ? $output->getMessageBag() : clone $output->getMessageBag();
9191

92-
if (null !== $streamedAssistantResponse && '' !== $streamedAssistantResponse->content) {
92+
if (null !== $streamedAssistantResponse && '' !== $streamedAssistantResponse->getContent()) {
9393
$messages->add($streamedAssistantResponse);
9494
}
9595

src/agent/tests/InputProcessor/SystemPromptInputProcessorTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testProcessInputAddsSystemMessageWhenNoneExists()
4141
$this->assertCount(2, $messages);
4242
$this->assertInstanceOf(SystemMessage::class, $messages[0]);
4343
$this->assertInstanceOf(UserMessage::class, $messages[1]);
44-
$this->assertSame('This is a system prompt', $messages[0]->content);
44+
$this->assertSame('This is a system prompt', $messages[0]->getContent());
4545
}
4646

4747
public function testProcessInputDoesNotAddSystemMessageWhenOneExists()
@@ -59,7 +59,7 @@ public function testProcessInputDoesNotAddSystemMessageWhenOneExists()
5959
$this->assertCount(2, $messages);
6060
$this->assertInstanceOf(SystemMessage::class, $messages[0]);
6161
$this->assertInstanceOf(UserMessage::class, $messages[1]);
62-
$this->assertSame('This is already a system prompt', $messages[0]->content);
62+
$this->assertSame('This is already a system prompt', $messages[0]->getContent());
6363
}
6464

6565
public function testDoesNotIncludeToolsIfToolboxIsEmpty()
@@ -86,7 +86,7 @@ public function execute(ToolCall $toolCall): mixed
8686
$this->assertCount(2, $messages);
8787
$this->assertInstanceOf(SystemMessage::class, $messages[0]);
8888
$this->assertInstanceOf(UserMessage::class, $messages[1]);
89-
$this->assertSame('This is a system prompt', $messages[0]->content);
89+
$this->assertSame('This is a system prompt', $messages[0]->getContent());
9090
}
9191

9292
public function testIncludeToolDefinitions()
@@ -138,7 +138,7 @@ public function execute(ToolCall $toolCall): mixed
138138
## tool_required_params
139139
A tool with required parameters
140140
or not
141-
PROMPT, $messages[0]->content);
141+
PROMPT, $messages[0]->getContent());
142142
}
143143

144144
public function testWithStringableSystemPrompt()
@@ -176,7 +176,7 @@ public function execute(ToolCall $toolCall): mixed
176176
177177
## tool_no_params
178178
A tool without parameters
179-
PROMPT, $messages[0]->content);
179+
PROMPT, $messages[0]->getContent());
180180
}
181181

182182
public function testWithTranslatedSystemPrompt()
@@ -190,7 +190,7 @@ public function testWithTranslatedSystemPrompt()
190190
$this->assertCount(2, $messages);
191191
$this->assertInstanceOf(SystemMessage::class, $messages[0]);
192192
$this->assertInstanceOf(UserMessage::class, $messages[1]);
193-
$this->assertSame('This is a cool translated system prompt', $messages[0]->content);
193+
$this->assertSame('This is a cool translated system prompt', $messages[0]->getContent());
194194
}
195195

196196
public function testWithTranslationDomainSystemPrompt()
@@ -207,7 +207,7 @@ public function testWithTranslationDomainSystemPrompt()
207207
$messages = $input->getMessageBag()->getMessages();
208208
$this->assertCount(1, $messages);
209209
$this->assertInstanceOf(SystemMessage::class, $messages[0]);
210-
$this->assertSame('This is a cool translated system prompt with a translation domain', $messages[0]->content);
210+
$this->assertSame('This is a cool translated system prompt with a translation domain', $messages[0]->getContent());
211211
}
212212

213213
public function testWithMissingTranslator()
@@ -237,7 +237,7 @@ public function testProcessInputWithFile()
237237
$this->assertCount(2, $messages);
238238
$this->assertInstanceOf(SystemMessage::class, $messages[0]);
239239
$this->assertInstanceOf(UserMessage::class, $messages[1]);
240-
$this->assertSame('This is a system prompt from a file', $messages[0]->content);
240+
$this->assertSame('This is a system prompt from a file', $messages[0]->getContent());
241241
} finally {
242242
unlink($tempFile);
243243
}
@@ -258,7 +258,7 @@ public function testProcessInputWithMultilineFile()
258258
$messages = $input->getMessageBag()->getMessages();
259259
$this->assertCount(2, $messages);
260260
$this->assertInstanceOf(SystemMessage::class, $messages[0]);
261-
$this->assertSame("Line 1\nLine 2\nLine 3", $messages[0]->content);
261+
$this->assertSame("Line 1\nLine 2\nLine 3", $messages[0]->getContent());
262262
} finally {
263263
unlink($tempFile);
264264
}

src/agent/tests/Memory/MemoryInputProcessorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testItIsAddingMemoryToSystemPrompt()
8282
8383
You are a helpful and kind assistant.
8484
MARKDOWN,
85-
$input->getMessageBag()->getSystemMessage()->content,
85+
$input->getMessageBag()->getSystemMessage()->getContent(),
8686
);
8787
}
8888

@@ -108,7 +108,7 @@ public function testItIsAddingMemoryToSystemPromptEvenItIsEmpty()
108108
109109
First memory content
110110
MARKDOWN,
111-
$input->getMessageBag()->getSystemMessage()->content,
111+
$input->getMessageBag()->getSystemMessage()->getContent(),
112112
);
113113
}
114114

@@ -135,7 +135,7 @@ public function testItIsAddingMultipleMemoryFromSingleProviderToSystemPrompt()
135135
First memory content
136136
Second memory content
137137
MARKDOWN,
138-
$input->getMessageBag()->getSystemMessage()->content,
138+
$input->getMessageBag()->getSystemMessage()->getContent(),
139139
);
140140
}
141141

@@ -151,6 +151,6 @@ public function testItIsNotAddingAnythingIfMemoryWasEmpty()
151151
$memoryInputProcessor->processInput($input = new Input('gpt-4', new MessageBag(), []));
152152

153153
$this->assertArrayNotHasKey('use_memory', $input->getOptions());
154-
$this->assertNull($input->getMessageBag()->getSystemMessage()?->content);
154+
$this->assertNull($input->getMessageBag()->getSystemMessage()?->getContent());
155155
}
156156
}

0 commit comments

Comments
 (0)