@mastra/memory 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807182016
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 +4656 -3
- package/LICENSE.md +15 -0
- package/README.md +26 -1
- package/dist/_types/@internal_ai-sdk-v4/dist/index.d.ts +7450 -0
- package/dist/docs/SKILL.md +62 -0
- package/dist/docs/assets/SOURCE_MAP.json +11 -0
- package/dist/docs/references/docs-agents-agent-approval.md +664 -0
- package/dist/docs/references/docs-agents-networks.md +184 -0
- package/dist/docs/references/docs-capabilities-subagents.md +454 -0
- package/dist/docs/references/docs-evals-evals-with-memory.md +146 -0
- package/dist/docs/references/docs-long-running-agents-background-tasks.md +382 -0
- package/dist/docs/references/docs-long-running-agents-goals.md +118 -0
- package/dist/docs/references/docs-memory-memory-processors.md +385 -0
- package/dist/docs/references/docs-memory-message-history.md +348 -0
- package/dist/docs/references/docs-memory-multi-user-threads.md +208 -0
- package/dist/docs/references/docs-memory-observational-memory.md +835 -0
- package/dist/docs/references/docs-memory-overview.md +266 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
- package/dist/docs/references/docs-memory-working-memory.md +431 -0
- package/dist/docs/references/docs-storage-overview.md +214 -0
- package/dist/docs/references/reference-core-getMemory.md +51 -0
- package/dist/docs/references/reference-core-listMemory.md +57 -0
- package/dist/docs/references/reference-file-based-agents-memory.md +58 -0
- package/dist/docs/references/reference-memory-clone-utilities.md +203 -0
- package/dist/docs/references/reference-memory-cloneThread.md +173 -0
- package/dist/docs/references/reference-memory-createThread.md +70 -0
- package/dist/docs/references/reference-memory-getThreadById.md +26 -0
- package/dist/docs/references/reference-memory-listThreads.md +147 -0
- package/dist/docs/references/reference-memory-memory-class.md +148 -0
- package/dist/docs/references/reference-memory-observational-memory.md +877 -0
- package/dist/docs/references/reference-memory-summarizeConversation.md +99 -0
- package/dist/docs/references/reference-memory-summarizeThread.md +93 -0
- package/dist/docs/references/reference-processors-token-limiter-processor.md +158 -0
- package/dist/docs/references/reference-storage-dsql.md +430 -0
- package/dist/docs/references/reference-storage-dynamodb.md +284 -0
- package/dist/docs/references/reference-storage-libsql.md +143 -0
- package/dist/docs/references/reference-storage-mongodb.md +267 -0
- package/dist/docs/references/reference-storage-postgresql.md +531 -0
- package/dist/docs/references/reference-storage-redis.md +268 -0
- package/dist/docs/references/reference-storage-upstash.md +162 -0
- package/dist/docs/references/reference-vectors-libsql.md +307 -0
- package/dist/docs/references/reference-vectors-mongodb.md +567 -0
- package/dist/docs/references/reference-vectors-pg.md +430 -0
- package/dist/docs/references/reference-vectors-upstash.md +296 -0
- package/dist/index.cjs +35 -893
- package/dist/index.d.ts +465 -62
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -887
- package/dist/processors/index.cjs +32 -165
- package/dist/processors/index.d.ts +1 -2
- package/dist/processors/index.d.ts.map +1 -1
- package/dist/processors/index.js +2 -158
- package/dist/processors/observational-memory/activation-ttl.d.ts +4 -0
- package/dist/processors/observational-memory/activation-ttl.d.ts.map +1 -0
- package/dist/processors/observational-memory/anchor-ids.d.ts +4 -0
- package/dist/processors/observational-memory/anchor-ids.d.ts.map +1 -0
- package/dist/processors/observational-memory/buffering-coordinator.d.ts +61 -0
- package/dist/processors/observational-memory/buffering-coordinator.d.ts.map +1 -0
- package/dist/processors/observational-memory/built-in-extractors.d.ts +15 -0
- package/dist/processors/observational-memory/built-in-extractors.d.ts.map +1 -0
- package/dist/processors/observational-memory/constants.d.ts +74 -0
- package/dist/processors/observational-memory/constants.d.ts.map +1 -0
- package/dist/processors/observational-memory/date-utils.d.ts +41 -0
- package/dist/processors/observational-memory/date-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/debug.d.ts +3 -0
- package/dist/processors/observational-memory/debug.d.ts.map +1 -0
- package/dist/processors/observational-memory/extracted-values.d.ts +44 -0
- package/dist/processors/observational-memory/extracted-values.d.ts.map +1 -0
- package/dist/processors/observational-memory/extraction-runner.d.ts +22 -0
- package/dist/processors/observational-memory/extraction-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/extractor.d.ts +76 -0
- package/dist/processors/observational-memory/extractor.d.ts.map +1 -0
- package/dist/processors/observational-memory/index.d.ts +30 -0
- package/dist/processors/observational-memory/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/internal-request-context.d.ts +17 -0
- package/dist/processors/observational-memory/internal-request-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/markers.d.ts +118 -0
- package/dist/processors/observational-memory/markers.d.ts.map +1 -0
- package/dist/processors/observational-memory/message-utils.d.ts +83 -0
- package/dist/processors/observational-memory/message-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/model-by-input-tokens.d.ts +14 -0
- package/dist/processors/observational-memory/model-by-input-tokens.d.ts.map +1 -0
- package/dist/processors/observational-memory/model-context.d.ts +2 -0
- package/dist/processors/observational-memory/model-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-groups.d.ts +15 -0
- package/dist/processors/observational-memory/observation-groups.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts +39 -0
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/base.d.ts +122 -0
- package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/index.d.ts +7 -0
- package/dist/processors/observational-memory/observation-strategies/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts +43 -0
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts +41 -0
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/types.d.ts +103 -0
- package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/index.d.ts +4 -0
- package/dist/processors/observational-memory/observation-turn/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts +9 -0
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/step.d.ts +53 -0
- package/dist/processors/observational-memory/observation-turn/step.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/turn.d.ts +139 -0
- package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/types.d.ts +46 -0
- package/dist/processors/observational-memory/observation-turn/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-utils.d.ts +16 -0
- package/dist/processors/observational-memory/observation-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/observational-memory.d.ts +930 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -0
- package/dist/processors/observational-memory/observer-agent.d.ts +190 -0
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/observer-runner.d.ts +141 -0
- package/dist/processors/observational-memory/observer-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/operation-registry.d.ts +14 -0
- package/dist/processors/observational-memory/operation-registry.d.ts.map +1 -0
- package/dist/processors/observational-memory/processor.d.ts +70 -0
- package/dist/processors/observational-memory/processor.d.ts.map +1 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts +62 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/reflector-runner.d.ts +133 -0
- package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/repro-capture.d.ts +33 -0
- package/dist/processors/observational-memory/repro-capture.d.ts.map +1 -0
- package/dist/processors/observational-memory/retry.d.ts +63 -0
- package/dist/processors/observational-memory/retry.d.ts.map +1 -0
- package/dist/processors/observational-memory/string-utils.d.ts +13 -0
- package/dist/processors/observational-memory/string-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/summarize.d.ts +92 -0
- package/dist/processors/observational-memory/summarize.d.ts.map +1 -0
- package/dist/processors/observational-memory/temporal-markers.d.ts +4 -0
- package/dist/processors/observational-memory/temporal-markers.d.ts.map +1 -0
- package/dist/processors/observational-memory/temporary-memory.d.ts +8 -0
- package/dist/processors/observational-memory/temporary-memory.d.ts.map +1 -0
- package/dist/processors/observational-memory/thresholds.d.ts +52 -0
- package/dist/processors/observational-memory/thresholds.d.ts.map +1 -0
- package/dist/processors/observational-memory/token-counter.d.ts +57 -0
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -0
- package/dist/processors/observational-memory/tool-result-helpers.d.ts +12 -0
- package/dist/processors/observational-memory/tool-result-helpers.d.ts.map +1 -0
- package/dist/processors/observational-memory/tracing.d.ts +17 -0
- package/dist/processors/observational-memory/tracing.d.ts.map +1 -0
- package/dist/processors/observational-memory/types.d.ts +997 -0
- package/dist/processors/observational-memory/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/working-memory-extractor.d.ts +5 -0
- package/dist/processors/observational-memory/working-memory-extractor.d.ts.map +1 -0
- package/dist/processors/working-memory-state/index.d.ts +2 -0
- package/dist/processors/working-memory-state/index.d.ts.map +1 -0
- package/dist/processors/working-memory-state/processor.d.ts +53 -0
- package/dist/processors/working-memory-state/processor.d.ts.map +1 -0
- package/dist/src-C1tlhGeW.js +28559 -0
- package/dist/src-C1tlhGeW.js.map +1 -0
- package/dist/src-DqoifKIy.cjs +28813 -0
- package/dist/src-DqoifKIy.cjs.map +1 -0
- package/dist/tools/om-tools.d.ts +172 -0
- package/dist/tools/om-tools.d.ts.map +1 -0
- package/dist/tools/working-memory.d.ts +33 -28
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +36 -28
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/processors/index.cjs.map +0 -1
- package/dist/processors/index.js.map +0 -1
- package/dist/processors/token-limiter.d.ts +0 -32
- package/dist/processors/token-limiter.d.ts.map +0 -1
- package/dist/processors/tool-call-filter.d.ts +0 -20
- package/dist/processors/tool-call-filter.d.ts.map +0 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Mastra.listMemory()
|
|
4
|
+
|
|
5
|
+
The `.listMemory()` method returns all memory instances registered with the Mastra instance.
|
|
6
|
+
|
|
7
|
+
## Usage example
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
const memoryInstances = mastra.listMemory()
|
|
11
|
+
|
|
12
|
+
for (const [key, memory] of Object.entries(memoryInstances)) {
|
|
13
|
+
console.log(`Memory "${key}": ${memory.id}`)
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Parameters
|
|
18
|
+
|
|
19
|
+
This method takes no parameters.
|
|
20
|
+
|
|
21
|
+
## Returns
|
|
22
|
+
|
|
23
|
+
**memory** (`Record<string, MastraMemory>`): An object containing all registered memory instances, keyed by their registry keys.
|
|
24
|
+
|
|
25
|
+
## Example: Checking registered 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
|
+
id: 'conversation-memory',
|
|
34
|
+
storage: new LibSQLStore({ id: 'conversation-store', url: ':memory:' }),
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const analyticsMemory = new Memory({
|
|
38
|
+
id: 'analytics-memory',
|
|
39
|
+
storage: new LibSQLStore({ id: 'analytics-store', url: ':memory:' }),
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const mastra = new Mastra({
|
|
43
|
+
memory: {
|
|
44
|
+
conversationMemory,
|
|
45
|
+
analyticsMemory,
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
// List all registered memory instances
|
|
50
|
+
const allMemory = mastra.listMemory()
|
|
51
|
+
console.log(Object.keys(allMemory)) // ["conversationMemory", "analyticsMemory"]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Related
|
|
55
|
+
|
|
56
|
+
- [Mastra.getMemory()](https://mastra.ai/reference/core/getMemory)
|
|
57
|
+
- [Memory overview](https://mastra.ai/docs/memory/overview)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Memory
|
|
4
|
+
|
|
5
|
+
A file-based agent gets [memory](https://mastra.ai/docs/memory/overview) from a `memory.ts` file that default-exports a [`Memory`](https://mastra.ai/reference/memory/memory-class) instance. Use this page for the file-based convention; use the memory docs for message history, semantic recall, storage, and processors.
|
|
6
|
+
|
|
7
|
+
Without `memory.ts` or `config.memory`, the agent has no memory by default. Each `generate()` or `stream()` call starts without remembered conversation state unless you pass the prior context yourself.
|
|
8
|
+
|
|
9
|
+
## Quickstart
|
|
10
|
+
|
|
11
|
+
Create `memory.ts` next to the agent's `config.ts`:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Memory } from '@mastra/memory'
|
|
15
|
+
|
|
16
|
+
export default new Memory()
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The exported instance becomes the agent's `memory`. If your app configures a storage provider on the main Mastra instance, memory data is stored there. See [storage](https://mastra.ai/docs/storage/overview) for more information.
|
|
20
|
+
|
|
21
|
+
Use the same `resource` and `thread` values when calling the agent to continue a conversation:
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
const response = await weatherAgent.generate('Remember that I prefer Celsius.', {
|
|
25
|
+
memory: {
|
|
26
|
+
resource: 'user-123',
|
|
27
|
+
thread: 'weather-chat',
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Configure memory
|
|
33
|
+
|
|
34
|
+
Pass options to `new Memory()` when the default behavior isn't enough.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { Memory } from '@mastra/memory'
|
|
38
|
+
|
|
39
|
+
export default new Memory({
|
|
40
|
+
options: {
|
|
41
|
+
lastMessages: 20,
|
|
42
|
+
workingMemory: {
|
|
43
|
+
enabled: true,
|
|
44
|
+
scope: 'resource',
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Visit the [`Memory` reference](https://mastra.ai/reference/memory/memory-class) for constructor options. Use these pages for related memory features:
|
|
51
|
+
|
|
52
|
+
- [Storage](https://mastra.ai/docs/storage/overview): configure persistence for memory data.
|
|
53
|
+
- [Semantic recall](https://mastra.ai/docs/memory/semantic-recall): retrieve relevant past messages by semantic meaning.
|
|
54
|
+
- [Memory processors](https://mastra.ai/docs/memory/memory-processors): filter, trim, or transform messages before memory adds them to model context.
|
|
55
|
+
|
|
56
|
+
## Precedence with config
|
|
57
|
+
|
|
58
|
+
`config.memory` wins over `memory.ts`. If neither is present, the assembled file-based agent has no memory. See [`config.ts` precedence](https://mastra.ai/reference/file-based-agents/config) for the full merge table.
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Cloned thread utilities
|
|
4
|
+
|
|
5
|
+
The Memory class provides utility methods for working with cloned threads. These methods help you check clone status, retrieve clone metadata, traverse clone relationships, and track clone history.
|
|
6
|
+
|
|
7
|
+
## `isClone()`
|
|
8
|
+
|
|
9
|
+
Checks whether a thread is a clone of another thread.
|
|
10
|
+
|
|
11
|
+
### Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
const isClonedThread = memory.isClone(thread)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Parameters
|
|
18
|
+
|
|
19
|
+
**thread** (`StorageThreadType`): The thread object to check.
|
|
20
|
+
|
|
21
|
+
### Example
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
const thread = await memory.getThreadById({ threadId: 'some-thread-id' })
|
|
25
|
+
|
|
26
|
+
if (memory.isClone(thread)) {
|
|
27
|
+
console.log('This thread was cloned from another thread')
|
|
28
|
+
} else {
|
|
29
|
+
console.log('This is an original thread')
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## `getCloneMetadata()`
|
|
34
|
+
|
|
35
|
+
Retrieves the clone metadata from a thread if it exists.
|
|
36
|
+
|
|
37
|
+
### Usage
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
const metadata = memory.getCloneMetadata(thread)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Parameters
|
|
44
|
+
|
|
45
|
+
**thread** (`StorageThreadType`): The thread object to extract clone metadata from.
|
|
46
|
+
|
|
47
|
+
### Example
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
const thread = await memory.getThreadById({ threadId: 'cloned-thread-id' })
|
|
51
|
+
const cloneInfo = memory.getCloneMetadata(thread)
|
|
52
|
+
|
|
53
|
+
if (cloneInfo) {
|
|
54
|
+
console.log(`Cloned from: ${cloneInfo.sourceThreadId}`)
|
|
55
|
+
console.log(`Cloned at: ${cloneInfo.clonedAt}`)
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## `getSourceThread()`
|
|
60
|
+
|
|
61
|
+
Retrieves the original source thread that a cloned thread was created from.
|
|
62
|
+
|
|
63
|
+
### Usage
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
const sourceThread = await memory.getSourceThread(threadId)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Parameters
|
|
70
|
+
|
|
71
|
+
**threadId** (`string`): The ID of the cloned thread.
|
|
72
|
+
|
|
73
|
+
### Example
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
const sourceThread = await memory.getSourceThread('cloned-thread-id')
|
|
77
|
+
|
|
78
|
+
if (sourceThread) {
|
|
79
|
+
console.log(`Original thread title: ${sourceThread.title}`)
|
|
80
|
+
console.log(`Original thread created: ${sourceThread.createdAt}`)
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## `listClones()`
|
|
85
|
+
|
|
86
|
+
Lists all threads that were cloned from a specific source thread.
|
|
87
|
+
|
|
88
|
+
### Usage
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
const clones = await memory.listClones(sourceThreadId)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Parameters
|
|
95
|
+
|
|
96
|
+
**sourceThreadId** (`string`): The ID of the source thread to find clones for.
|
|
97
|
+
|
|
98
|
+
### Example
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
const clones = await memory.listClones('original-thread-id')
|
|
102
|
+
|
|
103
|
+
console.log(`Found ${clones.length} clones`)
|
|
104
|
+
for (const clone of clones) {
|
|
105
|
+
console.log(`- ${clone.id}: ${clone.title}`)
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## `getCloneHistory()`
|
|
110
|
+
|
|
111
|
+
Retrieves the full clone history chain for a thread, tracing back to the original.
|
|
112
|
+
|
|
113
|
+
### Usage
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
const history = await memory.getCloneHistory(threadId)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Parameters
|
|
120
|
+
|
|
121
|
+
**threadId** (`string`): The ID of the thread to get clone history for.
|
|
122
|
+
|
|
123
|
+
### Example
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// If thread-c was cloned from thread-b, which was cloned from thread-a
|
|
127
|
+
const history = await memory.getCloneHistory('thread-c')
|
|
128
|
+
|
|
129
|
+
// history = [thread-a, thread-b, thread-c]
|
|
130
|
+
console.log(`Clone depth: ${history.length - 1}`)
|
|
131
|
+
console.log(`Original thread: ${history[0].id}`)
|
|
132
|
+
console.log(`Current thread: ${history[history.length - 1].id}`)
|
|
133
|
+
|
|
134
|
+
// Display the clone chain
|
|
135
|
+
for (let i = 0; i < history.length; i++) {
|
|
136
|
+
const prefix = i === 0 ? 'Original' : `Clone ${i}`
|
|
137
|
+
console.log(`${prefix}: ${history[i].title}`)
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Complete example
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import { mastra } from './mastra'
|
|
145
|
+
|
|
146
|
+
async function manageClones() {
|
|
147
|
+
const agent = mastra.getAgent('agent')
|
|
148
|
+
const memory = await agent.getMemory()
|
|
149
|
+
|
|
150
|
+
// Create an original conversation
|
|
151
|
+
const originalThread = await memory.createThread({
|
|
152
|
+
resourceId: 'user-123',
|
|
153
|
+
title: 'Original Conversation',
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
// Have a conversation...
|
|
157
|
+
await agent.generate("Hello! Let's discuss project options.", {
|
|
158
|
+
memory: {
|
|
159
|
+
thread: originalThread.id,
|
|
160
|
+
resource: 'user-123',
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
// Create multiple branches (clones) to explore different paths
|
|
165
|
+
const { thread: optionA } = await memory.cloneThread({
|
|
166
|
+
sourceThreadId: originalThread.id,
|
|
167
|
+
title: 'Option A - Conservative Approach',
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
const { thread: optionB } = await memory.cloneThread({
|
|
171
|
+
sourceThreadId: originalThread.id,
|
|
172
|
+
title: 'Option B - Aggressive Approach',
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
// Check clone status
|
|
176
|
+
console.log(memory.isClone(originalThread)) // false
|
|
177
|
+
console.log(memory.isClone(optionA)) // true
|
|
178
|
+
console.log(memory.isClone(optionB)) // true
|
|
179
|
+
|
|
180
|
+
// Get clone metadata
|
|
181
|
+
const metadataA = memory.getCloneMetadata(optionA)
|
|
182
|
+
console.log(metadataA?.sourceThreadId) // originalThread.id
|
|
183
|
+
|
|
184
|
+
// List all clones of the original
|
|
185
|
+
const allClones = await memory.listClones(originalThread.id)
|
|
186
|
+
console.log(`Total alternatives: ${allClones.length}`) // 2
|
|
187
|
+
|
|
188
|
+
// Get source thread from a clone
|
|
189
|
+
const source = await memory.getSourceThread(optionA.id)
|
|
190
|
+
console.log(source?.id === originalThread.id) // true
|
|
191
|
+
|
|
192
|
+
// Create a deeper clone chain
|
|
193
|
+
const { thread: optionA2 } = await memory.cloneThread({
|
|
194
|
+
sourceThreadId: optionA.id,
|
|
195
|
+
title: 'Option A - Variant 2',
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
// Get the full history
|
|
199
|
+
const history = await memory.getCloneHistory(optionA2.id)
|
|
200
|
+
// history = [originalThread, optionA, optionA2]
|
|
201
|
+
console.log(`Clone depth: ${history.length - 1}`) // 2
|
|
202
|
+
}
|
|
203
|
+
```
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Memory.cloneThread()
|
|
4
|
+
|
|
5
|
+
The `.cloneThread()` method creates a copy of an existing conversation thread, including all its messages. It supports 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.
|
|
6
|
+
|
|
7
|
+
## Usage example
|
|
8
|
+
|
|
9
|
+
The following example creates a `Memory` instance and clones an existing thread.
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { Memory } from '@mastra/memory'
|
|
13
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
14
|
+
|
|
15
|
+
const memory = new Memory({
|
|
16
|
+
storage: new LibSQLStore({ id: 'memory-store', url: 'file:./memory.db' }),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const { thread, clonedMessages } = await memory.cloneThread({
|
|
20
|
+
sourceThreadId: 'original-thread-123',
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Parameters
|
|
25
|
+
|
|
26
|
+
**sourceThreadId** (`string`): The ID of the thread to clone
|
|
27
|
+
|
|
28
|
+
**newThreadId** (`string`): Optional custom ID for the cloned thread. If not provided, one will be generated.
|
|
29
|
+
|
|
30
|
+
**resourceId** (`string`): Optional resource ID for the cloned thread. Defaults to the source thread's resourceId.
|
|
31
|
+
|
|
32
|
+
**title** (`string`): Optional title for the cloned thread. If omitted, the clone uses Clone of ${sourceThread.title} when the source thread has a title. Otherwise, the title is empty.
|
|
33
|
+
|
|
34
|
+
**metadata** (`Record<string, unknown>`): Optional metadata to merge with the source thread's metadata. Clone metadata is automatically added.
|
|
35
|
+
|
|
36
|
+
**options** (`CloneOptions`): Optional filtering options for the clone operation.
|
|
37
|
+
|
|
38
|
+
**options.messageLimit** (`number`): Maximum number of messages to clone. When set, clones the most recent N messages.
|
|
39
|
+
|
|
40
|
+
**options.messageFilter** (`MessageFilter`): Filter criteria for selecting which messages to clone.
|
|
41
|
+
|
|
42
|
+
**options.messageFilter.startDate** (`Date`): Only clone messages created on or after this date.
|
|
43
|
+
|
|
44
|
+
**options.messageFilter.endDate** (`Date`): Only clone messages created on or before this date.
|
|
45
|
+
|
|
46
|
+
**options.messageFilter.messageIds** (`string[]`): Only clone messages with these specific IDs.
|
|
47
|
+
|
|
48
|
+
## Returns
|
|
49
|
+
|
|
50
|
+
**thread** (`StorageThreadType`): The newly created cloned thread with clone metadata.
|
|
51
|
+
|
|
52
|
+
**clonedMessages** (`MastraDBMessage[]`): Array of the cloned messages with new IDs assigned to the new thread.
|
|
53
|
+
|
|
54
|
+
**messageIdMap** (`Record<string, string>`): A mapping from source message IDs to their corresponding cloned message IDs.
|
|
55
|
+
|
|
56
|
+
### Clone metadata
|
|
57
|
+
|
|
58
|
+
The cloned thread's metadata includes a `clone` property with:
|
|
59
|
+
|
|
60
|
+
**sourceThreadId** (`string`): The ID of the original thread that was cloned.
|
|
61
|
+
|
|
62
|
+
**clonedAt** (`Date`): Timestamp when the clone was created.
|
|
63
|
+
|
|
64
|
+
**lastMessageId** (`string`): The ID of the last message in the source thread at the time of cloning.
|
|
65
|
+
|
|
66
|
+
## Extended usage example
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { mastra } from './mastra'
|
|
70
|
+
|
|
71
|
+
const agent = mastra.getAgent('agent')
|
|
72
|
+
const memory = await agent.getMemory()
|
|
73
|
+
|
|
74
|
+
// Clone a thread with all messages
|
|
75
|
+
const { thread: fullClone } = await memory.cloneThread({
|
|
76
|
+
sourceThreadId: 'original-thread-123',
|
|
77
|
+
title: 'Alternative Conversation Path',
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
// Clone with a custom ID
|
|
81
|
+
const { thread: customIdClone } = await memory.cloneThread({
|
|
82
|
+
sourceThreadId: 'original-thread-123',
|
|
83
|
+
newThreadId: 'my-custom-clone-id',
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// Clone only the last 5 messages
|
|
87
|
+
const { thread: partialClone, clonedMessages } = await memory.cloneThread({
|
|
88
|
+
sourceThreadId: 'original-thread-123',
|
|
89
|
+
options: {
|
|
90
|
+
messageLimit: 5,
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
// Clone messages from a specific date range
|
|
95
|
+
const { thread: dateFilteredClone } = await memory.cloneThread({
|
|
96
|
+
sourceThreadId: 'original-thread-123',
|
|
97
|
+
options: {
|
|
98
|
+
messageFilter: {
|
|
99
|
+
startDate: new Date('2024-01-01'),
|
|
100
|
+
endDate: new Date('2024-01-31'),
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
// Clone specific messages
|
|
106
|
+
const { thread: selectedMessagesClone } = await memory.cloneThread({
|
|
107
|
+
sourceThreadId: 'original-thread-123',
|
|
108
|
+
options: {
|
|
109
|
+
messageFilter: {
|
|
110
|
+
messageIds: ['message-1', 'message-2'],
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
// Continue conversation on the cloned thread
|
|
116
|
+
const response = await agent.generate('Try a different approach', {
|
|
117
|
+
memory: {
|
|
118
|
+
thread: fullClone.id,
|
|
119
|
+
resource: fullClone.resourceId,
|
|
120
|
+
},
|
|
121
|
+
})
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Pass the cloned `thread.id` and `thread.resourceId` to `agent.generate()` to continue the conversation from the cloned thread.
|
|
125
|
+
|
|
126
|
+
## Vector embeddings
|
|
127
|
+
|
|
128
|
+
When the Memory instance has semantic recall enabled with a vector store and embedder configured, `cloneThread()` automatically creates vector embeddings for all cloned messages. It ensures that semantic search works correctly on the cloned thread.
|
|
129
|
+
|
|
130
|
+
In this example, `embeddingModel` is the embedding model configured for the project.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { Memory } from '@mastra/memory'
|
|
134
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
135
|
+
|
|
136
|
+
const memory = new Memory({
|
|
137
|
+
storage: new LibSQLStore({ id: 'memory-store', url: 'file:./memory.db' }),
|
|
138
|
+
vector: new LibSQLVector({ id: 'vector-store', url: 'file:./vector.db' }),
|
|
139
|
+
embedder: embeddingModel,
|
|
140
|
+
options: {
|
|
141
|
+
semanticRecall: true,
|
|
142
|
+
},
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
// Clone will also create embeddings for cloned messages
|
|
146
|
+
const { thread } = await memory.cloneThread({
|
|
147
|
+
sourceThreadId: 'original-thread',
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
// Semantic search works on the cloned thread
|
|
151
|
+
const results = await memory.recall({
|
|
152
|
+
threadId: thread.id,
|
|
153
|
+
vectorSearchString: 'search query',
|
|
154
|
+
})
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Working memory
|
|
158
|
+
|
|
159
|
+
When working memory is enabled, `cloneThread()` copies or shares working memory based on the working-memory scope and the clone's `resourceId`:
|
|
160
|
+
|
|
161
|
+
- **Thread-scoped working memory**: The working memory is copied to the cloned thread.
|
|
162
|
+
- **Resource-scoped working memory with the same `resourceId`**: The working memory is shared because the source and cloned threads belong to the same resource.
|
|
163
|
+
- **Resource-scoped working memory with a different `resourceId`**: The working memory is copied to the cloned thread's resource.
|
|
164
|
+
|
|
165
|
+
## Observational Memory
|
|
166
|
+
|
|
167
|
+
When [Observational Memory](https://mastra.ai/docs/memory/observational-memory) is enabled, `cloneThread()` automatically clones the OM records associated with the source thread. The behavior depends on the OM scope:
|
|
168
|
+
|
|
169
|
+
- **Thread-scoped OM**: The OM record is cloned to the new thread. All internal message ID references are remapped to point to the cloned messages.
|
|
170
|
+
- **Resource-scoped OM (same `resourceId`)**: The OM record is shared between the source and cloned threads since they belong to the same resource. No duplication occurs.
|
|
171
|
+
- **Resource-scoped OM (different `resourceId`)**: The OM record is cloned to the new resource. Message IDs are remapped and any thread-identifying tags within observations are updated to reference the cloned thread.
|
|
172
|
+
|
|
173
|
+
Only the current (most recent) OM generation is cloned. Older history generations aren't copied. Transient processing state (observation/reflection in-progress flags) is reset on the cloned record.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Memory.createThread()
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
## Usage example
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
await memory?.createThread({ resourceId: 'user-123' })
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Parameters
|
|
14
|
+
|
|
15
|
+
**resourceId** (`string`): Identifier for the resource this thread belongs to (e.g., user ID, project ID)
|
|
16
|
+
|
|
17
|
+
**threadId** (`string`): Optional custom ID for the thread. If not provided, one will be generated.
|
|
18
|
+
|
|
19
|
+
**title** (`string`): Optional title for the thread
|
|
20
|
+
|
|
21
|
+
**metadata** (`Record<string, unknown>`): Optional metadata to associate with the thread
|
|
22
|
+
|
|
23
|
+
## Returns
|
|
24
|
+
|
|
25
|
+
**id** (`string`): Unique identifier of the created thread
|
|
26
|
+
|
|
27
|
+
**resourceId** (`string`): Resource ID associated with the thread
|
|
28
|
+
|
|
29
|
+
**title** (`string`): Title of the thread (if provided)
|
|
30
|
+
|
|
31
|
+
**createdAt** (`Date`): Timestamp when the thread was created
|
|
32
|
+
|
|
33
|
+
**updatedAt** (`Date`): Timestamp when the thread was last updated
|
|
34
|
+
|
|
35
|
+
**metadata** (`Record<string, unknown>`): Additional metadata associated with the thread
|
|
36
|
+
|
|
37
|
+
## Extended usage example
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { mastra } from './mastra'
|
|
41
|
+
|
|
42
|
+
const agent = mastra.getAgent('agent')
|
|
43
|
+
const memory = await agent.getMemory()
|
|
44
|
+
|
|
45
|
+
const thread = await memory?.createThread({
|
|
46
|
+
resourceId: 'user-123',
|
|
47
|
+
title: 'Memory Test Thread',
|
|
48
|
+
metadata: {
|
|
49
|
+
source: 'test-script',
|
|
50
|
+
purpose: 'memory-testing',
|
|
51
|
+
},
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const response = await agent.generate('message for agent', {
|
|
55
|
+
memory: {
|
|
56
|
+
thread: thread!.id,
|
|
57
|
+
resource: thread!.resourceId,
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
console.log(response.text)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Related
|
|
65
|
+
|
|
66
|
+
- [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
|
|
67
|
+
- [Getting Started with Memory](https://mastra.ai/docs/memory/overview) (Covers threads concept)
|
|
68
|
+
- [getThreadById](https://mastra.ai/reference/memory/getThreadById)
|
|
69
|
+
- [listThreads](https://mastra.ai/reference/memory/listThreads)
|
|
70
|
+
- [recall](https://mastra.ai/reference/memory/recall)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Memory.getThreadById()
|
|
4
|
+
|
|
5
|
+
The `.getThreadById()` method retrieves a specific thread by its ID.
|
|
6
|
+
|
|
7
|
+
## Usage example
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
await memory?.getThreadById({ threadId: 'thread-123' })
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Parameters
|
|
14
|
+
|
|
15
|
+
**threadId** (`string`): The ID of the thread to be retrieved.
|
|
16
|
+
|
|
17
|
+
## Returns
|
|
18
|
+
|
|
19
|
+
**thread** (`Promise<StorageThreadType | null>`): A promise that resolves to the thread associated with the given ID, or null if not found.
|
|
20
|
+
|
|
21
|
+
### Related
|
|
22
|
+
|
|
23
|
+
- [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
|
|
24
|
+
- [Getting Started with Memory](https://mastra.ai/docs/memory/overview) (Covers threads concept)
|
|
25
|
+
- [createThread](https://mastra.ai/reference/memory/createThread)
|
|
26
|
+
- [listThreads](https://mastra.ai/reference/memory/listThreads)
|