fix: prevent empty </think> tags from appearing in messages

- Enhance _strip_think to handle stray tags:
  * Remove unmatched closing tags (</think>)
  * Remove incomplete blocks (<think> ... to end of string)
- Apply _strip_think to tool hint messages as well
- Prevents blank/parse errors from showing </think> in chat outputs

Fixes issue with empty </think> appearing in Feishu tool call cards and other messages.
This commit is contained in:
Tony
2026-03-13 14:55:34 +08:00
parent 87ab980bd1
commit 2787523f49

View File

@@ -163,7 +163,13 @@ class AgentLoop:
"""Remove <think>…</think> blocks that some models embed in content."""
if not text:
return None
return re.sub(r"<think>[\s\S]*?</think>", "", text).strip() or None
# Remove complete think blocks (non-greedy)
cleaned = re.sub(r"<think>[\s\S]*?</think>", "", text)
# Remove any stray closing tags left without opening
cleaned = re.sub(r"</think>", "", cleaned)
# Remove any stray opening tag and everything after it (incomplete block)
cleaned = re.sub(r"<think>[\s\S]*$", "", cleaned)
return cleaned.strip() or None
@staticmethod
def _tool_hint(tool_calls: list) -> str:
@@ -203,7 +209,9 @@ class AgentLoop:
thought = self._strip_think(response.content)
if thought:
await on_progress(thought)
await on_progress(self._tool_hint(response.tool_calls), tool_hint=True)
tool_hint = self._tool_hint(response.tool_calls)
tool_hint = self._strip_think(tool_hint)
await on_progress(tool_hint, tool_hint=True)
tool_call_dicts = [
tc.to_openai_tool_call()