Commit 4bfc0b92 by sunqi02 Committed by 胡悦

docs(agent_hub): document restoration criteria and update CLAUDE.md flow

1 parent 8c9de204
# Agent 服务层说明
> **本目录包含大量服务文件,但 Agent 相关(我们负责)的只有以下几个。其余服务属于管理后台/官网/群管理等模块,不需要我们修改。**
## 我们负责的文件
### 核心调度
| 文件 | 类 | 职责 |
|------|----|------|
| `agent_hub.py` | `AgentHub` | **中央调度器** — 唯一运行的 Agent 运行时 |
封装流程:
1. 接收 `process_message()` 调用
2. `get_or_create_session()` 获取/创建会话
3. `SessionMemory.get_messages()` 加载历史消息
4. `_build_main_prompt()` 组装 system prompt(含子 Agent 职责描述)
5. 创建 `ReActAgent`(AgentScope),注入 `InMemoryMemory` + `Toolkit`
6. `agent.reply(user_msg)` 执行 ReAct 循环(推理→工具调用→观察→再推理)
7. `_call_sub_agent()` 子 Agent 创建与执行(通过 `call_sub_agent` 工具触发)
8. `PostReplyHook.execute()` 通过 IM 发送最终回复
9. `save_to_db()` 持久化会话
### 会话管理
| 文件 | 类 | 职责 |
|------|----|------|
| `active_session_service.py` | `ActiveSessionService` | **活跃会话生命周期** |
封装流程:
- `get_or_create_session()` — 先查内存 KV(热路径)→ 再查 DB(冷恢复)→ 都没有则新建
- `save_to_db()` — 内存 KV → DB 持久化(归档时调用)
- 内存 KV 由 `SessionStore`(`infrastructure/session_store.py`)提供,TTL 30 分钟
### 消息历史
| 文件 | 类 | 职责 |
|------|----|------|
| `memory.py` | `SessionMemory` | **会话消息存取与压缩** |
封装流程:
- `add(role, content, agent_code)` — 追加一条消息到 `conversation_history`
- `get_messages(max_rounds)` — 获取历史,超限时自动 summarize_truncation(早期消息压缩为摘要)
- `mark(label)` — 插入检查点标记
- 底层读写 `SessionStore` 中的 `conversation_history` 数组
### IM 发送
| 文件 | 类 | 职责 |
|------|----|------|
| `im_service.py` | `IMService` | **生产环境 IM 发送** — 调用自建 IM 服务 API |
| `console_im_service.py` | `ConsoleIMService` | **测试/调试 IM** — 打印到控制台,不实际发送 |
两者实现相同接口 `send_message(bot_id, content, contact_id, at_list)`,通过 `IMSender` Protocol 解耦。
## 不属于我们的文件(不要修改)
| 文件 | 所属模块 |
|------|---------|
| `agent_service.py` | Agent 管理后台(CRUD 配置) |
| `agent_group_helper_service.py` | Agent 群管理(渠道活码、一客一群) |
| `analytics_service.py` | 数据分析 |
| `file_service.py` | 文件上传与文本提取(仅 `routing.py` 端点使用) |
| `methodology_service.py` | 方法论展示 |
| `official_site_*.py` | 官网各页面 |
| `position_tag_service.py` | 岗位标签管理 |
| `qrcode_channel_service.py` | 渠道活码 |
| `routing_service.py` | 路由配置 |
| `skill_service.py` | 技能管理后台 |
| `wecom_*.py` | 企微同步/群编排 |
## 关联目录
| 目录 | Agent 相关文件 | 说明 |
|------|---------------|------|
| `app/api/v1/endpoints/agent_hub.py` | 是 | API 入口,解析 `ChatRequest` → 调用 `AgentHub.process_message()` |
| `app/hooks/` | 是 | `PostReplyHook`(回复后 IM 发送)、`Hook` 基类 |
| `app/infrastructure/session_store.py` | 是 | `SessionStore` 内存 KV,`ActiveSessionService` 的底层存储 |
| `app/infrastructure/file_parser.py` | 是 | `FileParser` + `OSSClient`,`FileService` 的底层依赖 |
| `app/config/loader.py` | 是 | `ConfigLoader` 加载 agents/models/skills/mappings JSON 配置 |
| `app/model/session.py` | 是 | `Session`、`ConversationHistory` ORM 模型 |
| `config/agents.json` | 是 | Agent 配置(prompt、skills 列表等) |
| `config/models.json` | 是 | 模型配置(api_key 使用 `${OPENAI_API_KEY}` 占位) |
| `skills/` | 是 | Skill 目录,每个子目录有 `SKILL.md`,由 `Toolkit.register_agent_skill()` 加载 |
## Agent 调用完整流程
```
用户消息 (企微/微信)
│
▼
IM 网关 (5s 聚合窗口)
│
▼
POST /api/v1/chat/completions ← app/api/v1/endpoints/agent_hub.py
│ 解析 ChatRequest: group, agents, messages
│ 提取 query, user_id, allowed_agent_codes, bot_id_mapping
│
▼
BackgroundTasks.add_task() ← 异步后台处理,HTTP 立即返回 200
│
▼
AgentHub.process_message() ← app/service/agent_hub.py
│
├── ActiveSessionService.get_or_create_session()
│ 内存 KV → DB 冷恢复 → 新建
│
├── SessionMemory.get_messages()
│ 加载 conversation_history,超限自动压缩
│
├── _build_main_prompt()
│ steward prompt + 子 Agent 职责描述 + REVIEW_GUIDE
│
├── 创建 ReActAgent (AgentScope)
│ 注入: system_prompt, model, formatter, toolkit, memory
│ toolkit = basic 工具 + agent 专属 skills
│
├── agent.reply(user_msg) ← ReAct 循环
│ │
│ ├── [推理] LLM 生成 thinking + tool_use
│ │ → post_reasoning hook: 发送 thinking 全文到 IM(截断逻辑临时关闭,验证 THINKING_GUIDE 约束效果)
│ │
│ ├── [行动] 执行工具调用
│ │ → pre_acting hook: 发送工具调用信息到 IM
│ │ → 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 → 用户收到回复
```
......@@ -60,6 +60,7 @@ async def _im_post_reasoning_hook(
return None
# 中间过程只发前10个字符 + 省略号
# 临时注释:让 LLM 自己控制思考长度,验证 prompt 约束效果
# 验证标准:手动采样 20 条 sub_agent thinking 文字,中位数 ≤25 字 且 最大值 ≤40 字 可删除本注释
# content = content[:25] + "..." if len(content) > 10 else content
# 删除"下载"这个词
content = content.replace("下载", "点击")
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!