@mastra/libsql 0.0.0-structured-output-issue-20260227214155 → 0.0.0-structured-output-errors-20260409185629
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 +344 -3
- package/LICENSE.md +15 -0
- package/dist/docs/SKILL.md +15 -19
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-agents-agent-approval.md +136 -185
- package/dist/docs/references/docs-agents-networks.md +90 -207
- package/dist/docs/references/docs-memory-memory-processors.md +15 -15
- package/dist/docs/references/docs-memory-message-history.md +10 -8
- package/dist/docs/references/docs-memory-overview.md +219 -24
- package/dist/docs/references/docs-memory-semantic-recall.md +54 -29
- package/dist/docs/references/docs-memory-storage.md +14 -16
- package/dist/docs/references/docs-memory-working-memory.md +22 -22
- package/dist/docs/references/docs-rag-retrieval.md +16 -16
- package/dist/docs/references/docs-workflows-snapshots.md +1 -1
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +3 -3
- package/dist/docs/references/reference-core-getMemory.md +4 -5
- package/dist/docs/references/reference-core-listMemory.md +3 -4
- package/dist/docs/references/reference-core-mastra-class.md +18 -18
- package/dist/docs/references/reference-memory-memory-class.md +16 -18
- package/dist/docs/references/reference-storage-composite.md +19 -11
- package/dist/docs/references/reference-storage-dynamodb.md +16 -16
- package/dist/docs/references/reference-storage-libsql.md +3 -3
- package/dist/docs/references/reference-vectors-libsql.md +47 -47
- package/dist/index.cjs +512 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +513 -83
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +12 -0
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/domains/datasets/index.d.ts.map +1 -1
- package/dist/storage/domains/experiments/index.d.ts +3 -1
- package/dist/storage/domains/experiments/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +5 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/docs/references/docs-agents-agent-memory.md +0 -209
- package/dist/docs/references/docs-agents-network-approval.md +0 -275
- package/dist/docs/references/docs-observability-overview.md +0 -70
- package/dist/docs/references/docs-observability-tracing-exporters-default.md +0 -209
|
@@ -2,44 +2,239 @@
|
|
|
2
2
|
|
|
3
3
|
Memory enables your agent to remember user messages, agent replies, and tool results across interactions, giving it the context it needs to stay consistent, maintain conversation flow, and produce better answers over time.
|
|
4
4
|
|
|
5
|
-
Mastra
|
|
5
|
+
Mastra agents can be configured to store [message history](https://mastra.ai/docs/memory/message-history). Additionally, you can enable:
|
|
6
6
|
|
|
7
|
-
- [
|
|
8
|
-
- [
|
|
9
|
-
- [
|
|
10
|
-
- [**Observational memory**](https://mastra.ai/docs/memory/observational-memory) - uses background Observer and Reflector agents to maintain a dense observation log that replaces raw message history as it grows, keeping the context window small while preserving long-term memory across conversations.
|
|
7
|
+
- [Observational Memory](https://mastra.ai/docs/memory/observational-memory) (Recommended): Uses background agents to maintain a dense observation log that replaces raw message history as it grows. This keeps the context window small while preserving long-term memory.
|
|
8
|
+
- [Working memory](https://mastra.ai/docs/memory/working-memory): Stores persistent, structured user data such as names, preferences, and goals.
|
|
9
|
+
- [Semantic recall](https://mastra.ai/docs/memory/semantic-recall): Retrieves relevant past messages based on semantic meaning rather than exact keywords.
|
|
11
10
|
|
|
12
11
|
If the combined memory exceeds the model's context limit, [memory processors](https://mastra.ai/docs/memory/memory-processors) can filter, trim, or prioritize content so the most relevant information is preserved.
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
Memory results will be stored in one or more of your configured [storage providers](https://mastra.ai/docs/memory/storage).
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
## When to use memory
|
|
17
16
|
|
|
18
|
-
-
|
|
19
|
-
- [Working memory](https://mastra.ai/docs/memory/working-memory)
|
|
20
|
-
- [Semantic recall](https://mastra.ai/docs/memory/semantic-recall)
|
|
21
|
-
- [Observational memory](https://mastra.ai/docs/memory/observational-memory)
|
|
17
|
+
Use memory when your agent needs to maintain multi-turn conversations that reference prior exchanges, recall user preferences or facts from earlier in a session, or build context over time within a conversation thread. Skip memory for single-turn requests where each interaction is independent.
|
|
22
18
|
|
|
23
|
-
##
|
|
19
|
+
## Quickstart
|
|
24
20
|
|
|
25
|
-
|
|
21
|
+
1. Install the `@mastra/memory` package.
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
**npm**:
|
|
28
24
|
|
|
29
|
-
|
|
25
|
+
```bash
|
|
26
|
+
npm install @mastra/memory@latest
|
|
27
|
+
```
|
|
30
28
|
|
|
31
|
-
|
|
29
|
+
**pnpm**:
|
|
32
30
|
|
|
33
|
-
|
|
31
|
+
```bash
|
|
32
|
+
pnpm add @mastra/memory@latest
|
|
33
|
+
```
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
**Yarn**:
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
```bash
|
|
38
|
+
yarn add @mastra/memory@latest
|
|
39
|
+
```
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
**Bun**:
|
|
40
42
|
|
|
41
|
-
|
|
43
|
+
```bash
|
|
44
|
+
bun add @mastra/memory@latest
|
|
45
|
+
```
|
|
42
46
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
2. Memory **requires** a storage provider to persist message history, including user messages and agent responses.
|
|
48
|
+
|
|
49
|
+
For the purposes of this quickstart, use `@mastra/libsql`.
|
|
50
|
+
|
|
51
|
+
**npm**:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install @mastra/libsql@latest
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**pnpm**:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pnpm add @mastra/libsql@latest
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**Yarn**:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
yarn add @mastra/libsql@latest
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Bun**:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
bun add @mastra/libsql@latest
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
> **Note:** For more details on available providers and how storage works in Mastra, visit the [storage](https://mastra.ai/docs/memory/storage) documentation.
|
|
76
|
+
|
|
77
|
+
3. Add the storage provider to your main Mastra instance to enable memory across all configured agents.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { Mastra } from '@mastra/core'
|
|
81
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
82
|
+
|
|
83
|
+
export const mastra = new Mastra({
|
|
84
|
+
storage: new LibSQLStore({
|
|
85
|
+
id: 'mastra-storage',
|
|
86
|
+
url: ':memory:',
|
|
87
|
+
}),
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
4. Create a `Memory` instance and pass it to the agent's `memory` option.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { Agent } from '@mastra/core/agent'
|
|
95
|
+
import { Memory } from '@mastra/memory'
|
|
96
|
+
|
|
97
|
+
export const memoryAgent = new Agent({
|
|
98
|
+
id: 'memory-agent',
|
|
99
|
+
name: 'Memory Agent',
|
|
100
|
+
memory: new Memory({
|
|
101
|
+
options: {
|
|
102
|
+
lastMessages: 20,
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
> **Note:** Visit [Memory Class](https://mastra.ai/reference/memory/memory-class) for a full list of configuration options.
|
|
109
|
+
|
|
110
|
+
5. Call your agent, for example in [Studio](https://mastra.ai/docs/studio/overview). Inside Studio, start a new chat with your agent and take a look at the right sidebar. It'll now display various memory-related information.
|
|
111
|
+
|
|
112
|
+
## Message history
|
|
113
|
+
|
|
114
|
+
Pass a `memory` object with `resource` and `thread` to track message history.
|
|
115
|
+
|
|
116
|
+
- `resource`: A stable identifier for the user or entity.
|
|
117
|
+
- `thread`: An ID that isolates a specific conversation or session.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
const response = await memoryAgent.generate('Remember my favorite color is blue.', {
|
|
121
|
+
memory: {
|
|
122
|
+
resource: 'user-123',
|
|
123
|
+
thread: 'conversation-123',
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
To recall information stored in memory, call the agent with the same `resource` and `thread` values used in the original conversation.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const response = await memoryAgent.generate("What's my favorite color?", {
|
|
132
|
+
memory: {
|
|
133
|
+
resource: 'user-123',
|
|
134
|
+
thread: 'conversation-123',
|
|
135
|
+
},
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
// Response: "Your favorite color is blue."
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
> **Warning:** Each thread has an owner (`resourceId`) that can't be changed after creation. Avoid reusing the same thread ID for threads with different owners, as this will cause errors when querying.
|
|
142
|
+
|
|
143
|
+
To list all threads for a resource, or retrieve a specific thread, [use the memory API directly](https://mastra.ai/docs/memory/message-history).
|
|
144
|
+
|
|
145
|
+
## Observational Memory
|
|
146
|
+
|
|
147
|
+
For long-running conversations, raw message history grows until it fills the context window, degrading agent performance. [Observational Memory](https://mastra.ai/docs/memory/observational-memory) solves this by running background agents that compress old messages into dense observations, keeping the context window small while preserving long-term memory.
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { Agent } from '@mastra/core/agent'
|
|
151
|
+
import { Memory } from '@mastra/memory'
|
|
152
|
+
|
|
153
|
+
export const memoryAgent = new Agent({
|
|
154
|
+
id: 'memory-agent',
|
|
155
|
+
name: 'Memory Agent',
|
|
156
|
+
memory: new Memory({
|
|
157
|
+
options: {
|
|
158
|
+
observationalMemory: true,
|
|
159
|
+
},
|
|
160
|
+
}),
|
|
161
|
+
})
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
> **Note:** See [Observational Memory](https://mastra.ai/docs/memory/observational-memory) for details on how observations and reflections work, and [the reference](https://mastra.ai/reference/memory/observational-memory) for all configuration options.
|
|
165
|
+
|
|
166
|
+
## Memory in multi-agent systems
|
|
167
|
+
|
|
168
|
+
When a [supervisor agent](https://mastra.ai/docs/agents/supervisor-agents) delegates to a subagent, Mastra isolates subagent memory automatically. No flag enables this as it happens on every delegation. Understanding how this scoping works lets you decide what stays private and what to share intentionally.
|
|
169
|
+
|
|
170
|
+
### How delegation scopes memory
|
|
171
|
+
|
|
172
|
+
Each delegation creates a fresh `threadId` and a deterministic `resourceId` for the subagent:
|
|
173
|
+
|
|
174
|
+
- **Thread ID**: Unique per delegation. The subagent starts with a clean message history every time it's called.
|
|
175
|
+
- **Resource ID**: Derived as `{parentResourceId}-{agentName}`. Because the resource ID is stable across delegations, resource-scoped memory persists between calls. A subagent remembers facts from previous delegations by the same user.
|
|
176
|
+
- **Memory instance**: If a subagent has no memory configured, it inherits the supervisor's `Memory` instance. If the subagent defines its own, that takes precedence.
|
|
177
|
+
|
|
178
|
+
The supervisor forwards its conversation context to the subagent so it has enough background to complete the task. Only the delegation prompt and the subagent's response are saved — the full parent conversation isn't stored. You can control which messages reach the subagent with the [`messageFilter`](https://mastra.ai/docs/agents/supervisor-agents) callback.
|
|
179
|
+
|
|
180
|
+
> **Note:** Subagent resource IDs are always suffixed with the agent name (`{parentResourceId}-{agentName}`). Two different subagents under the same supervisor never share a resource ID through delegation.
|
|
181
|
+
|
|
182
|
+
To go beyond this default isolation, you can share memory between agents by passing matching identifiers when you call them directly.
|
|
183
|
+
|
|
184
|
+
### Share memory between agents
|
|
185
|
+
|
|
186
|
+
When you call agents directly (outside the delegation flow), memory sharing is controlled by two identifiers: `resourceId` and `threadId`. Agents that use the same values read and write to the same data. This is useful when agents collaborate on a shared context — for example, a researcher that saves notes and a writer that reads them.
|
|
187
|
+
|
|
188
|
+
**Resource-scoped sharing** is the most common pattern. [Working memory](https://mastra.ai/docs/memory/working-memory) and [semantic recall](https://mastra.ai/docs/memory/semantic-recall) default to `scope: 'resource'`. If two agents share a `resourceId`, they share observations, working memory, and embeddings — even across different threads:
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
// Both agents share the same resource-scoped memory
|
|
192
|
+
await researcher.generate('Find information about quantum computing.', {
|
|
193
|
+
memory: { resource: 'project-42', thread: 'research-session' },
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
await writer.generate('Write a summary from the research notes.', {
|
|
197
|
+
memory: { resource: 'project-42', thread: 'writing-session' },
|
|
198
|
+
})
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Because both calls use `resource: 'project-42'`, the writer can access the researcher's observations, working memory, and semantic embeddings. Each agent still has its own thread, so message histories stay separate.
|
|
202
|
+
|
|
203
|
+
**Thread-scoped sharing** gives tighter coupling. [Observational Memory](https://mastra.ai/docs/memory/observational-memory) uses `scope: 'thread'` by default. If two agents use the same `resource` _and_ `thread`, they share the full message history. Each agent sees every message the other has written. This is useful when agents need to build on each other's exact outputs.
|
|
204
|
+
|
|
205
|
+
## Observability
|
|
206
|
+
|
|
207
|
+
Enable [Tracing](https://mastra.ai/docs/observability/tracing/overview) to monitor and debug memory in action. Traces show you exactly which messages and observations the agent included in its context for each request, helping you understand agent behavior and verify that memory retrieval is working as expected.
|
|
208
|
+
|
|
209
|
+
Open [Studio](https://mastra.ai/docs/studio/overview) and select the **Observability** tab in the sidebar. Open the trace of a recent agent request, then look for spans of LLMs calls.
|
|
210
|
+
|
|
211
|
+
## Switch memory per request
|
|
212
|
+
|
|
213
|
+
Use [`RequestContext`](https://mastra.ai/docs/server/request-context) to access request-specific values. This lets you conditionally select different memory or storage configurations based on the context of the request.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
export type UserTier = {
|
|
217
|
+
'user-tier': 'enterprise' | 'pro'
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const premiumMemory = new Memory()
|
|
221
|
+
const standardMemory = new Memory()
|
|
222
|
+
|
|
223
|
+
export const memoryAgent = new Agent({
|
|
224
|
+
id: 'memory-agent',
|
|
225
|
+
name: 'Memory Agent',
|
|
226
|
+
memory: ({ requestContext }) => {
|
|
227
|
+
const userTier = requestContext.get('user-tier') as UserTier['user-tier']
|
|
228
|
+
|
|
229
|
+
return userTier === 'enterprise' ? premiumMemory : standardMemory
|
|
230
|
+
},
|
|
231
|
+
})
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
> **Note:** Visit [Request Context](https://mastra.ai/docs/server/request-context) for more information.
|
|
235
|
+
|
|
236
|
+
## Related
|
|
237
|
+
|
|
238
|
+
- [`Memory` reference](https://mastra.ai/reference/memory/memory-class)
|
|
239
|
+
- [Tracing](https://mastra.ai/docs/observability/tracing/overview)
|
|
240
|
+
- [Request Context](https://mastra.ai/docs/server/request-context)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# Semantic
|
|
1
|
+
# Semantic recall
|
|
2
2
|
|
|
3
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
4
|
|
|
5
5
|
> **Watch 📹:** What semantic recall is, how it works, and how to configure it in Mastra → [YouTube (5 minutes)](https://youtu.be/UVZtK8cK8xQ)
|
|
6
6
|
|
|
7
|
-
## How
|
|
7
|
+
## How semantic recall works
|
|
8
8
|
|
|
9
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
10
|
|
|
@@ -16,26 +16,41 @@ When it's enabled, new messages are used to query a vector DB for semantically s
|
|
|
16
16
|
|
|
17
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
18
|
|
|
19
|
-
##
|
|
19
|
+
## Quickstart
|
|
20
20
|
|
|
21
|
-
Semantic recall is
|
|
21
|
+
Semantic recall is disabled by default. To enable it, set `semanticRecall: true` in `options` and provide a `vector` store and `embedder`:
|
|
22
22
|
|
|
23
23
|
```typescript
|
|
24
24
|
import { Agent } from '@mastra/core/agent'
|
|
25
25
|
import { Memory } from '@mastra/memory'
|
|
26
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
27
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
26
28
|
|
|
27
29
|
const agent = new Agent({
|
|
28
30
|
id: 'support-agent',
|
|
29
31
|
name: 'SupportAgent',
|
|
30
32
|
instructions: 'You are a helpful support agent.',
|
|
31
|
-
model: 'openai/gpt-5.
|
|
32
|
-
memory: new Memory(
|
|
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
|
+
}),
|
|
33
48
|
})
|
|
34
49
|
```
|
|
35
50
|
|
|
36
|
-
## Using the recall()
|
|
51
|
+
## Using the `recall()` method
|
|
37
52
|
|
|
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
|
|
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`:
|
|
39
54
|
|
|
40
55
|
```typescript
|
|
41
56
|
const memory = await agent.getMemory()
|
|
@@ -77,6 +92,9 @@ const agent = new Agent({
|
|
|
77
92
|
id: 'agent-vector',
|
|
78
93
|
url: 'file:./local.db',
|
|
79
94
|
}),
|
|
95
|
+
options: {
|
|
96
|
+
semanticRecall: true,
|
|
97
|
+
},
|
|
80
98
|
}),
|
|
81
99
|
})
|
|
82
100
|
```
|
|
@@ -139,6 +157,9 @@ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
|
139
157
|
const agent = new Agent({
|
|
140
158
|
memory: new Memory({
|
|
141
159
|
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
160
|
+
options: {
|
|
161
|
+
semanticRecall: true,
|
|
162
|
+
},
|
|
142
163
|
}),
|
|
143
164
|
})
|
|
144
165
|
```
|
|
@@ -147,8 +168,24 @@ Supported embedding models:
|
|
|
147
168
|
|
|
148
169
|
- **OpenAI**: `text-embedding-3-small`, `text-embedding-3-large`, `text-embedding-ada-002`
|
|
149
170
|
- **Google**: `gemini-embedding-001`
|
|
171
|
+
- **OpenRouter**: Access embedding models from various providers
|
|
150
172
|
|
|
151
|
-
|
|
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`).
|
|
152
189
|
|
|
153
190
|
### Using AI SDK Packages
|
|
154
191
|
|
|
@@ -166,7 +203,7 @@ const agent = new Agent({
|
|
|
166
203
|
})
|
|
167
204
|
```
|
|
168
205
|
|
|
169
|
-
### Using FastEmbed (
|
|
206
|
+
### Using FastEmbed (local)
|
|
170
207
|
|
|
171
208
|
To use FastEmbed (a local embedding model), install `@mastra/fastembed`:
|
|
172
209
|
|
|
@@ -208,7 +245,7 @@ const agent = new Agent({
|
|
|
208
245
|
})
|
|
209
246
|
```
|
|
210
247
|
|
|
211
|
-
## PostgreSQL
|
|
248
|
+
## PostgreSQL index optimization
|
|
212
249
|
|
|
213
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.
|
|
214
251
|
|
|
@@ -246,27 +283,15 @@ const agent = new Agent({
|
|
|
246
283
|
|
|
247
284
|
For detailed information about index configuration options and performance tuning, see the [PgVector configuration guide](https://mastra.ai/reference/vectors/pg).
|
|
248
285
|
|
|
249
|
-
##
|
|
286
|
+
## Disable semantic recall
|
|
250
287
|
|
|
251
|
-
|
|
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
|
-
```
|
|
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.
|
|
264
289
|
|
|
265
|
-
|
|
290
|
+
Keep semantic recall disabled when:
|
|
266
291
|
|
|
267
|
-
-
|
|
268
|
-
-
|
|
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.
|
|
269
294
|
|
|
270
|
-
## Viewing
|
|
295
|
+
## Viewing recalled messages
|
|
271
296
|
|
|
272
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).
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Storage
|
|
2
2
|
|
|
3
|
-
For agents to remember previous interactions, Mastra needs a
|
|
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
4
|
|
|
5
5
|
```typescript
|
|
6
6
|
import { Mastra } from '@mastra/core'
|
|
@@ -14,7 +14,7 @@ export const mastra = new Mastra({
|
|
|
14
14
|
})
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
> **Sharing the database with
|
|
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
18
|
>
|
|
19
19
|
> ```typescript
|
|
20
20
|
> url: 'file:/absolute/path/to/your/project/mastra.db'
|
|
@@ -24,7 +24,7 @@ export const mastra = new Mastra({
|
|
|
24
24
|
|
|
25
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
26
|
|
|
27
|
-
Mastra automatically
|
|
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
28
|
|
|
29
29
|
## Supported providers
|
|
30
30
|
|
|
@@ -35,7 +35,7 @@ Each provider page includes installation instructions, configuration parameters,
|
|
|
35
35
|
- [MongoDB](https://mastra.ai/reference/storage/mongodb)
|
|
36
36
|
- [Upstash](https://mastra.ai/reference/storage/upstash)
|
|
37
37
|
- [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1)
|
|
38
|
-
- [Cloudflare Durable Objects](https://mastra.ai/reference/storage/cloudflare)
|
|
38
|
+
- [Cloudflare KV & Durable Objects](https://mastra.ai/reference/storage/cloudflare)
|
|
39
39
|
- [Convex](https://mastra.ai/reference/storage/convex)
|
|
40
40
|
- [DynamoDB](https://mastra.ai/reference/storage/dynamodb)
|
|
41
41
|
- [LanceDB](https://mastra.ai/reference/storage/lance)
|
|
@@ -49,7 +49,7 @@ Storage can be configured at the instance level (shared by all agents) or at the
|
|
|
49
49
|
|
|
50
50
|
### Instance-level storage
|
|
51
51
|
|
|
52
|
-
Add storage to your Mastra instance so all agents, workflows, observability traces and scores share the same
|
|
52
|
+
Add storage to your Mastra instance so all agents, workflows, observability traces, and scores share the same storage backend:
|
|
53
53
|
|
|
54
54
|
```typescript
|
|
55
55
|
import { Mastra } from '@mastra/core'
|
|
@@ -71,7 +71,7 @@ This is useful when all primitives share the same storage backend and have simil
|
|
|
71
71
|
|
|
72
72
|
#### Composite storage
|
|
73
73
|
|
|
74
|
-
[Composite storage](https://mastra.ai/reference/storage/composite) is an alternative way to configure instance-level storage. Use `MastraCompositeStore` to
|
|
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
75
|
|
|
76
76
|
```typescript
|
|
77
77
|
import { Mastra } from '@mastra/core'
|
|
@@ -100,7 +100,7 @@ This is useful when different types of data have different performance or operat
|
|
|
100
100
|
|
|
101
101
|
### Agent-level storage
|
|
102
102
|
|
|
103
|
-
Agent-level storage overrides storage configured at the instance level. Add storage to a specific agent when you need data
|
|
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
104
|
|
|
105
105
|
```typescript
|
|
106
106
|
import { Agent } from '@mastra/core/agent'
|
|
@@ -118,18 +118,16 @@ export const agent = new Agent({
|
|
|
118
118
|
})
|
|
119
119
|
```
|
|
120
120
|
|
|
121
|
-
> **Warning:** [Mastra Cloud Store](https://mastra.ai/docs/mastra-cloud/deployment) doesn't support agent-level storage.
|
|
122
|
-
|
|
123
121
|
## Threads and resources
|
|
124
122
|
|
|
125
123
|
Mastra organizes conversations using two identifiers:
|
|
126
124
|
|
|
127
|
-
- **Thread
|
|
128
|
-
- **Resource
|
|
125
|
+
- **Thread**: A conversation session containing a sequence of messages.
|
|
126
|
+
- **Resource**: The entity that owns the thread, such as a user, organization, project, or any other domain entity in your application.
|
|
129
127
|
|
|
130
128
|
Both identifiers are required for agents to store information:
|
|
131
129
|
|
|
132
|
-
|
|
130
|
+
**.generate()**:
|
|
133
131
|
|
|
134
132
|
```typescript
|
|
135
133
|
const response = await agent.generate('hello', {
|
|
@@ -140,7 +138,7 @@ const response = await agent.generate('hello', {
|
|
|
140
138
|
})
|
|
141
139
|
```
|
|
142
140
|
|
|
143
|
-
|
|
141
|
+
**.stream()**:
|
|
144
142
|
|
|
145
143
|
```typescript
|
|
146
144
|
const stream = await agent.stream('hello', {
|
|
@@ -151,7 +149,7 @@ const stream = await agent.stream('hello', {
|
|
|
151
149
|
})
|
|
152
150
|
```
|
|
153
151
|
|
|
154
|
-
> **Note:** [Studio](https://mastra.ai/docs/
|
|
152
|
+
> **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
153
|
|
|
156
154
|
### Thread title generation
|
|
157
155
|
|
|
@@ -170,7 +168,7 @@ export const agent = new Agent({
|
|
|
170
168
|
})
|
|
171
169
|
```
|
|
172
170
|
|
|
173
|
-
Title generation runs asynchronously after the agent responds and
|
|
171
|
+
Title generation runs asynchronously after the agent responds and doesn't affect response time.
|
|
174
172
|
|
|
175
173
|
To optimize cost or behavior, provide a smaller [`model`](https://mastra.ai/models) and custom `instructions`:
|
|
176
174
|
|
|
@@ -180,7 +178,7 @@ export const agent = new Agent({
|
|
|
180
178
|
memory: new Memory({
|
|
181
179
|
options: {
|
|
182
180
|
generateTitle: {
|
|
183
|
-
model: 'openai/gpt-
|
|
181
|
+
model: 'openai/gpt-5-mini',
|
|
184
182
|
instructions: 'Generate a 1 word title',
|
|
185
183
|
},
|
|
186
184
|
},
|