Execute a command handler and send the result back via RPC.
(
self,
request_id: str,
command_name: str,
command: str,
args: str,
)
| 2127 | pass # Connection lost or RPC error — nothing we can do |
| 2128 | |
| 2129 | async def _execute_command_and_respond( |
| 2130 | self, |
| 2131 | request_id: str, |
| 2132 | command_name: str, |
| 2133 | command: str, |
| 2134 | args: str, |
| 2135 | ) -> None: |
| 2136 | """Execute a command handler and send the result back via RPC.""" |
| 2137 | with self._command_handlers_lock: |
| 2138 | handler = self._command_handlers.get(command_name) |
| 2139 | |
| 2140 | if not handler: |
| 2141 | try: |
| 2142 | await self.rpc.commands.handle_pending_command( |
| 2143 | CommandsHandlePendingCommandRequest( |
| 2144 | request_id=request_id, |
| 2145 | error=f"Unknown command: {command_name}", |
| 2146 | ) |
| 2147 | ) |
| 2148 | except (JsonRpcError, ProcessExitedError, OSError): |
| 2149 | pass # Connection lost — nothing we can do |
| 2150 | return |
| 2151 | |
| 2152 | try: |
| 2153 | ctx = CommandContext( |
| 2154 | session_id=self.session_id, |
| 2155 | command=command, |
| 2156 | command_name=command_name, |
| 2157 | args=args, |
| 2158 | ) |
| 2159 | handler_start = time.perf_counter() |
| 2160 | result = handler(ctx) |
| 2161 | if inspect.isawaitable(result): |
| 2162 | await result |
| 2163 | log_timing( |
| 2164 | logger, |
| 2165 | logging.DEBUG, |
| 2166 | "CopilotSession._execute_command_and_respond dispatch", |
| 2167 | handler_start, |
| 2168 | session_id=self.session_id, |
| 2169 | request_id=request_id, |
| 2170 | command_name=command_name, |
| 2171 | ) |
| 2172 | rpc_start = time.perf_counter() |
| 2173 | await self.rpc.commands.handle_pending_command( |
| 2174 | CommandsHandlePendingCommandRequest(request_id=request_id) |
| 2175 | ) |
| 2176 | log_timing( |
| 2177 | logger, |
| 2178 | logging.DEBUG, |
| 2179 | "CopilotSession._execute_command_and_respond response sent successfully", |
| 2180 | rpc_start, |
| 2181 | session_id=self.session_id, |
| 2182 | request_id=request_id, |
| 2183 | command_name=command_name, |
| 2184 | ) |
| 2185 | except Exception as exc: |
| 2186 | message = str(exc) |
no test coverage detected