11"""Invoking and supporting prompt preprocessor hook implementations."""
22
3- import asyncio
4-
53from traceback import format_tb
64
75from typing import (
2321 ChannelEndpoint ,
2422 ChannelFinishedEvent ,
2523 ChannelRxEvent ,
26- LMStudioChannelClosedError ,
2724 load_struct ,
2825)
2926from ..._sdk_models import (
@@ -74,7 +71,7 @@ class PromptPreprocessingRequestEvent(ChannelRxEvent[PromptPreprocessingRequest]
7471class PromptPreprocessingEndpoint (
7572 ChannelEndpoint [tuple [str , str ], PromptPreprocessingRxEvent , EmptyDict ]
7673):
77- """API channel endpoint to register a development plugin and receive credentials ."""
74+ """API channel endpoint to accept prompt preprocessing requests ."""
7875
7976 _API_ENDPOINT = "setPromptPreprocessor"
8077 _NOTICE_PREFIX = "Prompt preprocessing"
@@ -87,9 +84,8 @@ def iter_message_events(
8784 ) -> Iterable [PromptPreprocessingRxEvent ]:
8885 match contents :
8986 case None :
90- raise LMStudioChannelClosedError (
91- "Server failed to complete development plugin registration."
92- )
87+ # Server can only terminate the link by closing the websocket
88+ pass
9389 case {"type" : "abort" , "task_id" : str (task_id )}:
9490 yield PromptPreprocessingAbortEvent (task_id )
9591 case {"type" : "preprocess" } as request_dict :
@@ -217,14 +213,10 @@ async def _invoke_hook(request: PromptPreprocessingRequest) -> None:
217213 response_dict : UserMessageDict
218214 try :
219215 response = await hook_impl (hook_controller , message )
220- except asyncio .CancelledError :
221- # Cancellation is a regular exception, so explicitly
222- # reraise it to avoid blocking Ctrl-C processing
223- raise
224216 except Exception as exc :
225217 err_msg = "Error calling prompt preprocessing hook"
226218 logger .error (err_msg , exc_info = True , exc = repr (exc ))
227- # TODO: Determine if it's practical to emit a stack trace the server can display
219+ # TODO: Determine if it's worth sending the stack trace to the server
228220 ui_cause = f"{ err_msg } \n ({ type (exc ).__name__ } : { exc } )"
229221 error_details = SerializedLMSExtendedErrorDict (
230222 cause = ui_cause , stack = "\n " .join (format_tb (exc .__traceback__ ))
0 commit comments