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 51d51f3a
authored
Jun 15, 2026
by
sunqi02
Committed by
胡悦
Jul 01, 2026
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat: 注入发言人 Agent 并重建所有 agent 的专属 toolkit,增加热加载功能
1 parent
01997231
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
43 deletions
workspace/agent-admin/backend/app/main.py
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/app/main.py
View file @
51d51f3
...
...
@@ -97,6 +97,7 @@ async def lifespan(app: FastAPI):
session_service
=
session_service
,
im_service
=
im_service
,
preloaded_agents
=
agents
,
db_factory
=
db_factory
,
)
# ── MessageGate 实例化 ──
...
...
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
51d51f3
import
logging
import
time
from
datetime
import
datetime
,
timezone
from
functools
import
partial
from
pathlib
import
Path
from
typing
import
Dict
,
List
,
Optional
,
Any
...
...
@@ -130,35 +131,19 @@ class AgentHub:
session_service
:
ActiveSessionService
,
im_service
,
preloaded_agents
:
Optional
[
Dict
[
str
,
Any
]]
=
None
,
db_factory
=
None
,
):
self
.
config_loader
=
config_loader
self
.
session_service
=
session_service
self
.
im_service
=
im_service
self
.
_db_factory
=
db_factory
if
preloaded_agents
is
None
:
raise
AgentHubError
(
"preloaded_agents is required — load from DB via ConfigLoader.load_agents_from_db()"
)
self
.
agents
:
Dict
[
str
,
Any
]
=
preloaded_agents
# 发言人(口)Agent:框架级固定构件,不依赖数据库
self
.
agents
[
"-1"
]
=
AgentConfig
(
name
=
"发言人"
,
code
=
"-1"
,
prompt
=
(
"你是发言人,代表主Agent与用户对话。
\n\n
"
"重要前提:各子Agent已经用自己的身份把处理结果直接发给了用户,"
"用户已经看到了。你不要重复子Agent已经发过的内容。
\n\n
"
"你的工作:
\n
"
"1. 子Agent已发的内容 → 一句话自然带过"
"(如'纪要助手刚发了文档,你看一下'),不要重新总结
\n
"
"2. 主Agent的补充分析、建议、追问 → 这是重点,展开说
\n
"
"3. 如果主Agent没有额外内容要补充 → 简单收尾即可,"
"如'有需要再找我'
\n\n
"
"禁止:重复总结子Agent已发内容、逐条列举各子Agent结果、"
"重新执行子Agent的任务"
),
description
=
"代表主Agent与用户对话,补充子Agent未覆盖的内容"
,
is_steward
=
False
,
)
self
.
_inject_mouth_agent
()
self
.
main_model_config
=
config_loader
.
load_models
()[
"main"
]
...
...
@@ -281,30 +266,9 @@ class AgentHub:
# 构建每个 agent 的专属 toolkit(按 skills 字段过滤)
self
.
agent_toolkits
:
Dict
[
str
,
Toolkit
]
=
{}
for
agent_code
,
agent_config
in
self
.
agents
.
items
():
logger
.
info
(
f
"构建 agent '{agent_code}' 的专属 Toolkit (skills: {agent_config.skills})"
)
agent_tk
=
Toolkit
()
# 复制 basic 组工具(所有 agent 共用)
for
name
,
tool
in
self
.
toolkit
.
tools
.
items
():
if
tool
.
group
==
"basic"
:
# 口 agent 不需要 call_sub_agent 工具
if
agent_code
==
"-1"
and
name
==
"call_sub_agent"
:
continue
agent_tk
.
register_tool_function
(
tool
.
original_func
,
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
:
if
skill_name
in
self
.
toolkit
.
skills
:
skill_info
=
self
.
toolkit
.
skills
[
skill_name
]
skill_dir
=
skill_info
.
get
(
"dir"
,
""
)
if
skill_dir
:
agent_tk
.
register_agent_skill
(
skill_dir
)
self
.
agent_toolkits
[
agent_code
]
=
agent_tk
self
.
_build_all_agent_toolkits
()
self
.
_agents_loaded_at
=
datetime
.
now
(
timezone
.
utc
)
.
replace
(
tzinfo
=
None
)
def
_register_im_hooks
(
self
,
...
...
@@ -341,6 +305,104 @@ class AgentHub:
return
key
raise
AgentHubError
(
"未找到主 Agent(is_steward=True)"
)
def
_inject_mouth_agent
(
self
):
"""注入发言人 Agent(框架级固定构件,不依赖数据库)。"""
self
.
agents
[
"-1"
]
=
AgentConfig
(
name
=
"发言人"
,
code
=
"-1"
,
prompt
=
(
"你是发言人,代表主Agent与用户对话。
\n\n
"
"重要前提:各子Agent已经用自己的身份把处理结果直接发给了用户,"
"用户已经看到了。你不要重复子Agent已经发过的内容。
\n\n
"
"你的工作:
\n
"
"1. 子Agent已发的内容 → 一句话自然带过"
"(如'纪要助手刚发了文档,你看一下'),不要重新总结
\n
"
"2. 主Agent的补充分析、建议、追问 → 这是重点,展开说
\n
"
"3. 如果主Agent没有额外内容要补充 → 简单收尾即可,"
"如'有需要再找我'
\n\n
"
"禁止:重复总结子Agent已发内容、逐条列举各子Agent结果、"
"重新执行子Agent的任务"
),
description
=
"代表主Agent与用户对话,补充子Agent未覆盖的内容"
,
is_steward
=
False
,
)
def
_build_all_agent_toolkits
(
self
):
"""根据当前 self.agents 重建所有 agent 的专属 toolkit。"""
self
.
agent_toolkits
.
clear
()
for
agent_code
,
agent_config
in
self
.
agents
.
items
():
logger
.
info
(
"构建 agent '
%
s' 的专属 Toolkit (skills:
%
s)"
,
agent_code
,
agent_config
.
skills
,
)
agent_tk
=
Toolkit
()
for
name
,
tool
in
self
.
toolkit
.
tools
.
items
():
if
tool
.
group
==
"basic"
:
if
agent_code
==
"-1"
and
name
==
"call_sub_agent"
:
continue
agent_tk
.
register_tool_function
(
tool
.
original_func
,
group_name
=
tool
.
group
,
func_name
=
tool
.
name
,
preset_kwargs
=
tool
.
preset_kwargs
,
json_schema
=
tool
.
json_schema
,
)
for
skill_name
in
agent_config
.
skills
:
if
skill_name
in
self
.
toolkit
.
skills
:
skill_info
=
self
.
toolkit
.
skills
[
skill_name
]
skill_dir
=
skill_info
.
get
(
"dir"
,
""
)
if
skill_dir
:
agent_tk
.
register_agent_skill
(
skill_dir
)
self
.
agent_toolkits
[
agent_code
]
=
agent_tk
async
def
_maybe_reload_agents
(
self
):
"""检查 ai_agents.updated_at,有变更则热加载 agent 配置。
最多每 30 秒检查一次,避免每条消息都查 DB。
"""
if
not
self
.
_db_factory
:
return
now
=
datetime
.
now
(
timezone
.
utc
)
.
replace
(
tzinfo
=
None
)
last_check
=
getattr
(
self
,
"_agents_last_check_at"
,
None
)
if
last_check
and
(
now
-
last_check
)
.
total_seconds
()
<
30
:
return
self
.
_agents_last_check_at
=
now
try
:
with
self
.
_db_factory
()
as
db
:
from
app.model.agent
import
Agent
from
sqlalchemy
import
func
max_updated
=
(
db
.
query
(
func
.
max
(
Agent
.
updated_at
))
.
filter
(
Agent
.
is_deleted
==
False
)
.
scalar
()
)
if
(
max_updated
and
self
.
_agents_loaded_at
and
max_updated
>
self
.
_agents_loaded_at
):
logger
.
info
(
"检测到 ai_agents 变更 (max_updated=
%
s > loaded_at=
%
s),重新加载"
,
max_updated
,
self
.
_agents_loaded_at
,
)
self
.
_reload_agents
()
except
Exception
:
logger
.
warning
(
"检查 agent 更新失败,跳过本次热加载"
,
exc_info
=
True
)
def
_reload_agents
(
self
):
"""从 DB 重新加载 agent 配置,重建 agents 和 toolkits。"""
with
self
.
_db_factory
()
as
db
:
new_agents
=
self
.
config_loader
.
load_agents_from_db
(
db
)
self
.
agents
=
new_agents
self
.
_inject_mouth_agent
()
self
.
_build_all_agent_toolkits
()
self
.
_agents_loaded_at
=
datetime
.
now
(
timezone
.
utc
)
.
replace
(
tzinfo
=
None
)
logger
.
info
(
"Agent 配置已热加载:
%
d 个"
,
len
(
self
.
agents
))
def
_build_main_prompt
(
self
,
allowed_agent_codes
:
Optional
[
list
]
=
None
,
...
...
@@ -411,6 +473,8 @@ class AgentHub:
"""
start_time
=
time
.
time
()
await
self
.
_maybe_reload_agents
()
logger
.
info
(
"[process_message 开始] session=
%
s user_query=
%
s file_url=
%
s"
,
session_id
,
user_query
[:
100
],
file_url
,
...
...
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