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

@reaatech/agent-memory-retrieval

v0.1.0

Published

Semantic memory retrieval with re-ranking and diversification strategies

Readme

@reaatech/agent-memory-retrieval

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Semantic memory retrieval with pluggable ranking strategies and context injection for LLM prompts. The retriever combines embedding-based similarity with recency, importance, and topic diversification to surface the most relevant memories.

Installation

npm install @reaatech/agent-memory-retrieval
# or
pnpm add @reaatech/agent-memory-retrieval

Feature Overview

  • 5 retrieval strategies — Semantic, Recency, Importance, Topic-based, Adaptive (weighted ensemble)
  • Pluggable strategy system — implement RetrievalStrategyBase for custom ranking
  • Adaptive ensemble — weighted combination of multiple strategies
  • Context injection — format retrieved memories for insertion into LLM prompts with token budgeting
  • Metadata filtering — all strategies support type, importance, tag, and date filters
  • Dual ESM/CJS output — works with import and require

Quick Start

import {
  MemoryRetriever,
  ContextInjector,
  RetrievalStrategy,
} from '@reaatech/agent-memory-retrieval';
import { InMemoryMemoryStorage } from '@reaatech/agent-memory-storage';
import { OpenAIEmbeddingProvider } from '@reaatech/agent-memory-embedding';

const storage = new InMemoryMemoryStorage();
const embedder = new OpenAIEmbeddingProvider({
  apiKey: process.env.OPENAI_API_KEY,
  model: 'text-embedding-3-small',
});

const retriever = new MemoryRetriever(storage, embedder, {
  defaultLimit: 5,
  useCrossEncoder: false,
  diversityFactor: 0.3,
  strategies: [RetrievalStrategy.SEMANTIC, RetrievalStrategy.RECENCY],
});

// Retrieve relevant memories
const memories = await retriever.retrieve('What does the user like?', {
  limit: 5,
  tenantId: 'default',
  filters: { types: ['preference'] },
});

// Inject into LLM context
const injector = new ContextInjector();
const prompt = await injector.injectMemoriesIntoContext(
  conversationTurns,
  memories,
  4000,  // token budget
);

API Reference

MemoryRetriever (class)

Central retrieval orchestrator:

const retriever = new MemoryRetriever(
  storage,
  embeddingProvider,
  config,
  strategyOverrides?,  // optional custom strategy instances
);

| Method | Returns | Description | |--------|---------|-------------| | retrieve(context, options?) | Promise<Memory[]> | Query for relevant memories |

RetrievalConfig

| Property | Type | Default | Description | |----------|------|---------|-------------| | defaultLimit | number | 5 | Default result count | | useCrossEncoder | boolean | false | Enable cross-encoder re-ranking | | diversityFactor | number | 0.3 | 0–1, how much to penalize duplicates | | strategies | RetrievalStrategy[] | [SEMANTIC] | Active strategies in execution order |

RetrievalOptions

| Property | Type | Description | |----------|------|-------------| | limit | number | Max results | | filters | MetadataFilter | Type, importance, tag, date constraints | | tenantId | string | Tenant isolation | | useCrossEncoder | boolean | Per-query override | | diversityFactor | number | Per-query override | | strategy | RetrievalStrategy | Force a single strategy |

RetrievalStrategy (enum)

enum RetrievalStrategy {
  SEMANTIC = 'semantic',
  RECENCY = 'recency',
  IMPORTANCE = 'importance',
  TOPIC = 'topic',
  ADAPTIVE = 'adaptive',
}

Strategy Implementations

SemanticRetrievalStrategy

Embeds the query, searches by cosine similarity, returns top matches.

RecencyRetrievalStrategy

Ranks by createdAt (newest first), optionally combined with semantic scoring.

ImportanceRetrievalStrategy

Ranks by importance level (CRITICAL > HIGH > MEDIUM > LOW > TRANSIENT).

TopicBasedRetrievalStrategy

Groups results by category/tags to ensure diverse topic coverage.

AdaptiveRetrievalStrategy

Weighted ensemble of multiple strategies:

const adaptive = new AdaptiveRetrievalStrategy([
  { strategy: new SemanticRetrievalStrategy(), weight: 0.5 },
  { strategy: new RecencyRetrievalStrategy(), weight: 0.3 },
  { strategy: new ImportanceRetrievalStrategy(), weight: 0.2 },
]);

const retriever = new MemoryRetriever(storage, embedder, config, {
  [RetrievalStrategy.ADAPTIVE]: adaptive,
});

ContextInjector (class)

Formats retrieved memories for insertion into LLM prompts:

const injector = new ContextInjector(
  100000,  // max tokens (default: 100000)
  4,       // chars per token estimate (default: 4)
);

const prompt = await injector.injectMemoriesIntoContext(
  conversationTurns,
  memories,
  4000,  // optional: per-call token budget
);

Output format:

<relevant_memories>
The following information has been retrieved from long-term memory:

[FACT] User lives in Seattle (Confidence: 95%, Date: 12/15/2024)
[PREFERENCE] User prefers dark mode (Confidence: 90%, Date: 12/15/2024)
[FACT] User is a software engineer (Confidence: 85%, Date: 12/14/2024)

Note: Memories are labeled with their type and confidence level.
</relevant_memories>

Custom Strategies

Implement RetrievalStrategyBase:

import type {
  RetrievalStrategyBase,
  RetrievalOptions,
} from '@reaatech/agent-memory-retrieval';
import type { Memory } from '@reaatech/agent-memory-core';
import type { MemoryStorage } from '@reaatech/agent-memory-storage';
import type { EmbeddingProvider } from '@reaatech/agent-memory-embedding';

class MyStrategy implements RetrievalStrategyBase {
  async retrieve(
    context: string,
    options: RetrievalOptions,
    storage: MemoryStorage,
    embeddingProvider: EmbeddingProvider,
  ): Promise<Memory[]> {
    // Custom ranking logic
    const candidates = await storage.searchSimilar(
      await embeddingProvider.embed(context),
      { tenantId: options.tenantId ?? 'default', limit: 100 },
    );
    return candidates.filter((m) => m.confidence > 0.8);
  }
}

Related Packages

License

MIT