Skip to content

Commit c0e7f3c

Browse files
committed
[AI Bundle] Improve ChatCommand
1 parent bf3d1fa commit c0e7f3c

File tree

1 file changed

+9
-27
lines changed

1 file changed

+9
-27
lines changed

src/ai-bundle/src/Command/ChatCommand.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
)]
3737
final class ChatCommand extends Command
3838
{
39-
private AgentInterface $agent;
40-
private string $agentName;
41-
4239
/**
4340
* @param ServiceLocator<AgentInterface> $agents
4441
*/
@@ -58,7 +55,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
5855
protected function configure(): void
5956
{
6057
$this
61-
->addArgument('agent', InputArgument::OPTIONAL, 'The name of the agent to chat with')
58+
->addArgument('agent', InputArgument::REQUIRED, 'The name of the agent to chat with')
6259
->setHelp(
6360
<<<'HELP'
6461
The <info>%command.name%</info> command allows you to chat with different agents.
@@ -70,7 +67,7 @@ protected function configure(): void
7067
<info>%command.full_name% wikipedia</info>
7168
7269
If no agent is specified, you'll be prompted to select one interactively.
73-
70+
7471
The chat session is interactive. Type your messages and press Enter to send.
7572
Type 'exit' or 'quit' to end the conversation.
7673
HELP
@@ -79,11 +76,6 @@ protected function configure(): void
7976

8077
protected function interact(InputInterface $input, OutputInterface $output): void
8178
{
82-
// Skip interaction in non-interactive mode
83-
if (!$input->isInteractive()) {
84-
return;
85-
}
86-
8779
$agentArg = $input->getArgument('agent');
8880

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

114-
protected function initialize(InputInterface $input, OutputInterface $output): void
115-
{
116-
// Initialization will be done in execute() after interact() has run
117-
}
118-
119106
protected function execute(InputInterface $input, OutputInterface $output): int
120107
{
121108
// Initialize agent (moved from initialize() to execute() so it runs after interact())
@@ -126,29 +113,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
126113
}
127114

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

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

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

146-
$this->agent = $this->agents->get($this->agentName);
128+
$agent = $this->agents->get($agentName);
147129

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

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

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

172154
try {
173-
$result = $this->agent->call($messages);
155+
$result = $agent->call($messages);
174156

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

0 commit comments

Comments
 (0)