@mastra/memory 1.41.1-alpha.4 → 1.41.1-alpha.5
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/dist/docs/SKILL.md +3 -3
- package/dist/docs/assets/SOURCE_MAP.json +48 -48
- package/dist/docs/references/docs-memory-multi-user-threads.md +57 -127
- package/dist/docs/references/docs-memory-observational-memory.md +196 -756
- package/dist/docs/references/docs-memory-overview.md +4 -0
- package/dist/docs/references/reference-memory-observational-memory.md +350 -151
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/processors/index.cjs +1 -1
- package/dist/processors/index.js +1 -1
- package/dist/{src-DvkS93hU.cjs → src-DiQcqLOH.cjs} +7 -7
- package/dist/{src-DvkS93hU.cjs.map → src-DiQcqLOH.cjs.map} +1 -1
- package/dist/{src-KIKQ7ztT.js → src-MUc-jGgr.js} +7 -7
- package/dist/{src-KIKQ7ztT.js.map → src-MUc-jGgr.js.map} +1 -1
- package/package.json +4 -4
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-memory
|
|
|
3
3
|
description: Documentation for @mastra/memory. Use when working with @mastra/memory APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/memory"
|
|
6
|
-
version: "1.41.1-alpha.
|
|
6
|
+
version: "1.41.1-alpha.5"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -25,8 +25,8 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
25
25
|
- [Knowledge](references/docs-knowledge-overview.md) - Give agents shared pages and notes they search, read and write, with access decided by your configuration rather than by the model.
|
|
26
26
|
- [Memory processors](references/docs-memory-memory-processors.md) - Configure Mastra memory processors to filter, trim, transform, and deduplicate messages before they reach the model while managing context limits.
|
|
27
27
|
- [Message history](references/docs-memory-message-history.md) - Store and retrieve recent Mastra conversation messages, configure history limits and thread titles, and query threads for chat interfaces.
|
|
28
|
-
- [Multi-user threads](references/docs-memory-multi-user-threads.md) - Share one Mastra memory thread across multiple users by
|
|
29
|
-
- [Observational Memory](references/docs-memory-observational-memory.md) -
|
|
28
|
+
- [Multi-user threads](references/docs-memory-multi-user-threads.md) - Share one Mastra memory thread across multiple users by sending each message as a signal with the speaker in its attributes.
|
|
29
|
+
- [Observational Memory](references/docs-memory-observational-memory.md) - Give agents long-running memory with a small, stable, cache-friendly context. Observational Memory turns older messages into dense observations as a thread grows.
|
|
30
30
|
- [Memory](references/docs-memory-overview.md) - Add memory to Mastra agents with message history, observational memory, and working memory across threads and multi-agent delegation.
|
|
31
31
|
- [Semantic recall](references/docs-memory-semantic-recall.md) - Retrieve relevant messages from past Mastra conversations with semantic recall, vector search, embeddings, metadata filters, and configurable storage.
|
|
32
32
|
- [Working memory](references/docs-memory-working-memory.md) - Persist user profiles, preferences, and application data with Mastra working memory using resource- or thread-scoped templates and storage adapters.
|
|
@@ -1,235 +1,235 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.41.1-alpha.
|
|
2
|
+
"version": "1.41.1-alpha.5",
|
|
3
3
|
"package": "@mastra/memory",
|
|
4
4
|
"exports": {
|
|
5
5
|
"AUTO_MODEL_BY_PROVIDER": {
|
|
6
6
|
"types": "dist/processors/observational-memory/auto-model.d.ts",
|
|
7
|
-
"implementation": "dist/src-
|
|
7
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
8
8
|
"line": 32196
|
|
9
9
|
},
|
|
10
10
|
"Extractor": {
|
|
11
11
|
"types": "dist/processors/observational-memory/extractor.d.ts",
|
|
12
|
-
"implementation": "dist/src-
|
|
12
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
13
13
|
"line": 23939
|
|
14
14
|
},
|
|
15
15
|
"ModelByInputTokens": {
|
|
16
16
|
"types": "dist/processors/observational-memory/model-by-input-tokens.d.ts",
|
|
17
|
-
"implementation": "dist/src-
|
|
17
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
18
18
|
"line": 31979
|
|
19
19
|
},
|
|
20
20
|
"OBSERVATIONAL_MEMORY_DEFAULTS": {
|
|
21
21
|
"types": "dist/processors/observational-memory/constants.d.ts",
|
|
22
|
-
"implementation": "dist/src-
|
|
22
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
23
23
|
"line": 23675
|
|
24
24
|
},
|
|
25
25
|
"OBSERVATION_CONTEXT_INSTRUCTIONS": {
|
|
26
26
|
"types": "dist/processors/observational-memory/constants.d.ts",
|
|
27
|
-
"implementation": "dist/src-
|
|
27
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
28
28
|
"line": 23736
|
|
29
29
|
},
|
|
30
30
|
"OBSERVATION_CONTEXT_PROMPT": {
|
|
31
31
|
"types": "dist/processors/observational-memory/constants.d.ts",
|
|
32
|
-
"implementation": "dist/src-
|
|
32
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
33
33
|
"line": 23718
|
|
34
34
|
},
|
|
35
35
|
"OBSERVATION_CONTEXT_PROMPT_THREAD": {
|
|
36
36
|
"types": "dist/processors/observational-memory/constants.d.ts",
|
|
37
|
-
"implementation": "dist/src-
|
|
37
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
38
38
|
"line": 23723
|
|
39
39
|
},
|
|
40
40
|
"OBSERVATION_CONTINUATION_HINT": {
|
|
41
41
|
"types": "dist/processors/observational-memory/constants.d.ts",
|
|
42
|
-
"implementation": "dist/src-
|
|
42
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
43
43
|
"line": 23705
|
|
44
44
|
},
|
|
45
45
|
"OBSERVER_SYSTEM_PROMPT": {
|
|
46
46
|
"types": "dist/processors/observational-memory/observer-agent.d.ts",
|
|
47
|
-
"implementation": "dist/src-
|
|
47
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
48
48
|
"line": 26300
|
|
49
49
|
},
|
|
50
50
|
"ObservationalMemory": {
|
|
51
51
|
"types": "dist/processors/observational-memory/observational-memory.d.ts",
|
|
52
|
-
"implementation": "dist/src-
|
|
52
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
53
53
|
"line": 37004
|
|
54
54
|
},
|
|
55
55
|
"ObservationalMemoryProcessor": {
|
|
56
56
|
"types": "dist/processors/observational-memory/processor.d.ts",
|
|
57
|
-
"implementation": "dist/src-
|
|
57
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
58
58
|
"line": 39739
|
|
59
59
|
},
|
|
60
60
|
"TokenCounter": {
|
|
61
61
|
"types": "dist/processors/observational-memory/token-counter.d.ts",
|
|
62
|
-
"implementation": "dist/src-
|
|
62
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
63
63
|
"line": 28804
|
|
64
64
|
},
|
|
65
65
|
"WorkingMemoryExtractor": {
|
|
66
66
|
"types": "dist/processors/observational-memory/working-memory-extractor.d.ts",
|
|
67
|
-
"implementation": "dist/src-
|
|
67
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
68
68
|
"line": 29820
|
|
69
69
|
},
|
|
70
70
|
"buildObserverPrompt": {
|
|
71
71
|
"types": "dist/processors/observational-memory/observer-agent.d.ts",
|
|
72
|
-
"implementation": "dist/src-
|
|
72
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
73
73
|
"line": 26983
|
|
74
74
|
},
|
|
75
75
|
"buildObserverSystemPrompt": {
|
|
76
76
|
"types": "dist/processors/observational-memory/observer-agent.d.ts",
|
|
77
|
-
"implementation": "dist/src-
|
|
77
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
78
78
|
"line": 26194
|
|
79
79
|
},
|
|
80
80
|
"combineObservationGroupRanges": {
|
|
81
81
|
"types": "dist/processors/observational-memory/observation-groups.d.ts",
|
|
82
|
-
"implementation": "dist/src-
|
|
82
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
83
83
|
"line": 30299
|
|
84
84
|
},
|
|
85
85
|
"deriveObservationGroupProvenance": {
|
|
86
86
|
"types": "dist/processors/observational-memory/observation-groups.d.ts",
|
|
87
|
-
"implementation": "dist/src-
|
|
87
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
88
88
|
"line": 30343
|
|
89
89
|
},
|
|
90
90
|
"extractCurrentTask": {
|
|
91
91
|
"types": "dist/processors/observational-memory/observer-agent.d.ts",
|
|
92
|
-
"implementation": "dist/src-
|
|
92
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
93
93
|
"line": 27296
|
|
94
94
|
},
|
|
95
95
|
"formatMessagesForObserver": {
|
|
96
96
|
"types": "dist/processors/observational-memory/observer-agent.d.ts",
|
|
97
|
-
"implementation": "dist/src-
|
|
97
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
98
98
|
"line": 26735
|
|
99
99
|
},
|
|
100
100
|
"getObservationContextPrompt": {
|
|
101
101
|
"types": "dist/processors/observational-memory/constants.d.ts",
|
|
102
|
-
"implementation": "dist/src-
|
|
102
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
103
103
|
"line": 23729
|
|
104
104
|
},
|
|
105
105
|
"getObservationsAsOf": {
|
|
106
106
|
"types": "dist/processors/observational-memory/observation-utils.d.ts",
|
|
107
|
-
"implementation": "dist/src-
|
|
107
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
108
108
|
"line": 40046
|
|
109
109
|
},
|
|
110
110
|
"hasCurrentTaskSection": {
|
|
111
111
|
"types": "dist/processors/observational-memory/observer-agent.d.ts",
|
|
112
|
-
"implementation": "dist/src-
|
|
112
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
113
113
|
"line": 27284
|
|
114
114
|
},
|
|
115
115
|
"injectAnchorIds": {
|
|
116
116
|
"types": "dist/processors/observational-memory/anchor-ids.d.ts",
|
|
117
|
-
"implementation": "dist/src-
|
|
117
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
118
118
|
"line": 24916
|
|
119
119
|
},
|
|
120
120
|
"optimizeObservationsForContext": {
|
|
121
121
|
"types": "dist/processors/observational-memory/observer-agent.d.ts",
|
|
122
|
-
"implementation": "dist/src-
|
|
122
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
123
123
|
"line": 27317
|
|
124
124
|
},
|
|
125
125
|
"parseAnchorId": {
|
|
126
126
|
"types": "dist/processors/observational-memory/anchor-ids.d.ts",
|
|
127
|
-
"implementation": "dist/src-
|
|
127
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
128
128
|
"line": 24900
|
|
129
129
|
},
|
|
130
130
|
"parseObservationGroups": {
|
|
131
131
|
"types": "dist/processors/observational-memory/observation-groups.d.ts",
|
|
132
|
-
"implementation": "dist/src-
|
|
132
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
133
133
|
"line": 30254
|
|
134
134
|
},
|
|
135
135
|
"parseObserverOutput": {
|
|
136
136
|
"types": "dist/processors/observational-memory/observer-agent.d.ts",
|
|
137
|
-
"implementation": "dist/src-
|
|
137
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
138
138
|
"line": 26994
|
|
139
139
|
},
|
|
140
140
|
"reconcileObservationGroupsFromReflection": {
|
|
141
141
|
"types": "dist/processors/observational-memory/observation-groups.d.ts",
|
|
142
|
-
"implementation": "dist/src-
|
|
142
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
143
143
|
"line": 30362
|
|
144
144
|
},
|
|
145
145
|
"renderObservationGroupsForReflection": {
|
|
146
146
|
"types": "dist/processors/observational-memory/observation-groups.d.ts",
|
|
147
|
-
"implementation": "dist/src-
|
|
147
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
148
148
|
"line": 30328
|
|
149
149
|
},
|
|
150
150
|
"resolveAutoModelId": {
|
|
151
151
|
"types": "dist/processors/observational-memory/auto-model.d.ts",
|
|
152
|
-
"implementation": "dist/src-
|
|
152
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
153
153
|
"line": 32216
|
|
154
154
|
},
|
|
155
155
|
"stripEphemeralAnchorIds": {
|
|
156
156
|
"types": "dist/processors/observational-memory/anchor-ids.d.ts",
|
|
157
|
-
"implementation": "dist/src-
|
|
157
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
158
158
|
"line": 24939
|
|
159
159
|
},
|
|
160
160
|
"stripObservationGroups": {
|
|
161
161
|
"types": "dist/processors/observational-memory/observation-groups.d.ts",
|
|
162
|
-
"implementation": "dist/src-
|
|
162
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
163
163
|
"line": 30271
|
|
164
164
|
},
|
|
165
165
|
"summarizeConversation": {
|
|
166
166
|
"types": "dist/processors/observational-memory/summarize.d.ts",
|
|
167
|
-
"implementation": "dist/src-
|
|
167
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
168
168
|
"line": 29435
|
|
169
169
|
},
|
|
170
170
|
"wrapInObservationGroup": {
|
|
171
171
|
"types": "dist/processors/observational-memory/observation-groups.d.ts",
|
|
172
|
-
"implementation": "dist/src-
|
|
172
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
173
173
|
"line": 30225
|
|
174
174
|
},
|
|
175
175
|
"KnowledgeSemanticIndexCoordinator": {
|
|
176
176
|
"types": "dist/removed-subconscious.d.ts",
|
|
177
|
-
"implementation": "dist/src-
|
|
177
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
178
178
|
"line": 29867
|
|
179
179
|
},
|
|
180
180
|
"Memory": {
|
|
181
181
|
"types": "dist/index.d.ts",
|
|
182
|
-
"implementation": "dist/src-
|
|
182
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
183
183
|
"line": 40257
|
|
184
184
|
},
|
|
185
185
|
"SUMMARIZE_THREAD_DEFAULTS": {
|
|
186
186
|
"types": "dist/processors/observational-memory/summarize.d.ts",
|
|
187
|
-
"implementation": "dist/src-
|
|
187
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
188
188
|
"line": 29391
|
|
189
189
|
},
|
|
190
190
|
"StaleKnowledgeSemanticIndexError": {
|
|
191
191
|
"types": "dist/removed-subconscious.d.ts",
|
|
192
|
-
"implementation": "dist/src-
|
|
192
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
193
193
|
"line": 29873
|
|
194
194
|
},
|
|
195
195
|
"Subconscious": {
|
|
196
196
|
"types": "dist/removed-subconscious.d.ts",
|
|
197
|
-
"implementation": "dist/src-
|
|
197
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
198
198
|
"line": 29861
|
|
199
199
|
},
|
|
200
200
|
"WORKING_MEMORY_STATE_ID": {
|
|
201
201
|
"types": "dist/processors/working-memory-state/processor.d.ts",
|
|
202
|
-
"implementation": "dist/src-
|
|
202
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
203
203
|
"line": 32025
|
|
204
204
|
},
|
|
205
205
|
"WORKING_MEMORY_STATE_PROCESSOR_ID": {
|
|
206
206
|
"types": "dist/processors/working-memory-state/processor.d.ts",
|
|
207
|
-
"implementation": "dist/src-
|
|
207
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
208
208
|
"line": 32026
|
|
209
209
|
},
|
|
210
210
|
"WorkingMemoryStateProcessor": {
|
|
211
211
|
"types": "dist/processors/working-memory-state/processor.d.ts",
|
|
212
|
-
"implementation": "dist/src-
|
|
212
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
213
213
|
"line": 32027
|
|
214
214
|
},
|
|
215
215
|
"deepMergeWorkingMemory": {
|
|
216
216
|
"types": "dist/tools/working-memory.d.ts",
|
|
217
|
-
"implementation": "dist/src-
|
|
217
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
218
218
|
"line": 29514
|
|
219
219
|
},
|
|
220
220
|
"extractWorkingMemoryContent": {
|
|
221
221
|
"types": "dist/index.d.ts",
|
|
222
|
-
"implementation": "dist/src-
|
|
222
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
223
223
|
"line": 40111
|
|
224
224
|
},
|
|
225
225
|
"extractWorkingMemoryTags": {
|
|
226
226
|
"types": "dist/index.d.ts",
|
|
227
|
-
"implementation": "dist/src-
|
|
227
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
228
228
|
"line": 40079
|
|
229
229
|
},
|
|
230
230
|
"removeWorkingMemoryTags": {
|
|
231
231
|
"types": "dist/index.d.ts",
|
|
232
|
-
"implementation": "dist/src-
|
|
232
|
+
"implementation": "dist/src-MUc-jGgr.js",
|
|
233
233
|
"line": 40092
|
|
234
234
|
}
|
|
235
235
|
},
|
|
@@ -237,7 +237,7 @@
|
|
|
237
237
|
"processors": {
|
|
238
238
|
"index": "dist/processors/index.js",
|
|
239
239
|
"chunks": [
|
|
240
|
-
"src-
|
|
240
|
+
"src-MUc-jGgr.js"
|
|
241
241
|
]
|
|
242
242
|
}
|
|
243
243
|
}
|
|
@@ -4,60 +4,50 @@
|
|
|
4
4
|
|
|
5
5
|
# Multi-user threads
|
|
6
6
|
|
|
7
|
-
A single Mastra thread can be shared by
|
|
7
|
+
A single Mastra thread can be shared by several users. Send each user's message with [`sendMessage()`](https://mastra.ai/docs/signals/sending-signals) and put the speaker in the message's `attributes`. Mastra renders the message as a `<user>` tag carrying those attributes, so the agent can tell the participants apart, now and when the message is loaded from history later.
|
|
8
8
|
|
|
9
9
|
## When to use multi-user threads
|
|
10
10
|
|
|
11
|
-
Use multi-user threads when several people
|
|
11
|
+
Use multi-user threads when several people work on the same subject through one agent:
|
|
12
12
|
|
|
13
|
-
- Collaborative documents with editors, reviewers
|
|
13
|
+
- Collaborative documents with editors, reviewers and approvers
|
|
14
14
|
- Group chats where one assistant serves many participants
|
|
15
|
-
-
|
|
15
|
+
- Reviews where different roles have different authority
|
|
16
16
|
|
|
17
17
|
## Share one `resourceId` across all participants
|
|
18
18
|
|
|
19
|
-
A thread belongs to exactly one `resourceId`, so
|
|
19
|
+
A thread belongs to exactly one `resourceId`, so everyone on a shared thread needs to pass the same value. Instead of a user ID (the default for single-user apps), key `resourceId` on the conversation itself, for example `doc_${docId}` for a shared document or `room_${roomId}` for a group chat. With everyone pointing at the same `resourceId` and `threadId`, they read and write the same history.
|
|
20
20
|
|
|
21
|
-
##
|
|
21
|
+
## Send each message with the speaker's identity
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
Build the tag with a small helper. The example below is one way to do it, copy it into your project and adapt it to your shape of user data:
|
|
23
|
+
Pass the message as an object with `contents` and `attributes`. Each attribute becomes an XML attribute on the `<user>` tag the model reads:
|
|
26
24
|
|
|
27
25
|
```typescript
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
26
|
+
agent.sendMessage(
|
|
27
|
+
{
|
|
28
|
+
contents: 'Please make the cover page teal.',
|
|
29
|
+
attributes: { name: 'Alice', role: 'editor' },
|
|
30
|
+
},
|
|
31
|
+
{ resourceId: 'doc_42', threadId: 'doc_42' },
|
|
32
|
+
)
|
|
33
|
+
```
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
return value
|
|
36
|
-
.replace(/&/g, '&')
|
|
37
|
-
.replace(/"/g, '"')
|
|
38
|
-
.replace(/</g, '<')
|
|
39
|
-
.replace(/>/g, '>')
|
|
40
|
-
}
|
|
35
|
+
The model receives:
|
|
41
36
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const name = escapeAttr(speaker.name)
|
|
45
|
-
const role = escapeAttr(speaker.role)
|
|
46
|
-
return {
|
|
47
|
-
role: 'user' as const,
|
|
48
|
-
content: `<turn author_id="${id}" author_name="${name}" functional_role="${role}">
|
|
49
|
-
${text}
|
|
50
|
-
</turn>`,
|
|
51
|
-
}
|
|
52
|
-
}
|
|
37
|
+
```xml
|
|
38
|
+
<user name="Alice" role="editor">Please make the cover page teal.</user>
|
|
53
39
|
```
|
|
54
40
|
|
|
55
|
-
|
|
41
|
+
Mastra escapes attribute values for you. Attribute names must be valid XML names, such as `name`, `role` or `user_id`. Any other name fails with `Invalid signal XML attribute name` when the message is rendered for the model. The attributes are stored with the message, so when history loads on a later turn, the model still sees who said what.
|
|
42
|
+
|
|
43
|
+
## Tell the agent how to read the tag
|
|
44
|
+
|
|
45
|
+
Describe the `<user>` tag and its attributes in the agent's instructions. The agent needs `memory` so its messages are stored on the thread:
|
|
56
46
|
|
|
57
47
|
```typescript
|
|
58
48
|
import { Agent } from '@mastra/core/agent'
|
|
59
|
-
import { Memory } from '@mastra/memory'
|
|
60
49
|
import { LibSQLStore } from '@mastra/libsql'
|
|
50
|
+
import { Memory } from '@mastra/memory'
|
|
61
51
|
|
|
62
52
|
const memory = new Memory({
|
|
63
53
|
storage: new LibSQLStore({ id: 'collab-storage', url: 'file:./collab.db' }),
|
|
@@ -72,139 +62,79 @@ export const collabAgent = new Agent({
|
|
|
72
62
|
model: 'openai/gpt-6-luna',
|
|
73
63
|
memory,
|
|
74
64
|
instructions: `
|
|
75
|
-
You are a collaborative document assistant.
|
|
65
|
+
You are a collaborative document assistant. Several people talk to you in the same thread.
|
|
76
66
|
|
|
77
|
-
|
|
67
|
+
Each user message arrives in a <user> tag whose attributes say who sent it:
|
|
78
68
|
|
|
79
|
-
<
|
|
80
|
-
...message text...
|
|
81
|
-
</turn>
|
|
69
|
+
<user name="Alice" role="editor">...message text...</user>
|
|
82
70
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
2. Respect functional_role: editors propose changes, reviewers approve.
|
|
86
|
-
3. When attributing past statements, read author_name from the surrounding <turn> tag.
|
|
87
|
-
4. Do not echo the <turn> tags back at users.
|
|
71
|
+
Address people by name. Editors propose changes and reviewers approve them.
|
|
72
|
+
When you refer to something said earlier, name the person who said it.
|
|
88
73
|
`.trim(),
|
|
89
74
|
})
|
|
90
75
|
```
|
|
91
76
|
|
|
92
|
-
|
|
77
|
+
Send every participant's message to the same thread. When the thread is idle, `sendMessage()` starts a run and `accepted` resolves with its output:
|
|
93
78
|
|
|
94
79
|
```typescript
|
|
95
|
-
import {
|
|
80
|
+
import { collabAgent } from './agents/collab'
|
|
96
81
|
|
|
97
|
-
const
|
|
98
|
-
const docThreadId = 'doc_42'
|
|
82
|
+
const doc = { resourceId: 'doc_42', threadId: 'doc_42' }
|
|
99
83
|
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
84
|
+
async function send(speaker: { name: string; role: string }, text: string) {
|
|
85
|
+
const { accepted } = collabAgent.sendMessage({ contents: text, attributes: speaker }, doc)
|
|
86
|
+
const result = await accepted
|
|
87
|
+
if (result.action === 'wake') {
|
|
88
|
+
return (await result.output.getFullOutput()).text
|
|
89
|
+
}
|
|
90
|
+
}
|
|
106
91
|
|
|
107
|
-
await
|
|
108
|
-
|
|
109
|
-
})
|
|
92
|
+
await send({ name: 'Alice', role: 'editor' }, 'Please make the cover page teal.')
|
|
93
|
+
await send({ name: 'Bob', role: 'reviewer' }, 'I want QA sign-off before we publish.')
|
|
94
|
+
await send({ name: 'Alice', role: 'editor' }, 'Who asked for QA sign-off?')
|
|
95
|
+
// The agent answers that Bob asked for it.
|
|
110
96
|
```
|
|
111
97
|
|
|
112
|
-
|
|
98
|
+
When a message arrives while the agent is still answering someone else, Mastra delivers it into the running turn instead of starting a new one, and `accepted` resolves with `action: 'deliver'`. In a live app, render replies from the [thread stream](https://mastra.ai/docs/signals/thread-streams) rather than from each `sendMessage()` call. That way every participant sees the same conversation.
|
|
113
99
|
|
|
114
100
|
## Combining with memory layers
|
|
115
101
|
|
|
116
|
-
|
|
102
|
+
Pick the memory layer by how long the conversation needs to remember each participant:
|
|
117
103
|
|
|
118
|
-
- **Short conversations
|
|
119
|
-
- **Long-running threads**
|
|
120
|
-
- **Need a structured participants list, or your storage adapter doesn't support OM** (OM requires LibSQL, PG, or MongoDB): use [working memory](#with-working-memory).
|
|
121
|
-
|
|
122
|
-
We recommend using either observational memory or working memory because they cover overlapping needs. Running both adds latency and token cost without much benefit.
|
|
104
|
+
- **Short conversations**, or when you need a verbatim record of who said what: use [message history alone](#message-history-alone).
|
|
105
|
+
- **Long-running threads** that outgrow the context window: use [Observational Memory](#with-observational-memory).
|
|
123
106
|
|
|
124
107
|
### Message history alone
|
|
125
108
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
```typescript
|
|
129
|
-
import { Memory } from '@mastra/memory'
|
|
130
|
-
import { LibSQLStore } from '@mastra/libsql'
|
|
131
|
-
|
|
132
|
-
const memory = new Memory({
|
|
133
|
-
storage: new LibSQLStore({ id: 'collab-storage', url: 'file:./collab.db' }),
|
|
134
|
-
options: {
|
|
135
|
-
lastMessages: 20,
|
|
136
|
-
},
|
|
137
|
-
})
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
The model reads identity from the `<turn>` tag on the current message and from prior tagged messages brought back through `lastMessages`.
|
|
141
|
-
|
|
142
|
-
### With Observational Memory (recommended)
|
|
109
|
+
`lastMessages` brings earlier messages back into context with their `<user>` attributes, so attribution survives across turns. The agent example above uses this setup.
|
|
143
110
|
|
|
144
|
-
|
|
111
|
+
### With Observational Memory
|
|
145
112
|
|
|
146
|
-
|
|
113
|
+
[Observational Memory](https://mastra.ai/docs/memory/observational-memory) (OM) keeps a long shared thread inside the context window by replacing older messages with observations. Enable it on the same `Memory`:
|
|
147
114
|
|
|
148
115
|
```typescript
|
|
149
|
-
import { Memory } from '@mastra/memory'
|
|
150
116
|
import { LibSQLStore } from '@mastra/libsql'
|
|
151
|
-
|
|
152
|
-
const memory = new Memory({
|
|
153
|
-
storage: new LibSQLStore({ id: 'collab-storage', url: 'file:./collab.db' }),
|
|
154
|
-
options: {
|
|
155
|
-
lastMessages: 20,
|
|
156
|
-
observationalMemory: true,
|
|
157
|
-
},
|
|
158
|
-
})
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
OM requires a storage adapter that supports it: `@mastra/libsql`, `@mastra/pg`, `@mastra/mongodb`, `@mastra/mysql`, `@mastra/convex`, or `@mastra/oracledb`.
|
|
162
|
-
|
|
163
|
-
> **Note:** If you switch the Observer to a less capable model and see facts attributed to a generic `User` instead of individual participants, use [`observation.instruction`](https://mastra.ai/reference/memory/observational-memory) to explain how to interpret the `<turn>` tag.
|
|
164
|
-
|
|
165
|
-
### With working memory
|
|
166
|
-
|
|
167
|
-
Use working memory when OM isn't an option, for example, when your storage adapter doesn't support OM, or when you need a structured, deterministic participants list the agent can read and write on every turn.
|
|
168
|
-
|
|
169
|
-
The default [working memory](https://mastra.ai/docs/memory/working-memory) template assumes one user per thread ("First Name", "Last Name", etc.). For multi-user threads, provide a template with a participants list:
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
117
|
import { Memory } from '@mastra/memory'
|
|
173
|
-
import { LibSQLStore } from '@mastra/libsql'
|
|
174
118
|
|
|
175
119
|
const memory = new Memory({
|
|
176
120
|
storage: new LibSQLStore({ id: 'collab-storage', url: 'file:./collab.db' }),
|
|
177
121
|
options: {
|
|
178
|
-
|
|
179
|
-
workingMemory: {
|
|
180
|
-
enabled: true,
|
|
181
|
-
scope: 'thread',
|
|
182
|
-
template: `# Document Collaboration State
|
|
183
|
-
|
|
184
|
-
## Participants
|
|
185
|
-
<!-- One entry per known collaborator. Use author_id as the stable key. -->
|
|
186
|
-
<!-- - **<author_name>** (<author_id>, <functional_role>): <their position> -->
|
|
187
|
-
|
|
188
|
-
## Open Questions
|
|
189
|
-
|
|
190
|
-
## Decisions
|
|
191
|
-
`,
|
|
192
|
-
},
|
|
122
|
+
observationalMemory: true,
|
|
193
123
|
},
|
|
194
124
|
})
|
|
195
125
|
```
|
|
196
126
|
|
|
197
|
-
|
|
127
|
+
OM replaces `lastMessages` history loading, so leave `lastMessages` out when OM is on.
|
|
198
128
|
|
|
199
|
-
|
|
129
|
+
The Observer keeps the speaker's attributes, so observations say who said what. A message sent with `attributes: { name: 'Alice', role: 'editor' }` is labeled `User (name="Alice" role="editor")`. Messages sent without attributes are labeled plain `User`.
|
|
200
130
|
|
|
201
131
|
## Security
|
|
202
132
|
|
|
203
|
-
Set the
|
|
133
|
+
Set the attributes on your server from the authenticated user, never from the request body. If a client can choose its own `name`, one user can impersonate another. Use [request context](https://mastra.ai/docs/server/request-context) to read the verified user from your auth layer and build the `attributes` before calling `sendMessage()`.
|
|
204
134
|
|
|
205
135
|
## Related
|
|
206
136
|
|
|
207
|
-
- [
|
|
208
|
-
- [
|
|
209
|
-
- [
|
|
137
|
+
- [Sending signals](https://mastra.ai/docs/signals/sending-signals)
|
|
138
|
+
- [Agent `sendMessage()` reference](https://mastra.ai/reference/agents/agent)
|
|
139
|
+
- [Observational Memory](https://mastra.ai/docs/memory/observational-memory)
|
|
210
140
|
- [`Memory` reference](https://mastra.ai/reference/memory/memory-class)
|