Skip to content
Merged
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
36 changes: 9 additions & 27 deletions src/ai-bundle/src/Command/ChatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
)]
final class ChatCommand extends Command
{
private AgentInterface $agent;
private string $agentName;

/**
* @param ServiceLocator<AgentInterface> $agents
*/
Expand All @@ -58,7 +55,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
protected function configure(): void
{
$this
->addArgument('agent', InputArgument::OPTIONAL, 'The name of the agent to chat with')
->addArgument('agent', InputArgument::REQUIRED, 'The name of the agent to chat with')
->setHelp(
<<<'HELP'
The <info>%command.name%</info> command allows you to chat with different agents.
Expand All @@ -70,7 +67,7 @@ protected function configure(): void
<info>%command.full_name% wikipedia</info>

If no agent is specified, you'll be prompted to select one interactively.

The chat session is interactive. Type your messages and press Enter to send.
Type 'exit' or 'quit' to end the conversation.
HELP
Expand All @@ -79,11 +76,6 @@ protected function configure(): void

protected function interact(InputInterface $input, OutputInterface $output): void
{
// Skip interaction in non-interactive mode
if (!$input->isInteractive()) {
return;
}

$agentArg = $input->getArgument('agent');

// If agent is already provided and valid, nothing to do
Expand Down Expand Up @@ -111,11 +103,6 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
$input->setArgument('agent', $selectedAgent);
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
// Initialization will be done in execute() after interact() has run
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
// Initialize agent (moved from initialize() to execute() so it runs after interact())
Expand All @@ -126,29 +113,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$agentArg = $input->getArgument('agent');
$this->agentName = \is_string($agentArg) ? $agentArg : '';

// In non-interactive mode, agent is required
if (!$this->agentName && !$input->isInteractive()) {
throw new InvalidArgumentException(\sprintf('Agent name is required. Available agents: "%s"', implode(', ', $availableAgents)));
}
$agentName = \is_string($agentArg) ? $agentArg : '';

// Validate that the agent exists if one was provided
if ($this->agentName && !$this->agents->has($this->agentName)) {
throw new InvalidArgumentException(\sprintf('Agent "%s" not found. Available agents: "%s"', $this->agentName, implode(', ', $availableAgents)));
if ($agentName && !$this->agents->has($agentName)) {
throw new InvalidArgumentException(\sprintf('Agent "%s" not found. Available agents: "%s"', $agentName, implode(', ', $availableAgents)));
}

// If we still don't have an agent name at this point, something went wrong
if (!$this->agentName) {
if (!$agentName) {
throw new InvalidArgumentException(\sprintf('Agent name is required. Available agents: "%s"', implode(', ', $availableAgents)));
}

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

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

$io->title(\sprintf('Chat with %s Agent', $this->agentName));
$io->title(\sprintf('Chat with %s Agent', $agentName));
$io->info('Type your message and press Enter. Type "exit" or "quit" to end the conversation.');
$io->newLine();

Expand All @@ -170,7 +152,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$messages->add(Message::ofUser($userInput));

try {
$result = $this->agent->call($messages);
$result = $agent->call($messages);

// Display system prompt after first successful call
if (!$systemPromptDisplayed && null !== ($systemMessage = $messages->getSystemMessage())) {
Expand Down