Commit 3d224390 by sunqi02

feat: add call_count counter to track sub_agent invocations per request

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent c9180cda
...@@ -375,6 +375,7 @@ class AgentHub: ...@@ -375,6 +375,7 @@ class AgentHub:
# 暂存路由上下文,供 _call_sub_agent 创建子 Agent 时注册 hook # 暂存路由上下文,供 _call_sub_agent 创建子 Agent 时注册 hook
self._current_route_ctx = dict( self._current_route_ctx = dict(
room_id=room_id, user_id=user_id, bot_id_mapping=bot_id_mapping or {}, room_id=room_id, user_id=user_id, bot_id_mapping=bot_id_mapping or {},
call_count=0,
) )
session = self.session_service.get_or_create_session( session = self.session_service.get_or_create_session(
...@@ -645,6 +646,11 @@ class AgentHub: ...@@ -645,6 +646,11 @@ class AgentHub:
f"正确示例:call_sub_agent(agent_code=\"{sub_example}\", instruction=\"请帮我处理任务\")" f"正确示例:call_sub_agent(agent_code=\"{sub_example}\", instruction=\"请帮我处理任务\")"
))] ))]
) )
# 新增:递增调用计数
ctx = getattr(self, "_current_route_ctx", None)
if ctx:
ctx["call_count"] = ctx.get("call_count", 0) + 1
result = await self._call_sub_agent(agent_code, instruction) result = await self._call_sub_agent(agent_code, instruction)
return ToolResponse( return ToolResponse(
content=[TextBlock(type="text", text=result)] content=[TextBlock(type="text", text=result)]
......
...@@ -979,3 +979,32 @@ async def test_hook_skips_after_call_sub_agent_result(): ...@@ -979,3 +979,32 @@ async def test_hook_skips_after_call_sub_agent_result():
assert result is None assert result is None
mock_im.send_message.assert_not_called() mock_im.send_message.assert_not_called()
mock_summarize.assert_not_called() mock_summarize.assert_not_called()
class TestCallSubAgentCounter:
"""测试 call_sub_agent 调用计数。"""
def test_call_count_initialized_in_route_ctx(self):
"""_current_route_ctx 初始化后 call_count 为 0。"""
hub = _make_test_hub()
hub._current_route_ctx = dict(
room_id="r1", user_id="u1", bot_id_mapping={},
call_count=0,
)
assert hub._current_route_ctx["call_count"] == 0
@pytest.mark.asyncio
async def test_call_count_increments_in_tool(self):
"""call_sub_agent 工具调用后计数器 +1。"""
hub = _make_test_hub()
hub._current_route_ctx = dict(
room_id="r1", user_id="u1", bot_id_mapping={},
call_count=0,
)
# patch _call_sub_agent 避免真实创建 ReActAgent
with patch.object(hub, '_call_sub_agent', new=AsyncMock(return_value="子Agent结果")):
tool = hub._make_call_sub_agent_tool()
await tool(agent_code=MEETING_KEY, instruction="测试指令")
assert hub._current_route_ctx["call_count"] == 1
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!