完整请求处理流程:“查下腾讯的股价”
以下是基于云上机器部署openclaw的实际日志和源码分析,还原的从飞书到 OpenClaw 最终返回飞书的完整处理链路:
详细分阶段分析(结合实际日志)
阶段一:飞书 → OpenClaw(消息接收)
时间线: 2026-03-06 15:21:22 (UTC+8)
- 飞书长连接接收消息
- 飞书 SDK 的 WSClient 通过 WebSocket 长连接与飞书服务器保持连接
- 飞书服务器通过 im.message.receive_v1 事件推送消息
- 源码位置: monitor.ts 中 eventDispatcher.register
2.消息去重 + 解析(bot.ts)
- tryRecordMessage(messageId) — 防止 WebSocket 重连导致重复处理
- parseFeishuMessageEvent() — 从飞书事件中解析出:
- chatId, messageId, senderOpenId: ou_bd19daf85e6ce4df1890b2e52f
- chatType: p2p(私聊)
- content: “看下腾讯股价”
- 日志: feishu[main]: received message from ou_bd19daf85e6ce4df1890b2e52f
3.路由解析
- resolveAgentRoute() 根据 channel=feishu, peer={kind:“direct”, id:“ou_bd19…”}
- 得到 sessionKey 和 agentId: “main”
4.构建 Agent Envelope
- formatAgentEnvelope() 将消息包装为标准格式,包含时间戳、发送者信息等
- finalizeInboundContext()
填充完整的上下文:
Provider: “feishu”, Surface: “feishu” From: “feishu:ou_bd19…” To: “user:ou_bd19…” SessionKey: “agent:main:feishu:dm:ou_bd19…”
阶段二:OpenClaw Core(Agent 调度)
- Dispatch 到 Agent
- dispatchReplyFromConfig() 是核心调度函数(dispatch-from-config.ts)
- 它调用 getReplyFromConfig() → 解析模型配置 → 创建/恢复 Agent Session
- 日志: feishu[main]: dispatching to agent (session=…)
2.Typing Indicator
- 飞书没有原生“正在输入”API,OpenClaw 用消息 Reaction(表情反应) 模拟
- addTypingIndicator() — 在用户消息上添加 emoji 反应表示正在处理
阶段三:Agent 与 LLM 交互(3轮对话)
从 session JSONL 日志中可以清晰看到 3 轮 LLM 调用:
🔹 第1轮:识别意图,读取 Skill(10秒)
时间: 07:21:22 → 07:21:32 (UTC) Model: hunyuan-turbo-latest Input: 28,643 tokens | Output: 89 tokens
- Agent 收到用户消息后,扫描系统提示中的 列表
- 识别出 stock skill 与“腾讯股价”最匹配
- 发起 Tool Call: read(“~/.openclaw/workspace/skills/stock/SKILL.md”)
- 系统提示中的 Skills 规则:
“Before replying: scan entries. If exactly one skill clearly applies: read its SKILL.md at with Read, then follow it.”
🔹 第2轮:按 SKILL.md 指引构造查询(24秒)
时间: 07:21:32 → 07:21:56 (UTC) Model: hunyuan-turbo-latest Input: 28,621 tokens | Output: 292 tokens
- Agent 阅读了 SKILL.md 中的 Workflow:
- 识别“腾讯” → 港股代码 00700 → query param r_hk00700
- 构造查询命令: curl -s –max-time 10 “https://qt.gtimg.cn/q=r_hk00700” > /tmp/stock.txt && python3 -c “…”
- 发起 Tool Call: exec(command) — 在服务器上执行 curl + python3
exec 执行详情 (耗时 193ms):
curl → https://qt.gtimg.cn/q=r_hk00700 → 保存到 /tmp/stock.txt python3 → 读取 GBK 编码文件 → 解析 ~ 分隔字段 → 输出: 腾讯控股 (00700) 当前价格: 516.500 HKD 涨跌: 14.500 (2.89%) 开盘: 504.000 | 最高: 522.000 | 最低: 502.000 昨收: 502.000 | 成交量: .0
🔹 第3轮:格式化最终回复(20秒)
时间: 07:21:57 → 07:22:17 (UTC) Model: hunyuan-turbo-latest Input: 28,651 tokens | Output: 148 tokens stopReason: “stop”
- Agent 将原始数据格式化为用户友好的 Markdown 回复:
📈 腾讯控股 (http://00700.HK) 当前价格: 516.500 HKD 涨跌: +14.500 HKD (+2.89%) … 腾讯股价今天表现不错,上涨超过2.8%…
阶段四:OpenClaw → 飞书(回复发送)
- Reply Dispatcher 处理(reply-dispatcher.ts)
- deliver(payload) 回调被触发,收到最终文本
- shouldUseCard(text) 检测是否包含 Markdown(bold → true)
- 选择 Card 渲染模式(sendMarkdownCardFeishu)
2.发送飞书消息(send.ts)
- buildMarkdownCard(text) 构建飞书交互式卡片(schema 2.0)
- sendCardFeishu() → 调用飞书 API client.im.message.reply()
- 以 reply 形式回复原消息(引用用户的消息)
3.清理 Typing Indicator
- removeTypingIndicator() — 移除之前添加的 emoji 反应
4.日志记录: feishu[main]: dispatch complete (queuedFinal=…, replies=1)
耗时统计
| 阶段 | 耗时 | 说明 |
| 飞书 → OpenClaw 接收 | < 100ms | WebSocket 长连接,几乎无延迟 |
| 第1轮 LLM(识别 Skill) | ~10s | 混元 turbo-latest,28K tokens 上下文 |
| 第2轮 LLM(构造查询) | ~24s | 含 SKILL.md 全文 |
| exec 执行 curl+python3 | 193ms | 腾讯财经 API 极快 |
| 第3轮 LLM(格式化回复) | ~20s | 生成最终 Markdown |
| 发送回飞书 | < 500ms | 飞书 API 调用 |
| 总计 | ~55s | 主要耗时在 LLM 3轮调用 |
关键数据
- 模型: hunyuan-turbo-latest(混元,腾讯云)
- Session 文件: /root/.openclaw/agents/main/sessions/647eaa2f-41cf-4521-a803-b62ce.jsonl
- 总 Token 消耗: ~86K input + ~529 output(3轮合计)
- 消息 ID: om_x100b559c4b2080a0b295e94cc
- 回复方式: 飞书交互式卡片(Markdown 渲染)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/214517.html