hermes-stdout-lesson.md 1.29 KB

Hermes chat stdout 不可靠

hermes chat --yolo 的 stdout 包含:

  1. Prompt 原文回声
  2. 工具调用结果(web_search、curl、terminal 输出)
  3. 元数据行(Messages:、Session:、Duration:、Model: 等)
  4. CLI 提示(Resume this session with:、hermes --resume xxx)
  5. 分隔线(───、═══ 等)
  6. 最终回复(可能在任意位置,可能被截断)

已尝试的解析方案(全部失败)

方案 失败原因
取最后一行 拿到 CLI 元数据 hermes --resume xxx
CJK 过滤 + 取最后 拿到截断碎片(47 chars)
===报告=== 分隔符 hermes 不遵守,或分隔符前有大量 prompt 回声
噪音前缀白名单 永远加不完(hermes/Resume this/Session saved/...)
全部有效行拼接 把 prompt 原文也拼进去了

唯一可靠方案:文件 I/O

# 1. Prompt 中指定输出文件
prompt = f"...把报告写入 {result_file}。只写报告本身。"

# 2. 必须带 -t terminal,file,web
cmd = ["hermes", "chat", "-q", prompt, "--yolo", "-t", "terminal,file,web"]

# 3. 运行后直接读文件
with open(result_file) as f:
    draft = f.read().strip()

# 4. 清理
os.remove(result_file)

这个教训适用于所有 spawn hermes chat 作为子进程的场景。