@mastra/memory 1.21.1-alpha.0 → 1.21.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 CHANGED
@@ -1,5 +1,29 @@
1
1
  # @mastra/memory
2
2
 
3
+ ## 1.21.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed direct memory recall so semantic recall score thresholds filter recalled messages. ([#18211](https://github.com/mastra-ai/mastra/pull/18211))
8
+
9
+ - Fixed semantic recall failing on long unbroken content. When a memory-enabled agent received a message containing a single very long whitespace-free string — a base64 data URI, a minified JS/JSON blob, a long URL, or spaceless CJK text — embedding could throw a provider "maximum context length" error and break that turn's persistence or recall. ([#18236](https://github.com/mastra-ai/mastra/pull/18236))
10
+
11
+ `chunkText` now hard-splits any single word that is longer than the chunk budget so every chunk stays under the embedder's token limit, and it no longer emits an empty leading chunk when the first word is oversized.
12
+
13
+ - Updated dependencies [[`5bd72d2`](https://github.com/mastra-ai/mastra/commit/5bd72d255f45b5ea8ab342643bd463814a980a24), [`1cc9ee1`](https://github.com/mastra-ai/mastra/commit/1cc9ee1ba51db53020a735626d33017a60b4b5b3), [`417baae`](https://github.com/mastra-ai/mastra/commit/417baae40b995db5819c845036947f0c27dc1c00), [`65f255a`](https://github.com/mastra-ai/mastra/commit/65f255a38667beb6ceeadabfa9eb5059bfec8298), [`74955f9`](https://github.com/mastra-ai/mastra/commit/74955f9120cde8b1d8ce4399232b4033236be858), [`30ebaf0`](https://github.com/mastra-ai/mastra/commit/30ebaf07bed5f4d30f2f257836c15d1bf7e40aae), [`5704634`](https://github.com/mastra-ai/mastra/commit/5704634b22133167dea337a942a34f57aaa3fa14), [`5c4e9a4`](https://github.com/mastra-ai/mastra/commit/5c4e9a4cfb2216bb3ea7f8988ad3727f3b92bb3a), [`4a88c6e`](https://github.com/mastra-ai/mastra/commit/4a88c6e2bdce316f8d7551b4ec3449b0b06fc71c), [`417baae`](https://github.com/mastra-ai/mastra/commit/417baae40b995db5819c845036947f0c27dc1c00), [`74955f9`](https://github.com/mastra-ai/mastra/commit/74955f9120cde8b1d8ce4399232b4033236be858), [`74955f9`](https://github.com/mastra-ai/mastra/commit/74955f9120cde8b1d8ce4399232b4033236be858), [`25961e3`](https://github.com/mastra-ai/mastra/commit/25961e3260ff3b1464637af8fcdb36210551c39f), [`6a1428a`](https://github.com/mastra-ai/mastra/commit/6a1428a23133fc070fc6c1caa08d28f3ba4fe5ff), [`87a17ef`](https://github.com/mastra-ai/mastra/commit/87a17efbd725aca6639febdc5e69e2abb3048689), [`e11ff30`](https://github.com/mastra-ai/mastra/commit/e11ff301408bf1731dca2fb7fbfcd8c819500a35), [`7794d71`](https://github.com/mastra-ai/mastra/commit/7794d71872c68733a30e028dfb7b1705daf6c5d2), [`9d2c946`](https://github.com/mastra-ai/mastra/commit/9d2c946d0859e90ae4bcec5beeb1da7398d2ad1e), [`c0eda2b`](https://github.com/mastra-ai/mastra/commit/c0eda2bcd91a228427314b12c91d8b147f3a739f), [`7b29f33`](https://github.com/mastra-ai/mastra/commit/7b29f332a357a83e555f29e718e5f2fab9979943), [`c0eda2b`](https://github.com/mastra-ai/mastra/commit/c0eda2bcd91a228427314b12c91d8b147f3a739f), [`b13925b`](https://github.com/mastra-ai/mastra/commit/b13925bfa91aa8700f56fa54a9ce707ee7e4ba62), [`f1ec385`](https://github.com/mastra-ai/mastra/commit/f1ec385386f62b1a0847ec5353ae2bb169d1c3d9), [`e14986f`](https://github.com/mastra-ai/mastra/commit/e14986f6e5478d6384d04ff9a7f9a79a46a8b529), [`24912b1`](https://github.com/mastra-ai/mastra/commit/24912b1f855d29ec36af4ef4bde1f7417e20cdf5), [`bf94ec6`](https://github.com/mastra-ai/mastra/commit/bf94ec68192d9f16e46ef7e5ac36370aeeddf35d), [`a29f371`](https://github.com/mastra-ai/mastra/commit/a29f371aef629ac8562661524a497127e93b5131), [`7686216`](https://github.com/mastra-ai/mastra/commit/7686216f37e74568feddec17cef3c3d24e10e60a), [`74955f9`](https://github.com/mastra-ai/mastra/commit/74955f9120cde8b1d8ce4399232b4033236be858), [`073f910`](https://github.com/mastra-ai/mastra/commit/073f910481e7d94b95ba3830f96531774ae95d33), [`0be490f`](https://github.com/mastra-ai/mastra/commit/0be490fabb538c5a7de796ea0aff7d04a0bea1f3), [`0be490f`](https://github.com/mastra-ai/mastra/commit/0be490fabb538c5a7de796ea0aff7d04a0bea1f3), [`ebbe1d3`](https://github.com/mastra-ai/mastra/commit/ebbe1d31a965a3adb0e728758f326b8122b4b55f), [`974f614`](https://github.com/mastra-ai/mastra/commit/974f614e083bd68278536f94453f7b320b86a3c7), [`3818814`](https://github.com/mastra-ai/mastra/commit/38188149ce454c4403fe9fcbdf73b735c68d36be), [`975c59a`](https://github.com/mastra-ai/mastra/commit/975c59ae363ee275fc55062392e1ffd2cbccbd53), [`1f97ce5`](https://github.com/mastra-ai/mastra/commit/1f97ce5695463bebb4eaacf501da6fb403e20885), [`74955f9`](https://github.com/mastra-ai/mastra/commit/74955f9120cde8b1d8ce4399232b4033236be858), [`7f51548`](https://github.com/mastra-ai/mastra/commit/7f515481213780be7047cef00640b9d35f3d545c), [`64f58c0`](https://github.com/mastra-ai/mastra/commit/64f58c04e78b40137497d47f781e897e416f22a5), [`74955f9`](https://github.com/mastra-ai/mastra/commit/74955f9120cde8b1d8ce4399232b4033236be858), [`ebbe1d3`](https://github.com/mastra-ai/mastra/commit/ebbe1d31a965a3adb0e728758f326b8122b4b55f), [`d95f394`](https://github.com/mastra-ai/mastra/commit/d95f394fd24c8411886930d727679c4d5252aa26), [`417baae`](https://github.com/mastra-ai/mastra/commit/417baae40b995db5819c845036947f0c27dc1c00), [`8e25a78`](https://github.com/mastra-ai/mastra/commit/8e25a78e0597575f0b0729bae8c5e190c84869b5), [`417baae`](https://github.com/mastra-ai/mastra/commit/417baae40b995db5819c845036947f0c27dc1c00), [`f3f0c9d`](https://github.com/mastra-ai/mastra/commit/f3f0c9d7c878db5a13177871ce3523a14f14b311), [`a5b22d3`](https://github.com/mastra-ai/mastra/commit/a5b22d314d62a68d801886a8d3d0eb6c089473db), [`31be1cf`](https://github.com/mastra-ai/mastra/commit/31be1cf5f2a7b5eef12f6123a40653b4d8115c16), [`417baae`](https://github.com/mastra-ai/mastra/commit/417baae40b995db5819c845036947f0c27dc1c00), [`74955f9`](https://github.com/mastra-ai/mastra/commit/74955f9120cde8b1d8ce4399232b4033236be858), [`74955f9`](https://github.com/mastra-ai/mastra/commit/74955f9120cde8b1d8ce4399232b4033236be858)]:
14
+ - @mastra/core@1.46.0
15
+
16
+ ## 1.21.1-alpha.1
17
+
18
+ ### Patch Changes
19
+
20
+ - Fixed semantic recall failing on long unbroken content. When a memory-enabled agent received a message containing a single very long whitespace-free string — a base64 data URI, a minified JS/JSON blob, a long URL, or spaceless CJK text — embedding could throw a provider "maximum context length" error and break that turn's persistence or recall. ([#18236](https://github.com/mastra-ai/mastra/pull/18236))
21
+
22
+ `chunkText` now hard-splits any single word that is longer than the chunk budget so every chunk stays under the embedder's token limit, and it no longer emits an empty leading chunk when the first word is oversized.
23
+
24
+ - Updated dependencies [[`65f255a`](https://github.com/mastra-ai/mastra/commit/65f255a38667beb6ceeadabfa9eb5059bfec8298), [`4a88c6e`](https://github.com/mastra-ai/mastra/commit/4a88c6e2bdce316f8d7551b4ec3449b0b06fc71c), [`87a17ef`](https://github.com/mastra-ai/mastra/commit/87a17efbd725aca6639febdc5e69e2abb3048689), [`e11ff30`](https://github.com/mastra-ai/mastra/commit/e11ff301408bf1731dca2fb7fbfcd8c819500a35), [`9d2c946`](https://github.com/mastra-ai/mastra/commit/9d2c946d0859e90ae4bcec5beeb1da7398d2ad1e), [`f1ec385`](https://github.com/mastra-ai/mastra/commit/f1ec385386f62b1a0847ec5353ae2bb169d1c3d9), [`e14986f`](https://github.com/mastra-ai/mastra/commit/e14986f6e5478d6384d04ff9a7f9a79a46a8b529), [`0be490f`](https://github.com/mastra-ai/mastra/commit/0be490fabb538c5a7de796ea0aff7d04a0bea1f3), [`0be490f`](https://github.com/mastra-ai/mastra/commit/0be490fabb538c5a7de796ea0aff7d04a0bea1f3), [`974f614`](https://github.com/mastra-ai/mastra/commit/974f614e083bd68278536f94453f7b320b86a3c7), [`31be1cf`](https://github.com/mastra-ai/mastra/commit/31be1cf5f2a7b5eef12f6123a40653b4d8115c16)]:
25
+ - @mastra/core@1.46.0-alpha.3
26
+
3
27
  ## 1.21.1-alpha.0
4
28
 
5
29
  ### Patch Changes
@@ -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.21.1-alpha.0"
6
+ version: "1.21.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.21.1-alpha.0",
2
+ "version": "1.21.1",
3
3
  "package": "@mastra/memory",
4
4
  "exports": {
5
5
  "ModelByInputTokens": {
@@ -2,7 +2,7 @@
2
2
 
3
3
  **Added in:** `@mastra/core@1.29.0`
4
4
 
5
- Background tasks let an agent dispatch a long-running tool call without blocking the agentic loop. The tool returns an immediate acknowledgement, the LLM continues responding, and the task runs to completion in the background. When it finishes, its result is written to memory and if you use [`streamUntilIdle()`](https://mastra.ai/reference/streaming/agents/streamUntilIdle) the agent is re-invoked automatically so the result is processed in the same call.
5
+ Background tasks let an agent dispatch a long-running tool call without blocking the agentic loop. The tool returns an immediate acknowledgement, the LLM continues responding, and the task runs to completion in the background. When it finishes, its result is written to memory and if you use `stream()` with the [`untilIdle`](https://mastra.ai/reference/agents/agent) option the agent is re-invoked automatically so the result is processed in the same call.
6
6
 
7
7
  ## When to use background tasks
8
8
 
@@ -119,7 +119,7 @@ If the agent has `backgroundTasks.disabled: true`, every tool call runs synchron
119
119
 
120
120
  ## Background tasks related stream chunks
121
121
 
122
- When a tool call dispatches as a background task, two streams may surface lifecycle events for it: the agent's own stream and the [`backgroundTaskManager.stream()`](https://mastra.ai/docs/streaming/background-task-streaming) SSE stream. Each stream covers a different set of chunk types:
122
+ When a tool call dispatches as a background task, two streams may surface lifecycle events for it: the agent's own stream and the [`backgroundTaskManager.stream()`](https://mastra.ai/docs/agents/background-tasks) SSE stream. Each stream covers a different set of chunk types:
123
123
 
124
124
  | Chunk type | When it fires | Emitted by |
125
125
  | --------------------------- | -------------------------------------------------------------------------------------- | -------------- |
@@ -133,20 +133,20 @@ When a tool call dispatches as a background task, two streams may surface lifecy
133
133
  | `background-task-suspended` | The tool called `suspend()` from inside its execute. | Manager stream |
134
134
  | `background-task-resumed` | A suspended task was resumed via `manager.resume(taskId, resumeData)`. | Manager stream |
135
135
 
136
- `agent.stream().fullStream` only emits the agent-loop chunks (`background-task-started`, `background-task-progress`) on its own. `agent.streamUntilIdle()` emits the same two chunks and additionally subscribes to the manager pubsub for the run's memory scope and pipes the seven manager chunks (`background-task-running`, `background-task-output`, `background-task-completed`, `background-task-failed`, `background-task-cancelled`, `background-task-suspended`, `background-task-resumed`) into the same `fullStream`.
136
+ `agent.stream().fullStream` only emits the agent-loop chunks (`background-task-started`, `background-task-progress`) on its own. `agent.stream()` with `untilIdle: true` emits the same two chunks and additionally subscribes to the manager pubsub for the run's memory scope and pipes the seven manager chunks (`background-task-running`, `background-task-output`, `background-task-completed`, `background-task-failed`, `background-task-cancelled`, `background-task-suspended`, `background-task-resumed`) into the same `fullStream`.
137
137
 
138
138
  `backgroundTaskManager.stream()` only emits the seven manager chunks.
139
139
 
140
140
  The full payload shapes are documented in the [background task chunks reference](https://mastra.ai/reference/streaming/ChunkType).
141
141
 
142
- ## Keep the agent stream open with `streamUntilIdle()`
142
+ ## Keep the agent stream open with `untilIdle`
143
143
 
144
- `agent.stream()` returns once the LLM emits a final response even if a background task is still running. Use `agent.streamUntilIdle()` when you want the stream to stay open until every dispatched background task has completed and the LLM has had a chance to respond to the result:
144
+ `agent.stream()` returns once the LLM emits a final response even if a background task is still running. Pass `untilIdle: true` when you want the stream to stay open until every dispatched background task has completed and the LLM has had a chance to respond to the result:
145
145
 
146
146
  ```typescript
147
- const stream = await agent.streamUntilIdle('Research solana for me', {
147
+ const stream = await agent.stream('Research solana for me', {
148
148
  memory: { thread: 't1', resource: 'u1' },
149
- maxIdleMs: 5 * 60_000,
149
+ untilIdle: true,
150
150
  })
151
151
 
152
152
  for await (const chunk of stream.fullStream) {
@@ -155,15 +155,22 @@ for await (const chunk of stream.fullStream) {
155
155
  }
156
156
  ```
157
157
 
158
- When a background task completes, the result is injected into the agent memory, `streamUntilIdle()` re-enters the agentic loop so the LLM can react to it. The stream closes when no tasks are running and no completions are queued.
158
+ When a background task completes, the result is injected into the agent memory, `stream()` re-enters the agentic loop so the LLM can react to it. The stream closes when no tasks are running and no completions are queued.
159
159
 
160
- `maxIdleMs` caps how long the stream waits between turns. The timer only runs while the wrapper is between turns, so a slow first token won't close the stream. The default is 5 minutes.
160
+ Customize the idle timeout by passing an object instead of `true`. The timer only runs while the wrapper is between turns, so a slow first token won't close the stream. The default is 5 minutes:
161
161
 
162
- > **Note:** Visit [`Agent.streamUntilIdle()`](https://mastra.ai/reference/streaming/agents/streamUntilIdle) for the full API.
162
+ ```typescript
163
+ const stream = await agent.stream('Research solana for me', {
164
+ memory: { thread: 't1', resource: 'u1' },
165
+ untilIdle: { maxIdleMs: 30_000 },
166
+ })
167
+ ```
168
+
169
+ > **Note:** Visit [`Agent.stream()`](https://mastra.ai/reference/agents/agent) for the full API.
163
170
 
164
171
  ### Aggregate properties
165
172
 
166
- `streamUntilIdle()` returns a `MastraModelOutput` that looks like the one from `stream()`, but only `fullStream` spans the initial turn **and** any auto-continuations. Aggregate properties (`text`, `toolCalls`, `toolResults`, `finishReason`, `messageList`, `getFullOutput()`) still resolve against the **first turn's** internal buffer. If you need an aggregate view across continuations, consume `fullStream` yourself and accumulate.
173
+ `stream()` with `untilIdle` returns a `MastraModelOutput` that looks like the one from a regular `stream()` call, but only `fullStream` spans the initial turn **and** any auto-continuations. Aggregate properties (`text`, `toolCalls`, `toolResults`, `finishReason`, `messageList`, `getFullOutput()`) still resolve against the **first turn's** internal buffer. If you need an aggregate view across continuations, consume `fullStream` yourself and accumulate.
167
174
 
168
175
  ## Subagents in the background
169
176
 
@@ -185,8 +192,9 @@ const supervisor = new Agent({
185
192
  },
186
193
  })
187
194
 
188
- const stream = await supervisor.streamUntilIdle('Research AI in education and write an article', {
195
+ const stream = await supervisor.stream('Research AI in education and write an article', {
189
196
  memory: { thread: 't1', resource: 'u1' },
197
+ untilIdle: true,
190
198
  })
191
199
  ```
192
200
 
@@ -263,7 +271,7 @@ await mastra.backgroundTaskManager?.resume(taskId, {
263
271
 
264
272
  ### What happens to the agent loop
265
273
 
266
- When a task suspends mid-`streamUntilIdle()`, the wrapper treats it as terminal for the current iteration and closes. To continue the agent immediately when the resume payload is in hand, call `agent.resumeStreamUntilIdle(resumeData, { runId, toolCallId, memory })`: the resumed bg task runs to completion, its result lands in the message list, and the agent runs a follow-up turn — all on the same SSE connection. If you'd rather drive the resume out-of-band, call `mastra.backgroundTaskManager.resume(taskId, resumeData)` directly and the result still writes into the thread for the next user turn to pick up.
274
+ When a task suspends mid-`stream()` with `untilIdle`, the wrapper treats it as terminal for the current iteration and closes. To continue the agent immediately when the resume payload is in hand, call `agent.resumeStream(resumeData, { runId, toolCallId, memory, untilIdle: true })`: the resumed bg task runs to completion, its result lands in the message list, and the agent runs a follow-up turn — all on the same SSE connection. If you'd rather drive the resume out-of-band, call `mastra.backgroundTaskManager.resume(taskId, resumeData)` directly and the result still writes into the thread for the next user turn to pick up.
267
275
 
268
276
  ### Re-registering the executor on resume
269
277
 
@@ -296,10 +304,77 @@ export const mastra = new Mastra({
296
304
  })
297
305
  ```
298
306
 
307
+ ## Streaming
308
+
309
+ ### Subscribe to all task events
310
+
311
+ Calling `stream()` with no filter returns a stream of every task event in the system. On connection, the stream emits a snapshot of all currently running tasks, then forwards live events as they happen.
312
+
313
+ ```typescript
314
+ const bgManager = mastra.backgroundTaskManager
315
+ if (!bgManager) throw new Error('Background tasks are not enabled')
316
+
317
+ const controller = new AbortController()
318
+ const stream = bgManager.stream({ abortSignal: controller.signal })
319
+
320
+ for await (const chunk of stream) {
321
+ switch (chunk.type) {
322
+ case 'background-task-running':
323
+ console.log('started', chunk.payload.taskId, chunk.payload.toolName)
324
+ break
325
+ case 'background-task-completed':
326
+ console.log('done', chunk.payload.taskId, chunk.payload.result)
327
+ break
328
+ case 'background-task-failed':
329
+ console.error('failed', chunk.payload.taskId, chunk.payload.error)
330
+ break
331
+ }
332
+ }
333
+ ```
334
+
335
+ The stream stays open until the caller's `AbortSignal` fires. Always pass an `abortSignal` so you can disconnect cleanly.
336
+
337
+ ### Filter the stream
338
+
339
+ Pass any combination of filter options to narrow the events you receive. Filters apply to both the initial snapshot and the live event subscription.
340
+
341
+ ```typescript
342
+ const stream = bgManager.stream({
343
+ agentId: 'researcher',
344
+ threadId: 't1',
345
+ resourceId: 'u1',
346
+ abortSignal: controller.signal,
347
+ })
348
+ ```
349
+
350
+ | Filter | Description |
351
+ | ------------- | --------------------------------------------------- |
352
+ | `agentId` | Only events from tasks dispatched by this agent |
353
+ | `runId` | Only events from this specific agent run |
354
+ | `threadId` | Only events from tasks scoped to this memory thread |
355
+ | `resourceId` | Only events from tasks scoped to this resource |
356
+ | `taskId` | Only events for a single task |
357
+ | `abortSignal` | Closes the stream when the signal aborts |
358
+
359
+ ### Look up task state directly
360
+
361
+ For one-off lookups instead of a live stream, use `getTask` and `listTasks`:
362
+
363
+ ```typescript
364
+ const task = await mastra.backgroundTaskManager?.getTask(taskId)
365
+ const { tasks, total } = await mastra.backgroundTaskManager?.listTasks({
366
+ status: 'running',
367
+ agentId: 'researcher',
368
+ })
369
+ ```
370
+
371
+ These read from storage rather than the pubsub stream, so they're suitable for paginated lists and detail views.
372
+
299
373
  ## Related
300
374
 
301
- - [`Agent.streamUntilIdle()` reference](https://mastra.ai/reference/streaming/agents/streamUntilIdle)
375
+ - [`Agent.stream()` reference](https://mastra.ai/reference/agents/agent)
302
376
  - [backgroundTasks configuration reference](https://mastra.ai/reference/configuration)
377
+ - [Durable agents](https://mastra.ai/docs/agents/durable-agents)
303
378
  - [Supervisor agents](https://mastra.ai/docs/agents/supervisor-agents)
304
379
  - [Stream chunk types](https://mastra.ai/reference/streaming/ChunkType)
305
380
  - [Storage](https://mastra.ai/docs/memory/storage)
package/dist/index.cjs CHANGED
@@ -47670,9 +47670,21 @@ ${workingMemory}`;
47670
47670
  let currentChunk = "";
47671
47671
  const words = text4.split(/\s+/);
47672
47672
  for (const word of words) {
47673
+ if (word.length > charSize) {
47674
+ if (currentChunk) {
47675
+ chunks.push(currentChunk);
47676
+ currentChunk = "";
47677
+ }
47678
+ for (let i = 0; i < word.length; i += charSize) {
47679
+ chunks.push(word.slice(i, i + charSize));
47680
+ }
47681
+ continue;
47682
+ }
47673
47683
  const wordWithSpace = currentChunk ? " " + word : word;
47674
47684
  if (currentChunk.length + wordWithSpace.length > charSize) {
47675
- chunks.push(currentChunk);
47685
+ if (currentChunk) {
47686
+ chunks.push(currentChunk);
47687
+ }
47676
47688
  currentChunk = word;
47677
47689
  } else {
47678
47690
  currentChunk += wordWithSpace;