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 320535a4
authored
Jun 26, 2026
by
sunqi02
Committed by
胡悦
Jul 06, 2026
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix: 更新 MessageGate 和 AgentHub,优化在途任务处理,使用 intent_session 存储用户消息
1 parent
9b77ff0b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
14 deletions
workspace/agent-admin/backend/app/service/CLAUDE.md
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/app/service/message_gate.py
workspace/agent-admin/backend/tests/test_message_gate.py
workspace/agent-admin/backend/app/service/CLAUDE.md
View file @
320535a
...
@@ -17,7 +17,7 @@
...
@@ -17,7 +17,7 @@
4.
根据 intent 分支:
4.
根据 intent 分支:
-
`chat`
→
`_handle_chat()`
只追加用户消息到 conversation_history,不触发 AgentHub,不发 IM("已读不回")
-
`chat`
→
`_handle_chat()`
只追加用户消息到 conversation_history,不触发 AgentHub,不发 IM("已读不回")
-
`pass`
→
`_handle_actionable()`
提取 query/file_url,调用
`AgentHub.process_message()`
-
`pass`
→
`_handle_actionable()`
提取 query/file_url,调用
`AgentHub.process_message()`
5.
在途任务忙碌:
`busy`
→
`_handle_busy()`
保存用户消息到
history
,发固定告知回复(
`BUSY_REPLY`
),不排队
5.
在途任务忙碌:
`busy`
→
`_handle_busy()`
保存用户消息到
`intent_session`
(隔离字段,不写
`conversation_history`
)
,发固定告知回复(
`BUSY_REPLY`
),不排队
6.
`_running_tasks[session_id] = asyncio.current_task()`
标记在途,
`finally`
清理
6.
`_running_tasks[session_id] = asyncio.current_task()`
标记在途,
`finally`
清理
| 文件 | 类 | 职责 |
| 文件 | 类 | 职责 |
...
@@ -41,18 +41,19 @@
...
@@ -41,18 +41,19 @@
封装流程:
封装流程:
1.
接收
`process_message()`
调用(由 MessageGate pass 分支调用,或兜底直接调用)
1.
接收
`process_message()`
调用(由 MessageGate pass 分支调用,或兜底直接调用)
2.
`get_or_create_session()`
获取/创建会话
2.
`get_or_create_session()`
获取/创建会话
3.
`SessionMemory.get_messages()`
加载历史消息
3.
消费
`intent_session`
积压消息(busy 分支写入的),拼入
`user_query`
前置,清空 KV 字段
4.
`_build_main_prompt()`
组装 system prompt(DB prompt + soul_guide 回复风格 + 子Agent职责列表 + DISPATCH_GUIDE + THINKING_GUIDE)
4.
`SessionMemory.get_messages()`
加载历史消息
5.
创建
`ReActAgent`
(AgentScope),注入
`InMemoryMemory`
+
`Toolkit`
,主 Agent 用
`self.model`
,子 Agent 用
`self.sub_model`
(temp=0.2)
5.
`_build_main_prompt()`
组装 system prompt(DB prompt + soul_guide 回复风格 + 子Agent职责列表 + DISPATCH_GUIDE + THINKING_GUIDE)
6.
`agent.reply(user_msg)`
执行 ReAct 循环(推理→工具调用→观察→再推理)
6.
创建
`ReActAgent`
(AgentScope),注入
`InMemoryMemory`
+
`Toolkit`
,主 Agent 用
`self.model`
,子 Agent 用
`self.sub_model`
(temp=0.2)
7.
`_call_sub_agent()`
子 Agent 创建与执行(通过
`call_sub_agent`
工具触发,使用
`self.sub_model`
)
7.
`agent.reply(user_msg)`
执行 ReAct 循环(推理→工具调用→观察→再推理)
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(code: -1)排除
`call_sub_agent`
工具,纯总结回复
-
口 Agent(code: -1)排除
`call_sub_agent`
工具,纯总结回复
8
.
回复处理:
9
.
回复处理:
-
`call_count >= 1`
:主Agent 静默——不写 KV、不存 DB、不发送(发言人已发送最终回复)
-
`call_count >= 1`
:主Agent 静默——不写 KV、不存 DB、不发送(发言人已发送最终回复)
-
`call_count == 0`
:主Agent 直接回复——正常写入 + 存 DB + PostReplyHook 发 IM
-
`call_count == 0`
:主Agent 直接回复——正常写入 + 存 DB + PostReplyHook 发 IM
9
.
`save_to_db()`
持久化会话
10
.
`save_to_db()`
持久化会话
### 会话管理
### 会话管理
...
@@ -141,7 +142,8 @@ MessageGate.evaluate() ← app/service/message_gate.py
...
@@ -141,7 +142,8 @@ MessageGate.evaluate() ← app/service/message_gate.py
│
│
├── 检查 _running_tasks(锁外,有在途任务则立即返回)
├── 检查 _running_tasks(锁外,有在途任务则立即返回)
│ └── busy → _handle_busy()
│ └── busy → _handle_busy()
│ 保存用户消息到 history → save_to_db()
│ 保存用户消息到 intent_session(隔离字段,不写 history)
│ 不调 save_to_db()
│ 发固定告知回复(BUSY_REPLY)→ IM 发送
│ 发固定告知回复(BUSY_REPLY)→ IM 发送
│ 不排队,不触发 AgentHub
│ 不排队,不触发 AgentHub
│
│
...
@@ -161,6 +163,10 @@ MessageGate.evaluate() ← app/service/message_gate.py
...
@@ -161,6 +163,10 @@ MessageGate.evaluate() ← app/service/message_gate.py
├── ActiveSessionService.get_or_create_session()
├── ActiveSessionService.get_or_create_session()
│ 内存 KV → DB 冷恢复 → 新建
│ 内存 KV → DB 冷恢复 → 新建
│
│
├── 消费 intent_session 积压消息(busy 分支写入的)
│ 读取 KV.intent_session,拼入 user_query 前置
│ 消费后清空,防止重复消费
│
├── SessionMemory.get_messages()
├── SessionMemory.get_messages()
│ 加载 conversation_history,超限自动压缩
│ 加载 conversation_history,超限自动压缩
│
│
...
...
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
320535a
...
@@ -491,6 +491,20 @@ class AgentHub:
...
@@ -491,6 +491,20 @@ class AgentHub:
channel
=
channel
channel
=
channel
)
)
# 消费 intent_session 中的积压消息(在途任务期间 busy 分支写入的)
data
=
self
.
session_service
.
store
.
get
(
session_id
)
or
{}
intent_msgs
=
data
.
get
(
"intent_session"
)
if
isinstance
(
intent_msgs
,
list
)
and
len
(
intent_msgs
)
>
0
:
pending_texts
=
[
m
[
"content"
]
for
m
in
intent_msgs
if
m
.
get
(
"role"
)
==
"user"
]
if
pending_texts
:
user_query
=
"
\n
"
.
join
(
pending_texts
+
[
user_query
])
data
[
"intent_session"
]
=
[]
self
.
session_service
.
store
.
set
(
session_id
,
data
)
logger
.
info
(
"[AgentHub] 消费 intent_session:
%
d 条积压消息,合并后 query_len=
%
d"
,
len
(
intent_msgs
),
len
(
user_query
),
)
memory
=
SessionMemory
(
memory
=
SessionMemory
(
session_id
=
session_id
,
session_id
=
session_id
,
store
=
self
.
session_service
.
store
,
store
=
self
.
session_service
.
store
,
...
...
workspace/agent-admin/backend/app/service/message_gate.py
View file @
320535a
...
@@ -310,8 +310,11 @@ class MessageGate:
...
@@ -310,8 +310,11 @@ class MessageGate:
pending
:
List
[
Dict
[
str
,
Any
]],
pending
:
List
[
Dict
[
str
,
Any
]],
bot_id_mapping
:
Dict
[
str
,
str
],
bot_id_mapping
:
Dict
[
str
,
str
],
)
->
None
:
)
->
None
:
"""在途任务忙碌分支:保存用户消息到 history,发固定告知回复。"""
"""在途任务忙碌分支:将用户消息写入 intent_session,发固定告知回复。
from
app.service.memory
import
SessionMemory
intent_session 是隔离字段,不与 AgentHub 的 conversation_history 竞争。
AgentHub 下次启动时读取并合并到输入中。
"""
from
app.infrastructure.session_store
import
SessionStore
from
app.infrastructure.session_store
import
SessionStore
store
=
SessionStore
()
store
=
SessionStore
()
text_msgs
=
[
text_msgs
=
[
...
@@ -320,9 +323,14 @@ class MessageGate:
...
@@ -320,9 +323,14 @@ class MessageGate:
]
]
if
text_msgs
:
if
text_msgs
:
merged_text
=
"
\n
"
.
join
(
text_msgs
)
merged_text
=
"
\n
"
.
join
(
text_msgs
)
mem
=
SessionMemory
(
session_id
,
store
)
data
=
store
.
get
(
session_id
)
or
{}
mem
.
add
(
"user"
,
merged_text
)
intent_msgs
=
data
.
get
(
"intent_session"
)
self
.
session_service
.
save_to_db
(
session_id
)
if
isinstance
(
intent_msgs
,
list
):
intent_msgs
.
append
({
"role"
:
"user"
,
"content"
:
merged_text
})
else
:
intent_msgs
=
[{
"role"
:
"user"
,
"content"
:
merged_text
}]
data
[
"intent_session"
]
=
intent_msgs
store
.
set
(
session_id
,
data
)
await
self
.
post_reply_hook
.
execute
({
await
self
.
post_reply_hook
.
execute
({
"agent_name"
:
self
.
main_agent_key
,
"agent_name"
:
self
.
main_agent_key
,
"response_content"
:
BUSY_REPLY
,
"response_content"
:
BUSY_REPLY
,
...
...
workspace/agent-admin/backend/tests/test_message_gate.py
View file @
320535a
...
@@ -161,3 +161,81 @@ def test_extract_query_and_file_url_empty():
...
@@ -161,3 +161,81 @@ def test_extract_query_and_file_url_empty():
query
,
file_url
=
MessageGate
.
_extract_query_and_file_url
([])
query
,
file_url
=
MessageGate
.
_extract_query_and_file_url
([])
assert
query
==
""
assert
query
==
""
assert
file_url
is
None
assert
file_url
is
None
@pytest.mark.asyncio
async
def
test_handle_busy_writes_to_intent_session
():
"""_handle_busy 写入 intent_session 且不污染 conversation_history。"""
from
app.infrastructure.session_store
import
SessionStore
,
clear_default_store_for_tests
clear_default_store_for_tests
()
store
=
SessionStore
()
sid
=
"test-busy-session"
store
.
set
(
sid
,
{
"conversation_history"
:
[],
"intent_session"
:
[]})
mock_post_reply
=
MagicMock
()
mock_post_reply
.
execute
=
AsyncMock
()
gate
=
MessageGate
(
config_loader
=
MagicMock
(),
session_service
=
MagicMock
(),
post_reply_hook
=
mock_post_reply
,
agent_hub
=
MagicMock
(),
main_agent_key
=
"11"
,
model
=
MagicMock
(),
)
pending
=
[{
"msg_type"
:
"text"
,
"msg_content"
:
"帮我查一下"
}]
await
gate
.
_handle_busy
(
session_id
=
sid
,
room_id
=
"room-1"
,
user_id
=
"user-1"
,
pending
=
pending
,
bot_id_mapping
=
{
"11"
:
"bot_main"
},
)
data
=
store
.
get
(
sid
)
# 不污染 conversation_history
assert
data
[
"conversation_history"
]
==
[]
# 写入 intent_session
assert
len
(
data
[
"intent_session"
])
==
1
assert
data
[
"intent_session"
][
0
][
"role"
]
==
"user"
assert
data
[
"intent_session"
][
0
][
"content"
]
==
"帮我查一下"
# 发送了 BUSY_REPLY
mock_post_reply
.
execute
.
assert_called_once
()
call_context
=
mock_post_reply
.
execute
.
call_args
[
0
][
0
]
assert
"正在执行任务中"
in
call_context
[
"response_content"
]
@pytest.mark.asyncio
async
def
test_handle_busy_accumulates_in_intent_session
():
"""多次 busy 调用应累积消息到 intent_session。"""
from
app.infrastructure.session_store
import
SessionStore
,
clear_default_store_for_tests
clear_default_store_for_tests
()
store
=
SessionStore
()
sid
=
"test-busy-accum"
store
.
set
(
sid
,
{
"conversation_history"
:
[],
"intent_session"
:
[]})
mock_post_reply
=
MagicMock
()
mock_post_reply
.
execute
=
AsyncMock
()
gate
=
MessageGate
(
config_loader
=
MagicMock
(),
session_service
=
MagicMock
(),
post_reply_hook
=
mock_post_reply
,
agent_hub
=
MagicMock
(),
main_agent_key
=
"11"
,
model
=
MagicMock
(),
)
await
gate
.
_handle_busy
(
sid
,
"room"
,
"u1"
,
[{
"msg_type"
:
"text"
,
"msg_content"
:
"第一条"
}],
{
"11"
:
"bot"
})
await
gate
.
_handle_busy
(
sid
,
"room"
,
"u1"
,
[{
"msg_type"
:
"text"
,
"msg_content"
:
"第二条"
}],
{
"11"
:
"bot"
})
data
=
store
.
get
(
sid
)
assert
len
(
data
[
"intent_session"
])
==
2
assert
data
[
"intent_session"
][
0
][
"content"
]
==
"第一条"
assert
data
[
"intent_session"
][
1
][
"content"
]
==
"第二条"
assert
data
[
"conversation_history"
]
==
[]
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