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

@memoweft/adapter-ai-sdk

v0.1.0

Published

Vercel AI SDK adapter for MemoWeft — inject long-term memory via middleware (recall) and persist the user's turn on finish (write).

Readme

@memoweft/adapter-ai-sdk

English · README.md

MemoWeft 的 Vercel AI SDK 适配器。 给你的 AI SDK 应用接上长期记忆: = 召回相关记忆、注入进 prompt; = 每轮对话结束后,沉淀【用户原话】。

这是个外部集成包,只消费 MemoWeft 的公开 Core 门面(createMemoWeftCore),不碰 Core 内部。ai 是 peer 依赖(你自备)。

安装

npm i ai memoweft @memoweft/adapter-ai-sdk

ai^7)和 memoweft^0.5.0)是 peer 依赖;跑真模型还需一个 ai provider(如 @ai-sdk/openai)。

两条路:读和写

读 —— 用 middleware 召回

createMemoWeftMiddleware(core) 返回一个 Vercel AI SDK 的 LanguageModelMiddleware。它在 transformParams 里取最后一条 user 消息的文本,调 core.recall({ query }),把召回到的认知注入回那条 user 消息,再交给模型。

import { wrapLanguageModel, generateText } from 'ai';
import { createMemoWeftCore } from 'memoweft';
import { createMemoWeftMiddleware } from '@memoweft/adapter-ai-sdk';

const core = createMemoWeftCore({ dbPath: './memory.db' });

const model = wrapLanguageModel({
  model: baseModel, // 你的 @ai-sdk/* 模型
  middleware: createMemoWeftMiddleware(core),
});

const { text } = await generateText({ model, prompt: '讲讲递归。' });

注入的这段说明照搬 MemoWeft Core 现成的中性措辞(逐字对齐 Core 的 knowledgeBlock):低置信条目明确标"只是假设,别当定论"。适配器不自造任何人格/人设 prompt——语气和角色仍归宿主。

召回为空、没有 user 文本、或召回抛错时,params 原样透传(召回失败绝不挡回话)。

选项:{ subjectId?, lang?: 'en' | 'zh', onRecall? }

写 —— 对话结束沉淀用户原话

createPersistOnEnd(core, { userMessage, originId }) 返回一个塞给 generateText/streamTextonEnd 回调(SDK 的 onFinishonEnd 的 @deprecated 别名)。这一轮结束后,它调 core.ingestUserMessage,把【用户原话】存成一条 spoken 证据。

import { createPersistOnEnd } from '@memoweft/adapter-ai-sdk';

const userMessage = '我很偏好简短、直接的回答。';
await generateText({
  model,
  prompt: userMessage,
  onEnd: createPersistOnEnd(core, { userMessage, originId: 'turn-1' }),
});

为什么要你显式传 userMessage SDK 的 onEnd 事件只带【结果侧】字段(text、steps、usage、response…),不带原始用户输入;而发给 provider 的请求体已经被读 middleware 改过(注入了召回记忆)。所以"用户真正说的那句原话"唯一干净的来源,就是你调 generateText 时本来就持有的那份。onEnd 事件对象不被使用——它只当触发时机。

  • 只存用户原话、绝不存助手回话(Core 纪律:助手回话不落证据)。
  • 给稳定的 originId(你的 turnId/messageId)保证幂等——同一轮即便 onEnd 触发多次也只落一条。
  • 不传任何上云授权位ingestUserMessagespoken 证据,本就不涉 observed 的上云授权位。
  • 空串/纯空白跳过不落库;落库出错走 onError(或静默吞)——存记忆失败绝不崩你这轮对话。

也有一个直白的 persistUserTurn(core, { userMessage, originId? }),想在 onEnd 之外自己调时用。

完整示例

examples/basic.ts——两轮对话:第 1 轮的话被存进去、召回进第 2 轮的 prompt。

与 MemoWeft Host 的关系

Host(apps/memoweft-host)是 MemoWeft 的参考应用——聊天界面、多会话、备份。本适配器是反方向:让你自己的应用(基于 Vercel AI SDK 搭的)把 MemoWeft 当记忆后端复用,不需要 Host。两者都对着同一个公开 Core 门面,彼此不依赖。想要现成 UI 就用 Host;已经有 AI SDK 应用、只想要记忆层,就用本适配器。

不做什么

  • 不做人格/人设 prompt(Core 无头——语气/角色归宿主)。
  • 不存助手回话,只存用户原话。
  • 不放宽 observed 上云,写路径不传授权位。

许可

MIT