agentic_huge_data_base / wiki
页面 Mem0 · 9.2 OpenClaw 插件·DeepWiki 中文全文译文

9.2 · OpenClaw 插件(OpenClaw Plugin)

长期记忆与上下文管理 · 本章是 Mem0 DeepWiki 中文译文的独立章节页,保留原始链接、源码锚点、模块标签和章节层级。

项目Mem0 章节9.2 状态全文译文 模块系统架构、测试、发布与运维、界面与交互、智能体运行时
源码线索
  • docs/changelog/openclaw.mdx
  • docs/integrations/openclaw.mdx
  • openclaw/Makefile
  • openclaw/README.md
  • openclaw/backend/platform.ts
  • openclaw/cli/commands.ts
  • openclaw/cli/config-file.ts
  • openclaw/config.ts
  • openclaw/dream-gate.ts
  • openclaw/index.ts
模块标签
  • 系统架构
  • 测试、发布与运维
  • 界面与交互
  • 智能体运行时
  • 安装与启动

中文译文

OpenClaw 插件(中文译文)

原始 DeepWiki 页面:https://deepwiki.com/mem0ai/mem0/9.2-openclaw-plugin
翻译时间:2026-05-27T08:45:09.486Z
翻译模型:deepseek-chat
原文字符数:12250
项目:Mem0 (mem0)

---

OpenClaw 插件

相关源文件

以下文件用于生成此 Wiki 页面:

  • docs/changelog/openclaw.mdx
  • docs/integrations/openclaw.mdx
  • openclaw/Makefile
  • openclaw/README.md
  • openclaw/backend/platform.ts
  • openclaw/cli/commands.ts
  • openclaw/cli/config-file.ts
  • openclaw/config.ts
  • openclaw/dream-gate.ts
  • openclaw/index.ts
  • openclaw/openclaw.plugin.json
  • openclaw/package.json
  • openclaw/providers.ts
  • openclaw/recall.ts
  • openclaw/scripts/configure.py
  • openclaw/skill-loader.ts
  • openclaw/skills/memory-dream/SKILL.md
  • openclaw/skills/memory-triage/SKILL.md
  • openclaw/sqlite-resilience.test.ts
  • openclaw/telemetry.ts
  • openclaw/tests/cli-commands.test.ts
  • openclaw/tests/config-file.test.ts
  • openclaw/tests/config.test.ts
  • openclaw/tests/providers.test.ts
  • openclaw/tests/telemetry.test.ts
  • openclaw/tests/tools.test.ts
  • openclaw/tsup.config.ts
  • openclaw/types.ts
  • openclaw/vitest.config.ts

本文档介绍 @mem0/openclaw-mem0 插件,该插件为 OpenClaw 智能体提供长期记忆能力。该插件将 Mem0 的记忆层集成到 OpenClaw 的智能体框架中,支持跨对话会话的自动记忆捕获和召回。

该插件同时支持 Mem0 平台(云端)和开源(自托管)两种模式。它为智能体提供了八个专用工具、自动上下文注入(自动召回)以及自动事实提取(自动捕获)。

来源: openclaw/index.ts:1-17, openclaw/README.md:1-6, openclaw/package.json:2-5

---

架构总览

OpenClaw 插件充当 OpenClaw 智能体运行时与 Mem0 记忆后端之间的桥梁。它注册到 OpenClaw 的插件系统中,并提供自动记忆管理(通过生命周期钩子)以及智能体可以调用的显式记忆工具。

系统组件与数据流

下图将记忆管理的自然语言概念与 openclaw/ 目录中的具体代码实体联系起来。

graph TB
    subgraph "OpenClaw 运行时空间"
        Agent["智能体实例"]
        PluginRegistry["插件注册表"]
        Gateway["网关(解析 openclaw.json)"]
    end

    subgraph "代码实体空间(@mem0/openclaw-mem0)"
        Entry["definePluginEntry<br/>openclaw/index.ts:94"]
        Config["mem0ConfigSchema<br/>openclaw/config.ts:241"]
        ProviderFactory["createProvider()<br/>openclaw/providers.ts:257"]

        subgraph "记忆机制"
            Recall["recall()<br/>openclaw/recall.ts:14"]
            Triage["memory-triage 技能<br/>openclaw/skills/memory-triage/SKILL.md"]
            Dream["memory-dream 技能<br/>openclaw/skills/memory-dream/SKILL.md"]
            Filtering["filterMessagesForExtraction()<br/>openclaw/filtering.ts:148"]
        end

        subgraph "后端"
            Platform["PlatformBackend<br/>openclaw/backend/platform.ts"]
            OSS["providerToBackend<br/>openclaw/providers.ts:275"]
        end
    end

    subgraph "外部服务"
        Mem0Cloud["api.mem0.ai"]
        SQLite["本地 SQLite<br/>(~/.mem0/vector_store.db)"]
    end

    Gateway --> Config
    Config --> Entry
    Entry --> ProviderFactory
    ProviderFactory --> Platform
    ProviderFactory --> OSS

    Platform --> Mem0Cloud
    OSS --> SQLite

    Agent -- "触发" --> Recall
    Recall -- "查询" --> Platform
    Recall -- "查询" --> OSS

来源: openclaw/index.ts:94-110, openclaw/providers.ts:257-273, openclaw/README.md:179-189, openclaw/config.ts:241-260

---

提供者与后端系统

该插件使用双层抽象来封装部署模式:提供者(用于高层操作)和后端(用于底层 SDK 交互)。

提供者抽象

Mem0Provider 接口定义了记忆操作的契约,由 PlatformProviderOSSProvider 实现。

角色文件
PlatformProvider封装 MemoryClient 用于云端 API 交互。openclaw/providers.ts:78-210
OSSProvider封装 Memory 类用于本地嵌入向量和存储。openclaw/providers.ts:215-255
PlatformBackend平台模式下 Backend 接口的实现。openclaw/backend/platform.ts:1-20
实现逻辑

该插件根据 openclaw.json 中的 mode 字段初始化相应的后端。如果 mode"platform",则实例化 PlatformBackend。如果为 "open-source",则使用 providerToBackend 将 OSS 提供者适配为后端接口。

来源: openclaw/index.ts:161-170, openclaw/providers.ts:78-85, openclaw/types.ts:137-159

---

记忆工具

该插件注册了八个智能体可以调用的工具。这些工具在插件清单中声明,并在插件的 register 生命周期中注册。

classDiagram
    class ToolDeps {
        +Backend backend
        +Mem0Provider provider
        +Mem0Config config
    }

    class MemoryTools {
        +memory_search(query, scope, categories, filters)
        +memory_add(text, facts, category, importance)
        +memory_get(memoryId)
        +memory_list(scope, page_size)
        +memory_update(memoryId, text)
        +memory_delete(memoryId)
        +memory_event_list()
        +memory_event_status(event_id)
    }

    ToolDeps <.. MemoryTools : 使用
工具描述
工具名称用途实现文件
memory_search支持 scope(会话/长期)的语义搜索。openclaw/skills/memory-triage/SKILL.md:23-30
memory_add存储事实,支持分类和重要性。openclaw/skills/memory-triage/SKILL.md:32-40
memory_get按 ID 检索特定记忆。openclaw/skills/memory-triage/SKILL.md:42-44
memory_list列出记忆,支持分页和作用域。openclaw/skills/memory-triage/SKILL.md:46-49
memory_update原地更新现有记忆的文本内容。openclaw/skills/memory-triage/SKILL.md:51-54
memory_delete按 ID、查询或批量删除记忆。openclaw/skills/memory-triage/SKILL.md:56-62
memory_event_list列出最近的后台处理事件(平台模式)。openclaw/skills/memory-triage/SKILL.md:64-65
memory_event_status获取特定后台事件的状态(平台模式)。openclaw/skills/memory-triage/SKILL.md:67-69

来源: openclaw/openclaw.plugin.json:14-19, openclaw/index.ts:58-59, openclaw/types.ts:35-56

---

自动召回与自动捕获

该插件的核心功能依赖于两个在智能体回合生命周期中触发的自动机制。

自动召回(上下文注入)

在智能体响应之前,插件会搜索 Mem0 中的相关记忆并将其注入到上下文中。

  • 逻辑:使用用户的最新消息执行语义搜索。
  • 作用域:同时搜索全局 user_id 命名空间和特定的 run_id(会话)命名空间。
  • 清理:在搜索之前对查询进行清理以去除噪声。

来源: openclaw/index.ts:11-12, openclaw/recall.ts:14-40, openclaw/README.md:185-185

自动捕获(事实提取)

在智能体响应之后,插件从对话中提取持久化事实。

  • 噪声过滤:使用 filterMessagesForExtraction 去除通用的助手闲聊和瞬时状态。
  • 时间锚定:提取提示(通过 DEFAULT_CUSTOM_INSTRUCTIONS)要求为时间敏感的事实包含日期。
  • 去重:系统会检查是否存在相似的现有事实,如果找到匹配项,则执行 UPDATE 而不是新的 ADD

来源: openclaw/index.ts:12-13, openclaw/filtering.ts:148-160, openclaw/config.ts:28-115

---

按智能体隔离

该插件通过将记忆路由到特定智能体的命名空间,自动处理多智能体环境。这可以防止为同一用户工作的不同智能体之间发生"记忆泄漏"。

graph LR
    subgraph "输入上下文"
        BaseUser["用户:alice"]
        SessionKey["会话:agent:researcher:uuid"]
    end

    subgraph "隔离逻辑"
        EffectiveUser["effectiveUserId()<br/>openclaw/isolation.ts:47"]
        AgentUser["agentUserId()<br/>openclaw/isolation.ts:53"]
    end

    subgraph "命名空间存储"
        Namespace["alice:agent:researcher"]
    end

    BaseUser & SessionKey --> EffectiveUser
    EffectiveUser --> Namespace
  • 逻辑effectiveUserId 函数接收基础 userId(例如"alice")和 sessionKey
  • 模式:如果会话属于某个智能体(例如"researcher"),则用于 Mem0 调用的最终 userId 变为 alice:agent:researcher
  • 回退:单智能体设置或非智能体会话默认回退到基础 userId

来源: openclaw/isolation.ts:47-56, openclaw/index.ts:178-182, openclaw/README.md:196-198

---

配置与设置

部署模式

该插件支持在 openclaw.json 中配置的两种模式:

  1. 平台模式
  2. - 需要 apiKey。 - 使用 Mem0 云基础设施(api.mem0.ai)。 - 支持 customInstructionscustomCategories

  3. 开源模式
  4. - 自托管。 - 可配置的 llmembeddervectorStore 提供者(例如 OpenAI、Ollama、Qdrant、PGVector)。 - 默认在 SQLite 中本地存储数据。

来源: openclaw/config.ts:147-154, openclaw/types.ts:5-33, openclaw/README.md:69-73

CLI 命令

该插件向 OpenClaw CLI 添加了 mem0 命令,用于管理和调试。

命令描述
openclaw mem0 init用于配置平台或开源模式的引导向导。
openclaw mem0 login通过 OTP 向 Mem0 平台进行认证。
openclaw mem0 search执行手动语义搜索。
openclaw mem0 status检查连接和配置状态。

来源: openclaw/cli/commands.ts:1-30, openclaw/index.ts:15-16, openclaw/openclaw.plugin.json:8-13

---

记忆技能(智能体记忆)

skills 模式激活时,插件从自动后台处理切换到智能体控制的记忆管理。

记忆-triage

智能体会逐轮评估对话,基于四个关卡决定是否值得存储某个事实:未来效用新颖性事实性安全性(凭证扫描)。 来源: openclaw/skills/memory-triage/SKILL.md:73-96

记忆-dream

一种后台整合协议,会审查整个记忆存储,合并重复项、删除过期条目(TTL)并重写模糊的记忆以提高清晰度。 来源: openclaw/skills/memory-dream/SKILL.md:13-40

梦境关卡

dream-gate.ts 模块管理整合的触发器,在获取 dreamLock 以开始整合之前,会检查"廉价关卡"(如会话计数)和"记忆关卡"(总记忆量)。 来源: openclaw/dream-gate.ts:48-53, openclaw/index.ts:47-53