fix(agent): preserve historical tool styles

This commit is contained in:
jxxghp
2026-10-01 14:41:10 +08:00
parent 3d6f29f0e8
commit 17db0b22b9
3 changed files with 29 additions and 6 deletions

View File

@@ -521,10 +521,9 @@ class StreamingHandler:
live_summary = self._live_tool_summary
visible_buffer = self._buffer
live_start = len(self._buffer)
old_summary = None
if live_summary and self._buffer[live_summary[0] :] == live_summary[1]:
live_start, _old_block, old_summary = live_summary
live_start, _old_block, _old_summary = live_summary
visible_buffer = self._buffer[:live_start]
if self._pending_tool_stats:
@@ -535,8 +534,8 @@ class StreamingHandler:
return ""
summary, summary_block = pending_summary
if old_summary:
self._tool_summaries.discard(old_summary)
# 保留历史摘要的样式登记。相同文案可能在不同工具批次重复出现,
# 删除旧字符串会让前一批已写入消息在后续编辑时失去富文本样式。
self._tool_summaries.add(summary)
self._buffer = visible_buffer + summary_block
self._live_tool_summary = (len(visible_buffer), summary_block, summary)

View File

@@ -2275,8 +2275,6 @@ class MoviePilotAgent:
def emit_visible_token(text: str) -> None:
"""只在真正产生可见答案时结束思考状态,再转发正文。"""
nonlocal thinking_finished
if not text:
return
if not thinking_finished:
finish_thinking = getattr(thinking_started, "thinking_finished", None)
if callable(finish_thinking):

View File

@@ -768,6 +768,32 @@ class TestAgentToolStreaming:
assert rich_message == "正文内容\n\n> (执行了 1 条命令)\n\n后续结论"
def test_rich_message_keeps_previous_tool_summaries_styled_after_edits(self):
"""Telegram 后续编辑时,重复文案的历史工具摘要仍应保持引用样式。"""
handler = StreamingHandler()
handler._channel = NotificationChannel.Telegram.value
handler._streaming_enabled = True
handler._can_stream = lambda: True
for _ in range(3):
handler.record_tool_call(
tool_name="execute_command",
tool_message="执行命令",
tool_kwargs={},
)
handler.emit("第一批完成")
for _ in range(4):
handler.record_tool_call(
tool_name="execute_command",
tool_message="执行命令",
tool_kwargs={},
)
rich_message = handler._get_rich_message(handler._buffer)
assert "> (执行了 3 条命令)" in rich_message
assert "> (执行了 4 条命令)" in rich_message
def test_rich_message_returns_none_for_non_telegram_channels(self):
"""校验非 Telegram 渠道不启用富文本,摘要保持原有纯文本格式。"""
handler = StreamingHandler()