Skip to content

Commit c245522

Browse files
committed
[Platform] Add UserMessage::asText() method
1 parent 637012d commit c245522

File tree

6 files changed

+73
-30
lines changed

6 files changed

+73
-30
lines changed

src/agent/src/MultiAgent/MultiAgent.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ public function call(MessageBag $messages, array $options = []): ResultInterface
6666
{
6767
$userMessages = $messages->withoutSystemMessage();
6868

69-
$userText = $userMessages->getUserMessageText();
70-
if (null === $userText) {
69+
$userMessage = $userMessages->getUserMessage();
70+
if (null === $userMessage) {
7171
throw new RuntimeException('No user message found in conversation.');
7272
}
73+
$userText = $userMessage->asText();
7374
$this->logger->debug('MultiAgent: Processing user message', ['user_text' => $userText]);
7475

7576
$this->logger->debug('MultiAgent: Available agents for routing', ['agents' => array_map(fn ($handoff) => [
@@ -120,11 +121,6 @@ public function call(MessageBag $messages, array $options = []): ResultInterface
120121

121122
$this->logger->debug('MultiAgent: Delegating to agent', ['agent_name' => $decision->agentName]);
122123

123-
$userMessage = $userMessages->getUserMessage();
124-
if (null === $userMessage) {
125-
throw new RuntimeException('No user message found in conversation.');
126-
}
127-
128124
// Call the selected agent with the original user question
129125
return $targetAgent->call(new MessageBag($userMessage), $options);
130126
}

src/agent/tests/MultiAgent/MultiAgentTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ public function testBuildAgentSelectionPromptIncludesFallback()
353353
->method('call')
354354
->with(
355355
$this->callback(function (MessageBag $messages) {
356-
$text = $messages->getUserMessageText();
356+
$userMessage = $messages->getUserMessage();
357+
$text = $userMessage?->asText();
357358

358359
return str_contains($text, 'general-fallback: fallback agent for general/unmatched queries');
359360
}),

src/platform/src/Message/MessageBag.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\AI\Platform\Message;
1313

14-
use Symfony\AI\Platform\Message\Content\Text;
1514
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
1615

1716
/**
@@ -66,23 +65,6 @@ public function getUserMessage(): ?UserMessage
6665
return null;
6766
}
6867

69-
public function getUserMessageText(): ?string
70-
{
71-
$userMessage = $this->getUserMessage();
72-
if (null === $userMessage) {
73-
return null;
74-
}
75-
76-
$textParts = [];
77-
foreach ($userMessage->content as $content) {
78-
if ($content instanceof Text) {
79-
$textParts[] = $content->text;
80-
}
81-
}
82-
83-
return implode(' ', $textParts);
84-
}
85-
8668
public function with(MessageInterface $message): self
8769
{
8870
$messages = clone $this;

src/platform/src/Message/UserMessage.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\AI\Platform\Message\Content\ContentInterface;
1616
use Symfony\AI\Platform\Message\Content\Image;
1717
use Symfony\AI\Platform\Message\Content\ImageUrl;
18+
use Symfony\AI\Platform\Message\Content\Text;
1819
use Symfony\Component\Uid\AbstractUid;
1920
use Symfony\Component\Uid\TimeBasedUidInterface;
2021
use Symfony\Component\Uid\Uuid;
@@ -69,4 +70,20 @@ public function hasImageContent(): bool
6970

7071
return false;
7172
}
73+
74+
public function asText(): ?string
75+
{
76+
$textParts = [];
77+
foreach ($this->content as $content) {
78+
if ($content instanceof Text) {
79+
$textParts[] = $content->text;
80+
}
81+
}
82+
83+
if ([] === $textParts) {
84+
return null;
85+
}
86+
87+
return implode(' ', $textParts);
88+
}
7289
}

src/platform/tests/Message/MessageBagTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ public function testGetUserMessageText()
216216
Message::ofAssistant('How can I help you?'),
217217
);
218218

219-
$userText = $messageBag->getUserMessageText();
219+
$userMessage = $messageBag->getUserMessage();
220+
$userText = $userMessage?->asText();
220221

221222
$this->assertSame('Hello, world!', $userText);
222223
}
@@ -228,7 +229,9 @@ public function testGetUserMessageTextReturnsNullWithoutUserMessage()
228229
Message::ofAssistant('It is time to sleep.'),
229230
);
230231

231-
$this->assertNull($messageBag->getUserMessageText());
232+
$userMessage = $messageBag->getUserMessage();
233+
234+
$this->assertNull($userMessage?->asText());
232235
}
233236

234237
public function testGetUserMessageTextWithMultipleTextParts()
@@ -239,7 +242,8 @@ public function testGetUserMessageTextWithMultipleTextParts()
239242
Message::ofAssistant('Response'),
240243
);
241244

242-
$userText = $messageBag->getUserMessageText();
245+
$userMessage = $messageBag->getUserMessage();
246+
$userText = $userMessage?->asText();
243247

244248
$this->assertSame('Part one Part two Part three', $userText);
245249
}
@@ -252,7 +256,8 @@ public function testGetUserMessageTextIgnoresNonTextContent()
252256
Message::ofAssistant('Response'),
253257
);
254258

255-
$userText = $messageBag->getUserMessageText();
259+
$userMessage = $messageBag->getUserMessage();
260+
$userText = $userMessage?->asText();
256261

257262
// Should only return the text content, ignoring the image
258263
$this->assertSame('Text content', $userText);

src/platform/tests/Message/UserMessageTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,46 @@ public function testMessageIdImplementsRequiredInterfaces()
108108
$this->assertInstanceOf(TimeBasedUidInterface::class, $message->getId());
109109
$this->assertInstanceOf(UuidV7::class, $message->getId());
110110
}
111+
112+
public function testAsTextWithSingleTextContent()
113+
{
114+
$message = new UserMessage(new Text('Hello, world!'));
115+
116+
$this->assertSame('Hello, world!', $message->asText());
117+
}
118+
119+
public function testAsTextWithMultipleTextParts()
120+
{
121+
$message = new UserMessage(new Text('Part one'), new Text('Part two'), new Text('Part three'));
122+
123+
$this->assertSame('Part one Part two Part three', $message->asText());
124+
}
125+
126+
public function testAsTextIgnoresNonTextContent()
127+
{
128+
$message = new UserMessage(
129+
new Text('Text content'),
130+
new ImageUrl('http://example.com/image.png'),
131+
new Text('More text')
132+
);
133+
134+
$this->assertSame('Text content More text', $message->asText());
135+
}
136+
137+
public function testAsTextWithoutTextContent()
138+
{
139+
$message = new UserMessage(new ImageUrl('http://example.com/image.png'));
140+
141+
$this->assertNull($message->asText());
142+
}
143+
144+
public function testAsTextWithAudioAndImage()
145+
{
146+
$message = new UserMessage(
147+
Audio::fromFile(\dirname(__DIR__, 4).'/fixtures/audio.mp3'),
148+
new ImageUrl('http://example.com/image.png')
149+
);
150+
151+
$this->assertNull($message->asText());
152+
}
111153
}

0 commit comments

Comments
 (0)