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 041c2ecc
authored
Jun 30, 2026
by
sunqi02
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat: 增加对下线/不可用Agent的支持,优化主Agent提示信息
1 parent
b5fef9ce
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
6 deletions
workspace/agent-admin/backend/app/agent_core/agent_hub.py
workspace/agent-admin/backend/app/agent_core/message_gate.py
workspace/agent-admin/backend/app/api/v1/endpoints/agent_hub.py
workspace/agent-admin/backend/test_request.json
workspace/agent-admin/backend/app/agent_core/agent_hub.py
View file @
041c2ec
...
@@ -525,10 +525,12 @@ class AgentHub:
...
@@ -525,10 +525,12 @@ class AgentHub:
def
_build_main_prompt
(
def
_build_main_prompt
(
self
,
self
,
allowed_agent_codes
:
Optional
[
list
]
=
None
,
allowed_agent_codes
:
Optional
[
list
]
=
None
,
offline_agents_info
:
Optional
[
list
]
=
None
,
)
->
str
:
)
->
str
:
"""
"""
Build the main agent's prompt including sub-agent duty descriptions.
Build the main agent's prompt including sub-agent duty descriptions.
When allowed_agent_codes is set, only agents in that list are advertised.
When allowed_agent_codes is set, only agents in that list are advertised.
offline_agents_info provides names of disabled/deleted agents for user notification.
"""
"""
steward
=
self
.
_find_steward_agent
()
steward
=
self
.
_find_steward_agent
()
prompt
=
steward
.
prompt
# 角色定义
prompt
=
steward
.
prompt
# 角色定义
...
@@ -549,6 +551,16 @@ class AgentHub:
...
@@ -549,6 +551,16 @@ class AgentHub:
if
sub_lines
:
if
sub_lines
:
prompt
+=
"
\n\n
## 可调用的子Agent
\n
"
+
"
\n
"
.
join
(
sub_lines
)
prompt
+=
"
\n\n
## 可调用的子Agent
\n
"
+
"
\n
"
.
join
(
sub_lines
)
# Inform about offline agents so the main agent can tell users
if
offline_agents_info
:
prompt
+=
"
\n\n
## 已下线/不可用的Agent"
prompt
+=
"
\n
以下Agent已下线,不可调用。"
prompt
+=
"
\n\n
当用户询问涉及以下Agent的事务时,请按以下模板回复:"
prompt
+=
"
\n
「[Agent名称]当前已下线,暂不可用。如需帮助,可以直接向我说明,我会为您处理。」"
prompt
+=
"
\n\n
已下线Agent列表:"
for
info
in
offline_agents_info
:
prompt
+=
f
"
\n
- {info['name']}(原调用码: {info['code']}): {info['description']}"
return
prompt
+
self
.
soul_guide
return
prompt
+
self
.
soul_guide
def
_build_sub_agent_prompt
(
self
,
agent_code
:
str
)
->
str
:
def
_build_sub_agent_prompt
(
self
,
agent_code
:
str
)
->
str
:
...
@@ -569,6 +581,7 @@ class AgentHub:
...
@@ -569,6 +581,7 @@ class AgentHub:
channel
:
str
=
"wechat_group"
,
channel
:
str
=
"wechat_group"
,
file_url
:
Optional
[
str
]
=
None
,
file_url
:
Optional
[
str
]
=
None
,
allowed_agent_codes
:
Optional
[
list
]
=
None
,
allowed_agent_codes
:
Optional
[
list
]
=
None
,
offline_agents_info
:
Optional
[
list
]
=
None
,
bot_id_mapping
:
Optional
[
Dict
[
str
,
str
]]
=
None
,
bot_id_mapping
:
Optional
[
Dict
[
str
,
str
]]
=
None
,
)
->
Dict
[
str
,
Any
]:
)
->
Dict
[
str
,
Any
]:
"""
"""
...
@@ -638,7 +651,10 @@ class AgentHub:
...
@@ -638,7 +651,10 @@ class AgentHub:
# Build prompt based on target agent
# Build prompt based on target agent
if
self
.
agents
[
target_agent
]
.
is_steward
:
if
self
.
agents
[
target_agent
]
.
is_steward
:
system_prompt
=
self
.
_build_main_prompt
(
allowed_agent_codes
=
allowed_agent_codes
)
system_prompt
=
self
.
_build_main_prompt
(
allowed_agent_codes
=
allowed_agent_codes
,
offline_agents_info
=
offline_agents_info
,
)
else
:
else
:
system_prompt
=
self
.
_build_sub_agent_prompt
(
target_agent
)
system_prompt
=
self
.
_build_sub_agent_prompt
(
target_agent
)
...
...
workspace/agent-admin/backend/app/agent_core/message_gate.py
View file @
041c2ec
...
@@ -88,6 +88,7 @@ class MessageGate:
...
@@ -88,6 +88,7 @@ class MessageGate:
user_id
:
str
,
user_id
:
str
,
bot_id_mapping
:
Dict
[
str
,
str
],
bot_id_mapping
:
Dict
[
str
,
str
],
wait_seconds
:
float
=
6.0
,
wait_seconds
:
float
=
6.0
,
offline_agents_info
:
Optional
[
list
]
=
None
,
)
->
None
:
)
->
None
:
"""评估入口:等待、分类、重写、分发。
"""评估入口:等待、分类、重写、分发。
...
@@ -184,6 +185,7 @@ class MessageGate:
...
@@ -184,6 +185,7 @@ class MessageGate:
query
=
query
,
query
=
query
,
file_url
=
file_url
,
file_url
=
file_url
,
bot_id_mapping
=
bot_id_mapping
,
bot_id_mapping
=
bot_id_mapping
,
offline_agents_info
=
offline_agents_info
,
)
)
finally
:
finally
:
self
.
_running_tasks
.
pop
(
session_id
,
None
)
self
.
_running_tasks
.
pop
(
session_id
,
None
)
...
@@ -375,6 +377,7 @@ class MessageGate:
...
@@ -375,6 +377,7 @@ class MessageGate:
query
:
str
,
query
:
str
,
file_url
:
Optional
[
str
],
file_url
:
Optional
[
str
],
bot_id_mapping
:
Dict
[
str
,
str
],
bot_id_mapping
:
Dict
[
str
,
str
],
offline_agents_info
:
Optional
[
list
]
=
None
,
)
->
None
:
)
->
None
:
"""pass 分支:分发到 AgentHub。query/file_url 由调用方在锁内预先提取。"""
"""pass 分支:分发到 AgentHub。query/file_url 由调用方在锁内预先提取。"""
if
not
query
:
if
not
query
:
...
@@ -389,6 +392,7 @@ class MessageGate:
...
@@ -389,6 +392,7 @@ class MessageGate:
user_query
=
query
,
user_query
=
query
,
file_url
=
file_url
,
file_url
=
file_url
,
allowed_agent_codes
=
None
,
# Phase 1 简化:不传
allowed_agent_codes
=
None
,
# Phase 1 简化:不传
offline_agents_info
=
offline_agents_info
,
bot_id_mapping
=
bot_id_mapping
,
bot_id_mapping
=
bot_id_mapping
,
)
)
except
asyncio
.
CancelledError
:
except
asyncio
.
CancelledError
:
...
...
workspace/agent-admin/backend/app/api/v1/endpoints/agent_hub.py
View file @
041c2ec
...
@@ -29,6 +29,8 @@ class AgentInfo(BaseModel):
...
@@ -29,6 +29,8 @@ class AgentInfo(BaseModel):
agent_name
:
Optional
[
str
]
=
Field
(
None
,
description
=
"Agent 名称"
)
agent_name
:
Optional
[
str
]
=
Field
(
None
,
description
=
"Agent 名称"
)
bot_id
:
Optional
[
str
]
=
Field
(
None
,
description
=
"机器人编码"
)
bot_id
:
Optional
[
str
]
=
Field
(
None
,
description
=
"机器人编码"
)
is_steward
:
Optional
[
bool
]
=
Field
(
False
,
description
=
"是否为管家 Agent"
)
is_steward
:
Optional
[
bool
]
=
Field
(
False
,
description
=
"是否为管家 Agent"
)
status
:
Optional
[
str
]
=
Field
(
"enable"
,
description
=
"状态: enable/disable"
)
is_deleted
:
Optional
[
bool
]
=
Field
(
False
,
description
=
"是否已删除"
)
class
MessageInfo
(
BaseModel
):
class
MessageInfo
(
BaseModel
):
...
@@ -95,8 +97,9 @@ async def chat(
...
@@ -95,8 +97,9 @@ async def chat(
# 2. 提取 user_id:取最后一条消息的 sender_id
# 2. 提取 user_id:取最后一条消息的 sender_id
user_id
=
request
.
messages
[
-
1
]
.
sender_id
user_id
=
request
.
messages
[
-
1
]
.
sender_id
# 3. 提取 allowed_agent_codes
和
bot_id_mapping
# 3. 提取 allowed_agent_codes
/ offline_agents_info /
bot_id_mapping
allowed_agent_codes
:
Optional
[
list
]
=
None
allowed_agent_codes
:
Optional
[
list
]
=
None
offline_agents_info
:
Optional
[
list
]
=
None
bot_id_mapping
:
dict
=
{}
bot_id_mapping
:
dict
=
{}
if
request
.
agents
:
if
request
.
agents
:
# 构建 AgentConfig.code → dict key 映射
# 构建 AgentConfig.code → dict key 映射
...
@@ -104,16 +107,35 @@ async def chat(
...
@@ -104,16 +107,35 @@ async def chat(
cfg
.
code
:
key
for
key
,
cfg
in
agent_hub
.
agents
.
items
()
cfg
.
code
:
key
for
key
,
cfg
in
agent_hub
.
agents
.
items
()
}
}
allowed_agent_codes
=
[]
allowed_agent_codes
=
[]
offline_agents_info
=
[]
for
agent_info
in
request
.
agents
:
for
agent_info
in
request
.
agents
:
agent_id_str
=
str
(
agent_info
.
agent_id
)
agent_id_str
=
str
(
agent_info
.
agent_id
)
agent_key
=
code_to_key
.
get
(
agent_id_str
)
agent_key
=
code_to_key
.
get
(
agent_id_str
)
if
agent_key
and
not
agent_hub
.
agents
[
agent_key
]
.
is_steward
:
if
not
agent_key
:
continue
# 过滤已下线/已删除的 agent,构建下线信息供主 Agent 知晓
is_offline
=
agent_info
.
status
!=
"enable"
or
agent_info
.
is_deleted
if
is_offline
and
not
agent_hub
.
agents
[
agent_key
]
.
is_steward
:
agent_cfg
=
agent_hub
.
agents
[
agent_key
]
offline_agents_info
.
append
({
"name"
:
agent_cfg
.
name
,
"code"
:
agent_key
,
"description"
:
agent_cfg
.
description
or
f
"{agent_cfg.name}相关任务"
,
})
if
agent_info
.
bot_id
:
bot_id_mapping
[
agent_key
]
=
agent_info
.
bot_id
continue
if
not
agent_hub
.
agents
[
agent_key
]
.
is_steward
:
allowed_agent_codes
.
append
(
agent_key
)
allowed_agent_codes
.
append
(
agent_key
)
# 提取 bot_id 映射:{dict_key: bot_id}
# 提取 bot_id 映射:{dict_key: bot_id}
if
agent_key
and
agent_info
.
bot_id
:
if
agent_key
and
agent_info
.
bot_id
:
bot_id_mapping
[
agent_key
]
=
agent_info
.
bot_id
bot_id_mapping
[
agent_key
]
=
agent_info
.
bot_id
if
not
allowed_agent_codes
:
if
not
allowed_agent_codes
:
allowed_agent_codes
=
None
# 无有效子 agent,走默认路由
allowed_agent_codes
=
None
# 无有效子 agent,走默认路由
if
not
offline_agents_info
:
offline_agents_info
=
None
# 后端自动补全发言人口 agent code,确保主Agent 始终可调用发言人
# 后端自动补全发言人口 agent code,确保主Agent 始终可调用发言人
MOUTH_AGENT_CODE
=
"-1"
MOUTH_AGENT_CODE
=
"-1"
...
@@ -151,6 +173,7 @@ async def chat(
...
@@ -151,6 +173,7 @@ async def chat(
user_id
=
user_id
,
user_id
=
user_id
,
messages
=
request
.
messages
,
messages
=
request
.
messages
,
bot_id_mapping
=
bot_id_mapping
,
bot_id_mapping
=
bot_id_mapping
,
offline_agents_info
=
offline_agents_info
,
)
)
return
{
"status"
:
"ok"
,
"session_id"
:
session_id
}
return
{
"status"
:
"ok"
,
"session_id"
:
session_id
}
...
@@ -164,6 +187,7 @@ async def _process_message_async_via_gate(
...
@@ -164,6 +187,7 @@ async def _process_message_async_via_gate(
user_id
:
str
,
user_id
:
str
,
messages
:
list
,
messages
:
list
,
bot_id_mapping
:
Optional
[
dict
]
=
None
,
bot_id_mapping
:
Optional
[
dict
]
=
None
,
offline_agents_info
:
Optional
[
list
]
=
None
,
):
):
"""通过 MessageGate 评估后再决定是否走 AgentHub。"""
"""通过 MessageGate 评估后再决定是否走 AgentHub。"""
try
:
try
:
...
@@ -172,6 +196,7 @@ async def _process_message_async_via_gate(
...
@@ -172,6 +196,7 @@ async def _process_message_async_via_gate(
room_id
=
room_id
,
room_id
=
room_id
,
user_id
=
user_id
,
user_id
=
user_id
,
bot_id_mapping
=
bot_id_mapping
or
{},
bot_id_mapping
=
bot_id_mapping
or
{},
offline_agents_info
=
offline_agents_info
,
)
)
except
Exception
:
except
Exception
:
logger
.
exception
(
"MessageGate 评估失败,尝试兜底"
)
logger
.
exception
(
"MessageGate 评估失败,尝试兜底"
)
...
@@ -195,6 +220,7 @@ async def _process_message_async_via_gate(
...
@@ -195,6 +220,7 @@ async def _process_message_async_via_gate(
user_query
=
query
,
user_query
=
query
,
file_url
=
file_url
,
file_url
=
file_url
,
allowed_agent_codes
=
allowed_agent_codes
,
allowed_agent_codes
=
allowed_agent_codes
,
offline_agents_info
=
offline_agents_info
,
bot_id_mapping
=
bot_id_mapping
or
{},
bot_id_mapping
=
bot_id_mapping
or
{},
)
)
except
Exception
:
except
Exception
:
...
...
workspace/agent-admin/backend/test_request.json
View file @
041c2ec
...
@@ -4,9 +4,9 @@
...
@@ -4,9 +4,9 @@
"group_name"
:
"岗位销售新服务群"
"group_name"
:
"岗位销售新服务群"
},
},
"agents"
:
[
"agents"
:
[
{
"agent_id"
:
11
,
"bot_id"
:
"5bcf81fa616f4b5aa836cc1f28eaa941"
,
"is_steward"
:
true
},
{
"agent_id"
:
11
,
"bot_id"
:
"5bcf81fa616f4b5aa836cc1f28eaa941"
,
"is_steward"
:
true
,
"status"
:
"enable"
,
"is_deleted"
:
false
},
{
"agent_id"
:
17
,
"bot_id"
:
"e02a9b3a27564c87875bfb5e74ffb564"
,
"is_steward"
:
false
},
{
"agent_id"
:
17
,
"bot_id"
:
"e02a9b3a27564c87875bfb5e74ffb564"
,
"is_steward"
:
false
,
"status"
:
"enable"
,
"is_deleted"
:
false
},
{
"agent_id"
:
21
,
"bot_id"
:
"ee79dee25ce24003b5d14d8235240bc2"
,
"is_steward"
:
false
}
{
"agent_id"
:
21
,
"bot_id"
:
"ee79dee25ce24003b5d14d8235240bc2"
,
"is_steward"
:
false
,
"status"
:
"enable"
,
"is_deleted"
:
false
}
],
],
"messages"
:
[
"messages"
:
[
{
{
...
...
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