reasoning_summaries.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """Boundary repair for providers that stream reasoning as discrete summary parts.
  2. Reasoning-summary models (OpenAI gpt-5.x and Responses-API relays onto the chat wire) emit one
  3. ``reasoning_content`` delta per *completed* summary part, each opening with a bold heading.
  4. The chat wire lacks the Responses API's ``summary_index`` delimiter (verified live on Nous
  5. Portal ``openai/gpt-5.6-sol``), so plain concatenation glues ``**One****Two**`` into one
  6. half-bold paragraph. The boundary is re-derived from a delta opening a bold heading, matching
  7. the blank-line join Hermes' own Responses adapter does.
  8. """
  9. from __future__ import annotations
  10. from typing import Any
  11. from agent.message_content import flatten_message_text
  12. __all__ = ["append_streamed_reasoning_detail", "separate_glued_reasoning_blocks"]
  13. def separate_glued_reasoning_blocks(previous: str, delta: Any) -> str:
  14. """Return *delta*, prefixed with a paragraph break when it glues onto *previous*.
  15. A break is inserted when *delta* opens a *closed* bold heading and *previous* is mid-line
  16. (heading butting heading, or prose butting heading). Token-streamed reasoning is left
  17. alone: its deltas carry their own whitespace, and a fragment that merely opens emphasis
  18. (``**`` alone) is not a part boundary — summary parts carry the whole heading in one delta.
  19. """
  20. # Relays also emit content-part lists/dicts; fragments carry their own whitespace.
  21. delta = flatten_message_text(delta, sep="")
  22. glued = previous and delta and not previous[-1].isspace() and delta.startswith("**") and "**" in delta[2:]
  23. return f"\n\n{delta}" if glued else delta
  24. # reasoning_details entry types whose consecutive fragments are ONE logical block.
  25. _MERGEABLE_DETAIL_TEXT_KEYS = {"reasoning.text": "text", "reasoning.summary": "summary"}
  26. _BACKFILL_DETAIL_KEYS = ("signature", "id", "format", "index")
  27. def streamed_reasoning_detail_text(detail: Any) -> str:
  28. """Readable text from a detail delta; never expose opaque replay material."""
  29. dtype = detail.get("type") if isinstance(detail, dict) else getattr(detail, "type", None)
  30. key = _MERGEABLE_DETAIL_TEXT_KEYS.get(dtype) if isinstance(dtype, str) else None
  31. if key is None:
  32. return ""
  33. text = detail.get(key) if isinstance(detail, dict) else getattr(detail, key, None)
  34. return text if isinstance(text, str) else ""
  35. def append_streamed_reasoning_detail(details_acc: list, detail: Any) -> None:
  36. """Accumulate one streamed ``reasoning_details`` delta entry into *details_acc*.
  37. OpenRouter streams ``reasoning_details`` as word-level deltas: consecutive
  38. ``reasoning.text`` / ``reasoning.summary`` entries are fragments of one logical
  39. block and are merged (later fragments backfill ``signature``/``id`` the first
  40. omitted); encrypted/opaque entries stay discrete. Unmerged, a long thought
  41. replays as hundreds of one-word entries and providers that validate the
  42. sequence shape on the next turn reject it. SDK objects are normalized to dicts.
  43. """
  44. if not isinstance(detail, dict):
  45. if hasattr(detail, "model_dump"):
  46. detail = detail.model_dump(warnings=False)
  47. elif hasattr(detail, "__dict__"):
  48. detail = dict(detail.__dict__)
  49. else:
  50. return
  51. dtype = detail.get("type")
  52. merge_key = _MERGEABLE_DETAIL_TEXT_KEYS.get(dtype)
  53. last = details_acc[-1] if details_acc else None
  54. if last is not None and merge_key and last.get("type") == dtype and isinstance(detail.get(merge_key), str):
  55. last[merge_key] = (last.get(merge_key) or "") + detail[merge_key]
  56. for k in _BACKFILL_DETAIL_KEYS:
  57. if last.get(k) in (None, "") and detail.get(k) not in (None, ""):
  58. last[k] = detail[k]
  59. return
  60. details_acc.append(dict(detail))