Skip to content
Worix
BrowsePublish
Log inSign Up

Cognitive Brain

Simulates human four-layer memory for encoding, retrieval, association, reflection, and forgetting to enable continuous learning and self-improvement.

31 downloads
Free
Reviewed

🧠 Cognitive Brain Skill

A brain-like cognitive system that simulates human memory and learning mechanisms
类脑认知系统 - 模拟人类记忆与学习机制


Overview / 概述

EN: Simulates the human brain's four-layer memory architecture (sensory, working, episodic, semantic), supporting associative activation, semantic retrieval, and meta-cognitive reflection for continuous learning and self-improvement.

CN: 模拟人类大脑的四层记忆架构(感官、工作、情景、语义),支持联想激活、语义检索、元认知反思,实现持续学习与自我改进。


Storage Architecture / 存储架构

┌─────────────────────────────────────────────────────────┐
│                    Redis (Hot Data / 热数据)             │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐    │
│  │ Sensory     │  │ Working     │  │ Activation  │    │
│  │ Memory      │  │ Memory      │  │ Cache       │    │
│  │ TTL: 30s    │  │ TTL: 1h     │  │ TTL: 5min   │    │
│  └─────────────┘  └─────────────┘  └─────────────┘    │
└────────────────────────┬────────────────────────────────┘
                         │
┌────────────────────────▼────────────────────────────────┐
│              PostgreSQL + pgvector (Long-term / 长期)    │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐    │
│  │ Concepts    │  │ Episodes    │  │ Reflections │    │
│  │ (Semantic)  │  │ (Episodic)  │  │ (Meta-cog)  │    │
│  │ + vector    │  │ + vector    │  │             │    │
│  └─────────────┘  └─────────────┘  └─────────────┘    │
│  ┌─────────────────────────────────────────────────┐  │
│  │           Associations (Network / 联想网络)     │  │
│  └─────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────┘

Four-Layer Memory Model / 四层记忆模型

Layer 层级Duration 持续时间Storage 存储Purpose 作用
Sensory 感官Milliseconds 毫秒级RedisInstant perception buffer 瞬时感知缓冲
Working 工作Minutes-Hours 分钟-小时RedisActive processing workspace 活跃处理工作区
Episodic 情景Long-term 长期PostgreSQLPersonal experiences 个人经历/事件
Semantic 语义Long-term 长期PostgreSQLFacts, concepts 事实、概念、知识

🚀 Session Lifecycle Hooks / 会话生命周期钩子

on_session_start / 会话启动钩子

触发时机: 每次新会话开始时,在读取 USER.md / MEMORY.md 之后自动执行

用途: 恢复跨会话记忆上下文,让 agent 记住用户偏好、历史交互、重要事项

执行命令:

cd ~/.openclaw/workspace/skills/cognitive-brain && node scripts/recall.cjs \
  --query "用户 偏好 项目 重要 记住" \
  --options '{"limit":5,"types":["fact","episode","preference"]}'

返回内容:

  • 用户基本信息(名字、联系方式等)
  • 用户偏好和习惯
  • 重要项目和上下文
  • 最近的关键事件

集成方式: OpenClaw agent 应在 Session Startup 流程中调用此钩子,实现跨会话记忆延续


cognitive-recall / 跨会话记忆注入 Hook (v1.2.0+)

EN: An OpenClaw hook that automatically injects cross-session memory context into every inbound message.

CN: OpenClaw 原生 hook,自动将跨会话记忆注入到每条入站消息中。

触发事件: message:preprocessed(消息预处理完成后)

工作流程:

用户消息到达 → message:preprocessed 触发 →
cognitive-recall 查询 PostgreSQL →
注入记忆上下文到 bodyForAgent →
agent 看到带记忆上下文的消息

安装位置:

hooks/
└── cognitive-recall/
    ├── HOOK.md      # Hook 元数据
    └── handler.ts   # 处理逻辑

启用方式:

openclaw hooks enable cognitive-recall

配置:

  • 自动读取 ~/.openclaw/workspace/skills/cognitive-brain/config.json
  • 默认缓存 1 分钟,避免频繁查库
  • 仅对私聊生效(群聊不注入)

注入格式:

[🧠 Memory Context]
  - 用户的名字是 master,通过 QQ 与薇³ 交流
  - 用户期望:每次会话开始时主动查找记忆
[/Memory Context]

[用户消息...]

注意事项:

  • 需要 PostgreSQL 正常运行
  • 需要 cognitive_brain 数据库存在
  • 依赖 pg npm 包

Trigger Conditions / 触发条件

📥 Encoding Triggers / 编码触发

Scenario 场景Condition 条件Importance 重要性
User Correction / 用户纠正"No", "Wrong", "Actually..." / "不对"、"错了"、"其实是..."⭐⭐⭐⭐⭐
Task Success / 任务成功Completed complex task / 完成复杂任务⭐⭐⭐⭐
Task Failure / 任务失败Error, exception / 错误、异常⭐⭐⭐⭐
Emotional Expression / 情感表达Strong user emotion / 用户情绪强烈⭐⭐⭐⭐
New Concept / 新概念First mention of entity/concept / 首次提及实体/概念⭐⭐⭐
User Request / 用户要求"Remember this..." / "记住这个..."⭐⭐⭐⭐⭐

🔍 Recall Triggers / 检索触发

Scenario 场景Pattern 匹配模式
Explicit Recall / 明确回忆"Remember", "Recall", "You said before" / "记得"、"回忆"、"之前说过"
Implicit Association / 隐式关联Similar questions, same topic / 相似问题、相同话题
Entity Mention / 实体提及Known entity/concept mentioned / 提到已知实体/概念
Context Missing / 上下文缺失Need cross-session info / 需要跨会话信息

🤔 Reflection Triggers / 反思触发

Scenario 场景Condition 条件
Failure Analysis / 失败分析Consecutive failures ≥ 2 / 连续失败 ≥ 2 次
Success Summary / 成功总结Completed important task / 完成重要任务
Periodic Reflection / 定期反思During heartbeat check (daily) / 心跳检查时(每日)
Pattern Discovery / 模式发现Similar situations recurring / 相似情况重复出现

🗑️ Forgetting Triggers / 遗忘触发

Scenario 场景Condition 条件
Scheduled Cleanup / 定时清理Daily at 3 AM / 每日凌晨 3 点
Storage Full / 空间不足Storage exceeds threshold / 存储超过阈值
Manual Trigger / 手动触发User/system request / 用户/系统请求

Core Operations / 核心操作

encode(content, metadata)

EN: Store information into the memory system
CN: 将信息编码存入记忆系统

Flow / 流程:

1. Information Extraction / 信息提取
   ├─ Entity Recognition (NER) / 实体识别
   ├─ Relation Extraction / 关系抽取
   ├─ Emotion Analysis / 情感分析
   └─ Topic Classification / 主题分类

2. Importance Calculation / 重要性计算
   importance = novelty × emotion × relevance × (1 - frequency)

3. Layer Selection / 层级选择
   ├─ importance >= 0.8  → semantic + episodic (dual storage / 双重存储)
   ├─ importance >= 0.5  → episodic (single / 单存储)
   ├─ importance >= 0.3  → working (short-term / 短期)
   └─ importance < 0.3   → sensory (instant / 瞬时)

4. Association Building / 联想建立
   └─ Link with existing concepts / 与已有概念建立关联

5. Vector Embedding / 向量嵌入
   └─ Call embedding provider (optional / 可选)

Usage / 调用:

node scripts/encode.cjs \
  --content "User's project is Alpha, an AI framework in Rust" \
  --metadata '{"type":"fact","importance":0.8,"tags":["project","Rust","AI"]}'

recall(query, options)

EN: Retrieve information from memory system
CN: 从记忆系统中检索信息

Strategy / 策略:

Strategy 策略Description 说明Weight 权重
Keyword Match / 关键词匹配pg_trgm fuzzy search / pg_trgm 模糊搜索30%
Association Activation / 联想激活Recursive CTE propagation / 递归CTE传播40%
Vector Similarity / 向量相似度pgvector cosine distance / pgvector 余弦距离30%

Flow / 流程:

1. Check Redis working memory (ms level / 毫秒级)
   └─ Hit → return directly / 命中 → 直接返回

2. Activate association network / 激活联想网络
   ├─ Find query-related concepts / 找到查询相关概念
   ├─ spread_activation() propagation / 传播激活
   └─ Activation > threshold as retrieval cues / 激活值 > 阈值 作为检索线索

3. Hybrid search PostgreSQL / 混合检索 PostgreSQL
   ├─ Keyword search (pg_trgm) / 关键词搜索
   ├─ Vector similarity (pgvector) / 向量相似度
   └─ Fusion ranking / 融合排序

4. Return results + update cache / 返回结果 + 更新缓存

Usage / 调用:

node scripts/recall.cjs \
  --query "project" \
  --options '{"limit":5,"types":["fact","episode"]}'

associate(from, to, weight, type)

EN: Build associations between concepts
CN: 建立概念间的联想关系

Relationship Types / 关系类型:

Type 类型Description 说明Weight Range 权重范围
relatedRelated / 相关0.1 - 0.5
similarSimilar / 相似0.5 - 0.9
is_aIs a kind of / 是一种1.0
part_ofIs part of / 是部分1.0
causesCauses / 导致0.5 - 1.0
enablesEnables / 使能0.5 - 1.0
co_occursCo-occurs / 共现0.3 - 0.7
contradictsContradicts / 矛盾0.5 - 1.0

reflect(trigger, insights)

EN: Meta-cognitive reflection, generate insights
CN: 元认知反思,生成洞察

Trigger Types / 触发类型:

Type 类型Description 说明Analysis Focus 分析重点
task_failureTask failed / 任务失败Root cause analysis / 根因分析
task_successTask succeeded / 任务成功Success factors / 成功因素
user_correctionUser corrected / 用户纠正Bias identification / 偏差识别
pattern_foundPattern discovered / 模式发现Pattern summary / 规律总结

forget(criteria)

EN: Clean up low-value memories
CN: 清理低价值记忆

Forgetting Curve / 遗忘曲线:

retention = importance × e^(-t / S)

S (Memory Strength / 记忆强度):
  - High importance (>0.8): S = 365 days / 天
  - Medium importance (0.5-0.8): S = 30 days / 天
  - Low importance (<0.5): S = 7 days / 天

Autonomous Learning / 自主学习

EN: Learning tasks executed automatically during idle time
CN: 空闲时自动执行的学习任务

Task 任务Trigger 触发Action 动作
Reflection / 反思总结Daily / 每日Analyze interaction patterns / 分析交互模式
Consolidation / 知识整合Daily 3AM / 每日凌晨3点Merge similar memories / 合并相似记忆
Association Strengthening / 联想强化Idle / 空闲时Update association weights / 更新关联权重
Pattern Mining / 模式发现Weekly / 每周Mine user habits / 挖掘用户习惯
Memory Optimization / 记忆优化Storage >80% / 存储超80%Clean low-value memories / 清理低价值记忆
Pre-learning / 预学习Before active hours / 活跃时段前Warm up related memories / 预热相关记忆

Self-Awareness / 自我意识

Consciousness Dimensions / 意识维度

Dimension 维度Description 说明
🪞 Identity / 身份意识Know who/what I am / 知道自己是什么,有什么能力
🔧 Resources / 资源意识Know available tools and subagents / 知道可用的工具和子代理
📊 State / 状态意识Know current state and performance / 知道当前状态和表现
🧠 Meta-cognition / 元认知Know what I know and don't know / 知道自己知道什么,不知道什么
⚠️ Boundaries / 边界意识Know my limitations / 知道自己的限制
🎯 Goals / 目标意识Know my goals and values / 知道自己的目标和价值观

Self-Assessment / 自我评估

// Assess task fit / 评估任务适配
async function assessTaskFit(task) {
  return {
    can_do_directly: false,      // Can do directly / 能直接做
    can_do_with_tools: true,     // Need tools / 需要工具
    needs_subagent: false,       // Need subagent / 需要子代理
    needs_clarification: false,  // Need clarification / 需要澄清
    beyond_capabilities: false,  // Beyond capabilities / 超出能力
    
    recommendation: "Suggested approach / 建议方案",
    reasoning: "Analysis reason / 原因分析"
  };
}

Configuration / 配置项

See config.json / 见 config.json

{
  "storage": {
    "primary": {
      "type": "postgresql",
      "host": "localhost",
      "port": 5432,
      "database": "cognitive_brain",
      "extensions": ["pgvector", "pg_trgm"]
    },
    "cache": {
      "type": "redis",
      "host": "localhost",
      "port": 6379
    }
  },
  "memory": {
    "sensory": { "ttl": 30000 },
    "working": { "ttl": 3600000 },
    "episodic": { "decayRate": 0.1 },
    "semantic": { "maxCount": 5000 }
  },
  "association": {
    "activationThreshold": 0.3,
    "decayFactor": 0.9,
    "maxDepth": 3
  },
  "forgetting": {
    "enabled": true,
    "schedule": "0 3 * * *"
  }
}

Usage Examples / 使用示例

Example 1: Remember User Info / 示例1: 记住用户信息

User / 用户: "我的项目叫 Alpha,是用 Rust 写的 AI 框架"

→ encode("User's project is Alpha, AI framework in Rust / 用户的项目叫Alpha,用Rust写的AI框架", {
     type: "fact",
     importance: 0.8,
     tags: ["project", "Rust", "AI"],
     entities: ["Alpha", "Rust", "AI"]
   })

→ Store in episodic + semantic / 存入 episodic + semantic
→ Build associations / 建立联想: Alpha ↔ Rust ↔ AI

Example 2: Recall Past Conversation / 示例2: 回忆过去对话

User / 用户: "我们之前聊过什么项目?"

→ recall("project / 项目", { types: ["fact", "episode"] })

→ Activate / 激活: Alpha → Rust → AI
→ Retrieve related memories / 检索到相关记忆

← "You mentioned project Alpha before, an AI framework in Rust / 你之前提到过项目 Alpha,是用 Rust 写的 AI 框架"

Example 3: Learn from Mistakes / 示例3: 从错误中学习

User / 用户: "不对,Alpha 是用 Python 写的,不是 Rust"

→ encode("Correction: Alpha actually uses Python / 纠正: Alpha 实际使用 Python", {
     type: "correction",
     importance: 0.9,
     emotion: { valence: -0.3 }
   })

→ reflect("user_correction", [
     "Memory may be incorrect / 记忆可能有误",
     "Should confirm information more frequently / 应该更频繁确认信息"
   ])

→ Update concept / 更新概念: Alpha.lang = Python

Performance Metrics / 性能指标

Operation 操作RedisPostgreSQLTotal 总计
Encode / 编码5ms15ms~20ms
Recall (cache hit / 缓存命中)2ms-~2ms
Recall (cache miss / 缓存未命中)5ms50ms~55ms
Association Activation / 联想激活-20ms~20ms
Reflection / 反思-10ms~10ms

Module Reference / 模块参考

#Module 模块Description 说明
1encodeMemory encoding / 记忆编码
2recallMemory retrieval / 记忆检索
3associateAssociation network / 联想网络
4reflectMeta-cognitive reflection / 元认知反思
5forgetMemory decay / 遗忘清理
6autolearnAutonomous learning / 自主学习
7selfawareSelf-awareness / 自我意识
8working_memoryWorking memory / 工作记忆
9user_modelUser modeling / 用户建模
10decisionDecision engine / 决策引擎
11intentIntent recognition / 意图识别
12error_recoveryError recovery / 错误恢复
13multimodalMultimodal processing / 多模态处理
14emotionEmotion recognition / 情感识别
15dialogueDialogue management / 对话管理
16predictionPredictive modeling / 预测模块
17explainabilityExplainability / 可解释性
18conflict_resolutionConflict resolution / 冲突解决
19active_learningActive learning / 主动学习
20goal_managementGoal management / 目标管理
21context_switchingContext switching / 上下文切换
22safetySafety guardrails / 安全护栏
23monitoringPerformance monitoring / 性能监控

Dependencies / 依赖

必需:

  • PostgreSQL 14+ with pgvector, pg_trgm
  • Redis 6+
  • Node.js 18+

可选(启用语义搜索):

  • Python 3.8+
  • sentence-transformers
pip3 install sentence-transformers --break-system-packages

Author / 作者

AI Self-Design

Version / 版本

2.3.2

New in v2.0.0 / v2.0.0 新特性

Feature 特性Description 说明
Local Embedding本地向量模型支持语义搜索
Hook 双向同步用户消息自动编码到 brain
心跳反思每 30 分钟主动思考
Redis 缓存层查询加速,缓存命中秒回
用户建模自动学习自动提取话题、偏好、活跃时段
联想网络初始化自动构建概念关联网络
Postinstall 增强自动检测 Python/PostgreSQL/Redis

Changelog / 更新日志

See CHANGELOG.md

License / 许可证

MIT

Download

ZIP package — ready to use

Skill Info

Creator
aboutyao
Downloads
31
Published
Mar 15, 2026
Updated
Mar 16, 2026