Extract helpers in _process_context to reduce repeated code

This commit is contained in:
Paul Kompfner
2026-03-30 23:10:38 -04:00
parent ab9f2a35b6
commit 3922963c7a

View File

@@ -746,33 +746,41 @@ class OpenAIResponsesLLMService(_BaseOpenAIResponsesLLMService, WebsocketLLMServ
full_input = invocation_params["input"] full_input = invocation_params["input"]
# -- first attempt (with previous_response_id optimization) ----------- def build_params(*, apply_optimization: bool) -> dict:
params = self._build_response_params(invocation_params)
# WebSocket mode does not use the "stream" parameter.
params.pop("stream", None)
if apply_optimization:
params = self._apply_previous_response_optimization(params, full_input)
return params
params = self._build_response_params(invocation_params) async def send_and_receive(params: dict):
# WebSocket mode does not use the "stream" parameter
params.pop("stream", None)
params = self._apply_previous_response_optimization(params, full_input)
try:
await self._ensure_connected() await self._ensure_connected()
await self.start_ttfb_metrics() await self.start_ttfb_metrics()
await self._ws_send({"type": "response.create", **params}) await self._ws_send({"type": "response.create", **params})
await self._receive_response_events(context, full_input) await self._receive_response_events(context, full_input)
async def cleanup():
self._clear_previous_response_state()
await self.stop_ttfb_metrics()
# -- first attempt (with previous_response_id optimization) -----------
try:
await send_and_receive(build_params(apply_optimization=True))
return # Success return # Success
except _PreviousResponseNotFoundError: except _PreviousResponseNotFoundError:
logger.warning( logger.warning(
f"{self}: previous_response_not_found — " f"{self}: previous_response_not_found — "
f"retrying with full context ({len(full_input)} items)" f"retrying with full context ({len(full_input)} items)"
) )
self._clear_previous_response_state() await cleanup()
await self.stop_ttfb_metrics()
except _ConnectionLimitReachedError: except _ConnectionLimitReachedError:
logger.warning( logger.warning(
f"{self}: WebSocket connection limit reached — " f"{self}: WebSocket connection limit reached — "
f"reconnecting and retrying with full context ({len(full_input)} items)" f"reconnecting and retrying with full context ({len(full_input)} items)"
) )
self._clear_previous_response_state() await cleanup()
await self.stop_ttfb_metrics()
await self._try_reconnect(report_error=self._report_error) await self._try_reconnect(report_error=self._report_error)
except WebsocketReconnectedError: except WebsocketReconnectedError:
# ConnectionClosed was handled by the base class — connection is # ConnectionClosed was handled by the base class — connection is
@@ -781,26 +789,17 @@ class OpenAIResponsesLLMService(_BaseOpenAIResponsesLLMService, WebsocketLLMServ
f"{self}: Connection lost and recovered — " f"{self}: Connection lost and recovered — "
f"retrying with full context ({len(full_input)} items)" f"retrying with full context ({len(full_input)} items)"
) )
self._clear_previous_response_state() await cleanup()
await self.stop_ttfb_metrics()
except Exception: except Exception:
self._clear_previous_response_state() await cleanup()
await self.stop_ttfb_metrics()
raise raise
# -- retry with full context (no optimization) ------------------------ # -- retry with full context (no optimization) ------------------------
params = self._build_response_params(invocation_params)
params.pop("stream", None)
try: try:
await self._ensure_connected() await send_and_receive(build_params(apply_optimization=False))
await self.start_ttfb_metrics()
await self._ws_send({"type": "response.create", **params})
await self._receive_response_events(context, full_input)
except Exception: except Exception:
self._clear_previous_response_state() await cleanup()
await self.stop_ttfb_metrics()
raise raise
async def _receive_response_events(self, context: LLMContext, full_input: list): async def _receive_response_events(self, context: LLMContext, full_input: list):