> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# 会话恢复

> 使用 listSessions()、固定 session ID、ResumeSessionConfig 以及最后一次会话恢复，来还原 Laravel Copilot SDK 的会话。

## 会话恢复

如何恢复之前的会话。

## 在 Artisan 命令中以对话形式使用聊天时

```php theme={null}
// 用 listSessions 获取以往的会话列表
$sessions = Copilot::client()->listSessions();

// 用 Laravel\Prompts\select 等选择要恢复的 session ID
$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,
);

// 用选中的 ID 恢复会话
$session = Copilot::client()->resumeSession($session_id);

// 获取以往的消息
$messages = $session->getMessages();
```

## 使用固定的 session ID 的情况

如果不是对话形式就无法进行选择，因此从一开始就指定固定的 ID。你可以准备与用户 ID 或某种上下文对应的 ID。
例如在 Web 服务上多用户使用时，就使用这种事先准备 ID 的方式。

```php theme={null}
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);
```

在恢复时，如果使用 `Copilot::start`，也可以通过 `resume` 参数指定。

```php theme={null}
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\ResumeSessionConfig;

// ResumeSessionConfig 中不能指定 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');
```

也可以在 `Copilot::start` 的闭包内销毁当前会话，然后重新恢复。

```php theme={null}
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.');
});
```

## 恢复最后一次会话

如果会话不存在，`getLastSessionId()` 会返回 `null`，自动作为新会话启动。也可以切换 `SessionConfig`。

```php theme={null}
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);
```

## 使用 SessionMetadata

如果会话不存在，`getSessionMetadata()` 也会返回 `null`，同样会作为新会话启动。还可以获取会话的创建时间和更新时间等。

```php theme={null}
use Revolution\Copilot\Types\SessionMetadata;
use Revolution\Copilot\Facades\Copilot;

$meta = Copilot::client()->getSessionMetadata('user-123-conversation');
```

<Info>
  最新信息请参考 [GitHub 仓库](https://github.com/invokable/laravel-copilot-sdk)。
</Info>


## Related topics

- [代理循环](/zh/packages/laravel-copilot-sdk/agent-loop.md)
- [会话钩子](/zh/packages/laravel-copilot-sdk/hooks.md)
- [SessionConfig](/zh/packages/laravel-copilot-sdk/session-config.md)
- [流式事件](/zh/packages/laravel-copilot-sdk/streaming-events.md)
- [Telemetry](/zh/packages/laravel-copilot-sdk/telemetry.md)
