Commit c8fb2938 by sunqi02

fix(agent_hub): 添加异常处理以确保 IM 消息发送失败时不影响推理流程

1 parent 16b29675
......@@ -45,10 +45,16 @@ async def _im_post_reasoning_hook(
# 删除"下载"这个词
content = content.replace("下载", "点击")
logger.info(f"[Hook 发送 IM] agent={self.name} bot_id={bot_id} content={content[:50]}...")
await im_service.send_message(
bot_id=bot_id, content=content,
contact_id=room_id, at_list="",
)
try:
await im_service.send_message(
bot_id=bot_id, content=content,
contact_id=room_id, at_list="",
)
except Exception:
logger.warning(
"[Hook IM 发送失败] agent=%s room=%s(不影响推理流程)",
self.name, room_id, exc_info=True,
)
async def _im_pre_acting_hook(
......@@ -65,10 +71,16 @@ async def _im_pre_acting_hook(
return None
content = str(tool_call)
logger.info(f"[Hook 发送 IM] agent={self.name} bot_id={bot_id} tool_call={tool_call}")
await im_service.send_message(
bot_id=bot_id, content=content,
contact_id=room_id, at_list="",
)
try:
await im_service.send_message(
bot_id=bot_id, content=content,
contact_id=room_id, at_list="",
)
except Exception:
logger.warning(
"[Hook IM 发送失败] agent=%s room=%s(不影响推理流程)",
self.name, room_id, exc_info=True,
)
class AgentHubError(Exception):
......@@ -150,6 +162,27 @@ class AgentHub:
call_sub,
func_name="call_sub_agent",
group_name="basic",
json_schema={
"type": "function",
"function": {
"name": "call_sub_agent",
"description": "调用子Agent处理特定任务。当用户问题属于某个子Agent的职责范围时,必须调用此工具委托处理。",
"parameters": {
"type": "object",
"properties": {
"agent_code": {
"type": "string",
"description": "子Agent调用码,必须从可用子Agent列表中选择,如 \"meeting\"",
},
"instruction": {
"type": "string",
"description": "给子Agent的具体指令,必须包含用户需求的完整描述",
},
},
"required": ["agent_code", "instruction"],
},
},
},
)
# Register sandboxed read_file tool for reading project files
......@@ -173,6 +206,7 @@ class AgentHub:
group_name=tool.group,
func_name=tool.name,
preset_kwargs=tool.preset_kwargs,
json_schema=tool.json_schema,
)
# 只复制该 agent 配置的 skills
for skill_name in agent_config.skills:
......@@ -229,9 +263,10 @@ class AgentHub:
continue
if allowed_agent_codes is not None and code not in allowed_agent_codes:
continue
# 使用简洁职责描述,避免原样拼入 prompt_template 中的 {company}/{duties} 占位符
sub_desc = f"负责{agent.role}相关任务"
duty_lines.append(
f"- {agent.name}({agent.role},调用码: {code}): "
f"{agent.prompt_template}"
f"- {agent.name}(调用码: {code}): {sub_desc}"
)
duties_text = "\n".join(duty_lines) if duty_lines else "暂无子Agent"
......@@ -384,13 +419,19 @@ class AgentHub:
self.session_service.save_to_db(session_id)
# Send via hook
await self.post_reply_hook.execute({
"agent_name": target_agent,
"response_content": response_content,
"room_id": room_id,
"mentioned_list": [user_id],
"bot_id_mapping": bot_id_mapping or {},
})
try:
await self.post_reply_hook.execute({
"agent_name": target_agent,
"response_content": response_content,
"room_id": room_id,
"mentioned_list": [user_id],
"bot_id_mapping": bot_id_mapping or {},
})
except Exception:
logger.warning(
"[最终回复 IM 发送失败] agent=%s session=%s room=%s",
target_agent, session_id, room_id, exc_info=True,
)
processing_time_ms = (time.time() - start_time) * 1000
......@@ -506,16 +547,25 @@ class AgentHub:
def _make_call_sub_agent_tool(self):
"""Create closure tool function for registration to Toolkit."""
async def call_sub_agent(agent_code: str, instruction: str) -> ToolResponse:
"""Call a sub-agent to handle a specific task.
async def call_sub_agent(agent_code: str = "", instruction: str = "") -> ToolResponse:
"""调用子Agent处理特定任务。
当用户问题属于某个子Agent的职责范围时,必须调用此工具委托处理。
Args:
agent_code: Sub-agent code, e.g. meeting / support
instruction: Specific instructions for the sub-agent
agent_code: 子Agent调用码,必须从可用子Agent列表中选择,如 "meeting"
instruction: 给子Agent的具体指令,必须包含用户需求的完整描述
Returns:
Sub-agent's processing result
子Agent的处理结果
"""
if not agent_code or not instruction:
return ToolResponse(
content=[TextBlock(type="text", text=(
"错误:agent_code 和 instruction 都是必填参数。\n"
"正确示例:call_sub_agent(agent_code=\"meeting\", instruction=\"请帮我整理会议纪要\")"
))]
)
result = await self._call_sub_agent(agent_code, instruction)
return ToolResponse(
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!