@mastra/memory 1.5.1 → 1.5.2
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 +28 -0
- package/dist/{chunk-6PKWQ3GH.js → chunk-HNPAIFCZ.js} +59 -16
- package/dist/chunk-HNPAIFCZ.js.map +1 -0
- package/dist/{chunk-6XVTMLW4.cjs → chunk-PVFLHAZX.cjs} +59 -16
- package/dist/chunk-PVFLHAZX.cjs.map +1 -0
- package/dist/docs/SKILL.md +55 -0
- package/dist/docs/assets/SOURCE_MAP.json +103 -0
- package/dist/docs/references/docs-agents-agent-approval.md +558 -0
- package/dist/docs/references/docs-agents-agent-memory.md +209 -0
- package/dist/docs/references/docs-agents-network-approval.md +275 -0
- package/dist/docs/references/docs-agents-networks.md +299 -0
- package/dist/docs/references/docs-agents-supervisor-agents.md +304 -0
- package/dist/docs/references/docs-memory-memory-processors.md +314 -0
- package/dist/docs/references/docs-memory-message-history.md +260 -0
- package/dist/docs/references/docs-memory-observational-memory.md +248 -0
- package/dist/docs/references/docs-memory-overview.md +45 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +272 -0
- package/dist/docs/references/docs-memory-storage.md +261 -0
- package/dist/docs/references/docs-memory-working-memory.md +400 -0
- package/dist/docs/references/reference-core-getMemory.md +50 -0
- package/dist/docs/references/reference-core-listMemory.md +56 -0
- package/dist/docs/references/reference-memory-clone-utilities.md +199 -0
- package/dist/docs/references/reference-memory-cloneThread.md +130 -0
- package/dist/docs/references/reference-memory-createThread.md +68 -0
- package/dist/docs/references/reference-memory-getThreadById.md +24 -0
- package/dist/docs/references/reference-memory-listThreads.md +145 -0
- package/dist/docs/references/reference-memory-memory-class.md +147 -0
- package/dist/docs/references/reference-memory-observational-memory.md +565 -0
- package/dist/docs/references/reference-processors-token-limiter-processor.md +115 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-libsql.md +135 -0
- package/dist/docs/references/reference-storage-mongodb.md +262 -0
- package/dist/docs/references/reference-storage-postgresql.md +526 -0
- package/dist/docs/references/reference-storage-upstash.md +160 -0
- package/dist/docs/references/reference-vectors-libsql.md +305 -0
- package/dist/docs/references/reference-vectors-mongodb.md +295 -0
- package/dist/docs/references/reference-vectors-pg.md +408 -0
- package/dist/docs/references/reference-vectors-upstash.md +294 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{observational-memory-AJWSMZVP.js → observational-memory-KAFD4QZK.js} +3 -3
- package/dist/{observational-memory-AJWSMZVP.js.map → observational-memory-KAFD4QZK.js.map} +1 -1
- package/dist/{observational-memory-Q5TO525O.cjs → observational-memory-Q47HN5YL.cjs} +17 -17
- package/dist/{observational-memory-Q5TO525O.cjs.map → observational-memory-Q47HN5YL.cjs.map} +1 -1
- package/dist/processors/index.cjs +15 -15
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +2 -2
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/chunk-6PKWQ3GH.js.map +0 -1
- package/dist/chunk-6XVTMLW4.cjs.map +0 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Mastra.getMemory()
|
|
2
|
+
|
|
3
|
+
The `.getMemory()` method retrieves a memory instance from the Mastra registry by its key. Memory instances are registered in the Mastra constructor and can be referenced by stored agents.
|
|
4
|
+
|
|
5
|
+
## Usage example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
const memory = mastra.getMemory('conversationMemory')
|
|
9
|
+
|
|
10
|
+
// Use the memory instance
|
|
11
|
+
const thread = await memory.createThread({
|
|
12
|
+
resourceId: 'user-123',
|
|
13
|
+
title: 'New Conversation',
|
|
14
|
+
})
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Parameters
|
|
18
|
+
|
|
19
|
+
**key:** (`TMemoryKey extends keyof TMemory`): The registry key of the memory instance to retrieve. Must match a key used when registering memory in the Mastra constructor.
|
|
20
|
+
|
|
21
|
+
## Returns
|
|
22
|
+
|
|
23
|
+
**memory:** (`TMemory[TMemoryKey]`): The memory instance with the specified key. Throws an error if the memory is not found.
|
|
24
|
+
|
|
25
|
+
## Example: Registering and Retrieving Memory
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Mastra } from '@mastra/core'
|
|
29
|
+
import { Memory } from '@mastra/memory'
|
|
30
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
31
|
+
|
|
32
|
+
const conversationMemory = new Memory({
|
|
33
|
+
storage: new LibSQLStore({ id: 'conversation-store', url: ':memory:' }),
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const mastra = new Mastra({
|
|
37
|
+
memory: {
|
|
38
|
+
conversationMemory,
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
// Later, retrieve the memory instance
|
|
43
|
+
const memory = mastra.getMemory('conversationMemory')
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Related
|
|
47
|
+
|
|
48
|
+
- [Mastra.listMemory()](https://mastra.ai/reference/core/listMemory)
|
|
49
|
+
- [Memory overview](https://mastra.ai/docs/memory/overview)
|
|
50
|
+
- [Agent Memory](https://mastra.ai/docs/agents/agent-memory)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Mastra.listMemory()
|
|
2
|
+
|
|
3
|
+
The `.listMemory()` method returns all memory instances registered with the Mastra instance.
|
|
4
|
+
|
|
5
|
+
## Usage example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
const memoryInstances = mastra.listMemory()
|
|
9
|
+
|
|
10
|
+
for (const [key, memory] of Object.entries(memoryInstances)) {
|
|
11
|
+
console.log(`Memory "${key}": ${memory.id}`)
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Parameters
|
|
16
|
+
|
|
17
|
+
This method takes no parameters.
|
|
18
|
+
|
|
19
|
+
## Returns
|
|
20
|
+
|
|
21
|
+
**memory:** (`Record<string, MastraMemory>`): An object containing all registered memory instances, keyed by their registry keys.
|
|
22
|
+
|
|
23
|
+
## Example: Checking Registered Memory
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { Mastra } from '@mastra/core'
|
|
27
|
+
import { Memory } from '@mastra/memory'
|
|
28
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
29
|
+
|
|
30
|
+
const conversationMemory = new Memory({
|
|
31
|
+
id: 'conversation-memory',
|
|
32
|
+
storage: new LibSQLStore({ id: 'conversation-store', url: ':memory:' }),
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const analyticsMemory = new Memory({
|
|
36
|
+
id: 'analytics-memory',
|
|
37
|
+
storage: new LibSQLStore({ id: 'analytics-store', url: ':memory:' }),
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const mastra = new Mastra({
|
|
41
|
+
memory: {
|
|
42
|
+
conversationMemory,
|
|
43
|
+
analyticsMemory,
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
// List all registered memory instances
|
|
48
|
+
const allMemory = mastra.listMemory()
|
|
49
|
+
console.log(Object.keys(allMemory)) // ["conversationMemory", "analyticsMemory"]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Related
|
|
53
|
+
|
|
54
|
+
- [Mastra.getMemory()](https://mastra.ai/reference/core/getMemory)
|
|
55
|
+
- [Memory overview](https://mastra.ai/docs/memory/overview)
|
|
56
|
+
- [Agent Memory](https://mastra.ai/docs/agents/agent-memory)
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# Cloned Thread Utilities
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
## isClone()
|
|
6
|
+
|
|
7
|
+
Checks whether a thread is a clone of another thread.
|
|
8
|
+
|
|
9
|
+
### Usage
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
const isClonedThread = memory.isClone(thread)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Parameters
|
|
16
|
+
|
|
17
|
+
**thread:** (`StorageThreadType`): The thread object to check.
|
|
18
|
+
|
|
19
|
+
### Example
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
const thread = await memory.getThreadById({ threadId: 'some-thread-id' })
|
|
23
|
+
|
|
24
|
+
if (memory.isClone(thread)) {
|
|
25
|
+
console.log('This thread was cloned from another thread')
|
|
26
|
+
} else {
|
|
27
|
+
console.log('This is an original thread')
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## getCloneMetadata()
|
|
32
|
+
|
|
33
|
+
Retrieves the clone metadata from a thread if it exists.
|
|
34
|
+
|
|
35
|
+
### Usage
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
const metadata = memory.getCloneMetadata(thread)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Parameters
|
|
42
|
+
|
|
43
|
+
**thread:** (`StorageThreadType`): The thread object to extract clone metadata from.
|
|
44
|
+
|
|
45
|
+
### Example
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
const thread = await memory.getThreadById({ threadId: 'cloned-thread-id' })
|
|
49
|
+
const cloneInfo = memory.getCloneMetadata(thread)
|
|
50
|
+
|
|
51
|
+
if (cloneInfo) {
|
|
52
|
+
console.log(`Cloned from: ${cloneInfo.sourceThreadId}`)
|
|
53
|
+
console.log(`Cloned at: ${cloneInfo.clonedAt}`)
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## getSourceThread()
|
|
58
|
+
|
|
59
|
+
Retrieves the original source thread that a cloned thread was created from.
|
|
60
|
+
|
|
61
|
+
### Usage
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
const sourceThread = await memory.getSourceThread(threadId)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Parameters
|
|
68
|
+
|
|
69
|
+
**threadId:** (`string`): The ID of the cloned thread.
|
|
70
|
+
|
|
71
|
+
### Example
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const sourceThread = await memory.getSourceThread('cloned-thread-id')
|
|
75
|
+
|
|
76
|
+
if (sourceThread) {
|
|
77
|
+
console.log(`Original thread title: ${sourceThread.title}`)
|
|
78
|
+
console.log(`Original thread created: ${sourceThread.createdAt}`)
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## listClones()
|
|
83
|
+
|
|
84
|
+
Lists all threads that were cloned from a specific source thread.
|
|
85
|
+
|
|
86
|
+
### Usage
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
const clones = await memory.listClones(sourceThreadId)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Parameters
|
|
93
|
+
|
|
94
|
+
**sourceThreadId:** (`string`): The ID of the source thread to find clones for.
|
|
95
|
+
|
|
96
|
+
### Example
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const clones = await memory.listClones('original-thread-id')
|
|
100
|
+
|
|
101
|
+
console.log(`Found ${clones.length} clones`)
|
|
102
|
+
for (const clone of clones) {
|
|
103
|
+
console.log(`- ${clone.id}: ${clone.title}`)
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## getCloneHistory()
|
|
108
|
+
|
|
109
|
+
Retrieves the full clone history chain for a thread, tracing back to the original.
|
|
110
|
+
|
|
111
|
+
### Usage
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const history = await memory.getCloneHistory(threadId)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Parameters
|
|
118
|
+
|
|
119
|
+
**threadId:** (`string`): The ID of the thread to get clone history for.
|
|
120
|
+
|
|
121
|
+
### Example
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// If thread-c was cloned from thread-b, which was cloned from thread-a
|
|
125
|
+
const history = await memory.getCloneHistory('thread-c')
|
|
126
|
+
|
|
127
|
+
// history = [thread-a, thread-b, thread-c]
|
|
128
|
+
console.log(`Clone depth: ${history.length - 1}`)
|
|
129
|
+
console.log(`Original thread: ${history[0].id}`)
|
|
130
|
+
console.log(`Current thread: ${history[history.length - 1].id}`)
|
|
131
|
+
|
|
132
|
+
// Display the clone chain
|
|
133
|
+
for (let i = 0; i < history.length; i++) {
|
|
134
|
+
const prefix = i === 0 ? 'Original' : `Clone ${i}`
|
|
135
|
+
console.log(`${prefix}: ${history[i].title}`)
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Complete Example
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { mastra } from './mastra'
|
|
143
|
+
|
|
144
|
+
async function manageClones() {
|
|
145
|
+
const agent = mastra.getAgent('agent')
|
|
146
|
+
const memory = await agent.getMemory()
|
|
147
|
+
|
|
148
|
+
// Create an original conversation
|
|
149
|
+
const originalThread = await memory.createThread({
|
|
150
|
+
resourceId: 'user-123',
|
|
151
|
+
title: 'Original Conversation',
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
// Have a conversation...
|
|
155
|
+
await agent.generate("Hello! Let's discuss project options.", {
|
|
156
|
+
threadId: originalThread.id,
|
|
157
|
+
resourceId: 'user-123',
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
// Create multiple branches (clones) to explore different paths
|
|
161
|
+
const { thread: optionA } = await memory.cloneThread({
|
|
162
|
+
sourceThreadId: originalThread.id,
|
|
163
|
+
title: 'Option A - Conservative Approach',
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
const { thread: optionB } = await memory.cloneThread({
|
|
167
|
+
sourceThreadId: originalThread.id,
|
|
168
|
+
title: 'Option B - Aggressive Approach',
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
// Check clone status
|
|
172
|
+
console.log(memory.isClone(originalThread)) // false
|
|
173
|
+
console.log(memory.isClone(optionA)) // true
|
|
174
|
+
console.log(memory.isClone(optionB)) // true
|
|
175
|
+
|
|
176
|
+
// Get clone metadata
|
|
177
|
+
const metadataA = memory.getCloneMetadata(optionA)
|
|
178
|
+
console.log(metadataA?.sourceThreadId) // originalThread.id
|
|
179
|
+
|
|
180
|
+
// List all clones of the original
|
|
181
|
+
const allClones = await memory.listClones(originalThread.id)
|
|
182
|
+
console.log(`Total alternatives: ${allClones.length}`) // 2
|
|
183
|
+
|
|
184
|
+
// Get source thread from a clone
|
|
185
|
+
const source = await memory.getSourceThread(optionA.id)
|
|
186
|
+
console.log(source?.id === originalThread.id) // true
|
|
187
|
+
|
|
188
|
+
// Create a deeper clone chain
|
|
189
|
+
const { thread: optionA2 } = await memory.cloneThread({
|
|
190
|
+
sourceThreadId: optionA.id,
|
|
191
|
+
title: 'Option A - Variant 2',
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
// Get the full history
|
|
195
|
+
const history = await memory.getCloneHistory(optionA2.id)
|
|
196
|
+
// history = [originalThread, optionA, optionA2]
|
|
197
|
+
console.log(`Clone depth: ${history.length - 1}`) // 2
|
|
198
|
+
}
|
|
199
|
+
```
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Memory.cloneThread()
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
## Usage Example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
const { thread, clonedMessages } = await memory.cloneThread({
|
|
9
|
+
sourceThreadId: 'original-thread-123',
|
|
10
|
+
})
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Parameters
|
|
14
|
+
|
|
15
|
+
**sourceThreadId:** (`string`): The ID of the thread to clone
|
|
16
|
+
|
|
17
|
+
**newThreadId?:** (`string`): Optional custom ID for the cloned thread. If not provided, one will be generated.
|
|
18
|
+
|
|
19
|
+
**resourceId?:** (`string`): Optional resource ID for the cloned thread. Defaults to the source thread's resourceId.
|
|
20
|
+
|
|
21
|
+
**title?:** (`string`): Optional title for the cloned thread. Defaults to '\[source title] (Copy)'.
|
|
22
|
+
|
|
23
|
+
**metadata?:** (`Record<string, unknown>`): Optional metadata to merge with the source thread's metadata. Clone metadata is automatically added.
|
|
24
|
+
|
|
25
|
+
**options?:** (`CloneOptions`): Optional filtering options for the clone operation.
|
|
26
|
+
|
|
27
|
+
### Options Parameters
|
|
28
|
+
|
|
29
|
+
**messageLimit?:** (`number`): Maximum number of messages to clone. When set, clones the most recent N messages.
|
|
30
|
+
|
|
31
|
+
**messageFilter?:** (`MessageFilter`): Filter criteria for selecting which messages to clone.
|
|
32
|
+
|
|
33
|
+
### MessageFilter Parameters
|
|
34
|
+
|
|
35
|
+
**startDate?:** (`Date`): Only clone messages created on or after this date.
|
|
36
|
+
|
|
37
|
+
**endDate?:** (`Date`): Only clone messages created on or before this date.
|
|
38
|
+
|
|
39
|
+
**messageIds?:** (`string[]`): Only clone messages with these specific IDs.
|
|
40
|
+
|
|
41
|
+
## Returns
|
|
42
|
+
|
|
43
|
+
**thread:** (`StorageThreadType`): The newly created cloned thread with clone metadata.
|
|
44
|
+
|
|
45
|
+
**clonedMessages:** (`MastraDBMessage[]`): Array of the cloned messages with new IDs assigned to the new thread.
|
|
46
|
+
|
|
47
|
+
### Clone Metadata
|
|
48
|
+
|
|
49
|
+
The cloned thread's metadata includes a `clone` property with:
|
|
50
|
+
|
|
51
|
+
**sourceThreadId:** (`string`): The ID of the original thread that was cloned.
|
|
52
|
+
|
|
53
|
+
**clonedAt:** (`Date`): Timestamp when the clone was created.
|
|
54
|
+
|
|
55
|
+
**lastMessageId?:** (`string`): The ID of the last message in the source thread at the time of cloning.
|
|
56
|
+
|
|
57
|
+
## Extended Usage Example
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { mastra } from './mastra'
|
|
61
|
+
|
|
62
|
+
const agent = mastra.getAgent('agent')
|
|
63
|
+
const memory = await agent.getMemory()
|
|
64
|
+
|
|
65
|
+
// Clone a thread with all messages
|
|
66
|
+
const { thread: fullClone } = await memory.cloneThread({
|
|
67
|
+
sourceThreadId: 'original-thread-123',
|
|
68
|
+
title: 'Alternative Conversation Path',
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
// Clone with a custom ID
|
|
72
|
+
const { thread: customIdClone } = await memory.cloneThread({
|
|
73
|
+
sourceThreadId: 'original-thread-123',
|
|
74
|
+
newThreadId: 'my-custom-clone-id',
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
// Clone only the last 5 messages
|
|
78
|
+
const { thread: partialClone, clonedMessages } = await memory.cloneThread({
|
|
79
|
+
sourceThreadId: 'original-thread-123',
|
|
80
|
+
options: {
|
|
81
|
+
messageLimit: 5,
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
// Clone messages from a specific date range
|
|
86
|
+
const { thread: dateFilteredClone } = await memory.cloneThread({
|
|
87
|
+
sourceThreadId: 'original-thread-123',
|
|
88
|
+
options: {
|
|
89
|
+
messageFilter: {
|
|
90
|
+
startDate: new Date('2024-01-01'),
|
|
91
|
+
endDate: new Date('2024-01-31'),
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// Continue conversation on the cloned thread
|
|
97
|
+
const response = await agent.generate("Let's try a different approach", {
|
|
98
|
+
threadId: fullClone.id,
|
|
99
|
+
resourceId: fullClone.resourceId,
|
|
100
|
+
})
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Vector Embeddings
|
|
104
|
+
|
|
105
|
+
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.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { Memory } from '@mastra/memory'
|
|
109
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
110
|
+
|
|
111
|
+
const memory = new Memory({
|
|
112
|
+
storage: new LibSQLStore({ id: 'memory-store', url: 'file:./memory.db' }),
|
|
113
|
+
vector: new LibSQLVector({ id: 'vector-store', url: 'file:./vector.db' }),
|
|
114
|
+
embedder: embeddingModel,
|
|
115
|
+
options: {
|
|
116
|
+
semanticRecall: true,
|
|
117
|
+
},
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
// Clone will also create embeddings for cloned messages
|
|
121
|
+
const { thread } = await memory.cloneThread({
|
|
122
|
+
sourceThreadId: 'original-thread',
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
// Semantic search works on the cloned thread
|
|
126
|
+
const results = await memory.recall({
|
|
127
|
+
threadId: thread.id,
|
|
128
|
+
vectorSearchString: 'search query',
|
|
129
|
+
})
|
|
130
|
+
```
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Memory.createThread()
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
## Usage Example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
await memory?.createThread({ resourceId: 'user-123' })
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
**resourceId:** (`string`): Identifier for the resource this thread belongs to (e.g., user ID, project ID)
|
|
14
|
+
|
|
15
|
+
**threadId?:** (`string`): Optional custom ID for the thread. If not provided, one will be generated.
|
|
16
|
+
|
|
17
|
+
**title?:** (`string`): Optional title for the thread
|
|
18
|
+
|
|
19
|
+
**metadata?:** (`Record<string, unknown>`): Optional metadata to associate with the thread
|
|
20
|
+
|
|
21
|
+
## Returns
|
|
22
|
+
|
|
23
|
+
**id:** (`string`): Unique identifier of the created thread
|
|
24
|
+
|
|
25
|
+
**resourceId:** (`string`): Resource ID associated with the thread
|
|
26
|
+
|
|
27
|
+
**title:** (`string`): Title of the thread (if provided)
|
|
28
|
+
|
|
29
|
+
**createdAt:** (`Date`): Timestamp when the thread was created
|
|
30
|
+
|
|
31
|
+
**updatedAt:** (`Date`): Timestamp when the thread was last updated
|
|
32
|
+
|
|
33
|
+
**metadata:** (`Record<string, unknown>`): Additional metadata associated with the thread
|
|
34
|
+
|
|
35
|
+
## Extended usage example
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { mastra } from './mastra'
|
|
39
|
+
|
|
40
|
+
const agent = mastra.getAgent('agent')
|
|
41
|
+
const memory = await agent.getMemory()
|
|
42
|
+
|
|
43
|
+
const thread = await memory?.createThread({
|
|
44
|
+
resourceId: 'user-123',
|
|
45
|
+
title: 'Memory Test Thread',
|
|
46
|
+
metadata: {
|
|
47
|
+
source: 'test-script',
|
|
48
|
+
purpose: 'memory-testing',
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const response = await agent.generate('message for agent', {
|
|
53
|
+
memory: {
|
|
54
|
+
thread: thread!.id,
|
|
55
|
+
resource: thread!.resourceId,
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
console.log(response.text)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Related
|
|
63
|
+
|
|
64
|
+
- [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
|
|
65
|
+
- [Getting Started with Memory](https://mastra.ai/docs/memory/overview) (Covers threads concept)
|
|
66
|
+
- [getThreadById](https://mastra.ai/reference/memory/getThreadById)
|
|
67
|
+
- [listThreads](https://mastra.ai/reference/memory/listThreads)
|
|
68
|
+
- [recall](https://mastra.ai/reference/memory/recall)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Memory.getThreadById()
|
|
2
|
+
|
|
3
|
+
The `.getThreadById()` method retrieves a specific thread by its ID.
|
|
4
|
+
|
|
5
|
+
## Usage Example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
await memory?.getThreadById({ threadId: 'thread-123' })
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
**threadId:** (`string`): The ID of the thread to be retrieved.
|
|
14
|
+
|
|
15
|
+
## Returns
|
|
16
|
+
|
|
17
|
+
**thread:** (`Promise<StorageThreadType | null>`): A promise that resolves to the thread associated with the given ID, or null if not found.
|
|
18
|
+
|
|
19
|
+
### Related
|
|
20
|
+
|
|
21
|
+
- [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
|
|
22
|
+
- [Getting Started with Memory](https://mastra.ai/docs/memory/overview) (Covers threads concept)
|
|
23
|
+
- [createThread](https://mastra.ai/reference/memory/createThread)
|
|
24
|
+
- [listThreads](https://mastra.ai/reference/memory/listThreads)
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Memory.listThreads()
|
|
2
|
+
|
|
3
|
+
The `listThreads()` method retrieves threads with pagination support and optional filtering by `resourceId`, `metadata`, or both.
|
|
4
|
+
|
|
5
|
+
## Usage Examples
|
|
6
|
+
|
|
7
|
+
### List all threads with pagination
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
const result = await memory.listThreads({
|
|
11
|
+
page: 0,
|
|
12
|
+
perPage: 10,
|
|
13
|
+
})
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Fetch all threads without pagination
|
|
17
|
+
|
|
18
|
+
Use `perPage: false` to retrieve all matching threads at once.
|
|
19
|
+
|
|
20
|
+
> **Warning:** Generally speaking it's recommended to use pagination, especially for large datasets. Use this option cautiously.
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
const result = await memory.listThreads({
|
|
24
|
+
filter: { resourceId: 'user-123' },
|
|
25
|
+
perPage: false,
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Filter by resourceId
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
const result = await memory.listThreads({
|
|
33
|
+
filter: { resourceId: 'user-123' },
|
|
34
|
+
page: 0,
|
|
35
|
+
perPage: 10,
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Filter by metadata
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const result = await memory.listThreads({
|
|
43
|
+
filter: { metadata: { category: 'support', priority: 'high' } },
|
|
44
|
+
page: 0,
|
|
45
|
+
perPage: 10,
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Combined filter (resourceId & metadata)
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const result = await memory.listThreads({
|
|
53
|
+
filter: {
|
|
54
|
+
resourceId: 'user-123',
|
|
55
|
+
metadata: { status: 'active' },
|
|
56
|
+
},
|
|
57
|
+
page: 0,
|
|
58
|
+
perPage: 10,
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Parameters
|
|
63
|
+
|
|
64
|
+
**filter?:** (`{ resourceId?: string; metadata?: Record<string, unknown> }`): Optional filter object. resourceId filters threads by resource ID. metadata filters threads by metadata key-value pairs (AND logic - all must match)
|
|
65
|
+
|
|
66
|
+
**page?:** (`number`): Page number (0-indexed) to retrieve
|
|
67
|
+
|
|
68
|
+
**perPage?:** (`number | false`): Maximum number of threads to return per page, or false to fetch all
|
|
69
|
+
|
|
70
|
+
**orderBy?:** (`{ field: 'createdAt' | 'updatedAt', direction: 'ASC' | 'DESC' }`): Sort configuration with field and direction (defaults to { field: 'createdAt', direction: 'DESC' })
|
|
71
|
+
|
|
72
|
+
## Returns
|
|
73
|
+
|
|
74
|
+
**result:** (`Promise<StorageListThreadsOutput>`): A promise that resolves to paginated thread results with metadata
|
|
75
|
+
|
|
76
|
+
The return object contains:
|
|
77
|
+
|
|
78
|
+
- `threads`: Array of thread objects
|
|
79
|
+
- `total`: Total number of threads matching the filter
|
|
80
|
+
- `page`: Current page number (same as the input `page` parameter)
|
|
81
|
+
- `perPage`: Items per page (same as the input `perPage` parameter)
|
|
82
|
+
- `hasMore`: Boolean indicating if more results are available
|
|
83
|
+
|
|
84
|
+
## Extended usage example
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { mastra } from './mastra'
|
|
88
|
+
|
|
89
|
+
const agent = mastra.getAgent('agent')
|
|
90
|
+
const memory = await agent.getMemory()
|
|
91
|
+
|
|
92
|
+
let currentPage = 0
|
|
93
|
+
const perPage = 25
|
|
94
|
+
let hasMorePages = true
|
|
95
|
+
|
|
96
|
+
// Fetch all active threads for a user, sorted by creation date
|
|
97
|
+
while (hasMorePages) {
|
|
98
|
+
const result = await memory?.listThreads({
|
|
99
|
+
filter: {
|
|
100
|
+
resourceId: 'user-123',
|
|
101
|
+
metadata: { status: 'active' },
|
|
102
|
+
},
|
|
103
|
+
page: currentPage,
|
|
104
|
+
perPage: perPage,
|
|
105
|
+
orderBy: { field: 'createdAt', direction: 'ASC' },
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
if (!result) {
|
|
109
|
+
console.log('No threads')
|
|
110
|
+
break
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
result.threads.forEach(thread => {
|
|
114
|
+
console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
hasMorePages = result.hasMore
|
|
118
|
+
currentPage++ // Move to next page
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Metadata Filtering
|
|
123
|
+
|
|
124
|
+
The metadata filter uses AND logic - all specified key-value pairs must match for a thread to be included in the results:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
// This will only return threads where BOTH conditions are true:
|
|
128
|
+
// - category === 'support'
|
|
129
|
+
// - priority === 'high'
|
|
130
|
+
await memory.listThreads({
|
|
131
|
+
filter: {
|
|
132
|
+
metadata: {
|
|
133
|
+
category: 'support',
|
|
134
|
+
priority: 'high',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Related
|
|
141
|
+
|
|
142
|
+
- [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
|
|
143
|
+
- [Getting Started with Memory](https://mastra.ai/docs/memory/overview)
|
|
144
|
+
- [createThread](https://mastra.ai/reference/memory/createThread)
|
|
145
|
+
- [getThreadById](https://mastra.ai/reference/memory/getThreadById)
|