perf: reduce streaming text processing and classic CLI redraws (#5728)

* perf: skip tag parsing for ordinary streamed text

* perf: coalesce classic CLI stream refreshes
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-15 11:48:40 +08:00
1 parent 1c3c68262f
commit aa257a705e
3 files changed
+114 -2

No files matched your search

+7
View File
@@ -191,6 +191,9 @@ def strip_think(text: str) -> str:
tokens mid-text would silently rewrite any message where a user or the
assistant discusses the tokens themselves.
"""
# Every supported control tag contains '<'; ordinary text only needs trimming.
if "<" not in text:
return text.strip()
# Well-formed blocks first.
text = re.sub(rf"<(?P<tag>{_THINKING_TAG})>[\s\S]*?</(?P=tag)>", "", text)
text = re.sub(rf"^\s*<{_THINKING_TAG}>[\s\S]*$", "", text)
@@ -224,6 +227,8 @@ def strip_reasoning_tags(text: object) -> str:
"""Remove wrapper tags from text that is already known to be reasoning."""
if not isinstance(text, str):
return ""
if "<" not in text:
return text.strip()
text = re.sub(rf"^\s*<{_THINKING_TAG}/>\s*", "", text)
text = re.sub(rf"\s*<{_THINKING_TAG}/>\s*$", "", text)
text = re.sub(rf"^\s*<{_THINKING_TAG}>\s*", "", text)
@@ -239,6 +244,8 @@ def extract_think(text: str) -> tuple[str | None, str]:
extracted; unclosed streaming prefixes are stripped from the cleaned
text but not surfaced — :func:`strip_think` handles that case.
"""
if "<" not in text:
return None, text.strip()
parts: list[str] = []
for m in re.finditer(rf"<(?P<tag>{_THINKING_TAG})>([\s\S]*?)</(?P=tag)>", text):
parts.append(m.group(2).strip())