@mastra/memory 1.1.0-alpha.1 → 1.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +112 -0
- package/dist/_types/@internal_ai-sdk-v4/dist/index.d.ts +30 -17
- package/dist/{chunk-FQJWVCDF.cjs → chunk-AWE2QQPI.cjs} +1884 -312
- package/dist/chunk-AWE2QQPI.cjs.map +1 -0
- package/dist/chunk-EQ4M72KU.js +439 -0
- package/dist/chunk-EQ4M72KU.js.map +1 -0
- package/dist/{chunk-O3CS4UGX.cjs → chunk-IDRQZVB4.cjs} +4 -4
- package/dist/{chunk-O3CS4UGX.cjs.map → chunk-IDRQZVB4.cjs.map} +1 -1
- package/dist/{chunk-YF4R74L2.js → chunk-RC6RZVYE.js} +4 -4
- package/dist/{chunk-YF4R74L2.js.map → chunk-RC6RZVYE.js.map} +1 -1
- package/dist/{chunk-6TXUWFIU.js → chunk-TYVPTNCP.js} +1885 -313
- package/dist/chunk-TYVPTNCP.js.map +1 -0
- package/dist/chunk-ZD3BKU5O.cjs +441 -0
- package/dist/chunk-ZD3BKU5O.cjs.map +1 -0
- package/dist/docs/SKILL.md +51 -50
- package/dist/docs/{SOURCE_MAP.json → assets/SOURCE_MAP.json} +22 -22
- package/dist/docs/{agents/03-agent-approval.md → references/docs-agents-agent-approval.md} +19 -19
- package/dist/docs/references/docs-agents-agent-memory.md +212 -0
- package/dist/docs/{agents/04-network-approval.md → references/docs-agents-network-approval.md} +13 -12
- package/dist/docs/{agents/02-networks.md → references/docs-agents-networks.md} +10 -12
- package/dist/docs/{memory/06-memory-processors.md → references/docs-memory-memory-processors.md} +6 -8
- package/dist/docs/{memory/03-message-history.md → references/docs-memory-message-history.md} +31 -20
- package/dist/docs/references/docs-memory-observational-memory.md +238 -0
- package/dist/docs/{memory/01-overview.md → references/docs-memory-overview.md} +8 -8
- package/dist/docs/{memory/05-semantic-recall.md → references/docs-memory-semantic-recall.md} +33 -17
- package/dist/docs/{memory/02-storage.md → references/docs-memory-storage.md} +29 -39
- package/dist/docs/{memory/04-working-memory.md → references/docs-memory-working-memory.md} +16 -27
- 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 +528 -0
- package/dist/docs/{processors/01-reference.md → references/reference-processors-token-limiter-processor.md} +25 -12
- 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 +529 -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 +919 -507
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +914 -502
- package/dist/index.js.map +1 -1
- package/dist/{observational-memory-3Q42SITP.cjs → observational-memory-3UO64HYD.cjs} +14 -14
- package/dist/{observational-memory-3Q42SITP.cjs.map → observational-memory-3UO64HYD.cjs.map} +1 -1
- package/dist/{observational-memory-VXLHOSDZ.js → observational-memory-TVHT3HP4.js} +3 -3
- package/dist/{observational-memory-VXLHOSDZ.js.map → observational-memory-TVHT3HP4.js.map} +1 -1
- package/dist/processors/index.cjs +12 -12
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/index.d.ts +1 -1
- package/dist/processors/observational-memory/index.d.ts.map +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +267 -1
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/observer-agent.d.ts +3 -1
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -1
- package/dist/processors/observational-memory/reflector-agent.d.ts +10 -3
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -1
- package/dist/processors/observational-memory/types.d.ts +243 -19
- package/dist/processors/observational-memory/types.d.ts.map +1 -1
- package/dist/{token-6GSAFR2W-WGTMOPEU.js → token-APYSY3BW-2DN6RAUY.js} +11 -11
- package/dist/token-APYSY3BW-2DN6RAUY.js.map +1 -0
- package/dist/{token-6GSAFR2W-2B4WM6AQ.cjs → token-APYSY3BW-ZQ7TMBY7.cjs} +14 -14
- package/dist/token-APYSY3BW-ZQ7TMBY7.cjs.map +1 -0
- package/dist/token-util-RMHT2CPJ-6TGPE335.cjs +10 -0
- package/dist/token-util-RMHT2CPJ-6TGPE335.cjs.map +1 -0
- package/dist/token-util-RMHT2CPJ-RJEA3FAN.js +8 -0
- package/dist/token-util-RMHT2CPJ-RJEA3FAN.js.map +1 -0
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +9 -10
- package/dist/chunk-6TXUWFIU.js.map +0 -1
- package/dist/chunk-FQJWVCDF.cjs.map +0 -1
- package/dist/chunk-WM6IIUQW.js +0 -250
- package/dist/chunk-WM6IIUQW.js.map +0 -1
- package/dist/chunk-ZSBBXHNM.cjs +0 -252
- package/dist/chunk-ZSBBXHNM.cjs.map +0 -1
- package/dist/docs/README.md +0 -36
- package/dist/docs/agents/01-agent-memory.md +0 -166
- package/dist/docs/core/01-reference.md +0 -114
- package/dist/docs/memory/07-reference.md +0 -687
- package/dist/docs/storage/01-reference.md +0 -1218
- package/dist/docs/vectors/01-reference.md +0 -942
- package/dist/token-6GSAFR2W-2B4WM6AQ.cjs.map +0 -1
- package/dist/token-6GSAFR2W-WGTMOPEU.js.map +0 -1
- package/dist/token-util-NEHG7TUY-TV2H7N56.js +0 -8
- package/dist/token-util-NEHG7TUY-TV2H7N56.js.map +0 -1
- package/dist/token-util-NEHG7TUY-WJZIPNNX.cjs +0 -10
- package/dist/token-util-NEHG7TUY-WJZIPNNX.cjs.map +0 -1
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
> Configure storage for Mastra
|
|
2
|
-
|
|
3
1
|
# Storage
|
|
4
2
|
|
|
5
|
-
For agents to remember previous interactions, Mastra needs a database. Use a storage adapter for one of the [supported databases](#supported-providers) and pass it to your Mastra instance.
|
|
3
|
+
For agents to remember previous interactions, Mastra needs a database. Use a storage adapter for one of the [supported databases](#supported-providers) and pass it to your Mastra instance.
|
|
6
4
|
|
|
7
|
-
```typescript
|
|
5
|
+
```typescript
|
|
8
6
|
import { Mastra } from "@mastra/core";
|
|
9
7
|
import { LibSQLStore } from "@mastra/libsql";
|
|
10
8
|
|
|
@@ -16,18 +14,17 @@ export const mastra = new Mastra({
|
|
|
16
14
|
});
|
|
17
15
|
```
|
|
18
16
|
|
|
19
|
-
> **Sharing the database with Mastra Studio
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
Relative paths like `file:./mastra.db` resolve based on each process's working directory, which may differ.
|
|
17
|
+
> **Sharing the database with Mastra Studio:** When running `mastra dev` alongside your application (e.g., Next.js), use an absolute path to ensure both processes access the same database:
|
|
18
|
+
>
|
|
19
|
+
> ```typescript
|
|
20
|
+
> url: "file:/absolute/path/to/your/project/mastra.db"
|
|
21
|
+
> ```
|
|
22
|
+
>
|
|
23
|
+
> Relative paths like `file:./mastra.db` resolve based on each process's working directory, which may differ.
|
|
27
24
|
|
|
28
25
|
This configures instance-level storage, which all agents share by default. You can also configure [agent-level storage](#agent-level-storage) for isolated data boundaries.
|
|
29
26
|
|
|
30
|
-
Mastra automatically creates the necessary tables on first interaction. See the [core schema](https://mastra.ai/reference/storage/overview
|
|
27
|
+
Mastra automatically creates the necessary tables on first interaction. See the [core schema](https://mastra.ai/reference/storage/overview) for details on what gets created, including tables for messages, threads, resources, workflows, traces, and evaluation datasets.
|
|
31
28
|
|
|
32
29
|
## Supported providers
|
|
33
30
|
|
|
@@ -44,8 +41,7 @@ Each provider page includes installation instructions, configuration parameters,
|
|
|
44
41
|
- [LanceDB](https://mastra.ai/reference/storage/lance)
|
|
45
42
|
- [Microsoft SQL Server](https://mastra.ai/reference/storage/mssql)
|
|
46
43
|
|
|
47
|
-
> **
|
|
48
|
-
libSQL is the easiest way to get started because it doesn’t require running a separate database server.
|
|
44
|
+
> **Tip:** libSQL is the easiest way to get started because it doesn’t require running a separate database server.
|
|
49
45
|
|
|
50
46
|
## Configuration scope
|
|
51
47
|
|
|
@@ -55,7 +51,7 @@ Storage can be configured at the instance level (shared by all agents) or at the
|
|
|
55
51
|
|
|
56
52
|
Add storage to your Mastra instance so all agents, workflows, observability traces and scores share the same memory provider:
|
|
57
53
|
|
|
58
|
-
```typescript
|
|
54
|
+
```typescript
|
|
59
55
|
import { Mastra } from "@mastra/core";
|
|
60
56
|
import { PostgresStore } from "@mastra/pg";
|
|
61
57
|
|
|
@@ -75,9 +71,9 @@ This is useful when all primitives share the same storage backend and have simil
|
|
|
75
71
|
|
|
76
72
|
#### Composite storage
|
|
77
73
|
|
|
78
|
-
[Composite storage](https://mastra.ai/reference/storage/composite) is an alternative way to configure instance-level storage. Use `MastraCompositeStore` to set the `memory` domain (and any other [domains](https://mastra.ai/reference/storage/composite
|
|
74
|
+
[Composite storage](https://mastra.ai/reference/storage/composite) is an alternative way to configure instance-level storage. Use `MastraCompositeStore` to set the `memory` domain (and any other [domains](https://mastra.ai/reference/storage/composite) you need) to different storage providers.
|
|
79
75
|
|
|
80
|
-
```typescript
|
|
76
|
+
```typescript
|
|
81
77
|
import { Mastra } from "@mastra/core";
|
|
82
78
|
import { MastraCompositeStore } from "@mastra/core/storage";
|
|
83
79
|
import { MemoryLibSQL } from "@mastra/libsql";
|
|
@@ -88,7 +84,6 @@ export const mastra = new Mastra({
|
|
|
88
84
|
storage: new MastraCompositeStore({
|
|
89
85
|
id: "composite",
|
|
90
86
|
domains: {
|
|
91
|
-
// highlight-next-line
|
|
92
87
|
memory: new MemoryLibSQL({ url: "file:./memory.db" }),
|
|
93
88
|
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
|
|
94
89
|
observability: new ObservabilityStorageClickhouse({
|
|
@@ -107,7 +102,7 @@ This is useful when different types of data have different performance or operat
|
|
|
107
102
|
|
|
108
103
|
Agent-level storage overrides storage configured at the instance level. Add storage to a specific agent when you need data boundaries or compliance requirements:
|
|
109
104
|
|
|
110
|
-
```typescript
|
|
105
|
+
```typescript
|
|
111
106
|
import { Agent } from "@mastra/core/agent";
|
|
112
107
|
import { Memory } from "@mastra/memory";
|
|
113
108
|
import { PostgresStore } from "@mastra/pg";
|
|
@@ -123,19 +118,18 @@ export const agent = new Agent({
|
|
|
123
118
|
});
|
|
124
119
|
```
|
|
125
120
|
|
|
126
|
-
> **
|
|
127
|
-
[Mastra Cloud Store](https://mastra.ai/docs/mastra-cloud/deployment#using-mastra-cloud-store) doesn't support agent-level storage.
|
|
121
|
+
> **Warning:** [Mastra Cloud Store](https://mastra.ai/docs/mastra-cloud/deployment) doesn't support agent-level storage.
|
|
128
122
|
|
|
129
123
|
## Threads and resources
|
|
130
124
|
|
|
131
|
-
Mastra organizes conversations using two identifiers:
|
|
125
|
+
Mastra organizes conversations using two identifiers:
|
|
132
126
|
|
|
133
127
|
- **Thread** - a conversation session containing a sequence of messages.
|
|
134
128
|
- **Resource** - the entity that owns the thread, such as a user, organization, project, or any other domain entity in your application.
|
|
135
129
|
|
|
136
130
|
Both identifiers are required for agents to store information:
|
|
137
131
|
|
|
138
|
-
|
|
132
|
+
**Generate**:
|
|
139
133
|
|
|
140
134
|
```typescript
|
|
141
135
|
const response = await agent.generate("hello", {
|
|
@@ -146,8 +140,7 @@ const response = await agent.generate("hello", {
|
|
|
146
140
|
});
|
|
147
141
|
```
|
|
148
142
|
|
|
149
|
-
|
|
150
|
-
**stream:**
|
|
143
|
+
**Stream**:
|
|
151
144
|
|
|
152
145
|
```typescript
|
|
153
146
|
const stream = await agent.stream("hello", {
|
|
@@ -158,10 +151,7 @@ const stream = await agent.stream("hello", {
|
|
|
158
151
|
});
|
|
159
152
|
```
|
|
160
153
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
> **Note:**
|
|
164
|
-
[Studio](https://mastra.ai/docs/getting-started/studio) automatically generates a thread and resource ID for you. When calling `stream()` or `generate()` yourself, remember to provide these identifiers explicitly.
|
|
154
|
+
> **Note:** [Studio](https://mastra.ai/docs/getting-started/studio) automatically generates a thread and resource ID for you. When calling `stream()` or `generate()` yourself, remember to provide these identifiers explicitly.
|
|
165
155
|
|
|
166
156
|
### Thread title generation
|
|
167
157
|
|
|
@@ -169,7 +159,7 @@ Mastra can automatically generate descriptive thread titles based on the user's
|
|
|
169
159
|
|
|
170
160
|
Use this option when implementing a ChatGPT-style chat interface to render a title alongside each thread in the conversation list (for example, in a sidebar) derived from the thread’s initial user message.
|
|
171
161
|
|
|
172
|
-
```typescript
|
|
162
|
+
```typescript
|
|
173
163
|
export const agent = new Agent({
|
|
174
164
|
id: "agent",
|
|
175
165
|
memory: new Memory({
|
|
@@ -182,9 +172,9 @@ export const agent = new Agent({
|
|
|
182
172
|
|
|
183
173
|
Title generation runs asynchronously after the agent responds and does not affect response time.
|
|
184
174
|
|
|
185
|
-
To optimize cost or behavior, provide a smaller [`model`](/models) and custom `instructions`:
|
|
175
|
+
To optimize cost or behavior, provide a smaller [`model`](https://mastra.ai/models) and custom `instructions`:
|
|
186
176
|
|
|
187
|
-
```typescript
|
|
177
|
+
```typescript
|
|
188
178
|
export const agent = new Agent({
|
|
189
179
|
id: "agent",
|
|
190
180
|
memory: new Memory({
|
|
@@ -206,17 +196,17 @@ Semantic recall has different storage requirements - it needs a vector database
|
|
|
206
196
|
|
|
207
197
|
Some storage providers enforce record size limits that base64-encoded file attachments (such as images) can exceed:
|
|
208
198
|
|
|
209
|
-
| Provider
|
|
210
|
-
|
|
|
211
|
-
| [DynamoDB](https://mastra.ai/reference/storage/dynamodb)
|
|
212
|
-
| [Convex](https://mastra.ai/reference/storage/convex)
|
|
213
|
-
| [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1) | 1 MiB
|
|
199
|
+
| Provider | Record size limit |
|
|
200
|
+
| ------------------------------------------------------------------ | ----------------- |
|
|
201
|
+
| [DynamoDB](https://mastra.ai/reference/storage/dynamodb) | 400 KB |
|
|
202
|
+
| [Convex](https://mastra.ai/reference/storage/convex) | 1 MiB |
|
|
203
|
+
| [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1) | 1 MiB |
|
|
214
204
|
|
|
215
205
|
PostgreSQL, MongoDB, and libSQL have higher limits and are generally unaffected.
|
|
216
206
|
|
|
217
207
|
To avoid this, use an input processor to upload attachments to external storage (S3, R2, GCS, [Convex file storage](https://docs.convex.dev/file-storage), etc.) and replace them with URL references before persistence.
|
|
218
208
|
|
|
219
|
-
```typescript
|
|
209
|
+
```typescript
|
|
220
210
|
import type { Processor } from "@mastra/core/processors";
|
|
221
211
|
import type { MastraDBMessage } from "@mastra/core/memory";
|
|
222
212
|
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
> Learn how to configure working memory in Mastra to store persistent user data, preferences.
|
|
2
|
-
|
|
3
1
|
# Working Memory
|
|
4
2
|
|
|
5
|
-
While [message history](https://mastra.ai/docs/memory/message-history) and [semantic recall](
|
|
3
|
+
While [message history](https://mastra.ai/docs/memory/message-history) and [semantic recall](https://mastra.ai/docs/memory/semantic-recall) help agents remember conversations, working memory allows them to maintain persistent information about users across interactions.
|
|
6
4
|
|
|
7
5
|
Think of it as the agent's active thoughts or scratchpad – the key information they keep available about the user or task. It's similar to how a person would naturally remember someone's name, preferences, or important details during a conversation.
|
|
8
6
|
|
|
@@ -19,7 +17,7 @@ Working memory can persist at two different scopes:
|
|
|
19
17
|
|
|
20
18
|
Here's a minimal example of setting up an agent with working memory:
|
|
21
19
|
|
|
22
|
-
```typescript
|
|
20
|
+
```typescript
|
|
23
21
|
import { Agent } from "@mastra/core/agent";
|
|
24
22
|
import { Memory } from "@mastra/memory";
|
|
25
23
|
|
|
@@ -43,7 +41,7 @@ const agent = new Agent({
|
|
|
43
41
|
|
|
44
42
|
Working memory is a block of Markdown text that the agent is able to update over time to store continuously relevant information:
|
|
45
43
|
|
|
46
|
-
|
|
44
|
+
[YouTube video player](https://www.youtube-nocookie.com/embed/UMy_JHLf1n8)
|
|
47
45
|
|
|
48
46
|
## Memory Persistence Scopes
|
|
49
47
|
|
|
@@ -136,7 +134,7 @@ Templates guide the agent on what information to track and update in working mem
|
|
|
136
134
|
|
|
137
135
|
Here's an example of a custom template. In this example the agent will store the users name, location, timezone, etc as soon as the user sends a message containing any of the info:
|
|
138
136
|
|
|
139
|
-
```typescript
|
|
137
|
+
```typescript
|
|
140
138
|
const memory = new Memory({
|
|
141
139
|
options: {
|
|
142
140
|
workingMemory: {
|
|
@@ -172,19 +170,13 @@ const memory = new Memory({
|
|
|
172
170
|
|
|
173
171
|
## Designing Effective Templates
|
|
174
172
|
|
|
175
|
-
A well-structured template keeps the information easy for the agent to parse and update. Treat the
|
|
176
|
-
template as a short form that you want the assistant to keep up to date.
|
|
173
|
+
A well-structured template keeps the information easy for the agent to parse and update. Treat the template as a short form that you want the assistant to keep up to date.
|
|
177
174
|
|
|
178
|
-
- **Short, focused labels.** Avoid paragraphs or very long headings. Keep labels brief (for example
|
|
179
|
-
|
|
180
|
-
- **
|
|
181
|
-
|
|
182
|
-
- **
|
|
183
|
-
fill in the correct spots.
|
|
184
|
-
- **Abbreviate very long values.** If you only need a short form, include guidance like
|
|
185
|
-
`- Name: [First name or nickname]` or `- Address (short):` rather than the full legal text.
|
|
186
|
-
- **Mention update rules in `instructions`.** You can instruct how and when to fill or clear parts of
|
|
187
|
-
the template directly in the agent's `instructions` field.
|
|
175
|
+
- **Short, focused labels.** Avoid paragraphs or very long headings. Keep labels brief (for example `## Personal Info` or `- Name:`) so updates are easy to read and less likely to be truncated.
|
|
176
|
+
- **Use consistent casing.** Inconsistent capitalization (`Timezone:` vs `timezone:`) can cause messy updates. Stick to Title Case or lower case for headings and bullet labels.
|
|
177
|
+
- **Keep placeholder text simple.** Use hints such as `[e.g., Formal]` or `[Date]` to help the LLM fill in the correct spots.
|
|
178
|
+
- **Abbreviate very long values.** If you only need a short form, include guidance like `- Name: [First name or nickname]` or `- Address (short):` rather than the full legal text.
|
|
179
|
+
- **Mention update rules in `instructions`.** You can instruct how and when to fill or clear parts of the template directly in the agent's `instructions` field.
|
|
188
180
|
|
|
189
181
|
### Alternative Template Styles
|
|
190
182
|
|
|
@@ -281,8 +273,7 @@ Schema-based working memory uses **merge semantics**, meaning the agent only nee
|
|
|
281
273
|
|
|
282
274
|
## Example: Multi-step Retention
|
|
283
275
|
|
|
284
|
-
Below is a simplified view of how the `User Profile` template updates across a short user
|
|
285
|
-
conversation:
|
|
276
|
+
Below is a simplified view of how the `User Profile` template updates across a short user conversation:
|
|
286
277
|
|
|
287
278
|
```nohighlight
|
|
288
279
|
# User Profile
|
|
@@ -308,11 +299,9 @@ conversation:
|
|
|
308
299
|
- Timezone: CET
|
|
309
300
|
```
|
|
310
301
|
|
|
311
|
-
The agent can now refer to `Sam` or `Berlin` in later responses without requesting the information
|
|
312
|
-
again because it has been stored in working memory.
|
|
302
|
+
The agent can now refer to `Sam` or `Berlin` in later responses without requesting the information again because it has been stored in working memory.
|
|
313
303
|
|
|
314
|
-
If your agent is not properly updating working memory when you expect it to, you can add system
|
|
315
|
-
instructions on _how_ and _when_ to use this template in your agent's `instructions` setting.
|
|
304
|
+
If your agent is not properly updating working memory when you expect it to, you can add system instructions on _how_ and _when_ to use this template in your agent's `instructions` setting.
|
|
316
305
|
|
|
317
306
|
## Setting Initial Working Memory
|
|
318
307
|
|
|
@@ -322,7 +311,7 @@ While agents typically update working memory through the `updateWorkingMemory` t
|
|
|
322
311
|
|
|
323
312
|
When creating a thread, you can provide initial working memory through the metadata's `workingMemory` key:
|
|
324
313
|
|
|
325
|
-
```typescript
|
|
314
|
+
```typescript
|
|
326
315
|
// Create a thread with initial working memory
|
|
327
316
|
const thread = await memory.createThread({
|
|
328
317
|
threadId: "thread-123",
|
|
@@ -353,7 +342,7 @@ await agent.generate("What's my blood type?", {
|
|
|
353
342
|
|
|
354
343
|
You can also update an existing thread's working memory:
|
|
355
344
|
|
|
356
|
-
```typescript
|
|
345
|
+
```typescript
|
|
357
346
|
// Update thread metadata to add/modify working memory
|
|
358
347
|
await memory.updateThread({
|
|
359
348
|
id: "thread-123",
|
|
@@ -375,7 +364,7 @@ await memory.updateThread({
|
|
|
375
364
|
|
|
376
365
|
Alternatively, use the `updateWorkingMemory` method directly:
|
|
377
366
|
|
|
378
|
-
```typescript
|
|
367
|
+
```typescript
|
|
379
368
|
await memory.updateWorkingMemory({
|
|
380
369
|
threadId: "thread-123",
|
|
381
370
|
resourceId: "user-456", // Required for resource-scoped memory
|
|
@@ -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
|
+
```
|