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 658d3b8c
authored
Jun 02, 2026
by
sunqi02
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat(agent_hub): inject soul_guide into main and sub-agent prompts
1 parent
094db379
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
379 additions
and
11 deletions
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/docs/superpowers/sunqi/plans/2026-06-02-sub-agent-thinking-control-plan.md
workspace/agent-admin/backend/app/model/soul/agent.md → workspace/agent-admin/backend/soul/agent.md
workspace/agent-admin/backend/test_request.json
workspace/agent-admin/backend/tests/test_agent_hub.py
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
658d3b8
...
@@ -147,6 +147,14 @@ class AgentHub:
...
@@ -147,6 +147,14 @@ class AgentHub:
)
)
self
.
formatter
=
OpenAIChatFormatter
()
self
.
formatter
=
OpenAIChatFormatter
()
# 加载回复风格指南(soul/agent.md)
soul_path
=
Path
(
__file__
)
.
parent
.
parent
.
parent
/
"soul"
/
"agent.md"
if
soul_path
.
exists
():
self
.
soul_guide
=
soul_path
.
read_text
(
encoding
=
"utf-8"
)
else
:
self
.
soul_guide
=
""
logger
.
warning
(
f
"soul/agent.md 未找到: {soul_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
,
...
@@ -306,7 +314,7 @@ class AgentHub:
...
@@ -306,7 +314,7 @@ 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
)
return
prompt
+
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
,
context
:
Dict
[
str
,
Any
])
->
str
:
"""Build prompt for a sub-agent when handling a routed query."""
"""Build prompt for a sub-agent when handling a routed query."""
...
@@ -314,7 +322,7 @@ class AgentHub:
...
@@ -314,7 +322,7 @@ class AgentHub:
if
not
agent
:
if
not
agent
:
raise
AgentHubError
(
f
"Agent {agent_code} 配置未找到"
)
raise
AgentHubError
(
f
"Agent {agent_code} 配置未找到"
)
return
agent
.
prompt
+
THINKING_GUIDE
# sub 加思考风格
约束
return
agent
.
prompt
+
self
.
soul_guide
+
THINKING_GUIDE
# sub 加回复风格+思考
约束
def
_dict_to_msg
(
self
,
m
:
dict
)
->
Msg
:
def
_dict_to_msg
(
self
,
m
:
dict
)
->
Msg
:
"""SessionStore dict → AgentScope Msg。只处理 user/assistant 文本消息。"""
"""SessionStore dict → AgentScope Msg。只处理 user/assistant 文本消息。"""
...
...
workspace/agent-admin/backend/docs/superpowers/sunqi/plans/2026-06-02-sub-agent-thinking-control-plan.md
0 → 100644
View file @
658d3b8
# Sub-Agent 思考控制 Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:**
让 sub_agent 的 reasoning 思考文字 ≤20 字,并通过 temperature=0.2 提升决策确定性。
**Architecture:**
在
`AgentHub`
中新增
`self.sub_model`
(独立 OpenAIChatModel 实例,temperature=0.2),sub_agent 改用此实例;新增
`THINKING_GUIDE`
常量拼到 main + sub 提示词末尾;临时注释掉 hook 里的截断逻辑以验证 prompt 约束效果。
**Tech Stack:**
Python 3.12+, FastAPI, AgentScope
`OpenAIChatModel`
, pytest, asyncio
---
## File Structure
| 文件 | 状态 | 职责 |
|------|------|------|
|
`app/service/agent_hub.py`
| Modify | 3 处改动:新增 sub_model、THINKING_GUIDE 注入、注释截断 |
|
`tests/test_agent_hub.py`
| Modify | 3 个新测试:sub_model 存在性、prompt 包含 THINKING_GUIDE、_call_sub_agent 用 sub_model |
无新文件、无新配置。
---
## Task 1: 新增 sub_model 实例 + 改 _call_sub_agent 引用
**Files:**
-
Modify:
`app/service/agent_hub.py:130-135`
(在
`__init__`
里新增
`self.sub_model`
)
-
Modify:
`app/service/agent_hub.py:455`
(
`_call_sub_agent`
里
`model=self.model`
→
`model=self.sub_model`
)
-
Test:
`tests/test_agent_hub.py`
(追加测试)
-
[
]
**Step 1: 写测试 — 验证 sub_model 属性存在且 temperature=0.2**
在
`tests/test_agent_hub.py`
文件末尾追加:
```
python
# ── Tests for sub_model (thinking control) ──
def
test_agent_hub_has_sub_model_with_low_temperature
():
"""AgentHub 应创建 sub_model 实例,temperature=0.2。"""
hub
=
_make_test_hub
()
# sub_model 应存在,且 generate_kwargs["temperature"] == 0.2
assert
hasattr
(
hub
,
"sub_model"
)
assert
hub
.
sub_model
is
not
None
assert
hub
.
sub_model
.
generate_kwargs
.
get
(
"temperature"
)
==
0.2
@pytest.mark.asyncio
async
def
test_call_sub_agent_uses_sub_model
():
"""_call_sub_agent 创建的 ReActAgent 应使用 sub_model 而非 main model。"""
from
unittest.mock
import
patch
,
AsyncMock
from
agentscope.agent
import
ReActAgent
hub
=
_make_test_hub
()
# _call_sub_agent 内部访问 _current_route_ctx,需先设置
hub
.
_current_route_ctx
=
{
"room_id"
:
"room_001"
,
"user_id"
:
"user_001"
,
"bot_id_mapping"
:
{},
}
captured_model
=
None
real_init
=
ReActAgent
.
__init__
def
capture_init
(
self
,
*
args
,
**
kwargs
):
nonlocal
captured_model
captured_model
=
kwargs
.
get
(
"model"
)
real_init
(
self
,
*
args
,
**
kwargs
)
# 把 reply 替换为 AsyncMock 避免真实模型调用
self
.
reply
=
AsyncMock
(
return_value
=
MagicMock
())
with
patch
.
object
(
ReActAgent
,
"__init__"
,
new
=
capture_init
):
await
hub
.
_call_sub_agent
(
MEETING_KEY
,
"test instruction"
)
# ReActAgent 应被创建,model 应该是 sub_model
assert
captured_model
is
not
None
assert
captured_model
is
hub
.
sub_model
# 不应是 main model
assert
captured_model
is
not
hub
.
model
```
-
[
]
**Step 2: 跑测试验证失败**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin/backend
PYTHONPATH
=
. python -m pytest tests/test_agent_hub.py::test_agent_hub_has_sub_model_with_low_temperature tests/test_agent_hub.py::test_call_sub_agent_uses_sub_model -v
```
Expected: 2 failures —
`AttributeError: 'AgentHub' object has no attribute 'sub_model'`
-
[
]
**Step 3: 实现 — 在 AgentHub.__init__ 里加 self.sub_model**
修改
`app/service/agent_hub.py`
,在
`self.model = OpenAIChatModel(...)`
之后(第 135 行后)追加:
```
python
self
.
formatter
=
OpenAIChatFormatter
()
```
紧接着(
**实际是替换第 135 行的 `self.formatter = OpenAIChatFormatter()` 这一行**
),改为:
```
python
self
.
formatter
=
OpenAIChatFormatter
()
# sub_agent 专用 model:temperature=0.2,更确定性
self
.
sub_model
=
OpenAIChatModel
(
model_name
=
self
.
main_model_config
.
model_name
,
api_key
=
self
.
main_model_config
.
api_key
,
stream
=
False
,
generate_kwargs
=
{
"temperature"
:
0.2
},
client_kwargs
=
{
"base_url"
:
self
.
main_model_config
.
api_base
}
)
```
-
[
]
**Step 4: 改 _call_sub_agent 用 sub_model**
修改
`app/service/agent_hub.py`
第 455 行附近(
`ReActAgent(...)`
调用处),将
`model=self.model`
改为
`model=self.sub_model`
:
找到这段(line 452-460):
```
python
sub_agent
=
ReActAgent
(
name
=
agent_code
,
sys_prompt
=
sub_prompt
,
model
=
self
.
model
,
formatter
=
self
.
formatter
,
toolkit
=
self
.
agent_toolkits
.
get
(
agent_code
,
self
.
toolkit
),
memory
=
sub_memory
,
max_iters
=
10
,
)
```
改为:
```
python
sub_agent
=
ReActAgent
(
name
=
agent_code
,
sys_prompt
=
sub_prompt
,
model
=
self
.
sub_model
,
# 改:sub_agent 专用低 temperature
formatter
=
self
.
formatter
,
toolkit
=
self
.
agent_toolkits
.
get
(
agent_code
,
self
.
toolkit
),
memory
=
sub_memory
,
max_iters
=
10
,
)
```
-
[
]
**Step 5: 跑测试验证通过**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin/backend
PYTHONPATH
=
. python -m pytest tests/test_agent_hub.py::test_agent_hub_has_sub_model_with_low_temperature tests/test_agent_hub.py::test_call_sub_agent_uses_sub_model -v
```
Expected: 2 PASS
-
[
]
**Step 6: 跑完整测试套件确保无回归**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin/backend
PYTHONPATH
=
. python -m pytest tests/ -v --tb
=
short
```
Expected: 原有 41 个测试 + 新增 2 个 = 43 个全 PASS
-
[
]
**Step 7: 提交**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin
git add backend/app/service/agent_hub.py backend/tests/test_agent_hub.py
git commit -m
"feat(agent_hub): add sub_model with temperature=0.2 for sub-agent determinism"
```
---
## Task 2: 新增 THINKING_GUIDE 常量 + 注入到 main + sub prompt
**Files:**
-
Modify:
`app/service/agent_hub.py:23-29`
(在
`REVIEW_GUIDE`
后面追加
`THINKING_GUIDE`
)
-
Modify:
`app/service/agent_hub.py:288`
(
`_build_main_prompt`
末尾拼接)
-
Modify:
`app/service/agent_hub.py:296`
(
`_build_sub_agent_prompt`
末尾拼接)
-
Test:
`tests/test_agent_hub.py`
(追加测试)
-
[
]
**Step 1: 写测试 — 验证 prompt 包含 THINKING_GUIDE**
在
`tests/test_agent_hub.py`
文件末尾(Task 1 测试之后)追加:
```
python
# ── Tests for THINKING_GUIDE injection ──
def
test_main_prompt_includes_thinking_guide
():
"""_build_main_prompt 输出应包含 THINKING_GUIDE。"""
hub
=
_make_test_hub
()
context
=
{
"user_query"
:
"test"
,
"messages"
:
[],
"room_id"
:
"r1"
,
"user_id"
:
"u1"
}
prompt
=
hub
.
_build_main_prompt
(
context
)
assert
"THINKING_GUIDE"
in
prompt
or
"思考"
in
prompt
assert
"≤20 字"
in
prompt
or
"20字"
in
prompt
# main 独有:REVIEW_GUIDE 应在
assert
"收到子Agent返回后"
in
prompt
def
test_sub_agent_prompt_includes_thinking_guide_only
():
"""_build_sub_agent_prompt 应只拼 THINKING_GUIDE,不含 REVIEW_GUIDE。"""
hub
=
_make_test_hub
()
prompt
=
hub
.
_build_sub_agent_prompt
(
"meeting"
,
{})
assert
"思考"
in
prompt
assert
"20"
in
prompt
# sub 不应有 REVIEW_GUIDE
assert
"收到子Agent返回后"
not
in
prompt
```
-
[
]
**Step 2: 跑测试验证失败**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin/backend
PYTHONPATH
=
. python -m pytest tests/test_agent_hub.py::test_main_prompt_includes_thinking_guide tests/test_agent_hub.py::test_sub_agent_prompt_includes_thinking_guide_only -v
```
Expected: 2 failures —
`AssertionError: '思考' not in prompt`
-
[
]
**Step 3: 实现 — 在 REVIEW_GUIDE 后面追加 THINKING_GUIDE 常量**
修改
`app/service/agent_hub.py`
第 23-29 行
`REVIEW_GUIDE`
定义后追加:
```
python
REVIEW_GUIDE
=
"""
## 收到子Agent返回后
1. 判断结果是否完整,是否还需要其他子Agent协作
2. 如果已经可以回复,整合所有结果用自然语言回复用户
3. 不要再调用同一个子Agent重复处理
"""
THINKING_GUIDE
=
"""
## 思考风格
- reasoning 阶段的思考文字 ≤20 字
- 行动时直接输出 tool_call,不要附加解释
- 需要详细推理时在 tool result 里总结
"""
```
-
[
]
**Step 4: 改 _build_main_prompt 末尾拼接**
修改
`app/service/agent_hub.py`
第 288 行,找到:
```
python
return
prompt
+
REVIEW_GUIDE
```
改为:
```
python
return
prompt
+
REVIEW_GUIDE
+
THINKING_GUIDE
```
-
[
]
**Step 5: 改 _build_sub_agent_prompt 末尾拼接**
修改
`app/service/agent_hub.py`
第 296 行,找到:
```
python
return
agent
.
prompt
# 直接使用,无需拼接
```
改为:
```
python
return
agent
.
prompt
+
THINKING_GUIDE
# sub 加思考风格约束
```
-
[
]
**Step 6: 跑测试验证通过**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin/backend
PYTHONPATH
=
. python -m pytest tests/test_agent_hub.py::test_main_prompt_includes_thinking_guide tests/test_agent_hub.py::test_sub_agent_prompt_includes_thinking_guide_only -v
```
Expected: 2 PASS
-
[
]
**Step 7: 跑完整测试套件**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin/backend
PYTHONPATH
=
. python -m pytest tests/ -v --tb
=
short
```
Expected: 43 个测试全 PASS(与 Task 1 完成后持平,无新增/破坏)
-
[
]
**Step 8: 提交**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin
git add backend/app/service/agent_hub.py backend/tests/test_agent_hub.py
git commit -m
"feat(agent_hub): inject THINKING_GUIDE into main and sub-agent prompts"
```
---
## Task 3: 注释 _im_post_reasoning_hook 里的截断逻辑
**Files:**
-
Modify:
`app/service/agent_hub.py:52`
(注释截断行)
-
Test: 不新增(hook 行为已通过手动验证评估,不在单元测试范围)
-
[
]
**Step 1: 注释截断代码**
修改
`app/service/agent_hub.py`
第 52 行附近,找到:
```
python
# 中间过程只发前10个字符 + 省略号
content
=
content
[:
25
]
+
"..."
if
len
(
content
)
>
10
else
content
```
改为:
```
python
# 中间过程只发前10个字符 + 省略号
# 临时注释:让 LLM 自己控制思考长度,验证 prompt 约束效果
# content = content[:25] + "..." if len(content) > 10 else content
```
-
[
]
**Step 2: 跑完整测试套件确保无破坏**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin/backend
PYTHONPATH
=
. python -m pytest tests/ -v --tb
=
short
```
Expected: 43 个测试全 PASS(hook 改动不影响测试,行为在 IM 端可观察)
-
[
]
**Step 3: 提交**
```
bash
cd
/Users/imac/Desktop/GLSK/program/postAgent/ai-workflow/workspace/agent-admin
git add backend/app/service/agent_hub.py
git commit -m
"chore(agent_hub): comment out thinking truncation to verify prompt constraint"
```
---
## 手动验证(Task 3 完成后执行)
完成所有 Task 后,按设计文档 §"测试验证" 章节手动验证:
1.
重启服务:
`PYTHONPATH=. python -m app.main`
2.
发送测试任务:@会议纪要 + 音频文件
3.
观察 IM 上 sub_agent 的 thinking 文字长度(应 ≤20 字)
4.
观察 sub_agent 任务完成情况(功能不回归)
5.
采样 20 条 thinking 文字:
-
中位数 ≤25 字 → 验证成功,可考虑删除截断代码(不在本计划范围)
-
中位数 >25 字 → 恢复截断(一行反向操作)
workspace/agent-admin/backend/
app/model/
soul/agent.md
→
workspace/agent-admin/backend/soul/agent.md
View file @
658d3b8
...
@@ -18,3 +18,7 @@
...
@@ -18,3 +18,7 @@
-
多条并列用数字序号:1. 2. 3.
-
多条并列用数字序号:1. 2. 3.
-
链接能描述就描述,贴出来尽量短
-
链接能描述就描述,贴出来尽量短
-
一条能说完不拆五条,五条能说清也别硬塞一条
-
一条能说完不拆五条,五条能说清也别硬塞一条
## 行动风格
-
行动时只输出工具调用,不要附加解释
-
行动阶段用较低 temperature 减少"废话"
workspace/agent-admin/backend/test_request.json
View file @
658d3b8
...
@@ -25,15 +25,6 @@
...
@@ -25,15 +25,6 @@
"msg_type"
:
"file"
,
"msg_type"
:
"file"
,
"msg_content"
:
"https://glsk-agentic-test.oss-cn-shenzhen.aliyuncs.com/meeting-minutes%2Faudio%2F1219%E4%BA%8C%E6%89%8B%E8%BD%A6%E4%BE%8B%E4%BC%9A%EF%BC%88%E8%B5%B5%E6%80%BB%EF%BC%89.mp3"
,
"msg_content"
:
"https://glsk-agentic-test.oss-cn-shenzhen.aliyuncs.com/meeting-minutes%2Faudio%2F1219%E4%BA%8C%E6%89%8B%E8%BD%A6%E4%BE%8B%E4%BC%9A%EF%BC%88%E8%B5%B5%E6%80%BB%EF%BC%89.mp3"
,
"payload"
:
{}
"payload"
:
{}
},
{
"msg_id"
:
"m5"
,
"msg_time"
:
"2026-05-26T10:00:00Z"
,
"sender_id"
:
""
,
"sender_name"
:
"孙奇"
,
"msg_type"
:
"text"
,
"msg_content"
:
"请帮我把这段会议录音转成文字,并总结会议纪要。"
,
"payload"
:
{}
}
}
]
]
}
}
workspace/agent-admin/backend/tests/test_agent_hub.py
View file @
658d3b8
...
@@ -806,3 +806,23 @@ def test_sub_agent_prompt_includes_thinking_guide_only():
...
@@ -806,3 +806,23 @@ def test_sub_agent_prompt_includes_thinking_guide_only():
assert
"20"
in
prompt
assert
"20"
in
prompt
# sub 不应有 REVIEW_GUIDE
# sub 不应有 REVIEW_GUIDE
assert
"收到子Agent返回后"
not
in
prompt
assert
"收到子Agent返回后"
not
in
prompt
# ── Tests for soul_guide injection ──
def
test_main_prompt_includes_soul_guide
():
"""_build_main_prompt 输出应包含 soul/agent.md 的回复风格内容。"""
hub
=
_make_test_hub
()
context
=
{
"user_query"
:
"test"
,
"messages"
:
[],
"room_id"
:
"r1"
,
"user_id"
:
"u1"
}
prompt
=
hub
.
_build_main_prompt
(
context
)
assert
"说人话"
in
prompt
assert
"微信适配"
in
prompt
def
test_sub_agent_prompt_includes_soul_guide
():
"""_build_sub_agent_prompt 输出应包含 soul/agent.md 的回复风格内容。"""
hub
=
_make_test_hub
()
prompt
=
hub
.
_build_sub_agent_prompt
(
MEETING_KEY
,
{})
assert
"说人话"
in
prompt
assert
"微信适配"
in
prompt
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