@mastra/pg 1.0.0-beta.11 → 1.0.0-beta.13

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.
@@ -0,0 +1,133 @@
1
+ # Memory API Reference
2
+
3
+ > API reference for memory - 1 entries
4
+
5
+
6
+ ---
7
+
8
+ ## Reference: Memory Class
9
+
10
+ > Documentation for the `Memory` class in Mastra, which provides a robust system for managing conversation history and thread-based message storage.
11
+
12
+ The `Memory` class provides a robust system for managing conversation history and thread-based message storage in Mastra. It enables persistent storage of conversations, semantic search capabilities, and efficient message retrieval. You must configure a storage provider for conversation history, and if you enable semantic recall you will also need to provide a vector store and embedder.
13
+
14
+ ## Usage example
15
+
16
+ ```typescript title="src/mastra/agents/test-agent.ts"
17
+ import { Memory } from "@mastra/memory";
18
+ import { Agent } from "@mastra/core/agent";
19
+
20
+ export const agent = new Agent({
21
+ name: "test-agent",
22
+ instructions: "You are an agent with memory.",
23
+ model: "openai/gpt-5.1",
24
+ memory: new Memory({
25
+ options: {
26
+ workingMemory: {
27
+ enabled: true,
28
+ },
29
+ },
30
+ }),
31
+ });
32
+ ```
33
+
34
+ > To enable `workingMemory` on an agent, you’ll need a storage provider configured on your main Mastra instance. See [Mastra class](../core/mastra-class) for more information.
35
+
36
+ ## Constructor parameters
37
+
38
+ ### Options parameters
39
+
40
+ ## Returns
41
+
42
+ ## Extended usage example
43
+
44
+ ```typescript title="src/mastra/agents/test-agent.ts"
45
+ import { Memory } from "@mastra/memory";
46
+ import { Agent } from "@mastra/core/agent";
47
+ import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
48
+
49
+ export const agent = new Agent({
50
+ name: "test-agent",
51
+ instructions: "You are an agent with memory.",
52
+ model: "openai/gpt-5.1",
53
+ memory: new Memory({
54
+ storage: new LibSQLStore({
55
+ id: 'test-agent-storage',
56
+ url: "file:./working-memory.db",
57
+ }),
58
+ vector: new LibSQLVector({
59
+ id: 'test-agent-vector',
60
+ url: "file:./vector-memory.db",
61
+ }),
62
+ options: {
63
+ lastMessages: 10,
64
+ semanticRecall: {
65
+ topK: 3,
66
+ messageRange: 2,
67
+ scope: "resource",
68
+ },
69
+ workingMemory: {
70
+ enabled: true,
71
+ },
72
+ generateTitle: true,
73
+ },
74
+ }),
75
+ });
76
+ ```
77
+
78
+ ## PostgreSQL with index configuration
79
+
80
+ ```typescript title="src/mastra/agents/pg-agent.ts"
81
+ import { Memory } from "@mastra/memory";
82
+ import { Agent } from "@mastra/core/agent";
83
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
84
+ import { PgStore, PgVector } from "@mastra/pg";
85
+
86
+ export const agent = new Agent({
87
+ name: "pg-agent",
88
+ instructions: "You are an agent with optimized PostgreSQL memory.",
89
+ model: "openai/gpt-5.1",
90
+ memory: new Memory({
91
+ storage: new PgStore({
92
+ id: 'pg-agent-storage',
93
+ connectionString: process.env.DATABASE_URL,
94
+ }),
95
+ vector: new PgVector({
96
+ id: 'pg-agent-vector',
97
+ connectionString: process.env.DATABASE_URL,
98
+ }),
99
+ embedder: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
100
+ options: {
101
+ lastMessages: 20,
102
+ semanticRecall: {
103
+ topK: 5,
104
+ messageRange: 3,
105
+ scope: "resource",
106
+ indexConfig: {
107
+ type: "hnsw", // Use HNSW for better performance
108
+ metric: "dotproduct", // Optimal for OpenAI embeddings
109
+ m: 16, // Number of bi-directional links
110
+ efConstruction: 64, // Construction-time candidate list size
111
+ },
112
+ },
113
+ workingMemory: {
114
+ enabled: true,
115
+ },
116
+ },
117
+ }),
118
+ });
119
+ ```
120
+
121
+ ### Related
122
+
123
+ - [Getting Started with Memory](https://mastra.ai/docs/v1/memory/overview)
124
+ - [Semantic Recall](https://mastra.ai/docs/v1/memory/semantic-recall)
125
+ - [Working Memory](https://mastra.ai/docs/v1/memory/working-memory)
126
+ - [Memory Processors](https://mastra.ai/docs/v1/memory/memory-processors)
127
+ - [createThread](https://mastra.ai/reference/v1/memory/createThread)
128
+ - [recall](https://mastra.ai/reference/v1/memory/recall)
129
+ - [getThreadById](https://mastra.ai/reference/v1/memory/getThreadById)
130
+ - [listThreadsByResourceId](https://mastra.ai/reference/v1/memory/listThreadsByResourceId)
131
+ - [deleteMessages](https://mastra.ai/reference/v1/memory/deleteMessages)
132
+ - [cloneThread](https://mastra.ai/reference/v1/memory/cloneThread)
133
+ - [Clone Utility Methods](https://mastra.ai/reference/v1/memory/clone-utilities)
@@ -0,0 +1,297 @@
1
+ # Processors API Reference
2
+
3
+ > API reference for processors - 3 entries
4
+
5
+
6
+ ---
7
+
8
+ ## Reference: Message History Processor
9
+
10
+ > Documentation for the MessageHistory processor in Mastra, which handles retrieval and persistence of conversation history.
11
+
12
+ The `MessageHistory` is a **hybrid processor** that handles both retrieval and persistence of message history. On input, it fetches historical messages from storage and prepends them to the conversation. On output, it persists new messages to storage.
13
+
14
+ ## Usage example
15
+
16
+ ```typescript
17
+ import { MessageHistory } from "@mastra/core/processors";
18
+
19
+ const processor = new MessageHistory({
20
+ storage: memoryStorage,
21
+ lastMessages: 50,
22
+ });
23
+ ```
24
+
25
+ ## Constructor parameters
26
+
27
+ ### Options
28
+
29
+ ## Returns
30
+
31
+ ## Extended usage example
32
+
33
+ ```typescript title="src/mastra/agents/memory-agent.ts"
34
+ import { Agent } from "@mastra/core/agent";
35
+ import { MessageHistory } from "@mastra/core/processors";
36
+ import { PostgresStorage } from "@mastra/pg";
37
+
38
+ const storage = new PostgresStorage({
39
+ connectionString: process.env.DATABASE_URL,
40
+ });
41
+
42
+ export const agent = new Agent({
43
+ name: "memory-agent",
44
+ instructions: "You are a helpful assistant with conversation memory",
45
+ model: "openai:gpt-4o",
46
+ inputProcessors: [
47
+ new MessageHistory({
48
+ storage,
49
+ lastMessages: 100,
50
+ }),
51
+ ],
52
+ outputProcessors: [
53
+ new MessageHistory({
54
+ storage,
55
+ }),
56
+ ],
57
+ });
58
+ ```
59
+
60
+ ## Behavior
61
+
62
+ ### Input processing
63
+ 1. Retrieves `threadId` from the request context
64
+ 2. Fetches historical messages from storage (ordered by creation date, descending)
65
+ 3. Filters out system messages (they should not be stored in the database)
66
+ 4. Merges historical messages with incoming messages, avoiding duplicates by ID
67
+ 5. Adds historical messages with `source: 'memory'` tag
68
+
69
+ ### Output processing
70
+ 1. Retrieves `threadId` from the request context
71
+ 2. Skips persistence if `readOnly` is set in memory config
72
+ 3. Filters out incomplete tool calls from messages
73
+ 4. Persists new user input and assistant response messages to storage
74
+ 5. Updates the thread's `updatedAt` timestamp
75
+
76
+ ## Related
77
+
78
+ - [Guardrails](https://mastra.ai/docs/v1/agents/guardrails)
79
+
80
+ ---
81
+
82
+ ## Reference: Semantic Recall Processor
83
+
84
+ > Documentation for the SemanticRecall processor in Mastra, which enables semantic search over conversation history using vector embeddings.
85
+
86
+ The `SemanticRecall` is a **hybrid processor** that enables semantic search over conversation history using vector embeddings. On input, it performs semantic search to find relevant historical messages. On output, it creates embeddings for new messages to enable future semantic retrieval.
87
+
88
+ ## Usage example
89
+
90
+ ```typescript
91
+ import { SemanticRecall } from "@mastra/core/processors";
92
+ import { openai } from "@ai-sdk/openai";
93
+
94
+ const processor = new SemanticRecall({
95
+ storage: memoryStorage,
96
+ vector: vectorStore,
97
+ embedder: openai.embedding("text-embedding-3-small"),
98
+ topK: 5,
99
+ messageRange: 2,
100
+ scope: "resource",
101
+ });
102
+ ```
103
+
104
+ ## Constructor parameters
105
+
106
+ ### Options
107
+
108
+ ## Returns
109
+
110
+ ## Extended usage example
111
+
112
+ ```typescript title="src/mastra/agents/semantic-memory-agent.ts"
113
+ import { Agent } from "@mastra/core/agent";
114
+ import { SemanticRecall, MessageHistory } from "@mastra/core/processors";
115
+ import { PostgresStorage } from "@mastra/pg";
116
+ import { PgVector } from "@mastra/pg";
117
+ import { openai } from "@ai-sdk/openai";
118
+
119
+ const storage = new PostgresStorage({
120
+ id: 'pg-storage',
121
+ connectionString: process.env.DATABASE_URL,
122
+ });
123
+
124
+ const vector = new PgVector({
125
+ id: 'pg-vector',
126
+ connectionString: process.env.DATABASE_URL,
127
+ });
128
+
129
+ const semanticRecall = new SemanticRecall({
130
+ storage,
131
+ vector,
132
+ embedder: openai.embedding("text-embedding-3-small"),
133
+ topK: 5,
134
+ messageRange: { before: 2, after: 1 },
135
+ scope: "resource",
136
+ threshold: 0.7,
137
+ });
138
+
139
+ export const agent = new Agent({
140
+ name: "semantic-memory-agent",
141
+ instructions: "You are a helpful assistant with semantic memory recall",
142
+ model: "openai:gpt-4o",
143
+ inputProcessors: [
144
+ semanticRecall,
145
+ new MessageHistory({ storage, lastMessages: 50 }),
146
+ ],
147
+ outputProcessors: [
148
+ semanticRecall,
149
+ new MessageHistory({ storage }),
150
+ ],
151
+ });
152
+ ```
153
+
154
+ ## Behavior
155
+
156
+ ### Input processing
157
+ 1. Extracts the user query from the last user message
158
+ 2. Generates embeddings for the query
159
+ 3. Performs vector search to find semantically similar messages
160
+ 4. Retrieves matched messages along with surrounding context (based on `messageRange`)
161
+ 5. For `scope: 'resource'`, formats cross-thread messages as a system message with timestamps
162
+ 6. Adds recalled messages with `source: 'memory'` tag
163
+
164
+ ### Output processing
165
+ 1. Extracts text content from new user and assistant messages
166
+ 2. Generates embeddings for each message
167
+ 3. Stores embeddings in the vector store with metadata (message ID, thread ID, resource ID, role, content, timestamp)
168
+ 4. Uses LRU caching for embeddings to avoid redundant API calls
169
+
170
+ ### Cross-thread recall
171
+ When `scope` is set to `'resource'`, the processor can recall messages from other threads. These cross-thread messages are formatted as a system message with timestamps and conversation labels to provide context about when and where the conversation occurred.
172
+
173
+ ## Related
174
+
175
+ - [Guardrails](https://mastra.ai/docs/v1/agents/guardrails)
176
+
177
+ ---
178
+
179
+ ## Reference: Working Memory Processor
180
+
181
+ > Documentation for the WorkingMemory processor in Mastra, which injects persistent user/context data as system instructions.
182
+
183
+ The `WorkingMemory` is an **input processor** that injects working memory data as a system message. It retrieves persistent information from storage and formats it as instructions for the LLM, enabling the agent to maintain context about users across conversations.
184
+
185
+ ## Usage example
186
+
187
+ ```typescript
188
+ import { WorkingMemory } from "@mastra/core/processors";
189
+
190
+ const processor = new WorkingMemory({
191
+ storage: memoryStorage,
192
+ scope: "resource",
193
+ template: {
194
+ format: "markdown",
195
+ content: `# User Profile
196
+ - **Name**:
197
+ - **Preferences**:
198
+ - **Goals**:
199
+ `,
200
+ },
201
+ });
202
+ ```
203
+
204
+ ## Constructor parameters
205
+
206
+ ### Options
207
+
208
+ ### WorkingMemoryTemplate
209
+
210
+ ## Returns
211
+
212
+ ## Extended usage example
213
+
214
+ ```typescript title="src/mastra/agents/personalized-agent.ts"
215
+ import { Agent } from "@mastra/core/agent";
216
+ import { WorkingMemory, MessageHistory } from "@mastra/core/processors";
217
+ import { PostgresStorage } from "@mastra/pg";
218
+
219
+ const storage = new PostgresStorage({
220
+ connectionString: process.env.DATABASE_URL,
221
+ });
222
+
223
+ export const agent = new Agent({
224
+ name: "personalized-agent",
225
+ instructions: "You are a helpful assistant that remembers user preferences",
226
+ model: "openai:gpt-4o",
227
+ inputProcessors: [
228
+ new WorkingMemory({
229
+ storage,
230
+ scope: "resource",
231
+ template: {
232
+ format: "markdown",
233
+ content: `# User Information
234
+ - **Name**:
235
+ - **Location**:
236
+ - **Preferences**:
237
+ - **Communication Style**:
238
+ - **Current Projects**:
239
+ `,
240
+ },
241
+ }),
242
+ new MessageHistory({ storage, lastMessages: 50 }),
243
+ ],
244
+ outputProcessors: [
245
+ new MessageHistory({ storage }),
246
+ ],
247
+ });
248
+ ```
249
+
250
+ ## JSON format example
251
+
252
+ ```typescript
253
+ import { WorkingMemory } from "@mastra/core/processors";
254
+
255
+ const processor = new WorkingMemory({
256
+ storage: memoryStorage,
257
+ scope: "resource",
258
+ template: {
259
+ format: "json",
260
+ content: JSON.stringify({
261
+ user: {
262
+ name: { type: "string" },
263
+ preferences: { type: "object" },
264
+ goals: { type: "array" },
265
+ },
266
+ }),
267
+ },
268
+ });
269
+ ```
270
+
271
+ ## Behavior
272
+
273
+ ### Input processing
274
+ 1. Retrieves `threadId` and `resourceId` from the request context
275
+ 2. Based on scope, fetches working memory from either:
276
+ - Thread metadata (`scope: 'thread'`)
277
+ - Resource record (`scope: 'resource'`)
278
+ 3. Resolves the template (from provider, options, or default)
279
+ 4. Generates system instructions that include:
280
+ - Guidelines for the LLM on storing and updating information
281
+ - The template structure
282
+ - Current working memory data
283
+ 5. Adds the instruction as a system message with `source: 'memory'` tag
284
+
285
+ ### Working memory updates
286
+ Working memory updates happen through the `updateWorkingMemory` tool provided by the Memory class, not through this processor. The processor only handles injecting the current working memory state into conversations.
287
+
288
+ ### Default template
289
+ If no template is provided, the processor uses a default markdown template with fields for:
290
+ - First Name, Last Name
291
+ - Location, Occupation
292
+ - Interests, Goals
293
+ - Events, Facts, Projects
294
+
295
+ ## Related
296
+
297
+ - [Guardrails](https://mastra.ai/docs/v1/agents/guardrails)
@@ -0,0 +1,74 @@
1
+ > Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
2
+
3
+ # RAG (Retrieval-Augmented Generation) in Mastra
4
+
5
+ RAG in Mastra helps you enhance LLM outputs by incorporating relevant context from your own data sources, improving accuracy and grounding responses in real information.
6
+
7
+ Mastra's RAG system provides:
8
+
9
+ - Standardized APIs to process and embed documents
10
+ - Support for multiple vector stores
11
+ - Chunking and embedding strategies for optimal retrieval
12
+ - Observability for tracking embedding and retrieval performance
13
+
14
+ ## Example
15
+
16
+ To implement RAG, you process your documents into chunks, create embeddings, store them in a vector database, and then retrieve relevant context at query time.
17
+
18
+ ```ts
19
+ import { embedMany } from "ai";
20
+ import { PgVector } from "@mastra/pg";
21
+ import { MDocument } from "@mastra/rag";
22
+ import { z } from "zod";
23
+
24
+ // 1. Initialize document
25
+ const doc = MDocument.fromText(`Your document text here...`);
26
+
27
+ // 2. Create chunks
28
+ const chunks = await doc.chunk({
29
+ strategy: "recursive",
30
+ size: 512,
31
+ overlap: 50,
32
+ });
33
+
34
+ // 3. Generate embeddings; we need to pass the text of each chunk
35
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
36
+
37
+ const { embeddings } = await embedMany({
38
+ values: chunks.map((chunk) => chunk.text),
39
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small")
40
+ });
41
+
42
+ // 4. Store in vector database
43
+ const pgVector = new PgVector({
44
+ id: 'pg-vector',
45
+ connectionString: process.env.POSTGRES_CONNECTION_STRING,
46
+ });
47
+ await pgVector.upsert({
48
+ indexName: "embeddings",
49
+ vectors: embeddings,
50
+ }); // using an index name of 'embeddings'
51
+
52
+ // 5. Query similar chunks
53
+ const results = await pgVector.query({
54
+ indexName: "embeddings",
55
+ queryVector: queryVector,
56
+ topK: 3,
57
+ }); // queryVector is the embedding of the query
58
+
59
+ console.log("Similar chunks:", results);
60
+ ```
61
+
62
+ This example shows the essentials: initialize a document, create chunks, generate embeddings, store them, and query for similar content.
63
+
64
+ ## Document Processing
65
+
66
+ The basic building block of RAG is document processing. Documents can be chunked using various strategies (recursive, sliding window, etc.) and enriched with metadata. See the [chunking and embedding doc](./chunking-and-embedding).
67
+
68
+ ## Vector Storage
69
+
70
+ Mastra supports multiple vector stores for embedding persistence and similarity search, including pgvector, Pinecone, Qdrant, and MongoDB. See the [vector database doc](./vector-databases).
71
+
72
+ ## More resources
73
+
74
+ - [Chain of Thought RAG Example](https://github.com/mastra-ai/mastra/tree/main/examples/basics/rag/cot-rag)