diff --git a/src/agent/src/Agent.php b/src/agent/src/Agent.php index 15722c7d9..42a4e9456 100644 --- a/src/agent/src/Agent.php +++ b/src/agent/src/Agent.php @@ -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 diff --git a/src/agent/src/AgentInterface.php b/src/agent/src/AgentInterface.php index 3ad1f7ec1..ab1b90969 100644 --- a/src/agent/src/AgentInterface.php +++ b/src/agent/src/AgentInterface.php @@ -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; } diff --git a/src/agent/tests/AgentTest.php b/src/agent/tests/AgentTest.php index 06d4b8e34..f83dfceec 100644 --- a/src/agent/tests/AgentTest.php +++ b/src/agent/tests/AgentTest.php @@ -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; @@ -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); diff --git a/src/ai-bundle/src/Command/ChatCommand.php b/src/ai-bundle/src/Command/ChatCommand.php index a58cb77f7..7d093f7f8 100644 --- a/src/ai-bundle/src/Command/ChatCommand.php +++ b/src/ai-bundle/src/Command/ChatCommand.php @@ -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("System prompt: $systemMessage"); + } $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'); @@ -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('Assistant:'); $io->writeln('');