Commit 0332ea14 by sunqi02 Committed by 胡悦

feat: sub-agent sends post_reply through its own bot account

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 89cb2679
......@@ -544,12 +544,29 @@ class AgentHub:
max_iters=10,
)
# 为子 Agent 注册 IM hook
# 为子 Agent 注册 IM hook(post_reasoning → thinking 摘要)
ctx = self._current_route_ctx
self._register_im_hooks(sub_agent, ctx["room_id"], ctx["user_id"], ctx["bot_id_mapping"])
response = await sub_agent.reply(Msg("user", instruction, "user"))
return response.get_text_content()
response_text = response.get_text_content()
# 子 Agent 用自己的 bot 账号发送最终回复
try:
await self.post_reply_hook.execute({
"agent_name": agent_code,
"response_content": response_text,
"room_id": ctx["room_id"],
"mentioned_list": [ctx["user_id"]],
"bot_id_mapping": ctx["bot_id_mapping"],
})
except Exception:
logger.warning(
"[子Agent IM 发送失败] agent=%s room=%s(不影响推理流程)",
agent_code, ctx.get("room_id"), exc_info=True,
)
return response_text
def _make_read_file_tool(self):
"""Create a sandboxed read_file tool function.
......
......@@ -1008,3 +1008,55 @@ class TestCallSubAgentCounter:
await tool(agent_code=MEETING_KEY, instruction="测试指令")
assert hub._current_route_ctx["call_count"] == 1
class TestSubAgentPostReply:
"""测试子 Agent 独立发送 post_reply。"""
@pytest.mark.asyncio
async def test_call_sub_agent_sends_post_reply(self):
"""_call_sub_agent 应通过 post_reply_hook 发送子 Agent 回复。"""
hub = _make_test_hub()
hub._current_route_ctx = dict(
room_id="r1", user_id="u1", bot_id_mapping={MEETING_KEY: "bot_meeting"},
call_count=0,
)
# post_reply_hook.execute 是真实方法,用 AsyncMock(wraps=...) 包装以跟踪调用
mock_execute = AsyncMock(wraps=hub.post_reply_hook.execute)
mock_reply = _make_mock_model_reply("会议纪要已生成")
# 必须同时 patch sub_model 和 thinking_summary_model:
# thinking_summary_model 在 __init__ 时赋值为 sub_model 引用,
# post_reasoning hook 会调 _summarize_thinking → thinking_summary_model
with patch.object(hub, 'sub_model', new=mock_reply):
with patch.object(hub, 'thinking_summary_model', new=mock_reply):
with patch.object(hub.post_reply_hook, 'execute', new=mock_execute):
result = await hub._call_sub_agent(MEETING_KEY, "处理会议录音")
assert "会议纪要已生成" in result
# 验证 post_reply_hook.execute 被调用
mock_execute.assert_called()
call_kwargs = mock_execute.call_args[0][0]
assert call_kwargs["agent_name"] == MEETING_KEY
assert "会议纪要已生成" == call_kwargs["response_content"]
assert call_kwargs["bot_id_mapping"][MEETING_KEY] == "bot_meeting"
@pytest.mark.asyncio
async def test_call_sub_agent_post_reply_failure_non_fatal(self):
"""post_reply 发送失败不影响子 Agent 返回结果。"""
hub = _make_test_hub()
hub._current_route_ctx = dict(
room_id="r1", user_id="u1", bot_id_mapping={MEETING_KEY: "bot_meeting"},
call_count=0,
)
hub.post_reply_hook.execute = AsyncMock(side_effect=Exception("IM down"))
mock_reply = _make_mock_model_reply("会议纪要已生成")
with patch.object(hub, 'sub_model', new=mock_reply):
with patch.object(hub, 'thinking_summary_model', new=mock_reply):
result = await hub._call_sub_agent(MEETING_KEY, "处理会议录音")
# 结果正常返回
assert "会议纪要已生成" in result
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!