@mastra/memory 1.21.1-alpha.0 → 1.21.1-alpha.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 +11 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-agents-background-tasks.md +89 -14
- package/dist/index.cjs +13 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @mastra/memory
|
|
2
2
|
|
|
3
|
+
## 1.21.1-alpha.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 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))
|
|
8
|
+
|
|
9
|
+
`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.
|
|
10
|
+
|
|
11
|
+
- 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)]:
|
|
12
|
+
- @mastra/core@1.46.0-alpha.3
|
|
13
|
+
|
|
3
14
|
## 1.21.1-alpha.0
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -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
|
|
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/
|
|
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.
|
|
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 `
|
|
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.
|
|
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.
|
|
147
|
+
const stream = await agent.stream('Research solana for me', {
|
|
148
148
|
memory: { thread: 't1', resource: 'u1' },
|
|
149
|
-
|
|
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, `
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
`
|
|
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.
|
|
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-`
|
|
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.
|
|
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
|
-
|
|
47685
|
+
if (currentChunk) {
|
|
47686
|
+
chunks.push(currentChunk);
|
|
47687
|
+
}
|
|
47676
47688
|
currentChunk = word;
|
|
47677
47689
|
} else {
|
|
47678
47690
|
currentChunk += wordWithSpace;
|