Skip to main content

Session 生命週期事件

Session 生命週期事件是通知 Session 的建立、刪除、更新,以及 foreground/background 狀態變更(TUI+Server 模式)的事件。

事件類型

SessionLifecycleEventType enum 定義了 5 種事件類型。

訂閱事件

使用 onLifecycle() 方法可訂閱 Session 生命週期事件。

SessionLifecycleEvent

SessionLifecycleEvent 是包含生命週期事件細節的 readonly class。

SessionLifecycleEventMetadata

包含 Session metadata 的類別(刪除事件不包含)。

Foreground Session 管理

若連接到以 TUI+Server 模式(--ui-server)執行的伺服器,可管理 foreground Session。

取得目前的 Foreground Session

設定 Foreground Session

ForegroundSessionInfo

包含 foreground Session 資訊的 readonly class。

於 TCP 模式使用

以 TUI+Server 模式啟動 Copilot CLI,並從 SDK 連線即可使用生命週期事件。

注意事項

  • session.foregroundsession.background 事件僅在 TUI+Server 模式下發生
  • Session 刪除時(session.deleted),metadata 屬性為 null
  • onLifecycle() 會回傳用於取消訂閱的回呼函式
  • Session HooksonSessionStartonSessionEnd 是不同的功能
最新資訊請參閱 GitHub 儲存庫
最後修改於 2026年8月2日