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

telegram-agent-memory

v0.2.1

Published

File-first long-term memory for Telegram bots and group members

Readme

Telegram Agent Memory

A file-first long-term memory library for Telegram bots.

You only need one supported LLM provider API key. No external database is required. This package is intentionally scoped to Telegram memory. External vector databases, graph databases, and generic memory-platform APIs are not part of the public product contract.

By default, stable member profile memory is shared across groups for the same userId, while recent daily memory stays group-specific.

The default product flow is simple:

  • When a member sends a message, the LLM extracts what is worth remembering
  • Long-term member state is written to PROFILE.md
  • Recent events are written to memory/YYYY-MM-DD.md
  • Before replying, context() always includes stable profile memory and automatically adds relevant recent memory

Install

npm install telegram-agent-memory

Minimal Setup

import { createTelegramMemory } from 'telegram-agent-memory';

const memory = await createTelegramMemory({
  apiKey: process.env.OPENAI_API_KEY!,
  whatToRemember: `
  Remember:
  - user preferences
  - ongoing plans
  - important status changes

  Ignore:
  - greetings
  - filler chat
  - one-off small talk
  `,
});

Store Memory

await memory.remember('I like coffee and usually work in Beijing.', {
  userId: 'tg_user_123',
  groupId: 'tg_group_456',
  username: 'alice',
});

By default, this creates files like:

.telegram-agent-memory/
  users/
    tg_user_123/
      PROFILE.md
      PROFILE.json
  groups/
    tg_group_456/
      members/
        tg_user_123/
          INDEX.json
          memory/
            2026-04-10.md
            2026-04-10.json

users/<userId>/PROFILE.* is the stable cross-group member profile. groups/<groupId>/members/<userId>/memory/* is recent group-specific memory.

INDEX.json is an internal local embedding index used for automatic recall. It is managed by the library and does not require any external database.

Build Prompt Context

This is the main read path.

context() automatically:

  • includes stable member profile facts
  • searches recent daily memory with the current message
  • adds only relevant recent memory when it helps
const context = await memory.context('What should I know before replying?', {
  userId: 'tg_user_123',
  groupId: 'tg_group_456',
});

console.log(context.text);

The host bot does not need to decide when to use profile memory vs recent memory. The library decides that inside context().

Optional Recall

recall() still exists as a lower-level compatibility API if you want the ranked memory items directly.

Initialize From Environment

import { createTelegramMemoryFromEnv } from 'telegram-agent-memory';

const memory = await createTelegramMemoryFromEnv();

Common environment variables:

TELEGRAM_MEMORY_LLM_PROVIDER=openai
OPENAI_API_KEY=sk-...
OPENAI_MODEL=gpt-4o-mini
OPENAI_BASE_URL=
OPENAI_EMBEDDING_MODEL=text-embedding-3-small
OPENAI_HEADERS_JSON=
ANTHROPIC_API_KEY=
ANTHROPIC_MODEL=claude-3-5-haiku-latest
ANTHROPIC_BASE_URL=
ANTHROPIC_HEADERS_JSON=
TELEGRAM_MEMORY_DIR=./data/telegram-memory
TELEGRAM_MEMORY_POLICY=Remember user preferences and important status changes. Ignore filler chat.
TELEGRAM_MEMORY_RECENT_DAYS=3
TELEGRAM_MEMORY_MAX_PROFILE_FACTS=40
TELEGRAM_MEMORY_MAX_FACTS_PER_MESSAGE=8
TELEGRAM_MEMORY_SHARE_PROFILE_ACROSS_GROUPS=true

If TELEGRAM_MEMORY_DIR is not set, the default path is .telegram-agent-memory/ under the current working directory.

Public API

  • createTelegramMemory(...)
  • createTelegramMemoryFromEnv(...)
  • TelegramMemory
  • remember(...)
  • context(...)
  • recall(...) for lower-level access

Provider Compatibility

Built-in support today:

  • OpenAI directly
  • OpenAI-compatible APIs through OPENAI_BASE_URL
  • OpenRouter-style setups, including optional headers through OPENAI_HEADERS_JSON, OPENROUTER_HTTP_REFERER, and OPENROUTER_APP_NAME
  • Anthropic Claude through TELEGRAM_MEMORY_LLM_PROVIDER=anthropic plus ANTHROPIC_API_KEY

Embeddings are local by default. If you explicitly want OpenAI-compatible remote embeddings, set OPENAI_EMBEDDING_MODEL.

Evaluation

The repo includes fixture-based product tests for Telegram memory behavior:

  • extraction into profile vs daily
  • skip behavior for low-value messages
  • automatic recent-memory recall inside context()
  • user/group isolation
  • English, Chinese, and mixed-language message coverage

Run them with:

npm run format:check
npm run test:telegram-memory
npm run lint
npm run release:check

Design Boundaries

  • The library is file-first and does not use Qdrant, Neo4j, or another external vector database as part of the product path
  • INDEX.json is an internal local semantic index, not a public storage abstraction
  • The only supported public entrypoint is the Telegram-focused package root
  • Historical generic-memory modules now live under legacy/ and are not part of the npm surface, the default tests, or the supported architecture
  • The package is not designed for high-concurrency centralized shared storage or as a generic cross-channel memory backend
  • Stable profile memory is shared across groups by default; set TELEGRAM_MEMORY_SHARE_PROFILE_ACROSS_GROUPS=false only if you explicitly want group-isolated profiles

See docs/ARCHITECTURE.md for the explicit product boundary. For local release hygiene, see docs/RELEASE.md.

License

MIT