Agent Skills › TanStack/ai › tanstack-ai-memory-redis

tanstack-ai-memory-redis

GitHub

提供基于 Redis 的 AI 记忆持久化方案,支持 ioredis 和 node-redis 客户端接入。涵盖存储模型设计、客户端排名逻辑及故障排查,适用于构建具备上下文记忆的 AI 应用后端。

packages/ai-memory/skills/tanstack-ai-memory-redis/SKILL.md TanStack/ai

Trigger Scenarios

需要为 AI 应用添加 Redis 持久化记忆功能 配置 @tanstack/ai-memory 的 Redis 适配器

Install

npx skills add TanStack/ai --skill tanstack-ai-memory-redis -g -y
More Options

Non-standard path

npx skills add https://github.com/TanStack/ai/tree/main/packages/ai-memory/skills/tanstack-ai-memory-redis -g -y

Use without installing

npx skills use TanStack/ai@tanstack-ai-memory-redis

指定 Agent (Claude Code)

npx skills add TanStack/ai --skill tanstack-ai-memory-redis -a claude-code -g -y

安装 repo 全部 skill

npx skills add TanStack/ai --all -g -y

预览 repo 内 skill

npx skills add TanStack/ai --list

SKILL.md

Frontmatter
{
    "name": "tanstack-ai-memory-redis",
    "description": "Use when wiring redis() from @tanstack\/ai-memory\/redis in production — covers client setup (ioredis or node-redis via fromNodeRedis), the storage model, client-side ranking limits, and troubleshooting."
}

Redis Memory Adapter

Production-grade recall/save adapter backed by plain Redis (no vector index required). Ranks client-side (lexical + optional cosine + recency + importance).

Setup

Bring your own Redis client. ioredis wires in directly; redis (node-redis v4+) needs a small wrapper.

Option A: ioredis

import Redis from 'ioredis'
import { memoryMiddleware } from '@tanstack/ai-memory'
import { redis } from '@tanstack/ai-memory/redis'

const client = new Redis(process.env.REDIS_URL ?? 'redis://localhost:6379')
const memory = redis({ redis: client, prefix: 'myapp:memory' })

// Resolve scope per request from the server-validated session — never from req.body.
function memoryFor(session: { userId: string; threadId: string }) {
  return memoryMiddleware({
    adapter: memory,
    scope: { threadId: session.threadId, userId: session.userId },
  })
}

Option B: redis (node-redis v4+)

import { createClient } from 'redis'
import { memoryMiddleware } from '@tanstack/ai-memory'
import { redis, fromNodeRedis } from '@tanstack/ai-memory/redis'

const client = createClient({ url: process.env.REDIS_URL })
await client.connect()

const memory = redis({
  redis: fromNodeRedis(client),
  prefix: 'myapp:memory',
})

function memoryFor(session: { userId: string; threadId: string }) {
  return memoryMiddleware({
    adapter: memory,
    scope: { threadId: session.threadId, userId: session.userId },
  })
}

node-redis exposes a camelCase API (sAdd, mGet); fromNodeRedis translates it to the lowercase RedisLike shape. Passing a raw node-redis client without the wrapper throws client.sadd is not a function.

redis() accepts the same topK / minScore / kinds / embedder / extract options as inMemory().

Storage model

{prefix}:record:{id}                                          -> JSON record
{prefix}:index:{tenantId or _}:{userId or _}:{threadId}       -> Set<id>

save writes the record and adds it to the scope's index set; recall loads the set, scores, and renders. Scope values are escaped (:, \, and _) so a delimiter or the unset placeholder inside a dim can't collide two scopes.

Hard cut: there is no dual-read of older index layouts. If you previously wrote under a different shape (e.g. without tenantId), reindex or wipe — old keys are orphaned.

Always pass the same tenantId/userId/threadId on write and read: missing optional dims become _, so omit ≠ "match any".

Ranking limits

Ranking is client-side: recall loads every record for the scope into Node and scores it. Fine up to ~10k records per scope. Beyond that, write a vector-index-aware adapter against the same recall/save contract.

Troubleshooting

  • Records not visible across processes: ensure every process uses the same REDIS_URL and prefix.
  • Malformed JSON rows: a row whose JSON won't parse is skipped on read and left in place (never deleted) — the signal is a one-time console.warn per bad id. Fix or delete the offending {prefix}:record:{id} key to remediate.

Version History

  • 645757a Current 2026-09-22 03:42

    修正代码示例中的环境变量默认值,增加会话作用域参数说明,完善存储模型描述并补充故障排查内容。

  • 05280a5 2026-07-30 23:52

Same Skill Collection

.agents/skills/add-example-tutorial/SKILL.md
.agents/skills/gap-analysis/SKILL.md
.agents/skills/i-have-adhd/SKILL.md
.agents/skills/pr-description/SKILL.md
.claude/skills/add-example-tutorial/SKILL.md
.claude/skills/gap-analysis/SKILL.md
.claude/skills/i-have-adhd/SKILL.md
.claude/skills/pr-description/SKILL.md
.grok/skills/add-example-tutorial/SKILL.md
.grok/skills/gap-analysis/SKILL.md
.grok/skills/i-have-adhd/SKILL.md
.grok/skills/pr-description/SKILL.md
examples/ts-remix-chat/.agents/skills/remix/SKILL.md
packages/ai-code-mode/skills/ai-code-mode/SKILL.md
packages/ai-mcp/skills/ai-mcp/SKILL.md
packages/ai-memory/skills/tanstack-ai-memory-hindsight/SKILL.md
packages/ai-memory/skills/tanstack-ai-memory-honcho/SKILL.md
packages/ai-memory/skills/tanstack-ai-memory-in-memory/SKILL.md
packages/ai-memory/skills/tanstack-ai-memory-mem0/SKILL.md
packages/ai-memory/skills/tanstack-ai-memory/SKILL.md
packages/ai-persistence/skills/ai-persistence/build-cloudflare-adapter/SKILL.md
packages/ai-persistence/skills/ai-persistence/build-cloudflare-artifact-store/SKILL.md
packages/ai-persistence/skills/ai-persistence/build-custom-adapter/SKILL.md
packages/ai-persistence/skills/ai-persistence/build-drizzle-adapter/SKILL.md
packages/ai-persistence/skills/ai-persistence/build-prisma-adapter/SKILL.md
packages/ai-persistence/skills/ai-persistence/server/SKILL.md
packages/ai-persistence/skills/ai-persistence/SKILL.md
packages/ai-persistence/skills/ai-persistence/stores/SKILL.md
packages/ai/skills/ai-core/ag-ui-protocol/SKILL.md
packages/ai/skills/ai-core/chat-experience/SKILL.md
packages/ai/skills/ai-core/custom-backend-integration/SKILL.md
packages/ai/skills/ai-core/debug-logging/SKILL.md
packages/ai/skills/ai-core/locks/SKILL.md
packages/ai/skills/ai-core/middleware/SKILL.md
packages/ai/skills/ai-core/SKILL.md
packages/ai/skills/ai-core/tool-calling/SKILL.md
testing/panel/skills/emoji-storyteller/SKILL.md
testing/panel/skills/haiku/SKILL.md
testing/panel/skills/pirate-speak/SKILL.md
.agents/skills/bugfix-pr/SKILL.md
.agents/skills/docs/SKILL.md
.agents/skills/ponytail/SKILL.md
.agents/skills/pr-sweep/SKILL.md
.agents/skills/simple-english/SKILL.md
.agents/skills/triage-github/SKILL.md
.claude/skills/bugfix-pr/SKILL.md
.claude/skills/docs/SKILL.md
.claude/skills/ponytail/SKILL.md
.claude/skills/pr-sweep/SKILL.md

Metadata

Files
0
Version
3e30cde
Hash
1fdbbd83
Indexed
2026-07-30 23:52

Home - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-09-27 20:07
浙ICP备14020137号-1