stream_consumer_think.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Think-block filtering for GatewayStreamConsumer.
  2. Some models emit inline <think>...</think> blocks in content. The agent strips
  3. them from the final response, but intermediate edits go out before that, so this
  4. mirrors the CLI's _stream_delta state machine; tag primitives are shared with
  5. ``agent/think_scrubber.py`` so the progressive display matches the post-stream scrubber."""
  6. from __future__ import annotations
  7. import logging
  8. from agent.think_scrubber import THINK_CLOSE_TAGS, THINK_OPEN_TAGS
  9. from agent.think_scrubber import StreamingThinkScrubber as _Scrubber
  10. logger = logging.getLogger("gateway.stream_consumer")
  11. class StreamThinkFilterMixin:
  12. """Progressive <think>-tag suppression over streamed deltas."""
  13. _OPEN_THINK_TAGS = THINK_OPEN_TAGS
  14. _CLOSE_THINK_TAGS = THINK_CLOSE_TAGS
  15. def _at_block_boundary(self, buf: str, idx: int) -> bool:
  16. """Tag at ``idx`` starts a block: start of text, or newline + optional whitespace.
  17. Prose that merely *mentions* a tag must not trigger.
  18. """
  19. acc_boundary = not self._accumulated or self._accumulated.endswith("\n")
  20. if idx == 0:
  21. return acc_boundary
  22. preceding = buf[:idx]
  23. last_nl = preceding.rfind("\n")
  24. if last_nl == -1:
  25. return acc_boundary and preceding.strip() == ""
  26. return preceding[last_nl + 1:].strip() == ""
  27. def _earliest_open_tag(self, buf: str, lower_buf: str) -> tuple[int, int]:
  28. """(index, length) of the earliest block-boundary opening tag, or (-1, 0)."""
  29. best_idx, best_len = -1, 0
  30. for tag in self._OPEN_THINK_TAGS:
  31. tag_lower = tag.lower()
  32. search_start = 0
  33. while (idx := lower_buf.find(tag_lower, search_start)) != -1:
  34. if self._at_block_boundary(buf, idx):
  35. if best_idx == -1 or idx < best_idx:
  36. best_idx, best_len = idx, len(tag)
  37. break # first boundary hit for this tag is enough
  38. search_start = idx + 1
  39. return best_idx, best_len
  40. def _filter_and_accumulate(self, text: str) -> None:
  41. """Append a delta to the buffer, discarding think blocks.
  42. Partial tags at buffer boundaries are held in ``_think_buffer`` until
  43. enough characters arrive to decide.
  44. """
  45. buf = self._think_buffer + text
  46. self._think_buffer = ""
  47. while buf:
  48. # Case-insensitive: models emit <Think>, <THINKING>, …
  49. lower_buf = buf.lower()
  50. if self._in_think_block:
  51. best_idx, best_len = _Scrubber._find_first_tag(buf, self._CLOSE_THINK_TAGS)
  52. if best_len:
  53. self._in_think_block = False
  54. buf = buf[best_idx + best_len:]
  55. else:
  56. # Hold a tail that could be a partial close tag; discard the rest.
  57. max_tag = max(len(t) for t in self._CLOSE_THINK_TAGS)
  58. self._think_buffer = buf[-max_tag:] if len(buf) > max_tag else buf
  59. return
  60. else:
  61. best_idx, best_len = self._earliest_open_tag(buf, lower_buf)
  62. if best_len:
  63. self._append_accumulated(buf[:best_idx])
  64. self._in_think_block = True
  65. buf = buf[best_idx + best_len:]
  66. else:
  67. # Hold back a partial open tag at the tail.
  68. held_back = _Scrubber._max_partial_suffix(buf, self._OPEN_THINK_TAGS)
  69. if held_back:
  70. self._append_accumulated(buf[:-held_back])
  71. self._think_buffer = buf[-held_back:]
  72. else:
  73. # An orphan </think> (thinking-mode toggle dropped the open, or
  74. # incomplete upstream stripping) is noise.
  75. self._append_accumulated(self._strip_orphan_close_tags(buf))
  76. return
  77. @staticmethod
  78. def _strip_orphan_close_tags(text: str) -> str:
  79. """Remove close tags (plus trailing whitespace) that have no matching open."""
  80. return _Scrubber._strip_orphan_close_tags(text)
  81. def _flush_think_buffer(self) -> None:
  82. """On stream end, flush text held back waiting for a possible open tag."""
  83. if self._think_buffer and not self._in_think_block:
  84. self._append_accumulated(self._strip_orphan_close_tags(self._think_buffer))
  85. self._think_buffer = ""