@mastra/pg 1.6.1 → 1.7.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.
Files changed (27) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/dist/docs/SKILL.md +40 -0
  3. package/dist/docs/assets/SOURCE_MAP.json +6 -0
  4. package/dist/docs/references/docs-memory-semantic-recall.md +272 -0
  5. package/dist/docs/references/docs-memory-storage.md +261 -0
  6. package/dist/docs/references/docs-memory-working-memory.md +400 -0
  7. package/dist/docs/references/docs-rag-overview.md +72 -0
  8. package/dist/docs/references/docs-rag-retrieval.md +515 -0
  9. package/dist/docs/references/docs-rag-vector-databases.md +645 -0
  10. package/dist/docs/references/reference-memory-memory-class.md +147 -0
  11. package/dist/docs/references/reference-processors-message-history-processor.md +85 -0
  12. package/dist/docs/references/reference-processors-semantic-recall-processor.md +117 -0
  13. package/dist/docs/references/reference-processors-working-memory-processor.md +152 -0
  14. package/dist/docs/references/reference-rag-metadata-filters.md +216 -0
  15. package/dist/docs/references/reference-storage-composite.md +235 -0
  16. package/dist/docs/references/reference-storage-dynamodb.md +282 -0
  17. package/dist/docs/references/reference-storage-postgresql.md +526 -0
  18. package/dist/docs/references/reference-tools-vector-query-tool.md +459 -0
  19. package/dist/docs/references/reference-vectors-pg.md +408 -0
  20. package/dist/index.cjs +62 -5
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.js +62 -5
  23. package/dist/index.js.map +1 -1
  24. package/dist/storage/db/index.d.ts.map +1 -1
  25. package/dist/storage/domains/memory/index.d.ts.map +1 -1
  26. package/dist/vector/index.d.ts.map +1 -1
  27. package/package.json +5 -5
package/CHANGELOG.md CHANGED
@@ -1,5 +1,79 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 1.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - `PgVector.query()` now supports querying by metadata filters alone without providing a query vector — useful when you need to retrieve records by metadata without performing similarity search. ([#13286](https://github.com/mastra-ai/mastra/pull/13286))
8
+
9
+ **Before** (queryVector was required):
10
+
11
+ ```ts
12
+ const results = await pgVector.query({
13
+ indexName: 'my-index',
14
+ queryVector: [0.1, 0.2, ...],
15
+ filter: { category: 'docs' },
16
+ });
17
+ ```
18
+
19
+ **After** (metadata-only query):
20
+
21
+ ```ts
22
+ const results = await pgVector.query({
23
+ indexName: 'my-index',
24
+ filter: { category: 'docs' },
25
+ });
26
+ // Returns matching records with score: 0 (no similarity ranking)
27
+ ```
28
+
29
+ At least one of `queryVector` or `filter` must be provided. When `queryVector` is omitted, results are returned with `score: 0` since no similarity computation is performed.
30
+
31
+ ### Patch Changes
32
+
33
+ - Set REPLICA IDENTITY USING INDEX on the mastra_workflow_snapshot table so PostgreSQL logical replication can track row updates. The table only has a UNIQUE constraint with no PRIMARY KEY, which caused "cannot update table because it does not have a replica identity and publishes updates" errors when logical replication was enabled. Fixes #13097. ([#13178](https://github.com/mastra-ai/mastra/pull/13178))
34
+
35
+ - Fixed observation activation to always preserve a minimum amount of context. Previously, swapping buffered observation chunks could unexpectedly drop the context window to near-zero tokens. ([#13476](https://github.com/mastra-ai/mastra/pull/13476))
36
+
37
+ - Updated dependencies [[`df170fd`](https://github.com/mastra-ai/mastra/commit/df170fd139b55f845bfd2de8488b16435bd3d0da), [`ae55343`](https://github.com/mastra-ai/mastra/commit/ae5534397fc006fd6eef3e4f80c235bcdc9289ef), [`c290cec`](https://github.com/mastra-ai/mastra/commit/c290cec5bf9107225de42942b56b487107aa9dce), [`f03e794`](https://github.com/mastra-ai/mastra/commit/f03e794630f812b56e95aad54f7b1993dc003add), [`aa4a5ae`](https://github.com/mastra-ai/mastra/commit/aa4a5aedb80d8d6837bab8cbb2e301215d1ba3e9), [`de3f584`](https://github.com/mastra-ai/mastra/commit/de3f58408752a8d80a295275c7f23fc306cf7f4f), [`d3fb010`](https://github.com/mastra-ai/mastra/commit/d3fb010c98f575f1c0614452667396e2653815f6), [`702ee1c`](https://github.com/mastra-ai/mastra/commit/702ee1c41be67cc532b4dbe89bcb62143508f6f0), [`f495051`](https://github.com/mastra-ai/mastra/commit/f495051eb6496a720f637fc85b6d69941c12554c), [`e622f1d`](https://github.com/mastra-ai/mastra/commit/e622f1d3ab346a8e6aca6d1fe2eac99bd961e50b), [`861f111`](https://github.com/mastra-ai/mastra/commit/861f11189211b20ddb70d8df81a6b901fc78d11e), [`00f43e8`](https://github.com/mastra-ai/mastra/commit/00f43e8e97a80c82b27d5bd30494f10a715a1df9), [`1b6f651`](https://github.com/mastra-ai/mastra/commit/1b6f65127d4a0d6c38d0a1055cb84527db529d6b), [`96a1702`](https://github.com/mastra-ai/mastra/commit/96a1702ce362c50dda20c8b4a228b4ad1a36a17a), [`cb9f921`](https://github.com/mastra-ai/mastra/commit/cb9f921320913975657abb1404855d8c510f7ac5), [`114e7c1`](https://github.com/mastra-ai/mastra/commit/114e7c146ac682925f0fb37376c1be70e5d6e6e5), [`1b6f651`](https://github.com/mastra-ai/mastra/commit/1b6f65127d4a0d6c38d0a1055cb84527db529d6b), [`72df4a8`](https://github.com/mastra-ai/mastra/commit/72df4a8f9bf1a20cfd3d9006a4fdb597ad56d10a)]:
38
+ - @mastra/core@1.8.0
39
+
40
+ ## 1.7.0-alpha.0
41
+
42
+ ### Minor Changes
43
+
44
+ - `PgVector.query()` now supports querying by metadata filters alone without providing a query vector — useful when you need to retrieve records by metadata without performing similarity search. ([#13286](https://github.com/mastra-ai/mastra/pull/13286))
45
+
46
+ **Before** (queryVector was required):
47
+
48
+ ```ts
49
+ const results = await pgVector.query({
50
+ indexName: 'my-index',
51
+ queryVector: [0.1, 0.2, ...],
52
+ filter: { category: 'docs' },
53
+ });
54
+ ```
55
+
56
+ **After** (metadata-only query):
57
+
58
+ ```ts
59
+ const results = await pgVector.query({
60
+ indexName: 'my-index',
61
+ filter: { category: 'docs' },
62
+ });
63
+ // Returns matching records with score: 0 (no similarity ranking)
64
+ ```
65
+
66
+ At least one of `queryVector` or `filter` must be provided. When `queryVector` is omitted, results are returned with `score: 0` since no similarity computation is performed.
67
+
68
+ ### Patch Changes
69
+
70
+ - Set REPLICA IDENTITY USING INDEX on the mastra_workflow_snapshot table so PostgreSQL logical replication can track row updates. The table only has a UNIQUE constraint with no PRIMARY KEY, which caused "cannot update table because it does not have a replica identity and publishes updates" errors when logical replication was enabled. Fixes #13097. ([#13178](https://github.com/mastra-ai/mastra/pull/13178))
71
+
72
+ - Fixed observation activation to always preserve a minimum amount of context. Previously, swapping buffered observation chunks could unexpectedly drop the context window to near-zero tokens. ([#13476](https://github.com/mastra-ai/mastra/pull/13476))
73
+
74
+ - Updated dependencies [[`df170fd`](https://github.com/mastra-ai/mastra/commit/df170fd139b55f845bfd2de8488b16435bd3d0da), [`ae55343`](https://github.com/mastra-ai/mastra/commit/ae5534397fc006fd6eef3e4f80c235bcdc9289ef), [`c290cec`](https://github.com/mastra-ai/mastra/commit/c290cec5bf9107225de42942b56b487107aa9dce), [`f03e794`](https://github.com/mastra-ai/mastra/commit/f03e794630f812b56e95aad54f7b1993dc003add), [`aa4a5ae`](https://github.com/mastra-ai/mastra/commit/aa4a5aedb80d8d6837bab8cbb2e301215d1ba3e9), [`de3f584`](https://github.com/mastra-ai/mastra/commit/de3f58408752a8d80a295275c7f23fc306cf7f4f), [`d3fb010`](https://github.com/mastra-ai/mastra/commit/d3fb010c98f575f1c0614452667396e2653815f6), [`702ee1c`](https://github.com/mastra-ai/mastra/commit/702ee1c41be67cc532b4dbe89bcb62143508f6f0), [`f495051`](https://github.com/mastra-ai/mastra/commit/f495051eb6496a720f637fc85b6d69941c12554c), [`e622f1d`](https://github.com/mastra-ai/mastra/commit/e622f1d3ab346a8e6aca6d1fe2eac99bd961e50b), [`861f111`](https://github.com/mastra-ai/mastra/commit/861f11189211b20ddb70d8df81a6b901fc78d11e), [`00f43e8`](https://github.com/mastra-ai/mastra/commit/00f43e8e97a80c82b27d5bd30494f10a715a1df9), [`1b6f651`](https://github.com/mastra-ai/mastra/commit/1b6f65127d4a0d6c38d0a1055cb84527db529d6b), [`96a1702`](https://github.com/mastra-ai/mastra/commit/96a1702ce362c50dda20c8b4a228b4ad1a36a17a), [`cb9f921`](https://github.com/mastra-ai/mastra/commit/cb9f921320913975657abb1404855d8c510f7ac5), [`114e7c1`](https://github.com/mastra-ai/mastra/commit/114e7c146ac682925f0fb37376c1be70e5d6e6e5), [`1b6f651`](https://github.com/mastra-ai/mastra/commit/1b6f65127d4a0d6c38d0a1055cb84527db529d6b), [`72df4a8`](https://github.com/mastra-ai/mastra/commit/72df4a8f9bf1a20cfd3d9006a4fdb597ad56d10a)]:
75
+ - @mastra/core@1.8.0-alpha.0
76
+
3
77
  ## 1.6.1
4
78
 
5
79
  ### Patch Changes
@@ -0,0 +1,40 @@
1
+ ---
2
+ name: mastra-pg
3
+ description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
4
+ metadata:
5
+ package: "@mastra/pg"
6
+ version: "1.7.0"
7
+ ---
8
+
9
+ ## When to use
10
+
11
+ Use this skill whenever you are working with @mastra/pg to obtain the domain-specific knowledge.
12
+
13
+ ## How to use
14
+
15
+ Read the individual reference documents for detailed explanations and code examples.
16
+
17
+ ### Docs
18
+
19
+ - [Semantic Recall](references/docs-memory-semantic-recall.md) - Learn how to use semantic recall in Mastra to retrieve relevant messages from past conversations using vector search and embeddings.
20
+ - [Storage](references/docs-memory-storage.md) - Configure storage for Mastra's memory system to persist conversations, workflows, and traces.
21
+ - [Working Memory](references/docs-memory-working-memory.md) - Learn how to configure working memory in Mastra to store persistent user data, preferences.
22
+ - [RAG (Retrieval-Augmented Generation) in Mastra](references/docs-rag-overview.md) - Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
23
+ - [Retrieval, Semantic Search, Reranking](references/docs-rag-retrieval.md) - Guide on retrieval processes in Mastra's RAG systems, including semantic search, filtering, and re-ranking.
24
+ - [Storing Embeddings in A Vector Database](references/docs-rag-vector-databases.md) - Guide on vector storage options in Mastra, including embedded and dedicated vector databases for similarity search.
25
+
26
+ ### Reference
27
+
28
+ - [Reference: Memory Class](references/reference-memory-memory-class.md) - Documentation for the `Memory` class in Mastra, which provides a robust system for managing conversation history and thread-based message storage.
29
+ - [Reference: Message History Processor](references/reference-processors-message-history-processor.md) - Documentation for the MessageHistory processor in Mastra, which handles retrieval and persistence of conversation history.
30
+ - [Reference: Semantic Recall Processor](references/reference-processors-semantic-recall-processor.md) - Documentation for the SemanticRecall processor in Mastra, which enables semantic search over conversation history using vector embeddings.
31
+ - [Reference: Working Memory Processor](references/reference-processors-working-memory-processor.md) - Documentation for the WorkingMemory processor in Mastra, which injects persistent user/context data as system instructions.
32
+ - [Reference: Metadata Filters](references/reference-rag-metadata-filters.md) - Documentation for metadata filtering capabilities in Mastra, which allow for precise querying of vector search results across different vector stores.
33
+ - [Reference: Composite Storage](references/reference-storage-composite.md) - Documentation for combining multiple storage backends in Mastra.
34
+ - [Reference: DynamoDB Storage](references/reference-storage-dynamodb.md) - Documentation for the DynamoDB storage implementation in Mastra, using a single-table design with ElectroDB.
35
+ - [Reference: PostgreSQL Storage](references/reference-storage-postgresql.md) - Documentation for the PostgreSQL storage implementation in Mastra.
36
+ - [Reference: createVectorQueryTool()](references/reference-tools-vector-query-tool.md) - Documentation for the Vector Query Tool in Mastra, which facilitates semantic search over vector stores with filtering and reranking capabilities.
37
+ - [Reference: PG Vector Store](references/reference-vectors-pg.md) - Documentation for the PgVector class in Mastra, which provides vector search using PostgreSQL with pgvector extension.
38
+
39
+
40
+ Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": "1.7.0",
3
+ "package": "@mastra/pg",
4
+ "exports": {},
5
+ "modules": {}
6
+ }
@@ -0,0 +1,272 @@
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
+ ![Diagram showing Mastra Memory semantic recall](/assets/images/semantic-recall-fd7b9336a6d0d18019216cb6d3dbe710.png)
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
+ ## Quick Start
20
+
21
+ Semantic recall is enabled by default, so if you give your agent memory it will be included:
22
+
23
+ ```typescript
24
+ import { Agent } from '@mastra/core/agent'
25
+ import { Memory } from '@mastra/memory'
26
+
27
+ const agent = new Agent({
28
+ id: 'support-agent',
29
+ name: 'SupportAgent',
30
+ instructions: 'You are a helpful support agent.',
31
+ model: 'openai/gpt-5.1',
32
+ memory: new Memory(),
33
+ })
34
+ ```
35
+
36
+ ## Using the recall() Method
37
+
38
+ 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 just recency, use `recall()` with a `vectorSearchString`:
39
+
40
+ ```typescript
41
+ const memory = await agent.getMemory()
42
+
43
+ // Basic recall - similar to listMessages
44
+ const { messages } = await memory!.recall({
45
+ threadId: 'thread-123',
46
+ perPage: 50,
47
+ })
48
+
49
+ // Semantic recall - find messages by meaning
50
+ const { messages: relevantMessages } = await memory!.recall({
51
+ threadId: 'thread-123',
52
+ vectorSearchString: 'What did we discuss about the project deadline?',
53
+ threadConfig: {
54
+ semanticRecall: true,
55
+ },
56
+ })
57
+ ```
58
+
59
+ ## Storage configuration
60
+
61
+ Semantic recall relies on a [storage and vector db](https://mastra.ai/reference/memory/memory-class) to store messages and their embeddings.
62
+
63
+ ```ts
64
+ import { Memory } from '@mastra/memory'
65
+ import { Agent } from '@mastra/core/agent'
66
+ import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
67
+
68
+ const agent = new Agent({
69
+ memory: new Memory({
70
+ // this is the default storage db if omitted
71
+ storage: new LibSQLStore({
72
+ id: 'agent-storage',
73
+ url: 'file:./local.db',
74
+ }),
75
+ // this is the default vector db if omitted
76
+ vector: new LibSQLVector({
77
+ id: 'agent-vector',
78
+ url: 'file:./local.db',
79
+ }),
80
+ }),
81
+ })
82
+ ```
83
+
84
+ Each vector store page below includes installation instructions, configuration parameters, and usage examples:
85
+
86
+ - [Astra](https://mastra.ai/reference/vectors/astra)
87
+ - [Chroma](https://mastra.ai/reference/vectors/chroma)
88
+ - [Cloudflare Vectorize](https://mastra.ai/reference/vectors/vectorize)
89
+ - [Convex](https://mastra.ai/reference/vectors/convex)
90
+ - [Couchbase](https://mastra.ai/reference/vectors/couchbase)
91
+ - [DuckDB](https://mastra.ai/reference/vectors/duckdb)
92
+ - [Elasticsearch](https://mastra.ai/reference/vectors/elasticsearch)
93
+ - [LanceDB](https://mastra.ai/reference/vectors/lance)
94
+ - [libSQL](https://mastra.ai/reference/vectors/libsql)
95
+ - [MongoDB](https://mastra.ai/reference/vectors/mongodb)
96
+ - [OpenSearch](https://mastra.ai/reference/vectors/opensearch)
97
+ - [Pinecone](https://mastra.ai/reference/vectors/pinecone)
98
+ - [PostgreSQL](https://mastra.ai/reference/vectors/pg)
99
+ - [Qdrant](https://mastra.ai/reference/vectors/qdrant)
100
+ - [S3 Vectors](https://mastra.ai/reference/vectors/s3vectors)
101
+ - [Turbopuffer](https://mastra.ai/reference/vectors/turbopuffer)
102
+ - [Upstash](https://mastra.ai/reference/vectors/upstash)
103
+
104
+ ## Recall configuration
105
+
106
+ The three main parameters that control semantic recall behavior are:
107
+
108
+ 1. **topK**: How many semantically similar messages to retrieve
109
+ 2. **messageRange**: How much surrounding context to include with each match
110
+ 3. **scope**: Whether to search within the current thread or across all threads owned by a resource (the default is resource scope).
111
+
112
+ ```typescript
113
+ const agent = new Agent({
114
+ memory: new Memory({
115
+ options: {
116
+ semanticRecall: {
117
+ topK: 3, // Retrieve 3 most similar messages
118
+ messageRange: 2, // Include 2 messages before and after each match
119
+ scope: 'resource', // Search across all threads for this user (default setting if omitted)
120
+ },
121
+ },
122
+ }),
123
+ })
124
+ ```
125
+
126
+ ## Embedder configuration
127
+
128
+ 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.
129
+
130
+ ### Using the Model Router (Recommended)
131
+
132
+ The simplest way is to use a `provider/model` string with autocomplete support:
133
+
134
+ ```ts
135
+ import { Memory } from '@mastra/memory'
136
+ import { Agent } from '@mastra/core/agent'
137
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
138
+
139
+ const agent = new Agent({
140
+ memory: new Memory({
141
+ embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
142
+ }),
143
+ })
144
+ ```
145
+
146
+ Supported embedding models:
147
+
148
+ - **OpenAI**: `text-embedding-3-small`, `text-embedding-3-large`, `text-embedding-ada-002`
149
+ - **Google**: `gemini-embedding-001`
150
+
151
+ The model router automatically handles API key detection from environment variables (`OPENAI_API_KEY`, `GOOGLE_GENERATIVE_AI_API_KEY`).
152
+
153
+ ### Using AI SDK Packages
154
+
155
+ You can also use AI SDK embedding models directly:
156
+
157
+ ```ts
158
+ import { Memory } from '@mastra/memory'
159
+ import { Agent } from '@mastra/core/agent'
160
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
161
+
162
+ const agent = new Agent({
163
+ memory: new Memory({
164
+ embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
165
+ }),
166
+ })
167
+ ```
168
+
169
+ ### Using FastEmbed (Local)
170
+
171
+ To use FastEmbed (a local embedding model), install `@mastra/fastembed`:
172
+
173
+ **npm**:
174
+
175
+ ```bash
176
+ npm install @mastra/fastembed@latest
177
+ ```
178
+
179
+ **pnpm**:
180
+
181
+ ```bash
182
+ pnpm add @mastra/fastembed@latest
183
+ ```
184
+
185
+ **Yarn**:
186
+
187
+ ```bash
188
+ yarn add @mastra/fastembed@latest
189
+ ```
190
+
191
+ **Bun**:
192
+
193
+ ```bash
194
+ bun add @mastra/fastembed@latest
195
+ ```
196
+
197
+ Then configure it in your memory:
198
+
199
+ ```ts
200
+ import { Memory } from '@mastra/memory'
201
+ import { Agent } from '@mastra/core/agent'
202
+ import { fastembed } from '@mastra/fastembed'
203
+
204
+ const agent = new Agent({
205
+ memory: new Memory({
206
+ embedder: fastembed,
207
+ }),
208
+ })
209
+ ```
210
+
211
+ ## PostgreSQL Index Optimization
212
+
213
+ 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.
214
+
215
+ 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.
216
+
217
+ ```typescript
218
+ import { Memory } from '@mastra/memory'
219
+ import { PgStore, PgVector } from '@mastra/pg'
220
+
221
+ const agent = new Agent({
222
+ memory: new Memory({
223
+ storage: new PgStore({
224
+ id: 'agent-storage',
225
+ connectionString: process.env.DATABASE_URL,
226
+ }),
227
+ vector: new PgVector({
228
+ id: 'agent-vector',
229
+ connectionString: process.env.DATABASE_URL,
230
+ }),
231
+ options: {
232
+ semanticRecall: {
233
+ topK: 5,
234
+ messageRange: 2,
235
+ indexConfig: {
236
+ type: 'hnsw', // Use HNSW for better performance
237
+ metric: 'dotproduct', // Best for OpenAI embeddings
238
+ m: 16, // Number of bi-directional links (default: 16)
239
+ efConstruction: 64, // Size of candidate list during construction (default: 64)
240
+ },
241
+ },
242
+ },
243
+ }),
244
+ })
245
+ ```
246
+
247
+ For detailed information about index configuration options and performance tuning, see the [PgVector configuration guide](https://mastra.ai/reference/vectors/pg).
248
+
249
+ ## Disabling
250
+
251
+ There is a performance impact to using semantic recall. New messages are converted into embeddings and used to query a vector database before new messages are sent to the LLM.
252
+
253
+ Semantic recall is enabled by default but can be disabled when not needed:
254
+
255
+ ```typescript
256
+ const agent = new Agent({
257
+ memory: new Memory({
258
+ options: {
259
+ semanticRecall: false,
260
+ },
261
+ }),
262
+ })
263
+ ```
264
+
265
+ You might want to disable semantic recall in scenarios like:
266
+
267
+ - When message history provides sufficient context for the current conversation.
268
+ - In performance-sensitive applications, like realtime two-way audio, where the added latency of creating embeddings and running vector queries is noticeable.
269
+
270
+ ## Viewing Recalled Messages
271
+
272
+ 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 database. Use a storage adapter for one of the [supported databases](#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 Mastra 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 creates the necessary tables on first interaction. See the [core schema](https://mastra.ai/reference/storage/overview) for details on what gets created, including tables for messages, threads, resources, workflows, traces, and evaluation datasets.
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 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 memory provider:
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 set the `memory` domain (and any other [domains](https://mastra.ai/reference/storage/composite) you need) 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 data boundaries or compliance requirements:
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:** [Mastra Cloud Store](https://mastra.ai/docs/mastra-cloud/deployment) doesn't support agent-level storage.
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/getting-started/studio) 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 does not 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-4o-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
+ ```