Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/agent/src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public function call(MessageBag $messages, array $options = []): ResultInterface
return $output->result;
}

public function getSystemMessage(): ?string
{
$input = new Input($this->model, new MessageBag(), []);

array_map(fn (InputProcessorInterface $processor) => $processor->processInput($input), $this->inputProcessors);

return $input->messages->getSystemMessage()?->content;
}

/**
* @param InputProcessorInterface[]|OutputProcessorInterface[] $processors
* @param class-string $interface
Expand Down
2 changes: 2 additions & 0 deletions src/agent/src/AgentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public function call(MessageBag $messages, array $options = []): ResultInterface
* Get the agent's name, which can be used for debugging or multi-agent configuration.
*/
public function getName(): string;

public function getSystemMessage(): ?string;
}
43 changes: 43 additions & 0 deletions src/agent/tests/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Symfony\AI\Platform\Message\Content\Audio;
use Symfony\AI\Platform\Message\Content\Image;
use Symfony\AI\Platform\Message\Content\Text;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Message\UserMessage;
use Symfony\AI\Platform\Model;
Expand Down Expand Up @@ -406,6 +407,48 @@ public function testConstructorAcceptsTraversableProcessors()
$this->assertInstanceOf(AgentInterface::class, $agent);
}

public function testGetSystemMessageReturnsExpectedValue()
{
$platform = $this->createMock(PlatformInterface::class);
$model = $this->createMock(Model::class);
$expectedSystemMessage = 'System prompt here';

$inputProcessor = $this->createMock(InputProcessorInterface::class);
$outputProcessor = $this->createMock(OutputProcessorInterface::class);

$inputProcessor->expects($this->once())
->method('processInput')
->willReturnCallback(function (Input $input) use ($expectedSystemMessage) {
$input->messages->add(Message::forSystem($expectedSystemMessage));
});

$agent = new Agent($platform, $model, new \ArrayIterator([$inputProcessor]), new \ArrayIterator([$outputProcessor]));

$systemMessage = $agent->getSystemMessage();

$this->assertSame($expectedSystemMessage, $systemMessage);
}

public function testGetSystemMessageReturnsNullIfNoSystemMessageSet()
{
$platform = $this->createMock(PlatformInterface::class);
$model = $this->createMock(Model::class);

$inputProcessor = $this->createMock(InputProcessorInterface::class);
$outputProcessor = $this->createMock(OutputProcessorInterface::class);

$inputProcessor->expects($this->once())
->method('processInput')
->willReturnCallback(function (Input $input) {
});

$agent = new Agent($platform, $model, new \ArrayIterator([$inputProcessor]), new \ArrayIterator([$outputProcessor]));

$systemMessage = $agent->getSystemMessage();

$this->assertNull($systemMessage);
}

public function testGetNameReturnsDefaultName()
{
$platform = $this->createMock(PlatformInterface::class);
Expand Down
12 changes: 4 additions & 8 deletions src/ai-bundle/src/Command/ChatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$agent = $this->agents->get($agentName);
$systemMessage = $agent->getSystemMessage();

// Now start the chat
$io = new SymfonyStyle($input, $output);

$io->title(\sprintf('Chat with %s Agent', $agentName));
if (null !== $systemMessage) {
$io->writeln("<comment>System prompt: $systemMessage</comment>");
}
$io->info('Type your message and press Enter. Type "exit" or "quit" to end the conversation.');
$io->newLine();

$messages = new MessageBag();
$systemPromptDisplayed = false;

while (true) {
$userInput = $io->ask('You');
Expand All @@ -155,13 +158,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
try {
$result = $agent->call($messages);

// Display system prompt after first successful call
if (!$systemPromptDisplayed && null !== ($systemMessage = $messages->getSystemMessage())) {
$io->section('System Prompt');
$io->block($systemMessage->content, null, 'fg=gray', ' ', true);
$systemPromptDisplayed = true;
}

if ($result instanceof TextResult) {
$io->write('<fg=yellow>Assistant</>:');
$io->writeln('');
Expand Down
Loading