Disconnect this session and release all in-memory resources (event handlers, tool handlers, permission handlers). Session state on disk (conversation history, planning state, artifacts) is preserved, so the conversation can be resumed later by calling :meth:
(self)
| 2747 | return [session_event_from_dict(event_dict) for event_dict in events_dicts] |
| 2748 | |
| 2749 | async def disconnect(self) -> None: |
| 2750 | """ |
| 2751 | Disconnect this session and release all in-memory resources (event handlers, |
| 2752 | tool handlers, permission handlers). |
| 2753 | |
| 2754 | Session state on disk (conversation history, planning state, artifacts) |
| 2755 | is preserved, so the conversation can be resumed later by calling |
| 2756 | :meth:`CopilotClient.resume_session` with the session ID. To |
| 2757 | permanently remove all session data including files on disk, use |
| 2758 | :meth:`CopilotClient.delete_session` instead. |
| 2759 | |
| 2760 | After calling this method, the session object can no longer be used. |
| 2761 | |
| 2762 | This method is idempotent—calling it multiple times is safe and will |
| 2763 | not raise an error if the session is already disconnected. |
| 2764 | |
| 2765 | Raises: |
| 2766 | Exception: If the connection fails (on first disconnect call). |
| 2767 | |
| 2768 | Example: |
| 2769 | >>> # Clean up when done — session can still be resumed later |
| 2770 | >>> await session.disconnect() |
| 2771 | """ |
| 2772 | # Ensure that the check and update of _destroyed are atomic so that |
| 2773 | # only the first caller proceeds to send the destroy RPC. |
| 2774 | with self._event_handlers_lock: |
| 2775 | if self._destroyed: |
| 2776 | return |
| 2777 | self._destroyed = True |
| 2778 | |
| 2779 | try: |
| 2780 | await self._client.request("session.destroy", {"sessionId": self.session_id}) |
| 2781 | finally: |
| 2782 | # Clear handlers even if the request fails. |
| 2783 | with self._event_handlers_lock: |
| 2784 | self._event_handlers.clear() |
| 2785 | with self._tool_handlers_lock: |
| 2786 | self._tool_handlers.clear() |
| 2787 | with self._permission_handler_lock: |
| 2788 | self._permission_handler = None |
| 2789 | with self._command_handlers_lock: |
| 2790 | self._command_handlers.clear() |
| 2791 | with self._elicitation_handler_lock: |
| 2792 | self._elicitation_handler = None |
| 2793 | with self._exit_plan_mode_handler_lock: |
| 2794 | self._exit_plan_mode_handler = None |
| 2795 | with self._auto_mode_switch_handler_lock: |
| 2796 | self._auto_mode_switch_handler = None |
| 2797 | |
| 2798 | async def __aenter__(self) -> CopilotSession: |
| 2799 | """Enable use as an async context manager.""" |