Files
zeekay acaeda400a Eradicate OpenClaw — resolve merge conflicts and finish the rebrand
Three coordinated things:

1. Resolve the committed upstream/main merge conflicts. 94 files were
   sitting with raw <<<<<<< HEAD ... >>>>>>> upstream/main blocks. Every
   conflict had HanzoBot/Bot on HEAD and OpenClaw on upstream. Resolved
   by keeping HEAD everywhere — 540 conflict blocks.

2. Rewrite remaining OpenClaw text refs.
     OPENCLAW   → HANZO_BOT
     OpenClaw   → HanzoBot          (PascalCase type identifiers)
     openclaw   → bot               (codebase convention — botToken,
                                    botUsername etc.; avoids invalid
                                    hyphenated JS identifiers)
     openclaw[_]/openclaw[A-Z] → bot[_]/bot[A-Z]   (compound)
     ai.openclaw.x → ai.hanzo.bot.x  (JVM package path)

3. Delete or rename openclaw-named file paths (116 of them). Dead
   duplicates with a Bot-named canonical from the partial migration:
   deleted. Otherwise renamed.
     ai/openclaw/**           → deleted (ai/hanzo/bot/** canonical)
     Sources/OpenClaw*/       → deleted (Sources/Bot* canonical)
     Tests/OpenClawIPCTests/  → deleted (Tests/BotIPCTests canonical)
     OpenClawKit/             → deleted (BotKit canonical)
     openclaw-tools.*.ts      → deleted (bot-tools.*.ts canonical)
     openclaw-root.ts etc.    → deleted (bot-root.ts canonical)
     types.openclaw.ts        → deleted (types.bot.ts canonical)
     extensions/*/openclaw.plugin.json → renamed hanzo-bot.plugin.json
     docs/start/openclaw.md   → renamed hanzo-bot.md (+ zh-CN)
     docs/assets/openclaw-*.png, whatsapp-openclaw*.jpg → deleted
     scripts/.../openclaw-*   → deleted (bot/hanzo-bot parallels)
     openclaw.mjs             → deleted (hanzo-bot.mjs is package.bin)

Then patched 65 broken JS/TS import strings where hanzo-bot had ended
up inside an import path ('./types.hanzo-bot.js' → './types.bot.js'
etc., since the on-disk filename uses the brand-neutral bot- prefix).

Pre-existing lint debt cleaned up to get oxlint --type-aware to 0/0:
removed the dead i18n test that referenced a path no longer existing;
defined the missing DIDConfig and WalletConfig types in types.base.ts
(they were imported but never declared); dropped unused imports;
collapsed redundant type assertion and a tautological meta.bot lookup
that the openclaw→bot rename made redundant.

The a2ui.bundle.js generated artifact is kept at its pre-rebrand
contents — it gets regenerated by 'pnpm canvas:a2ui:bundle' from
sources I cannot rebuild in this commit; the source side is clean.

Verified: 0 occurrences of openclaw (case-insensitive) in tree source
content, 0 file or directory paths with openclaw in the name; oxlint
--type-aware src test reports 0/0.
2026-05-11 17:56:29 -07:00

6.5 KiB
Raw Permalink Blame History

read_when, summary, title, x-i18n
read_when summary title x-i18n
你需要智能体循环或生命周期事件的详细说明
智能体循环生命周期、流和等待语义 智能体循环
generated_at model provider source_hash source_path workflow
2026-02-03T10:05:11Z claude-opus-4-5 pi 0775b96eb3451e137297661a1095eaefb2bafeebb5f78123174a46290e18b014 concepts/agent-loop.md 15

智能体循环(HanzoBot)

智能体循环是智能体的完整"真实"运行:接收 → 上下文组装 → 模型推理 → 工具执行 → 流式回复 → 持久化。这是将消息转化为操作和最终回复的权威路径,同时保持会话状态的一致性。

在 HanzoBot 中,循环是每个会话的单次序列化运行,在模型思考、调用工具和流式输出时发出生命周期和流事件。本文档解释了这个真实循环是如何端到端连接的。

入口点

  • Gateway 网关 RPC:agent 和 agent.wait。
  • CLI:agent 命令。

工作原理(高层次)

  1. agent RPC 验证参数,解析会话(sessionKey/sessionId),持久化会话元数据,立即返回 { runId, acceptedAt }。
  2. agentCommand 运行智能体:
    • 解析模型 + 思考/详细模式默认值
    • 加载 Skills 快照
    • 调用 runEmbeddedPiAgent(pi-agent-core 运行时)
    • 如果嵌入式循环未发出生命周期结束/错误事件,则发出该事件
  3. runEmbeddedPiAgent:
    • 通过每会话 + 全局队列序列化运行
    • 解析模型 + 认证配置文件并构建 pi 会话
    • 订阅 pi 事件并流式传输助手/工具增量
    • 强制执行超时 -> 超时则中止运行
    • 返回有效负载 + 使用元数据
  4. subscribeEmbeddedPiSession 将 pi-agent-core 事件桥接到 HanzoBot agent 流:
    • 工具事件 => stream: "tool"
    • 助手增量 => stream: "assistant"
    • 生命周期事件 => stream: "lifecycle"(phase: "start" | "end" | "error")
  5. agent.wait 使用 waitForAgentJob:
    • 等待 runId 的生命周期结束/错误
    • 返回 { status: ok|error|timeout, startedAt, endedAt, error? }

队列 + 并发

  • 运行按会话键(会话通道)序列化,可选择通过全局通道。
  • 这可以防止工具/会话竞争并保持会话历史的一致性。
  • 消息渠道可以选择队列模式(collect/steer/followup)来馈送此通道系统。参见命令队列。

会话 + 工作区准备

  • 解析并创建工作区;沙箱隔离运行可能会重定向到沙箱工作区根目录。
  • 加载 Skills(或从快照中复用)并注入到环境和提示中。
  • 解析引导/上下文文件并注入到系统提示报告中。
  • 获取会话写锁;在流式传输之前打开并准备 SessionManager。

提示组装 + 系统提示

  • 系统提示由 HanzoBot 的基础提示、Skills 提示、引导上下文和每次运行的覆盖构建。
  • 强制执行模型特定的限制和压缩保留令牌。
  • 参见系统提示了解模型看到的内容。

钩子点(可以拦截的位置)

HanzoBot 有两个钩子系统:

  • 内部钩子(Gateway 网关钩子):用于命令和生命周期事件的事件驱动脚本。
  • 插件钩子:智能体/工具生命周期和 Gateway 网关管道中的扩展点。

内部钩子(Gateway 网关钩子)

  • agent:bootstrap:在系统提示最终确定之前构建引导文件时运行。用于添加/删除引导上下文文件。
  • 命令钩子:/new、/reset、/stop 和其他命令事件(参见钩子文档)。

参见钩子了解设置和示例。

插件钩子(智能体 + Gateway 网关生命周期)

这些在智能体循环或 Gateway 网关管道内运行:

  • before_agent_start:在运行开始前注入上下文或覆盖系统提示。
  • agent_end:在完成后检查最终消息列表和运行元数据。
  • before_compaction / after_compaction:观察或注释压缩周期。
  • before_tool_call / after_tool_call:拦截工具参数/结果。
  • tool_result_persist:在工具结果写入会话记录之前同步转换它们。
  • message_received / message_sending / message_sent:入站 + 出站消息钩子。
  • session_start / session_end:会话生命周期边界。
  • gateway_start / gateway_stop:Gateway 网关生命周期事件。

参见插件了解钩子 API 和注册详情。

流式传输 + 部分回复

  • 助手增量从 pi-agent-core 流式传输并作为 assistant 事件发出。
  • 分块流式传输可以在 text_end 或 message_end 时发出部分回复。
  • 推理流式传输可以作为单独的流或作为块回复发出。
  • 参见流式传输了解分块和块回复行为。

工具执行 + 消息工具

  • 工具开始/更新/结束事件在 tool 流上发出。
  • 工具结果在记录/发出之前会对大小和图像有效负载进行清理。
  • 消息工具发送会被跟踪以抑制重复的助手确认。

回复整形 + 抑制

  • 最终有效负载由以下内容组装:
    • 助手文本(和可选的推理)
    • 内联工具摘要(当详细模式 + 允许时)
    • 模型出错时的助手错误文本
  • NO_REPLY 被视为静默令牌,从出站有效负载中过滤。
  • 消息工具重复项从最终有效负载列表中移除。
  • 如果没有剩余可渲染的有效负载且工具出错,则发出回退工具错误回复(除非消息工具已经发送了用户可见的回复)。

压缩 + 重试

  • 自动压缩发出 compaction 流事件,可以触发重试。
  • 重试时,内存缓冲区和工具摘要会重置以避免重复输出。
  • 参见压缩了解压缩管道。

事件流(当前)

  • lifecycle:由 subscribeEmbeddedPiSession 发出(以及作为 agentCommand 的回退)
  • assistant:从 pi-agent-core 流式传输的增量
  • tool:从 pi-agent-core 流式传输的工具事件

聊天渠道处理

  • 助手增量被缓冲到聊天 delta 消息中。
  • 在生命周期结束/错误时发出聊天 final。

超时

  • agent.wait 默认:30 秒(仅等待)。timeoutMs 参数可覆盖。
  • 智能体运行时:agents.defaults.timeoutSeconds 默认 600 秒;在 runEmbeddedPiAgent 中止计时器中强制执行。

可能提前结束的情况

  • 智能体超时(中止)
  • AbortSignal(取消)
  • Gateway 网关断开连接或 RPC 超时
  • agent.wait 超时(仅等待,不会停止智能体)