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

# SessionConfig

> 透過 Laravel Copilot SDK 的 SessionConfig 設定工具、MCP、Hooks、Agent 與生命週期事件。

## SessionConfig

透過 `SessionConfig` 類別可進行各種設定。

使用方式如 `Copilot::run(prompt: '...', config: $config)` 或 `Copilot::start(function (CopilotSession $session) { ... }, config: $config)`。
若只是想單純指定模型等,無需使用 `SessionConfig` 類別時,也可以以陣列形式指定,例如:`Copilot::run(prompt: '...', config: ['model' => 'auto'])`。

```php theme={null}
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\SessionConfig;
use Revolution\Copilot\Types\SystemMessageConfig;
use Revolution\Copilot\Types\InfiniteSessionConfig;
use Revolution\Copilot\Types\UserInputRequest;
use Revolution\Copilot\Types\Rpc\ModelCapabilitiesOverride;
use Revolution\Copilot\Types\Rpc\ModelCapabilitiesOverrideSupports;
use Revolution\Copilot\Types\Rpc\ModelCapabilitiesOverrideLimits;
use Revolution\Copilot\Enums\ReasoningEffort;

$config = new SessionConfig(
    // 指定要使用的模型
    model: 'auto',

    // 建立新 Session 時指定固定的 Session ID
    sessionId: 'session-123',

    clientName: 'my-app',

    // 推論等級。僅在支援的模型上可設定。
    reasoningEffort: ReasoningEffort::HIGH,

    // 覆寫模型的 capabilities。會與 runtime 的預設值進行 deep-merge。
    // modelCapabilities: new ModelCapabilitiesOverride(
    //     supports: new ModelCapabilitiesOverrideSupports(vision: true, reasoningEffort: true),
    //     limits: new ModelCapabilitiesOverrideLimits(max_prompt_tokens: 200000),
    // ),
    // 也可以用陣列指定
    // modelCapabilities: ['supports' => ['vision' => true]],

    // 覆寫 config 目錄設定
    configDirectory: '',

    // 啟用 MCP 設定或 Skill 目錄的自動探索
    // 會從 working directory 自動探索 .mcp.json 或 .vscode/mcp.json,
    // 並與明確指定的 mcpServers、skillDirectories 合併(名稱衝突時明確指定優先)
    enableConfigDiscovery: true,

    // 自訂工具
    tools: [...],

    // 系統訊息
    systemMessage: new SystemMessageConfig(
        content: 'You are a helpful assistant for Laravel developers.',
    ),

    // 系統訊息: replace 模式(完全取代系統提示)
    // systemMessage: new SystemMessageConfig(
    //     mode: 'replace',
    //     content: '完整自訂系統提示',
    // ),

    // 系統訊息: customize 模式(逐區塊覆寫)
    // systemMessage: new SystemMessageConfig(
    //     mode: 'customize',
    //     sections: [
    //         'tone' => new SectionOverride(action: SectionOverrideAction::REPLACE, content: 'Always respond in Japanese.'),
    //         'safety' => new SectionOverride(action: SectionOverrideAction::REMOVE),
    //         'custom_instructions' => new SectionOverride(action: SectionOverrideAction::APPEND, content: 'Additional instructions here.'),
    //     ],
    //     content: '與 append 模式相同的追加內容(選填)',
    // ),

    // 自訂 provider
    provider: new ProviderConfig(),

    // 每個 Session 的 GitHub Token(支援多租戶)
    // 除了 client 層級的 github_token 之外,可以在 Session 層級指定 Token
    gitHubToken: $user->github_token,

    // Remote Session 的運作模式
    // 'off' - 僅本地(預設)
    // 'export' - 將 Session 事件匯出至 GitHub(不啟用 Remote Steering)
    // 'on' - 同時啟用匯出與 Remote Steering
    remoteSession: \Revolution\Copilot\Enums\RemoteSessionMode::Export,

    // Cloud Session(在雲端而非本地建立 Session)
    // 可選擇關聯儲存庫資訊
    cloud: new \Revolution\Copilot\Types\CloudSessionOptions(
        repository: new \Revolution\Copilot\Types\CloudSessionRepository(
            owner: 'myorg',
            name: 'myrepo',
            branch: 'main',
        ),
    ),

    onPermissionRequest: function (array $request) {
        // 處理權限請求
    },

    onUserInputRequest: function (UserInputRequest $request) {
        // 處理使用者輸入請求。ask_user
    },

    // Elicitation 請求處理器
    // 設定後可接收來自 Agent 的表單式 UI 對話框請求
    onElicitationRequest: function (ElicitationContext $context) {
        // 處理 Elicitation 請求
        return ['action' => 'accept', 'content' => ['field' => 'value']];
    },

    // Plan 模式退出請求處理器
    // 設定後可接收 Agent 退出 Plan 模式時的請求
    onExitPlanModeRequest: function (ExitPlanModeRequest $request) {
        // 檢查 plan 內容並核准/拒絕
        return new ExitPlanModeResult(approved: true, selectedAction: $request->recommendedAction);
    },

    // 自動模式切換請求處理器
    // 可在達到 rate limit 時選擇是否允許切換至 auto 模式
    onAutoModeSwitchRequest: function (AutoModeSwitchRequest $request) {
        // 回傳 "yes"、"yes_always"、"no" 其中之一
        return 'yes';
    },

    // Session Hooks
    hooks: [],

    // Working directory
    workingDirectory: '',

    // 啟用 streaming
    streaming: true,

    // 是否將 sub-agent 的 streaming 事件納入主 stream(預設: true)
    includeSubAgentStreamingEvents: true,

    // 可用的內建工具
    availableTools: ['read_file', 'write_file'],

    // 或要排除的工具
    excludedTools: ['shell'],

    // MCP 伺服器設定
    mcpServers: [
        'github' => [
            'type' => 'http',
            'url' => 'https://api.githubcopilot.com/mcp/',
        ],
    ],

    // 自訂 Agent
    customAgents: [
        [
            'name' => 'reviewer',
            'displayName' => 'Code Reviewer',
            'description' => 'Reviews code for best practices',
            'prompt' => 'You are an expert code reviewer.',
        ],
    ],

    // 預設 Agent 設定。未選取自訂 Agent 時使用的內建 Agent 設定。
    // excludedTools: 要從預設 Agent 排除的工具清單。
    // 自訂 sub-agent 仍可繼續使用。
    defaultAgent: ['excludedTools' => ['tool_name']],

    // 指定 Session 開始時要啟用的 Agent。必須與 customAgents 的 name 相符。
    agent: 'reviewer',

    // Skill 目錄
    skillDirectories: [],

    // 停用的 Skill
    disabledSkills: [],

    // Infinite Session 設定。預設為啟用。
    infiniteSessions: new InfiniteSessionConfig(
        enabled: true,
        backgroundCompactionThreshold: 0.80, // 當 context 使用率達 80% 時開始壓縮
        bufferExhaustionThreshold: 0.95,     // 在壓縮完成前於 95% 時阻擋
    ),

    // 停用 Infinite Session
    // infiniteSessions: new InfiniteSessionConfig(enabled: false),

    // 在 Session 建立 RPC 之前註冊事件處理器。
    // 用來避免遺漏 session.start 等早期事件。
    // 等同於稍後呼叫 $session->on($handler),但註冊時機更早。
    onEvent: function (SessionEvent $event) {
        // 接收所有 Session 事件
    },
);

$response = Copilot::run('...', config: $config);
```

在 Session 恢復時使用 `ResumeSessionConfig` 類別。與 `SessionConfig` 幾乎相同,但略有差異。ResumeSessionConfig 只需指定想變更的項目。其他項目會沿用新 Session 開始時的設定。

雲端執行請參閱 [Cloud Sessions](/zh-TW/packages/laravel-copilot-sdk/cloud-sessions),Plugin Directory 請參閱 [Plugin Directories](/zh-TW/packages/laravel-copilot-sdk/plugin-directories)。

<Info>
  最新資訊請參閱 [GitHub 儲存庫](https://github.com/invokable/laravel-copilot-sdk)。
</Info>


## Related topics

- [Cloud Sessions](/zh-TW/packages/laravel-copilot-sdk/cloud-sessions.md)
- [Session 恢復](/zh-TW/packages/laravel-copilot-sdk/resume.md)
- [自訂 Agent](/zh-TW/packages/laravel-copilot-sdk/custom-agents.md)
- [開始使用 - GitHub Copilot SDK for Laravel](/zh-TW/packages/laravel-copilot-sdk/getting-started.md)
- [Streaming Events](/zh-TW/packages/laravel-copilot-sdk/streaming-events.md)
