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 fbfe63e4
authored
Jun 02, 2026
by
sunqi02
Committed by
胡悦
Jul 01, 2026
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat(agent_hub): 添加思考摘要功能并更新相关文档
1 parent
9fda9de9
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
145 additions
and
56 deletions
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/config/models.json
workspace/agent-admin/backend/soul/agent.md
workspace/agent-admin/backend/soul/thinking-summarize.md
workspace/agent-admin/backend/tests/test_agent_hub.py
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
fbfe63e
...
@@ -41,9 +41,9 @@ THINKING_GUIDE = """
...
@@ -41,9 +41,9 @@ THINKING_GUIDE = """
async
def
_im_post_reasoning_hook
(
async
def
_im_post_reasoning_hook
(
self
,
kwargs
,
output
,
self
,
kwargs
,
output
,
*
,
im_service
,
room_id
,
user_id
,
bot_id_mapping
,
*
,
im_service
,
room_id
,
user_id
,
bot_id_mapping
,
summarize_thinking
=
None
,
):
):
"""AgentScope native hook: 推理完成后
通过 IM 发送 thinking 文本
。"""
"""AgentScope native hook: 推理完成后
摘要化 thinking 并通过 IM 发送
。"""
logger
.
info
(
f
"[Hook 触发] agent={self.name} 时机=post_reasoning"
)
logger
.
info
(
f
"[Hook 触发] agent={self.name} 时机=post_reasoning"
)
bot_id
=
bot_id_mapping
.
get
(
self
.
name
)
bot_id
=
bot_id_mapping
.
get
(
self
.
name
)
if
not
bot_id
:
if
not
bot_id
:
...
@@ -58,12 +58,11 @@ async def _im_post_reasoning_hook(
...
@@ -58,12 +58,11 @@ async def _im_post_reasoning_hook(
content
=
"
\n
"
.
join
(
content_parts
)
if
content_parts
else
None
content
=
"
\n
"
.
join
(
content_parts
)
if
content_parts
else
None
if
not
content
:
if
not
content
:
return
None
return
None
# 中间过程只发前10个字符 + 省略号
# 临时注释:让 LLM 自己控制思考长度,验证 prompt 约束效果
# 验证标准:手动采样 20 条 sub_agent thinking 文字,中位数 ≤25 字 且 最大值 ≤40 字 可删除本注释
# content = content[:25] + "..." if len(content) > 10 else content
# 删除"下载"这个词
# 删除"下载"这个词
content
=
content
.
replace
(
"下载"
,
"点击"
)
content
=
content
.
replace
(
"下载"
,
"点击"
)
# 摘要化 thinking:调用模型总结为状态更新,不暴露内部推理
if
summarize_thinking
:
content
=
await
summarize_thinking
(
content
)
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]}..."
)
try
:
try
:
await
im_service
.
send_message
(
await
im_service
.
send_message
(
...
@@ -77,32 +76,6 @@ async def _im_post_reasoning_hook(
...
@@ -77,32 +76,6 @@ async def _im_post_reasoning_hook(
)
)
async
def
_im_pre_acting_hook
(
self
,
kwargs
,
*
,
im_service
,
room_id
,
user_id
,
bot_id_mapping
,
):
"""AgentScope native hook: 工具调用前通过 IM 发送即将执行的工具信息。"""
logger
.
info
(
f
"[Hook 触发] agent={self.name} 时机=pre_acting"
)
bot_id
=
bot_id_mapping
.
get
(
self
.
name
)
if
not
bot_id
:
return
None
tool_call
=
kwargs
.
get
(
"tool_call"
)
if
not
tool_call
:
return
None
content
=
str
(
tool_call
)
logger
.
info
(
f
"[Hook 发送 IM] agent={self.name} bot_id={bot_id} tool_call={tool_call}"
)
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
):
class
AgentHubError
(
Exception
):
"""Raised when AgentHub processing fails."""
"""Raised when AgentHub processing fails."""
pass
pass
...
@@ -155,6 +128,14 @@ class AgentHub:
...
@@ -155,6 +128,14 @@ class AgentHub:
self
.
soul_guide
=
""
self
.
soul_guide
=
""
logger
.
warning
(
f
"soul/agent.md 未找到: {soul_path}"
)
logger
.
warning
(
f
"soul/agent.md 未找到: {soul_path}"
)
# 加载 thinking 摘要提示词(soul/thinking-summarize.md)
summarize_prompt_path
=
Path
(
__file__
)
.
parent
.
parent
.
parent
/
"soul"
/
"thinking-summarize.md"
if
summarize_prompt_path
.
exists
():
self
.
summarize_prompt
=
summarize_prompt_path
.
read_text
(
encoding
=
"utf-8"
)
.
strip
()
else
:
self
.
summarize_prompt
=
"将以下AI思考过程总结为一句简短的状态更新(≤20字),描述AI当前正在做什么。只输出状态更新本身。"
logger
.
warning
(
f
"soul/thinking-summarize.md 未找到: {summarize_prompt_path}"
)
# sub_agent 专用 model:低 temperature,更确定性
# sub_agent 专用 model:低 temperature,更确定性
self
.
sub_model
=
OpenAIChatModel
(
self
.
sub_model
=
OpenAIChatModel
(
model_name
=
self
.
main_model_config
.
model_name
,
model_name
=
self
.
main_model_config
.
model_name
,
...
@@ -164,6 +145,19 @@ class AgentHub:
...
@@ -164,6 +145,19 @@ class AgentHub:
client_kwargs
=
{
"base_url"
:
self
.
main_model_config
.
api_base
}
client_kwargs
=
{
"base_url"
:
self
.
main_model_config
.
api_base
}
)
)
# thinking 摘要专用 model:便宜、快,和 message_gate 同配置
thinking_summary_config
=
config_loader
.
load_models
()
.
get
(
"thinking_summary"
)
if
thinking_summary_config
:
self
.
thinking_summary_model
=
OpenAIChatModel
(
model_name
=
thinking_summary_config
.
model_name
,
api_key
=
thinking_summary_config
.
api_key
,
stream
=
False
,
client_kwargs
=
{
"base_url"
:
thinking_summary_config
.
api_base
}
)
else
:
self
.
thinking_summary_model
=
self
.
sub_model
logger
.
warning
(
"thinking_summary 模型配置未找到,降级使用 sub_model"
)
# Register all skills from skills/ directory to Toolkit
# Register all skills from skills/ directory to Toolkit
skills_dir
=
Path
(
__file__
)
.
parent
.
parent
.
parent
/
"skills"
skills_dir
=
Path
(
__file__
)
.
parent
.
parent
.
parent
/
"skills"
logger
.
info
(
f
"扫描 skills 目录并注册到全局 Toolkit: {skills_dir}"
)
logger
.
info
(
f
"扫描 skills 目录并注册到全局 Toolkit: {skills_dir}"
)
...
@@ -270,6 +264,7 @@ class AgentHub:
...
@@ -270,6 +264,7 @@ class AgentHub:
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
{},
summarize_thinking
=
self
.
_summarize_thinking
,
)
)
agent
.
register_instance_hook
(
agent
.
register_instance_hook
(
"post_reasoning"
,
"im_post_reasoning"
,
"post_reasoning"
,
"im_post_reasoning"
,
...
@@ -292,7 +287,6 @@ class AgentHub:
...
@@ -292,7 +287,6 @@ class AgentHub:
def
_build_main_prompt
(
def
_build_main_prompt
(
self
,
self
,
context
:
Dict
[
str
,
Any
],
allowed_agent_codes
:
Optional
[
list
]
=
None
,
allowed_agent_codes
:
Optional
[
list
]
=
None
,
)
->
str
:
)
->
str
:
"""
"""
...
@@ -316,7 +310,7 @@ class AgentHub:
...
@@ -316,7 +310,7 @@ class AgentHub:
return
prompt
+
self
.
soul_guide
+
REVIEW_GUIDE
+
THINKING_GUIDE
return
prompt
+
self
.
soul_guide
+
REVIEW_GUIDE
+
THINKING_GUIDE
def
_build_sub_agent_prompt
(
self
,
agent_code
:
str
,
context
:
Dict
[
str
,
Any
]
)
->
str
:
def
_build_sub_agent_prompt
(
self
,
agent_code
:
str
)
->
str
:
"""Build prompt for a sub-agent when handling a routed query."""
"""Build prompt for a sub-agent when handling a routed query."""
agent
=
self
.
agents
.
get
(
agent_code
)
agent
=
self
.
agents
.
get
(
agent_code
)
if
not
agent
:
if
not
agent
:
...
@@ -380,13 +374,6 @@ class AgentHub:
...
@@ -380,13 +374,6 @@ class AgentHub:
target_agent
=
self
.
_find_steward_key
()
target_agent
=
self
.
_find_steward_key
()
routing_reason
=
"default"
routing_reason
=
"default"
context
=
{
"user_query"
:
user_query
,
"messages"
:
messages
,
"room_id"
:
room_id
,
"user_id"
:
user_id
}
logger
.
debug
(
logger
.
debug
(
"路由决策: target=
%
s reason=
%
s position_restricted=
%
s allowed=
%
s"
,
"路由决策: target=
%
s reason=
%
s position_restricted=
%
s allowed=
%
s"
,
target_agent
,
routing_reason
,
target_agent
,
routing_reason
,
...
@@ -395,9 +382,9 @@ class AgentHub:
...
@@ -395,9 +382,9 @@ 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
(
context
,
allowed_agent_codes
=
allowed_agent_codes
)
system_prompt
=
self
.
_build_main_prompt
(
allowed_agent_codes
=
allowed_agent_codes
)
else
:
else
:
system_prompt
=
self
.
_build_sub_agent_prompt
(
target_agent
,
context
)
system_prompt
=
self
.
_build_sub_agent_prompt
(
target_agent
)
# Bridge: convert SessionStore dict history → AgentScope Msg[]
# Bridge: convert SessionStore dict history → AgentScope Msg[]
agent_memory
=
InMemoryMemory
()
agent_memory
=
InMemoryMemory
()
...
@@ -472,12 +459,9 @@ class AgentHub:
...
@@ -472,12 +459,9 @@ class AgentHub:
f
"可用调用码: {valid}"
f
"可用调用码: {valid}"
)
)
sub_prompt
=
self
.
_build_sub_agent_prompt
(
agent_code
,
{}
)
sub_prompt
=
self
.
_build_sub_agent_prompt
(
agent_code
)
sub_memory
=
InMemoryMemory
()
sub_memory
=
InMemoryMemory
()
# All agents use the shared toolkit
sub_toolkit
=
self
.
toolkit
sub_agent
=
ReActAgent
(
sub_agent
=
ReActAgent
(
name
=
agent_code
,
name
=
agent_code
,
sys_prompt
=
sub_prompt
,
sys_prompt
=
sub_prompt
,
...
@@ -597,6 +581,19 @@ class AgentHub:
...
@@ -597,6 +581,19 @@ class AgentHub:
return
call_sub_agent
return
call_sub_agent
async
def
_summarize_thinking
(
self
,
thinking_text
:
str
)
->
str
:
"""将思考过程总结为简短的状态更新,不暴露内部推理细节。"""
try
:
system_msg
=
Msg
(
"system"
,
self
.
summarize_prompt
,
"system"
)
user_msg
=
Msg
(
"user"
,
thinking_text
,
"user"
)
formatted
=
await
self
.
formatter
.
format
([
system_msg
,
user_msg
])
response
=
await
self
.
thinking_summary_model
(
formatted
)
summary
=
(
response
.
get_text_content
()
or
""
)
.
strip
()
return
summary
or
thinking_text
[:
30
]
+
"..."
except
Exception
:
logger
.
warning
(
"thinking 摘要化失败,使用截断兜底"
,
exc_info
=
True
)
return
thinking_text
[:
30
]
+
"..."
def
register_skill_tool
(
self
,
skill_name
:
str
,
tool_func
,
func_name
:
str
):
def
register_skill_tool
(
self
,
skill_name
:
str
,
tool_func
,
func_name
:
str
):
"""将工具注册到所有拥有该 skill 的 agent toolkit"""
"""将工具注册到所有拥有该 skill 的 agent toolkit"""
for
agent_code
,
agent_config
in
self
.
agents
.
items
():
for
agent_code
,
agent_config
in
self
.
agents
.
items
():
...
...
workspace/agent-admin/backend/config/models.json
View file @
fbfe63e
...
@@ -17,6 +17,12 @@
...
@@ -17,6 +17,12 @@
"api_base"
:
"https://dashscope.aliyuncs.com/compatible-mode/v1"
,
"api_base"
:
"https://dashscope.aliyuncs.com/compatible-mode/v1"
,
"model_name"
:
"deepseek-v4-flash"
,
"model_name"
:
"deepseek-v4-flash"
,
"api_key"
:
"sk-107dd26bca184e39bd1744af7d634488"
"api_key"
:
"sk-107dd26bca184e39bd1744af7d634488"
},
"thinking_summary"
:
{
"type"
:
"openai_compatible"
,
"api_base"
:
"https://dashscope.aliyuncs.com/compatible-mode/v1"
,
"model_name"
:
"deepseek-v4-flash"
,
"api_key"
:
"sk-107dd26bca184e39bd1744af7d634488"
}
}
},
},
"meeting_minutes"
:
{
"meeting_minutes"
:
{
...
...
workspace/agent-admin/backend/soul/agent.md
View file @
fbfe63e
...
@@ -19,6 +19,3 @@
...
@@ -19,6 +19,3 @@
-
链接能描述就描述,贴出来尽量短
-
链接能描述就描述,贴出来尽量短
-
一条能说完不拆五条,五条能说清也别硬塞一条
-
一条能说完不拆五条,五条能说清也别硬塞一条
## 行动风格
-
行动时只输出工具调用,不要附加解释
-
行动阶段用较低 temperature 减少"废话"
workspace/agent-admin/backend/soul/thinking-summarize.md
0 → 100644
View file @
fbfe63e
你是一个状态更新机器人。将以下AI思考过程总结为一句简短的状态更新(≤20字),描述AI当前正在做什么。只输出状态更新本身,不要引号、不要解释、不要前缀。
workspace/agent-admin/backend/tests/test_agent_hub.py
View file @
fbfe63e
...
@@ -790,8 +790,7 @@ async def test_call_sub_agent_uses_sub_model():
...
@@ -790,8 +790,7 @@ async def test_call_sub_agent_uses_sub_model():
def
test_main_prompt_includes_thinking_guide
():
def
test_main_prompt_includes_thinking_guide
():
"""_build_main_prompt 输出应包含 THINKING_GUIDE。"""
"""_build_main_prompt 输出应包含 THINKING_GUIDE。"""
hub
=
_make_test_hub
()
hub
=
_make_test_hub
()
context
=
{
"user_query"
:
"test"
,
"messages"
:
[],
"room_id"
:
"r1"
,
"user_id"
:
"u1"
}
prompt
=
hub
.
_build_main_prompt
()
prompt
=
hub
.
_build_main_prompt
(
context
)
assert
"思考"
in
prompt
assert
"思考"
in
prompt
assert
"20"
in
prompt
assert
"20"
in
prompt
# main 独有:REVIEW_GUIDE 应在
# main 独有:REVIEW_GUIDE 应在
...
@@ -801,7 +800,7 @@ def test_main_prompt_includes_thinking_guide():
...
@@ -801,7 +800,7 @@ def test_main_prompt_includes_thinking_guide():
def
test_sub_agent_prompt_includes_thinking_guide_only
():
def
test_sub_agent_prompt_includes_thinking_guide_only
():
"""_build_sub_agent_prompt 应只拼 THINKING_GUIDE,不含 REVIEW_GUIDE。"""
"""_build_sub_agent_prompt 应只拼 THINKING_GUIDE,不含 REVIEW_GUIDE。"""
hub
=
_make_test_hub
()
hub
=
_make_test_hub
()
prompt
=
hub
.
_build_sub_agent_prompt
(
MEETING_KEY
,
{}
)
prompt
=
hub
.
_build_sub_agent_prompt
(
MEETING_KEY
)
assert
"思考"
in
prompt
assert
"思考"
in
prompt
assert
"20"
in
prompt
assert
"20"
in
prompt
# sub 不应有 REVIEW_GUIDE
# sub 不应有 REVIEW_GUIDE
...
@@ -814,8 +813,7 @@ def test_sub_agent_prompt_includes_thinking_guide_only():
...
@@ -814,8 +813,7 @@ def test_sub_agent_prompt_includes_thinking_guide_only():
def
test_main_prompt_includes_soul_guide
():
def
test_main_prompt_includes_soul_guide
():
"""_build_main_prompt 输出应包含 soul/agent.md 的回复风格内容。"""
"""_build_main_prompt 输出应包含 soul/agent.md 的回复风格内容。"""
hub
=
_make_test_hub
()
hub
=
_make_test_hub
()
context
=
{
"user_query"
:
"test"
,
"messages"
:
[],
"room_id"
:
"r1"
,
"user_id"
:
"u1"
}
prompt
=
hub
.
_build_main_prompt
()
prompt
=
hub
.
_build_main_prompt
(
context
)
assert
"说人话"
in
prompt
assert
"说人话"
in
prompt
assert
"微信适配"
in
prompt
assert
"微信适配"
in
prompt
...
@@ -823,6 +821,96 @@ def test_main_prompt_includes_soul_guide():
...
@@ -823,6 +821,96 @@ def test_main_prompt_includes_soul_guide():
def
test_sub_agent_prompt_includes_soul_guide
():
def
test_sub_agent_prompt_includes_soul_guide
():
"""_build_sub_agent_prompt 输出应包含 soul/agent.md 的回复风格内容。"""
"""_build_sub_agent_prompt 输出应包含 soul/agent.md 的回复风格内容。"""
hub
=
_make_test_hub
()
hub
=
_make_test_hub
()
prompt
=
hub
.
_build_sub_agent_prompt
(
MEETING_KEY
,
{}
)
prompt
=
hub
.
_build_sub_agent_prompt
(
MEETING_KEY
)
assert
"说人话"
in
prompt
assert
"说人话"
in
prompt
assert
"微信适配"
in
prompt
assert
"微信适配"
in
prompt
# ── Tests for thinking summarization ──
@pytest.mark.asyncio
async
def
test_summarize_thinking_calls_model
():
"""任意长度 thinking 都应调用 thinking_summary_model 摘要化。"""
hub
=
_make_test_hub
()
short
=
"正在处理"
mock_response
=
MagicMock
()
mock_response
.
get_text_content
.
return_value
=
"处理中"
hub
.
thinking_summary_model
=
AsyncMock
(
return_value
=
mock_response
)
hub
.
formatter
.
format
=
AsyncMock
(
return_value
=
[{
"role"
:
"user"
,
"content"
:
short
}])
result
=
await
hub
.
_summarize_thinking
(
short
)
assert
hub
.
thinking_summary_model
.
called
assert
result
==
"处理中"
@pytest.mark.asyncio
async
def
test_summarize_thinking_long_text_calls_model
():
"""长 thinking 应调用 thinking_summary_model 摘要化。"""
hub
=
_make_test_hub
()
long_text
=
"用户需要我根据提供的音频文件生成会议纪要。根据系统提示,我需要使用 meeting_minutes 技能来处理这个任务。让我先查看 meeting_minutes 技能的 SKILL.md 文件。"
mock_response
=
MagicMock
()
mock_response
.
get_text_content
.
return_value
=
"正在生成会议纪要"
hub
.
thinking_summary_model
=
AsyncMock
(
return_value
=
mock_response
)
hub
.
formatter
.
format
=
AsyncMock
(
return_value
=
[{
"role"
:
"user"
,
"content"
:
long_text
}])
result
=
await
hub
.
_summarize_thinking
(
long_text
)
assert
hub
.
thinking_summary_model
.
called
assert
"会议纪要"
in
result
@pytest.mark.asyncio
async
def
test_hook_summarizes_thinking_before_send
():
"""hook 应在发送 IM 前对长 thinking 调用摘要化。"""
from
app.service.agent_hub
import
_im_post_reasoning_hook
mock_im
=
AsyncMock
()
mock_im
.
send_message
.
return_value
=
{
"code"
:
200
}
mock_summarize
=
AsyncMock
(
return_value
=
"正在处理会议纪要"
)
mock_agent
=
MagicMock
()
mock_agent
.
name
=
MEETING_KEY
mock_output
=
MagicMock
()
mock_output
.
content
=
[{
"type"
:
"thinking"
,
"thinking"
:
"这条思考过程非常长,"
*
10
}]
await
_im_post_reasoning_hook
(
mock_agent
,
{},
mock_output
,
im_service
=
mock_im
,
room_id
=
"room_001"
,
user_id
=
"user_001"
,
bot_id_mapping
=
{
MEETING_KEY
:
"bot_001"
},
summarize_thinking
=
mock_summarize
,
)
mock_summarize
.
assert_called_once
()
sent_content
=
mock_im
.
send_message
.
call_args
[
1
][
"content"
]
assert
sent_content
==
"正在处理会议纪要"
@pytest.mark.asyncio
async
def
test_hook_summarizes_even_short_thinking
():
"""即使是短 thinking,也应走摘要化(不直接暴露内部推理)。"""
from
app.service.agent_hub
import
_im_post_reasoning_hook
mock_im
=
AsyncMock
()
mock_im
.
send_message
.
return_value
=
{
"code"
:
200
}
mock_summarize
=
AsyncMock
(
return_value
=
"正在处理"
)
mock_agent
=
MagicMock
()
mock_agent
.
name
=
MEETING_KEY
mock_output
=
MagicMock
()
mock_output
.
content
=
[{
"type"
:
"thinking"
,
"thinking"
:
"正在处理"
}]
await
_im_post_reasoning_hook
(
mock_agent
,
{},
mock_output
,
im_service
=
mock_im
,
room_id
=
"room_001"
,
user_id
=
"user_001"
,
bot_id_mapping
=
{
MEETING_KEY
:
"bot_001"
},
summarize_thinking
=
mock_summarize
,
)
# 不论长短都应调用摘要化
mock_summarize
.
assert_called_once
()
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