npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ai-agent-memory-system

v0.4.2

Published

A memory management system for AI agents with session tracking, daily journals, and knowledge extraction

Downloads

84

Readme

AI Agent Memory System

一个为AI智能体设计的记忆管理系统,支持项目级记忆、会话管理、日记记录和知识抽取。

特性

  • 项目级记忆 - 记忆跟随项目,自动加载项目上下文
  • 会话管理 - 管理短期记忆,支持会话开始/结束
  • 事件记录 - 记录重要决策、模式、问题解决等事件
  • 日记系统 - 每日自动生成日记,记录开发历程
  • 知识抽取 - 从日记中提取结构化知识,形成长期记忆
  • 语义检索 - 基于关键词的记忆搜索,支持多维度查询
  • 分层存储 - 短期/中期/长期记忆分层管理

目录结构

npm 包结构:

ai-agent-memory-system/
├── src/
│   ├── MemoryManager.js      # 核心管理器
│   ├── MemoryStore.js        # 存储层
│   ├── SessionManager.js     # 会话管理
│   ├── DailyJournal.js       # 日记管理
│   ├── KnowledgeExtractor.js # 知识抽取
│   ├── MemoryIndexer.js      # 记忆索引
│   └── assistant/            # 智能助手模块
└── package.json

生成的记忆文件结构:

.memory/
├── .metadata/               # 元数据
│   ├── project_context.json # 项目基本信息
│   └── memory_stats.json   # 记忆统计
├── sessions/                # 会话记录
│   ├── session_20260318_001.json
│   └── latest_session.json
├── daily/                   # 日记
│   ├── 2026-03-18.md
│   └── 2026-03-17.md
├── extracted/              # 知识抽取结果
│   ├── decisions.md
│   ├── patterns.md
│   ├── troubleshooting.md
│   └── insights.md
└── index/                  # 索引
    └── search_index.json

快速开始

安装

npm install ai-agent-memory-system

基础示例

import { MemoryManager } from 'ai-agent-memory-system';

const memory = new MemoryManager({
  basePath: '.memory'
});

// 初始化
await memory.initialize();

// 设置项目名称
await memory.setProjectName('My Project');

// 开始会话
const sessionId = await memory.startSession({
  goal: '开发用户认证功能'
});

// 记录事件
await memory.recordEvent({
  type: 'decision',
  description: '采用JWT认证',
  details: '使用access token + refresh token',
  importance: 5
});

// 结束会话
await memory.endSession('完成认证模块开发');

// 搜索记忆
const results = await memory.search('JWT');
console.log(results);

运行示例

# 基础示例
npm run test

# 完整演示
npm run demo

API 文档

MemoryManager

initialize()

初始化记忆系统,创建必要的目录结构。

startSession(options)

开始新会话。

await memory.startSession({
  goal: '会话目标描述'
});

endSession(summary)

结束当前会话并生成日记。

await memory.endSession('会话总结');

recordEvent(event)

记录事件。

await memory.recordEvent({
  type: 'decision',        // decision | pattern | bug_fix | note | insight | troubleshoot
  description: '事件描述',
  details: '详细信息',
  importance: 5            // 1-5,5为最重要
});

extractKnowledge(startDate, endDate, options)

从指定日期范围的日记中提取知识。

const result = await memory.extractKnowledge(startDate, endDate, {
  decisions: true,
  patterns: true,
  troubleshooting: true,
  insights: true,
  architecture: true
});

search(query, options)

搜索记忆。

const results = await memory.search('JWT', {
  scope: 'all',            // all | sessions | daily | extracted
  limit: 10
});

getProjectMemory()

获取完整的项目记忆(长期记忆 + 最近日记)。

const memory = await memory.getProjectMemory();
// { context, longTermMemory, recentJournals }

记忆分层

短期记忆 (Session Memory)

  • 内容: 当前会话的事件记录
  • 生命周期: 当前会话
  • 存储位置: .memory/sessions/
  • 特点: 实时记录,会话结束时整理为日记

中期记忆 (Project Memory)

  • 内容: 项目上下文、架构决策、代码模式
  • 生命周期: 随项目存在
  • 存储位置: .memory/daily/
  • 特点: 按日期组织,方便回顾

长期记忆 (Knowledge Base)

  • 内容: 知识抽取后的结构化数据
  • 生命周期: 持久化
  • 存储位置: .memory/extracted/
  • 特点: 高度提炼,跨项目复用

知识抽取

系统支持自动从日记中提取以下类型的知识:

  • 决策 (Decisions): 重要的技术决策和权衡
  • 模式 (Patterns): 代码模式和最佳实践
  • 问题解决 (Troubleshooting): 问题诊断和解决方案
  • 洞察 (Insights): 项目洞察和经验总结
  • 架构 (Architecture): 架构设计和演进

搜索能力

多维度搜索

// 搜索所有记忆
await memory.search('JWT');

// 只搜索长期记忆
await memory.search('JWT', { scope: 'extracted' });

// 只搜索最近3天
await memory.search('bug', { scope: 'daily' });

搜索结果

{
  sessions: [/* 匹配的会话 */],
  daily: [/* 匹配的日记 */],
  extracted: [/* 匹配的长期记忆 */],
  keywords: [/* 匹配的关键词 */]
}

使用场景

场景1: 项目上下文恢复

// 进入项目时
const memory = new MemoryManager({ basePath: '.memory' });
await memory.initialize();

// 获取项目记忆,快速了解项目背景
const projectMemory = await memory.getProjectMemory();
console.log(projectMemory.context.project_name);
console.log(projectMemory.longTermMemory.decisions);  // 重要的技术决策

场景2: 会话记录

// 开始开发任务
await memory.startSession({ goal: '实现用户注册功能' });

// 记录重要决策
await memory.recordEvent({
  type: 'decision',
  description: '使用bcrypt密码哈希',
  importance: 5
});

// 记录遇到的问题
await memory.recordEvent({
  type: 'bug_fix',
  description: '修复邮箱验证码发送失败',
  details: 'SMTP配置错误,更新为正确的端口',
  importance: 4
});

// 任务完成
await memory.endSession('完成用户注册功能,包括邮箱验证');

场景3: 知识积累

// 定期从日记中抽取知识
const startDate = startOfDay(subDays(new Date(), 30));
const endDate = new Date();

await memory.extractKnowledge(startDate, endDate, {
  decisions: true,
  patterns: true,
  troubleshooting: true
});

// 生成的知识文档会自动保存到 .memory/extracted/

场景4: 问题排查

// 搜索相关问题的历史记录
const results = await memory.search('token 过期', { scope: 'all' });

// 查看之前如何解决类似问题
for (const session of results.sessions) {
  const sessionData = await memory.getSession(session.id);
  console.log(sessionData);
}

扩展建议

1. 集成LLM

  • 使用LLM生成更智能的日记摘要
  • 使用LLM进行更准确的知识抽取
  • 使用LLM进行语义搜索(向量检索)

2. 持久化存储

  • 支持SQLite、MongoDB等数据库
  • 支持云存储(S3、阿里云OSS)

3. 协作功能

  • 多用户协作
  • 记忆分享和同步

4. 可视化

  • 记忆可视化界面
  • 项目时间线
  • 知识图谱

License

MIT