@mastra/memory 1.0.0-beta.10 → 1.0.0-beta.11
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 +87 -0
- package/dist/docs/README.md +36 -0
- package/dist/docs/SKILL.md +42 -0
- package/dist/docs/SOURCE_MAP.json +31 -0
- package/dist/docs/agents/01-agent-memory.md +160 -0
- package/dist/docs/agents/02-networks.md +236 -0
- package/dist/docs/agents/03-agent-approval.md +317 -0
- package/dist/docs/core/01-reference.md +114 -0
- package/dist/docs/memory/01-overview.md +76 -0
- package/dist/docs/memory/02-storage.md +181 -0
- package/dist/docs/memory/03-working-memory.md +386 -0
- package/dist/docs/memory/04-semantic-recall.md +235 -0
- package/dist/docs/memory/05-memory-processors.md +319 -0
- package/dist/docs/memory/06-reference.md +617 -0
- package/dist/docs/processors/01-reference.md +81 -0
- package/dist/docs/storage/01-reference.md +972 -0
- package/dist/docs/vectors/01-reference.md +929 -0
- package/dist/index.cjs +247 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +134 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +247 -14
- package/dist/index.js.map +1 -1
- package/dist/tools/working-memory.d.ts +1 -1
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +11 -10
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
# Memory API Reference
|
|
2
|
+
|
|
3
|
+
> API reference for memory - 6 entries
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Reference: Clone Utility Methods
|
|
9
|
+
|
|
10
|
+
> Documentation for utility methods to work with cloned threads in Mastra Memory.
|
|
11
|
+
|
|
12
|
+
The Memory class provides utility methods for working with cloned threads. These methods help you check clone status, retrieve clone metadata, navigate clone relationships, and track clone history.
|
|
13
|
+
|
|
14
|
+
## isClone()
|
|
15
|
+
|
|
16
|
+
Checks whether a thread is a clone of another thread.
|
|
17
|
+
|
|
18
|
+
### Usage
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
const isClonedThread = memory.isClone(thread);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Parameters
|
|
25
|
+
|
|
26
|
+
### Returns
|
|
27
|
+
|
|
28
|
+
### Example
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
const thread = await memory.getThreadById({ threadId: "some-thread-id" });
|
|
32
|
+
|
|
33
|
+
if (memory.isClone(thread)) {
|
|
34
|
+
console.log("This thread was cloned from another thread");
|
|
35
|
+
} else {
|
|
36
|
+
console.log("This is an original thread");
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## getCloneMetadata()
|
|
43
|
+
|
|
44
|
+
Retrieves the clone metadata from a thread if it exists.
|
|
45
|
+
|
|
46
|
+
### Usage
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
const metadata = memory.getCloneMetadata(thread);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Parameters
|
|
53
|
+
|
|
54
|
+
### Returns
|
|
55
|
+
|
|
56
|
+
Returns `ThreadCloneMetadata | null`:
|
|
57
|
+
|
|
58
|
+
### Example
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const thread = await memory.getThreadById({ threadId: "cloned-thread-id" });
|
|
62
|
+
const cloneInfo = memory.getCloneMetadata(thread);
|
|
63
|
+
|
|
64
|
+
if (cloneInfo) {
|
|
65
|
+
console.log(`Cloned from: ${cloneInfo.sourceThreadId}`);
|
|
66
|
+
console.log(`Cloned at: ${cloneInfo.clonedAt}`);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## getSourceThread()
|
|
73
|
+
|
|
74
|
+
Retrieves the original source thread that a cloned thread was created from.
|
|
75
|
+
|
|
76
|
+
### Usage
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const sourceThread = await memory.getSourceThread(threadId);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Parameters
|
|
83
|
+
|
|
84
|
+
### Returns
|
|
85
|
+
|
|
86
|
+
### Example
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
const sourceThread = await memory.getSourceThread("cloned-thread-id");
|
|
90
|
+
|
|
91
|
+
if (sourceThread) {
|
|
92
|
+
console.log(`Original thread title: ${sourceThread.title}`);
|
|
93
|
+
console.log(`Original thread created: ${sourceThread.createdAt}`);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## listClones()
|
|
100
|
+
|
|
101
|
+
Lists all threads that were cloned from a specific source thread.
|
|
102
|
+
|
|
103
|
+
### Usage
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
const clones = await memory.listClones(sourceThreadId);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Parameters
|
|
110
|
+
|
|
111
|
+
### Returns
|
|
112
|
+
|
|
113
|
+
### Example
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
const clones = await memory.listClones("original-thread-id");
|
|
117
|
+
|
|
118
|
+
console.log(`Found ${clones.length} clones`);
|
|
119
|
+
for (const clone of clones) {
|
|
120
|
+
console.log(`- ${clone.id}: ${clone.title}`);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## getCloneHistory()
|
|
127
|
+
|
|
128
|
+
Retrieves the full clone history chain for a thread, tracing back to the original.
|
|
129
|
+
|
|
130
|
+
### Usage
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
const history = await memory.getCloneHistory(threadId);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Parameters
|
|
137
|
+
|
|
138
|
+
### Returns
|
|
139
|
+
|
|
140
|
+
### Example
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
// If thread-c was cloned from thread-b, which was cloned from thread-a
|
|
144
|
+
const history = await memory.getCloneHistory("thread-c");
|
|
145
|
+
|
|
146
|
+
// history = [thread-a, thread-b, thread-c]
|
|
147
|
+
console.log(`Clone depth: ${history.length - 1}`);
|
|
148
|
+
console.log(`Original thread: ${history[0].id}`);
|
|
149
|
+
console.log(`Current thread: ${history[history.length - 1].id}`);
|
|
150
|
+
|
|
151
|
+
// Display the clone chain
|
|
152
|
+
for (let i = 0; i < history.length; i++) {
|
|
153
|
+
const prefix = i === 0 ? "Original" : `Clone ${i}`;
|
|
154
|
+
console.log(`${prefix}: ${history[i].title}`);
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Complete Example
|
|
161
|
+
|
|
162
|
+
```typescript title="src/clone-management.ts"
|
|
163
|
+
import { mastra } from "./mastra";
|
|
164
|
+
|
|
165
|
+
async function manageClones() {
|
|
166
|
+
const agent = mastra.getAgent("agent");
|
|
167
|
+
const memory = await agent.getMemory();
|
|
168
|
+
|
|
169
|
+
// Create an original conversation
|
|
170
|
+
const originalThread = await memory.createThread({
|
|
171
|
+
resourceId: "user-123",
|
|
172
|
+
title: "Original Conversation",
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// Have a conversation...
|
|
176
|
+
await agent.generate("Hello! Let's discuss project options.", {
|
|
177
|
+
threadId: originalThread.id,
|
|
178
|
+
resourceId: "user-123",
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Create multiple branches (clones) to explore different paths
|
|
182
|
+
const { thread: optionA } = await memory.cloneThread({
|
|
183
|
+
sourceThreadId: originalThread.id,
|
|
184
|
+
title: "Option A - Conservative Approach",
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const { thread: optionB } = await memory.cloneThread({
|
|
188
|
+
sourceThreadId: originalThread.id,
|
|
189
|
+
title: "Option B - Aggressive Approach",
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Check clone status
|
|
193
|
+
console.log(memory.isClone(originalThread)); // false
|
|
194
|
+
console.log(memory.isClone(optionA)); // true
|
|
195
|
+
console.log(memory.isClone(optionB)); // true
|
|
196
|
+
|
|
197
|
+
// Get clone metadata
|
|
198
|
+
const metadataA = memory.getCloneMetadata(optionA);
|
|
199
|
+
console.log(metadataA?.sourceThreadId); // originalThread.id
|
|
200
|
+
|
|
201
|
+
// List all clones of the original
|
|
202
|
+
const allClones = await memory.listClones(originalThread.id);
|
|
203
|
+
console.log(`Total alternatives: ${allClones.length}`); // 2
|
|
204
|
+
|
|
205
|
+
// Get source thread from a clone
|
|
206
|
+
const source = await memory.getSourceThread(optionA.id);
|
|
207
|
+
console.log(source?.id === originalThread.id); // true
|
|
208
|
+
|
|
209
|
+
// Create a deeper clone chain
|
|
210
|
+
const { thread: optionA2 } = await memory.cloneThread({
|
|
211
|
+
sourceThreadId: optionA.id,
|
|
212
|
+
title: "Option A - Variant 2",
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Get the full history
|
|
216
|
+
const history = await memory.getCloneHistory(optionA2.id);
|
|
217
|
+
// history = [originalThread, optionA, optionA2]
|
|
218
|
+
console.log(`Clone depth: ${history.length - 1}`); // 2
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Related
|
|
223
|
+
|
|
224
|
+
- [cloneThread](https://mastra.ai/reference/v1/memory/cloneThread)
|
|
225
|
+
- [Memory Class Reference](https://mastra.ai/reference/v1/memory/memory-class)
|
|
226
|
+
- [getThreadById](https://mastra.ai/reference/v1/memory/getThreadById)
|
|
227
|
+
- [listThreadsByResourceId](https://mastra.ai/reference/v1/memory/listThreadsByResourceId)
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## Reference: Memory.cloneThread()
|
|
232
|
+
|
|
233
|
+
> Documentation for the `Memory.cloneThread()` method in Mastra, which creates a copy of a conversation thread with all its messages.
|
|
234
|
+
|
|
235
|
+
The `.cloneThread()` method creates a copy of an existing conversation thread, including all its messages. This enables creating divergent conversation paths from a specific point in a conversation. When semantic recall is enabled, the method also creates vector embeddings for the cloned messages.
|
|
236
|
+
|
|
237
|
+
## Usage Example
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
const { thread, clonedMessages } = await memory.cloneThread({
|
|
241
|
+
sourceThreadId: "original-thread-123",
|
|
242
|
+
});
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Parameters
|
|
246
|
+
|
|
247
|
+
### Options Parameters
|
|
248
|
+
|
|
249
|
+
### MessageFilter Parameters
|
|
250
|
+
|
|
251
|
+
## Returns
|
|
252
|
+
|
|
253
|
+
### Clone Metadata
|
|
254
|
+
|
|
255
|
+
The cloned thread's metadata includes a `clone` property with:
|
|
256
|
+
|
|
257
|
+
## Extended Usage Example
|
|
258
|
+
|
|
259
|
+
```typescript title="src/test-clone.ts"
|
|
260
|
+
import { mastra } from "./mastra";
|
|
261
|
+
|
|
262
|
+
const agent = mastra.getAgent("agent");
|
|
263
|
+
const memory = await agent.getMemory();
|
|
264
|
+
|
|
265
|
+
// Clone a thread with all messages
|
|
266
|
+
const { thread: fullClone } = await memory.cloneThread({
|
|
267
|
+
sourceThreadId: "original-thread-123",
|
|
268
|
+
title: "Alternative Conversation Path",
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// Clone with a custom ID
|
|
272
|
+
const { thread: customIdClone } = await memory.cloneThread({
|
|
273
|
+
sourceThreadId: "original-thread-123",
|
|
274
|
+
newThreadId: "my-custom-clone-id",
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// Clone only the last 5 messages
|
|
278
|
+
const { thread: partialClone, clonedMessages } = await memory.cloneThread({
|
|
279
|
+
sourceThreadId: "original-thread-123",
|
|
280
|
+
options: {
|
|
281
|
+
messageLimit: 5,
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// Clone messages from a specific date range
|
|
286
|
+
const { thread: dateFilteredClone } = await memory.cloneThread({
|
|
287
|
+
sourceThreadId: "original-thread-123",
|
|
288
|
+
options: {
|
|
289
|
+
messageFilter: {
|
|
290
|
+
startDate: new Date("2024-01-01"),
|
|
291
|
+
endDate: new Date("2024-01-31"),
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// Continue conversation on the cloned thread
|
|
297
|
+
const response = await agent.generate("Let's try a different approach", {
|
|
298
|
+
threadId: fullClone.id,
|
|
299
|
+
resourceId: fullClone.resourceId,
|
|
300
|
+
});
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Vector Embeddings
|
|
304
|
+
|
|
305
|
+
When the Memory instance has semantic recall enabled (with a vector store and embedder configured), `cloneThread()` automatically creates vector embeddings for all cloned messages. This ensures that semantic search works correctly on the cloned thread.
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { Memory } from "@mastra/memory";
|
|
309
|
+
import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
|
|
310
|
+
|
|
311
|
+
const memory = new Memory({
|
|
312
|
+
storage: new LibSQLStore({ url: "file:./memory.db" }),
|
|
313
|
+
vector: new LibSQLVector({ connectionUrl: "file:./vector.db" }),
|
|
314
|
+
embedder: embeddingModel,
|
|
315
|
+
options: {
|
|
316
|
+
semanticRecall: true,
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// Clone will also create embeddings for cloned messages
|
|
321
|
+
const { thread } = await memory.cloneThread({
|
|
322
|
+
sourceThreadId: "original-thread",
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// Semantic search works on the cloned thread
|
|
326
|
+
const results = await memory.recall({
|
|
327
|
+
threadId: thread.id,
|
|
328
|
+
vectorSearchString: "search query",
|
|
329
|
+
});
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Related
|
|
333
|
+
|
|
334
|
+
- [Memory Class Reference](https://mastra.ai/reference/v1/memory/memory-class)
|
|
335
|
+
- [createThread](https://mastra.ai/reference/v1/memory/createThread)
|
|
336
|
+
- [Clone Utility Methods](https://mastra.ai/reference/v1/memory/clone-utilities)
|
|
337
|
+
- [recall](https://mastra.ai/reference/v1/memory/recall)
|
|
338
|
+
- [Semantic Recall](https://mastra.ai/docs/v1/memory/semantic-recall)
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
342
|
+
## Reference: Memory.createThread()
|
|
343
|
+
|
|
344
|
+
> Documentation for the `Memory.createThread()` method in Mastra, which creates a new conversation thread in the memory system.
|
|
345
|
+
|
|
346
|
+
The `.createThread()` method creates a new conversation thread in the memory system. Each thread represents a distinct conversation or context and can contain multiple messages.
|
|
347
|
+
|
|
348
|
+
## Usage Example
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
await memory?.createThread({ resourceId: "user-123" });
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## Parameters
|
|
355
|
+
|
|
356
|
+
## Returns
|
|
357
|
+
|
|
358
|
+
## Extended usage example
|
|
359
|
+
|
|
360
|
+
```typescript title="src/test-memory.ts"
|
|
361
|
+
import { mastra } from "./mastra";
|
|
362
|
+
|
|
363
|
+
const agent = mastra.getAgent("agent");
|
|
364
|
+
const memory = await agent.getMemory();
|
|
365
|
+
|
|
366
|
+
const thread = await memory?.createThread({
|
|
367
|
+
resourceId: "user-123",
|
|
368
|
+
title: "Memory Test Thread",
|
|
369
|
+
metadata: {
|
|
370
|
+
source: "test-script",
|
|
371
|
+
purpose: "memory-testing",
|
|
372
|
+
},
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
const response = await agent.generate("message for agent", {
|
|
376
|
+
memory: {
|
|
377
|
+
thread: thread!.id,
|
|
378
|
+
resource: thread!.resourceId,
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
console.log(response.text);
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### Related
|
|
386
|
+
|
|
387
|
+
- [Memory Class Reference](https://mastra.ai/reference/v1/memory/memory-class)
|
|
388
|
+
- [Getting Started with Memory](https://mastra.ai/docs/v1/memory/overview) (Covers threads concept)
|
|
389
|
+
- [getThreadById](https://mastra.ai/reference/v1/memory/getThreadById)
|
|
390
|
+
- [listThreadsByResourceId](https://mastra.ai/reference/v1/memory/listThreadsByResourceId)
|
|
391
|
+
- [query](https://mastra.ai/reference/v1/memory/query)
|
|
392
|
+
|
|
393
|
+
---
|
|
394
|
+
|
|
395
|
+
## Reference: Memory.getThreadById()
|
|
396
|
+
|
|
397
|
+
> Documentation for the `Memory.getThreadById()` method in Mastra, which retrieves a specific thread by its ID.
|
|
398
|
+
|
|
399
|
+
The `.getThreadById()` method retrieves a specific thread by its ID.
|
|
400
|
+
|
|
401
|
+
## Usage Example
|
|
402
|
+
|
|
403
|
+
```typescript
|
|
404
|
+
await memory?.getThreadById({ threadId: "thread-123" });
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
## Parameters
|
|
408
|
+
|
|
409
|
+
## Returns
|
|
410
|
+
|
|
411
|
+
### Related
|
|
412
|
+
|
|
413
|
+
- [Memory Class Reference](https://mastra.ai/reference/v1/memory/memory-class)
|
|
414
|
+
- [Getting Started with Memory](https://mastra.ai/docs/v1/memory/overview) (Covers threads concept)
|
|
415
|
+
- [createThread](https://mastra.ai/reference/v1/memory/createThread)
|
|
416
|
+
- [listThreadsByResourceId](https://mastra.ai/reference/v1/memory/listThreadsByResourceId)
|
|
417
|
+
|
|
418
|
+
---
|
|
419
|
+
|
|
420
|
+
## Reference: Memory.listThreadsByResourceId()
|
|
421
|
+
|
|
422
|
+
> Documentation for the `Memory.listThreadsByResourceId()` method in Mastra, which retrieves threads associated with a specific resource ID with pagination support.
|
|
423
|
+
|
|
424
|
+
The `.listThreadsByResourceId()` method retrieves threads associated with a specific resource ID with pagination support.
|
|
425
|
+
|
|
426
|
+
## Usage Example
|
|
427
|
+
|
|
428
|
+
```typescript
|
|
429
|
+
await memory.listThreadsByResourceId({
|
|
430
|
+
resourceId: "user-123",
|
|
431
|
+
page: 0,
|
|
432
|
+
perPage: 10,
|
|
433
|
+
});
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
## Parameters
|
|
437
|
+
|
|
438
|
+
## Returns
|
|
439
|
+
|
|
440
|
+
The return object contains:
|
|
441
|
+
- `threads`: Array of thread objects
|
|
442
|
+
- `total`: Total number of threads for this resource
|
|
443
|
+
- `page`: Current page number (same as the input `page` parameter)
|
|
444
|
+
- `perPage`: Items per page (same as the input `perPage` parameter)
|
|
445
|
+
- `hasMore`: Boolean indicating if more results are available
|
|
446
|
+
|
|
447
|
+
## Extended usage example
|
|
448
|
+
|
|
449
|
+
```typescript title="src/test-memory.ts"
|
|
450
|
+
import { mastra } from "./mastra";
|
|
451
|
+
|
|
452
|
+
const agent = mastra.getAgent("agent");
|
|
453
|
+
const memory = await agent.getMemory();
|
|
454
|
+
|
|
455
|
+
let currentPage = 0;
|
|
456
|
+
const perPage = 25;
|
|
457
|
+
let hasMorePages = true;
|
|
458
|
+
|
|
459
|
+
while (hasMorePages) {
|
|
460
|
+
const result = await memory?.listThreadsByResourceId({
|
|
461
|
+
resourceId: "user-123",
|
|
462
|
+
page: currentPage,
|
|
463
|
+
perPage: perPage,
|
|
464
|
+
orderBy: { field: "createdAt", direction: "ASC" },
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
if (!result) {
|
|
468
|
+
console.log("No threads");
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
result.threads.forEach((thread) => {
|
|
473
|
+
console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
hasMorePages = result.hasMore;
|
|
477
|
+
currentPage++; // Move to next page
|
|
478
|
+
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
## Related
|
|
482
|
+
|
|
483
|
+
- [Memory Class Reference](https://mastra.ai/reference/v1/memory/memory-class)
|
|
484
|
+
- [Getting Started with Memory](https://mastra.ai/docs/v1/memory/overview) (Covers threads/resources concept)
|
|
485
|
+
- [createThread](https://mastra.ai/reference/v1/memory/createThread)
|
|
486
|
+
- [getThreadById](https://mastra.ai/reference/v1/memory/getThreadById)
|
|
487
|
+
|
|
488
|
+
---
|
|
489
|
+
|
|
490
|
+
## Reference: Memory Class
|
|
491
|
+
|
|
492
|
+
> Documentation for the `Memory` class in Mastra, which provides a robust system for managing conversation history and thread-based message storage.
|
|
493
|
+
|
|
494
|
+
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.
|
|
495
|
+
|
|
496
|
+
## Usage example
|
|
497
|
+
|
|
498
|
+
```typescript title="src/mastra/agents/test-agent.ts"
|
|
499
|
+
import { Memory } from "@mastra/memory";
|
|
500
|
+
import { Agent } from "@mastra/core/agent";
|
|
501
|
+
|
|
502
|
+
export const agent = new Agent({
|
|
503
|
+
name: "test-agent",
|
|
504
|
+
instructions: "You are an agent with memory.",
|
|
505
|
+
model: "openai/gpt-5.1",
|
|
506
|
+
memory: new Memory({
|
|
507
|
+
options: {
|
|
508
|
+
workingMemory: {
|
|
509
|
+
enabled: true,
|
|
510
|
+
},
|
|
511
|
+
},
|
|
512
|
+
}),
|
|
513
|
+
});
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
> 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.
|
|
517
|
+
|
|
518
|
+
## Constructor parameters
|
|
519
|
+
|
|
520
|
+
### Options parameters
|
|
521
|
+
|
|
522
|
+
## Returns
|
|
523
|
+
|
|
524
|
+
## Extended usage example
|
|
525
|
+
|
|
526
|
+
```typescript title="src/mastra/agents/test-agent.ts"
|
|
527
|
+
import { Memory } from "@mastra/memory";
|
|
528
|
+
import { Agent } from "@mastra/core/agent";
|
|
529
|
+
import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
|
|
530
|
+
|
|
531
|
+
export const agent = new Agent({
|
|
532
|
+
name: "test-agent",
|
|
533
|
+
instructions: "You are an agent with memory.",
|
|
534
|
+
model: "openai/gpt-5.1",
|
|
535
|
+
memory: new Memory({
|
|
536
|
+
storage: new LibSQLStore({
|
|
537
|
+
id: 'test-agent-storage',
|
|
538
|
+
url: "file:./working-memory.db",
|
|
539
|
+
}),
|
|
540
|
+
vector: new LibSQLVector({
|
|
541
|
+
id: 'test-agent-vector',
|
|
542
|
+
connectionUrl: "file:./vector-memory.db",
|
|
543
|
+
}),
|
|
544
|
+
options: {
|
|
545
|
+
lastMessages: 10,
|
|
546
|
+
semanticRecall: {
|
|
547
|
+
topK: 3,
|
|
548
|
+
messageRange: 2,
|
|
549
|
+
scope: "resource",
|
|
550
|
+
},
|
|
551
|
+
workingMemory: {
|
|
552
|
+
enabled: true,
|
|
553
|
+
},
|
|
554
|
+
threads: {
|
|
555
|
+
generateTitle: true,
|
|
556
|
+
},
|
|
557
|
+
},
|
|
558
|
+
}),
|
|
559
|
+
});
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
## PostgreSQL with index configuration
|
|
563
|
+
|
|
564
|
+
```typescript title="src/mastra/agents/pg-agent.ts"
|
|
565
|
+
import { Memory } from "@mastra/memory";
|
|
566
|
+
import { Agent } from "@mastra/core/agent";
|
|
567
|
+
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
|
|
568
|
+
import { PgStore, PgVector } from "@mastra/pg";
|
|
569
|
+
|
|
570
|
+
export const agent = new Agent({
|
|
571
|
+
name: "pg-agent",
|
|
572
|
+
instructions: "You are an agent with optimized PostgreSQL memory.",
|
|
573
|
+
model: "openai/gpt-5.1",
|
|
574
|
+
memory: new Memory({
|
|
575
|
+
storage: new PgStore({
|
|
576
|
+
id: 'pg-agent-storage',
|
|
577
|
+
connectionString: process.env.DATABASE_URL,
|
|
578
|
+
}),
|
|
579
|
+
vector: new PgVector({
|
|
580
|
+
id: 'pg-agent-vector',
|
|
581
|
+
connectionString: process.env.DATABASE_URL,
|
|
582
|
+
}),
|
|
583
|
+
embedder: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
|
|
584
|
+
options: {
|
|
585
|
+
lastMessages: 20,
|
|
586
|
+
semanticRecall: {
|
|
587
|
+
topK: 5,
|
|
588
|
+
messageRange: 3,
|
|
589
|
+
scope: "resource",
|
|
590
|
+
indexConfig: {
|
|
591
|
+
type: "hnsw", // Use HNSW for better performance
|
|
592
|
+
metric: "dotproduct", // Optimal for OpenAI embeddings
|
|
593
|
+
m: 16, // Number of bi-directional links
|
|
594
|
+
efConstruction: 64, // Construction-time candidate list size
|
|
595
|
+
},
|
|
596
|
+
},
|
|
597
|
+
workingMemory: {
|
|
598
|
+
enabled: true,
|
|
599
|
+
},
|
|
600
|
+
},
|
|
601
|
+
}),
|
|
602
|
+
});
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
### Related
|
|
606
|
+
|
|
607
|
+
- [Getting Started with Memory](https://mastra.ai/docs/v1/memory/overview)
|
|
608
|
+
- [Semantic Recall](https://mastra.ai/docs/v1/memory/semantic-recall)
|
|
609
|
+
- [Working Memory](https://mastra.ai/docs/v1/memory/working-memory)
|
|
610
|
+
- [Memory Processors](https://mastra.ai/docs/v1/memory/memory-processors)
|
|
611
|
+
- [createThread](https://mastra.ai/reference/v1/memory/createThread)
|
|
612
|
+
- [recall](https://mastra.ai/reference/v1/memory/recall)
|
|
613
|
+
- [getThreadById](https://mastra.ai/reference/v1/memory/getThreadById)
|
|
614
|
+
- [listThreadsByResourceId](https://mastra.ai/reference/v1/memory/listThreadsByResourceId)
|
|
615
|
+
- [deleteMessages](https://mastra.ai/reference/v1/memory/deleteMessages)
|
|
616
|
+
- [cloneThread](https://mastra.ai/reference/v1/memory/cloneThread)
|
|
617
|
+
- [Clone Utility Methods](https://mastra.ai/reference/v1/memory/clone-utilities)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Processors API Reference
|
|
2
|
+
|
|
3
|
+
> API reference for processors - 1 entries
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Reference: Token Limiter Processor
|
|
9
|
+
|
|
10
|
+
> Documentation for the TokenLimiterProcessor in Mastra, which limits the number of tokens in messages.
|
|
11
|
+
|
|
12
|
+
The `TokenLimiterProcessor` limits the number of tokens in messages. It can be used as both an input and output processor:
|
|
13
|
+
|
|
14
|
+
- **Input processor**: Filters historical messages to fit within the context window, prioritizing recent messages
|
|
15
|
+
- **Output processor**: Limits generated response tokens via streaming or non-streaming with configurable strategies for handling exceeded limits
|
|
16
|
+
|
|
17
|
+
## Usage example
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { TokenLimiterProcessor } from "@mastra/core/processors";
|
|
21
|
+
|
|
22
|
+
const processor = new TokenLimiterProcessor({
|
|
23
|
+
limit: 1000,
|
|
24
|
+
strategy: "truncate",
|
|
25
|
+
countMode: "cumulative"
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Constructor parameters
|
|
30
|
+
|
|
31
|
+
### Options
|
|
32
|
+
|
|
33
|
+
## Returns
|
|
34
|
+
|
|
35
|
+
## Extended usage example
|
|
36
|
+
|
|
37
|
+
### As an input processor (limit context window)
|
|
38
|
+
|
|
39
|
+
Use `inputProcessors` to limit historical messages sent to the model, which helps stay within context window limits:
|
|
40
|
+
|
|
41
|
+
```typescript title="src/mastra/agents/context-limited-agent.ts"
|
|
42
|
+
import { Agent } from "@mastra/core/agent";
|
|
43
|
+
import { Memory } from "@mastra/memory";
|
|
44
|
+
import { TokenLimiterProcessor } from "@mastra/core/processors";
|
|
45
|
+
|
|
46
|
+
export const agent = new Agent({
|
|
47
|
+
name: "context-limited-agent",
|
|
48
|
+
instructions: "You are a helpful assistant",
|
|
49
|
+
model: "openai/gpt-4o",
|
|
50
|
+
memory: new Memory({ /* ... */ }),
|
|
51
|
+
inputProcessors: [
|
|
52
|
+
new TokenLimiterProcessor({ limit: 4000 }) // Limits historical messages to ~4000 tokens
|
|
53
|
+
]
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### As an output processor (limit response length)
|
|
58
|
+
|
|
59
|
+
Use `outputProcessors` to limit the length of generated responses:
|
|
60
|
+
|
|
61
|
+
```typescript title="src/mastra/agents/response-limited-agent.ts"
|
|
62
|
+
import { Agent } from "@mastra/core/agent";
|
|
63
|
+
import { TokenLimiterProcessor } from "@mastra/core/processors";
|
|
64
|
+
|
|
65
|
+
export const agent = new Agent({
|
|
66
|
+
name: "response-limited-agent",
|
|
67
|
+
instructions: "You are a helpful assistant",
|
|
68
|
+
model: "openai/gpt-4o",
|
|
69
|
+
outputProcessors: [
|
|
70
|
+
new TokenLimiterProcessor({
|
|
71
|
+
limit: 1000,
|
|
72
|
+
strategy: "truncate",
|
|
73
|
+
countMode: "cumulative"
|
|
74
|
+
})
|
|
75
|
+
]
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Related
|
|
80
|
+
|
|
81
|
+
- [Guardrails](https://mastra.ai/docs/v1/agents/guardrails)
|