← Back to Skills Marketplace
aboutyao

Cognitive Brain

by aboutyao · GitHub ↗ · v7.0.1 · MIT-0
cross-platform ⚠ suspicious
849
Downloads
0
Stars
2
Active Installs
79
Versions
Install in OpenClaw
/install cognitive-brain
Description
Provides a cross-session AI memory and cognition system with four-layer memory, real-time sync, free thinking, intelligent prediction, and knowledge visualiz...
README (SKILL.md)

🧠 Cognitive Brain

跨会话记忆与认知系统 v5.0 | Cross-Session Memory & Cognition System

让 AI 拥有像人类一样的记忆、思考和预测能力

Version: 7.0.1 | License: MIT


✨ 功能特性 | Features

功能 Feature 描述
🔄 实时共享 Real-time Sharing 跨会话毫秒级记忆同步
🧠 四层记忆 Four-Layer Memory 感官/工作/情景/语义记忆架构
💭 自由思考 Free Thinking 非任务驱动的意识流思考
🔮 智能预测 Prediction 预测用户需求,预加载记忆
📊 可视化 Visualization 知识图谱、时间线、摘要
🔗 联想网络 Association Network 概念关联,激活扩散算法
🏗️ 分层架构 Layered Architecture Domain/Repository/Service 分层设计
🔒 事务安全 Transaction Safety UnitOfWork 确保数据一致性
📚 完整文档 Documentation API 文档 + 架构文档
✅ 测试覆盖 Test Coverage 自动化测试套件

🚀 快速开始 | Quick Start

一键安装 | Quick Install

# 方式1: ClawHub 安装(推荐)
clawhub install cognitive-brain
cd ~/.openclaw/workspace/skills/cognitive-brain
npm run setup:auto    # 非交互模式,自动安装所有依赖

# 方式2: 手动安装
cd ~/.openclaw/workspace/skills/cognitive-brain
npm install
npm run setup         # 交互式配置

安装模式 | Install Modes

命令 说明
npm run check 检查系统依赖(PostgreSQL, Redis, pgvector)
npm run setup 交互式安装,会提示输入数据库配置
npm run setup:auto 自动安装,使用默认配置,自动安装缺失依赖
npm run setup:resume 从断点恢复安装(安装失败后使用)
npm run health 健康检查
npm run reset 重置数据库(清空记忆,保留表结构)
npm run uninstall 卸载(清理数据库、hooks、配置文件)

安装特性 | Install Features

特性 说明
🔄 断点恢复 安装失败后运行 npm run setup:resume 继续
📋 安装日志 记录到 install.log,便于排查问题
🔍 已安装检测 自动检测是否已安装,避免重复
⚡ 一键安装 installCommand 自动执行完整安装流程

系统要求 | Requirements

依赖 版本 安装命令 (Ubuntu)
Node.js >= 18 apt install nodejs npm
PostgreSQL >= 14 apt install postgresql
Redis >= 6 apt install redis-server
pgvector - apt install postgresql-16-pgvector

💡 npm run setup:auto 会自动安装这些依赖(需要 root 权限)

基础使用 | Basic Usage

const { CognitiveBrain } = require('./src/index.js');
const brain = new CognitiveBrain();

// 存储记忆
const memory = await brain.encode('用户的项目叫 Alpha', {
  type: 'conversation',
  importance: 0.8
});

// 检索记忆
const memories = await brain.recall('项目');

// 获取统计
const stats = await brain.stats();

CLI 工具

# 编码记忆
node scripts/core/encode.cjs "内容" -t conversation -i 0.8

# 检索记忆
node scripts/core/recall.cjs --query "关键词" --limit 5

# 健康检查
node scripts/tools/health_check.cjs

# 运行测试
./tests/run.sh

🏗️ 架构概览 | Architecture

v5.0 分层架构

┌─────────────────────────────────────────────────────────────┐
│                      API / CLI 层                           │
│  ┌────────────┐  ┌────────────┐  ┌────────────┐            │
│  │  REST API  │  │   CLI      │  │   Hook     │            │
│  └────────────┘  └────────────┘  └────────────┘            │
└─────────────────────────┬───────────────────────────────────┘
                          │
┌─────────────────────────▼───────────────────────────────────┐
│                   Service 层                                │
│  ┌────────────┐  ┌────────────┐  ┌────────────┐            │
│  │   Memory   │  │   Concept  │  │ Association│            │
│  │   Service  │  │   Service  │  │   Service  │            │
│  └────────────┘  └────────────┘  └────────────┘            │
└─────────────────────────┬───────────────────────────────────┘
                          │
┌─────────────────────────▼───────────────────────────────────┐
│                Repository 层                                │
│  ┌────────────┐  ┌────────────┐  ┌────────────┐            │
│  │   Memory   │  │   Concept  │  │ Association│            │
│  │ Repository │  │ Repository │  │ Repository │            │
│  └────────────┘  └────────────┘  └────────────┘            │
│                      UnitOfWork (事务)                      │
└─────────────────────────┬───────────────────────────────────┘
                          │
┌─────────────────────────▼───────────────────────────────────┐
│                  Domain 层                                  │
│  ┌────────────┐  ┌────────────┐  ┌────────────┐            │
│  │   Memory   │  │   Concept  │  │ Association│            │
│  │   Entity   │  │   Entity   │  │   Entity   │            │
│  └────────────┘  └────────────┘  └────────────┘            │
└─────────────────────────┬───────────────────────────────────┘
                          │
┌─────────────────────────▼───────────────────────────────────┐
│              持久层 | Persistence                           │
│  ┌────────────┐  ┌────────────┐  ┌────────────┐            │
│  │  PostgreSQL│  │   Redis    │  │   Files    │            │
│  │  (主存储)   │  │  (缓存)     │  │  (配置/日志)│            │
│  └────────────┘  └────────────┘  └────────────┘            │
└─────────────────────────────────────────────────────────────┘

设计原则

  1. 单一职责: 每个模块只做一件事
  2. 依赖注入: 通过参数传入依赖,不直接创建
  3. 接口隔离: 清晰的模块边界
  4. 事务安全: 多表操作原子性保证

🧠 四层记忆模型 | Four-Layer Memory

层级 Layer Duration Purpose Description
感官记忆 Sensory Milliseconds Buffer Transient sensory buffer for immediate perception
工作记忆 Working Minutes~Hours Active Processing Active processing workspace for current tasks
情景记忆 Episodic Long-term Experiences Personal experiences and events with context
语义记忆 Semantic Long-term Knowledge Facts, concepts, and structured knowledge

📚 核心 API

CognitiveBrain

主入口类,提供统一的操作接口。

const { CognitiveBrain } = require('./src/index.js');
const brain = new CognitiveBrain();

// 编码记忆
const memory = await brain.encode(content, {
  type: 'conversation',
  importance: 0.8,
  sourceChannel: 'qq'
});

// 检索记忆
const memories = await brain.recall('关键词', { limit: 10 });

// 事务操作
await brain.transaction(async (uow) => {
  // 多表原子操作
});

// 获取统计
const stats = await brain.stats();

实体模型

Memory (记忆)

{
  id: 'uuid',
  content: '记忆内容',
  type: 'episodic',
  importance: 0.5,
  sourceChannel: 'qq',
  role: 'user',
  entities: ['关键词'],
  emotions: { valence: 0, arousal: 0 },
  createdAt: Date,
  updatedAt: Date
}

Concept (概念)

{
  id: 'uuid',
  name: '概念名称',
  type: 'general',
  importance: 0.5,
  activation: 0.0,
  accessCount: 0
}

Association (关联)

{
  id: 'uuid',
  fromId: '概念A-ID',
  toId: '概念B-ID',
  type: 'related',
  weight: 0.5
}

🔒 事务管理

使用 UnitOfWork 模式确保数据一致性:

const { UnitOfWork } = require('./src/repositories/UnitOfWork.js');
const { MemoryRepository } = require('./src/repositories/MemoryRepository.js');

// 自动事务
await UnitOfWork.withTransaction(pool, async (uow) => {
  const memRepo = new MemoryRepository(uow.getQueryClient());
  const conceptRepo = new ConceptRepository(uow.getQueryClient());
  
  await memRepo.create(memory);
  await conceptRepo.create(concept);
  // 自动 commit 或 rollback
});

🧪 测试

# 运行所有测试
./tests/run.sh

# 运行特定测试
node tests/v5.test.cjs
node tests/db.test.cjs
node tests/memory.test.cjs

# 健康检查
node scripts/tools/health_check.cjs

📖 文档


🔄 共享工作区 | Shared Workspace

核心表 | Core Tables

表名 用途 说明
episodes 情景记忆 存储对话和事件
concepts 概念节点 提取的实体和关键词
associations 概念关联 概念间的关系
system_memory 系统配置 全局设置和状态

Hook 集成

自动集成到 OpenClaw 消息流程:

用户消息 → Hook(cognitive-recall) → 检索记忆 → 注入上下文 → AI回复 → 编码记忆

🔧 配置

{
  "version": "5.3.25",
  "storage": {
    "primary": {
      "type": "postgresql",
      "host": "localhost",
      "port": 5432,
      "database": "cognitive_brain"
    },
    "cache": {
      "type": "redis",
      "host": "localhost",
      "port": 6379
    }
  }
}

📈 版本历史

v7.0.1 (2026-03-21)

  • 🔧 记忆系统重构 - 修复 Hook 触发问题,改用 Cron 方案
  • 记忆读取 - agent:bootstrap Hook 即时注入记忆
  • 记忆写入 - Cron 每 1 分钟扫描会话文件
  • 🗑️ 清理无用脚本 - 删除 20+ 不常用脚本
  • 📝 文档更新 - 简化架构说明

v5.3.32 (2026-03-21)

  • 🐛 修复类型验证 - type: 'conversation' 改为有效类型 'episodic'
  • 🐛 修复 Embedding 路径 - embed.py 路径错误导致 embedding 生成失败

v5.3.22 (2026-03-20)

  • 发布版本更新

v5.3.11 (2026-03-18)

  • 🧪 测试覆盖率提升 - 新增 Repository/Service/API 测试 (7个测试文件)
  • 🧹 事件监听器清理 - 优雅关闭时清理所有监听器,防止内存泄漏
  • 💓 WebSocket 心跳 - 30秒 ping/pong 检测,120秒无响应自动断开
  • N+1 查询优化 - 批量插入从 O(N) 优化为 O(1)
  • 🔒 API 速率限制 - 100 req/min 限流保护

v5.3.7 (2026-03-18)

  • 🗑️ 删除重复文档 - 删除 README.md,统一文档入口
  • 🔧 统一代码规范 - 修复 80+ 文件缺少换行符
  • 🔧 统一 Logger - Winston logger 替代 console.log
  • 🔒 事务安全 - 隔离级别 + 死锁重试 + 熔断器
  • ⚙️ 配置增强 - 连接池可配置 + 日志配置

v5.3.20 (2026-03-19)

  • 安装流程优化 - 非交互模式、断点恢复、安装日志
  • Skill.json installCommand - 支持一键自动安装
  • 卸载脚本 - npm run uninstall 清理所有组件
  • 重置脚本 - npm run reset 清空记忆数据
  • 🔧 修复 create_indexes.cjs - 模块路径错误
  • 🔧 修复 health_check.cjs - SQL 语法错误
  • 📝 文档更新 - 安装指南、命令说明

v5.0.0 (2026-03-18)

  • ✨ 重构为分层架构 (Domain/Repository/Service)
  • ✨ 添加 UnitOfWork 事务管理
  • ✨ 添加领域模型验证
  • ✨ 添加测试框架 (13个测试)
  • ✨ 添加统一日志模块
  • ✨ 添加配置管理器
  • ✅ 修复 Pool 连接泄漏
  • ✅ 修复空 catch 块
  • ✅ 移除文件 fallback,强制数据库优先

v4.1.0 (Previous)

  • 基础记忆功能
  • 联想网络
  • 预测预加载
  • 心跳反思
  • 自由思考

🤝 贡献

  1. Fork 仓库
  2. 创建分支 (git checkout -b feature/xxx)
  3. 提交修改 (git commit -m 'Add feature')
  4. 推送分支 (git push origin feature/xxx)
  5. 创建 Pull Request

📄 许可

MIT License


让 AI 拥有记忆,让对话更有温度 ❤️

Usage Guidance
This package appears to be a real cross‑session memory system, but several things don't add up and carry risk: - It needs a PostgreSQL and Redis backend and will prompt for or expect DB credentials (PG* and REDIS_*), yet the registry declared no required env vars — verify and only provide a database account with minimal privileges. - The installer (npm postinstall / scripts/tools/postinstall.cjs and install.sh) will modify OpenClaw gateway hooks, create cron tasks, and can run arbitrary JS during npm install — inspect postinstall and scripts/tools/* before running. Consider running npm install in a sandbox or offline environment first. - The cognitive‑recall hook automatically reads memory/episodes and injects them into agent bootstrap contexts; this is the feature but also a privacy surface — review what data will be injected (may include sensitive user content) and whether you want automatic injection enabled. - There are metadata inconsistencies (declared 'instruction-only' vs. many code files, version mismatches). Confirm repository origin and integrity (the install script references a placeholder repo URL and package-lock shows external mirrors). Recommended actions before installing: 1. Inspect scripts/tools/postinstall.cjs, scripts/tools/sync-hooks.cjs, and hooks/cognitive-recall/handler.js to see exactly what files/config they change. 2. Back up OpenClaw gateway configuration. 3. Test install in an isolated environment (container/VM) and do a code audit for any external network calls or unexpected exec/spawn usage. 4. Provide least‑privilege DB credentials and disable automatic hook injection until reviewed. If you want, I can: (a) point to specific files to inspect (postinstall, sync-hooks, handler), (b) summarize any network or exec usage found in the omitted files, or (c) produce a short checklist for a safe audit/run.
Capability Analysis
Type: OpenClaw Skill Name: cognitive-brain Version: 7.0.1 The 'cognitive-brain' skill is a sophisticated cross-session memory and cognition system for OpenClaw. It implements a multi-layered memory architecture using PostgreSQL (with pgvector) and Redis. The codebase is exceptionally well-structured, following a Domain-Driven Design (DDD) approach with clear separation between services, repositories, and domain models. Notably, it includes a dedicated security module (scripts/core/safety.cjs) designed to detect and block dangerous operations such as shell injections (e.g., curl|bash), data exfiltration, and unauthorized file access. While the skill performs sensitive actions like reading session logs and spawning background processes for embeddings (scripts/embed.py), these behaviors are essential to its stated purpose and are implemented with robust validation and error handling. No evidence of malicious intent, obfuscation, or unauthorized data transmission was found.
Capability Assessment
Purpose & Capability
The code, docs and scripts align with a memory/cognition system (Postgres + Redis, embedding script, hooks, API, CLI). However metadata in the registry (no required env vars / no install spec) contradicts the actual bundle which requires DB/Redis configuration and includes install/postinstall logic. Version numbers in files (5.x) vs SKILL.md header (7.0.1) are inconsistent.
Instruction Scope
SKILL.md and install docs instruct automatic installation of system packages (PostgreSQL, Redis, pgvector), running npm scripts that initialize DB and 'sync hooks'. The cognitive‑recall hook auto‑reads the skill config, local user_model.json and may query the episodes table to inject user memory into agent bootstrap context — a core feature but also a privacy-sensitive automatic behavior. The instructions also guide modifying OpenClaw gateway hooks and creating cron tasks during installation.
Install Mechanism
Although registry listed no install spec, the bundle contains install.sh and Skill.json with an installCommand that runs npm install and a postinstall script. npm postinstall and postinstall JS can run arbitrary code and the changelog claims automatic modification of gateway hook configuration and backups. install.sh attempts to git clone a repo (placeholder URL present) and runs npm install; package-lock references external npm mirrors. Automatic postinstall changes to gateway/hooks and cron setup increase risk and should be audited.
Credentials
The registry declares no required environment variables, but code and docs rely on PGHOST/PGPORT/PGDATABASE/PGUSER/PGPASSWORD and REDIS_HOST/REDIS_PORT and may read config.json under the skill directory. The hook expects DB creds and will attempt DB queries; this is reasonable for a memory store but the absence of declared env requirements is an inconsistency and increases the chance of accidental credential misuse.
Persistence & Privilege
Installation/postinstall purportedly modifies OpenClaw gateway hooks (auto‑injects cognitive‑recall), sets cron jobs (forgetting, proactive tasks) and writes .installed.json/.bak backups. These actions modify system-wide agent behavior and persist beyond one invocation. 'always' is false, but the skill’s installer still gains persistent presence by changing gateway hooks — review postinstall and sync-hooks implementation before allowing install.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install cognitive-brain
  3. After installation, invoke the skill by name or use /cognitive-brain
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v7.0.1
改为5分钟间隔,后台静默运行
v7.0.0
记忆系统重构:Hook读取+Cron写入
v6.1.0
记忆系统重构:Hook读取+Cron写入
v6.0.0
记忆系统重构:Hook 读取 + Cron 写入
v5.3.35
修复 USER_MODEL_PATH 未定义错误,创建 data 目录和 user_model.json
v5.3.34
修复 shared_memory.cjs 中 redis 模块加载路径问题(Hook 环境需要绝对路径)
v5.3.33
recall.cjs 改用应用层(带 embedding 语义搜索),fallback 到数据库直连
v5.3.32
新增 recall.cjs 手动召回工具,在回复前查询相关记忆
v5.3.31
更新 SKILL.md 版本号,确保文档与代码同步
v5.3.30
新增 encode_reply.cjs 工具用于快速记录 AI 回复
v5.3.29
修复记忆注入问题:记忆虽然注入到 event.context 但模型看不到。现在使用 injectToPrompt 方法
v5.3.28
简化 encode_reply.cjs,直接插入数据库跳过 embedding,保存速度从 60s 降到 1s
v5.3.27
增加 embedding 超时到 60 秒,移除无用的 response.completed hook
v5.3.26
移除无用的 response.completed hook,AI 回复通过工具手动记录
v5.3.25
v5.3.25 - 修订配置文件与元数据:版本号更新为 5.3.25 - 文档(SKILL.md、README.md、INSTALL_GUIDE.md)小幅修订,版本号与内容同步 - 更新 package.json 和依赖锁文件,确保依赖一致 - 其他脚本和配置的细节调整以反映新的版本信息
v5.3.24
- updated 13 file(s). - Updated SKILL.md and bundle contents.
v5.3.23
- added 2, updated 11 file(s). - Updated SKILL.md and bundle contents.
v5.3.22
修复: emotion/emotions列名, AssociationRepository ON CONFLICT, 测试用例类型错误; 添加: bidirectional和updated_at列
v5.3.21
修复 emotion/emotions 列名不匹配问题
v5.3.20
**v5.3.20 introduces a redesigned auto-install and database reset/uninstall workflow.** - Added `install.sh` and unified one-click auto-install and uninstall scripts (`setup:auto`, `uninstall`). - New commands for interactive install, auto install, install-resume, health check, and DB reset (see Quick Start). - Installation is now resumable; failures can be recovered with `npm run setup:resume`. - Install log is generated; existing installs are auto-detected for safety. - Documentation and CLI usage updated to reflect simplified installation and management.
Metadata
Slug cognitive-brain
Version 7.0.1
License MIT-0
All-time Installs 2
Active Installs 2
Total Versions 79
Frequently Asked Questions

What is Cognitive Brain?

Provides a cross-session AI memory and cognition system with four-layer memory, real-time sync, free thinking, intelligent prediction, and knowledge visualiz... It is an AI Agent Skill for Claude Code / OpenClaw, with 849 downloads so far.

How do I install Cognitive Brain?

Run "/install cognitive-brain" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Cognitive Brain free?

Yes, Cognitive Brain is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Cognitive Brain support?

Cognitive Brain is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Cognitive Brain?

It is built and maintained by aboutyao (@aboutyao); the current version is v7.0.1.

💬 Comments