@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.
Files changed (58) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/dist/{chunk-5UYAHJVJ.cjs → chunk-D6II7EP4.cjs} +660 -531
  3. package/dist/chunk-D6II7EP4.cjs.map +1 -0
  4. package/dist/{chunk-A62BQK35.js → chunk-GBBQIJQF.js} +660 -531
  5. package/dist/chunk-GBBQIJQF.js.map +1 -0
  6. package/dist/docs/SKILL.md +1 -1
  7. package/dist/docs/assets/SOURCE_MAP.json +25 -25
  8. package/dist/docs/references/docs-agents-agent-approval.md +61 -31
  9. package/dist/docs/references/docs-agents-supervisor-agents.md +1 -1
  10. package/dist/docs/references/docs-memory-observational-memory.md +9 -0
  11. package/dist/docs/references/docs-memory-semantic-recall.md +17 -1
  12. package/dist/docs/references/reference-core-getMemory.md +2 -2
  13. package/dist/docs/references/reference-core-listMemory.md +1 -1
  14. package/dist/docs/references/reference-memory-clone-utilities.md +5 -5
  15. package/dist/docs/references/reference-memory-cloneThread.md +17 -21
  16. package/dist/docs/references/reference-memory-createThread.md +10 -10
  17. package/dist/docs/references/reference-memory-getThreadById.md +2 -2
  18. package/dist/docs/references/reference-memory-listThreads.md +5 -5
  19. package/dist/docs/references/reference-memory-memory-class.md +12 -14
  20. package/dist/docs/references/reference-memory-observational-memory.md +102 -94
  21. package/dist/docs/references/reference-processors-token-limiter-processor.md +11 -13
  22. package/dist/docs/references/reference-storage-dynamodb.md +9 -9
  23. package/dist/docs/references/reference-storage-libsql.md +2 -2
  24. package/dist/docs/references/reference-storage-mongodb.md +5 -5
  25. package/dist/docs/references/reference-storage-postgresql.md +25 -25
  26. package/dist/docs/references/reference-storage-upstash.md +3 -3
  27. package/dist/docs/references/reference-vectors-libsql.md +31 -31
  28. package/dist/docs/references/reference-vectors-mongodb.md +32 -32
  29. package/dist/docs/references/reference-vectors-pg.md +60 -44
  30. package/dist/docs/references/reference-vectors-upstash.md +25 -25
  31. package/dist/index.cjs +246 -57
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.d.ts +19 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +246 -57
  36. package/dist/index.js.map +1 -1
  37. package/dist/{observational-memory-MXI54VC7.cjs → observational-memory-AHVELJX4.cjs} +17 -17
  38. package/dist/{observational-memory-MXI54VC7.cjs.map → observational-memory-AHVELJX4.cjs.map} +1 -1
  39. package/dist/{observational-memory-SR6G4HN5.js → observational-memory-QFQUF5EY.js} +3 -3
  40. package/dist/{observational-memory-SR6G4HN5.js.map → observational-memory-QFQUF5EY.js.map} +1 -1
  41. package/dist/processors/index.cjs +15 -15
  42. package/dist/processors/index.js +1 -1
  43. package/dist/processors/observational-memory/date-utils.d.ts +35 -0
  44. package/dist/processors/observational-memory/date-utils.d.ts.map +1 -0
  45. package/dist/processors/observational-memory/markers.d.ts +94 -0
  46. package/dist/processors/observational-memory/markers.d.ts.map +1 -0
  47. package/dist/processors/observational-memory/observational-memory.d.ts +0 -76
  48. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  49. package/dist/processors/observational-memory/operation-registry.d.ts +14 -0
  50. package/dist/processors/observational-memory/operation-registry.d.ts.map +1 -0
  51. package/dist/processors/observational-memory/thresholds.d.ts +52 -0
  52. package/dist/processors/observational-memory/thresholds.d.ts.map +1 -0
  53. package/dist/processors/observational-memory/token-counter.d.ts +4 -0
  54. package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
  55. package/dist/tools/working-memory.d.ts.map +1 -1
  56. package/package.json +7 -7
  57. package/dist/chunk-5UYAHJVJ.cjs.map +0 -1
  58. package/dist/chunk-A62BQK35.js.map +0 -1
@@ -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.6.0"
6
+ version: "1.6.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,71 +1,71 @@
1
1
  {
2
- "version": "1.6.0",
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-A62BQK35.js"
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-A62BQK35.js"
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-A62BQK35.js"
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-A62BQK35.js"
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-A62BQK35.js"
23
+ "implementation": "dist/chunk-GBBQIJQF.js"
24
24
  },
25
25
  "ObservationalMemory": {
26
26
  "types": "dist/processors/index.d.ts",
27
- "implementation": "dist/chunk-A62BQK35.js",
28
- "line": 1258
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-A62BQK35.js",
33
- "line": 919
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-A62BQK35.js",
38
- "line": 533
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-A62BQK35.js",
43
- "line": 281
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-A62BQK35.js",
48
- "line": 662
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-A62BQK35.js",
53
- "line": 376
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-A62BQK35.js",
58
- "line": 650
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-A62BQK35.js",
63
- "line": 673
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-A62BQK35.js",
68
- "line": 564
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-A62BQK35.js"
99
+ "chunk-GBBQIJQF.js"
100
100
  ]
101
101
  }
102
102
  }
@@ -1,10 +1,29 @@
1
- # Agent Approval
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 performing running long processes. With agent approval you can suspend a tool call and provide feedback to the user, or approve or decline a tool call based on targeted application conditions.
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
- ## Tool call approval
5
+ ## How approval works
6
6
 
7
- Tool call approval can be enabled at the agent level and apply to every tool the agent uses, or at the tool level providing more granular control over individual tool calls.
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
- When calling an agent using `.stream()` set `requireToolApproval` to `true` which will prevent the agent from calling any of the tools defined in its configuration.
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
- To approve a tool call, access `approveToolCall` from the `agent`, passing in the `runId` of the stream. This will let the agent know its now OK to call its tools.
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
- To decline a tool call, access the `declineToolCall` from the `agent`. You will see the streamed response from the agent, but it won't call its tools.
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 using `generate()` with `requireToolApproval: true`, the method returns immediately when a tool requires approval instead of executing it.
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'` - indicates the agent is waiting for approval
74
- - `suspendPayload` - contains tool call details (`toolCallId`, `toolName`, `args`)
75
- - `runId` - needed to approve or decline the tool call
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
- To approve a tool call with `generate()`, use the `approveToolCallGenerate` method:
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
- To decline a tool call, use the `declineToolCallGenerate` method:
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 will respond acknowledging the declined tool
141
+ // Agent responds acknowledging the declined tool
112
142
  console.log(result.text)
113
143
  }
114
144
  ```
115
145
 
116
- ### Stream vs Generate comparison
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
- There are two types of tool call approval. The first uses `requireApproval`, which is a property on the tool definition, while `requireToolApproval` is a parameter passed to `agent.stream()`. The second uses `suspend` and lets the agent provide context or confirmation prompts so the user can decide whether the tool call should continue.
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
- ### Tool approval using `requireToolApproval`
160
+ ### Approval using `requireApproval`
131
161
 
132
- In this approach, `requireApproval` is configured on the tool definition (shown below) rather than on the agent.
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 for a tool, the stream will include chunks of type `tool-call-approval` to indicate that the call is paused. To continue the call, invoke `resumeStream` with the required `resumeSchema` and the `runId`.
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 handleResume = async () => {
169
- const resumedStream = await agent.resumeStream({ approved: true }, { runId: stream.runId })
198
+ const handleApproval = async () => {
199
+ const approvedStream = await agent.approveToolCall({ runId: stream.runId })
170
200
 
171
- for await (const chunk of resumedStream.textStream) {
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
- ### Tool approval using `suspend`
208
+ ### Approval using `suspend`
179
209
 
180
- With this approach, neither the agent nor the tool uses `requireApproval`. Instead, the tool implementation calls `suspend` to pause execution and return context or confirmation prompts to the user.
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 will include a `tool-call-suspended` chunk, and the `suspendPayload` will contain the `reason` defined by the tool's `suspendSchema`. To continue the call, invoke `resumeStream` with the required `resumeSchema` and the `runId`.
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 - the agent extracts `{ city: "San Francisco" }` from the user's message and passes it as `resumeData`.
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 -- the approval request surfaces at the supervisor level where you can handle it, regardless of which subagent triggered it.
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
- You can also decline tool calls at the supervisor level by calling `declineToolCall`. The supervisor will respond acknowledging the declined tool without executing it:
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 will respond acknowledging the declined tool
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()`](#tool-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:
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 add guidance that's visible to the next iteration.
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
- The model router automatically handles API key detection from environment variables (`OPENAI_API_KEY`, `GOOGLE_GENERATIVE_AI_API_KEY`).
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:** (`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.
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:** (`TMemory[TMemoryKey]`): The memory instance with the specified key. Throws an error if the memory is not found.
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:** (`Record<string, MastraMemory>`): An object containing all registered memory instances, keyed by their registry keys.
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:** (`StorageThreadType`): The thread object to check.
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:** (`StorageThreadType`): The thread object to extract clone metadata from.
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:** (`string`): The ID of the cloned thread.
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:** (`string`): The ID of the source thread to find clones for.
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:** (`string`): The ID of the thread to get clone history for.
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:** (`string`): The ID of the thread to clone
15
+ **sourceThreadId** (`string`): The ID of the thread to clone
16
16
 
17
- **newThreadId?:** (`string`): Optional custom ID for the cloned thread. If not provided, one will be generated.
17
+ **newThreadId** (`string`): Optional custom ID for the cloned thread. If not provided, one will be generated.
18
18
 
19
- **resourceId?:** (`string`): Optional resource ID for the cloned thread. Defaults to the source thread's resourceId.
19
+ **resourceId** (`string`): Optional resource ID for the cloned thread. Defaults to the source thread's resourceId.
20
20
 
21
- **title?:** (`string`): Optional title for the cloned thread. Defaults to '\[source title] (Copy)'.
21
+ **title** (`string`): Optional title for the cloned thread. Defaults to '\[source title] (Copy)'.
22
22
 
23
- **metadata?:** (`Record<string, unknown>`): Optional metadata to merge with the source thread's metadata. Clone metadata is automatically added.
23
+ **metadata** (`Record<string, unknown>`): Optional metadata to merge with the source thread's metadata. Clone metadata is automatically added.
24
24
 
25
- **options?:** (`CloneOptions`): Optional filtering options for the clone operation.
25
+ **options** (`CloneOptions`): Optional filtering options for the clone operation.
26
26
 
27
- ### Options Parameters
27
+ **options.messageLimit** (`number`): Maximum number of messages to clone. When set, clones the most recent N messages.
28
28
 
29
- **messageLimit?:** (`number`): Maximum number of messages to clone. When set, clones the most recent N messages.
29
+ **options.messageFilter** (`MessageFilter`): Filter criteria for selecting which messages to clone.
30
30
 
31
- **messageFilter?:** (`MessageFilter`): Filter criteria for selecting which messages to clone.
31
+ **options.messageFilter.startDate** (`Date`): Only clone messages created on or after this date.
32
32
 
33
- ### MessageFilter Parameters
33
+ **options.messageFilter.endDate** (`Date`): Only clone messages created on or before this date.
34
34
 
35
- **startDate?:** (`Date`): Only clone messages created on or after this date.
36
-
37
- **endDate?:** (`Date`): Only clone messages created on or before this date.
38
-
39
- **messageIds?:** (`string[]`): Only clone messages with these specific IDs.
35
+ **options.messageFilter.messageIds** (`string[]`): Only clone messages with these specific IDs.
40
36
 
41
37
  ## Returns
42
38
 
43
- **thread:** (`StorageThreadType`): The newly created cloned thread with clone metadata.
39
+ **thread** (`StorageThreadType`): The newly created cloned thread with clone metadata.
44
40
 
45
- **clonedMessages:** (`MastraDBMessage[]`): Array of the cloned messages with new IDs assigned to the new thread.
41
+ **clonedMessages** (`MastraDBMessage[]`): Array of the cloned messages with new IDs assigned to the new thread.
46
42
 
47
- **messageIdMap?:** (`Record<string, string>`): A mapping from source message IDs to their corresponding cloned message IDs.
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:** (`string`): The ID of the original thread that was cloned.
49
+ **sourceThreadId** (`string`): The ID of the original thread that was cloned.
54
50
 
55
- **clonedAt:** (`Date`): Timestamp when the clone was created.
51
+ **clonedAt** (`Date`): Timestamp when the clone was created.
56
52
 
57
- **lastMessageId?:** (`string`): The ID of the last message in the source thread at the time of cloning.
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:** (`string`): Identifier for the resource this thread belongs to (e.g., user ID, project ID)
13
+ **resourceId** (`string`): Identifier for the resource this thread belongs to (e.g., user ID, project ID)
14
14
 
15
- **threadId?:** (`string`): Optional custom ID for the thread. If not provided, one will be generated.
15
+ **threadId** (`string`): Optional custom ID for the thread. If not provided, one will be generated.
16
16
 
17
- **title?:** (`string`): Optional title for the thread
17
+ **title** (`string`): Optional title for the thread
18
18
 
19
- **metadata?:** (`Record<string, unknown>`): Optional metadata to associate with the thread
19
+ **metadata** (`Record<string, unknown>`): Optional metadata to associate with the thread
20
20
 
21
21
  ## Returns
22
22
 
23
- **id:** (`string`): Unique identifier of the created thread
23
+ **id** (`string`): Unique identifier of the created thread
24
24
 
25
- **resourceId:** (`string`): Resource ID associated with the thread
25
+ **resourceId** (`string`): Resource ID associated with the thread
26
26
 
27
- **title:** (`string`): Title of the thread (if provided)
27
+ **title** (`string`): Title of the thread (if provided)
28
28
 
29
- **createdAt:** (`Date`): Timestamp when the thread was created
29
+ **createdAt** (`Date`): Timestamp when the thread was created
30
30
 
31
- **updatedAt:** (`Date`): Timestamp when the thread was last updated
31
+ **updatedAt** (`Date`): Timestamp when the thread was last updated
32
32
 
33
- **metadata:** (`Record<string, unknown>`): Additional metadata associated with the thread
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:** (`string`): The ID of the thread to be retrieved.
13
+ **threadId** (`string`): The ID of the thread to be retrieved.
14
14
 
15
15
  ## Returns
16
16
 
17
- **thread:** (`Promise<StorageThreadType | null>`): A promise that resolves to the thread associated with the given ID, or null if not found.
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?:** (`{ 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)
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?:** (`number`): Page number (0-indexed) to retrieve
66
+ **page** (`number`): Page number (0-indexed) to retrieve
67
67
 
68
- **perPage?:** (`number | false`): Maximum number of threads to return per page, or false to fetch all
68
+ **perPage** (`number | false`): Maximum number of threads to return per page, or false to fetch all
69
69
 
70
- **orderBy?:** (`{ field: 'createdAt' | 'updatedAt', direction: 'ASC' | 'DESC' }`): Sort configuration with field and direction (defaults to { field: 'createdAt', direction: 'DESC' })
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:** (`Promise<StorageListThreadsOutput>`): A promise that resolves to paginated thread results with metadata
74
+ **result** (`Promise<StorageListThreadsOutput>`): A promise that resolves to paginated thread results with metadata
75
75
 
76
76
  The return object contains:
77
77