@agent-assistant/memory

中文文档
0.4.37发布于 2026/10/04MITTypeScriptES Module构建来源已验证近 30 天下载 3.9万

Assistant-scoped memory composition layer with in-memory and Supermemory adapters

原始使用文档
文档目录12 节
  1. Installation
  2. Exports
  3. Quick Start
  4. Relay Reuse
  5. Scope Mapping
  6. Retrieval Rules
  7. Promotion and Compaction
  8. Expiry and Provenance
  9. Production Usage
  10. Turn Memory Helpers
  11. Isolation
  12. Not In V1

@agent-assistant/memory

@agent-assistant/memory is the v1 assistant-facing composition layer over the relay memory adapters it ships (InMemoryAdapter, SupermemoryAdapter, createMemoryAdapter; originally from @agent-relay/memory).

It uses those adapters directly for persistence and CRUD, then adds only the approved v1 behaviors:

  • assistant memory scopes: session, user, workspace, org, object
  • structured retrieval by scope, tags, and recency
  • optional user-scope expansion into an explicit session scope
  • promotion to broader scopes
  • compaction orchestration through a caller-supplied callback
  • TTL filtering through assistant-managed metadata

It does not implement a new memory engine, semantic search, policy controls, cloud-only behavior, or cross-agent consolidation.

Installation

npm install @agent-assistant/memory

Exports

import {
  createRelayBackedMemoryStore,
  createMemoryStore,
  InMemoryMemoryStoreAdapter,
  RelayMemoryStoreAdapter,
  retrieveTurnMemoryContext,
  promoteLatestSessionMemory,
} from '@agent-assistant/memory';

The package exports the public MemoryEntry, MemoryScope, MemoryStore, input/query types, and the error classes MemoryEntryNotFoundError, InvalidScopePromotionError, and CompactionError.

Quick Start

import { createMemoryStore, InMemoryMemoryStoreAdapter } from '@agent-assistant/memory';

const memory = createMemoryStore({
  adapter: new InMemoryMemoryStoreAdapter(),
});

const sessionEntry = await memory.write({
  scope: { kind: 'session', sessionId: 'sess-123' },
  content: 'User prefers concise responses.',
  tags: ['preference', 'style'],
  metadata: {
    agentId: 'assistant-1',
    source: 'capability:intake',
  },
});

const results = await memory.retrieve({
  scope: { kind: 'session', sessionId: 'sess-123' },
  tags: ['preference'],
});

const promoted = await memory.promote({
  sourceEntryId: sessionEntry.id,
  targetScope: { kind: 'user', userId: 'user-456' },
  deleteOriginal: true,
});

const compacted = await memory.compact({
  sourceEntryIds: [promoted.id],
  targetScope: { kind: 'user', userId: 'user-456' },
  compactionCallback: (entries) => entries.map((entry) => entry.content).join('\n'),
  tags: ['summary'],
});

Relay Reuse

V1 reuses the relay memory adapters (vendored from @agent-relay/memory 7.1.1 under src/relay-memory/):

  • InMemoryAdapter powers InMemoryMemoryStoreAdapter
  • any compatible relay MemoryAdapter can be wrapped with RelayMemoryStoreAdapter
  • relay add(), get(), update(), delete(), and list() remain the underlying storage operations

V1 intentionally does not use relay MemoryService or search():

  • MemoryService does not expose the full update/get/delete-by-scope surface the assistant package needs
  • semantic search is deferred; v1 retrieval is structured and recency-biased

Scope Mapping

Assistant scopes are encoded onto relay fields and metadata:

Assistant scopeRelay mapping
sessionsessionId + metadata._scopeKind = 'session'
usermetadata.userId + metadata._scopeKind = 'user'
workspaceprojectId + metadata._scopeKind = 'workspace'
orgmetadata.orgId + metadata._scopeKind = 'org'
objectmetadata.objectId + metadata.objectType + metadata._scopeKind = 'object'

The package also stores metadata._updatedAt because relay entries expose createdAt but no native updatedAt.

Retrieval Rules

retrieve() filters by:

  • exact scope match
  • all requested tags
  • since timestamp
  • expiry

includeNarrower: true is intentionally narrow in v1:

  • for a user scope query, the store also includes session entries only when context.sessionId is provided
  • there is no implicit session discovery
  • workspace/org fan-out is not implemented in v1

Retrieval uses relay list() and assistant-side filtering. Because relay list() is a recent-items primitive, v1 retrieval is recency-biased rather than a guaranteed exhaustive scan over all stored entries.

Promotion and Compaction

Promotion is upward only:

  • session -> user | workspace | org | object
  • user -> workspace | org
  • workspace -> org
  • object -> user | workspace | org

Compaction is bounded in v1:

  • all source entries must exist
  • all sources must share one scope
  • targetScope must match that source scope
  • the caller supplies the compaction callback
  • the package never makes a model call

Expiry and Provenance

expiresAt is stored in relay metadata and filtered out by get() and retrieve(). Expired records remain in the underlying adapter until explicitly deleted.

The package preserves assistant provenance metadata through writes, updates, promotion, and compaction. The intended fields are:

  • agentId
  • source
  • confidence
  • createdInSessionId
  • promotedFromId
  • compactedFromIds

Production Usage

import {
  createMemoryAdapter,
  createMemoryStore,
  RelayMemoryStoreAdapter,
} from '@agent-assistant/memory';

const relayAdapter = await createMemoryAdapter({
  type: 'supermemory',
  apiKey: process.env.SUPERMEMORY_API_KEY,
});

const memory = createMemoryStore({
  adapter: new RelayMemoryStoreAdapter(relayAdapter),
});

The wrapped relay adapter must implement list() and update(). The constructor rejects adapters that do not provide both methods.

Turn Memory Helpers

Consumers that build their own prompt formatting can use the raw turn helpers instead of reimplementing scoped retrieval and promotion:

import {
  createRelayBackedMemoryStore,
  retrieveTurnMemoryContext,
  promoteLatestSessionMemory,
} from '@agent-assistant/memory';

const { store, close } = await createRelayBackedMemoryStore({
  config: {
    type: 'supermemory',
    apiKey: process.env.SUPERMEMORY_API_KEY,
    defaultAgentId: 'sage',
    defaultProjectId: 'workspace-123',
  },
});

const context = await retrieveTurnMemoryContext({
  store,
  sessionId: 'thread-1',
  userId: 'user-1',
  workspaceId: 'workspace-123',
  tags: ['conversation'],
  limit: 5,
});

await promoteLatestSessionMemory({
  store,
  sessionId: 'thread-1',
  userId: 'user-1',
  tags: ['promotable'],
});

await close();

retrieveTurnMemoryContext() returns entries in session, user, then workspace order by default, deduped by entry id. promoteLatestSessionMemory() promotes the newest matching session entry to a user or caller-supplied target scope.

Isolation

packages/memory is runnable on its own:

  • npm install
  • npm run build
  • npm test

The package has no cloud requirement. InMemoryMemoryStoreAdapter is the default local/test backend.

Not In V1

  • semantic or embedding search
  • automatic memory-writing heuristics
  • session archival workflows
  • policy-gated access control
  • assistant-layer encryption
  • cross-agent consolidation, contradiction handling, or shared-team publication

MEMORY_PACKAGE_IMPLEMENTED

版本与发布记录

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

版本发布日期距今
0.4.37latest2026/10/041 个月前
0.4.362026/10/041 个月前
0.4.352026/05/235 个月前
0.4.332026/05/155 个月前
0.4.322026/05/125 个月前
0.4.312026/05/086 个月前

关于 @agent-assistant/memory 的常见问题

@agent-assistant/memory 是什么?

Assistant-scoped memory composition layer with in-memory and Supermemory adapters

如何安装 @agent-assistant/memory?

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

@agent-assistant/memory 支持 TypeScript 吗?

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

@agent-assistant/memory 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@agent-assistant/memory 可以商用吗?

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

@agent-assistant/memory 的下载量有多少?

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

@agent-assistant/memory 还在维护吗?

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