# Hermes WeChat 本地闭环架构方案 > 版本: 2.0 (HTTP API 模式) > 日期: 2026-05-22 > 目标设备: Pixel 10 Fold (Android) > 运行环境: Termux + Ubuntu proot-distro > 微信版本: 8.0.72 --- ## 1. 目标 在 Pixel 手机本地实现微信全自动闭环: ``` 收到消息 → 读取内容 → HTTP 调用 hermes → 生成回复 → 发送回复 ``` **不依赖 Mac 远程 adb**,所有组件运行在 Android 本地。 **核心设计原则**: - **daemon** 是常驻后台进程,只负责数据读写和消息收发 - **hermes** (lemonPCRM) 是独立的 AI 服务,负责 LLM 推理和回复生成 - 两者通过 HTTP API (`/api/master/chat`) 通信 --- ## 2. 架构总览 ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ Pixel 手机本地运行环境 │ │ │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ Ubuntu proot-distro 容器 │ │ │ │ ┌─────────────────────────────────────────────────────────────────┐ │ │ │ │ │ hermes (lemonPCRM) │ │ │ │ │ │ ┌──────────────┐ ┌──────────┐ ┌──────────┐ │ │ │ │ │ │ │ Node.js │ │ Master │ │ Claude │ │ │ │ │ │ │ │ Express │ │ Session │ │ CLI │ │ │ │ │ │ │ │ :3010 │ │ Manager │ │ Protocol │ │ │ │ │ │ │ └──────────────┘ └──────────┘ └──────────┘ │ │ │ │ │ │ ↑ │ │ │ │ │ │ │ POST /api/master/chat (SSE) │ │ │ │ │ │ │ {message, sessionId} │ │ │ │ │ └─────────┼────────────────────────────────────────────────────────┘ │ │ │ │ │ │ │ │ │ ┌─────────┴─────────────────────────────────────────────────────────┐ │ │ │ │ │ daemon (Python) │ │ │ │ │ │ ┌──────────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │ │ │ │ │ │ │ DB 轮询/ │→ │ 新消息 │→ │ HTTP │→ │ am broadcast │ │ │ │ │ │ │ │ 通知触发 │ │ 检测 │ │ hermes │ │ 发送回复 │ │ │ │ │ │ │ └──────────────┘ └──────────┘ └──────────┘ └──────────────┘ │ │ │ │ │ │ ↑ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ┌──────────────┐ ┌──────────┴─────┐ │ │ │ │ │ │ │ sqlcipher │ │ 回复内容 │ │ │ │ │ │ │ │ 解密读取 │ │ (SSE result) │ │ │ │ │ │ │ └──────────────┘ └────────────────┘ │ │ │ │ │ └──────────────────────────────────────────────────────────────────┘ │ │ │ │ ↑ │ │ │ │ │ │ │ │ │ ┌──────────────┐ ┌──────────┴──┐│ │ │ │ su -c cp DB │ │ LSPosed ││ │ │ │ + WAL + SHM │ │ WeChatHook ││ │ │ └──────────────┘ └─────────────┘│ │ └───────────────────────────────────────────────────────────────────────────┘ │ │ ↑ │ │ │ │ ▼ │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ Termux 环境 (与 proot 共享 home) │ │ │ │ ┌─────────────────────────────────────────────────────────────────┐ │ │ │ │ │ 通知监听线程 (辅助通道) │ │ │ │ │ │ termux-notification-list 每 3 秒轮询 │ │ │ │ │ │ 发现微信通知 → 触发紧急 DB 轮询 │ │ │ │ │ └─────────────────────────────────────────────────────────────────┘ │ │ │ │ ┌─────────────────────────────────────────────────────────────────┐ │ │ │ │ │ Termux:Boot 开机自启 │ │ │ │ │ │ ~/.termux/boot/start-hermes.sh │ │ │ │ │ └─────────────────────────────────────────────────────────────────┘ │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ │ ↑ │ │ │ │ ▼ │ │ ┌───────────────────────────────────────────────────────────────────────┐ │ │ │ Android 系统层 │ │ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ │ │ 微信 APK │ │ SQLCipher DB │ │ 通知栏 │ │ │ │ │ │ (已 root) │ │ EnMicroMsg │ │ (微信通知) │ │ │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │ │ └───────────────────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────────────────────────┘ ``` --- ## 3. 数据流详解 ### 3.1 完整消息处理流程 ``` 1. 通知触发(或定时轮询) │ ▼ 2. daemon 复制 EnMicroMsg.db + WAL + SHM 到 /sdcard/hermes/dbs/ │ ▼ 3. daemon 用 sqlcipher 解密 DB │ ▼ 4. daemon 查询 message 表:SELECT * WHERE msgId > last_msg_id │ ▼ 5. daemon 提取消息内容 + 最近 5 条上下文 │ ▼ 6. daemon 构造 prompt,HTTP POST 到 hermes /api/master/chat │ { │ "message": "收到消息(来自 xxx):...\n上下文: ...", │ "sessionId": "wx-xxx" │ } │ ▼ 7. hermes (lemonPCRM) 调用 Claude CLI 处理消息,返回 SSE 流 │ events: session → progress → agent_output → result │ ▼ 8. daemon 解析 SSE,提取 result.output 作为回复文本 │ ▼ 9. daemon 执行 am broadcast 发送回复到微信 │ ▼ 10. LSPosed 模块拦截广播,调用微信内部 API 发送消息 ``` ### 3.2 Session 管理 每个微信联系人 (`talker`) 对应一个独立的 hermes session: | talker | sessionId | 作用 | |--------|-----------|------| | `filehelper` | `wx-filehelper` | 文件传输助手 | | `wxid_abc123` | `wx-wxid_abc123` | 个人联系人 | | `123456789@chatroom` | `wx-123456789@chatroom` | 群聊 | **好处**: - hermes 自动维护每个对话的上下文记忆 - 不同联系人的对话互不干扰 - session 10 分钟空闲后自动清理 --- ## 4. 消息监听双通道 ### 4.1 通道 A: DB 轮询(主推) **原理**: 定期复制微信加密 DB → sqlcipher 解密 → 查询 message 表 → 检测 msgId 变化 **流程**: ```python while True: su -c "cp /data/data/com.tencent.mm/MicroMsg//EnMicroMsg.db /sdcard/hermes/dbs/" su -c "cp /data/data/com.tencent.mm/MicroMsg//EnMicroMsg.db-wal /sdcard/hermes/dbs/" sqlcipher EnMicroMsg.db "PRAGMA key='e03ce81'; ..." SELECT * FROM message WHERE msgId > last_msg_id ORDER BY msgId UPDATE last_msg_id = max(msgId) sleep(10) ``` **关键参数**: | 参数 | 值 | 说明 | |------|-----|------| | SQLCipher key | `e03ce81` | 7 字符字符串密码 | | cipher_compatibility | `1` | WCDB 兼容模式 | | cipher_page_size | `1024` | 页大小 | | 轮询间隔 | `10s` | 可配置 | | WAL 文件 | **必须复制** | 否则丢失最新数据 | ### 4.2 通道 B: 通知监听(辅助) **原理**: 通过 Termux:API 的 `termux-notification-list` 读取系统通知栏,发现微信通知后立即触发一次 DB 轮询。 **流程**: ```python while True: notifs = termux-notification-list wechat_notifs = [n for n in notifs if n.pkgName == "com.tencent.mm"] for n in wechat_notifs: if is_new(n): trigger_urgent_db_poll() # 不等 10 秒周期 sleep(3) ``` **权限要求**: 1. 安装 Termux:API 应用 2. 设置 → 应用 → Termux:API → 通知访问权限 → 允许 3. 关闭 Termux 和 Termux:API 的电池优化 ### 4.3 双通道协同 ``` 正常状态: DB 轮询每 10 秒执行一次 收到新消息: 通知栏产生通知 ──→ termux-notification-list 在 3 秒内发现 │ ▼ 触发紧急 DB 轮询(不等 10 秒周期) │ ▼ 获取完整消息内容 ──→ HTTP POST hermes ──→ 发送回复 ``` --- ## 5. Hermes HTTP API 调用 ### 5.1 API 端点 | 端点 | 方法 | 说明 | |------|------|------| | `POST /api/master/chat` | SSE | 发送消息,流式返回处理结果 | | `GET /api/health` | HTTP | 健康检查 | ### 5.2 请求格式 ```json POST /api/master/chat Content-Type: application/json { "message": "收到消息(来自 filehelper):你好\n请回复:", "sessionId": "wx-filehelper" } ``` ### 5.3 SSE 响应格式 ``` event: session data: {"sessionId": "wx-filehelper"} event: progress data: {"step": "initializing", "message": "🤖 Master Agent 启动中..."} event: agent_output data: {"agent": "master", "message": "你好!很高兴收到你的消息。"} event: result data: {"sessionId": "wx-filehelper", "type": "text", "output": "你好!很高兴收到你的消息。"} ``` daemon 提取 `result` 事件的 `output` 字段作为最终回复。 ### 5.4 Prompt 构造 daemon 发送给 hermes 的消息格式: ``` 你是一个微信智能助手。根据收到的消息,给出简短、自然、得体的中文回复。 最近对话上下文: 对方: 在吗? 我: 在的,有什么可以帮你的? 收到新消息(来自 filehelper):帮我查一下明天的天气 请直接给出回复内容,不要加任何解释或格式标记。 ``` --- ## 6. 消息发送 ### 6.1 发送机制 LSPosed 模块 `HermesWeChatModule` 注入微信进程,暴露 BroadcastReceiver: ```bash /system/bin/am broadcast \ -a com.hermes.wechat.SEND_MSG \ --es toUser "filehelper" \ --es content "回复内容" ``` ### 6.2 发送限制 | 限制 | 说明 | |------|------| | 仅文本消息 | 当前模块只支持文本 | | 主线程调用 | NetScene 提交必须在主线程 | | 微信前台 | 建议保持微信在前台或后台运行 | --- ## 7. 开机自启(Termux:Boot) ### 7.1 启动脚本 文件: `~/.termux/boot/start-hermes.sh` ```bash #!/bin/bash # 启动 frida-server(可选) if [ -f "/data/local/tmp/frida-server" ]; then su -c "/data/local/tmp/frida-server &" fi # 启动微信(如果未运行) if ! su -c "pgrep -x com.tencent.mm" > /dev/null 2>&1; then su -c "am start -n com.tencent.mm/.ui.LauncherUI" sleep 5 fi # 启动 hermes (lemonPCRM) — 在 proot 内 export PATH=/data/data/com.termux/files/usr/bin:$PATH proot-distro login --shared-home ubuntu -- bash -c \ "cd /root/aipcrm && npm start &" sleep 5 # 启动守护进程 proot-distro login --shared-home ubuntu -- bash -c \ "cd /root/local-testkit && python3 hermes_wechat_daemon.py --daemon" # 发送启动通知 termux-notification \ --title "Hermes WeChat" \ --content "守护进程已启动" \ --priority low ``` ### 7.2 安装步骤 ```bash mkdir -p ~/.termux/boot cp setup_termux_boot.sh ~/.termux/boot/start-hermes.sh chmod +x ~/.termux/boot/start-hermes.sh ``` --- ## 8. 目录结构 ``` ~/local-testkit/ # daemon 目录 ├── hermes_wechat_daemon.py # 主守护进程 ├── config.json # 运行时配置 ├── test_env.py # 环境预检 ├── test_db_poll.py # DB 轮询测试 ├── test_send_msg.py # 发送消息测试 ├── test_termux_api.py # Termux API 测试 ├── test_hermes_api.py # hermes HTTP API 测试 ├── run_all_tests.sh # 一键测试 ├── setup_termux_boot.sh # 开机自启脚本 └── HERMES_WECHAT_ARCHITECTURE.md # 本方案文档 ~/aipcrm/ # hermes (lemonPCRM) 目录 ├── server/ # Express 后端 ├── packages/ # ACP 协议 + Agent 运行时 ├── package.json └── ... /sdcard/hermes/ # 运行时数据 ├── dbs/ # DB 副本 │ ├── EnMicroMsg.db │ ├── EnMicroMsg.db-wal │ └── EnMicroMsg.db.decrypted.db ├── wechat_msgs.jsonl # 消息日志 └── daemon.log # 守护进程日志 ``` --- ## 9. 配置说明 `config.json`: ```json { "db_src_path": "", "db_copy_dir": "/sdcard/hermes/dbs", "sqlcipher_key": "e03ce81", "cipher_compatibility": 1, "cipher_page_size": 1024, "poll_interval_sec": 10, "max_msgs_per_poll": 50, "hermes": { "api_url": "http://localhost:3010/api/master/chat", "timeout_sec": 120, "default_session_prefix": "wx-", "system_hint": "你是一个微信智能助手。根据收到的消息,给出简短、自然、得体的中文回复。" }, "auto_reply": { "enabled": false, "contacts": ["filehelper"], "exclude_types": [10000], "cooldown_sec": 5 }, "send": { "method": "am_broadcast", "broadcast_action": "com.hermes.wechat.SEND_MSG", "am_path": "/system/bin/am" }, "logging": { "level": "INFO", "file": "/sdcard/hermes/daemon.log" } } ``` **注意**:daemon 不配置 LLM API key,所有 LLM 配置在 hermes (lemonPCRM) 中。 --- ## 10. 部署步骤 ### Step 1: 环境准备 ```bash # Pixel Termux 中 pkg update pkg install python sqlcipher termux-api # 安装 Termux:Boot 和 Termux:API 应用(F-Droid 或 GitHub) ``` ### Step 2: 部署 hermes (lemonPCRM) ```bash # 在 proot Ubuntu 中 cd ~/aipcrm npm install # 配置 .env 中的 Claude API key npm start # 或 npm run dev(热重载) ``` 确认 hermes 监听 `localhost:3010`。 ### Step 3: 部署 daemon ```bash cd ~/local-testkit ``` ### Step 4: 运行测试 ```bash chmod +x run_all_tests.sh ./run_all_tests.sh ``` 预期结果: - `test_env.py`: 全部 [OK] - `test_db_poll.py`: DB 复制、解密、查询通过 - `test_send_msg.py`: am broadcast 发送成功 - `test_termux_api.py`: 通知读取成功 - `test_hermes_api.py`: hermes API 调用成功,收到回复 ### Step 5: 启动守护进程 ```bash # 单次测试 python3 hermes_wechat_daemon.py --once # 正常运行(前台) python3 hermes_wechat_daemon.py # 后台运行 python3 hermes_wechat_daemon.py --daemon ``` ### Step 6: 设置开机自启 ```bash cp setup_termux_boot.sh ~/.termux/boot/start-hermes.sh chmod +x ~/.termux/boot/start-hermes.sh ``` --- ## 11. 故障排查 ### Q: DB 解密失败 **A**: 1. 确认 key 正确 2. 确认 WAL 文件已复制:`ls -la /sdcard/hermes/dbs/EnMicroMsg.db*` 3. 微信运行时 DB 被锁定,必须通过 `su -c cp` 复制后再解密 ### Q: hermes API 调用超时 **A**: 1. 确认 hermes 已启动:`curl http://localhost:3010/api/health` 2. 检查 hermes 日志是否有 Claude CLI 调用错误 3. 增加 `hermes.timeout_sec`(Claude 推理可能需要 30-60 秒) ### Q: hermes 返回空回复 **A**: 1. 检查 hermes 的 `system/agents/` 目录是否有微信相关 Agent 2. 查看 hermes 日志中的 SSE 事件流 3. 确认 Claude CLI 进程正常运行(非 mock 模式) ### Q: am broadcast 发送成功但微信没收到 **A**: 1. 检查 LSPosed 模块是否启用且作用域包含微信 2. 检查日志:`su -c "cat /data/adb/lspd/log/modules_*.log | grep HermesWX | tail -20"` 3. 确认微信在主进程运行 ### Q: termux-notification-list 返回空 **A**: 1. 确认 Termux:API 应用已安装 2. 确认已授予通知访问权限 3. 发送一条测试消息,等待通知出现 --- ## 12. 安全与风险提示 1. **微信封号风险**: 自动化操作存在被检测风险,建议使用测试小号 2. **Root 权限**: 需要 Magisk root 3. **LSPosed 模块**: 仅作用于微信进程 4. **API Key**: Claude API key 仅在 hermes 中配置,daemon 不持有 --- ## 13. 扩展方向 - [ ] 支持群聊 @识别和回复 - [ ] 支持图片/语音消息处理 - [ ] 本地 LLM 部署(减少 API 延迟) - [ ] Web UI 管理界面 - [ ] 多账号支持