Connect to the CLI server via stdio pipes. Creates a JSON-RPC client using the CLI process's stdin/stdout. Raises: RuntimeError: If the CLI process is not started.
(self)
| 3644 | ) |
| 3645 | |
| 3646 | async def _connect_via_stdio(self) -> None: |
| 3647 | """ |
| 3648 | Connect to the CLI server via stdio pipes. |
| 3649 | |
| 3650 | Creates a JSON-RPC client using the CLI process's stdin/stdout. |
| 3651 | |
| 3652 | Raises: |
| 3653 | RuntimeError: If the CLI process is not started. |
| 3654 | """ |
| 3655 | if not self._process: |
| 3656 | raise RuntimeError("CLI process not started") |
| 3657 | |
| 3658 | # Create JSON-RPC client with the process |
| 3659 | self._client = JsonRpcClient(self._process) |
| 3660 | self._client.on_close = lambda: setattr(self, "_state", "disconnected") |
| 3661 | self._rpc = ServerRpc(self._client) |
| 3662 | |
| 3663 | # Set up notification handler for session events |
| 3664 | # Note: This handler is called from the event loop (thread-safe scheduling) |
| 3665 | def handle_notification(method: str, params: dict): |
| 3666 | if method == "session.event": |
| 3667 | session_id = params["sessionId"] |
| 3668 | event_dict = params["event"] |
| 3669 | # Convert dict to SessionEvent object |
| 3670 | event = session_event_from_dict(event_dict) |
| 3671 | with self._sessions_lock: |
| 3672 | session = self._sessions.get(session_id) |
| 3673 | if session: |
| 3674 | session._dispatch_event(event) |
| 3675 | elif method == "session.lifecycle": |
| 3676 | # Handle session lifecycle events |
| 3677 | lifecycle_event = _session_lifecycle_event_from_dict(params) |
| 3678 | self._dispatch_lifecycle_event(lifecycle_event) |
| 3679 | |
| 3680 | self._client.set_notification_handler(handle_notification) |
| 3681 | self._client.set_request_handler("userInput.request", self._handle_user_input_request) |
| 3682 | self._client.set_request_handler( |
| 3683 | "exitPlanMode.request", self._handle_exit_plan_mode_request |
| 3684 | ) |
| 3685 | self._client.set_request_handler( |
| 3686 | "autoModeSwitch.request", self._handle_auto_mode_switch_request |
| 3687 | ) |
| 3688 | self._client.set_request_handler("hooks.invoke", self._handle_hooks_invoke) |
| 3689 | self._client.set_request_handler( |
| 3690 | "systemMessage.transform", self._handle_system_message_transform |
| 3691 | ) |
| 3692 | register_client_session_api_handlers(self._client, self._get_client_session_handlers) |
| 3693 | self._register_llm_inference_handlers() |
| 3694 | |
| 3695 | # Start listening for messages |
| 3696 | loop = asyncio.get_running_loop() |
| 3697 | self._client.start(loop) |
| 3698 | |
| 3699 | async def _connect_via_tcp(self) -> None: |
| 3700 | """ |
no test coverage detected