@mastra/memory 1.5.0 → 1.5.1
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/CHANGELOG.md +30 -0
- package/dist/{chunk-DF7NDDSM.js → chunk-6PKWQ3GH.js} +28 -11
- package/dist/chunk-6PKWQ3GH.js.map +1 -0
- package/dist/{chunk-LLTHE64H.cjs → chunk-6XVTMLW4.cjs} +28 -11
- package/dist/chunk-6XVTMLW4.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{observational-memory-ZNTAIUGT.js → observational-memory-AJWSMZVP.js} +3 -3
- package/dist/{observational-memory-ZNTAIUGT.js.map → observational-memory-AJWSMZVP.js.map} +1 -1
- package/dist/{observational-memory-4PCXEZIS.cjs → observational-memory-Q5TO525O.cjs} +17 -17
- package/dist/{observational-memory-4PCXEZIS.cjs.map → observational-memory-Q5TO525O.cjs.map} +1 -1
- package/dist/processors/index.cjs +15 -15
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/chunk-DF7NDDSM.js.map +0 -1
- package/dist/chunk-LLTHE64H.cjs.map +0 -1
- package/dist/docs/SKILL.md +0 -54
- package/dist/docs/assets/SOURCE_MAP.json +0 -103
- package/dist/docs/references/docs-agents-agent-approval.md +0 -377
- package/dist/docs/references/docs-agents-agent-memory.md +0 -212
- package/dist/docs/references/docs-agents-network-approval.md +0 -275
- package/dist/docs/references/docs-agents-networks.md +0 -290
- package/dist/docs/references/docs-memory-memory-processors.md +0 -316
- package/dist/docs/references/docs-memory-message-history.md +0 -260
- package/dist/docs/references/docs-memory-observational-memory.md +0 -246
- package/dist/docs/references/docs-memory-overview.md +0 -45
- package/dist/docs/references/docs-memory-semantic-recall.md +0 -272
- package/dist/docs/references/docs-memory-storage.md +0 -261
- package/dist/docs/references/docs-memory-working-memory.md +0 -400
- package/dist/docs/references/reference-core-getMemory.md +0 -50
- package/dist/docs/references/reference-core-listMemory.md +0 -56
- package/dist/docs/references/reference-memory-clone-utilities.md +0 -199
- package/dist/docs/references/reference-memory-cloneThread.md +0 -130
- package/dist/docs/references/reference-memory-createThread.md +0 -68
- package/dist/docs/references/reference-memory-getThreadById.md +0 -24
- package/dist/docs/references/reference-memory-listThreads.md +0 -145
- package/dist/docs/references/reference-memory-memory-class.md +0 -147
- package/dist/docs/references/reference-memory-observational-memory.md +0 -565
- package/dist/docs/references/reference-processors-token-limiter-processor.md +0 -113
- package/dist/docs/references/reference-storage-dynamodb.md +0 -282
- package/dist/docs/references/reference-storage-libsql.md +0 -135
- package/dist/docs/references/reference-storage-mongodb.md +0 -262
- package/dist/docs/references/reference-storage-postgresql.md +0 -529
- package/dist/docs/references/reference-storage-upstash.md +0 -160
- package/dist/docs/references/reference-vectors-libsql.md +0 -305
- package/dist/docs/references/reference-vectors-mongodb.md +0 -295
- package/dist/docs/references/reference-vectors-pg.md +0 -408
- package/dist/docs/references/reference-vectors-upstash.md +0 -294
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
# DynamoDB Storage
|
|
2
|
-
|
|
3
|
-
The DynamoDB storage implementation provides a scalable and performant NoSQL database solution for Mastra, leveraging a single-table design pattern with [ElectroDB](https://electrodb.dev/).
|
|
4
|
-
|
|
5
|
-
> **Observability Not Supported:** DynamoDB storage **does not support the observability domain**. Traces from the `DefaultExporter` cannot be persisted to DynamoDB, and Mastra Studio's observability features won't work with DynamoDB as your only storage provider. To enable observability, use [composite storage](https://mastra.ai/reference/storage/composite) to route observability data to a supported provider like ClickHouse or PostgreSQL.
|
|
6
|
-
|
|
7
|
-
> **Item Size Limit:** DynamoDB enforces a **400 KB maximum item size**. This limit can be exceeded when storing messages with base64-encoded attachments such as images. See [Handling large attachments](https://mastra.ai/docs/memory/storage) for workarounds including uploading attachments to external storage.
|
|
8
|
-
|
|
9
|
-
## Features
|
|
10
|
-
|
|
11
|
-
- Efficient single-table design for all Mastra storage needs
|
|
12
|
-
- Based on ElectroDB for type-safe DynamoDB access
|
|
13
|
-
- Support for AWS credentials, regions, and endpoints
|
|
14
|
-
- Compatible with AWS DynamoDB Local for development
|
|
15
|
-
- Stores Thread, Message, Eval, and Workflow data
|
|
16
|
-
- Optimized for serverless environments
|
|
17
|
-
- Configurable TTL (Time To Live) for automatic data expiration per entity type
|
|
18
|
-
|
|
19
|
-
## Installation
|
|
20
|
-
|
|
21
|
-
**npm**:
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
npm install @mastra/dynamodb@latest
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
**pnpm**:
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
pnpm add @mastra/dynamodb@latest
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
**Yarn**:
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
yarn add @mastra/dynamodb@latest
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
**Bun**:
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
bun add @mastra/dynamodb@latest
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Prerequisites
|
|
46
|
-
|
|
47
|
-
Before using this package, you **must** create a DynamoDB table with a specific structure, including primary keys and Global Secondary Indexes (GSIs). This adapter expects the DynamoDB table and its GSIs to be provisioned externally.
|
|
48
|
-
|
|
49
|
-
Detailed instructions for setting up the table using AWS CloudFormation or AWS CDK are available in [TABLE\_SETUP.md](https://github.com/mastra-ai/mastra/blob/main/stores/dynamodb/TABLE_SETUP.md). Please ensure your table is configured according to those instructions before proceeding.
|
|
50
|
-
|
|
51
|
-
## Usage
|
|
52
|
-
|
|
53
|
-
### Basic Usage
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
import { Memory } from "@mastra/memory";
|
|
57
|
-
import { DynamoDBStore } from "@mastra/dynamodb";
|
|
58
|
-
|
|
59
|
-
// Initialize the DynamoDB storage
|
|
60
|
-
const storage = new DynamoDBStore({
|
|
61
|
-
id: "dynamodb", // Unique identifier for this storage instance
|
|
62
|
-
config: {
|
|
63
|
-
tableName: "mastra-single-table", // Name of your DynamoDB table
|
|
64
|
-
region: "us-east-1", // Optional: AWS region, defaults to 'us-east-1'
|
|
65
|
-
// endpoint: "http://localhost:8000", // Optional: For local DynamoDB
|
|
66
|
-
// credentials: { accessKeyId: "YOUR_ACCESS_KEY", secretAccessKey: "YOUR_SECRET_KEY" } // Optional
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// Example: Initialize Memory with DynamoDB storage
|
|
71
|
-
const memory = new Memory({
|
|
72
|
-
storage,
|
|
73
|
-
options: {
|
|
74
|
-
lastMessages: 10,
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### Local Development with DynamoDB Local
|
|
80
|
-
|
|
81
|
-
For local development, you can use [DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html).
|
|
82
|
-
|
|
83
|
-
1. **Run DynamoDB Local (e.g., using Docker):**
|
|
84
|
-
|
|
85
|
-
```bash
|
|
86
|
-
docker run -p 8000:8000 amazon/dynamodb-local
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
2. **Configure `DynamoDBStore` to use the local endpoint:**
|
|
90
|
-
|
|
91
|
-
```typescript
|
|
92
|
-
import { DynamoDBStore } from "@mastra/dynamodb";
|
|
93
|
-
|
|
94
|
-
const storage = new DynamoDBStore({
|
|
95
|
-
id: "dynamodb-local",
|
|
96
|
-
config: {
|
|
97
|
-
tableName: "mastra-single-table", // Ensure this table is created in your local DynamoDB
|
|
98
|
-
region: "localhost", // Can be any string for local, 'localhost' is common
|
|
99
|
-
endpoint: "http://localhost:8000",
|
|
100
|
-
// For DynamoDB Local, credentials are not typically required unless configured.
|
|
101
|
-
// If you've configured local credentials:
|
|
102
|
-
// credentials: { accessKeyId: "fakeMyKeyId", secretAccessKey: "fakeSecretAccessKey" }
|
|
103
|
-
},
|
|
104
|
-
});
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
You will still need to create the table and GSIs in your local DynamoDB instance, for example, using the AWS CLI pointed to your local endpoint.
|
|
108
|
-
|
|
109
|
-
## Parameters
|
|
110
|
-
|
|
111
|
-
**id:** (`string`): Unique identifier for this storage instance.
|
|
112
|
-
|
|
113
|
-
**config.tableName:** (`string`): The name of your DynamoDB table.
|
|
114
|
-
|
|
115
|
-
**config.region?:** (`string`): AWS region. Defaults to 'us-east-1'. For local development, can be set to 'localhost' or similar.
|
|
116
|
-
|
|
117
|
-
**config.endpoint?:** (`string`): Custom endpoint for DynamoDB (e.g., 'http\://localhost:8000' for local development).
|
|
118
|
-
|
|
119
|
-
**config.credentials?:** (`object`): AWS credentials object with \`accessKeyId\` and \`secretAccessKey\`. If not provided, the AWS SDK will attempt to source credentials from environment variables, IAM roles (e.g., for EC2/Lambda), or the shared AWS credentials file.
|
|
120
|
-
|
|
121
|
-
**config.ttl?:** (`object`): TTL (Time To Live) configuration for automatic data expiration. Configure per entity type: thread, message, trace, eval, workflow\_snapshot, resource, score. Each entity config includes: enabled (boolean), attributeName (string, default: 'ttl'), defaultTtlSeconds (number).
|
|
122
|
-
|
|
123
|
-
## TTL (Time To Live) Configuration
|
|
124
|
-
|
|
125
|
-
DynamoDB TTL allows you to automatically delete items after a specified time period. This is useful for:
|
|
126
|
-
|
|
127
|
-
- **Cost optimization**: Automatically remove old data to reduce storage costs
|
|
128
|
-
- **Data lifecycle management**: Implement retention policies for compliance
|
|
129
|
-
- **Performance**: Prevent tables from growing indefinitely
|
|
130
|
-
- **Privacy compliance**: Automatically purge personal data after specified periods
|
|
131
|
-
|
|
132
|
-
### Enabling TTL
|
|
133
|
-
|
|
134
|
-
To use TTL, you must:
|
|
135
|
-
|
|
136
|
-
1. **Configure TTL in DynamoDBStore** (shown below)
|
|
137
|
-
2. **Enable TTL on your DynamoDB table** via AWS Console or CLI, specifying the attribute name (default: `ttl`)
|
|
138
|
-
|
|
139
|
-
```typescript
|
|
140
|
-
import { DynamoDBStore } from "@mastra/dynamodb";
|
|
141
|
-
|
|
142
|
-
const storage = new DynamoDBStore({
|
|
143
|
-
name: "dynamodb",
|
|
144
|
-
config: {
|
|
145
|
-
tableName: "mastra-single-table",
|
|
146
|
-
region: "us-east-1",
|
|
147
|
-
ttl: {
|
|
148
|
-
// Messages expire after 30 days
|
|
149
|
-
message: {
|
|
150
|
-
enabled: true,
|
|
151
|
-
defaultTtlSeconds: 30 * 24 * 60 * 60, // 30 days
|
|
152
|
-
},
|
|
153
|
-
// Threads expire after 90 days
|
|
154
|
-
thread: {
|
|
155
|
-
enabled: true,
|
|
156
|
-
defaultTtlSeconds: 90 * 24 * 60 * 60, // 90 days
|
|
157
|
-
},
|
|
158
|
-
// Traces expire after 7 days with custom attribute name
|
|
159
|
-
trace: {
|
|
160
|
-
enabled: true,
|
|
161
|
-
attributeName: "expiresAt", // Custom TTL attribute
|
|
162
|
-
defaultTtlSeconds: 7 * 24 * 60 * 60, // 7 days
|
|
163
|
-
},
|
|
164
|
-
// Workflow snapshots don't expire
|
|
165
|
-
workflow_snapshot: {
|
|
166
|
-
enabled: false,
|
|
167
|
-
},
|
|
168
|
-
},
|
|
169
|
-
},
|
|
170
|
-
});
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### Supported Entity Types
|
|
174
|
-
|
|
175
|
-
TTL can be configured for these entity types:
|
|
176
|
-
|
|
177
|
-
| Entity | Description |
|
|
178
|
-
| ------------------- | ------------------------ |
|
|
179
|
-
| `thread` | Conversation threads |
|
|
180
|
-
| `message` | Messages within threads |
|
|
181
|
-
| `trace` | Observability traces |
|
|
182
|
-
| `eval` | Evaluation results |
|
|
183
|
-
| `workflow_snapshot` | Workflow state snapshots |
|
|
184
|
-
| `resource` | User/resource data |
|
|
185
|
-
| `score` | Scoring results |
|
|
186
|
-
|
|
187
|
-
### TTL Entity Configuration
|
|
188
|
-
|
|
189
|
-
Each entity type accepts the following configuration:
|
|
190
|
-
|
|
191
|
-
**enabled:** (`boolean`): Whether TTL is enabled for this entity type.
|
|
192
|
-
|
|
193
|
-
**attributeName?:** (`string`): The DynamoDB attribute name to use for TTL. Must match the TTL attribute configured on your DynamoDB table. Defaults to 'ttl'.
|
|
194
|
-
|
|
195
|
-
**defaultTtlSeconds?:** (`number`): Default TTL in seconds from item creation time. Items will be automatically deleted by DynamoDB after this duration.
|
|
196
|
-
|
|
197
|
-
### Enabling TTL on Your DynamoDB Table
|
|
198
|
-
|
|
199
|
-
After configuring TTL in your code, you must enable TTL on the DynamoDB table itself:
|
|
200
|
-
|
|
201
|
-
**Using AWS CLI:**
|
|
202
|
-
|
|
203
|
-
```bash
|
|
204
|
-
aws dynamodb update-time-to-live \
|
|
205
|
-
--table-name mastra-single-table \
|
|
206
|
-
--time-to-live-specification "Enabled=true, AttributeName=ttl"
|
|
207
|
-
```
|
|
208
|
-
|
|
209
|
-
**Using AWS Console:**
|
|
210
|
-
|
|
211
|
-
1. Go to the DynamoDB console
|
|
212
|
-
2. Select your table
|
|
213
|
-
3. Go to "Additional settings" tab
|
|
214
|
-
4. Under "Time to Live (TTL)", click "Manage TTL"
|
|
215
|
-
5. Enable TTL and specify the attribute name (default: `ttl`)
|
|
216
|
-
|
|
217
|
-
> **Note**: DynamoDB deletes expired items within 48 hours after expiration. Items remain queryable until actually deleted.
|
|
218
|
-
|
|
219
|
-
## AWS IAM Permissions
|
|
220
|
-
|
|
221
|
-
The IAM role or user executing the code needs appropriate permissions to interact with the specified DynamoDB table and its indexes. Below is a sample policy. Replace `${YOUR_TABLE_NAME}` with your actual table name and `${YOUR_AWS_REGION}` and `${YOUR_AWS_ACCOUNT_ID}` with appropriate values.
|
|
222
|
-
|
|
223
|
-
```json
|
|
224
|
-
{
|
|
225
|
-
"Version": "2012-10-17",
|
|
226
|
-
"Statement": [
|
|
227
|
-
{
|
|
228
|
-
"Effect": "Allow",
|
|
229
|
-
"Action": [
|
|
230
|
-
"dynamodb:DescribeTable",
|
|
231
|
-
"dynamodb:GetItem",
|
|
232
|
-
"dynamodb:PutItem",
|
|
233
|
-
"dynamodb:UpdateItem",
|
|
234
|
-
"dynamodb:DeleteItem",
|
|
235
|
-
"dynamodb:Query",
|
|
236
|
-
"dynamodb:Scan",
|
|
237
|
-
"dynamodb:BatchGetItem",
|
|
238
|
-
"dynamodb:BatchWriteItem"
|
|
239
|
-
],
|
|
240
|
-
"Resource": [
|
|
241
|
-
"arn:aws:dynamodb:${YOUR_AWS_REGION}:${YOUR_AWS_ACCOUNT_ID}:table/${YOUR_TABLE_NAME}",
|
|
242
|
-
"arn:aws:dynamodb:${YOUR_AWS_REGION}:${YOUR_AWS_ACCOUNT_ID}:table/${YOUR_TABLE_NAME}/index/*"
|
|
243
|
-
]
|
|
244
|
-
}
|
|
245
|
-
]
|
|
246
|
-
}
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
## Key Considerations
|
|
250
|
-
|
|
251
|
-
Before diving into the architectural details, keep these key points in mind when working with the DynamoDB storage adapter:
|
|
252
|
-
|
|
253
|
-
- **External Table Provisioning:** This adapter _requires_ you to create and configure the DynamoDB table and its Global Secondary Indexes (GSIs) yourself, prior to using the adapter. Follow the guide in [TABLE\_SETUP.md](https://github.com/mastra-ai/mastra/blob/main/stores/dynamodb/TABLE_SETUP.md).
|
|
254
|
-
- **Single-Table Design:** All Mastra data (threads, messages, etc.) is stored in one DynamoDB table. This is a deliberate design choice optimized for DynamoDB, differing from relational database approaches.
|
|
255
|
-
- **Understanding GSIs:** Familiarity with how the GSIs are structured (as per `TABLE_SETUP.md`) is important for understanding data retrieval and potential query patterns.
|
|
256
|
-
- **ElectroDB:** The adapter uses ElectroDB to manage interactions with DynamoDB, providing a layer of abstraction and type safety over raw DynamoDB operations.
|
|
257
|
-
|
|
258
|
-
## Architectural Approach
|
|
259
|
-
|
|
260
|
-
This storage adapter utilizes a **single-table design pattern** leveraging [ElectroDB](https://electrodb.dev/), a common and recommended approach for DynamoDB. This differs architecturally from relational database adapters (like `@mastra/pg` or `@mastra/libsql`) that typically use multiple tables, each dedicated to a specific entity (threads, messages, etc.).
|
|
261
|
-
|
|
262
|
-
Key aspects of this approach:
|
|
263
|
-
|
|
264
|
-
- **DynamoDB Native:** The single-table design is optimized for DynamoDB's key-value and query capabilities, often leading to better performance and scalability compared to mimicking relational models.
|
|
265
|
-
- **External Table Management:** Unlike some adapters that might offer helper functions to create tables via code, this adapter **expects the DynamoDB table and its associated Global Secondary Indexes (GSIs) to be provisioned externally** before use. Please refer to [TABLE\_SETUP.md](https://github.com/mastra-ai/mastra/blob/main/stores/dynamodb/TABLE_SETUP.md) for detailed instructions using tools like AWS CloudFormation or CDK. The adapter focuses solely on interacting with the pre-existing table structure.
|
|
266
|
-
- **Consistency via Interface:** While the underlying storage model differs, this adapter adheres to the same `MastraStorage` interface as other adapters, ensuring it can be used interchangeably within the Mastra `Memory` component.
|
|
267
|
-
|
|
268
|
-
### Mastra Data in the Single Table
|
|
269
|
-
|
|
270
|
-
Within the single DynamoDB table, different Mastra data entities (such as Threads, Messages, Traces, Evals, and Workflows) are managed and distinguished using ElectroDB. ElectroDB defines specific models for each entity type, which include unique key structures and attributes. This allows the adapter to store and retrieve diverse data types efficiently within the same table.
|
|
271
|
-
|
|
272
|
-
For example, a `Thread` item might have a primary key like `THREAD#<threadId>`, while a `Message` item belonging to that thread might use `THREAD#<threadId>` as a partition key and `MESSAGE#<messageId>` as a sort key. The Global Secondary Indexes (GSIs), detailed in `TABLE_SETUP.md`, are strategically designed to support common access patterns across these different entities, such as fetching all messages for a thread or querying traces associated with a particular workflow.
|
|
273
|
-
|
|
274
|
-
### Advantages of Single-Table Design
|
|
275
|
-
|
|
276
|
-
This implementation uses a single-table design pattern with ElectroDB, which offers several advantages within the context of DynamoDB:
|
|
277
|
-
|
|
278
|
-
1. **Lower cost (potentially):** Fewer tables can simplify Read/Write Capacity Unit (RCU/WCU) provisioning and management, especially with on-demand capacity.
|
|
279
|
-
2. **Better performance:** Related data can be co-located or accessed efficiently through GSIs, enabling fast lookups for common access patterns.
|
|
280
|
-
3. **Simplified administration:** Fewer distinct tables to monitor, back up, and manage.
|
|
281
|
-
4. **Reduced complexity in access patterns:** ElectroDB helps manage the complexity of item types and access patterns on a single table.
|
|
282
|
-
5. **Transaction support:** DynamoDB transactions can be used across different "entity" types stored within the same table if needed.
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
# libSQL Storage
|
|
2
|
-
|
|
3
|
-
[libSQL](https://docs.turso.tech/libsql) is an open-source, SQLite-compatible database that supports both local and remote deployments. It can be used to store message history, workflow snapshots, traces, and eval scores.
|
|
4
|
-
|
|
5
|
-
For vectors like semantic recall or traditional RAG, use [libSQL Vector](https://mastra.ai/reference/vectors/libsql) which covers embeddings and vector search.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
Storage providers must be installed as separate packages:
|
|
10
|
-
|
|
11
|
-
**npm**:
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npm install @mastra/libsql@latest
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
**pnpm**:
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
pnpm add @mastra/libsql@latest
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
**Yarn**:
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
yarn add @mastra/libsql@latest
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
**Bun**:
|
|
30
|
-
|
|
31
|
-
```bash
|
|
32
|
-
bun add @mastra/libsql@latest
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## Usage
|
|
36
|
-
|
|
37
|
-
```typescript
|
|
38
|
-
import { LibSQLStore } from "@mastra/libsql";
|
|
39
|
-
import { Mastra } from "@mastra/core";
|
|
40
|
-
|
|
41
|
-
const mastra = new Mastra({
|
|
42
|
-
storage: new LibSQLStore({
|
|
43
|
-
id: 'libsql-storage',
|
|
44
|
-
url: "file:./storage.db",
|
|
45
|
-
}),
|
|
46
|
-
});
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
Agent-level file storage:
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
import { Memory } from "@mastra/memory";
|
|
53
|
-
import { Agent } from "@mastra/core/agent";
|
|
54
|
-
import { LibSQLStore } from "@mastra/libsql";
|
|
55
|
-
|
|
56
|
-
export const agent = new Agent({
|
|
57
|
-
id: "example-agent",
|
|
58
|
-
memory: new Memory({
|
|
59
|
-
storage: new LibSQLStore({
|
|
60
|
-
id: 'libsql-storage',
|
|
61
|
-
url: "file:./agent.db",
|
|
62
|
-
}),
|
|
63
|
-
}),
|
|
64
|
-
});
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
> **Warning:** File storage doesn't work with serverless platforms that have ephemeral file systems. For serverless deployments, use [Turso](https://turso.tech) or a different database engine.
|
|
68
|
-
|
|
69
|
-
Production with remote database:
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
storage: new LibSQLStore({
|
|
73
|
-
id: 'libsql-storage',
|
|
74
|
-
url: "libsql://your-db-name.aws-ap-northeast-1.turso.io",
|
|
75
|
-
authToken: process.env.TURSO_AUTH_TOKEN,
|
|
76
|
-
})
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
For local development and testing, you can store data in memory:
|
|
80
|
-
|
|
81
|
-
```typescript
|
|
82
|
-
storage: new LibSQLStore({
|
|
83
|
-
id: 'libsql-storage',
|
|
84
|
-
url: ":memory:",
|
|
85
|
-
})
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
> **Warning:** In-memory storage resets when the process changes. Only suitable for development.
|
|
89
|
-
|
|
90
|
-
## Options
|
|
91
|
-
|
|
92
|
-
**url:** (`string`): Database URL. Use \`:memory:\` for in-memory database, \`file:filename.db\` for a file database, or a libSQL connection string (e.g., \`libsql://your-database.turso.io\`) for remote storage.
|
|
93
|
-
|
|
94
|
-
**authToken?:** (`string`): Authentication token for remote libSQL databases.
|
|
95
|
-
|
|
96
|
-
## Initialization
|
|
97
|
-
|
|
98
|
-
When you pass storage to the Mastra class, `init()` is called automatically to create the [core schema](https://mastra.ai/reference/storage/overview):
|
|
99
|
-
|
|
100
|
-
```typescript
|
|
101
|
-
import { Mastra } from "@mastra/core";
|
|
102
|
-
import { LibSQLStore } from "@mastra/libsql";
|
|
103
|
-
|
|
104
|
-
const storage = new LibSQLStore({
|
|
105
|
-
id: 'libsql-storage',
|
|
106
|
-
url: "file:./storage.db",
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
const mastra = new Mastra({
|
|
110
|
-
storage, // init() called automatically
|
|
111
|
-
});
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
If using storage directly without Mastra, call `init()` explicitly:
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
import { LibSQLStore } from "@mastra/libsql";
|
|
118
|
-
|
|
119
|
-
const storage = new LibSQLStore({
|
|
120
|
-
id: 'libsql-storage',
|
|
121
|
-
url: "file:./storage.db",
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
await storage.init();
|
|
125
|
-
|
|
126
|
-
// Access domain-specific stores via getStore()
|
|
127
|
-
const memoryStore = await storage.getStore('memory');
|
|
128
|
-
const thread = await memoryStore?.getThreadById({ threadId: "..." });
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
## Observability
|
|
132
|
-
|
|
133
|
-
libSQL supports observability and is ideal for local development. Use the `realtime` [tracing strategy](https://mastra.ai/docs/observability/tracing/exporters/default) for immediate visibility while debugging.
|
|
134
|
-
|
|
135
|
-
For production environments with higher trace volumes, consider using [PostgreSQL](https://mastra.ai/reference/storage/postgresql) or [ClickHouse via composite storage](https://mastra.ai/reference/storage/composite).
|
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
# MongoDB Storage
|
|
2
|
-
|
|
3
|
-
The MongoDB storage implementation provides a scalable storage solution using MongoDB databases with support for both document storage and vector operations.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
**npm**:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install @mastra/mongodb@latest
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
**pnpm**:
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
pnpm add @mastra/mongodb@latest
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
**Yarn**:
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
yarn add @mastra/mongodb@latest
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
**Bun**:
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
bun add @mastra/mongodb@latest
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Usage
|
|
32
|
-
|
|
33
|
-
Ensure you have a MongoDB Atlas Local (via Docker) or MongoDB Atlas Cloud instance with Atlas Search enabled. MongoDB 7.0+ is recommended.
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
import { MongoDBStore } from "@mastra/mongodb";
|
|
37
|
-
|
|
38
|
-
const storage = new MongoDBStore({
|
|
39
|
-
id: 'mongodb-storage',
|
|
40
|
-
uri: process.env.MONGODB_URI,
|
|
41
|
-
dbName: process.env.MONGODB_DATABASE,
|
|
42
|
-
});
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Parameters
|
|
46
|
-
|
|
47
|
-
**id:** (`string`): Unique identifier for this storage instance.
|
|
48
|
-
|
|
49
|
-
**uri:** (`string`): MongoDB connection string (e.g., mongodb+srv://user:password\@cluster.mongodb.net)
|
|
50
|
-
|
|
51
|
-
**url?:** (`string`): Deprecated. Use uri instead. MongoDB connection string (supported for backward compatibility).
|
|
52
|
-
|
|
53
|
-
**dbName:** (`string`): The name of the database you want the storage to use.
|
|
54
|
-
|
|
55
|
-
**options?:** (`MongoClientOptions`): MongoDB client options for advanced configuration (SSL, connection pooling, etc.).
|
|
56
|
-
|
|
57
|
-
> **Deprecation Notice:** The `url` parameter is deprecated but still supported for backward compatibility. Please use `uri` instead in all new code.
|
|
58
|
-
|
|
59
|
-
## Constructor Examples
|
|
60
|
-
|
|
61
|
-
You can instantiate `MongoDBStore` in the following ways:
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
import { MongoDBStore } from "@mastra/mongodb";
|
|
65
|
-
|
|
66
|
-
// Basic connection without custom options
|
|
67
|
-
const store1 = new MongoDBStore({
|
|
68
|
-
id: 'mongodb-storage-01',
|
|
69
|
-
uri: "mongodb+srv://user:password@cluster.mongodb.net",
|
|
70
|
-
dbName: "mastra_storage",
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Using connection string with options
|
|
74
|
-
const store2 = new MongoDBStore({
|
|
75
|
-
id: 'mongodb-storage-02',
|
|
76
|
-
uri: "mongodb+srv://user:password@cluster.mongodb.net",
|
|
77
|
-
dbName: "mastra_storage",
|
|
78
|
-
options: {
|
|
79
|
-
retryWrites: true,
|
|
80
|
-
maxPoolSize: 10,
|
|
81
|
-
serverSelectionTimeoutMS: 5000,
|
|
82
|
-
socketTimeoutMS: 45000,
|
|
83
|
-
},
|
|
84
|
-
});
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## Additional Notes
|
|
88
|
-
|
|
89
|
-
### Collection Management
|
|
90
|
-
|
|
91
|
-
The storage implementation handles collection creation and management automatically. It creates the following collections:
|
|
92
|
-
|
|
93
|
-
- `mastra_workflow_snapshot`: Stores workflow state and execution data
|
|
94
|
-
- `mastra_evals`: Stores evaluation results and metadata
|
|
95
|
-
- `mastra_threads`: Stores conversation threads
|
|
96
|
-
- `mastra_messages`: Stores individual messages
|
|
97
|
-
- `mastra_traces`: Stores telemetry and tracing data
|
|
98
|
-
- `mastra_scorers`: Stores scoring and evaluation data
|
|
99
|
-
- `mastra_resources`: Stores resource working memory data
|
|
100
|
-
|
|
101
|
-
### Initialization
|
|
102
|
-
|
|
103
|
-
When you pass storage to the Mastra class, `init()` is called automatically before any storage operation:
|
|
104
|
-
|
|
105
|
-
```typescript
|
|
106
|
-
import { Mastra } from "@mastra/core";
|
|
107
|
-
import { MongoDBStore } from "@mastra/mongodb";
|
|
108
|
-
|
|
109
|
-
const storage = new MongoDBStore({
|
|
110
|
-
id: 'mongodb-storage',
|
|
111
|
-
uri: process.env.MONGODB_URI,
|
|
112
|
-
dbName: process.env.MONGODB_DATABASE,
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
const mastra = new Mastra({
|
|
116
|
-
storage, // init() is called automatically
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
If you're using storage directly without Mastra, you must call `init()` explicitly to create the collections:
|
|
121
|
-
|
|
122
|
-
```typescript
|
|
123
|
-
import { MongoDBStore } from "@mastra/mongodb";
|
|
124
|
-
|
|
125
|
-
const storage = new MongoDBStore({
|
|
126
|
-
id: 'mongodb-storage',
|
|
127
|
-
uri: process.env.MONGODB_URI,
|
|
128
|
-
dbName: process.env.MONGODB_DATABASE,
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// Required when using storage directly
|
|
132
|
-
await storage.init();
|
|
133
|
-
|
|
134
|
-
// Access domain-specific stores via getStore()
|
|
135
|
-
const memoryStore = await storage.getStore('memory');
|
|
136
|
-
const thread = await memoryStore?.getThreadById({ threadId: "..." });
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
> **Warning:** If `init()` is not called, collections won't be created and storage operations will fail silently or throw errors.
|
|
140
|
-
|
|
141
|
-
## Vector Search Capabilities
|
|
142
|
-
|
|
143
|
-
MongoDB storage includes built-in vector search capabilities for AI applications:
|
|
144
|
-
|
|
145
|
-
### Vector Index Creation
|
|
146
|
-
|
|
147
|
-
```typescript
|
|
148
|
-
import { MongoDBVector } from "@mastra/mongodb";
|
|
149
|
-
|
|
150
|
-
const vectorStore = new MongoDBVector({
|
|
151
|
-
id: 'mongodb-vector',
|
|
152
|
-
uri: process.env.MONGODB_URI,
|
|
153
|
-
dbName: process.env.MONGODB_DATABASE,
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
// Create a vector index for embeddings
|
|
157
|
-
await vectorStore.createIndex({
|
|
158
|
-
indexName: "document_embeddings",
|
|
159
|
-
dimension: 1536,
|
|
160
|
-
});
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
### Vector Operations
|
|
164
|
-
|
|
165
|
-
```typescript
|
|
166
|
-
// Store vectors with metadata
|
|
167
|
-
await vectorStore.upsert({
|
|
168
|
-
indexName: "document_embeddings",
|
|
169
|
-
vectors: [
|
|
170
|
-
{
|
|
171
|
-
id: "doc-1",
|
|
172
|
-
values: [0.1, 0.2, 0.3, ...], // 1536-dimensional vector
|
|
173
|
-
metadata: {
|
|
174
|
-
title: "Document Title",
|
|
175
|
-
category: "technical",
|
|
176
|
-
source: "api-docs",
|
|
177
|
-
},
|
|
178
|
-
},
|
|
179
|
-
],
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
// Similarity search
|
|
183
|
-
const results = await vectorStore.query({
|
|
184
|
-
indexName: "document_embeddings",
|
|
185
|
-
vector: queryEmbedding,
|
|
186
|
-
topK: 5,
|
|
187
|
-
filter: {
|
|
188
|
-
category: "technical",
|
|
189
|
-
},
|
|
190
|
-
});
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
## Usage Example
|
|
194
|
-
|
|
195
|
-
### Adding memory to an agent
|
|
196
|
-
|
|
197
|
-
To add MongoDB memory to an agent use the `Memory` class and create a new `storage` key using `MongoDBStore`. The configuration supports both local and remote MongoDB instances.
|
|
198
|
-
|
|
199
|
-
```typescript
|
|
200
|
-
import { Memory } from "@mastra/memory";
|
|
201
|
-
import { Agent } from "@mastra/core/agent";
|
|
202
|
-
import { MongoDBStore } from "@mastra/mongodb";
|
|
203
|
-
|
|
204
|
-
export const mongodbAgent = new Agent({
|
|
205
|
-
id: "mongodb-agent",
|
|
206
|
-
name: "mongodb-agent",
|
|
207
|
-
instructions:
|
|
208
|
-
"You are an AI agent with the ability to automatically recall memories from previous interactions.",
|
|
209
|
-
model: "openai/gpt-5.1",
|
|
210
|
-
memory: new Memory({
|
|
211
|
-
storage: new MongoDBStore({
|
|
212
|
-
uri: process.env.MONGODB_URI!,
|
|
213
|
-
dbName: process.env.MONGODB_DB_NAME!,
|
|
214
|
-
}),
|
|
215
|
-
options: {
|
|
216
|
-
generateTitle: true,
|
|
217
|
-
},
|
|
218
|
-
}),
|
|
219
|
-
});
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
### Using the agent
|
|
223
|
-
|
|
224
|
-
Use `memoryOptions` to scope recall for this request. Set `lastMessages: 5` to limit recency-based recall, and use `semanticRecall` to fetch the `topK: 3` most relevant messages, including `messageRange: 2` neighboring messages for context around each match.
|
|
225
|
-
|
|
226
|
-
```typescript
|
|
227
|
-
import "dotenv/config";
|
|
228
|
-
|
|
229
|
-
import { mastra } from "./mastra";
|
|
230
|
-
|
|
231
|
-
const threadId = "123";
|
|
232
|
-
const resourceId = "user-456";
|
|
233
|
-
|
|
234
|
-
const agent = mastra.getAgent("mongodbAgent");
|
|
235
|
-
|
|
236
|
-
const message = await agent.stream("My name is Mastra", {
|
|
237
|
-
memory: {
|
|
238
|
-
thread: threadId,
|
|
239
|
-
resource: resourceId,
|
|
240
|
-
},
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
await message.textStream.pipeTo(new WritableStream());
|
|
244
|
-
|
|
245
|
-
const stream = await agent.stream("What's my name?", {
|
|
246
|
-
memory: {
|
|
247
|
-
thread: threadId,
|
|
248
|
-
resource: resourceId,
|
|
249
|
-
},
|
|
250
|
-
memoryOptions: {
|
|
251
|
-
lastMessages: 5,
|
|
252
|
-
semanticRecall: {
|
|
253
|
-
topK: 3,
|
|
254
|
-
messageRange: 2,
|
|
255
|
-
},
|
|
256
|
-
},
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
for await (const chunk of stream.textStream) {
|
|
260
|
-
process.stdout.write(chunk);
|
|
261
|
-
}
|
|
262
|
-
```
|