auxiliary_codex_response.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Codex Responses → chat.completions normalization for auxiliary callers.
  2. Aux consumers speak Chat Completions, so a Responses result is mapped onto
  3. ``(text_parts, tool_calls, usage, finish_reason)`` through the main loop's shared
  4. normalizer, keeping its phase and completion gates.
  5. """
  6. from types import SimpleNamespace
  7. from typing import Any, List, Optional, Tuple
  8. def _parse_codex_final_response(
  9. final: Any, *, issuer_kind: Optional[str] = None, issuer_model: Optional[str] = None,
  10. ) -> tuple[list[str], list[Any], Any, str]:
  11. """Normalize Responses output without losing phase or completion state for aux callers."""
  12. from agent.codex_responses_adapter import _lower_or_none, _normalize_codex_response
  13. # The shared normalizer reads SDK-style items. Keep support for compatible hosts
  14. # returning dict items, and the aux adapter's legacy empty completed response.
  15. output = [
  16. SimpleNamespace(**item) if isinstance(item, dict) else item
  17. for item in (getattr(final, "output", None) or [])
  18. ]
  19. normalized_final = SimpleNamespace(
  20. output=output or [SimpleNamespace(type="message", content=[])],
  21. output_text=getattr(final, "output_text", None),
  22. status=getattr(final, "status", None),
  23. incomplete_details=getattr(final, "incomplete_details", None),
  24. error=getattr(final, "error", None),
  25. )
  26. # Aux has no continuation to re-elicit a leaked tool call, so tool-call-shaped text stays content
  27. # and goes through the normalizer's own phase/completion gates (no clear, no WARNING).
  28. message, finish_reason = _normalize_codex_response(
  29. normalized_final, issuer_kind=issuer_kind, issuer_model=issuer_model, recover_leaked_tool_call=False,
  30. )
  31. # Aux consumers speak Chat Completions: "length" activates their existing
  32. # partial-summary rejection/fallback, whereas Codex's "incomplete" does not.
  33. # Any provider-incomplete response (token cap or content_filter) is a partial no aux consumer
  34. # may commit; completed tool calls stay "tool_calls" so dispatchers (e.g. MCP sampling) still run them.
  35. if finish_reason != "tool_calls" and (
  36. finish_reason == "incomplete" or _lower_or_none(normalized_final.status) == "incomplete"
  37. ):
  38. finish_reason = "length"
  39. text_parts = [message.content] if message.content else []
  40. tool_calls_raw = message.tool_calls
  41. usage = None
  42. resp_usage = getattr(final, "usage", None)
  43. if resp_usage:
  44. def _u(key: str) -> int:
  45. return getattr(resp_usage, key, 0) or (resp_usage.get(key, 0) if isinstance(resp_usage, dict) else 0)
  46. usage = SimpleNamespace(
  47. prompt_tokens=_u("input_tokens"), completion_tokens=_u("output_tokens"),
  48. total_tokens=_u("total_tokens"))
  49. return text_parts, tool_calls_raw, usage, finish_reason