@mastra/memory 1.6.0 → 1.6.1
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 +54 -0
- package/dist/{chunk-5UYAHJVJ.cjs → chunk-D6II7EP4.cjs} +660 -531
- package/dist/chunk-D6II7EP4.cjs.map +1 -0
- package/dist/{chunk-A62BQK35.js → chunk-GBBQIJQF.js} +660 -531
- package/dist/chunk-GBBQIJQF.js.map +1 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +25 -25
- package/dist/docs/references/docs-agents-agent-approval.md +61 -31
- package/dist/docs/references/docs-agents-supervisor-agents.md +1 -1
- package/dist/docs/references/docs-memory-observational-memory.md +9 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +17 -1
- package/dist/docs/references/reference-core-getMemory.md +2 -2
- package/dist/docs/references/reference-core-listMemory.md +1 -1
- package/dist/docs/references/reference-memory-clone-utilities.md +5 -5
- package/dist/docs/references/reference-memory-cloneThread.md +17 -21
- package/dist/docs/references/reference-memory-createThread.md +10 -10
- package/dist/docs/references/reference-memory-getThreadById.md +2 -2
- package/dist/docs/references/reference-memory-listThreads.md +5 -5
- package/dist/docs/references/reference-memory-memory-class.md +12 -14
- package/dist/docs/references/reference-memory-observational-memory.md +102 -94
- package/dist/docs/references/reference-processors-token-limiter-processor.md +11 -13
- package/dist/docs/references/reference-storage-dynamodb.md +9 -9
- package/dist/docs/references/reference-storage-libsql.md +2 -2
- package/dist/docs/references/reference-storage-mongodb.md +5 -5
- package/dist/docs/references/reference-storage-postgresql.md +25 -25
- package/dist/docs/references/reference-storage-upstash.md +3 -3
- package/dist/docs/references/reference-vectors-libsql.md +31 -31
- package/dist/docs/references/reference-vectors-mongodb.md +32 -32
- package/dist/docs/references/reference-vectors-pg.md +60 -44
- package/dist/docs/references/reference-vectors-upstash.md +25 -25
- package/dist/index.cjs +246 -57
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +246 -57
- package/dist/index.js.map +1 -1
- package/dist/{observational-memory-MXI54VC7.cjs → observational-memory-AHVELJX4.cjs} +17 -17
- package/dist/{observational-memory-MXI54VC7.cjs.map → observational-memory-AHVELJX4.cjs.map} +1 -1
- package/dist/{observational-memory-SR6G4HN5.js → observational-memory-QFQUF5EY.js} +3 -3
- package/dist/{observational-memory-SR6G4HN5.js.map → observational-memory-QFQUF5EY.js.map} +1 -1
- package/dist/processors/index.cjs +15 -15
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/date-utils.d.ts +35 -0
- package/dist/processors/observational-memory/date-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/markers.d.ts +94 -0
- package/dist/processors/observational-memory/markers.d.ts.map +1 -0
- package/dist/processors/observational-memory/observational-memory.d.ts +0 -76
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- 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/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 +4 -0
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/chunk-5UYAHJVJ.cjs.map +0 -1
- package/dist/chunk-A62BQK35.js.map +0 -1
package/dist/docs/SKILL.md
CHANGED
|
@@ -1,71 +1,71 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.6.
|
|
2
|
+
"version": "1.6.1",
|
|
3
3
|
"package": "@mastra/memory",
|
|
4
4
|
"exports": {
|
|
5
5
|
"OBSERVATIONAL_MEMORY_DEFAULTS": {
|
|
6
6
|
"types": "dist/processors/index.d.ts",
|
|
7
|
-
"implementation": "dist/chunk-
|
|
7
|
+
"implementation": "dist/chunk-GBBQIJQF.js"
|
|
8
8
|
},
|
|
9
9
|
"OBSERVATION_CONTEXT_INSTRUCTIONS": {
|
|
10
10
|
"types": "dist/processors/index.d.ts",
|
|
11
|
-
"implementation": "dist/chunk-
|
|
11
|
+
"implementation": "dist/chunk-GBBQIJQF.js"
|
|
12
12
|
},
|
|
13
13
|
"OBSERVATION_CONTEXT_PROMPT": {
|
|
14
14
|
"types": "dist/processors/index.d.ts",
|
|
15
|
-
"implementation": "dist/chunk-
|
|
15
|
+
"implementation": "dist/chunk-GBBQIJQF.js"
|
|
16
16
|
},
|
|
17
17
|
"OBSERVATION_CONTINUATION_HINT": {
|
|
18
18
|
"types": "dist/processors/index.d.ts",
|
|
19
|
-
"implementation": "dist/chunk-
|
|
19
|
+
"implementation": "dist/chunk-GBBQIJQF.js"
|
|
20
20
|
},
|
|
21
21
|
"OBSERVER_SYSTEM_PROMPT": {
|
|
22
22
|
"types": "dist/processors/index.d.ts",
|
|
23
|
-
"implementation": "dist/chunk-
|
|
23
|
+
"implementation": "dist/chunk-GBBQIJQF.js"
|
|
24
24
|
},
|
|
25
25
|
"ObservationalMemory": {
|
|
26
26
|
"types": "dist/processors/index.d.ts",
|
|
27
|
-
"implementation": "dist/chunk-
|
|
28
|
-
"line":
|
|
27
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
28
|
+
"line": 1642
|
|
29
29
|
},
|
|
30
30
|
"TokenCounter": {
|
|
31
31
|
"types": "dist/processors/index.d.ts",
|
|
32
|
-
"implementation": "dist/chunk-
|
|
33
|
-
"line":
|
|
32
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
33
|
+
"line": 1401
|
|
34
34
|
},
|
|
35
35
|
"buildObserverPrompt": {
|
|
36
36
|
"types": "dist/processors/index.d.ts",
|
|
37
|
-
"implementation": "dist/chunk-
|
|
38
|
-
"line":
|
|
37
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
38
|
+
"line": 822
|
|
39
39
|
},
|
|
40
40
|
"buildObserverSystemPrompt": {
|
|
41
41
|
"types": "dist/processors/index.d.ts",
|
|
42
|
-
"implementation": "dist/chunk-
|
|
43
|
-
"line":
|
|
42
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
43
|
+
"line": 570
|
|
44
44
|
},
|
|
45
45
|
"extractCurrentTask": {
|
|
46
46
|
"types": "dist/processors/index.d.ts",
|
|
47
|
-
"implementation": "dist/chunk-
|
|
48
|
-
"line":
|
|
47
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
48
|
+
"line": 951
|
|
49
49
|
},
|
|
50
50
|
"formatMessagesForObserver": {
|
|
51
51
|
"types": "dist/processors/index.d.ts",
|
|
52
|
-
"implementation": "dist/chunk-
|
|
53
|
-
"line":
|
|
52
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
53
|
+
"line": 665
|
|
54
54
|
},
|
|
55
55
|
"hasCurrentTaskSection": {
|
|
56
56
|
"types": "dist/processors/index.d.ts",
|
|
57
|
-
"implementation": "dist/chunk-
|
|
58
|
-
"line":
|
|
57
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
58
|
+
"line": 939
|
|
59
59
|
},
|
|
60
60
|
"optimizeObservationsForContext": {
|
|
61
61
|
"types": "dist/processors/index.d.ts",
|
|
62
|
-
"implementation": "dist/chunk-
|
|
63
|
-
"line":
|
|
62
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
63
|
+
"line": 962
|
|
64
64
|
},
|
|
65
65
|
"parseObserverOutput": {
|
|
66
66
|
"types": "dist/processors/index.d.ts",
|
|
67
|
-
"implementation": "dist/chunk-
|
|
68
|
-
"line":
|
|
67
|
+
"implementation": "dist/chunk-GBBQIJQF.js",
|
|
68
|
+
"line": 853
|
|
69
69
|
},
|
|
70
70
|
"extractWorkingMemoryContent": {
|
|
71
71
|
"types": "dist/index.d.ts",
|
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
"processors": {
|
|
97
97
|
"index": "dist/processors/index.js",
|
|
98
98
|
"chunks": [
|
|
99
|
-
"chunk-
|
|
99
|
+
"chunk-GBBQIJQF.js"
|
|
100
100
|
]
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -1,10 +1,29 @@
|
|
|
1
|
-
# Agent
|
|
1
|
+
# Agent approval
|
|
2
2
|
|
|
3
|
-
Agents sometimes require the same [human-in-the-loop](https://mastra.ai/docs/workflows/human-in-the-loop) oversight used in workflows when calling tools that handle sensitive operations, like deleting resources or
|
|
3
|
+
Agents sometimes require the same [human-in-the-loop](https://mastra.ai/docs/workflows/human-in-the-loop) oversight used in workflows when calling tools that handle sensitive operations, like deleting resources or running long processes. With agent approval you can suspend a tool call before it executes so a human can approve or decline it, or let tools suspend themselves to request additional context from the user.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## How approval works
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Mastra offers two distinct mechanisms for pausing tool calls: **pre-execution approval** and **runtime suspension**.
|
|
8
|
+
|
|
9
|
+
### Pre-execution approval
|
|
10
|
+
|
|
11
|
+
Pre-execution approval pauses a tool call _before_ its `execute` function runs. The LLM still decides which tool to call and provides arguments, but `execute` doesn't run until you explicitly approve.
|
|
12
|
+
|
|
13
|
+
Two flags control this, combined with OR logic. If _either_ is `true`, the call pauses:
|
|
14
|
+
|
|
15
|
+
| Flag | Where to set it | Scope |
|
|
16
|
+
| --------------------------- | --------------------------------- | ------------------------------------------- |
|
|
17
|
+
| `requireToolApproval: true` | `stream()` / `generate()` options | Pauses **every** tool call for that request |
|
|
18
|
+
| `requireApproval: true` | `createTool()` definition | Pauses calls to **that specific tool** |
|
|
19
|
+
|
|
20
|
+
The stream emits a `tool-call-approval` chunk when a call is paused this way. You then call `approveToolCall()` or `declineToolCall()` to continue.
|
|
21
|
+
|
|
22
|
+
### Runtime suspension with `suspend()`
|
|
23
|
+
|
|
24
|
+
A tool can also pause _during_ its `execute` function by calling `suspend()`. This is useful when the tool starts running and then discovers it needs additional user input or confirmation before it can finish.
|
|
25
|
+
|
|
26
|
+
The stream emits a `tool-call-suspended` chunk with a custom payload defined by the tool's `suspendSchema`. You resume by calling `resumeStream()` with data matching the tool's `resumeSchema`.
|
|
8
27
|
|
|
9
28
|
### Storage
|
|
10
29
|
|
|
@@ -24,7 +43,7 @@ export const mastra = new Mastra({
|
|
|
24
43
|
|
|
25
44
|
## Agent-level approval
|
|
26
45
|
|
|
27
|
-
|
|
46
|
+
Pass `requireToolApproval: true` to `stream()` or `generate()` to pause every tool call before execution. The LLM still decides which tools to call and with what arguments but no tool runs until you approve or decline.
|
|
28
47
|
|
|
29
48
|
```typescript
|
|
30
49
|
const stream = await agent.stream("What's the weather in London?", {
|
|
@@ -32,9 +51,20 @@ const stream = await agent.stream("What's the weather in London?", {
|
|
|
32
51
|
})
|
|
33
52
|
```
|
|
34
53
|
|
|
54
|
+
When a tool call is paused, the stream emits a `tool-call-approval` chunk containing the `toolCallId`, `toolName`, and `args`. Use this to inspect the pending call and decide whether to approve or decline:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
for await (const chunk of stream.fullStream) {
|
|
58
|
+
if (chunk.type === 'tool-call-approval') {
|
|
59
|
+
console.log('Tool:', chunk.payload.toolName)
|
|
60
|
+
console.log('Args:', chunk.payload.args)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
35
65
|
### Approving tool calls
|
|
36
66
|
|
|
37
|
-
|
|
67
|
+
Call `approveToolCall()` on the agent with the `runId` of the stream to resume the suspended tool call and let it execute:
|
|
38
68
|
|
|
39
69
|
```typescript
|
|
40
70
|
const handleApproval = async () => {
|
|
@@ -49,7 +79,7 @@ const handleApproval = async () => {
|
|
|
49
79
|
|
|
50
80
|
### Declining tool calls
|
|
51
81
|
|
|
52
|
-
|
|
82
|
+
Call `declineToolCall()` on the agent to skip the tool call. The agent continues without executing the tool and responds accordingly:
|
|
53
83
|
|
|
54
84
|
```typescript
|
|
55
85
|
const handleDecline = async () => {
|
|
@@ -64,19 +94,19 @@ const handleDecline = async () => {
|
|
|
64
94
|
|
|
65
95
|
## Tool approval with generate()
|
|
66
96
|
|
|
67
|
-
Tool approval also works with the `generate()` method for non-streaming use cases. When
|
|
97
|
+
Tool approval also works with the `generate()` method for non-streaming use cases. When a tool requires approval during a `generate()` call, the method returns immediately instead of executing the tool.
|
|
68
98
|
|
|
69
99
|
### How it works
|
|
70
100
|
|
|
71
101
|
When a tool requires approval during a `generate()` call, the response includes:
|
|
72
102
|
|
|
73
|
-
- `finishReason: 'suspended'
|
|
74
|
-
- `suspendPayload
|
|
75
|
-
- `runId
|
|
103
|
+
- `finishReason: 'suspended'`: Indicates the agent is waiting for approval
|
|
104
|
+
- `suspendPayload`: Contains tool call details (`toolCallId`, `toolName`, `args`)
|
|
105
|
+
- `runId`: Needed to approve or decline the tool call
|
|
76
106
|
|
|
77
107
|
### Approving tool calls
|
|
78
108
|
|
|
79
|
-
|
|
109
|
+
Use `approveToolCallGenerate()` to approve the tool call and get the final result:
|
|
80
110
|
|
|
81
111
|
```typescript
|
|
82
112
|
const output = await agent.generate('Find user John', {
|
|
@@ -99,7 +129,7 @@ if (output.finishReason === 'suspended') {
|
|
|
99
129
|
|
|
100
130
|
### Declining tool calls
|
|
101
131
|
|
|
102
|
-
|
|
132
|
+
Use `declineToolCallGenerate()` to skip the tool call:
|
|
103
133
|
|
|
104
134
|
```typescript
|
|
105
135
|
if (output.finishReason === 'suspended') {
|
|
@@ -108,12 +138,12 @@ if (output.finishReason === 'suspended') {
|
|
|
108
138
|
toolCallId: output.suspendPayload.toolCallId,
|
|
109
139
|
})
|
|
110
140
|
|
|
111
|
-
// Agent
|
|
141
|
+
// Agent responds acknowledging the declined tool
|
|
112
142
|
console.log(result.text)
|
|
113
143
|
}
|
|
114
144
|
```
|
|
115
145
|
|
|
116
|
-
### Stream vs
|
|
146
|
+
### Stream vs generate comparison
|
|
117
147
|
|
|
118
148
|
| Aspect | `stream()` | `generate()` |
|
|
119
149
|
| ------------------ | ---------------------------- | ------------------------------------------------ |
|
|
@@ -125,11 +155,11 @@ if (output.finishReason === 'suspended') {
|
|
|
125
155
|
|
|
126
156
|
## Tool-level approval
|
|
127
157
|
|
|
128
|
-
|
|
158
|
+
Instead of pausing every tool call at the agent level, you can mark individual tools as requiring approval. This gives you granular control — only specific tools pause, while others execute immediately.
|
|
129
159
|
|
|
130
|
-
###
|
|
160
|
+
### Approval using `requireApproval`
|
|
131
161
|
|
|
132
|
-
|
|
162
|
+
Set `requireApproval: true` on a tool definition. The tool pauses before execution regardless of whether `requireToolApproval` is set on the agent:
|
|
133
163
|
|
|
134
164
|
```typescript
|
|
135
165
|
export const testTool = createTool({
|
|
@@ -154,30 +184,30 @@ export const testTool = createTool({
|
|
|
154
184
|
})
|
|
155
185
|
```
|
|
156
186
|
|
|
157
|
-
When `requireApproval` is true
|
|
187
|
+
When `requireApproval` is `true`, the stream emits `tool-call-approval` chunks the same way agent-level approval does. Use `approveToolCall()` or `declineToolCall()` to continue:
|
|
158
188
|
|
|
159
189
|
```typescript
|
|
160
190
|
const stream = await agent.stream("What's the weather in London?")
|
|
161
191
|
|
|
162
192
|
for await (const chunk of stream.fullStream) {
|
|
163
193
|
if (chunk.type === 'tool-call-approval') {
|
|
164
|
-
console.log('Approval required.
|
|
194
|
+
console.log('Approval required for:', chunk.payload.toolName)
|
|
165
195
|
}
|
|
166
196
|
}
|
|
167
197
|
|
|
168
|
-
const
|
|
169
|
-
const
|
|
198
|
+
const handleApproval = async () => {
|
|
199
|
+
const approvedStream = await agent.approveToolCall({ runId: stream.runId })
|
|
170
200
|
|
|
171
|
-
for await (const chunk of
|
|
201
|
+
for await (const chunk of approvedStream.textStream) {
|
|
172
202
|
process.stdout.write(chunk)
|
|
173
203
|
}
|
|
174
204
|
process.stdout.write('\n')
|
|
175
205
|
}
|
|
176
206
|
```
|
|
177
207
|
|
|
178
|
-
###
|
|
208
|
+
### Approval using `suspend`
|
|
179
209
|
|
|
180
|
-
With this approach, neither the agent nor the tool uses `requireApproval`. Instead, the tool
|
|
210
|
+
With this approach, neither the agent nor the tool uses `requireApproval`. Instead, the tool's `execute` function calls `suspend` to pause at a specific point and return context or confirmation prompts to the user. This is useful when approval depends on runtime conditions rather than being unconditional.
|
|
181
211
|
|
|
182
212
|
```typescript
|
|
183
213
|
export const testToolB = createTool({
|
|
@@ -210,7 +240,7 @@ export const testToolB = createTool({
|
|
|
210
240
|
})
|
|
211
241
|
```
|
|
212
242
|
|
|
213
|
-
With this approach the stream
|
|
243
|
+
With this approach the stream includes a `tool-call-suspended` chunk, and the `suspendPayload` contains the `reason` defined by the tool's `suspendSchema`. Call `resumeStream` with the `resumeSchema` data and `runId` to continue:
|
|
214
244
|
|
|
215
245
|
```typescript
|
|
216
246
|
const stream = await agent.stream("What's the weather in London?")
|
|
@@ -349,7 +379,7 @@ User: "San Francisco"
|
|
|
349
379
|
Agent: "The weather in San Francisco is: San Francisco: ☀️ +72°F"
|
|
350
380
|
```
|
|
351
381
|
|
|
352
|
-
The second message automatically resumes the suspended tool
|
|
382
|
+
The second message automatically resumes the suspended tool, the agent extracts `{ city: "San Francisco" }` from the user's message and passes it as `resumeData`.
|
|
353
383
|
|
|
354
384
|
### Requirements
|
|
355
385
|
|
|
@@ -370,7 +400,7 @@ Both approaches work with the same tool definitions. Automatic resumption trigge
|
|
|
370
400
|
|
|
371
401
|
## Tool approval: Supervisor pattern
|
|
372
402
|
|
|
373
|
-
The [supervisor pattern](https://mastra.ai/docs/agents/networks) lets a supervisor agent coordinate multiple subagents using `.stream()` or `.generate()`. The supervisor delegates tasks to subagents, which may use tools that require approval. When this happens, tool approvals properly propagate through the delegation chain
|
|
403
|
+
The [supervisor pattern](https://mastra.ai/docs/agents/networks) lets a supervisor agent coordinate multiple subagents using `.stream()` or `.generate()`. The supervisor delegates tasks to subagents, which may use tools that require approval. When this happens, tool approvals properly propagate through the delegation chain — the approval request surfaces at the supervisor level where you can handle it, regardless of which subagent triggered it.
|
|
374
404
|
|
|
375
405
|
### How it works
|
|
376
406
|
|
|
@@ -453,7 +483,7 @@ for await (const chunk of stream.fullStream) {
|
|
|
453
483
|
|
|
454
484
|
### Declining tool calls in supervisor pattern
|
|
455
485
|
|
|
456
|
-
|
|
486
|
+
Decline tool calls at the supervisor level by calling `declineToolCall`. The supervisor responds acknowledging the declined tool without executing it:
|
|
457
487
|
|
|
458
488
|
```typescript
|
|
459
489
|
for await (const chunk of stream.fullStream) {
|
|
@@ -466,7 +496,7 @@ for await (const chunk of stream.fullStream) {
|
|
|
466
496
|
toolCallId: chunk.payload.toolCallId,
|
|
467
497
|
})
|
|
468
498
|
|
|
469
|
-
// The supervisor
|
|
499
|
+
// The supervisor responds acknowledging the declined tool
|
|
470
500
|
for await (const declineChunk of declineStream.textStream) {
|
|
471
501
|
process.stdout.write(declineChunk)
|
|
472
502
|
}
|
|
@@ -476,7 +506,7 @@ for await (const chunk of stream.fullStream) {
|
|
|
476
506
|
|
|
477
507
|
### Using suspend() in supervisor pattern
|
|
478
508
|
|
|
479
|
-
Tools can also use [`suspend()`](#
|
|
509
|
+
Tools can also use [`suspend()`](#approval-using-suspend) to pause execution and return context to the user. This approach works through the supervisor delegation chain the same way `requireApproval` does — the suspension surfaces at the supervisor level:
|
|
480
510
|
|
|
481
511
|
```typescript
|
|
482
512
|
const conditionalTool = createTool({
|
|
@@ -192,7 +192,7 @@ const stream = await supervisor.stream('Research AI trends', {
|
|
|
192
192
|
})
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
-
Return `{ continue: true }` to keep iterating, or `{ continue: false }` to stop. Include optional `feedback` to
|
|
195
|
+
Return `{ continue: true }` to keep iterating, or `{ continue: false }` to stop. Include optional `feedback` to inject guidance into the conversation. When `feedback` is combined with `continue: false`, the model may get one final turn to produce a text response incorporating the feedback, but only if the current iteration is still active (e.g., after tool calls) — otherwise no extra turn is granted.
|
|
196
196
|
|
|
197
197
|
## Memory isolation
|
|
198
198
|
|
|
@@ -170,6 +170,15 @@ const memory = new Memory({
|
|
|
170
170
|
})
|
|
171
171
|
```
|
|
172
172
|
|
|
173
|
+
### Token counting cache
|
|
174
|
+
|
|
175
|
+
OM caches tiktoken part estimates in message metadata to reduce repeat counting work during threshold checks and buffering decisions.
|
|
176
|
+
|
|
177
|
+
- Per-part estimates are stored on `part.providerMetadata.mastra` and reused on subsequent passes when the cache version/tokenizer source matches.
|
|
178
|
+
- For string-only message content (without parts), OM uses a message-level metadata fallback cache.
|
|
179
|
+
- Message and conversation overhead are still recalculated on every pass. The cache only stores payload estimates, so counting semantics stay the same.
|
|
180
|
+
- `data-*` and `reasoning` parts are still skipped and are not cached.
|
|
181
|
+
|
|
173
182
|
## Async Buffering
|
|
174
183
|
|
|
175
184
|
Without async buffering, the Observer runs synchronously when the message threshold is reached — the agent pauses mid-conversation while the Observer LLM call completes. With async buffering (enabled by default), observations are pre-computed in the background as the conversation grows. When the threshold is hit, buffered observations activate instantly with no pause.
|
|
@@ -147,8 +147,24 @@ Supported embedding models:
|
|
|
147
147
|
|
|
148
148
|
- **OpenAI**: `text-embedding-3-small`, `text-embedding-3-large`, `text-embedding-ada-002`
|
|
149
149
|
- **Google**: `gemini-embedding-001`
|
|
150
|
+
- **OpenRouter**: Access embedding models from various providers
|
|
150
151
|
|
|
151
|
-
|
|
152
|
+
```ts
|
|
153
|
+
import { Agent } from '@mastra/core/agent'
|
|
154
|
+
import { Memory } from '@mastra/memory'
|
|
155
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
156
|
+
|
|
157
|
+
const agent = new Agent({
|
|
158
|
+
memory: new Memory({
|
|
159
|
+
embedder: new ModelRouterEmbeddingModel({
|
|
160
|
+
providerId: 'openrouter',
|
|
161
|
+
modelId: 'openai/text-embedding-3-small',
|
|
162
|
+
}),
|
|
163
|
+
}),
|
|
164
|
+
})
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
The model router automatically handles API key detection from environment variables (`OPENAI_API_KEY`, `GOOGLE_GENERATIVE_AI_API_KEY`, `OPENROUTER_API_KEY`).
|
|
152
168
|
|
|
153
169
|
### Using AI SDK Packages
|
|
154
170
|
|
|
@@ -16,11 +16,11 @@ const thread = await memory.createThread({
|
|
|
16
16
|
|
|
17
17
|
## Parameters
|
|
18
18
|
|
|
19
|
-
**key
|
|
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
20
|
|
|
21
21
|
## Returns
|
|
22
22
|
|
|
23
|
-
**memory
|
|
23
|
+
**memory** (`TMemory[TMemoryKey]`): The memory instance with the specified key. Throws an error if the memory is not found.
|
|
24
24
|
|
|
25
25
|
## Example: Registering and Retrieving Memory
|
|
26
26
|
|
|
@@ -18,7 +18,7 @@ This method takes no parameters.
|
|
|
18
18
|
|
|
19
19
|
## Returns
|
|
20
20
|
|
|
21
|
-
**memory
|
|
21
|
+
**memory** (`Record<string, MastraMemory>`): An object containing all registered memory instances, keyed by their registry keys.
|
|
22
22
|
|
|
23
23
|
## Example: Checking Registered Memory
|
|
24
24
|
|
|
@@ -14,7 +14,7 @@ const isClonedThread = memory.isClone(thread)
|
|
|
14
14
|
|
|
15
15
|
### Parameters
|
|
16
16
|
|
|
17
|
-
**thread
|
|
17
|
+
**thread** (`StorageThreadType`): The thread object to check.
|
|
18
18
|
|
|
19
19
|
### Example
|
|
20
20
|
|
|
@@ -40,7 +40,7 @@ const metadata = memory.getCloneMetadata(thread)
|
|
|
40
40
|
|
|
41
41
|
### Parameters
|
|
42
42
|
|
|
43
|
-
**thread
|
|
43
|
+
**thread** (`StorageThreadType`): The thread object to extract clone metadata from.
|
|
44
44
|
|
|
45
45
|
### Example
|
|
46
46
|
|
|
@@ -66,7 +66,7 @@ const sourceThread = await memory.getSourceThread(threadId)
|
|
|
66
66
|
|
|
67
67
|
### Parameters
|
|
68
68
|
|
|
69
|
-
**threadId
|
|
69
|
+
**threadId** (`string`): The ID of the cloned thread.
|
|
70
70
|
|
|
71
71
|
### Example
|
|
72
72
|
|
|
@@ -91,7 +91,7 @@ const clones = await memory.listClones(sourceThreadId)
|
|
|
91
91
|
|
|
92
92
|
### Parameters
|
|
93
93
|
|
|
94
|
-
**sourceThreadId
|
|
94
|
+
**sourceThreadId** (`string`): The ID of the source thread to find clones for.
|
|
95
95
|
|
|
96
96
|
### Example
|
|
97
97
|
|
|
@@ -116,7 +116,7 @@ const history = await memory.getCloneHistory(threadId)
|
|
|
116
116
|
|
|
117
117
|
### Parameters
|
|
118
118
|
|
|
119
|
-
**threadId
|
|
119
|
+
**threadId** (`string`): The ID of the thread to get clone history for.
|
|
120
120
|
|
|
121
121
|
### Example
|
|
122
122
|
|
|
@@ -12,49 +12,45 @@ const { thread, clonedMessages } = await memory.cloneThread({
|
|
|
12
12
|
|
|
13
13
|
## Parameters
|
|
14
14
|
|
|
15
|
-
**sourceThreadId
|
|
15
|
+
**sourceThreadId** (`string`): The ID of the thread to clone
|
|
16
16
|
|
|
17
|
-
**newThreadId
|
|
17
|
+
**newThreadId** (`string`): Optional custom ID for the cloned thread. If not provided, one will be generated.
|
|
18
18
|
|
|
19
|
-
**resourceId
|
|
19
|
+
**resourceId** (`string`): Optional resource ID for the cloned thread. Defaults to the source thread's resourceId.
|
|
20
20
|
|
|
21
|
-
**title
|
|
21
|
+
**title** (`string`): Optional title for the cloned thread. Defaults to '\[source title] (Copy)'.
|
|
22
22
|
|
|
23
|
-
**metadata
|
|
23
|
+
**metadata** (`Record<string, unknown>`): Optional metadata to merge with the source thread's metadata. Clone metadata is automatically added.
|
|
24
24
|
|
|
25
|
-
**options
|
|
25
|
+
**options** (`CloneOptions`): Optional filtering options for the clone operation.
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
**options.messageLimit** (`number`): Maximum number of messages to clone. When set, clones the most recent N messages.
|
|
28
28
|
|
|
29
|
-
**
|
|
29
|
+
**options.messageFilter** (`MessageFilter`): Filter criteria for selecting which messages to clone.
|
|
30
30
|
|
|
31
|
-
**messageFilter
|
|
31
|
+
**options.messageFilter.startDate** (`Date`): Only clone messages created on or after this date.
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
**options.messageFilter.endDate** (`Date`): Only clone messages created on or before this date.
|
|
34
34
|
|
|
35
|
-
**
|
|
36
|
-
|
|
37
|
-
**endDate?:** (`Date`): Only clone messages created on or before this date.
|
|
38
|
-
|
|
39
|
-
**messageIds?:** (`string[]`): Only clone messages with these specific IDs.
|
|
35
|
+
**options.messageFilter.messageIds** (`string[]`): Only clone messages with these specific IDs.
|
|
40
36
|
|
|
41
37
|
## Returns
|
|
42
38
|
|
|
43
|
-
**thread
|
|
39
|
+
**thread** (`StorageThreadType`): The newly created cloned thread with clone metadata.
|
|
44
40
|
|
|
45
|
-
**clonedMessages
|
|
41
|
+
**clonedMessages** (`MastraDBMessage[]`): Array of the cloned messages with new IDs assigned to the new thread.
|
|
46
42
|
|
|
47
|
-
**messageIdMap
|
|
43
|
+
**messageIdMap** (`Record<string, string>`): A mapping from source message IDs to their corresponding cloned message IDs.
|
|
48
44
|
|
|
49
45
|
### Clone Metadata
|
|
50
46
|
|
|
51
47
|
The cloned thread's metadata includes a `clone` property with:
|
|
52
48
|
|
|
53
|
-
**sourceThreadId
|
|
49
|
+
**sourceThreadId** (`string`): The ID of the original thread that was cloned.
|
|
54
50
|
|
|
55
|
-
**clonedAt
|
|
51
|
+
**clonedAt** (`Date`): Timestamp when the clone was created.
|
|
56
52
|
|
|
57
|
-
**lastMessageId
|
|
53
|
+
**lastMessageId** (`string`): The ID of the last message in the source thread at the time of cloning.
|
|
58
54
|
|
|
59
55
|
## Extended Usage Example
|
|
60
56
|
|
|
@@ -10,27 +10,27 @@ await memory?.createThread({ resourceId: 'user-123' })
|
|
|
10
10
|
|
|
11
11
|
## Parameters
|
|
12
12
|
|
|
13
|
-
**resourceId
|
|
13
|
+
**resourceId** (`string`): Identifier for the resource this thread belongs to (e.g., user ID, project ID)
|
|
14
14
|
|
|
15
|
-
**threadId
|
|
15
|
+
**threadId** (`string`): Optional custom ID for the thread. If not provided, one will be generated.
|
|
16
16
|
|
|
17
|
-
**title
|
|
17
|
+
**title** (`string`): Optional title for the thread
|
|
18
18
|
|
|
19
|
-
**metadata
|
|
19
|
+
**metadata** (`Record<string, unknown>`): Optional metadata to associate with the thread
|
|
20
20
|
|
|
21
21
|
## Returns
|
|
22
22
|
|
|
23
|
-
**id
|
|
23
|
+
**id** (`string`): Unique identifier of the created thread
|
|
24
24
|
|
|
25
|
-
**resourceId
|
|
25
|
+
**resourceId** (`string`): Resource ID associated with the thread
|
|
26
26
|
|
|
27
|
-
**title
|
|
27
|
+
**title** (`string`): Title of the thread (if provided)
|
|
28
28
|
|
|
29
|
-
**createdAt
|
|
29
|
+
**createdAt** (`Date`): Timestamp when the thread was created
|
|
30
30
|
|
|
31
|
-
**updatedAt
|
|
31
|
+
**updatedAt** (`Date`): Timestamp when the thread was last updated
|
|
32
32
|
|
|
33
|
-
**metadata
|
|
33
|
+
**metadata** (`Record<string, unknown>`): Additional metadata associated with the thread
|
|
34
34
|
|
|
35
35
|
## Extended usage example
|
|
36
36
|
|
|
@@ -10,11 +10,11 @@ await memory?.getThreadById({ threadId: 'thread-123' })
|
|
|
10
10
|
|
|
11
11
|
## Parameters
|
|
12
12
|
|
|
13
|
-
**threadId
|
|
13
|
+
**threadId** (`string`): The ID of the thread to be retrieved.
|
|
14
14
|
|
|
15
15
|
## Returns
|
|
16
16
|
|
|
17
|
-
**thread
|
|
17
|
+
**thread** (`Promise<StorageThreadType | null>`): A promise that resolves to the thread associated with the given ID, or null if not found.
|
|
18
18
|
|
|
19
19
|
### Related
|
|
20
20
|
|
|
@@ -61,17 +61,17 @@ const result = await memory.listThreads({
|
|
|
61
61
|
|
|
62
62
|
## Parameters
|
|
63
63
|
|
|
64
|
-
**filter
|
|
64
|
+
**filter** (`{ resourceId?: string; metadata?: Record<string, unknown> }`): Optional filter object. resourceId filters threads by resource ID. metadata filters threads by metadata key-value pairs (AND logic - all must match)
|
|
65
65
|
|
|
66
|
-
**page
|
|
66
|
+
**page** (`number`): Page number (0-indexed) to retrieve
|
|
67
67
|
|
|
68
|
-
**perPage
|
|
68
|
+
**perPage** (`number | false`): Maximum number of threads to return per page, or false to fetch all
|
|
69
69
|
|
|
70
|
-
**orderBy
|
|
70
|
+
**orderBy** (`{ field: 'createdAt' | 'updatedAt', direction: 'ASC' | 'DESC' }`): Sort configuration with field and direction (defaults to { field: 'createdAt', direction: 'DESC' })
|
|
71
71
|
|
|
72
72
|
## Returns
|
|
73
73
|
|
|
74
|
-
**result
|
|
74
|
+
**result** (`Promise<StorageListThreadsOutput>`): A promise that resolves to paginated thread results with metadata
|
|
75
75
|
|
|
76
76
|
The return object contains:
|
|
77
77
|
|