User input requests
Configure theonUserInputRequest handler and the agent can ask the user questions through the ask_user tool.
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Types\UserInputRequest;
use Revolution\Copilot\Types\UserInputResponse;
Copilot::start(function (CopilotSession $session) {
$response = $session->sendAndWait(prompt: 'Ask the user which language they prefer');
dump($response->content());
}, config: [
'model' => 'gpt-5',
'onUserInputRequest' => function (UserInputRequest $request): UserInputResponse {
// $request->question - the question text
// $request->choices - an optional array of multiple-choice choices
// $request->allowFreeform - whether freeform input is allowed (default: true)
dump("Question from agent: {$request->question}");
if ($request->choices) {
dump('Choices: '.implode(', ', $request->choices));
}
// Return the user's answer
return new UserInputResponse(
answer: 'PHP',
wasFreeform: true, // Whether the answer was freeform rather than from a choice
);
},
]);
UserInputRequest class
The user input request from the agent.| Property | Type | Description |
|---|---|---|
question | string | The question to ask the user |
choices | ?array | Multiple-choice options (optional) |
allowFreeform | ?bool | Whether freeform input is allowed (default: true) |
UserInputResponse class
The response to a user input request.| Property | Type | Description |
|---|---|---|
answer | string | The user’s answer |
wasFreeform | bool | Whether the answer was freeform (true when it wasn’t chosen from the given choices) |
Practical example
Using it in an interactive command:use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Types\UserInputRequest;
use Revolution\Copilot\Types\UserInputResponse;
// Inside an Artisan command
Copilot::start(function (CopilotSession $session) use ($command) {
$response = $session->sendAndWait(prompt: 'Configure the project settings');
$command->info($response->content());
}, config: [
'onUserInputRequest' => function (UserInputRequest $request) use ($command): UserInputResponse {
if ($request->choices) {
// Use the choice method when choices are provided
$answer = $command->choice(
$request->question,
$request->choices,
$request->choices[0] ?? null
);
return new UserInputResponse(answer: $answer, wasFreeform: false);
}
// Freeform input
$answer = $command->ask($request->question);
return new UserInputResponse(answer: $answer, wasFreeform: true);
},
]);
For the latest updates, see the GitHub repository.