context_compressor_retry.py 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. """Lossless /retry text extraction for a user-originated turn's live content."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from agent.message_metadata import typed_prompt_content # re-exported with the retry text helper
  5. def retryable_user_text(content: Any) -> str:
  6. """Lossless retry text, or raise before destructive mutation (media/unknown parts fail closed: no replay protocol)."""
  7. if not isinstance(content, (str, list)):
  8. raise ValueError("retry does not support non-text content")
  9. chunks: list[str] = []
  10. for part in [content] if isinstance(content, str) else content:
  11. if isinstance(part, str):
  12. chunks.append(part)
  13. continue
  14. if not isinstance(part, dict):
  15. raise ValueError("retry does not support non-text content")
  16. if part.get("type") not in {"text", "input_text", "output_text"}:
  17. raise ValueError("retry does not support media or unknown content parts")
  18. if set(part) - {"type", "text"}:
  19. raise ValueError("retry cannot losslessly flatten annotated text parts")
  20. if not isinstance(part.get("text"), str):
  21. raise ValueError("retry text parts must contain text")
  22. chunks.append(part["text"])
  23. text = "".join(chunks)
  24. if not text.strip():
  25. raise ValueError("retry found no text to send")
  26. return text