@mastra/memory 1.5.0 → 1.5.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 (50) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/dist/{chunk-DF7NDDSM.js → chunk-6PKWQ3GH.js} +28 -11
  3. package/dist/chunk-6PKWQ3GH.js.map +1 -0
  4. package/dist/{chunk-LLTHE64H.cjs → chunk-6XVTMLW4.cjs} +28 -11
  5. package/dist/chunk-6XVTMLW4.cjs.map +1 -0
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/{observational-memory-ZNTAIUGT.js → observational-memory-AJWSMZVP.js} +3 -3
  9. package/dist/{observational-memory-ZNTAIUGT.js.map → observational-memory-AJWSMZVP.js.map} +1 -1
  10. package/dist/{observational-memory-4PCXEZIS.cjs → observational-memory-Q5TO525O.cjs} +17 -17
  11. package/dist/{observational-memory-4PCXEZIS.cjs.map → observational-memory-Q5TO525O.cjs.map} +1 -1
  12. package/dist/processors/index.cjs +15 -15
  13. package/dist/processors/index.js +1 -1
  14. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  15. package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
  16. package/package.json +7 -7
  17. package/dist/chunk-DF7NDDSM.js.map +0 -1
  18. package/dist/chunk-LLTHE64H.cjs.map +0 -1
  19. package/dist/docs/SKILL.md +0 -54
  20. package/dist/docs/assets/SOURCE_MAP.json +0 -103
  21. package/dist/docs/references/docs-agents-agent-approval.md +0 -377
  22. package/dist/docs/references/docs-agents-agent-memory.md +0 -212
  23. package/dist/docs/references/docs-agents-network-approval.md +0 -275
  24. package/dist/docs/references/docs-agents-networks.md +0 -290
  25. package/dist/docs/references/docs-memory-memory-processors.md +0 -316
  26. package/dist/docs/references/docs-memory-message-history.md +0 -260
  27. package/dist/docs/references/docs-memory-observational-memory.md +0 -246
  28. package/dist/docs/references/docs-memory-overview.md +0 -45
  29. package/dist/docs/references/docs-memory-semantic-recall.md +0 -272
  30. package/dist/docs/references/docs-memory-storage.md +0 -261
  31. package/dist/docs/references/docs-memory-working-memory.md +0 -400
  32. package/dist/docs/references/reference-core-getMemory.md +0 -50
  33. package/dist/docs/references/reference-core-listMemory.md +0 -56
  34. package/dist/docs/references/reference-memory-clone-utilities.md +0 -199
  35. package/dist/docs/references/reference-memory-cloneThread.md +0 -130
  36. package/dist/docs/references/reference-memory-createThread.md +0 -68
  37. package/dist/docs/references/reference-memory-getThreadById.md +0 -24
  38. package/dist/docs/references/reference-memory-listThreads.md +0 -145
  39. package/dist/docs/references/reference-memory-memory-class.md +0 -147
  40. package/dist/docs/references/reference-memory-observational-memory.md +0 -565
  41. package/dist/docs/references/reference-processors-token-limiter-processor.md +0 -113
  42. package/dist/docs/references/reference-storage-dynamodb.md +0 -282
  43. package/dist/docs/references/reference-storage-libsql.md +0 -135
  44. package/dist/docs/references/reference-storage-mongodb.md +0 -262
  45. package/dist/docs/references/reference-storage-postgresql.md +0 -529
  46. package/dist/docs/references/reference-storage-upstash.md +0 -160
  47. package/dist/docs/references/reference-vectors-libsql.md +0 -305
  48. package/dist/docs/references/reference-vectors-mongodb.md +0 -295
  49. package/dist/docs/references/reference-vectors-pg.md +0 -408
  50. package/dist/docs/references/reference-vectors-upstash.md +0 -294
@@ -1,54 +0,0 @@
1
- ---
2
- name: mastra-memory
3
- description: Documentation for @mastra/memory. Use when working with @mastra/memory APIs, configuration, or implementation.
4
- metadata:
5
- package: "@mastra/memory"
6
- version: "1.5.0"
7
- ---
8
-
9
- ## When to use
10
-
11
- Use this skill whenever you are working with @mastra/memory to obtain the domain-specific knowledge.
12
-
13
- ## How to use
14
-
15
- Read the individual reference documents for detailed explanations and code examples.
16
-
17
- ### Docs
18
-
19
- - [Agent Approval](references/docs-agents-agent-approval.md) - Learn how to require approvals, suspend tool execution, and automatically resume suspended tools while keeping humans in control of agent workflows.
20
- - [Agent Memory](references/docs-agents-agent-memory.md) - Learn how to add memory to agents to store message history and maintain context across interactions.
21
- - [Network Approval](references/docs-agents-network-approval.md) - Learn how to require approvals, suspend execution, and resume suspended networks while keeping humans in control of agent network workflows.
22
- - [Agent Networks](references/docs-agents-networks.md) - Learn how to coordinate multiple agents, workflows, and tools using agent networks for complex, non-deterministic task execution.
23
- - [Memory Processors](references/docs-memory-memory-processors.md) - Learn how to use memory processors in Mastra to filter, trim, and transform messages before they're sent to the language model to manage context window limits.
24
- - [Message History](references/docs-memory-message-history.md) - Learn how to configure message history in Mastra to store recent messages from the current conversation.
25
- - [Observational Memory](references/docs-memory-observational-memory.md) - Learn how Observational Memory keeps your agent's context window small while preserving long-term memory across conversations.
26
- - [Memory overview](references/docs-memory-overview.md) - Learn how Mastra's memory system works with working memory, message history, semantic recall, and observational memory.
27
- - [Semantic Recall](references/docs-memory-semantic-recall.md) - Learn how to use semantic recall in Mastra to retrieve relevant messages from past conversations using vector search and embeddings.
28
- - [Storage](references/docs-memory-storage.md) - Configure storage for Mastra's memory system to persist conversations, workflows, and traces.
29
- - [Working Memory](references/docs-memory-working-memory.md) - Learn how to configure working memory in Mastra to store persistent user data, preferences.
30
-
31
- ### Reference
32
-
33
- - [Reference: Mastra.getMemory()](references/reference-core-getMemory.md) - Documentation for the `Mastra.getMemory()` method in Mastra, which retrieves a registered memory instance by its registry key.
34
- - [Reference: Mastra.listMemory()](references/reference-core-listMemory.md) - Documentation for the `Mastra.listMemory()` method in Mastra, which returns all registered memory instances.
35
- - [Reference: Cloned Thread Utilities](references/reference-memory-clone-utilities.md) - Documentation for utility methods to work with cloned threads in Mastra Memory.
36
- - [Reference: Memory.cloneThread()](references/reference-memory-cloneThread.md) - Documentation for the `Memory.cloneThread()` method in Mastra, which creates a copy of a conversation thread with all its messages.
37
- - [Reference: Memory.createThread()](references/reference-memory-createThread.md) - Documentation for the `Memory.createThread()` method in Mastra, which creates a new conversation thread in the memory system.
38
- - [Reference: Memory.getThreadById()](references/reference-memory-getThreadById.md) - Documentation for the `Memory.getThreadById()` method in Mastra, which retrieves a specific thread by its ID.
39
- - [Reference: Memory.listThreads()](references/reference-memory-listThreads.md) - Documentation for the `Memory.listThreads()` method in Mastra, which retrieves threads with optional filtering by resourceId and/or metadata.
40
- - [Reference: Memory Class](references/reference-memory-memory-class.md) - Documentation for the `Memory` class in Mastra, which provides a robust system for managing conversation history and thread-based message storage.
41
- - [Reference: Observational Memory](references/reference-memory-observational-memory.md) - API reference for Observational Memory in Mastra — a three-tier memory system that uses Observer and Reflector agents to maintain long-term memory across conversations.
42
- - [Reference: Token Limiter Processor](references/reference-processors-token-limiter-processor.md) - Documentation for the TokenLimiterProcessor in Mastra, which limits the number of tokens in messages.
43
- - [Reference: DynamoDB Storage](references/reference-storage-dynamodb.md) - Documentation for the DynamoDB storage implementation in Mastra, using a single-table design with ElectroDB.
44
- - [Reference: libSQL Storage](references/reference-storage-libsql.md) - Documentation for the libSQL storage implementation in Mastra.
45
- - [Reference: MongoDB Storage](references/reference-storage-mongodb.md) - Documentation for the MongoDB storage implementation in Mastra.
46
- - [Reference: PostgreSQL Storage](references/reference-storage-postgresql.md) - Documentation for the PostgreSQL storage implementation in Mastra.
47
- - [Reference: Upstash Storage](references/reference-storage-upstash.md) - Documentation for the Upstash storage implementation in Mastra.
48
- - [Reference: libSQL Vector Store](references/reference-vectors-libsql.md) - Documentation for the LibSQLVector class in Mastra, which provides vector search using libSQL with vector extensions.
49
- - [Reference: MongoDB Vector Store](references/reference-vectors-mongodb.md) - Documentation for the MongoDBVector class in Mastra, which provides vector search using MongoDB Atlas and Atlas Vector Search.
50
- - [Reference: PG Vector Store](references/reference-vectors-pg.md) - Documentation for the PgVector class in Mastra, which provides vector search using PostgreSQL with pgvector extension.
51
- - [Reference: Upstash Vector Store](references/reference-vectors-upstash.md) - Documentation for the UpstashVector class in Mastra, which provides vector search using Upstash Vector.
52
-
53
-
54
- Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
@@ -1,103 +0,0 @@
1
- {
2
- "version": "1.5.0",
3
- "package": "@mastra/memory",
4
- "exports": {
5
- "OBSERVATIONAL_MEMORY_DEFAULTS": {
6
- "types": "dist/processors/index.d.ts",
7
- "implementation": "dist/chunk-DF7NDDSM.js"
8
- },
9
- "OBSERVATION_CONTEXT_INSTRUCTIONS": {
10
- "types": "dist/processors/index.d.ts",
11
- "implementation": "dist/chunk-DF7NDDSM.js"
12
- },
13
- "OBSERVATION_CONTEXT_PROMPT": {
14
- "types": "dist/processors/index.d.ts",
15
- "implementation": "dist/chunk-DF7NDDSM.js"
16
- },
17
- "OBSERVATION_CONTINUATION_HINT": {
18
- "types": "dist/processors/index.d.ts",
19
- "implementation": "dist/chunk-DF7NDDSM.js"
20
- },
21
- "OBSERVER_SYSTEM_PROMPT": {
22
- "types": "dist/processors/index.d.ts",
23
- "implementation": "dist/chunk-DF7NDDSM.js"
24
- },
25
- "ObservationalMemory": {
26
- "types": "dist/processors/index.d.ts",
27
- "implementation": "dist/chunk-DF7NDDSM.js",
28
- "line": 1251
29
- },
30
- "TokenCounter": {
31
- "types": "dist/processors/index.d.ts",
32
- "implementation": "dist/chunk-DF7NDDSM.js",
33
- "line": 912
34
- },
35
- "buildObserverPrompt": {
36
- "types": "dist/processors/index.d.ts",
37
- "implementation": "dist/chunk-DF7NDDSM.js",
38
- "line": 533
39
- },
40
- "buildObserverSystemPrompt": {
41
- "types": "dist/processors/index.d.ts",
42
- "implementation": "dist/chunk-DF7NDDSM.js",
43
- "line": 281
44
- },
45
- "extractCurrentTask": {
46
- "types": "dist/processors/index.d.ts",
47
- "implementation": "dist/chunk-DF7NDDSM.js",
48
- "line": 662
49
- },
50
- "formatMessagesForObserver": {
51
- "types": "dist/processors/index.d.ts",
52
- "implementation": "dist/chunk-DF7NDDSM.js",
53
- "line": 376
54
- },
55
- "hasCurrentTaskSection": {
56
- "types": "dist/processors/index.d.ts",
57
- "implementation": "dist/chunk-DF7NDDSM.js",
58
- "line": 650
59
- },
60
- "optimizeObservationsForContext": {
61
- "types": "dist/processors/index.d.ts",
62
- "implementation": "dist/chunk-DF7NDDSM.js",
63
- "line": 673
64
- },
65
- "parseObserverOutput": {
66
- "types": "dist/processors/index.d.ts",
67
- "implementation": "dist/chunk-DF7NDDSM.js",
68
- "line": 564
69
- },
70
- "extractWorkingMemoryContent": {
71
- "types": "dist/index.d.ts",
72
- "implementation": "dist/memory"
73
- },
74
- "extractWorkingMemoryTags": {
75
- "types": "dist/index.d.ts",
76
- "implementation": "dist/memory"
77
- },
78
- "removeWorkingMemoryTags": {
79
- "types": "dist/index.d.ts",
80
- "implementation": "dist/memory"
81
- },
82
- "MessageHistory": {
83
- "types": "dist/index.d.ts",
84
- "implementation": "dist/processors"
85
- },
86
- "SemanticRecall": {
87
- "types": "dist/index.d.ts",
88
- "implementation": "dist/processors"
89
- },
90
- "WorkingMemory": {
91
- "types": "dist/index.d.ts",
92
- "implementation": "dist/processors"
93
- }
94
- },
95
- "modules": {
96
- "processors": {
97
- "index": "dist/processors/index.js",
98
- "chunks": [
99
- "chunk-DF7NDDSM.js"
100
- ]
101
- }
102
- }
103
- }
@@ -1,377 +0,0 @@
1
- # Agent Approval
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.
4
-
5
- ## Tool call approval
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.
8
-
9
- ### Storage
10
-
11
- Agent approval uses a snapshot to capture the state of the request. Ensure you've enabled a storage provider in your main Mastra instance. If storage isn't enabled you'll see an error relating to snapshot not found.
12
-
13
- ```typescript
14
- import { Mastra } from "@mastra/core/mastra";
15
- import { LibSQLStore } from "@mastra/libsql";
16
-
17
- export const mastra = new Mastra({
18
- storage: new LibSQLStore({
19
- id: "mastra-storage",
20
- url: ":memory:"
21
- })
22
- });
23
- ```
24
-
25
- ## Agent-level approval
26
-
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.
28
-
29
- ```typescript
30
- const stream = await agent.stream("What's the weather in London?", {
31
- requireToolApproval: true
32
- });
33
- ```
34
-
35
- ### Approving tool calls
36
-
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.
38
-
39
- ```typescript
40
- const handleApproval = async () => {
41
- const approvedStream = await agent.approveToolCall({ runId: stream.runId });
42
-
43
- for await (const chunk of approvedStream.textStream) {
44
- process.stdout.write(chunk);
45
- }
46
- process.stdout.write("\n");
47
- };
48
- ```
49
-
50
- ### Declining tool calls
51
-
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.
53
-
54
- ```typescript
55
- const handleDecline = async () => {
56
- const declinedStream = await agent.declineToolCall({ runId: stream.runId });
57
-
58
- for await (const chunk of declinedStream.textStream) {
59
- process.stdout.write(chunk);
60
- }
61
- process.stdout.write("\n");
62
- };
63
- ```
64
-
65
- ## Tool approval with generate()
66
-
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.
68
-
69
- ### How it works
70
-
71
- When a tool requires approval during a `generate()` call, the response includes:
72
-
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
76
-
77
- ### Approving tool calls
78
-
79
- To approve a tool call with `generate()`, use the `approveToolCallGenerate` method:
80
-
81
- ```typescript
82
- const output = await agent.generate("Find user John", {
83
- requireToolApproval: true,
84
- });
85
-
86
- if (output.finishReason === "suspended") {
87
- console.log("Tool requires approval:", output.suspendPayload.toolName);
88
- console.log("Arguments:", output.suspendPayload.args);
89
-
90
- // Approve the tool call and get the final result
91
- const result = await agent.approveToolCallGenerate({
92
- runId: output.runId,
93
- toolCallId: output.suspendPayload.toolCallId,
94
- });
95
-
96
- console.log("Final result:", result.text);
97
- }
98
- ```
99
-
100
- ### Declining tool calls
101
-
102
- To decline a tool call, use the `declineToolCallGenerate` method:
103
-
104
- ```typescript
105
- if (output.finishReason === "suspended") {
106
- const result = await agent.declineToolCallGenerate({
107
- runId: output.runId,
108
- toolCallId: output.suspendPayload.toolCallId,
109
- });
110
-
111
- // Agent will respond acknowledging the declined tool
112
- console.log(result.text);
113
- }
114
- ```
115
-
116
- ### Stream vs Generate comparison
117
-
118
- | Aspect | `stream()` | `generate()` |
119
- | ------------------ | ---------------------------- | ------------------------------------------------ |
120
- | Response type | Streaming chunks | Complete response |
121
- | Approval detection | `tool-call-approval` chunk | `finishReason: 'suspended'` |
122
- | Approve method | `approveToolCall({ runId })` | `approveToolCallGenerate({ runId, toolCallId })` |
123
- | Decline method | `declineToolCall({ runId })` | `declineToolCallGenerate({ runId, toolCallId })` |
124
- | Result | Stream to iterate | Full output object |
125
-
126
- ## Tool-level approval
127
-
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.
129
-
130
- ### Tool approval using `requireToolApproval`
131
-
132
- In this approach, `requireApproval` is configured on the tool definition (shown below) rather than on the agent.
133
-
134
- ```typescript
135
- export const testTool = createTool({
136
- id: "test-tool",
137
- description: "Fetches weather for a location",
138
- inputSchema: z.object({
139
- location: z.string()
140
- }),
141
- outputSchema: z.object({
142
- weather: z.string()
143
- }),
144
- resumeSchema: z.object({
145
- approved: z.boolean()
146
- }),
147
- execute: async (inputData) => {
148
- const response = await fetch(`https://wttr.in/${inputData.location}?format=3`);
149
- const weather = await response.text();
150
-
151
- return { weather };
152
- },
153
- requireApproval: true
154
- });
155
- ```
156
-
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`.
158
-
159
- ```typescript
160
- const stream = await agent.stream("What's the weather in London?");
161
-
162
- for await (const chunk of stream.fullStream) {
163
- if (chunk.type === "tool-call-approval") {
164
- console.log("Approval required.");
165
- }
166
- }
167
-
168
- const handleResume = async () => {
169
- const resumedStream = await agent.resumeStream({ approved: true }, { runId: stream.runId });
170
-
171
- for await (const chunk of resumedStream.textStream) {
172
- process.stdout.write(chunk);
173
- }
174
- process.stdout.write("\n");
175
- };
176
- ```
177
-
178
- ### Tool approval using `suspend`
179
-
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.
181
-
182
- ```typescript
183
- export const testToolB = createTool({
184
- id: "test-tool-b",
185
- description: "Fetches weather for a location",
186
- inputSchema: z.object({
187
- location: z.string()
188
- }),
189
- outputSchema: z.object({
190
- weather: z.string()
191
- }),
192
- resumeSchema: z.object({
193
- approved: z.boolean()
194
- }),
195
- suspendSchema: z.object({
196
- reason: z.string()
197
- }),
198
- execute: async (inputData, context) => {
199
- const { resumeData: { approved } = {}, suspend } = context?.agent ?? {};
200
-
201
- if (!approved) {
202
- return suspend?.({ reason: "Approval required." });
203
- }
204
-
205
- const response = await fetch(`https://wttr.in/${inputData.location}?format=3`);
206
- const weather = await response.text();
207
-
208
- return { weather };
209
- }
210
- });
211
- ```
212
-
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`.
214
-
215
- ```typescript
216
- const stream = await agent.stream("What's the weather in London?");
217
-
218
- for await (const chunk of stream.fullStream) {
219
- if (chunk.type === "tool-call-suspended") {
220
- console.log(chunk.payload.suspendPayload);
221
- }
222
- }
223
-
224
- const handleResume = async () => {
225
- const resumedStream = await agent.resumeStream({ approved: true }, { runId: stream.runId });
226
-
227
- for await (const chunk of resumedStream.textStream) {
228
- process.stdout.write(chunk);
229
- }
230
- process.stdout.write("\n");
231
- };
232
- ```
233
-
234
- ## Automatic tool resumption
235
-
236
- When using tools that call `suspend()`, you can enable automatic resumption so the agent resumes suspended tools based on the user's next message. This creates a conversational flow where users provide the required information naturally, without your application needing to call `resumeStream()` explicitly.
237
-
238
- ### Enabling auto-resume
239
-
240
- Set `autoResumeSuspendedTools` to `true` in the agent's default options or when calling `stream()`:
241
-
242
- ```typescript
243
- import { Agent } from "@mastra/core/agent";
244
- import { Memory } from "@mastra/memory";
245
-
246
- // Option 1: In agent configuration
247
- const agent = new Agent({
248
- id: "my-agent",
249
- name: "My Agent",
250
- instructions: "You are a helpful assistant",
251
- model: "openai/gpt-4o-mini",
252
- tools: { weatherTool },
253
- memory: new Memory(),
254
- defaultOptions: {
255
- autoResumeSuspendedTools: true,
256
- },
257
- });
258
-
259
- // Option 2: Per-request
260
- const stream = await agent.stream("What's the weather?", {
261
- autoResumeSuspendedTools: true,
262
- });
263
- ```
264
-
265
- ### How it works
266
-
267
- When `autoResumeSuspendedTools` is enabled:
268
-
269
- 1. A tool suspends execution by calling `suspend()` with a payload (e.g., requesting more information)
270
-
271
- 2. The suspension is persisted to memory along with the conversation
272
-
273
- 3. When the user sends their next message on the same thread, the agent:
274
-
275
- - Detects the suspended tool from message history
276
- - Extracts `resumeData` from the user's message based on the tool's `resumeSchema`
277
- - Automatically resumes the tool with the extracted data
278
-
279
- ### Example
280
-
281
- ```typescript
282
- import { createTool } from "@mastra/core/tools";
283
- import { z } from "zod";
284
-
285
- export const weatherTool = createTool({
286
- id: "weather-info",
287
- description: "Fetches weather information for a city",
288
- suspendSchema: z.object({
289
- message: z.string(),
290
- }),
291
- resumeSchema: z.object({
292
- city: z.string(),
293
- }),
294
- execute: async (_inputData, context) => {
295
- // Check if this is a resume with data
296
- if (!context?.agent?.resumeData) {
297
- // First call - suspend and ask for the city
298
- return context?.agent?.suspend({
299
- message: "What city do you want to know the weather for?",
300
- });
301
- }
302
-
303
- // Resume call - city was extracted from user's message
304
- const { city } = context.agent.resumeData;
305
- const response = await fetch(`https://wttr.in/${city}?format=3`);
306
- const weather = await response.text();
307
-
308
- return { city, weather };
309
- },
310
- });
311
-
312
- const agent = new Agent({
313
- id: "my-agent",
314
- name: "My Agent",
315
- instructions: "You are a helpful assistant",
316
- model: "openai/gpt-4o-mini",
317
- tools: { weatherTool },
318
- memory: new Memory(),
319
- defaultOptions: {
320
- autoResumeSuspendedTools: true,
321
- },
322
- });
323
-
324
- const stream = await agent.stream("What's the weather like?");
325
-
326
- for await (const chunk of stream.fullStream) {
327
- if (chunk.type === "tool-call-suspended") {
328
- console.log(chunk.payload.suspendPayload);
329
- }
330
- }
331
-
332
- const handleResume = async () => {
333
- const resumedStream = await agent.stream("San Francisco");
334
-
335
- for await (const chunk of resumedStream.textStream) {
336
- process.stdout.write(chunk);
337
- }
338
- process.stdout.write("\n");
339
- };
340
- ```
341
-
342
- **Conversation flow:**
343
-
344
- ```text
345
- User: "What's the weather like?"
346
- Agent: "What city do you want to know the weather for?"
347
-
348
- User: "San Francisco"
349
- Agent: "The weather in San Francisco is: San Francisco: ☀️ +72°F"
350
- ```
351
-
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`.
353
-
354
- ### Requirements
355
-
356
- For automatic tool resumption to work:
357
-
358
- - **Memory configured**: The agent needs memory to track suspended tools across messages
359
- - **Same thread**: The follow-up message must use the same memory thread and resource identifiers
360
- - **`resumeSchema` defined**: The tool must define a `resumeSchema` so the agent knows what data structure to extract from the user's message
361
-
362
- ### Manual vs automatic resumption
363
-
364
- | Approach | Use case |
365
- | -------------------------------------- | ------------------------------------------------------------------------ |
366
- | Manual (`resumeStream()`) | Programmatic control, webhooks, button clicks, external triggers |
367
- | Automatic (`autoResumeSuspendedTools`) | Conversational flows where users provide resume data in natural language |
368
-
369
- Both approaches work with the same tool definitions. Automatic resumption triggers only when suspended tools exist in the message history and the user sends a new message on the same thread.
370
-
371
- ## Related
372
-
373
- - [Using Tools](https://mastra.ai/docs/agents/using-tools)
374
- - [Agent Overview](https://mastra.ai/docs/agents/overview)
375
- - [Tools Overview](https://mastra.ai/docs/mcp/overview)
376
- - [Agent Memory](https://mastra.ai/docs/agents/agent-memory)
377
- - [Request Context](https://mastra.ai/docs/server/request-context)