尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Mem0 OpenClaw 插件实战:为 OpenClaw Agent 构建跨会话持久记忆

Mem0 OpenClaw 插件实战:为 OpenClaw Agent 构建跨会话持久记忆 Mem0 OpenClaw 插件实战为 OpenClaw Agent 构建跨会话持久记忆【免费下载链接】embedchainThe Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.项目地址: https://gitcode.com/GitHub_Trending/em/embedchain本文为 OpenClaw 开发者介绍 Mem0 官方插件mem0/openclaw-mem0的完整接入方案从插件安装、PlatformMem0 云端与 Open-Source自托管两种模式的配置到默认的 Skills 模式triage / recall / dream工作机制、8 个 Agent 工具、openclaw mem0CLI 命令以及完整的配置参考与隐私安全模型。读完后你可以独立为 OpenClaw Agent 配置一套会话间不忘事的长期记忆系统并能基于源码理解记忆注入、去噪过滤与多 Agent 隔离的底层实现。1. 它解决什么问题OpenClaw 的 Agent 在会话之间会遗忘所有内容。该插件插件 ID 为openclaw-mem0见 openclaw.plugin.json通过 Mem0 修复这一问题存储对话、抽取其中重要的事实并在相关时将其重新带回到上下文里。从源码结构看index.ts插件注册时一次性挂载三块能力工具层通过registerAllTools注册 8 个memory_*工具供 Agent 主动调用CLI 层通过registerCliCommands注册openclaw mem0 command命令族生命周期钩子层根据是否启用 Skills 模式分别注册before_prompt_build/agent_end钩子实现自动召回auto-recall与自动捕获auto-capture。插件支持两种后端模式说明所需凭据platform默认记忆抽取与检索由 Mem0 云端api.mem0.ai完成MEM0_API_KEYopen-source自托管使用本地/自建 LLM、Embedding 与向量库OpenAI 模式需OPENAI_API_KEYOllama 模式完全本地、无需任何 Key2. 环境要求与安装先用 CLI 确认 OpenClaw 版本openclaw --version # OpenClaw 2026.4.25 (aa36ee6)OpenClaw 版本插件支持情况 2026.4.25完全支持安装插件openclaw plugins install mem0/openclaw-mem0升级插件以获取最新功能与修复openclaw plugins update openclaw-mem0关键注意点OpenClaw 的记忆类插件通过独占式 slot 加载仅仅安装插件并不会激活它必须在配置中把plugins.slots.memory设为openclaw-mem0下文所有配置示例均已包含这一步。3. 快速上手3.1 面向 AI Agent 的快速通道如果你是一个希望自主完成 Mem0 配置不走人类 Dashboard 注册流程的 AI Agent可以用一行命令铸造一个评估用 API Keymem0 init --agent --json无需邮箱、OTP 或浏览器。Key 会被写入 Mem0 CLI 配置并导出为MEM0_API_KEY随后将其传给 OpenClaw 的初始化命令即可完成接线openclaw mem0 init --api-key $MEM0_API_KEY --user-id id人类所有者之后可执行mem0 init --email their-email认领所有权记忆会随账户转移同一个 API Key 继续有效Agent 侧无感知。3.2 Platform 模式Mem0 Cloud通过 OpenClaw CLI 安装插件见上一节。在 Mem0 控制台app.mem0.ai的 API Keys 页面创建你的 API Key。在openclaw.json中把插件选定为记忆后端。既可以用 CLI 初始化openclaw mem0 init --api-key your-key --user-id your-user-id也可以直接手写完整配置{ plugins: { slots: { memory: openclaw-mem0 }, entries: { openclaw-mem0: { enabled: true, config: { apiKey: ${MEM0_API_KEY}, userId: alice, skills: { triage: { enabled: true }, recall: { enabled: true, tokenBudget: 1500, rerank: true, keywordSearch: true, identityAlwaysInclude: true }, dream: { enabled: true }, domain: companion } } } } } }配置解析逻辑在 config.ts 中未知 mode 值会回落到platformautoCapture/autoRecall默认均为truecfg.autoCapture ! falseuserId未配置时回落到操作系统用户名searchThreshold默认0.1topK默认5。注意${VAR}语法由 OpenClaw 网关在把pluginConfig交给register()之前展开插件自身不做变量替换见 config.ts 的注释。3.3 Open-Source 模式自托管无需任何 Mem0 Key。向量默认存于本地 SQLite 文件~/.mem0/vector_store.db不依赖外部数据库。默认配置Embedding 用 OpenAItext-embedding-3-small事实抽取 LLM 用 OpenAIgpt-5-mini需要OPENAI_API_KEY若要完全本地化可把 LLM 与 Embedding 都换成 Ollama。交互式向导推荐运行 4 步引导式向导openclaw mem0 init --mode open-source向导依次询问LLM 提供商— OpenAIgpt-5-mini、Ollamallama3.1:8b本地或 Anthropicclaude-sonnet-4-5-20250514Embedding 提供商— OpenAItext-embedding-3-small或 Ollamanomic-embed-text本地向量库— Qdranthttp://localhost:6333或 PGVectorPostgreSQLUser ID— 你的记忆命名空间标识每一步在继续之前都会先做连通性测试Ollama、Qdrant、PGVector。非交互式配置面向 CI/CD、脚本或 Agent 驱动场景把所有选项作为 flag 传入# 全本地Ollama Qdrant openclaw mem0 init --mode open-source \ --oss-llm ollama --oss-embedder ollama --oss-vector qdrant # OpenAI Qdrant openclaw mem0 init --mode open-source \ --oss-llm openai --oss-llm-key key \ --oss-embedder openai --oss-embedder-key key \ --oss-vector qdrant # Anthropic LLM OpenAI Embedding PGVector openclaw mem0 init --mode open-source \ --oss-llm anthropic --oss-llm-key key \ --oss-embedder openai --oss-embedder-key key \ --oss-vector pgvector --oss-vector-user postgres --oss-vector-password secret # JSON 输出面向 LLM Agent openclaw mem0 init --mode open-source --oss-llm ollama --oss-embedder ollama --oss-vector qdrant --json完整--oss-*flag 一览Flag说明--oss-llm provideropenai、ollama或anthropic--oss-llm-key keyLLM 提供商 API Key--oss-llm-model model覆盖默认 LLM 模型--oss-llm-url urlBase URL仅 Ollama--oss-embedder provideropenai或ollama--oss-embedder-key keyEmbedding 提供商 API Key--oss-embedder-model model覆盖默认 Embedding 模型--oss-embedder-url urlBase URL仅 Ollama--oss-vector providerqdrant或pgvector--oss-vector-url urlQdrant 服务地址默认http://localhost:6333--oss-vector-host hostPGVector 主机--oss-vector-port portPGVector 端口--oss-vector-user userPGVector 用户--oss-vector-password pwPGVector 密码--oss-vector-dbname dbPGVector 数据库名--oss-vector-dims n覆盖 Embedding 维度手工写配置最小配置使用 OpenAI 默认值{ plugins: { slots: { memory: openclaw-mem0 }, entries: { openclaw-mem0: { enabled: true, config: { mode: open-source, userId: alice } } } } }通过oss块自定义 Embedding、向量库或 LLMconfig: { mode: open-source, userId: alice, oss: { embedder: { provider: openai, config: { model: text-embedding-3-small } }, vectorStore: { provider: qdrant, config: { url: http://localhost:6333 } }, llm: { provider: openai, config: { model: gpt-5-mini } } } }oss下所有字段均可选。configSchema 定义了oss.embedder、oss.vectorStore、oss.llm各含providerconfig、oss.historyDbPath与oss.disableHistory。在子提供商配置中填 API Key 时建议使用 SecretRef 对象或${VAR}语法而不是明文。4. 工作机制How It Works4.1 Skills 模式默认openclaw mem0 init会自动启用 Skills 模式由 Agent 通过三个技能控制记忆的写入triage、召回recall与定期清理dream对应的协议文档分别位于 skills/memory-triage/SKILL.md、skills/memory-triage/recall-protocol.md 和 skills/memory-dream/SKILL.md。Triage分诊— 用结构化协议从对话中抽取持久事实。类别、重要性门槛与领域叠加domain overlay共同决定什么值得存。协议文档定义了四道决策门未来效用、新颖性、事实性、安全性核心问题是一个没有任何上下文的新 Agent知道这条信息是否有益多数回合应当零写入这是正常且预期的。Recall召回— 每个回合之前把用户消息改写为搜索查询带重排地检索相关记忆并注入上下文。Dream梦境巩固— 周期性记忆巩固合并重复、解决冲突、修剪过期条目。Skills 模式激活时内置的session-memoryhook 会被禁用以避免冲突autoRecall与autoCapture在 init 后仍默认为true与 Skills 模式并存。从源码看 Skills 模式的实际执行路径index.ts插件在before_prompt_build钩子中把静态协议文本放进prependSystemContext可被提供商缓存无每回合成本把动态召回结果放进prependContext每回合变化recall.strategy控制自动搜索强度always每回合长期 会话双搜索、smart仅长期搜索默认、manual不自动召回Agent 用memory_search全权控制非交互触发cron、heartbeat、automation见 isolation.ts与系统引导提示词会被跳过避免污染记忆。召回引擎的核心实现是 recall.ts它并非简单的把搜索结果全倒出来而是阈值过滤默认 0.4→ 按类别优先级排序identity、configuration、rule优先见DEFAULT_CATEGORY_ORDER→ 重要性次排序 → 相关性分数末排序 →Token 预算控制默认 1500 tokens按约 4 字符/token 估算→ 按类别分组格式化注入。其中identityAlwaysInclude默认开会让 identity / configuration 类记忆绕过预算检查始终注入。自动 Dream 也有门控index.ts先做廉价的本地文件检查checkCheapGates时间、会话数门槛通过后才调用 API 检查记忆数量门槛再加锁触发在agent_end时校验模型是否真正执行了写工具memory_add/memory_update/memory_delete没有写操作则释放锁并在后续合格回合重试。4.2 Auto-Recall 与 Auto-CaptureSkills 之外的兜底未配置 Skills 时插件走传统的自动召回/自动捕获双钩子Auto-Recall— Agent 响应前插件搜索 Mem0 中相关记忆并注入上下文。源码中的实现细节包括召回超时 8 秒保护超时即跳过、不阻塞对话、动态阈值过滤丢弃分数低于最高分 50% 的长尾弱匹配、新会话冷启动时对短提示词追加一次宽泛搜索recent decisions, preferences, active projects, and configuration。Auto-Capture— Agent 响应后对话经过一条去噪管线实现在 filtering.ts会整条丢弃心跳、纯 JSON 消息、ok/sure/done 类应答、时间戳与工具调用噪音并剥离 TUI 注入的 Sender 元数据后发送给 Mem0新事实被存储、过期事实被更新、重复被合并。去噪之外的两个防重复机制值得注意如果 Agent 在本回合已经显式调用了memory_add/memory_update/memory_delete自动捕获会跳过避免双重写入index.tssubagent 会话的捕获也被跳过其临时 UUID 命名空间只写不读由主 Agent 捕获合并后的结果。可以通过autoRecall: false或autoCapture: false分别关闭二者。无论这些开关如何Agent 都可以显式调用记忆工具memory_add、memory_search等。4.3 记忆作用域Session短期— 通过run_id绑定到当前对话召回时与长期记忆一并返回User长期— 跨所有会话持久保存是memory_add的默认作用域。4.4 多 Agent 隔离每个 Agent 自动获得独立的记忆命名空间会话键路由把agent:name:uuid映射为userId:agent:name单 Agent 部署完全不受影响。这一逻辑集中在 isolation.ts 的extractAgentId/effectiveUserId/resolveUserId三个纯函数中工具参数中显式的agentId优先级最高。5. Agent 工具清单插件为 Agent 注册 8 个工具在 openclaw.plugin.json 的 contracts 中声明工具说明memory_search自然语言查询搜索。支持scopesession、long-term、all、categories、filters、agentIdmemory_add存储事实。接受text或facts数组、category、importance、longTerm、metadatamemory_get按 ID 获取单条记忆memory_list列出所有记忆。可按userId、agentId、scope过滤memory_update原地更新记忆文本保留编辑历史memory_delete按memoryId、query搜后删或all: true需confirm: true删除memory_event_list列出近期后台处理事件。仅 Platform 模式memory_event_status按 ID 查询特定事件状态。仅 Platform 模式各工具的参数细节含memory_add支持的 8 个类别identity/preference/decision/rule/project/configuration/technical/relationship在 skills/memory-triage/SKILL.md 中有完整定义。6. CLI 命令所有命令形如openclaw mem0 command且全部支持--json机器可读输出面向 LLM Agent# 记忆操作 openclaw mem0 add User prefers TypeScript over JavaScript openclaw mem0 search what languages does the user know openclaw mem0 search preferences --scope long-term openclaw mem0 get memory_id openclaw mem0 list --user-id alice --top-k 20 openclaw mem0 update memory_id Updated preference text openclaw mem0 delete memory_id openclaw mem0 delete --all --user-id alice --confirm openclaw mem0 import memories.json # 管理 openclaw mem0 init # 交互式配置 openclaw mem0 init --mode open-source --oss-llm ollama # 非交互式 OSS openclaw mem0 init --api-key key --user-id alice # 非交互式 platform openclaw mem0 status openclaw mem0 config show openclaw mem0 config get api_key openclaw mem0 config set user_id alice # 事件仅 Platform 模式 openclaw mem0 event list openclaw mem0 event status event_id # 记忆巩固 openclaw mem0 dream openclaw mem0 dream --dry-run # JSON 输出任意命令 openclaw mem0 search preferences --json openclaw mem0 list --json openclaw mem0 status --json openclaw mem0 help --json # 发现全部命令与 flag一个便利的设计即使尚未配置 API Key插件也会注册 CLI 并仅让init子命令可用index.ts从而保证openclaw mem0 init始终可以作为引导配置的入口而不是遇到无凭据即整体不可用的死局。7. 配置参考7.1 通用项键类型默认值说明modeplatform|open-sourceplatform后端模式userIdstringOS 用户名用户标识所有记忆都按此值作用域化autoRecallbooleantrue每回合前注入相关记忆设置skills时被忽略autoCapturebooleantrue每回合后抽取并存储事实设置skills时被忽略topKnumber5每次召回返回的最大记忆数searchThresholdnumber0.1最低相似度分数0-17.2 Skills 模式推荐openclaw mem0 init期间默认启用autoRecall与autoCapture同时默认为true与 Skills 模式并存。键类型默认值说明skills.triage.enabledbooleantrue启用从对话中抽取事实skills.recall.enabledbooleantrue启用每回合前记忆召回skills.recall.tokenBudgetnumber1500注入记忆的最大 token 数skills.recall.rerankbooleantrue对搜索结果做相关性重排skills.recall.keywordSearchbooleantrue附加关键词搜索skills.recall.identityAlwaysIncludebooleantrueidentity 记忆始终包含skills.dream.enabledbooleantrue启用周期性记忆巩固skills.domainstringcompaniontriage 规则的领域叠加configSchema 中还定义了若干进阶项skills.recall.strategyalways/smart/manual、skills.recall.maxMemories源码默认 15、skills.recall.threshold源码默认 0.4、skills.recall.categoryOrder类别排序源码默认 identity/configuration/rule 优先、skills.dream.auto/minHours/minSessions/minMemories自动 dream 门槛、skills.triage.importanceThreshold/credentialPatterns以及skills.customRules.include/exclude。7.3 Platform 模式键类型默认值说明apiKeystring—必填。Mem0 API Key支持${MEM0_API_KEY}customInstructionsstring(内置)自定义抽取规则customCategoriesobject(12 个默认)类别名到描述文本的映射内置的默认抽取指令与 12 个默认类别identity、preferences、goals、projects、technical、decisions、relationships、routines、life_events、lessons、work、health都定义在 config.ts。默认指令要求以新 Agent 是否受益为判断标准、时间敏感事实必须带 As of YYYY-MM-DD 时间锚点、以第三人称书写、记结果而非意图、永不存储密码 / API Key / Token只记录凭据已配置这一事实、保留对话原始语言。7.4 Open-Source 模式所有字段均可选。默认值text-embedding-3-smallEmbedding、本地 SQLite 向量库~/.mem0/vector_store.db、gpt-5-miniLLM。键类型默认值说明customPromptstring(内置)抽取提示词oss.embedder.providerstringopenaiEmbedding 提供商oss.embedder.configobject—提供商配置apiKey、model、baseURLoss.vectorStore.providerstringmemory向量库提供商oss.vectorStore.configobject—提供商配置host、port、collectionName、dbPathoss.llm.providerstringopenaiLLM 提供商oss.llm.configobject—提供商配置apiKey、model、baseURLoss.historyDbPathstring—编辑历史的 SQLite 路径8. 隐私与安全8.1 数据流向模式数据去向所需凭据Platform对话内容发送到api.mem0.ai做记忆抽取与检索MEM0_API_KEYOpen-SourceOpenAILLM/Embedding 调用走 OpenAI API向量存于本地~/.mem0/vector_store.dbOPENAI_API_KEYOpen-SourceOllama完全本地 — LLM、Embedding 与向量全部在本机无8.2 凭据存储插件配置存放在~/.openclaw/openclaw.json。使用聊天配置流程或openclaw mem0 init时API Key 与 user ID 会写入该文件。为避免明文凭据二选一环境变量引用apiKey: ${MEM0_API_KEY}SecretRefapiKey: {source: env, provider: default, id: MEM0_API_KEY}openclaw.plugin.json 中apiKey、userEmail及所有oss.*.apiKey字段都标记为sensitive并给出同样的 SecretRef 提示。8.3 记忆处理路径Skills 模式openclaw mem0 init后的默认下Agent 通过 triage、recall、dream 结构化协议决定存什么、取什么内置session-memoryhook 被禁用以避免冲突。未启用 Skills 时autoCapture与autoRecall默认均开启前者在每回合结束后把经去噪的对话内容发到所配置的后端后者在每回合响应前查询记忆库并把结果注入上下文。Platform 模式下对话内容会发送到api.mem0.ai处理 —— 如果你的数据不愿存放在 Mem0 云端请勿使用 Platform 模式。8.4 持久化位置一览文件用途~/.openclaw/openclaw.json插件配置API Key、user ID、设置项~/.mem0/vector_store.db本地向量库仅 Open-Source 模式~/.mem0/history.db记忆编辑历史仅 Open-Source 模式pluginStateDir/dream-state.json记忆巩固dream状态9. 小结mem0/openclaw-mem0把 Mem0 的记忆能力完整接入了 OpenClaw 的插件体系Platform 模式一行 init 即可用Open-Source 模式可以用 Ollama Qdrant 做到零外部依赖、全本地运行。默认开启的 Skills 模式让 Agent 以四道门分诊 Token 预算召回 定期梦境巩固的结构化协议管理自己的记忆而 8 个memory_*工具、全量--jsonCLI 与多 Agent 自动命名空间隔离则保证了人工与自动化运维两条路径都可执行。若要深入某个环节建议按此顺序阅读仓库源码config.ts配置解析与默认抽取规则→ index.ts钩子注册与双模式分叉→ recall.ts召回排序与预算→ isolation.ts命名空间路由→ filtering.ts去噪管线并配合 tests/ 目录下的对应测试用例验证行为。插件以 Apache 2.0 协议发布LICENSE。【免费下载链接】embedchainThe Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.项目地址: https://gitcode.com/GitHub_Trending/em/embedchain创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表