|
73 | 73 | RemoteSessionMode, |
74 | 74 | ServerRpc, |
75 | 75 | _ConnectResult, |
| 76 | + _HookInvokeRequest, |
| 77 | + _HookInvokeResponse, |
76 | 78 | from_datetime, |
77 | 79 | register_client_global_api_handlers, |
78 | 80 | register_client_session_api_handlers, |
@@ -479,6 +481,25 @@ async def event(self, params: GitHubTelemetryNotification) -> None: |
479 | 481 | logger.warning("Error handling gitHubTelemetry.event notification", exc_info=True) |
480 | 482 |
|
481 | 483 |
|
| 484 | +class _HooksAdapter: |
| 485 | + """Adapts session-scoped hook dispatch to the generated ``HooksHandler`` protocol. |
| 486 | +
|
| 487 | + ``hooks.invoke`` is a client-global RPC method whose payload carries a |
| 488 | + ``sessionId``. This adapter routes each invocation to the matching session's |
| 489 | + registered hook handlers. |
| 490 | + """ |
| 491 | + |
| 492 | + def __init__(self, get_session: Callable[[str], CopilotSession | None]) -> None: |
| 493 | + self._get_session = get_session |
| 494 | + |
| 495 | + async def invoke(self, params: _HookInvokeRequest) -> _HookInvokeResponse: |
| 496 | + session = self._get_session(params.session_id) |
| 497 | + if session is None: |
| 498 | + raise ValueError(f"unknown session {params.session_id}") |
| 499 | + output = await session._handle_hooks_invoke(params.hook_type.value, params.input) |
| 500 | + return _HookInvokeResponse(output=output) |
| 501 | + |
| 502 | + |
482 | 503 | @dataclass |
483 | 504 | class _CopilotClientOptions: |
484 | 505 | """Internal configuration carrier used by :class:`CopilotClient`. |
@@ -4072,7 +4093,6 @@ def handle_notification(method: str, params: dict): |
4072 | 4093 | self._client.set_request_handler( |
4073 | 4094 | "autoModeSwitch.request", self._handle_auto_mode_switch_request |
4074 | 4095 | ) |
4075 | | - self._client.set_request_handler("hooks.invoke", self._handle_hooks_invoke) |
4076 | 4096 | self._client.set_request_handler( |
4077 | 4097 | "systemMessage.transform", self._handle_system_message_transform |
4078 | 4098 | ) |
@@ -4192,7 +4212,6 @@ def handle_notification(method: str, params: dict): |
4192 | 4212 | self._client.set_request_handler( |
4193 | 4213 | "autoModeSwitch.request", self._handle_auto_mode_switch_request |
4194 | 4214 | ) |
4195 | | - self._client.set_request_handler("hooks.invoke", self._handle_hooks_invoke) |
4196 | 4215 | self._client.set_request_handler( |
4197 | 4216 | "systemMessage.transform", self._handle_system_message_transform |
4198 | 4217 | ) |
@@ -4282,16 +4301,19 @@ def _register_client_global_handlers(self) -> None: |
4282 | 4301 | github_telemetry_adapter = None |
4283 | 4302 | if self._on_github_telemetry is not None: |
4284 | 4303 | github_telemetry_adapter = _GitHubTelemetryAdapter(self._on_github_telemetry) |
4285 | | - if llm_inference_adapter is None and github_telemetry_adapter is None: |
4286 | | - return |
4287 | 4304 | register_client_global_api_handlers( |
4288 | 4305 | self._client, |
4289 | 4306 | ClientGlobalApiHandlers( |
| 4307 | + hooks=_HooksAdapter(self._get_session), |
4290 | 4308 | llm_inference=llm_inference_adapter, |
4291 | 4309 | git_hub_telemetry=github_telemetry_adapter, |
4292 | 4310 | ), |
4293 | 4311 | ) |
4294 | 4312 |
|
| 4313 | + def _get_session(self, session_id: str) -> CopilotSession | None: |
| 4314 | + with self._sessions_lock: |
| 4315 | + return self._sessions.get(session_id) |
| 4316 | + |
4295 | 4317 | async def _set_llm_inference_provider(self) -> None: |
4296 | 4318 | if self._request_handler is None or self._rpc is None: |
4297 | 4319 | return |
@@ -4364,34 +4386,6 @@ async def _handle_auto_mode_switch_request(self, params: dict) -> dict: |
4364 | 4386 | response = await session._handle_auto_mode_switch_request(params) |
4365 | 4387 | return {"response": response} |
4366 | 4388 |
|
4367 | | - async def _handle_hooks_invoke(self, params: dict) -> dict: |
4368 | | - """ |
4369 | | - Handle a hooks invocation from the CLI server. |
4370 | | -
|
4371 | | - Args: |
4372 | | - params: The hooks invocation parameters from the server. |
4373 | | -
|
4374 | | - Returns: |
4375 | | - A dict containing the hook output. |
4376 | | -
|
4377 | | - Raises: |
4378 | | - ValueError: If the request payload is invalid. |
4379 | | - """ |
4380 | | - session_id = params.get("sessionId") |
4381 | | - hook_type = params.get("hookType") |
4382 | | - input_data = params.get("input") |
4383 | | - |
4384 | | - if not session_id or not hook_type: |
4385 | | - raise ValueError("invalid hooks invoke payload") |
4386 | | - |
4387 | | - with self._sessions_lock: |
4388 | | - session = self._sessions.get(session_id) |
4389 | | - if not session: |
4390 | | - raise ValueError(f"unknown session {session_id}") |
4391 | | - |
4392 | | - output = await session._handle_hooks_invoke(hook_type, input_data) |
4393 | | - return {"output": output} |
4394 | | - |
4395 | 4389 | async def _handle_system_message_transform(self, params: dict) -> dict: |
4396 | 4390 | """Handle a systemMessage.transform request from the CLI server.""" |
4397 | 4391 | session_id = params.get("sessionId") |
|
0 commit comments