Commit 1fd187ea by sunqi02 Committed by 胡悦

feat: 增加日志记录以跟踪 process_message 和子Agent调用的详细信息

1 parent f2b48278
...@@ -377,6 +377,11 @@ class AgentHub: ...@@ -377,6 +377,11 @@ class AgentHub:
""" """
start_time = time.time() start_time = time.time()
logger.info(
"[process_message 开始] session=%s user_query=%s file_url=%s",
session_id, user_query[:100], file_url,
)
# 暂存路由上下文,供 _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 {},
...@@ -496,6 +501,13 @@ class AgentHub: ...@@ -496,6 +501,13 @@ class AgentHub:
processing_time_ms = (time.time() - start_time) * 1000 processing_time_ms = (time.time() - start_time) * 1000
logger.info(
"[process_message 完成] session=%s call_count=%d response_len=%d time=%.0fms",
session_id, call_count,
len(response_content) if response_content else 0,
processing_time_ms,
)
return { return {
"session_id": session.session_id, "session_id": session.session_id,
"response_agent_code": target_agent, "response_agent_code": target_agent,
...@@ -522,7 +534,7 @@ class AgentHub: ...@@ -522,7 +534,7 @@ class AgentHub:
if block_type == "tool_use": if block_type == "tool_use":
inp = block.get("input", {}) inp = block.get("input", {})
name = block.get("name", "") name = block.get("name", "")
memory.add("user", str(inp), agent_code=default_agent_code or name) memory.add("assistant", str(inp), agent_code=default_agent_code or name)
elif block_type == "tool_result": elif block_type == "tool_result":
output = block.get("output", "") output = block.get("output", "")
if isinstance(output, list): if isinstance(output, list):
...@@ -563,6 +575,8 @@ class AgentHub: ...@@ -563,6 +575,8 @@ class AgentHub:
sub_prompt = self._build_sub_agent_prompt(agent_code) sub_prompt = self._build_sub_agent_prompt(agent_code)
sub_memory = InMemoryMemory() sub_memory = InMemoryMemory()
logger.info("[子Agent 开始] agent_code=%s instruction=%s", agent_code, instruction[:80])
sub_agent = ReActAgent( sub_agent = ReActAgent(
name=agent_code, name=agent_code,
sys_prompt=sub_prompt, sys_prompt=sub_prompt,
...@@ -579,6 +593,7 @@ class AgentHub: ...@@ -579,6 +593,7 @@ class AgentHub:
response = await sub_agent.reply(Msg("user", instruction, "user")) response = await sub_agent.reply(Msg("user", instruction, "user"))
response_text = response.get_text_content() response_text = response.get_text_content()
logger.info("[子Agent 完成] agent_code=%s response_len=%d", agent_code, len(response_text) if response_text else 0)
# 子Agent 的 ReAct 消息落地到同一 session # 子Agent 的 ReAct 消息落地到同一 session
sub_msgs = await sub_agent.memory.get_memory() sub_msgs = await sub_agent.memory.get_memory()
...@@ -703,8 +718,19 @@ class AgentHub: ...@@ -703,8 +718,19 @@ class AgentHub:
ctx = getattr(self, "_current_route_ctx", None) ctx = getattr(self, "_current_route_ctx", None)
if ctx: if ctx:
ctx["call_count"] = ctx.get("call_count", 0) + 1 ctx["call_count"] = ctx.get("call_count", 0) + 1
call_no = ctx["call_count"]
else:
call_no = 0
logger.info(
"[call_sub_agent #%d] agent_code=%s instruction=%s",
call_no, agent_code, instruction[:80],
)
result = await self._call_sub_agent(agent_code, instruction) result = await self._call_sub_agent(agent_code, instruction)
logger.info(
"[call_sub_agent #%d 完成] agent_code=%s result_len=%d",
call_no, agent_code, len(result),
)
return ToolResponse( return ToolResponse(
content=[TextBlock(type="text", text=result)] content=[TextBlock(type="text", text=result)]
) )
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!