Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
Resume session
How to resume a previous session.
When using interactive chat in an Artisan command
// Get previous sessions with listSessions
$sessions = Copilot::client()->listSessions();
// Choose a session ID to resume with Laravel\Prompts\select
$sessions = collect(Copilot::client()->listSessions())
->mapWithKeys(function (SessionMetadata $session) {
return [$session->sessionId => $session->summary ?? ''];
})
->toArray();
$session_id = select(
label: 'What session do you want to resume?',
options: $sessions,
);
// Resume the session with the selected ID
$session = Copilot::client()->resumeSession($session_id);
// Get previous messages
$messages = $session->getMessages();
When using a fixed session ID
If your flow is not interactive, specify a fixed ID from the beginning.
Create IDs based on user IDs or other context.
For example, this works well in a multi-user web service.
use Revolution\Copilot\Types\SessionConfig;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
$config = new SessionConfig(
sessionId: 'user-123-conversation',
);
Copilot::start(function (CopilotSession $session) {
dump('Starting Copilot session: '.$session->id());
$response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
}, config: $config);
When resuming, you can also pass resume to Copilot::start.
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\ResumeSessionConfig;
// ResumeSessionConfig does not allow setting sessionId
$config = new ResumeSessionConfig();
Copilot::start(function (CopilotSession $session) {
dump('Starting Copilot session: '.$session->id());
$response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
}, config: $config, resume: 'user-123-conversation');
You can also discard the current session inside the Copilot::start closure and resume another one.
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
Copilot::start(function (CopilotSession $session) {
dump('Starting Copilot session: '.$session->id());
$session->disconnect();
$session = Copilot::client()->resumeSession(sessionId: 'user-123-conversation');
$response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
});
Resume the last session
If no session exists, getLastSessionId() returns null, and the session starts as new.
You can switch between SessionConfig and ResumeSessionConfig.
use Revolution\Copilot\Types\SessionConfig;
use Revolution\Copilot\Types\ResumeSessionConfig;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
$session_id = Copilot::client()->getLastSessionId();
if (empty($session_id)) {
$config = new SessionConfig();
} else {
$config = new ResumeSessionConfig();
}
Copilot::start(function (CopilotSession $session) {
dump('Starting Copilot session: '.$session->id());
$response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
}, config: $config, resume: $session_id);
getSessionMetadata() also returns null when the session does not exist, so you can use the same new-session fallback.
It also provides metadata such as created/updated timestamps.
use Revolution\Copilot\Types\SessionMetadata;
use Revolution\Copilot\Facades\Copilot;
$meta = Copilot::client()->getSessionMetadata('user-123-conversation');