@tanstack/ai-memory 0.2.0 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +152 -0
- package/package.json +4 -4
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source
|
|
4
|
+
media="(prefers-color-scheme: dark)"
|
|
5
|
+
srcset="https://tanstack.com/api/readme/ai.png?theme=dark"
|
|
6
|
+
/>
|
|
7
|
+
<source
|
|
8
|
+
media="(prefers-color-scheme: light)"
|
|
9
|
+
srcset="https://tanstack.com/api/readme/ai.png"
|
|
10
|
+
/>
|
|
11
|
+
<img
|
|
12
|
+
src="https://tanstack.com/api/readme/ai.png"
|
|
13
|
+
alt="TanStack AI"
|
|
14
|
+
width="900"
|
|
15
|
+
/>
|
|
16
|
+
</picture>
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
<br />
|
|
20
|
+
|
|
21
|
+
# @tanstack/ai-memory
|
|
22
|
+
|
|
23
|
+
Pluggable memory adapters for TanStack AI's `memoryMiddleware`
|
|
24
|
+
|
|
25
|
+
`memoryMiddleware` gives a `chat()` call memory across turns and sessions: each turn it recalls relevant memory into the system prompt, then saves the finished user/assistant turn through an adapter. The package ships the middleware, the `MemoryAdapter` contract, the built-in adapters, and adapters for hosted memory services, each on its own subpath.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install @tanstack/ai-memory
|
|
31
|
+
# or
|
|
32
|
+
pnpm add @tanstack/ai-memory
|
|
33
|
+
# or
|
|
34
|
+
yarn add @tanstack/ai-memory
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### Wire `memoryMiddleware` into `chat()`
|
|
40
|
+
|
|
41
|
+
Start with the in-memory adapter, then swap it for a persistent one without changing anything else:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { chat } from '@tanstack/ai'
|
|
45
|
+
import { openaiText } from '@tanstack/ai-openai'
|
|
46
|
+
import { memoryMiddleware } from '@tanstack/ai-memory'
|
|
47
|
+
import { inMemory } from '@tanstack/ai-memory/in-memory'
|
|
48
|
+
|
|
49
|
+
const memory = inMemory()
|
|
50
|
+
|
|
51
|
+
const stream = chat({
|
|
52
|
+
adapter: openaiText('gpt-5.5'),
|
|
53
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
54
|
+
middleware: [
|
|
55
|
+
memoryMiddleware({
|
|
56
|
+
adapter: memory,
|
|
57
|
+
scope: { threadId: 'demo-thread', userId: 'alice' },
|
|
58
|
+
}),
|
|
59
|
+
],
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Derive scope server-side
|
|
64
|
+
|
|
65
|
+
`scope` is the isolation boundary. In a real app, derive it per request from server-validated session data, never from the request body:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
memoryMiddleware({
|
|
69
|
+
adapter: memory,
|
|
70
|
+
scope: (ctx) => {
|
|
71
|
+
const session = getSession(ctx)
|
|
72
|
+
return { threadId: session.threadId, userId: session.userId }
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Memory is entirely server-side; the client consumes the same stream as any other `chat()` endpoint.
|
|
78
|
+
|
|
79
|
+
## Adapters
|
|
80
|
+
|
|
81
|
+
| Adapter | Import | Backing store |
|
|
82
|
+
| ------------- | ------------------------------- | ---------------------------------------------------------- |
|
|
83
|
+
| `inMemory()` | `@tanstack/ai-memory/in-memory` | A `Map` in the current process. Development, tests, demos. |
|
|
84
|
+
| `redis()` | `@tanstack/ai-memory/redis` | Redis, via your own `ioredis` or `redis` client. |
|
|
85
|
+
| `hindsight()` | `@tanstack/ai-memory/hindsight` | A hosted Hindsight server. |
|
|
86
|
+
| `mem0()` | `@tanstack/ai-memory/mem0` | A mem0 server, over plain HTTP. |
|
|
87
|
+
| `honcho()` | `@tanstack/ai-memory/honcho` | A hosted Honcho server. |
|
|
88
|
+
|
|
89
|
+
### Redis
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import Redis from 'ioredis'
|
|
93
|
+
import { redis } from '@tanstack/ai-memory/redis'
|
|
94
|
+
|
|
95
|
+
const memory = redis({ redis: new Redis(process.env.REDIS_URL) })
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Using `redis` (node-redis) instead of `ioredis`? Wrap the client with `fromNodeRedis` from the same subpath.
|
|
99
|
+
|
|
100
|
+
### Semantic scoring
|
|
101
|
+
|
|
102
|
+
The built-in adapters score lexically by default. Pass an `embedder` for semantic recall when scopes grow large or queries don't share keywords with stored text:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import OpenAI from 'openai'
|
|
106
|
+
import { inMemory } from '@tanstack/ai-memory/in-memory'
|
|
107
|
+
|
|
108
|
+
const openai = new OpenAI()
|
|
109
|
+
|
|
110
|
+
const memory = inMemory({
|
|
111
|
+
embedder: {
|
|
112
|
+
async embed(text) {
|
|
113
|
+
const result = await openai.embeddings.create({
|
|
114
|
+
model: 'text-embedding-3-small',
|
|
115
|
+
input: text,
|
|
116
|
+
})
|
|
117
|
+
const embedding = result.data[0]?.embedding
|
|
118
|
+
if (!embedding) throw new Error('embedding request returned no vector')
|
|
119
|
+
return embedding
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Hosted services
|
|
126
|
+
|
|
127
|
+
`hindsight()`, `mem0()`, and `honcho()` map `recall`/`save` onto the vendor API. `@vectorize-io/hindsight-client` and `@honcho-ai/sdk` are optional peer dependencies, loaded lazily by their adapters; `mem0()` needs no SDK.
|
|
128
|
+
|
|
129
|
+
## Custom adapters
|
|
130
|
+
|
|
131
|
+
Implement the `MemoryAdapter` contract — a stable `id` plus `recall` and `save` — then prove it with the same contract suite the built-in adapters run:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { runMemoryAdapterContract } from '@tanstack/ai-memory/testkit'
|
|
135
|
+
import { myAdapter } from './my-adapter'
|
|
136
|
+
|
|
137
|
+
runMemoryAdapterContract('myAdapter', () => myAdapter())
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The testkit is a Vitest suite. `vitest` is an optional peer dependency, so install it in your project before importing `@tanstack/ai-memory/testkit`.
|
|
141
|
+
|
|
142
|
+
## Documentation
|
|
143
|
+
|
|
144
|
+
- [Overview](https://tanstack.com/ai/latest/docs/memory/overview): the `recall`/`save` contract, scope, and how a turn flows
|
|
145
|
+
- [Quickstart](https://tanstack.com/ai/latest/docs/memory/quickstart)
|
|
146
|
+
- [Adapters](https://tanstack.com/ai/latest/docs/memory/adapters): every adapter's options
|
|
147
|
+
- [Operating memory](https://tanstack.com/ai/latest/docs/memory/operating): options, telemetry, devtools events, and failures
|
|
148
|
+
- [Custom Adapter](https://tanstack.com/ai/latest/docs/memory/custom-adapter)
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-memory",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Pluggable memory adapters for TanStack AI memoryMiddleware",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"tanstack-intent"
|
|
74
74
|
],
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@tanstack/ai-event-client": "^0.
|
|
76
|
+
"@tanstack/ai-event-client": "^0.12.1"
|
|
77
77
|
},
|
|
78
78
|
"peerDependencies": {
|
|
79
79
|
"@honcho-ai/sdk": ">=2.0.0",
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"ioredis": ">=5.0.0",
|
|
82
82
|
"redis": ">=4.0.0",
|
|
83
83
|
"vitest": "^4.1.10",
|
|
84
|
-
"@tanstack/ai": "^0.
|
|
84
|
+
"@tanstack/ai": "^0.58.0"
|
|
85
85
|
},
|
|
86
86
|
"peerDependenciesMeta": {
|
|
87
87
|
"ioredis": {
|
|
@@ -107,7 +107,7 @@
|
|
|
107
107
|
"ioredis-mock": "^8.9.0",
|
|
108
108
|
"redis": "^4.7.0",
|
|
109
109
|
"vitest": "^4.1.10",
|
|
110
|
-
"@tanstack/ai": "0.
|
|
110
|
+
"@tanstack/ai": "0.58.0"
|
|
111
111
|
},
|
|
112
112
|
"scripts": {
|
|
113
113
|
"build": "vite build",
|