From fde5c58dfa115e7147ab9ec596568069988beb2a Mon Sep 17 00:00:00 2001 From: yibo Date: Thu, 4 Jun 2026 00:44:52 +0800 Subject: [PATCH] docs(hermes/lifelog): api_server 纪要后端 + token 精简 + service.d 自启 --- lifelog-recorder/HANDOFF.md | 44 +++++++++++++++++++++++++++++++++++++++----- wechat_plain/scripts/local-testkit/docs/HERMES_AGENT_PIXEL_SETUP.md | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/lifelog-recorder/HANDOFF.md b/lifelog-recorder/HANDOFF.md index 5530a59..efafb10 100644 --- a/lifelog-recorder/HANDOFF.md +++ b/lifelog-recorder/HANDOFF.md @@ -166,7 +166,7 @@ VAD 参数(在 `RecorderService.runPipeline` 里调): 2. **设置页**:存储用量、模型切换、provider 切换、暂停时段配置(v1 定的 MVP 是「一键暂停就够」,但 v2 可能要细化)。 3. **多 anchor 平均**:现在 enroll 是单段单 anchor,可以「连选多段都标 我」自动算平均 embedding 提高匹配率。 4. **回链 lemonPCRM**:Transcript 提到的人名 / 项目 → GraphStore `MENTIONED_IN` 边。 -5. **hermes gateway 自启**:当前需用户手动跑 `bash ~/start_gw.sh`,可写到 Termux:Boot 启动脚本。 +5. ✅ **hermes gateway 自启**(2026-06-04 完成):用 Magisk `service.d` 脚本开机拉起(Termux:Boot 未装,root 方案更稳),见下方「§ 自启(Magisk service.d)」。 ### hermes gateway 设置(v0.5 关键前置) @@ -178,8 +178,8 @@ API_SERVER_PORT=8642 API_SERVER_KEY= API_SERVER_MODEL_NAME=kimi-for-coding -# 需要 anthropic SDK(kimi-for-coding 协议层走 Anthropic-compat) -~/hermes-agent/venv/bin/pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ 'anthropic>=0.39.0' +# anthropic SDK:NousResearch hermes-agent 的 .[termux-all] 已自带(v0.15.1 = anthropic 0.87); +# 旧版若缺再补:~/.hermes/hermes-agent/venv/bin/pip install 'anthropic>=0.39.0' # 启动:必须用 setsid + disown 才能在 adb shell 之外存活 cat > ~/start_gw.sh <<'EOF' @@ -196,8 +196,42 @@ chmod 755 ~/start_gw.sh bash ~/start_gw.sh ``` -lifelog 的 `keys.properties` 里 `hermes.apiUrl` / `hermes.apiKey` 要跟这里 -对得上才能调通。`network_security_config.xml` 已开 127.0.0.1 cleartext 豁免。 +lifelog 的 `keys.properties` 里 `hermes.apiUrl`(http://127.0.0.1:8642) / `hermes.apiKey` +要跟 `API_SERVER_KEY` 对得上才能调通。`network_security_config.xml` 已开 127.0.0.1 cleartext 豁免。 + +> [!IMPORTANT] hermes 的 Kimi provider 配置(v0.5 实测) +> hermes-agent(NousResearch)原生支持 Kimi Coding Plan,配成 `model.provider=kimi-coding` + +> `model.base_url=https://api.kimi.com/coding` + `model.default=kimi-for-coding`,`.env` 放 +> `KIMI_API_KEY=sk-kimi-...`。运行时自动切 Anthropic 协议 + 注入 claude-code UA。 +> 完整流程见 `wechat_plain/scripts/local-testkit/docs/HERMES_AGENT_PIXEL_SETUP.md`。 + +### ⚠️ 必做:精简 api_server 的 token 开销(2026-06-04 实测) + +api_server **默认走 hermes 完整 agent**,每次纪要 prompt 注入 tools(37.8KB/17工具) + skills index(9.1KB) += **~12,500 token**,纯属浪费(纪要只要纯文本总结)。只对 api_server 关掉 tools+skills(不影响 CLI/微信 bot): + +```yaml +# ~/.hermes/config.yaml +platform_toolsets: + api_server: [] # 关 tools +skills: + platform_disabled: + api_server: [<列出 ~/.hermes/skills 下所有 skill 名>] # 关 skills index +``` +用 hermes 自己的 config 模块改最稳(`load_config`/`save_config`,保留其他配置)。改完重启 gateway。 +**实测:prompt 12,500 → 399 token(-97%),纪要正常生成。** + +### 自启(Magisk service.d) + +Termux:Boot 未装(且无匹配签名 APK);已 root,用 Magisk service.d 更稳(不需开 Termux app)。 +`/data/adb/service.d/start-hermes-gw.sh`(root, 755):等 `boot_completed` → sleep 25 → +动态推导 Termux uid + su 路径(`/system_ext/bin/su`,**不是** `/system/bin/su`)→ 以 Termux 用户 +(带 `-G 3003 -G 9997 -G 20 -G 50`)跑 `~/start_gw.sh`。实测手动执行 `launched rc=0`, +gateway 以 `u0_a302` 属主起来、api_server 正常。**冷启动慢 ~3–5 分钟**(初始化时连被墙的 +openrouter/nous metadata 超时),对开机服务可接受。 + +> 验证端到端:`adb shell "su -g 10302 -G 3003 -G 9997 -G 20302 -G 50302 10302 -c '…venv/bin/python ~/test_api.py'"` +> 或 lifelog 纪要 tab「现在跑一次」(需最近 1h 有 ≥60s 语音)。 ### 存储预算(v0.2 后) diff --git a/wechat_plain/scripts/local-testkit/docs/HERMES_AGENT_PIXEL_SETUP.md b/wechat_plain/scripts/local-testkit/docs/HERMES_AGENT_PIXEL_SETUP.md index bc751e5..bbb9366 100644 --- a/wechat_plain/scripts/local-testkit/docs/HERMES_AGENT_PIXEL_SETUP.md +++ b/wechat_plain/scripts/local-testkit/docs/HERMES_AGENT_PIXEL_SETUP.md @@ -143,6 +143,67 @@ hermes -z "用中文只回答一个数字: 1+1=?" | `HTTP 403 access_terminated_error` | 走了 OpenAI `/chat/completions` 路径;必须 kimi-coding(anthropic 协议,hermes 自动加 claude-code UA) | | `no final response was produced` | config 残留错的 `model.base_url`(如 openrouter)或 provider 不对;按 2.2 重设三项 | | `curl: Could not resolve host` | `su` 切 uid 时漏了 `-G 3003`(inet 组) | +| `APIConnectionError / Connection error`(api_server 返回 200 但内容报错) | 出口被掐:VPN(ClashMeta) 死节点把 TCP 全断,或 WiFi 没真连上(`UnderlyingNetworks: []`)。停 VPN / 修网即恢复 | + +--- + +## 3. 作为 lifelog 纪要后端:开 api_server + 省 token + 自启 + +hermes gateway 内建一个 OpenAI 兼容的 **api_server**,lifelog 的 SummaryWorker 用它跑纪要 +(`POST http://127.0.0.1:8642/v1/chat/completions`)。2026-06-04 实测打通。 + +### 3.1 开 api_server + +`~/.hermes/.env` 追加(`API_SERVER_KEY` **必须**等于 lifelog `keys.properties` 的 `hermes.apiKey`): +``` +API_SERVER_ENABLED=true +API_SERVER_HOST=127.0.0.1 +API_SERVER_PORT=8642 +API_SERVER_KEY=<与 lifelog hermes.apiKey 一致> +API_SERVER_MODEL_NAME=kimi-for-coding +``` +启动:`hermes gateway run`(前台);常驻用 `~/start_gw.sh`(`setsid hermes gateway run` + disown, +先 `set -a; . ~/.hermes/.env; set +a`)。监听后日志:`[Api_Server] API server listening on http://127.0.0.1:8642`。 + +### 3.2 ⚠️ 必做:精简 token(12,500 → 399) + +api_server 默认走**完整 agent**:每请求注入 tools(37.8KB/17工具) + skills index(9.1KB) ≈ **12,500 token**, +纯总结根本用不上。只对 api_server 关掉(不影响 CLI / 微信 bot): +```yaml +# ~/.hermes/config.yaml +platform_toolsets: + api_server: [] +skills: + platform_disabled: + api_server: [<~/.hermes/skills 下所有 skill 名>] +``` +> 用 hermes 的 `load_config`/`save_config` 改最稳(保留其余配置)。源码依据:`gateway/platforms/api_server.py` +> 的 `enabled_toolsets = _get_platform_tools(config, "api_server")`;skills 走 `skills.platform_disabled`。 +> 改完重启 gateway,实测 **prompt 399 token**。 + +### 3.3 开机自启(Magisk service.d) + +Termux:Boot 未装;已 root → 用 `/data/adb/service.d/start-hermes-gw.sh`(root, 755): +```sh +#!/system/bin/sh +LOG=/data/adb/hermes-boot.log +{ echo "=== $(date) ===" + i=0; while [ "$(getprop sys.boot_completed)" != "1" ] && [ "$i" -lt 60 ]; do sleep 3; i=$((i+1)); done + sleep 25 + T=/data/data/com.termux; GW=$T/files/home/start_gw.sh; [ -f "$GW" ] || exit 0 + SU=$(command -v su); [ -x "$SU" ] || for p in /system_ext/bin/su /system/bin/su /debug_ramdisk/su; do [ -x "$p" ] && SU=$p && break; done + U=$(stat -c %u "$T"); L=$((U-10000)) + "$SU" -g "$U" -G 3003 -G 9997 -G "20$L" -G "50$L" "$U" -c "/data/data/com.termux/files/usr/bin/bash $GW" +} >> "$LOG" 2>&1 +``` +**坑**:这台机 Magisk 的 su 在 **`/system_ext/bin/su`**(`/system/bin/su` 不存在)→ 必须 `command -v su` 动态解析。 +uid 动态推导(Termux 重装会变)。gateway 以 Termux 用户跑(保属主),冷启动慢 ~3–5 分钟(连被墙 metadata 超时)。 + +### 3.4 验证端到端 + +- 直打:`hermes` venv python POST `/v1/chat/completions`(Bearer=API_SERVER_KEY, model=kimi-for-coding)→ 200 + 纪要。 +- 整链:lifelog 纪要 tab「现在跑一次」(需最近 1h ≥60s 语音)→ logcat `SummaryWorker: summary saved` → 纪要入库。 +- **坑**:api_server 收到请求会回 200,但若上游连不上(VPN/断网)正文是 `API call failed ... Connection error`——查 `~/.hermes/logs/gateway.log`。 --- -- libgit2 0.21.4