@mastra/redis 0.0.0
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 +223 -0
- package/package.json +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# @mastra/redis
|
|
2
|
+
|
|
3
|
+
Redis storage provider for Mastra that provides storage capabilities for direct Redis connections.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mastra/redis
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { RedisStore } from '@mastra/redis';
|
|
17
|
+
|
|
18
|
+
// Using connection string
|
|
19
|
+
const storage = new RedisStore({
|
|
20
|
+
id: 'my-storage',
|
|
21
|
+
connectionString: 'redis://localhost:6379',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Using host/port config
|
|
25
|
+
const storage = new RedisStore({
|
|
26
|
+
id: 'my-storage',
|
|
27
|
+
host: 'localhost',
|
|
28
|
+
port: 6379,
|
|
29
|
+
password: 'your-password',
|
|
30
|
+
db: 0,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Initialize (connects to Redis)
|
|
34
|
+
await storage.init();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### With Pre-configured Client
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { RedisStore } from '@mastra/redis';
|
|
41
|
+
import { createClient } from 'redis';
|
|
42
|
+
|
|
43
|
+
// Create a custom redis client with specific settings
|
|
44
|
+
const client = createClient({
|
|
45
|
+
url: 'redis://localhost:6379',
|
|
46
|
+
socket: {
|
|
47
|
+
reconnectStrategy: retries => Math.min(retries * 50, 2000),
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Connect the client before passing to RedisStore
|
|
52
|
+
await client.connect();
|
|
53
|
+
|
|
54
|
+
const storage = new RedisStore({
|
|
55
|
+
id: 'my-storage',
|
|
56
|
+
client,
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Parameters
|
|
61
|
+
|
|
62
|
+
| Parameter | Type | Description |
|
|
63
|
+
| ------------------ | ------------- | ------------------------------------------------------ |
|
|
64
|
+
| `id` | `string` | Unique identifier for the storage instance |
|
|
65
|
+
| `connectionString` | `string` | Redis connection URL (e.g., `redis://localhost:6379`) |
|
|
66
|
+
| `host` | `string` | Redis host address |
|
|
67
|
+
| `port` | `number` | Redis port (default: 6379) |
|
|
68
|
+
| `password` | `string` | Redis password for authentication |
|
|
69
|
+
| `db` | `number` | Redis database number (default: 0) |
|
|
70
|
+
| `client` | `RedisClient` | Pre-configured redis client (from the `redis` package) |
|
|
71
|
+
| `disableInit` | `boolean` | Disable automatic initialization |
|
|
72
|
+
|
|
73
|
+
## Accessing Storage Domains
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
// Access memory domain (threads, messages, resources)
|
|
77
|
+
const memory = await storage.getStore('memory');
|
|
78
|
+
await memory?.saveThread({ thread });
|
|
79
|
+
await memory?.saveMessages({ messages });
|
|
80
|
+
|
|
81
|
+
// Access workflows domain
|
|
82
|
+
const workflows = await storage.getStore('workflows');
|
|
83
|
+
await workflows?.persistWorkflowSnapshot({ workflowName, runId, snapshot });
|
|
84
|
+
|
|
85
|
+
// Access scores domain
|
|
86
|
+
const scores = await storage.getStore('scores');
|
|
87
|
+
await scores?.saveScore(score);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Usage with Mastra Agent
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { Memory } from '@mastra/memory';
|
|
94
|
+
import { Agent } from '@mastra/core/agent';
|
|
95
|
+
import { RedisStore } from '@mastra/redis';
|
|
96
|
+
|
|
97
|
+
export const redisAgent = new Agent({
|
|
98
|
+
id: 'redis-agent',
|
|
99
|
+
name: 'Redis Agent',
|
|
100
|
+
instructions: 'You are an AI agent with memory backed by Redis.',
|
|
101
|
+
model: 'openai/gpt-4',
|
|
102
|
+
memory: new Memory({
|
|
103
|
+
storage: new RedisStore({
|
|
104
|
+
id: 'redis-agent-storage',
|
|
105
|
+
connectionString: process.env.REDIS_URL!,
|
|
106
|
+
}),
|
|
107
|
+
options: {
|
|
108
|
+
generateTitle: true,
|
|
109
|
+
},
|
|
110
|
+
}),
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Accessing the Underlying Client
|
|
115
|
+
|
|
116
|
+
You can access the underlying redis client for advanced operations:
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
const storage = new RedisStore({
|
|
120
|
+
id: 'my-storage',
|
|
121
|
+
connectionString: 'redis://localhost:6379',
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
await storage.init();
|
|
125
|
+
|
|
126
|
+
// Get the redis client
|
|
127
|
+
const client = storage.getClient();
|
|
128
|
+
|
|
129
|
+
// Use for custom operations
|
|
130
|
+
await client.set('custom-key', 'value');
|
|
131
|
+
const value = await client.get('custom-key');
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Key Structure
|
|
135
|
+
|
|
136
|
+
The Redis storage uses the following key patterns:
|
|
137
|
+
|
|
138
|
+
- Threads: `mastra_threads:id:{threadId}`
|
|
139
|
+
- Messages: `mastra_messages:threadId:{threadId}:id:{messageId}`
|
|
140
|
+
- Message index: `msg-idx:{messageId}` (for fast lookups)
|
|
141
|
+
- Thread messages sorted set: `thread:{threadId}:messages`
|
|
142
|
+
- Workflow snapshots: `mastra_workflow_snapshot:namespace:{ns}:workflow_name:{name}:run_id:{id}`
|
|
143
|
+
- Scores: `mastra_scorers:id:{scoreId}`
|
|
144
|
+
- Resources: `mastra_resources:{resourceId}`
|
|
145
|
+
|
|
146
|
+
## Features
|
|
147
|
+
|
|
148
|
+
- Direct Redis connections via the official `redis` package (node-redis)
|
|
149
|
+
- Support for Redis Sentinel and Cluster (via custom client)
|
|
150
|
+
- Persistent storage for threads, messages, and resources
|
|
151
|
+
- Workflow state persistence with snapshot support
|
|
152
|
+
- Evaluation scores storage
|
|
153
|
+
- Sorted sets for message ordering
|
|
154
|
+
- Efficient batch operations with multi/exec
|
|
155
|
+
|
|
156
|
+
## Connection Options
|
|
157
|
+
|
|
158
|
+
### Standalone Redis
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
const storage = new RedisStore({
|
|
162
|
+
id: 'standalone',
|
|
163
|
+
host: 'localhost',
|
|
164
|
+
port: 6379,
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Redis with Password
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const storage = new RedisStore({
|
|
172
|
+
id: 'auth',
|
|
173
|
+
connectionString: 'redis://:password@localhost:6379',
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Redis Sentinel (via custom client)
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { createClient } from 'redis';
|
|
181
|
+
|
|
182
|
+
const client = createClient({
|
|
183
|
+
url: 'redis://localhost:26379',
|
|
184
|
+
// Configure sentinel options as needed
|
|
185
|
+
});
|
|
186
|
+
await client.connect();
|
|
187
|
+
|
|
188
|
+
const storage = new RedisStore({
|
|
189
|
+
id: 'sentinel',
|
|
190
|
+
client,
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Redis Cluster (via custom client)
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { RedisStore } from '@mastra/redis';
|
|
198
|
+
import { createCluster } from 'redis';
|
|
199
|
+
|
|
200
|
+
const cluster = createCluster({
|
|
201
|
+
rootNodes: [{ url: 'redis://node-1:6379' }, { url: 'redis://node-2:6379' }],
|
|
202
|
+
});
|
|
203
|
+
await cluster.connect();
|
|
204
|
+
|
|
205
|
+
const storage = new RedisStore({
|
|
206
|
+
id: 'cluster',
|
|
207
|
+
client: cluster,
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Closing Connections
|
|
212
|
+
|
|
213
|
+
Always close connections when done:
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
await storage.close();
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Related Links
|
|
220
|
+
|
|
221
|
+
- [Redis Documentation](https://redis.io/documentation)
|
|
222
|
+
- [node-redis Documentation](https://github.com/redis/node-redis)
|
|
223
|
+
- [Mastra Documentation](https://mastra.ai/docs)
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mastra/redis",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Redis storage provider for Mastra - provides storage capabilities for direct Redis connections",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"author": "Mauro Erta <mauro@vlkstudio.com>",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"pretest": "docker compose up -d",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"posttest": "docker compose down",
|
|
26
|
+
"lint": "eslint .",
|
|
27
|
+
"build:lib": "tsup --silent --config tsup.config.ts",
|
|
28
|
+
"build:docs": "pnpx tsx ../../scripts/generate-package-docs.ts stores/redis",
|
|
29
|
+
"build:watch": "tsup --watch --silent --config tsup.config.ts"
|
|
30
|
+
},
|
|
31
|
+
"license": "Apache-2.0",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"redis": "5.10.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@internal/lint": "workspace:*",
|
|
37
|
+
"@internal/storage-test-utils": "workspace:*",
|
|
38
|
+
"@internal/types-builder": "workspace:*",
|
|
39
|
+
"@mastra/core": "workspace:*",
|
|
40
|
+
"@types/node": "22.13.17",
|
|
41
|
+
"@vitest/coverage-v8": "catalog:",
|
|
42
|
+
"@vitest/ui": "catalog:",
|
|
43
|
+
"dotenv": "^17.0.0",
|
|
44
|
+
"eslint": "^9.37.0",
|
|
45
|
+
"tsup": "^8.5.0",
|
|
46
|
+
"typescript": "catalog:",
|
|
47
|
+
"vitest": "catalog:"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@mastra/core": ">=1.0.0-0 <2.0.0-0"
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"dist",
|
|
54
|
+
"CHANGELOG.md"
|
|
55
|
+
],
|
|
56
|
+
"homepage": "https://mastra.ai",
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "git+https://github.com/mastra-ai/mastra.git",
|
|
60
|
+
"directory": "stores/redis"
|
|
61
|
+
},
|
|
62
|
+
"bugs": {
|
|
63
|
+
"url": "https://github.com/mastra-ai/mastra/issues"
|
|
64
|
+
},
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": ">=22.13.0"
|
|
67
|
+
}
|
|
68
|
+
}
|