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

@carbide/l2-memory-manager

v1.0.0

Published

MemoryManager 组件,对话记忆管理 - Day 1 核心组件 L2

Readme

L2 MemoryManager - 对话记忆管理

Day 1 核心组件 L2,基于 LangChain.js 的 RunnableWithMessageHistoryChatMessageHistory 实现的对话记忆管理。

安装

npm install @carbide/l2-memory-manager

使用

基本用法(默认会话)

import { MemoryManager } from '@carbide/l2-memory-manager';

const memoryManager = new MemoryManager({
  apiKey: 'your-openai-api-key',
  modelName: 'gpt-4',
  memoryKey: 'history'
});

// 对话(自动记忆上下文)
const response1 = await memoryManager.chat('我叫张三');
const response2 = await memoryManager.chat('我叫什么名字?'); // 能记住是张三

// 获取历史记录
const history = await memoryManager.getHistory();

// 手动保存上下文
await memoryManager.saveContext('你好', '你好!有什么可以帮助你的?');

// 清空记忆
await memoryManager.clearMemory();

多会话管理

// 用户1的对话
const response1 = await memoryManager.chat('你好,我叫小明', 'user-001');
const response2 = await memoryManager.chat('你还记得我的名字吗?', 'user-001');

// 用户2的对话(独立的历史)
const response3 = await memoryManager.chat('你好,我叫小红', 'user-002');
const response4 = await memoryManager.chat('你知道我是谁吗?', 'user-002');

// 管理会话
const sessionIds = memoryManager.getSessionIds(); // 获取所有会话ID
await memoryManager.clearMemory('user-001'); // 清除指定会话的历史
memoryManager.removeSession('user-002'); // 移除指定会话

API

| 方法 | 说明 | 参数 | |------|------|------| | chat(message, sessionId?) | 对话(自动保存到记忆) | message: 消息内容sessionId: 会话ID,默认为"default" | | getHistory(sessionId?) | 获取历史记录 | sessionId: 会话ID,默认为"default" | | saveContext(input, output, sessionId?) | 手动保存上下文 | input: 用户输入output: AI输出sessionId: 会话ID,默认为"default" | | clearMemory(sessionId?) | 清空记忆 | sessionId: 会话ID,默认为"default" | | removeSession(sessionId) | 移除指定会话 | sessionId: 会话ID | | getSessionIds() | 获取所有会话ID | 无 |

测试

运行测试

npm run test

测试文件

  • src/index.test.ts - 单元测试文件

示例

基本使用示例

详见 examples/basic-usage.ts 文件,包含以下示例:

  1. 基本对话(默认会话)
  2. 多会话管理
  3. 历史管理
  4. 手动保存上下文

运行示例

# 安装 tsx
npm install -g tsx

# 运行示例
OPENAI_API_KEY="your-api-key" tsx examples/basic-usage.ts

技术实现

  • 使用 ChatPromptTemplate.fromMessages 构建提示模板
  • 使用 RunnableWithMessageHistory 管理对话历史
  • 使用 ChatMessageHistory 存储会话历史
  • 支持多会话管理,通过 sessionId 区分不同用户

在项目中使用

详见 day-01/02-chat-bot 等项目。