@mastra/libsql 1.6.0 → 1.6.1-alpha.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.
- package/CHANGELOG.md +11 -0
- package/dist/index.cjs +17 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +17 -8
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/docs/SKILL.md +0 -50
- package/dist/docs/assets/SOURCE_MAP.json +0 -6
- package/dist/docs/references/docs-agents-agent-approval.md +0 -377
- package/dist/docs/references/docs-agents-agent-memory.md +0 -212
- package/dist/docs/references/docs-agents-network-approval.md +0 -275
- package/dist/docs/references/docs-agents-networks.md +0 -290
- package/dist/docs/references/docs-memory-memory-processors.md +0 -316
- package/dist/docs/references/docs-memory-message-history.md +0 -260
- package/dist/docs/references/docs-memory-overview.md +0 -45
- package/dist/docs/references/docs-memory-semantic-recall.md +0 -272
- package/dist/docs/references/docs-memory-storage.md +0 -261
- package/dist/docs/references/docs-memory-working-memory.md +0 -400
- package/dist/docs/references/docs-observability-overview.md +0 -70
- package/dist/docs/references/docs-observability-tracing-exporters-default.md +0 -211
- package/dist/docs/references/docs-rag-retrieval.md +0 -521
- package/dist/docs/references/docs-workflows-snapshots.md +0 -238
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +0 -140
- package/dist/docs/references/reference-core-getMemory.md +0 -50
- package/dist/docs/references/reference-core-listMemory.md +0 -56
- package/dist/docs/references/reference-core-mastra-class.md +0 -66
- package/dist/docs/references/reference-memory-memory-class.md +0 -147
- package/dist/docs/references/reference-storage-composite.md +0 -235
- package/dist/docs/references/reference-storage-dynamodb.md +0 -282
- package/dist/docs/references/reference-storage-libsql.md +0 -135
- package/dist/docs/references/reference-vectors-libsql.md +0 -305
|
@@ -1,272 +0,0 @@
|
|
|
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
|
-
## 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).
|
|
@@ -1,261 +0,0 @@
|
|
|
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
|
-
```
|