fix(agent): emit web tool status immediately

This commit is contained in:
jxxghp
2026-08-07 13:01:47 +08:00
parent 865635c59d
commit 7985268f10
2 changed files with 33 additions and 0 deletions

View File

@@ -252,6 +252,20 @@ class _WebAgentStreamingHandler(StreamingHandler):
"""
self._on_emit = on_emit
def record_tool_call(
self,
tool_name: str,
tool_message: Optional[str] = None,
tool_kwargs: Optional[dict[str, Any]] = None,
) -> None:
"""记录并立即输出 Web 工具事件,避免汇总延迟到正文结束后。"""
super().record_tool_call(
tool_name=tool_name,
tool_message=tool_message,
tool_kwargs=tool_kwargs,
)
self.flush_pending_tool_summary()
def emit(self, token: str) -> str:
"""追加 token 并同步通知 SSE 生产者。"""
emitted = super().emit(token)

View File

@@ -395,6 +395,25 @@ def test_web_agent_output_callback_receives_only_new_text():
assert agent._streamed_output == "你好"
def test_web_agent_tool_summary_is_emitted_before_following_text():
"""Web 工具状态应在调用发生时输出,不能拖到正文结束后。"""
outputs = []
agent = _WebAgentMoviePilotAgent(
session_id="web-agent:tool-order",
user_id="7",
channel=MessageChannel.WebAgent.value,
source="web-agent",
username="admin",
replay_mode=ReplyMode.CAPTURE_ONLY,
output_callback=outputs.append,
)
agent.stream_handler.record_tool_call("query_download_tasks")
agent._handle_stream_text("查询完成。")
assert outputs == ["(查询了 1 次数据)\n\n", "查询完成。"]
def test_web_agent_channel_supports_streaming_and_attachments():
"""WebAgent 渠道应声明流式、多媒体和文件发送能力。"""
assert ChannelCapabilityManager.supports_capability(