@mastra/libsql 0.0.0-error-handler-fix-20251020202607 → 0.0.0-everlasting-joggers-20260331153923
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 +3120 -3
- package/LICENSE.md +15 -0
- package/README.md +30 -20
- package/dist/docs/SKILL.md +46 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/docs-agents-agent-approval.md +509 -0
- package/dist/docs/references/docs-agents-networks.md +182 -0
- package/dist/docs/references/docs-memory-memory-processors.md +314 -0
- package/dist/docs/references/docs-memory-message-history.md +262 -0
- package/dist/docs/references/docs-memory-overview.md +240 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +297 -0
- package/dist/docs/references/docs-memory-storage.md +261 -0
- package/dist/docs/references/docs-memory-working-memory.md +400 -0
- package/dist/docs/references/docs-rag-retrieval.md +515 -0
- package/dist/docs/references/docs-workflows-snapshots.md +238 -0
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +140 -0
- package/dist/docs/references/reference-core-getMemory.md +49 -0
- package/dist/docs/references/reference-core-listMemory.md +55 -0
- package/dist/docs/references/reference-core-mastra-class.md +66 -0
- package/dist/docs/references/reference-memory-memory-class.md +145 -0
- package/dist/docs/references/reference-storage-composite.md +243 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-libsql.md +135 -0
- package/dist/docs/references/reference-vectors-libsql.md +305 -0
- package/dist/index.cjs +9006 -1972
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +8993 -1973
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +318 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/{domains → db}/utils.d.ts +21 -13
- package/dist/storage/db/utils.d.ts.map +1 -0
- package/dist/storage/domains/agents/index.d.ts +30 -0
- package/dist/storage/domains/agents/index.d.ts.map +1 -0
- package/dist/storage/domains/blobs/index.d.ts +17 -0
- package/dist/storage/domains/blobs/index.d.ts.map +1 -0
- package/dist/storage/domains/datasets/index.d.ts +43 -0
- package/dist/storage/domains/datasets/index.d.ts.map +1 -0
- package/dist/storage/domains/experiments/index.d.ts +31 -0
- package/dist/storage/domains/experiments/index.d.ts.map +1 -0
- package/dist/storage/domains/mcp-clients/index.d.ts +26 -0
- package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
- package/dist/storage/domains/mcp-servers/index.d.ts +26 -0
- package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +41 -53
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +41 -29
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts +25 -0
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts +26 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +19 -31
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/skills/index.d.ts +26 -0
- package/dist/storage/domains/skills/index.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +20 -32
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/domains/workspaces/index.d.ts +26 -0
- package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +68 -259
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +12 -4
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/sql-builder.d.ts.map +1 -1
- package/package.json +17 -13
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -18
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/operations/index.d.ts +0 -110
- package/dist/storage/domains/operations/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -21
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
- package/dist/storage/domains/utils.d.ts.map +0 -1
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# Semantic recall
|
|
2
|
+
|
|
3
|
+
If you ask your friend what they did last weekend, they will search in their memory for events associated with "last weekend" and then tell you what they did. That's sort of like how semantic recall works in Mastra.
|
|
4
|
+
|
|
5
|
+
> **Watch 📹:** What semantic recall is, how it works, and how to configure it in Mastra → [YouTube (5 minutes)](https://youtu.be/UVZtK8cK8xQ)
|
|
6
|
+
|
|
7
|
+
## How semantic recall works
|
|
8
|
+
|
|
9
|
+
Semantic recall is RAG-based search that helps agents maintain context across longer interactions when messages are no longer within [recent message history](https://mastra.ai/docs/memory/message-history).
|
|
10
|
+
|
|
11
|
+
It uses vector embeddings of messages for similarity search, integrates with various vector stores, and has configurable context windows around retrieved messages.
|
|
12
|
+
|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
When it's enabled, new messages are used to query a vector DB for semantically similar messages.
|
|
16
|
+
|
|
17
|
+
After getting a response from the LLM, all new messages (user, assistant, and tool calls/results) are inserted into the vector DB to be recalled in later interactions.
|
|
18
|
+
|
|
19
|
+
## Quickstart
|
|
20
|
+
|
|
21
|
+
Semantic recall is disabled by default. To enable it, set `semanticRecall: true` in `options` and provide a `vector` store and `embedder`:
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { Agent } from '@mastra/core/agent'
|
|
25
|
+
import { Memory } from '@mastra/memory'
|
|
26
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
27
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
28
|
+
|
|
29
|
+
const agent = new Agent({
|
|
30
|
+
id: 'support-agent',
|
|
31
|
+
name: 'SupportAgent',
|
|
32
|
+
instructions: 'You are a helpful support agent.',
|
|
33
|
+
model: 'openai/gpt-5.4',
|
|
34
|
+
memory: new Memory({
|
|
35
|
+
storage: new LibSQLStore({
|
|
36
|
+
id: 'agent-storage',
|
|
37
|
+
url: 'file:./local.db',
|
|
38
|
+
}),
|
|
39
|
+
vector: new LibSQLVector({
|
|
40
|
+
id: 'agent-vector',
|
|
41
|
+
url: 'file:./local.db',
|
|
42
|
+
}),
|
|
43
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
44
|
+
options: {
|
|
45
|
+
semanticRecall: true,
|
|
46
|
+
},
|
|
47
|
+
}),
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Using the `recall()` method
|
|
52
|
+
|
|
53
|
+
While `listMessages` retrieves messages by thread ID with basic pagination, [`recall()`](https://mastra.ai/reference/memory/recall) adds support for **semantic search**. When you need to find messages by meaning rather than recency, use `recall()` with a `vectorSearchString`:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
const memory = await agent.getMemory()
|
|
57
|
+
|
|
58
|
+
// Basic recall - similar to listMessages
|
|
59
|
+
const { messages } = await memory!.recall({
|
|
60
|
+
threadId: 'thread-123',
|
|
61
|
+
perPage: 50,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
// Semantic recall - find messages by meaning
|
|
65
|
+
const { messages: relevantMessages } = await memory!.recall({
|
|
66
|
+
threadId: 'thread-123',
|
|
67
|
+
vectorSearchString: 'What did we discuss about the project deadline?',
|
|
68
|
+
threadConfig: {
|
|
69
|
+
semanticRecall: true,
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Storage configuration
|
|
75
|
+
|
|
76
|
+
Semantic recall relies on a [storage and vector db](https://mastra.ai/reference/memory/memory-class) to store messages and their embeddings.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import { Memory } from '@mastra/memory'
|
|
80
|
+
import { Agent } from '@mastra/core/agent'
|
|
81
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
82
|
+
|
|
83
|
+
const agent = new Agent({
|
|
84
|
+
memory: new Memory({
|
|
85
|
+
// this is the default storage db if omitted
|
|
86
|
+
storage: new LibSQLStore({
|
|
87
|
+
id: 'agent-storage',
|
|
88
|
+
url: 'file:./local.db',
|
|
89
|
+
}),
|
|
90
|
+
// this is the default vector db if omitted
|
|
91
|
+
vector: new LibSQLVector({
|
|
92
|
+
id: 'agent-vector',
|
|
93
|
+
url: 'file:./local.db',
|
|
94
|
+
}),
|
|
95
|
+
options: {
|
|
96
|
+
semanticRecall: true,
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Each vector store page below includes installation instructions, configuration parameters, and usage examples:
|
|
103
|
+
|
|
104
|
+
- [Astra](https://mastra.ai/reference/vectors/astra)
|
|
105
|
+
- [Chroma](https://mastra.ai/reference/vectors/chroma)
|
|
106
|
+
- [Cloudflare Vectorize](https://mastra.ai/reference/vectors/vectorize)
|
|
107
|
+
- [Convex](https://mastra.ai/reference/vectors/convex)
|
|
108
|
+
- [Couchbase](https://mastra.ai/reference/vectors/couchbase)
|
|
109
|
+
- [DuckDB](https://mastra.ai/reference/vectors/duckdb)
|
|
110
|
+
- [Elasticsearch](https://mastra.ai/reference/vectors/elasticsearch)
|
|
111
|
+
- [LanceDB](https://mastra.ai/reference/vectors/lance)
|
|
112
|
+
- [libSQL](https://mastra.ai/reference/vectors/libsql)
|
|
113
|
+
- [MongoDB](https://mastra.ai/reference/vectors/mongodb)
|
|
114
|
+
- [OpenSearch](https://mastra.ai/reference/vectors/opensearch)
|
|
115
|
+
- [Pinecone](https://mastra.ai/reference/vectors/pinecone)
|
|
116
|
+
- [PostgreSQL](https://mastra.ai/reference/vectors/pg)
|
|
117
|
+
- [Qdrant](https://mastra.ai/reference/vectors/qdrant)
|
|
118
|
+
- [S3 Vectors](https://mastra.ai/reference/vectors/s3vectors)
|
|
119
|
+
- [Turbopuffer](https://mastra.ai/reference/vectors/turbopuffer)
|
|
120
|
+
- [Upstash](https://mastra.ai/reference/vectors/upstash)
|
|
121
|
+
|
|
122
|
+
## Recall configuration
|
|
123
|
+
|
|
124
|
+
The three main parameters that control semantic recall behavior are:
|
|
125
|
+
|
|
126
|
+
1. **topK**: How many semantically similar messages to retrieve
|
|
127
|
+
2. **messageRange**: How much surrounding context to include with each match
|
|
128
|
+
3. **scope**: Whether to search within the current thread or across all threads owned by a resource (the default is resource scope).
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const agent = new Agent({
|
|
132
|
+
memory: new Memory({
|
|
133
|
+
options: {
|
|
134
|
+
semanticRecall: {
|
|
135
|
+
topK: 3, // Retrieve 3 most similar messages
|
|
136
|
+
messageRange: 2, // Include 2 messages before and after each match
|
|
137
|
+
scope: 'resource', // Search across all threads for this user (default setting if omitted)
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
}),
|
|
141
|
+
})
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Embedder configuration
|
|
145
|
+
|
|
146
|
+
Semantic recall relies on an [embedding model](https://mastra.ai/reference/memory/memory-class) to convert messages into embeddings. Mastra supports embedding models through the model router using `provider/model` strings, or you can use any [embedding model](https://sdk.vercel.ai/docs/ai-sdk-core/embeddings) compatible with the AI SDK.
|
|
147
|
+
|
|
148
|
+
### Using the Model Router (Recommended)
|
|
149
|
+
|
|
150
|
+
The simplest way is to use a `provider/model` string with autocomplete support:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import { Memory } from '@mastra/memory'
|
|
154
|
+
import { Agent } from '@mastra/core/agent'
|
|
155
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
156
|
+
|
|
157
|
+
const agent = new Agent({
|
|
158
|
+
memory: new Memory({
|
|
159
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
160
|
+
options: {
|
|
161
|
+
semanticRecall: true,
|
|
162
|
+
},
|
|
163
|
+
}),
|
|
164
|
+
})
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Supported embedding models:
|
|
168
|
+
|
|
169
|
+
- **OpenAI**: `text-embedding-3-small`, `text-embedding-3-large`, `text-embedding-ada-002`
|
|
170
|
+
- **Google**: `gemini-embedding-001`
|
|
171
|
+
- **OpenRouter**: Access embedding models from various providers
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { Agent } from '@mastra/core/agent'
|
|
175
|
+
import { Memory } from '@mastra/memory'
|
|
176
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
177
|
+
|
|
178
|
+
const agent = new Agent({
|
|
179
|
+
memory: new Memory({
|
|
180
|
+
embedder: new ModelRouterEmbeddingModel({
|
|
181
|
+
providerId: 'openrouter',
|
|
182
|
+
modelId: 'openai/text-embedding-3-small',
|
|
183
|
+
}),
|
|
184
|
+
}),
|
|
185
|
+
})
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
The model router automatically handles API key detection from environment variables (`OPENAI_API_KEY`, `GOOGLE_GENERATIVE_AI_API_KEY`, `OPENROUTER_API_KEY`).
|
|
189
|
+
|
|
190
|
+
### Using AI SDK Packages
|
|
191
|
+
|
|
192
|
+
You can also use AI SDK embedding models directly:
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
import { Memory } from '@mastra/memory'
|
|
196
|
+
import { Agent } from '@mastra/core/agent'
|
|
197
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
198
|
+
|
|
199
|
+
const agent = new Agent({
|
|
200
|
+
memory: new Memory({
|
|
201
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
202
|
+
}),
|
|
203
|
+
})
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Using FastEmbed (local)
|
|
207
|
+
|
|
208
|
+
To use FastEmbed (a local embedding model), install `@mastra/fastembed`:
|
|
209
|
+
|
|
210
|
+
**npm**:
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
npm install @mastra/fastembed@latest
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
**pnpm**:
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
pnpm add @mastra/fastembed@latest
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Yarn**:
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
yarn add @mastra/fastembed@latest
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**Bun**:
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
bun add @mastra/fastembed@latest
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Then configure it in your memory:
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import { Memory } from '@mastra/memory'
|
|
238
|
+
import { Agent } from '@mastra/core/agent'
|
|
239
|
+
import { fastembed } from '@mastra/fastembed'
|
|
240
|
+
|
|
241
|
+
const agent = new Agent({
|
|
242
|
+
memory: new Memory({
|
|
243
|
+
embedder: fastembed,
|
|
244
|
+
}),
|
|
245
|
+
})
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## PostgreSQL index optimization
|
|
249
|
+
|
|
250
|
+
When using PostgreSQL as your vector store, you can optimize semantic recall performance by configuring the vector index. This is particularly important for large-scale deployments with thousands of messages.
|
|
251
|
+
|
|
252
|
+
PostgreSQL supports both IVFFlat and HNSW indexes. By default, Mastra creates an IVFFlat index, but HNSW indexes typically provide better performance, especially with OpenAI embeddings which use inner product distance.
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
import { Memory } from '@mastra/memory'
|
|
256
|
+
import { PgStore, PgVector } from '@mastra/pg'
|
|
257
|
+
|
|
258
|
+
const agent = new Agent({
|
|
259
|
+
memory: new Memory({
|
|
260
|
+
storage: new PgStore({
|
|
261
|
+
id: 'agent-storage',
|
|
262
|
+
connectionString: process.env.DATABASE_URL,
|
|
263
|
+
}),
|
|
264
|
+
vector: new PgVector({
|
|
265
|
+
id: 'agent-vector',
|
|
266
|
+
connectionString: process.env.DATABASE_URL,
|
|
267
|
+
}),
|
|
268
|
+
options: {
|
|
269
|
+
semanticRecall: {
|
|
270
|
+
topK: 5,
|
|
271
|
+
messageRange: 2,
|
|
272
|
+
indexConfig: {
|
|
273
|
+
type: 'hnsw', // Use HNSW for better performance
|
|
274
|
+
metric: 'dotproduct', // Best for OpenAI embeddings
|
|
275
|
+
m: 16, // Number of bi-directional links (default: 16)
|
|
276
|
+
efConstruction: 64, // Size of candidate list during construction (default: 64)
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
}),
|
|
281
|
+
})
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
For detailed information about index configuration options and performance tuning, see the [PgVector configuration guide](https://mastra.ai/reference/vectors/pg).
|
|
285
|
+
|
|
286
|
+
## Disable semantic recall
|
|
287
|
+
|
|
288
|
+
Semantic recall is disabled by default (`semanticRecall: false`). Each call adds latency because new messages are converted into embeddings and used to query a vector database before the LLM receives them.
|
|
289
|
+
|
|
290
|
+
Keep semantic recall disabled when:
|
|
291
|
+
|
|
292
|
+
- Message history provides sufficient context for the current conversation.
|
|
293
|
+
- You're building performance-sensitive applications, like realtime two-way audio, where embedding and vector query latency is noticeable.
|
|
294
|
+
|
|
295
|
+
## Viewing recalled messages
|
|
296
|
+
|
|
297
|
+
When tracing is enabled, any messages retrieved via semantic recall will appear in the agent's trace output, alongside recent message history (if configured).
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# Storage
|
|
2
|
+
|
|
3
|
+
For agents to remember previous interactions, Mastra needs a storage adapter. Use one of the [supported providers](#supported-providers) and pass it to your Mastra instance.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { Mastra } from '@mastra/core'
|
|
7
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
8
|
+
|
|
9
|
+
export const mastra = new Mastra({
|
|
10
|
+
storage: new LibSQLStore({
|
|
11
|
+
id: 'mastra-storage',
|
|
12
|
+
url: 'file:./mastra.db',
|
|
13
|
+
}),
|
|
14
|
+
})
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
> **Sharing the database with Studio:** When running `mastra dev` alongside your application (e.g., Next.js), use an absolute path to ensure both processes access the same database:
|
|
18
|
+
>
|
|
19
|
+
> ```typescript
|
|
20
|
+
> url: 'file:/absolute/path/to/your/project/mastra.db'
|
|
21
|
+
> ```
|
|
22
|
+
>
|
|
23
|
+
> Relative paths like `file:./mastra.db` resolve based on each process's working directory, which may differ.
|
|
24
|
+
|
|
25
|
+
This configures instance-level storage, which all agents share by default. You can also configure [agent-level storage](#agent-level-storage) for isolated data boundaries.
|
|
26
|
+
|
|
27
|
+
Mastra automatically initializes the necessary storage structures on first interaction. See [Storage Overview](https://mastra.ai/reference/storage/overview) for domain coverage and the schema used by the built-in database-backed domains.
|
|
28
|
+
|
|
29
|
+
## Supported providers
|
|
30
|
+
|
|
31
|
+
Each provider page includes installation instructions, configuration parameters, and usage examples:
|
|
32
|
+
|
|
33
|
+
- [libSQL](https://mastra.ai/reference/storage/libsql)
|
|
34
|
+
- [PostgreSQL](https://mastra.ai/reference/storage/postgresql)
|
|
35
|
+
- [MongoDB](https://mastra.ai/reference/storage/mongodb)
|
|
36
|
+
- [Upstash](https://mastra.ai/reference/storage/upstash)
|
|
37
|
+
- [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1)
|
|
38
|
+
- [Cloudflare KV & Durable Objects](https://mastra.ai/reference/storage/cloudflare)
|
|
39
|
+
- [Convex](https://mastra.ai/reference/storage/convex)
|
|
40
|
+
- [DynamoDB](https://mastra.ai/reference/storage/dynamodb)
|
|
41
|
+
- [LanceDB](https://mastra.ai/reference/storage/lance)
|
|
42
|
+
- [Microsoft SQL Server](https://mastra.ai/reference/storage/mssql)
|
|
43
|
+
|
|
44
|
+
> **Tip:** libSQL is the easiest way to get started because it doesn’t require running a separate database server.
|
|
45
|
+
|
|
46
|
+
## Configuration scope
|
|
47
|
+
|
|
48
|
+
Storage can be configured at the instance level (shared by all agents) or at the agent level (isolated to a specific agent).
|
|
49
|
+
|
|
50
|
+
### Instance-level storage
|
|
51
|
+
|
|
52
|
+
Add storage to your Mastra instance so all agents, workflows, observability traces, and scores share the same storage backend:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { Mastra } from '@mastra/core'
|
|
56
|
+
import { PostgresStore } from '@mastra/pg'
|
|
57
|
+
|
|
58
|
+
export const mastra = new Mastra({
|
|
59
|
+
storage: new PostgresStore({
|
|
60
|
+
id: 'mastra-storage',
|
|
61
|
+
connectionString: process.env.DATABASE_URL,
|
|
62
|
+
}),
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// Both agents inherit storage from the Mastra instance above
|
|
66
|
+
const agent1 = new Agent({ id: 'agent-1', memory: new Memory() })
|
|
67
|
+
const agent2 = new Agent({ id: 'agent-2', memory: new Memory() })
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
This is useful when all primitives share the same storage backend and have similar performance, scaling, and operational requirements.
|
|
71
|
+
|
|
72
|
+
#### Composite storage
|
|
73
|
+
|
|
74
|
+
[Composite storage](https://mastra.ai/reference/storage/composite) is an alternative way to configure instance-level storage. Use `MastraCompositeStore` to route `memory` and any other [supported domains](https://mastra.ai/reference/storage/composite) to different storage providers.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { Mastra } from '@mastra/core'
|
|
78
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
79
|
+
import { MemoryLibSQL } from '@mastra/libsql'
|
|
80
|
+
import { WorkflowsPG } from '@mastra/pg'
|
|
81
|
+
import { ObservabilityStorageClickhouse } from '@mastra/clickhouse'
|
|
82
|
+
|
|
83
|
+
export const mastra = new Mastra({
|
|
84
|
+
storage: new MastraCompositeStore({
|
|
85
|
+
id: 'composite',
|
|
86
|
+
domains: {
|
|
87
|
+
memory: new MemoryLibSQL({ url: 'file:./memory.db' }),
|
|
88
|
+
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
|
|
89
|
+
observability: new ObservabilityStorageClickhouse({
|
|
90
|
+
url: process.env.CLICKHOUSE_URL,
|
|
91
|
+
username: process.env.CLICKHOUSE_USERNAME,
|
|
92
|
+
password: process.env.CLICKHOUSE_PASSWORD,
|
|
93
|
+
}),
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
})
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
This is useful when different types of data have different performance or operational requirements, such as low-latency storage for memory, durable storage for workflows, and high-throughput storage for observability.
|
|
100
|
+
|
|
101
|
+
### Agent-level storage
|
|
102
|
+
|
|
103
|
+
Agent-level storage overrides storage configured at the instance level. Add storage to a specific agent when you need to keep data separate or use different providers per agent.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { Agent } from '@mastra/core/agent'
|
|
107
|
+
import { Memory } from '@mastra/memory'
|
|
108
|
+
import { PostgresStore } from '@mastra/pg'
|
|
109
|
+
|
|
110
|
+
export const agent = new Agent({
|
|
111
|
+
id: 'agent',
|
|
112
|
+
memory: new Memory({
|
|
113
|
+
storage: new PostgresStore({
|
|
114
|
+
id: 'agent-storage',
|
|
115
|
+
connectionString: process.env.AGENT_DATABASE_URL,
|
|
116
|
+
}),
|
|
117
|
+
}),
|
|
118
|
+
})
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
> **Warning:** Agent-level storage isn't supported when using [Mastra Cloud Store](https://mastra.ai/docs/mastra-cloud/deployment). If you use Mastra Cloud Store, configure storage on the Mastra instance instead. This limitation doesn't apply if you bring your own database.
|
|
122
|
+
|
|
123
|
+
## Threads and resources
|
|
124
|
+
|
|
125
|
+
Mastra organizes conversations using two identifiers:
|
|
126
|
+
|
|
127
|
+
- **Thread**: A conversation session containing a sequence of messages.
|
|
128
|
+
- **Resource**: The entity that owns the thread, such as a user, organization, project, or any other domain entity in your application.
|
|
129
|
+
|
|
130
|
+
Both identifiers are required for agents to store information:
|
|
131
|
+
|
|
132
|
+
**.generate()**:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
const response = await agent.generate('hello', {
|
|
136
|
+
memory: {
|
|
137
|
+
thread: 'conversation-abc-123',
|
|
138
|
+
resource: 'user_123',
|
|
139
|
+
},
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**.stream()**:
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
const stream = await agent.stream('hello', {
|
|
147
|
+
memory: {
|
|
148
|
+
thread: 'conversation-abc-123',
|
|
149
|
+
resource: 'user_123',
|
|
150
|
+
},
|
|
151
|
+
})
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
> **Note:** [Studio](https://mastra.ai/docs/studio/overview) automatically generates a thread and resource ID for you. When calling `stream()` or `generate()` yourself, remember to provide these identifiers explicitly.
|
|
155
|
+
|
|
156
|
+
### Thread title generation
|
|
157
|
+
|
|
158
|
+
Mastra can automatically generate descriptive thread titles based on the user's first message when `generateTitle` is enabled.
|
|
159
|
+
|
|
160
|
+
Use this option when implementing a ChatGPT-style chat interface to render a title alongside each thread in the conversation list (for example, in a sidebar) derived from the thread’s initial user message.
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
export const agent = new Agent({
|
|
164
|
+
id: 'agent',
|
|
165
|
+
memory: new Memory({
|
|
166
|
+
options: {
|
|
167
|
+
generateTitle: true,
|
|
168
|
+
},
|
|
169
|
+
}),
|
|
170
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Title generation runs asynchronously after the agent responds and doesn't affect response time.
|
|
174
|
+
|
|
175
|
+
To optimize cost or behavior, provide a smaller [`model`](https://mastra.ai/models) and custom `instructions`:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
export const agent = new Agent({
|
|
179
|
+
id: 'agent',
|
|
180
|
+
memory: new Memory({
|
|
181
|
+
options: {
|
|
182
|
+
generateTitle: {
|
|
183
|
+
model: 'openai/gpt-5-mini',
|
|
184
|
+
instructions: 'Generate a 1 word title',
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
}),
|
|
188
|
+
})
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Semantic recall
|
|
192
|
+
|
|
193
|
+
Semantic recall has different storage requirements - it needs a vector database in addition to the standard storage adapter. See [Semantic recall](https://mastra.ai/docs/memory/semantic-recall) for setup and supported vector providers.
|
|
194
|
+
|
|
195
|
+
## Handling large attachments
|
|
196
|
+
|
|
197
|
+
Some storage providers enforce record size limits that base64-encoded file attachments (such as images) can exceed:
|
|
198
|
+
|
|
199
|
+
| Provider | Record size limit |
|
|
200
|
+
| ------------------------------------------------------------------ | ----------------- |
|
|
201
|
+
| [DynamoDB](https://mastra.ai/reference/storage/dynamodb) | 400 KB |
|
|
202
|
+
| [Convex](https://mastra.ai/reference/storage/convex) | 1 MiB |
|
|
203
|
+
| [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1) | 1 MiB |
|
|
204
|
+
|
|
205
|
+
PostgreSQL, MongoDB, and libSQL have higher limits and are generally unaffected.
|
|
206
|
+
|
|
207
|
+
To avoid this, use an input processor to upload attachments to external storage (S3, R2, GCS, [Convex file storage](https://docs.convex.dev/file-storage), etc.) and replace them with URL references before persistence.
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import type { Processor } from '@mastra/core/processors'
|
|
211
|
+
import type { MastraDBMessage } from '@mastra/core/memory'
|
|
212
|
+
|
|
213
|
+
export class AttachmentUploader implements Processor {
|
|
214
|
+
id = 'attachment-uploader'
|
|
215
|
+
|
|
216
|
+
async processInput({ messages }: { messages: MastraDBMessage[] }) {
|
|
217
|
+
return Promise.all(messages.map(msg => this.processMessage(msg)))
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async processMessage(msg: MastraDBMessage) {
|
|
221
|
+
const attachments = msg.content.experimental_attachments
|
|
222
|
+
if (!attachments?.length) return msg
|
|
223
|
+
|
|
224
|
+
const uploaded = await Promise.all(
|
|
225
|
+
attachments.map(async att => {
|
|
226
|
+
// Skip if already a URL
|
|
227
|
+
if (!att.url?.startsWith('data:')) return att
|
|
228
|
+
|
|
229
|
+
// Upload base64 data and replace with URL
|
|
230
|
+
const url = await this.upload(att.url, att.contentType)
|
|
231
|
+
return { ...att, url }
|
|
232
|
+
}),
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
return { ...msg, content: { ...msg.content, experimental_attachments: uploaded } }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async upload(dataUri: string, contentType?: string): Promise<string> {
|
|
239
|
+
const base64 = dataUri.split(',')[1]
|
|
240
|
+
const buffer = Buffer.from(base64, 'base64')
|
|
241
|
+
|
|
242
|
+
// Replace with your storage provider (S3, R2, GCS, Convex, etc.)
|
|
243
|
+
// return await s3.upload(buffer, contentType);
|
|
244
|
+
throw new Error('Implement upload() with your storage provider')
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Use the processor with your agent:
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
import { Agent } from '@mastra/core/agent'
|
|
253
|
+
import { Memory } from '@mastra/memory'
|
|
254
|
+
import { AttachmentUploader } from './processors/attachment-uploader'
|
|
255
|
+
|
|
256
|
+
const agent = new Agent({
|
|
257
|
+
id: 'my-agent',
|
|
258
|
+
memory: new Memory({ storage: yourStorage }),
|
|
259
|
+
inputProcessors: [new AttachmentUploader()],
|
|
260
|
+
})
|
|
261
|
+
```
|