Skip to main content

模式 3:OpenClaw

在 OpenClaw 项目中装入 @sonzai-labs/openclaw-context 插件,Sonzai 即成为智能体的 contextEngine — 持久记忆、情绪、人格、关系,全在 OpenClaw 现有的聊天循环之下。

OpenClaw 是一个开源对话式 AI 智能体框架,使用基于槽位的插件系统。决定每次系统提示词中包含哪些上下文 的槽位叫 contextEngine。安装 @sonzai-labs/openclaw-context 后会以 "sonzai" 名称注册 Sonzai 上下文引擎 — 在 openclaw.json 中把它 分配给该槽位,每次对话就会经过心智层,无需任何额外代码。

何时使用

  • 你已经在 OpenClaw 上开发,或团队已经标准化在 OpenClaw 上。
  • 你希望保留 OpenClaw 现有的聊天循环、遥测和工具插件 — 仅替换记忆/人格层。
  • 你希望在每个回合的系统提示词里自动注入一段 <sonzai-context>,按优先级排序、按 token 预算裁剪。

何时切换

架构

OpenClaw Runtime              SonzaiContextEngine            Sonzai Mind Layer
    |                                |                            |
    |-- bootstrap(sessionId) ------->|                            |
    |                                |-- resolve agent + session->|
    |                                |<-- session state ----------|
    |                                |                            |
    |-- assemble(messages, budget) ->|                            |
    |                                |-- fetch memory, mood,      |
    |                                |   personality, goals ----->|
    |                                |<-- ranked context blocks --|
    |<-- systemPromptAddition -------|   priority-ordered,        |
    |                                |   token-budget-trimmed     |
    |                                |                            |
    |  [LLM call w/ enriched prompt] |                            |
    |                                |                            |
    |-- afterTurn(sessionId) ------->|                            |
    |                                |-- send transcript -------->|
    |                                |   Mind Layer extracts      |
    |                                |   facts, updates mood,     |
    |                                |   evolves personality      |
    |                                |                            |
    |-- compact(sessionId) --------->|                            |
    |                                |-- merge short → long term->|

端到端示例

OpenClaw 插件本身只支持 JavaScript(OpenClaw 是 JS 框架)。Python 与 Go 分支展示等价的 B2B 配置流程:通过 SHA1 推导出确定性的 agent UUID 并 写入 OpenClaw 配置 — 真正消费这份配置的运行时仍是 JS。

// 1. Install:
//    openclaw plugins install @sonzai-labs/openclaw-context
//    # or: npm install @sonzai-labs/openclaw-context
//
// 2. Run the setup wizard (interactive — asks for API key, agent name):
//    npx @sonzai-labs/openclaw-context setup
//
// 3. The wizard writes openclaw.json:
//    {
//      "plugins": {
//        "slots": { "contextEngine": "sonzai" },
//        "entries": {
//          "sonzai": {
//            "enabled": true,
//            "apiKey": "sk_your_api_key",
//            "agentId": "a1b2c3d4-..."
//          }
//        }
//      }
//    }
//
// 4. Start chatting — Sonzai is now the contextEngine:
//    openclaw chat
//
// For programmatic / B2B provisioning use the exported setup() helper:
import { setup } from "@sonzai-labs/openclaw-context";

const result = await setup({
apiKey:     "sk_your_api_key",
agentName:  "customer-support-bot",
configPath: "/path/to/openclaw.json",
});

console.log(result.agentId);  // deterministic UUID — safe to re-run
console.log(result.written);  // true — config file updated

幂等的配置流程

Agent ID 由 SHA1(tenantID + agentName) 推导。对相同 tenant + name 多次调用 setup()(或 Python/Go 等价实现)总是返回同一 agent — 每次部署重跑都安全。

下一步

On this page