Zum Hauptinhalt springen

Benutzereingabe-Anfrage

Wenn Sie den Handler onUserInputRequest konfigurieren, kann der Agent den Benutzer über das Tool ask_user befragen.
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: 'ユーザーに好きな言語を聞いて');
    dump($response->content());
}, config: [
    'model' => 'gpt-5',
    'onUserInputRequest' => function (UserInputRequest $request): UserInputResponse {
        // $request->question - Inhalt der Frage
        // $request->choices - Array mit Auswahlmöglichkeiten (optional)
        // $request->allowFreeform - Ob Freitext erlaubt ist (Standard: true)

        dump("Question from agent: {$request->question}");
        if ($request->choices) {
            dump('Choices: '.implode(', ', $request->choices));
        }

        // Antwort des Benutzers zurückgeben
        return new UserInputResponse(
            answer: 'PHP',
            wasFreeform: true, // War es Freitext statt Auswahl?
        );
    },
]);

Klasse UserInputRequest

Eine Benutzereingabe-Anfrage vom Agenten.
EigenschaftTypBeschreibung
questionstringDie an den Benutzer zu stellende Frage
choices?arrayAuswahlmöglichkeiten bei Mehrfachauswahl (optional)
allowFreeform?boolOb Freitext erlaubt ist (Standard: true)

Klasse UserInputResponse

Die Antwort auf eine Benutzereingabe-Anfrage.
EigenschaftTypBeschreibung
answerstringDie Antwort des Benutzers
wasFreeformboolOb die Antwort per Freitext erfolgte (true, wenn nicht aus Auswahlmöglichkeiten)

Praxisbeispiel

Verwendung in einem interaktiven Command.
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Types\UserInputRequest;
use Revolution\Copilot\Types\UserInputResponse;

// Innerhalb eines Artisan-Commands
Copilot::start(function (CopilotSession $session) use ($command) {
    $response = $session->sendAndWait(prompt: 'プロジェクトの設定を行います');
    $command->info($response->content());
}, config: [
    'onUserInputRequest' => function (UserInputRequest $request) use ($command): UserInputResponse {
        if ($request->choices) {
            // Bei vorhandenen Auswahlmöglichkeiten die Methode choice verwenden
            $answer = $command->choice(
                $request->question,
                $request->choices,
                $request->choices[0] ?? null
            );

            return new UserInputResponse(answer: $answer, wasFreeform: false);
        }

        // Freitext
        $answer = $command->ask($request->question);

        return new UserInputResponse(answer: $answer, wasFreeform: true);
    },
]);
Aktuelle Informationen finden Sie im GitHub-Repository.
Zuletzt geändert am 13. Juli 2026