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 07040861
authored
Jun 29, 2026
by
sunqi02
Committed by
胡悦
Jul 01, 2026
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat: 增强子Agent功能,支持独立会话存储和消息处理
1 parent
cea33b52
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
11 deletions
workspace/agent-admin/backend/app/service/CLAUDE.md
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/tests/test_agent_hub.py
workspace/agent-admin/backend/app/service/CLAUDE.md
View file @
0704086
...
@@ -49,10 +49,12 @@
...
@@ -49,10 +49,12 @@
8.
`_call_sub_agent()`
子 Agent 创建与执行(通过
`call_sub_agent`
工具触发,使用
`self.sub_model`
)
8.
`_call_sub_agent()`
子 Agent 创建与执行(通过
`call_sub_agent`
工具触发,使用
`self.sub_model`
)
-
自调用防护:拒绝调用 steward 自己的 code,引导调用发言人 Agent
-
自调用防护:拒绝调用 steward 自己的 code,引导调用发言人 Agent
-
子Agent ReAct 消息通过
`_save_tool_interactions()`
写入 SessionMemory,落 DB
-
子Agent ReAct 消息通过
`_save_tool_interactions()`
写入 SessionMemory,落 DB
-
子Agent 通过
`PostReplyHook`
直接发送自己的处理结果(用自身 bot_id),
**不走发言人 Agent 转发**
-
口 Agent(code: -1)排除
`call_sub_agent`
工具,纯总结回复
-
口 Agent(code: -1)排除
`call_sub_agent`
工具,纯总结回复
9.
回复处理:
9.
回复处理
(
`call_count`
机制 — 解决 LLM 在 tool_result 后总是继续输出的问题)
:
-
`call_count >= 1`
:主Agent 静默——
不写 KV(
`memory.remove_last()`
)、不存 DB、不发送 IM(发言人已发送最终回复)
-
`call_count >= 1`
:主Agent 静默——
`memory.remove_last()`
剔除主Agent 最终回复,不发送 IM。子Agent 已各自发送,主Agent 无需再说一次
-
`call_count == 0`
:主Agent 直接回复——正常写入 + 存 DB + PostReplyHook 发 IM
-
`call_count == 0`
:主Agent 直接回复——正常写入 + 存 DB + PostReplyHook 发 IM
-
发言人 Agent(code -1):当主Agent 在已派发子Agent 后仍有
**子Agent 未覆盖的剩余内容**
时调用,由主Agent 在 ReAct 循环中自行决定是否触发
10.
`save_to_db()`
持久化会话
10.
`save_to_db()`
持久化会话
### 会话管理
### 会话管理
...
@@ -190,16 +192,18 @@ MessageGate.evaluate() ← app/service/message_gate.py
...
@@ -190,16 +192,18 @@ MessageGate.evaluate() ← app/service/message_gate.py
│ │ │
│ │ │
│ │ ├── 自调用防护:拒绝 steward 自己的 code
│ │ ├── 自调用防护:拒绝 steward 自己的 code
│ │ ├── 创建子 ReActAgent + 执行
│ │ ├── 创建子 ReActAgent + 执行
│ │ ├── 子Agent 通过 PostReplyHook 直接发送结果(用自身 bot_id)
│ │ ├── 子Agent ReAct 消息写入 SessionMemory(通过 _current_route_ctx)
│ │ ├── 子Agent ReAct 消息写入 SessionMemory(通过 _current_route_ctx)
│ │ └──
串行链式调用:sub_A → sub_B → 口 Agent (-1)
│ │ └──
调用顺序由主Agent ReAct 推理决定,非固定链式
│ │
│ │
│ ├── [观察] 工具返回结果,注入 memory
│ ├── [观察] 工具返回结果,注入 memory
│ │
│ │
│ └── 重复,直到 LLM 输出最终回复(无 tool_use)
│ └── 重复,直到 LLM 输出最终回复(无 tool_use)
│
│
├── 回复判断(call_count)
├── 回复判断(call_count — 抑制 LLM 在 tool_result 后继续输出的问题)
│ ├── call_count >= 1 → 静默(发言人已发最终回复)
│ ├── call_count >= 1 → 主Agent 静默
│ │ 不写 KV、不存 DB、不发送
│ │ `memory.remove_last()` 剔除最终回复,不发送 IM
│ │ 子Agent 已各自直接发送,发言人口按需由主Agent 自行触发
│ └── call_count == 0 → 主Agent 直接回复
│ └── call_count == 0 → 主Agent 直接回复
│ SessionMemory.add("assistant", response)
│ SessionMemory.add("assistant", response)
│ → save_to_db()
│ → save_to_db()
...
...
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
0704086
...
@@ -482,6 +482,7 @@ class AgentHub:
...
@@ -482,6 +482,7 @@ class AgentHub:
# 供 _call_sub_agent 创建子 Agent 时注册 hook 和发 IM
# 供 _call_sub_agent 创建子 Agent 时注册 hook 和发 IM
_route_context_var
.
set
(
dict
(
_route_context_var
.
set
(
dict
(
room_id
=
room_id
,
user_id
=
user_id
,
bot_id_mapping
=
bot_id_mapping
or
{},
room_id
=
room_id
,
user_id
=
user_id
,
bot_id_mapping
=
bot_id_mapping
or
{},
session_id
=
session_id
,
call_count
=
0
,
call_count
=
0
,
))
))
...
@@ -692,9 +693,25 @@ class AgentHub:
...
@@ -692,9 +693,25 @@ class AgentHub:
)
)
sub_prompt
=
self
.
_build_sub_agent_prompt
(
agent_code
)
sub_prompt
=
self
.
_build_sub_agent_prompt
(
agent_code
)
sub_memory
=
InMemoryMemory
()
logger
.
info
(
"[子Agent 开始] agent_code=
%
s instruction=
%
s"
,
agent_code
,
instruction
[:
80
])
# 子 Agent 独立存储:session_id = {group_contact_id}:agent:{agent_code}
ctx
=
_route_context_var
.
get
()
sub_session_id
=
f
"{ctx['session_id']}:agent:{agent_code}"
# 加载子 Agent 自己的历史(过滤掉主 Agent 的 ReAct 干扰)
sub_memory
=
InMemoryMemory
()
sub_session_memory
=
SessionMemory
(
session_id
=
sub_session_id
,
store
=
self
.
session_service
.
store
,
)
sub_history
=
sub_session_memory
.
get_messages
(
max_rounds
=
10
)
history_msgs
=
[
Msg
.
from_dict
(
m
)
for
m
in
sub_history
if
"name"
in
m
]
if
history_msgs
:
await
sub_memory
.
add
(
history_msgs
)
logger
.
info
(
"[子Agent 开始] agent_code=
%
s history=
%
d instruction=
%
s"
,
agent_code
,
len
(
history_msgs
),
instruction
[:
80
],
)
sub_agent
=
ReActAgent
(
sub_agent
=
ReActAgent
(
name
=
agent_code
,
name
=
agent_code
,
...
@@ -707,13 +724,37 @@ class AgentHub:
...
@@ -707,13 +724,37 @@ class AgentHub:
)
)
# 为子 Agent 注册 IM hook(post_reasoning → thinking 摘要)
# 为子 Agent 注册 IM hook(post_reasoning → thinking 摘要)
ctx
=
_route_context_var
.
get
()
self
.
_register_im_hooks
(
sub_agent
,
ctx
[
"room_id"
],
ctx
[
"user_id"
],
ctx
[
"bot_id_mapping"
])
self
.
_register_im_hooks
(
sub_agent
,
ctx
[
"room_id"
],
ctx
[
"user_id"
],
ctx
[
"bot_id_mapping"
])
response
=
await
sub_agent
.
reply
(
Msg
(
"user"
,
instruction
,
"user"
))
response
=
await
sub_agent
.
reply
(
Msg
(
"user"
,
instruction
,
"user"
))
response_text
=
response
.
get_text_content
()
response_text
=
response
.
get_text_content
()
logger
.
info
(
"[子Agent 完成] agent_code=
%
s response_len=
%
d"
,
agent_code
,
len
(
response_text
)
if
response_text
else
0
)
logger
.
info
(
"[子Agent 完成] agent_code=
%
s response_len=
%
d"
,
agent_code
,
len
(
response_text
)
if
response_text
else
0
)
# 保存子 Agent 的 ReAct 消息到独立 KV + DB
try
:
self
.
session_service
.
get_or_create_session
(
session_id
=
sub_session_id
,
room_id
=
agent_code
,
channel
=
"sub_agent"
,
)
sub_memory_msgs
=
await
sub_agent
.
memory
.
get_memory
()
for
msg
in
sub_memory_msgs
:
sub_session_memory
.
add
(
role
=
msg
.
role
,
content
=
msg
.
content
,
name
=
msg
.
name
,
)
self
.
session_service
.
save_to_db
(
sub_session_id
)
logger
.
info
(
"[子Agent 保存] sub_session=
%
s messages=
%
d"
,
sub_session_id
,
len
(
sub_memory_msgs
),
)
except
Exception
:
logger
.
warning
(
"[子Agent 保存失败] sub_session=
%
s(不影响推理流程)"
,
sub_session_id
,
exc_info
=
True
,
)
# 子 Agent 用自己的 bot 账号发送最终回复
# 子 Agent 用自己的 bot 账号发送最终回复
try
:
try
:
await
self
.
post_reply_hook
.
execute
({
await
self
.
post_reply_hook
.
execute
({
...
...
workspace/agent-admin/backend/tests/test_agent_hub.py
View file @
0704086
...
@@ -782,6 +782,7 @@ async def test_call_sub_agent_uses_sub_model():
...
@@ -782,6 +782,7 @@ async def test_call_sub_agent_uses_sub_model():
hub
=
_make_test_hub
()
hub
=
_make_test_hub
()
_token
=
_route_context_var
.
set
({
_token
=
_route_context_var
.
set
({
"room_id"
:
"room_001"
,
"user_id"
:
"user_001"
,
"bot_id_mapping"
:
{},
"room_id"
:
"room_001"
,
"user_id"
:
"user_001"
,
"bot_id_mapping"
:
{},
"session_id"
:
"sess_test_001"
,
"call_count"
:
0
,
"call_count"
:
0
,
})
})
captured_model
=
None
captured_model
=
None
...
@@ -1026,7 +1027,7 @@ class TestSubAgentPostReply:
...
@@ -1026,7 +1027,7 @@ class TestSubAgentPostReply:
hub
=
_make_test_hub
()
hub
=
_make_test_hub
()
token
=
_route_context_var
.
set
(
dict
(
token
=
_route_context_var
.
set
(
dict
(
room_id
=
"r1"
,
user_id
=
"u1"
,
bot_id_mapping
=
{
MEETING_KEY
:
"bot_meeting"
},
room_id
=
"r1"
,
user_id
=
"u1"
,
bot_id_mapping
=
{
MEETING_KEY
:
"bot_meeting"
},
call_count
=
0
,
session_id
=
"sess_test_002"
,
call_count
=
0
,
))
))
# post_reply_hook.execute 是真实方法,用 AsyncMock(wraps=...) 包装以跟踪调用
# post_reply_hook.execute 是真实方法,用 AsyncMock(wraps=...) 包装以跟踪调用
...
@@ -1059,7 +1060,7 @@ class TestSubAgentPostReply:
...
@@ -1059,7 +1060,7 @@ class TestSubAgentPostReply:
hub
=
_make_test_hub
()
hub
=
_make_test_hub
()
token
=
_route_context_var
.
set
(
dict
(
token
=
_route_context_var
.
set
(
dict
(
room_id
=
"r1"
,
user_id
=
"u1"
,
bot_id_mapping
=
{
MEETING_KEY
:
"bot_meeting"
},
room_id
=
"r1"
,
user_id
=
"u1"
,
bot_id_mapping
=
{
MEETING_KEY
:
"bot_meeting"
},
call_count
=
0
,
session_id
=
"sess_test_003"
,
call_count
=
0
,
))
))
hub
.
post_reply_hook
.
execute
=
AsyncMock
(
side_effect
=
Exception
(
"IM down"
))
hub
.
post_reply_hook
.
execute
=
AsyncMock
(
side_effect
=
Exception
(
"IM down"
))
...
...
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