Session send() and on()
sendAndWait() is easy to understand because it returns quickly, but you only receive the final assistant message.
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
Copilot::start(function (CopilotSession $session) {
$response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
dump($response->content());
});
If you also want intermediate messages, register an event listener with on().
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
Copilot::start(function (CopilotSession $session) {
$session->on(function (SessionEvent $event): void {
if ($event->isAssistantMessage()) {
dump($event->content());
} else {
dump($event);
}
});
$message_id = $session->send(prompt: 'Tell me something about Laravel.');
// Wait for incoming messages in a loop
$session->wait(timeout: 60.0);
});
In PHP, this can be hard to read, so combining on() with sendAndWait() is recommended.
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
Copilot::start(function (CopilotSession $session) {
$session->on(function (SessionEvent $event): void {
if ($event->isAssistantMessage()) {
dump($event->content());
} else {
dump($event);
}
});
$response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
// The wait loop is handled inside sendAndWait(), so by this point
// you have already received messages up to the final one.
// Intermediate messages are handled by on() above.
// The final message from sendAndWait() is unnecessary here.
// dump($response->content());
});
Subscribe to a specific event type with on()
You can subscribe to only one event by passing a SessionEventType enum value or a string.
use Revolution\Copilot\Enums\SessionEventType;
use Revolution\Copilot\Types\SessionEvent;
$session->on(SessionEventType::ASSISTANT_MESSAGE, function (SessionEvent $event): void {
dump($event->content());
});
$session->on('assistant.message', function (SessionEvent $event): void {
dump($event->content());
});
Subscribe to all event types
If you do not specify an event type, it subscribes to all events.
use Revolution\Copilot\Enums\SessionEventType;
use Revolution\Copilot\Types\SessionEvent;
$session->on(function (SessionEvent $event): void {
});
// You can also use named arguments
$session->on(handler: function (SessionEvent $event): void {
});
// Dynamic type assignment that allows null is also possible.
$type = null;
$session->on(type: $type, handler: function (SessionEvent $event): void {
});
Last modified on April 19, 2026