Commit a2fa07e1 by sunqi02 Committed by 胡悦

docs: update all CLAUDE.md files to reflect MessageGate redesign architecture

- Root: add message-gate-redesign completion, remove intent_session from session flow, update design decisions
- Backend: replace intent_session decision with binary activation/mouth agent/dispatch mode
- Service: rewrite MessageGate flow, AgentHub dispatch, flow diagram, DISPATCH_GUIDE

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 1b8c8953
......@@ -8,20 +8,16 @@
| 文件 | 类 | 职责 |
|------|----|------|
| `message_gate.py` | `MessageGate` | **消息门控层** — 意图分类、重写、分发 |
| `message_gate.py` | `MessageGate` | **消息门控层** — 激活识别(chat/pass) |
封装流程:
1. `evaluate()` 获取 session 锁,等待非 text 消息的补充窗口
2. `_classify_and_rewrite()` 调 LLM 分类意图(actionable / casual / incomplete / ignore)+ 重写 query
- 从 `data["intent_session"]`(KV/DB 冷恢复)取出 base_messages
- history 拼成原生多轮 user/assistant messages 对(不再拼到 system prompt 文本中)
- 写回 `intent_session` → KV + DB(跨重启可恢复)
3. 根据 intent 分支:
- `actionable` → `_handle_actionable()` 调用 `AgentHub.process_message()` + 清空 intent_session
- `casual` → `_handle_casual()` 直接发 IM 回复 + 清空 intent_session
- `incomplete` → `_handle_incomplete()` 追问用户 → **追问写入 intent_session(不污染 conversation_history)** + 保留 pending_messages
- `ignore` → `remove_by_msg_ids()` + 清空 intent_session + save_to_db
4. `_get_lock(session_id)` 按 session 串行化,防止并发竞争
1. `evaluate()` 获取 session 锁,等待非 text 消息的补充窗口(默认 6s)
2. 检查 running task(已有则跳过,防重复处理)
3. `_classify_and_rewrite()` 调 LLM 做二元激活识别(chat vs pass),无历史上下文,只看当前消息
4. 根据 intent 分支:
- `chat` → `_handle_chat()` 只追加用户消息到 conversation_history,不触发 AgentHub,不发 IM("已读不回")
- `pass` → `_handle_actionable()` 提取 query/file_url,调用 `AgentHub.process_message()`
5. `_get_lock(session_id)` 按 session 串行化,防止并发竞争
| 文件 | 类 | 职责 |
|------|----|------|
......@@ -42,14 +38,19 @@
| `agent_hub.py` | `AgentHub` | **中央调度器** — 唯一运行的 Agent 运行时 |
封装流程:
1. 接收 `process_message()` 调用(由 MessageGate actionable 分支调用,或兜底直接调用)
1. 接收 `process_message()` 调用(由 MessageGate pass 分支调用,或兜底直接调用)
2. `get_or_create_session()` 获取/创建会话
3. `SessionMemory.get_messages()` 加载历史消息
4. `_build_main_prompt()` 组装 system prompt(DB prompt + soul_guide + 子Agent职责 + REVIEW_GUIDE + THINKING_GUIDE)
4. `_build_main_prompt()` 组装 system prompt(DB prompt + soul_guide 回复风格 + 子Agent职责列表 + DISPATCH_GUIDE + THINKING_GUIDE)
5. 创建 `ReActAgent`(AgentScope),注入 `InMemoryMemory` + `Toolkit`,主 Agent 用 `self.model`,子 Agent 用 `self.sub_model` (temp=0.2)
6. `agent.reply(user_msg)` 执行 ReAct 循环(推理→工具调用→观察→再推理)
7. `_call_sub_agent()` 子 Agent 创建与执行(通过 `call_sub_agent` 工具触发,使用 `self.sub_model`)
8. `PostReplyHook.execute()` 通过 IM 发送最终回复
- 自调用防护:拒绝调用 steward 自己的 code,引导调用发言人 Agent
- 子Agent ReAct 消息通过 `_save_tool_interactions()` 写入 SessionMemory,落 DB
- 口 Agent(code: -1)排除 `call_sub_agent` 工具,纯总结回复
8. 回复处理:
- `call_count >= 1`:主Agent 静默——不写 KV、不存 DB、不发送(发言人已发送最终回复)
- `call_count == 0`:主Agent 直接回复——正常写入 + 存 DB + PostReplyHook 发 IM
9. `save_to_db()` 持久化会话
### 会话管理
......@@ -60,9 +61,7 @@
封装流程:
- `get_or_create_session()` — 先查内存 KV(热路径)→ 再查 DB 冷恢复(`with db_factory() as db:` 短 session)→ 都没有则新建
- 冷恢复时将 `intent_session` 列(JSON)解析回 KV
- `save_to_db()` — 内存 KV → DB 持久化(`with db_factory() as db:` 短 session,修 Broken pipe)
- 写入 `intent_session` 列(list→JSON str,空字符串→`""`)
- `save_to_db()` — 内存 KV → DB 持久化(`with db_factory() as db:` 短 session,修 Broken pipe),写 `conversation_history` JSON
- 构造函数持有 `db_factory: Callable[[], Session]`(非长生命周期 db 实例)
- 内存 KV 由 `SessionStore`(`infrastructure/session_store.py`)提供,TTL 30 分钟
......@@ -131,6 +130,7 @@ POST /api/v1/chat/completions ← app/api/v1/endpoints/agent_hub.py
│ 解析 ChatRequest: group, agents, messages
│ 提取 query, user_id, bot_id_mapping
│ 追加消息到 SessionBuffer
│ 后端自动补全发言人 Agent code (-1) 到 allowed_agent_codes
│
▼
BackgroundTasks.add_task() ← 异步后台处理,HTTP 立即返回 200
......@@ -141,51 +141,59 @@ MessageGate.evaluate() ← app/service/message_gate.py
├── 获取 session 锁(按 session_id 串行化)
├── 等待非 text 消息补充窗口(默认 6s)
├── 检查 running task(已有则跳过)
├── _classify_and_rewrite() → LLM 意图分类 + query 重写
├── _classify_and_rewrite() → LLM 二元激活识别(chat/pass)
│
├── intent=ignore → 移除 pending_messages,结束
├── intent=casual → 直接发 IM 回复 + save_to_db()
├── intent=incomplete → 追问用户 + 保留 pending 等待补充
├── intent=chat → _handle_chat()
│ 追加用户消息到 conversation_history → save_to_db()
│ 不触发 AgentHub,不发 IM("已读不回")
│
└── intent=actionable → _handle_actionable()
│
▼
AgentHub.process_message() ← app/service/agent_hub.py
│
├── ActiveSessionService.get_or_create_session()
│ 内存 KV → DB 冷恢复 → 新建
│
├── SessionMemory.get_messages()
│ 加载 conversation_history,超限自动压缩
│
├── _build_main_prompt()
│ DB prompt + soul/agent.md 回复风格 + 子Agent职责 + REVIEW_GUIDE + THINKING_GUIDE
│
├── 创建 ReActAgent (AgentScope)
│ 主 Agent 用 self.model,子 Agent 用 self.sub_model (temp=0.2)
│ toolkit = basic 工具 + agent 专属 skills
│
├── agent.reply(user_msg) ← ReAct 循环
│ │
│ ├── [推理] LLM 生成 thinking + tool_use
│ │ → post_reasoning hook: _summarize_thinking()
│ │ 用 thinking_summary_model (deepseek-v4-flash)
│ │ 摘要化后发送到 IM,不泄露内部推理细节
│ │ (call_sub_agent tool_result 后的 reasoning 跳过,避免与最终回复重复)
│ │
│ ├── [行动] 执行工具调用
│ │ → call_sub_agent / read_file / skill 工具 / execute_code
│ │
│ ├── [观察] 工具返回结果,注入 memory
│ │
│ └── 重复,直到 LLM 输出最终回复(无 tool_use)
│
├── SessionMemory.add("assistant", response)
│
├── ActiveSessionService.save_to_db()
│
└── PostReplyHook.execute()
→ IMService.send_message() → 自建 IM 服务 API → 用户收到回复
└── intent=pass → _handle_actionable()
│
▼
AgentHub.process_message() ← app/service/agent_hub.py
│
├── ActiveSessionService.get_or_create_session()
│ 内存 KV → DB 冷恢复 → 新建
│
├── SessionMemory.get_messages()
│ 加载 conversation_history,超限自动压缩
│
├── _build_main_prompt()
│ DB prompt + soul/agent.md 回复风格 + 子Agent职责列表 + DISPATCH_GUIDE + THINKING_GUIDE
│
├── 创建 ReActAgent (AgentScope)
│ 主 Agent 用 self.model,子 Agent 用 self.sub_model (temp=0.2)
│ toolkit = basic 工具 + agent 专属 skills
│ 口 Agent (-1): 排除 call_sub_agent 工具,纯总结回复
│
├── agent.reply(user_msg) ← ReAct 循环
│ │
│ ├── [推理] LLM 生成 thinking + tool_use
│ │ → post_reasoning hook: _summarize_thinking()
│ │ 用 thinking_summary_model (deepseek-v4-flash)
│ │ 摘要化后发送到 IM,不泄露内部推理细节
│ │ (call_sub_agent tool_result 后的 reasoning 跳过,避免与最终回复重复)
│ │
│ ├── [调度] call_sub_agent → 子Agent ReAct 执行
│ │ │
│ │ ├── 自调用防护:拒绝 steward 自己的 code
│ │ ├── 创建子 ReActAgent + 执行
│ │ ├── 子Agent ReAct 消息写入 SessionMemory(通过 _current_route_ctx)
│ │ └── 串行链式调用:sub_A → sub_B → 口 Agent (-1)
│ │
│ ├── [观察] 工具返回结果,注入 memory
│ │
│ └── 重复,直到 LLM 输出最终回复(无 tool_use)
│
├── 回复判断(call_count)
│ ├── call_count >= 1 → 静默(发言人已发最终回复)
│ │ 不写 KV、不存 DB、不发送
│ └── call_count == 0 → 主Agent 直接回复
│ SessionMemory.add("assistant", response)
│ → save_to_db()
│ → PostReplyHook.execute() → IM 发送
│
└── ActiveSessionService.save_to_db()
```
### MessageGate 兜底逻辑
......@@ -204,6 +212,15 @@ MessageGate.evaluate() ← app/service/message_gate.py
### Prompt 装配顺序
```
_build_main_prompt: agent.prompt → soul_guide → 子Agent列表 → REVIEW_GUIDE → THINKING_GUIDE
_build_main_prompt: agent.prompt → soul_guide 回复风格 → 子Agent职责列表 → DISPATCH_GUIDE → THINKING_GUIDE
_build_sub_agent_prompt: agent.prompt → soul_guide → THINKING_GUIDE
```
### DISPATCH_GUIDE(调度规则)
```
1. 分析用户意图,判断是否在你的能力范围内(追问、澄清、简单对话等),可以直接回复
2. 复杂任务拆解为子任务步骤,按顺序调用子Agent(通过 call_sub_agent 工具),每次一个
3. 所有子Agent 执行完毕后,必须调用"发言人"Agent(调用码: -1)总结回复
4. 不要再调用同一个子Agent重复处理同一任务
```
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!