36
36
)]
37
37
final class ChatCommand extends Command
38
38
{
39
- private AgentInterface $ agent ;
40
- private string $ agentName ;
41
-
42
39
/**
43
40
* @param ServiceLocator<AgentInterface> $agents
44
41
*/
@@ -58,7 +55,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
58
55
protected function configure (): void
59
56
{
60
57
$ 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 ' )
62
59
->setHelp (
63
60
<<<'HELP'
64
61
The <info>%command.name%</info> command allows you to chat with different agents.
@@ -70,7 +67,7 @@ protected function configure(): void
70
67
<info>%command.full_name% wikipedia</info>
71
68
72
69
If no agent is specified, you'll be prompted to select one interactively.
73
-
70
+
74
71
The chat session is interactive. Type your messages and press Enter to send.
75
72
Type 'exit' or 'quit' to end the conversation.
76
73
HELP
@@ -79,11 +76,6 @@ protected function configure(): void
79
76
80
77
protected function interact (InputInterface $ input , OutputInterface $ output ): void
81
78
{
82
- // Skip interaction in non-interactive mode
83
- if (!$ input ->isInteractive ()) {
84
- return ;
85
- }
86
-
87
79
$ agentArg = $ input ->getArgument ('agent ' );
88
80
89
81
// If agent is already provided and valid, nothing to do
@@ -111,11 +103,6 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
111
103
$ input ->setArgument ('agent ' , $ selectedAgent );
112
104
}
113
105
114
- protected function initialize (InputInterface $ input , OutputInterface $ output ): void
115
- {
116
- // Initialization will be done in execute() after interact() has run
117
- }
118
-
119
106
protected function execute (InputInterface $ input , OutputInterface $ output ): int
120
107
{
121
108
// Initialize agent (moved from initialize() to execute() so it runs after interact())
@@ -126,29 +113,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
126
113
}
127
114
128
115
$ 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 : '' ;
135
117
136
118
// 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 )));
139
121
}
140
122
141
123
// If we still don't have an agent name at this point, something went wrong
142
- if (!$ this -> agentName ) {
124
+ if (!$ agentName ) {
143
125
throw new InvalidArgumentException (\sprintf ('Agent name is required. Available agents: "%s" ' , implode (', ' , $ availableAgents )));
144
126
}
145
127
146
- $ this -> agent = $ this ->agents ->get ($ this -> agentName );
128
+ $ agent = $ this ->agents ->get ($ agentName );
147
129
148
130
// Now start the chat
149
131
$ io = new SymfonyStyle ($ input , $ output );
150
132
151
- $ io ->title (\sprintf ('Chat with %s Agent ' , $ this -> agentName ));
133
+ $ io ->title (\sprintf ('Chat with %s Agent ' , $ agentName ));
152
134
$ io ->info ('Type your message and press Enter. Type "exit" or "quit" to end the conversation. ' );
153
135
$ io ->newLine ();
154
136
@@ -170,7 +152,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
170
152
$ messages ->add (Message::ofUser ($ userInput ));
171
153
172
154
try {
173
- $ result = $ this -> agent ->call ($ messages );
155
+ $ result = $ agent ->call ($ messages );
174
156
175
157
// Display system prompt after first successful call
176
158
if (!$ systemPromptDisplayed && null !== ($ systemMessage = $ messages ->getSystemMessage ())) {
0 commit comments