Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ai
/
ai-workflow
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit c8fb2938
authored
May 26, 2026
by
sunqi02
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix(agent_hub): 添加异常处理以确保 IM 消息发送失败时不影响推理流程
1 parent
16b29675
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
22 deletions
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
c8fb293
...
@@ -45,10 +45,16 @@ async def _im_post_reasoning_hook(
...
@@ -45,10 +45,16 @@ async def _im_post_reasoning_hook(
# 删除"下载"这个词
# 删除"下载"这个词
content
=
content
.
replace
(
"下载"
,
"点击"
)
content
=
content
.
replace
(
"下载"
,
"点击"
)
logger
.
info
(
f
"[Hook 发送 IM] agent={self.name} bot_id={bot_id} content={content[:50]}..."
)
logger
.
info
(
f
"[Hook 发送 IM] agent={self.name} bot_id={bot_id} content={content[:50]}..."
)
await
im_service
.
send_message
(
try
:
bot_id
=
bot_id
,
content
=
content
,
await
im_service
.
send_message
(
contact_id
=
room_id
,
at_list
=
""
,
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
(
async
def
_im_pre_acting_hook
(
...
@@ -65,10 +71,16 @@ async def _im_pre_acting_hook(
...
@@ -65,10 +71,16 @@ async def _im_pre_acting_hook(
return
None
return
None
content
=
str
(
tool_call
)
content
=
str
(
tool_call
)
logger
.
info
(
f
"[Hook 发送 IM] agent={self.name} bot_id={bot_id} tool_call={tool_call}"
)
logger
.
info
(
f
"[Hook 发送 IM] agent={self.name} bot_id={bot_id} tool_call={tool_call}"
)
await
im_service
.
send_message
(
try
:
bot_id
=
bot_id
,
content
=
content
,
await
im_service
.
send_message
(
contact_id
=
room_id
,
at_list
=
""
,
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
):
class
AgentHubError
(
Exception
):
...
@@ -150,6 +162,27 @@ class AgentHub:
...
@@ -150,6 +162,27 @@ class AgentHub:
call_sub
,
call_sub
,
func_name
=
"call_sub_agent"
,
func_name
=
"call_sub_agent"
,
group_name
=
"basic"
,
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
# Register sandboxed read_file tool for reading project files
...
@@ -173,6 +206,7 @@ class AgentHub:
...
@@ -173,6 +206,7 @@ class AgentHub:
group_name
=
tool
.
group
,
group_name
=
tool
.
group
,
func_name
=
tool
.
name
,
func_name
=
tool
.
name
,
preset_kwargs
=
tool
.
preset_kwargs
,
preset_kwargs
=
tool
.
preset_kwargs
,
json_schema
=
tool
.
json_schema
,
)
)
# 只复制该 agent 配置的 skills
# 只复制该 agent 配置的 skills
for
skill_name
in
agent_config
.
skills
:
for
skill_name
in
agent_config
.
skills
:
...
@@ -229,9 +263,10 @@ class AgentHub:
...
@@ -229,9 +263,10 @@ class AgentHub:
continue
continue
if
allowed_agent_codes
is
not
None
and
code
not
in
allowed_agent_codes
:
if
allowed_agent_codes
is
not
None
and
code
not
in
allowed_agent_codes
:
continue
continue
# 使用简洁职责描述,避免原样拼入 prompt_template 中的 {company}/{duties} 占位符
sub_desc
=
f
"负责{agent.role}相关任务"
duty_lines
.
append
(
duty_lines
.
append
(
f
"- {agent.name}({agent.role},调用码: {code}): "
f
"- {agent.name}(调用码: {code}): {sub_desc}"
f
"{agent.prompt_template}"
)
)
duties_text
=
"
\n
"
.
join
(
duty_lines
)
if
duty_lines
else
"暂无子Agent"
duties_text
=
"
\n
"
.
join
(
duty_lines
)
if
duty_lines
else
"暂无子Agent"
...
@@ -384,13 +419,19 @@ class AgentHub:
...
@@ -384,13 +419,19 @@ class AgentHub:
self
.
session_service
.
save_to_db
(
session_id
)
self
.
session_service
.
save_to_db
(
session_id
)
# Send via hook
# Send via hook
await
self
.
post_reply_hook
.
execute
({
try
:
"agent_name"
:
target_agent
,
await
self
.
post_reply_hook
.
execute
({
"response_content"
:
response_content
,
"agent_name"
:
target_agent
,
"room_id"
:
room_id
,
"response_content"
:
response_content
,
"mentioned_list"
:
[
user_id
],
"room_id"
:
room_id
,
"bot_id_mapping"
:
bot_id_mapping
or
{},
"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
processing_time_ms
=
(
time
.
time
()
-
start_time
)
*
1000
...
@@ -506,16 +547,25 @@ class AgentHub:
...
@@ -506,16 +547,25 @@ class AgentHub:
def
_make_call_sub_agent_tool
(
self
):
def
_make_call_sub_agent_tool
(
self
):
"""Create closure tool function for registration to Toolkit."""
"""Create closure tool function for registration to Toolkit."""
async
def
call_sub_agent
(
agent_code
:
str
,
instruction
:
str
)
->
ToolResponse
:
async
def
call_sub_agent
(
agent_code
:
str
=
""
,
instruction
:
str
=
""
)
->
ToolResponse
:
"""Call a sub-agent to handle a specific task.
"""调用子Agent处理特定任务。
当用户问题属于某个子Agent的职责范围时,必须调用此工具委托处理。
Args:
Args:
agent_code:
Sub-agent code, e.g. meeting / support
agent_code:
子Agent调用码,必须从可用子Agent列表中选择,如 "meeting"
instruction:
Specific instructions for the sub-agent
instruction:
给子Agent的具体指令,必须包含用户需求的完整描述
Returns:
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
)
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
)]
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment