@agentskit/memory

中文文档
0.12.1发布于 2026/10/24MITTypeScriptESM + CJS构建来源已验证近 30 天下载 2万

Persistent and vector memory backends for AgentsKit.

原始使用文档
文档目录18 节
  1. Verified proof
  2. How this fits the ecosystem
  3. Why memory
  4. Install
  5. Quick example
  6. With RAG
  7. Features
  8. Chat memory (4)
  9. Browser Web Storage
  10. Vector memory (11)
  11. Higher-order wrappers (6)
  12. Ecosystem
  13. Contributors
  14. License
  15. Docs
  16. Maturity and compatibility
  17. Contributing
  18. PostgreSQL chat memory (Node and Workers)

@agentskit/memory

Profile: major-package

AgentsKit

Persist conversations and add vector search to your agents — swap backends without changing agent code.

npm version npm downloads bundle size license stability GitHub stars

Tags: ai · agents · llm · agentskit · ai-agents · memory · vector-db · embeddings · rag · sqlite · redis · vector-search

Verified proof

How this fits the ecosystem

@agentskit/memory gives agents continuity: chat history, vector memory, graph memory, encrypted stores, and local or hosted backends.

  • AgentsKit: compose it with the other packages in this repo to build agents from small, swappable parts.
  • Registry: look for ready agents and templates that already use this layer at registry.agentskit.io.
  • Playbook: learn the production patterns behind this layer at playbook.agentskit.io.

Docs: package guide · agent handoff

Why memory

  • Conversations that survive restarts — SQLite for local development, Redis for production; your agent remembers context across sessions with zero code changes
  • RAG-ready vector search — store and retrieve embeddings with fileVectorMemory (pure JS, no native deps) or Redis vector search for scale
  • Plug any backend — the VectorStore interface is 3 methods; bring LanceDB, Pinecone, or any custom store in minutes
  • One interface, every deployment target — swap from inMemory to sqlite to redis without touching agent code

Install

npm install @agentskit/memory better-sqlite3
# For production:  npm install redis
# For vectors:     npm install vectra

Quick example

import { createRuntime } from '@agentskit/runtime'
import { anthropic } from '@agentskit/adapters'
import { sqliteChatMemory } from '@agentskit/memory'

const runtime = createRuntime({
  adapter: anthropic({ apiKey: process.env.ANTHROPIC_API_KEY, model: 'claude-sonnet-4-6' }),
  memory: sqliteChatMemory({ path: './chat.db' }),
})

// Agent now remembers previous conversations across process restarts
const result = await runtime.run('What did we discuss yesterday?')
console.log(result.content)

With RAG

Use a vector backend with @agentskit/rag createRAG({ embed, store }) — fileVectorMemory and redisVectorMemory implement VectorMemory for chunk storage and search.

Features

Chat memory (4)

  • fileChatMemory({ path }) — JSON on disk; zero infra.
  • sqliteChatMemory({ path }) — WAL-mode SQLite; indexed by session.
  • redisChatMemory({ client, keyPrefix }) — distributed, serverless-friendly.
  • tursoChatMemory({ url, authToken? }) — hosted or local libSQL/Turso.

All on top of createInMemoryMemory / createLocalStorageMemory from @agentskit/core.

Browser Web Storage

Use the browser-safe subpath when a host needs validated, bounded storage or sessionStorage instead of the legacy Core localStorage helper:

import { createWebStorageMemory } from '@agentskit/memory/web-storage'

const memory = createWebStorageMemory({
  key: 'app:chat',
  getStorage: () => typeof sessionStorage === 'undefined' ? undefined : sessionStorage,
  maxMessages: 20,
  maxRecordBytes: 1_048_576,
})

Canonical records are runtime validated. Oversized saves reject with AK_MEMORY_SAVE_FAILED before changing storage. migration may supply legacy keys and a host-owned parser; a legacy key is removed only after canonical persistence succeeds.

Vector memory (11)

  • fileVectorMemory — pure-JS, file-persisted (good to ~10k vectors).
  • redisVectorMemory — Redis Stack / Redis 8+ HNSW.
  • pgvector — BYO SQL runner (postgres.js, pg, Drizzle, Prisma, Neon).
  • pinecone — managed; namespaces + metadata filters.
  • qdrant — self-hosted or cloud via HTTP.
  • chroma — Chroma v2 HTTP client with tenant, database, and token support.
  • upstashVector — serverless HTTP.
  • supabaseVectorStore — Supabase-hosted pgvector RPC.
  • weaviateVectorStore — Weaviate Cloud or self-hosted HTTP.
  • milvusVectorStore — Milvus/Zilliz REST API.
  • mongoAtlasVectorStore — MongoDB Atlas Vector Search with an injected collection.

Vector adapters follow ADR 0003: a result is included only when its score is strictly greater than threshold. Remote HTTP adapters accept timeoutMs and maxResponseBytes, defaulting to 15 seconds and 2 MiB; both require positive safe integers. Injected fetch and caller signal are also supported. Timeout and response-limit failures use AK_MEMORY_REMOTE_HTTP. File KV writes are atomic and serialized across instances in one process. They are not a multi-process coordination primitive; use SQLite, Redis, or another external store when several processes write the same file.

Same 3-method VectorStore contract — swap without touching agent code.

Higher-order wrappers (6)

  • createHierarchicalMemory — MemGPT-style tiers: working / recall / archival. Recipe.
  • createVirtualizedMemory — hot window + cold retriever for long sessions. Recipe.
  • createAutoSummarizingMemory (via @agentskit/core/auto-summarize) — fold oldest turns into a running summary. Recipe.
  • createEncryptedMemory — AES-GCM-256 envelope over any ChatMemory; keys never leave the caller. Recipe.
  • createInMemoryGraph — knowledge graph (nodes + edges + BFS). Recipe.
  • createInMemoryPersonalization + renderProfileContext — per-user trait profile. Recipe.

Memory contract v1 (ADR 0003) — substitutable across runtime, useChat, and every framework binding.

Ecosystem

PackageRole
@agentskit/coreMemory, VectorMemory types
@agentskit/ragChunking + retrieval on top of vector memory
@agentskit/runtimememory / retriever options
@agentskit/adaptersEmbeddings for RAG

Contributors

AgentsKit contributors

License

MIT — see LICENSE.

Docs

Full documentation · GitHub

Maturity and compatibility

  • Stability: beta — see docs/STABILITY.md
  • Node.js 20+ and TypeScript strict mode
  • Published as @agentskit/memory

Contributing

See CONTRIBUTING.md and the monorepo LICENSE.

PostgreSQL chat memory (Node and Workers)

Install drizzle-orm and pg alongside this package. The optional @agentskit/memory/postgres subpath exports postgresChatMemory, postgresChatTable (Drizzle schema), and postgresChatMigrationSql. Neither peer is imported by the main entry.

import { Pool } from 'pg'
import { drizzle } from 'drizzle-orm/node-postgres'
import { sql } from 'drizzle-orm'
import { postgresChatMemory, postgresChatMigrationSql } from '@agentskit/memory/postgres'

const pool = new Pool() // host owns connection configuration and shutdown
const db = drizzle(pool)
// Run once in your migration tooling, not on every request:
await db.execute(sql.raw(postgresChatMigrationSql))
const memory = postgresChatMemory({ db, tenantId: 'tenant-a', sessionId: 'session-a' })
await memory.save([])
await pool.end()

In Workers, enable nodejs_compat; create and connect a pg.Client inside each request, pass drizzle(client), and close it with ctx.waitUntil(client.end()) after all memory operations (and any stream using them) have finished. The package never connects, migrates, or closes your client. A Node Pool can be shared across requests. No Neon, Hyperdrive, or Cloudflare dependency is needed; the host can supply their connection configuration when appropriate.

Every query uses both tenant and session, which form the primary key. Derive these identifiers from trusted authorization context: caller-supplied tenant names alone are not authorization. Saves atomically replace a versioned JSONB snapshot, preserving parts, tools, metadata, dates, and message order. Concurrent saves use last-writer-wins; this ChatMemory does not implement session CAS or turn leases. Schema migration is explicit; SQL/connection errors propagate and invalid records fail rather than silently discarding history.

No truncation is performed by default (CM1–CM6). Opt into maxMessages: N to keep roughly the newest N messages on each save, in their original order. Retention cuts only at a user turn boundary: the kept window always starts at a user message, so a user question is never separated from its assistant answer and a tool call is never separated from its results. When the N-newest cutoff falls inside the latest (possibly in-progress) turn, that whole turn is kept, so the result can exceed N. Leading system messages are always kept. A history with no user message is stored unchanged rather than emptied. This permanently removes older turns and counts messages, not tokens or bytes. For semantic summarization, prepare the history in the host instead. A positive safe integer is required; empty saves still persist [].

The supported Drizzle peer range is ^0.44.0 || ^0.45.3. Both lower bounds passed the real PostgreSQL contract suite and the package typecheck.

signal rejects already-aborted operations before any SQL is issued. Loads also check after SQL returns. Drizzle/pg does not cancel in-flight SQL here: a save/clear already submitted can commit after an abort and will report its actual SQL outcome. Configure query/statement timeouts on the host connection.

Optional defense-in-depth RLS (run as migration owner, access through a non-owner role without BYPASSRLS; set tenant inside a host-managed transaction):

ALTER TABLE agentskit_chat_memory ENABLE ROW LEVEL SECURITY;
ALTER TABLE agentskit_chat_memory FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON agentskit_chat_memory
  USING (tenant_id = current_setting('app.tenant_id', true))
  WITH CHECK (tenant_id = current_setting('app.tenant_id', true));

Do not set a tenant on a shared pool connection outside a transaction. The host must enforce role permissions and set its trusted tenant with transaction-local set_config('app.tenant_id', tenant, true).

Real acceptance checks (local disposable PostgreSQL 16, trust auth on loopback; no production accounts):

docker run -d --name ak-memory-test -p 127.0.0.1:55439:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:16
AK_POSTGRES_TEST_PORT=55439 pnpm --filter @agentskit/memory test:postgres
AK_POSTGRES_TEST_PORT=55439 pnpm --filter @agentskit/memory test:workerd
docker rm -f ak-memory-test

CI runs test:postgres against a PostgreSQL 16 service with trust authentication and an explicit test port; no secrets are required. Locally it is skipped without its explicit test port. test:workerd runs wrangler dev --local, uses a Client per request and waitUntil shutdown, and exercises the same synthetic contract suite as the Node Pool test.

版本与发布记录

@agentskit/memory 共发布 44 个版本,首次发布于 2026/04/06;过去 12 个月发布了 44 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.12.1latest2026/10/2412 天前
0.12.02026/10/1026 天前
0.11.142026/10/031 个月前
0.11.132026/10/031 个月前
0.11.122026/09/291 个月前
0.11.112026/09/261 个月前

关于 @agentskit/memory 的常见问题

@agentskit/memory 是什么?

Persistent and vector memory backends for AgentsKit.

如何安装 @agentskit/memory?

在项目目录运行 npm install @agentskit/memory,或使用 pnpm add / yarn add / bun add。

@agentskit/memory 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@agentskit/memory 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@agentskit/memory 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@agentskit/memory 可以商用吗?

@agentskit/memory 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@agentskit/memory 的下载量有多少?

根据 npm 官方下载统计,@agentskit/memory 在 2026/10/05 – 2026/11/03 这 30 天内被下载 20,347 次,日均约 678 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@agentskit/memory 还在维护吗?

最新版本 0.12.1 发布于 2026/10/24(12 天前);至今共发布 44 个版本,其中过去 12 个月发布了 44 个;首次发布于 2026/04/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。