@mastra/mcp-docs-server 0.0.3 → 0.0.4-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/.docs/organized/changelogs/%40mastra%2Fastra.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +29 -29
- package/.docs/organized/changelogs/%40mastra%2Fcomposio.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +23 -23
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +36 -36
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +35 -35
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +35 -35
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +29 -29
- package/.docs/organized/changelogs/%40mastra%2Fgithub.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +26 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +35 -35
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Frag.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fragie.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-azure.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-deepgram.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-elevenlabs.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-google.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-ibm.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-murf.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-openai.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-playai.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-replicate.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fspeech-speechify.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fstabilityai.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +25 -0
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +31 -31
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +30 -30
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +25 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +27 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +26 -26
- package/.docs/organized/changelogs/create-mastra.md +22 -22
- package/.docs/organized/changelogs/mastra.md +47 -47
- package/.docs/organized/code-examples/ai-sdk-useChat.md +2 -1
- package/.docs/raw/agents/02-adding-tools.mdx +6 -0
- package/.docs/raw/agents/02a-mcp-guide.mdx +192 -0
- package/.docs/raw/agents/03-adding-voice.mdx +8 -8
- package/.docs/raw/deployment/deployment.mdx +5 -42
- package/.docs/raw/deployment/server.mdx +45 -3
- package/.docs/raw/evals/00-overview.mdx +2 -2
- package/.docs/raw/evals/03-running-in-ci.mdx +7 -4
- package/.docs/raw/getting-started/mcp-docs-server.mdx +5 -2
- package/.docs/raw/guides/04-research-assistant.mdx +273 -0
- package/.docs/raw/local-dev/mastra-dev.mdx +2 -2
- package/.docs/raw/observability/logging.mdx +38 -0
- package/.docs/raw/observability/nextjs-tracing.mdx +102 -0
- package/.docs/raw/observability/tracing.mdx +110 -0
- package/.docs/raw/rag/overview.mdx +3 -3
- package/.docs/raw/rag/retrieval.mdx +7 -4
- package/.docs/raw/rag/vector-databases.mdx +107 -40
- package/.docs/raw/reference/client-js/memory.mdx +6 -3
- package/.docs/raw/reference/client-js/workflows.mdx +1 -0
- package/.docs/raw/reference/observability/providers/langsmith.mdx +2 -0
- package/.docs/raw/reference/rag/libsql.mdx +3 -3
- package/.docs/raw/reference/rag/upstash.mdx +50 -1
- package/.docs/raw/reference/rag/vectorize.mdx +48 -3
- package/.docs/raw/reference/tools/client.mdx +10 -2
- package/.docs/raw/reference/tools/vector-query-tool.mdx +1 -1
- package/.docs/raw/reference/voice/sarvam.mdx +260 -0
- package/.docs/raw/reference/workflows/afterEvent.mdx +76 -0
- package/.docs/raw/reference/workflows/events.mdx +305 -0
- package/.docs/raw/reference/workflows/resumeWithEvent.mdx +134 -0
- package/.docs/raw/reference/workflows/snapshots.mdx +204 -0
- package/.docs/raw/reference/workflows/step-retries.mdx +203 -0
- package/.docs/raw/voice/overview.mdx +135 -0
- package/.docs/raw/voice/speech-to-text.mdx +45 -0
- package/.docs/raw/voice/text-to-speech.mdx +52 -0
- package/.docs/raw/voice/voice-to-voice.mdx +310 -0
- package/.docs/raw/workflows/dynamic-workflows.mdx +4 -0
- package/.docs/raw/workflows/error-handling.mdx +183 -0
- package/.docs/raw/workflows/steps.mdx +12 -2
- package/.docs/raw/workflows/suspend-and-resume.mdx +207 -2
- package/.docs/raw/workflows/variables.mdx +23 -3
- package/dist/_tsup-dts-rollup.d.ts +83 -0
- package/dist/chunk-YEOOTUPA.js +191 -0
- package/dist/prepare-docs/prepare.d.ts +1 -1
- package/dist/prepare-docs/prepare.js +1 -13
- package/dist/stdio.d.ts +0 -1
- package/dist/stdio.js +352 -5
- package/package.json +9 -15
- package/.docs/raw/deployment/logging-and-tracing.mdx +0 -242
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -19
- package/dist/prepare-docs/code-examples.d.ts +0 -4
- package/dist/prepare-docs/code-examples.js +0 -91
- package/dist/prepare-docs/copy-raw.d.ts +0 -1
- package/dist/prepare-docs/copy-raw.js +0 -41
- package/dist/prepare-docs/index.d.ts +0 -1
- package/dist/prepare-docs/index.js +0 -8
- package/dist/prepare-docs/package-changes.d.ts +0 -4
- package/dist/prepare-docs/package-changes.js +0 -92
- package/dist/sse.d.ts +0 -1
- package/dist/sse.js +0 -9
- package/dist/tools/__tests__/blog.test.d.ts +0 -1
- package/dist/tools/__tests__/blog.test.js +0 -48
- package/dist/tools/__tests__/changes.test.d.ts +0 -1
- package/dist/tools/__tests__/changes.test.js +0 -37
- package/dist/tools/__tests__/docs.test.d.ts +0 -1
- package/dist/tools/__tests__/docs.test.js +0 -46
- package/dist/tools/__tests__/examples.test.d.ts +0 -1
- package/dist/tools/__tests__/examples.test.js +0 -53
- package/dist/tools/blog.d.ts +0 -15
- package/dist/tools/blog.js +0 -73
- package/dist/tools/changes.d.ts +0 -11
- package/dist/tools/changes.js +0 -69
- package/dist/tools/docs.d.ts +0 -11
- package/dist/tools/docs.js +0 -176
- package/dist/tools/examples.d.ts +0 -11
- package/dist/tools/examples.js +0 -61
- package/dist/utils.d.ts +0 -6
- package/dist/utils.js +0 -9
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: ".resumeWithEvent() Method | Mastra Docs"
|
|
3
|
+
description: "Reference for the resumeWithEvent method that resumes suspended workflows using event data."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# resumeWithEvent()
|
|
7
|
+
|
|
8
|
+
The `resumeWithEvent()` method resumes workflow execution by providing data for a specific event that the workflow is waiting for.
|
|
9
|
+
|
|
10
|
+
## Syntax
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
const run = workflow.createRun();
|
|
14
|
+
|
|
15
|
+
// After the workflow has started and suspended at an event step
|
|
16
|
+
await run.resumeWithEvent(eventName: string, data: any): Promise<WorkflowRunResult>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Parameters
|
|
20
|
+
|
|
21
|
+
| Parameter | Type | Description |
|
|
22
|
+
|-----------|------|-------------|
|
|
23
|
+
| eventName | string | The name of the event to trigger. Must match an event defined in the workflow's `events` configuration. |
|
|
24
|
+
| data | any | The event data to provide. Must conform to the schema defined for that event. |
|
|
25
|
+
|
|
26
|
+
## Return Value
|
|
27
|
+
|
|
28
|
+
Returns a Promise that resolves to a `WorkflowRunResult` object, containing:
|
|
29
|
+
|
|
30
|
+
- `results`: The result status and output of each step in the workflow
|
|
31
|
+
- `activePaths`: A map of active workflow paths and their states
|
|
32
|
+
- `value`: The current state value of the workflow
|
|
33
|
+
- Other workflow execution metadata
|
|
34
|
+
|
|
35
|
+
## Description
|
|
36
|
+
|
|
37
|
+
The `resumeWithEvent()` method is used to resume a workflow that has been suspended at an event step created by the `afterEvent()` method. When called, this method:
|
|
38
|
+
|
|
39
|
+
1. Validates the provided event data against the schema defined for that event
|
|
40
|
+
2. Loads the workflow snapshot from storage
|
|
41
|
+
3. Updates the context with the event data in the `resumedEvent` field
|
|
42
|
+
4. Resumes execution from the event step
|
|
43
|
+
5. Continues workflow execution with the subsequent steps
|
|
44
|
+
|
|
45
|
+
This method is part of Mastra's event-driven workflow capabilities, allowing you to create workflows that can respond to external events or user interactions.
|
|
46
|
+
|
|
47
|
+
## Usage Notes
|
|
48
|
+
|
|
49
|
+
- The workflow must be in a suspended state, specifically at the event step created by `afterEvent(eventName)`
|
|
50
|
+
- The event data must conform to the schema defined for that event in the workflow configuration
|
|
51
|
+
- The workflow will continue execution from the point it was suspended
|
|
52
|
+
- If the workflow is not suspended or is suspended at a different step, this method may throw an error
|
|
53
|
+
- The event data is made available to subsequent steps via `context.inputData.resumedEvent`
|
|
54
|
+
|
|
55
|
+
## Examples
|
|
56
|
+
|
|
57
|
+
### Basic Usage
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// Define and start a workflow
|
|
61
|
+
const workflow = mastra.getWorkflow('approval-workflow');
|
|
62
|
+
const run = workflow.createRun();
|
|
63
|
+
|
|
64
|
+
// Start the workflow
|
|
65
|
+
await run.start({ triggerData: { requestId: 'req-123' } });
|
|
66
|
+
|
|
67
|
+
// Later, when the approval event occurs:
|
|
68
|
+
const result = await run.resumeWithEvent('approval', {
|
|
69
|
+
approved: true,
|
|
70
|
+
approverName: 'John Doe',
|
|
71
|
+
comment: 'Looks good to me!'
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
console.log(result.results);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### With Error Handling
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
try {
|
|
81
|
+
const result = await run.resumeWithEvent('paymentReceived', {
|
|
82
|
+
amount: 100.50,
|
|
83
|
+
transactionId: 'tx-456',
|
|
84
|
+
paymentMethod: 'credit-card',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
console.log('Workflow resumed successfully:', result.results);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error('Failed to resume workflow with event:', error);
|
|
90
|
+
// Handle error - could be invalid event data, workflow not suspended, etc.
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Monitoring and Auto-Resuming
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// Start a workflow
|
|
98
|
+
const { start, watch, resumeWithEvent } = workflow.createRun();
|
|
99
|
+
|
|
100
|
+
// Watch for suspended event steps
|
|
101
|
+
watch(async ({ context, activePaths }) => {
|
|
102
|
+
// Check if suspended at the approval event step
|
|
103
|
+
if (activePaths.some(path =>
|
|
104
|
+
path.stepId === '__approval_event' &&
|
|
105
|
+
path.status === 'suspended'
|
|
106
|
+
)) {
|
|
107
|
+
console.log('Workflow waiting for approval');
|
|
108
|
+
|
|
109
|
+
// In a real scenario, you would wait for the actual event
|
|
110
|
+
// Here we're simulating with a timeout
|
|
111
|
+
setTimeout(async () => {
|
|
112
|
+
try {
|
|
113
|
+
await resumeWithEvent('approval', {
|
|
114
|
+
approved: true,
|
|
115
|
+
approverName: 'Auto Approver',
|
|
116
|
+
});
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.error('Failed to auto-resume workflow:', error);
|
|
119
|
+
}
|
|
120
|
+
}, 5000); // Wait 5 seconds before auto-approving
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Start the workflow
|
|
125
|
+
await start({ triggerData: { requestId: 'auto-123' } });
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Related
|
|
129
|
+
|
|
130
|
+
- [Event-Driven Workflows](./events.mdx)
|
|
131
|
+
- [afterEvent()](./afterEvent.mdx)
|
|
132
|
+
- [Suspend and Resume](../../workflows/suspend-and-resume.mdx)
|
|
133
|
+
- [resume()](./resume.mdx)
|
|
134
|
+
- [watch()](./watch.mdx)
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: Snapshots | Workflow State Persistence | Mastra Docs"
|
|
3
|
+
description: "Technical reference on snapshots in Mastra - the serialized workflow state that enables suspend and resume functionality"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Snapshots
|
|
7
|
+
|
|
8
|
+
In Mastra, a snapshot is a serializable representation of a workflow's complete execution state at a specific point in time. Snapshots capture all the information needed to resume a workflow from exactly where it left off, including:
|
|
9
|
+
|
|
10
|
+
- The current state of each step in the workflow
|
|
11
|
+
- The outputs of completed steps
|
|
12
|
+
- The execution path taken through the workflow
|
|
13
|
+
- Any suspended steps and their metadata
|
|
14
|
+
- The remaining retry attempts for each step
|
|
15
|
+
- Additional contextual data needed to resume execution
|
|
16
|
+
|
|
17
|
+
Snapshots are automatically created and managed by Mastra whenever a workflow is suspended, and are persisted to the configured storage system.
|
|
18
|
+
|
|
19
|
+
## The Role of Snapshots in Suspend and Resume
|
|
20
|
+
|
|
21
|
+
Snapshots are the key mechanism enabling Mastra's suspend and resume capabilities. When a workflow step calls `await suspend()`:
|
|
22
|
+
|
|
23
|
+
1. The workflow execution is paused at that exact point
|
|
24
|
+
2. The current state of the workflow is captured as a snapshot
|
|
25
|
+
3. The snapshot is persisted to storage
|
|
26
|
+
4. The workflow step is marked as "suspended" with a status of `'suspended'`
|
|
27
|
+
5. Later, when `resume()` is called on the suspended step, the snapshot is retrieved
|
|
28
|
+
6. The workflow execution resumes from exactly where it left off
|
|
29
|
+
|
|
30
|
+
This mechanism provides a powerful way to implement human-in-the-loop workflows, handle rate limiting, wait for external resources, and implement complex branching workflows that may need to pause for extended periods.
|
|
31
|
+
|
|
32
|
+
## Snapshot Anatomy
|
|
33
|
+
|
|
34
|
+
A Mastra workflow snapshot consists of several key components:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
export interface WorkflowRunState {
|
|
38
|
+
// Core state info
|
|
39
|
+
value: Record<string, string>; // Current state machine value
|
|
40
|
+
context: { // Workflow context
|
|
41
|
+
steps: Record<string, { // Step execution results
|
|
42
|
+
status: 'success' | 'failed' | 'suspended' | 'waiting' | 'skipped';
|
|
43
|
+
payload?: any; // Step-specific data
|
|
44
|
+
error?: string; // Error info if failed
|
|
45
|
+
}>;
|
|
46
|
+
triggerData: Record<string, any>; // Initial trigger data
|
|
47
|
+
attempts: Record<string, number>; // Remaining retry attempts
|
|
48
|
+
inputData: Record<string, any>; // Initial input data
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
activePaths: Array<{ // Currently active execution paths
|
|
52
|
+
stepPath: string[];
|
|
53
|
+
stepId: string;
|
|
54
|
+
status: string;
|
|
55
|
+
}>;
|
|
56
|
+
|
|
57
|
+
// Metadata
|
|
58
|
+
runId: string; // Unique run identifier
|
|
59
|
+
timestamp: number; // Time snapshot was created
|
|
60
|
+
|
|
61
|
+
// For nested workflows and suspended steps
|
|
62
|
+
childStates?: Record<string, WorkflowRunState>; // Child workflow states
|
|
63
|
+
suspendedSteps?: Record<string, string>; // Mapping of suspended steps
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## How Snapshots Are Saved and Retrieved
|
|
68
|
+
|
|
69
|
+
Mastra persists snapshots to the configured storage system. By default, snapshots are saved to a LibSQL database, but can be configured to use other storage providers like Upstash.
|
|
70
|
+
The snapshots are stored in the `workflow_snapshots` table and identified uniquely by the `run_id` for the associated run when using libsql.
|
|
71
|
+
Utilizing a persistence layer allows for the snapshots to be persisted across workflow runs, allowing for advanced human-in-the-loop functionality.
|
|
72
|
+
|
|
73
|
+
Read more about [libsql storage](../storage/libsql.mdx) and [upstash storage](../storage/upstash.mdx) here.
|
|
74
|
+
|
|
75
|
+
### Saving Snapshots
|
|
76
|
+
|
|
77
|
+
When a workflow is suspended, Mastra automatically persists the workflow snapshot with these steps:
|
|
78
|
+
|
|
79
|
+
1. The `suspend()` function in a step execution triggers the snapshot process
|
|
80
|
+
2. The `WorkflowInstance.suspend()` method records the suspended machine
|
|
81
|
+
3. `persistWorkflowSnapshot()` is called to save the current state
|
|
82
|
+
4. The snapshot is serialized and stored in the configured database in the `workflow_snapshots` table
|
|
83
|
+
5. The storage record includes the workflow name, run ID, and the serialized snapshot
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
### Retrieving Snapshots
|
|
87
|
+
|
|
88
|
+
When a workflow is resumed, Mastra retrieves the persisted snapshot with these steps:
|
|
89
|
+
|
|
90
|
+
1. The `resume()` method is called with a specific step ID
|
|
91
|
+
2. The snapshot is loaded from storage using `loadWorkflowSnapshot()`
|
|
92
|
+
3. The snapshot is parsed and prepared for resumption
|
|
93
|
+
4. The workflow execution is recreated with the snapshot state
|
|
94
|
+
5. The suspended step is resumed, and execution continues
|
|
95
|
+
|
|
96
|
+
## Storage Options for Snapshots
|
|
97
|
+
|
|
98
|
+
Mastra provides multiple storage options for persisting snapshots.
|
|
99
|
+
|
|
100
|
+
A `storage` instance is configured on the `Mastra` class, and is used to setup a snapshot persistence layer for all workflows registered on the `Mastra` instance.
|
|
101
|
+
This means that storage is shared across all workflows registered with the same `Mastra` instance.
|
|
102
|
+
|
|
103
|
+
### LibSQL (Default)
|
|
104
|
+
|
|
105
|
+
The default storage option is LibSQL, a SQLite-compatible database:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
109
|
+
import { DefaultStorage } from '@mastra/core/storage/libsql';
|
|
110
|
+
|
|
111
|
+
const mastra = new Mastra({
|
|
112
|
+
storage: new DefaultStorage({
|
|
113
|
+
config: {
|
|
114
|
+
url: "file:storage.db", // Local file-based database
|
|
115
|
+
// For production:
|
|
116
|
+
// url: process.env.DATABASE_URL,
|
|
117
|
+
// authToken: process.env.DATABASE_AUTH_TOKEN,
|
|
118
|
+
}
|
|
119
|
+
}),
|
|
120
|
+
workflows: {
|
|
121
|
+
weatherWorkflow,
|
|
122
|
+
travelWorkflow,
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Upstash (Redis-Compatible)
|
|
128
|
+
|
|
129
|
+
For serverless environments:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
133
|
+
import { UpstashStore } from "@mastra/upstash";
|
|
134
|
+
|
|
135
|
+
const mastra = new Mastra({
|
|
136
|
+
storage: new UpstashStore({
|
|
137
|
+
url: process.env.UPSTASH_URL,
|
|
138
|
+
token: process.env.UPSTASH_TOKEN,
|
|
139
|
+
}),
|
|
140
|
+
workflows: {
|
|
141
|
+
weatherWorkflow,
|
|
142
|
+
travelWorkflow,
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Best Practices for Working with Snapshots
|
|
148
|
+
|
|
149
|
+
1. **Ensure Serializability**: Any data that needs to be included in the snapshot must be serializable (convertible to JSON).
|
|
150
|
+
|
|
151
|
+
2. **Minimize Snapshot Size**: Avoid storing large data objects directly in the workflow context. Instead, store references to them (like IDs) and retrieve the data when needed.
|
|
152
|
+
|
|
153
|
+
3. **Handle Resume Context Carefully**: When resuming a workflow, carefully consider what context to provide. This will be merged with the existing snapshot data.
|
|
154
|
+
|
|
155
|
+
4. **Set Up Proper Monitoring**: Implement monitoring for suspended workflows, especially long-running ones, to ensure they are properly resumed.
|
|
156
|
+
|
|
157
|
+
5. **Consider Storage Scaling**: For applications with many suspended workflows, ensure your storage solution is appropriately scaled.
|
|
158
|
+
|
|
159
|
+
## Advanced Snapshot Patterns
|
|
160
|
+
|
|
161
|
+
### Custom Snapshot Metadata
|
|
162
|
+
|
|
163
|
+
When suspending a workflow, you can include custom metadata that can help when resuming:
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
await suspend({
|
|
167
|
+
reason: "Waiting for customer approval",
|
|
168
|
+
requiredApprovers: ["manager", "finance"],
|
|
169
|
+
requestedBy: currentUser,
|
|
170
|
+
urgency: "high",
|
|
171
|
+
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
This metadata is stored with the snapshot and available when resuming.
|
|
176
|
+
|
|
177
|
+
### Conditional Resumption
|
|
178
|
+
|
|
179
|
+
You can implement conditional logic based on the suspend payload when resuming:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
run.watch(async ({ context, activePaths }) => {
|
|
183
|
+
for (const path of activePaths) {
|
|
184
|
+
const approvalStep = context.steps?.approval;
|
|
185
|
+
if (approvalStep?.status === 'suspended') {
|
|
186
|
+
const payload = approvalStep.suspendPayload;
|
|
187
|
+
|
|
188
|
+
if (payload.urgency === "high" && currentUser.role === "manager") {
|
|
189
|
+
await resume({
|
|
190
|
+
stepId: 'approval',
|
|
191
|
+
context: { approved: true, approver: currentUser.id },
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Related
|
|
200
|
+
|
|
201
|
+
- [Suspend Function Reference](./suspend.mdx)
|
|
202
|
+
- [Resume Function Reference](./resume.mdx)
|
|
203
|
+
- [Watch Function Reference](./watch.mdx)
|
|
204
|
+
- [Suspend and Resume Guide](../../workflows/suspend-and-resume.mdx)
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Step Retries | Error Handling | Mastra Docs"
|
|
3
|
+
description: "Automatically retry failed steps in Mastra workflows with configurable retry policies."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Step Retries
|
|
7
|
+
|
|
8
|
+
Mastra provides built-in retry mechanisms to handle transient failures in workflow steps. This allows workflows to recover gracefully from temporary issues without requiring manual intervention.
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
When a step in a workflow fails (throws an exception), Mastra can automatically retry the step execution based on a configurable retry policy. This is useful for handling:
|
|
13
|
+
|
|
14
|
+
- Network connectivity issues
|
|
15
|
+
- Service unavailability
|
|
16
|
+
- Rate limiting
|
|
17
|
+
- Temporary resource constraints
|
|
18
|
+
- Other transient failures
|
|
19
|
+
|
|
20
|
+
## Default Behavior
|
|
21
|
+
|
|
22
|
+
By default, steps do not retry when they fail. This means:
|
|
23
|
+
|
|
24
|
+
- A step will execute once
|
|
25
|
+
- If it fails, it will immediately mark the step as failed
|
|
26
|
+
- The workflow will continue to execute any subsequent steps that don't depend on the failed step
|
|
27
|
+
|
|
28
|
+
## Configuration Options
|
|
29
|
+
|
|
30
|
+
Retries can be configured at two levels:
|
|
31
|
+
|
|
32
|
+
### 1. Workflow-level Configuration
|
|
33
|
+
|
|
34
|
+
You can set a default retry configuration for all steps in a workflow:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
const workflow = new Workflow({
|
|
38
|
+
name: 'my-workflow',
|
|
39
|
+
retryConfig: {
|
|
40
|
+
attempts: 3, // Number of retries (in addition to the initial attempt)
|
|
41
|
+
delay: 1000, // Delay between retries in milliseconds
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 2. Step-level Configuration
|
|
47
|
+
|
|
48
|
+
You can also configure retries on individual steps, which will override the workflow-level configuration for that specific step:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const fetchDataStep = new Step({
|
|
52
|
+
id: 'fetchData',
|
|
53
|
+
execute: async () => {
|
|
54
|
+
// Fetch data from external API
|
|
55
|
+
},
|
|
56
|
+
retryConfig: {
|
|
57
|
+
attempts: 5, // This step will retry up to 5 times
|
|
58
|
+
delay: 2000, // With a 2-second delay between retries
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Retry Parameters
|
|
64
|
+
|
|
65
|
+
The `retryConfig` object supports the following parameters:
|
|
66
|
+
|
|
67
|
+
| Parameter | Type | Default | Description |
|
|
68
|
+
|-----------|------|---------|-------------|
|
|
69
|
+
| `attempts` | number | 0 | The number of retry attempts (in addition to the initial attempt) |
|
|
70
|
+
| `delay` | number | 1000 | Time in milliseconds to wait between retries |
|
|
71
|
+
|
|
72
|
+
## How Retries Work
|
|
73
|
+
|
|
74
|
+
When a step fails, Mastra's retry mechanism:
|
|
75
|
+
|
|
76
|
+
1. Checks if the step has retry attempts remaining
|
|
77
|
+
2. If attempts remain:
|
|
78
|
+
- Decrements the attempt counter
|
|
79
|
+
- Transitions the step to a "waiting" state
|
|
80
|
+
- Waits for the configured delay period
|
|
81
|
+
- Retries the step execution
|
|
82
|
+
3. If no attempts remain or all attempts have been exhausted:
|
|
83
|
+
- Marks the step as "failed"
|
|
84
|
+
- Continues workflow execution (for steps that don't depend on the failed step)
|
|
85
|
+
|
|
86
|
+
During retry attempts, the workflow execution remains active but paused for the specific step that is being retried.
|
|
87
|
+
|
|
88
|
+
## Examples
|
|
89
|
+
|
|
90
|
+
### Basic Retry Example
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { Workflow, Step } from '@mastra/core/workflows';
|
|
94
|
+
|
|
95
|
+
// Define a step that might fail
|
|
96
|
+
const unreliableApiStep = new Step({
|
|
97
|
+
id: 'callUnreliableApi',
|
|
98
|
+
execute: async () => {
|
|
99
|
+
// Simulate an API call that might fail
|
|
100
|
+
const random = Math.random();
|
|
101
|
+
if (random < 0.7) {
|
|
102
|
+
throw new Error('API call failed');
|
|
103
|
+
}
|
|
104
|
+
return { data: 'API response data' };
|
|
105
|
+
},
|
|
106
|
+
retryConfig: {
|
|
107
|
+
attempts: 3, // Retry up to 3 times
|
|
108
|
+
delay: 2000, // Wait 2 seconds between attempts
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Create a workflow with the unreliable step
|
|
113
|
+
const workflow = new Workflow({
|
|
114
|
+
name: 'retry-demo-workflow',
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
workflow
|
|
118
|
+
.step(unreliableApiStep)
|
|
119
|
+
.then(processResultStep)
|
|
120
|
+
.commit();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Workflow-level Retries with Step Override
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { Workflow, Step } from '@mastra/core/workflows';
|
|
127
|
+
|
|
128
|
+
// Create a workflow with default retry configuration
|
|
129
|
+
const workflow = new Workflow({
|
|
130
|
+
name: 'multi-retry-workflow',
|
|
131
|
+
retryConfig: {
|
|
132
|
+
attempts: 2, // All steps will retry twice by default
|
|
133
|
+
delay: 1000, // With a 1-second delay
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// This step uses the workflow's default retry configuration
|
|
138
|
+
const standardStep = new Step({
|
|
139
|
+
id: 'standardStep',
|
|
140
|
+
execute: async () => {
|
|
141
|
+
// Some operation that might fail
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// This step overrides the workflow's retry configuration
|
|
146
|
+
const criticalStep = new Step({
|
|
147
|
+
id: 'criticalStep',
|
|
148
|
+
execute: async () => {
|
|
149
|
+
// Critical operation that needs more retry attempts
|
|
150
|
+
},
|
|
151
|
+
retryConfig: {
|
|
152
|
+
attempts: 5, // Override with 5 retry attempts
|
|
153
|
+
delay: 5000, // And a longer 5-second delay
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// This step disables retries
|
|
158
|
+
const noRetryStep = new Step({
|
|
159
|
+
id: 'noRetryStep',
|
|
160
|
+
execute: async () => {
|
|
161
|
+
// Operation that should not retry
|
|
162
|
+
},
|
|
163
|
+
retryConfig: {
|
|
164
|
+
attempts: 0, // Explicitly disable retries
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
workflow
|
|
169
|
+
.step(standardStep)
|
|
170
|
+
.then(criticalStep)
|
|
171
|
+
.then(noRetryStep)
|
|
172
|
+
.commit();
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Monitoring Retries
|
|
176
|
+
|
|
177
|
+
You can monitor retry attempts in your logs. Mastra logs retry-related events at the `debug` level:
|
|
178
|
+
|
|
179
|
+
```
|
|
180
|
+
[DEBUG] Step fetchData failed (runId: abc-123)
|
|
181
|
+
[DEBUG] Attempt count for step fetchData: 2 remaining attempts (runId: abc-123)
|
|
182
|
+
[DEBUG] Step fetchData waiting (runId: abc-123)
|
|
183
|
+
[DEBUG] Step fetchData finished waiting (runId: abc-123)
|
|
184
|
+
[DEBUG] Step fetchData pending (runId: abc-123)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Best Practices
|
|
188
|
+
|
|
189
|
+
1. **Use Retries for Transient Failures**: Only configure retries for operations that might experience transient failures. For deterministic errors (like validation failures), retries won't help.
|
|
190
|
+
|
|
191
|
+
2. **Set Appropriate Delays**: Consider using longer delays for external API calls to allow time for services to recover.
|
|
192
|
+
|
|
193
|
+
3. **Limit Retry Attempts**: Don't set extremely high retry counts as this could cause workflows to run for excessive periods during outages.
|
|
194
|
+
|
|
195
|
+
4. **Implement Idempotent Operations**: Ensure your step's `execute` function is idempotent (can be called multiple times without side effects) since it may be retried.
|
|
196
|
+
|
|
197
|
+
5. **Consider Backoff Strategies**: For more advanced scenarios, consider implementing exponential backoff in your step's logic for operations that might be rate-limited.
|
|
198
|
+
|
|
199
|
+
## Related
|
|
200
|
+
|
|
201
|
+
- [Step Class Reference](./step-class.mdx)
|
|
202
|
+
- [Workflow Configuration](./workflow.mdx)
|
|
203
|
+
- [Error Handling in Workflows](../../workflows/error-handling.mdx)
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Voice in Mastra | Mastra Docs
|
|
3
|
+
description: Overview of voice capabilities in Mastra, including text-to-speech, speech-to-text, and real-time voice-to-voice interactions.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Voice in Mastra
|
|
7
|
+
|
|
8
|
+
Mastra's Voice system provides a unified interface for voice interactions, enabling text-to-speech (TTS), speech-to-text (STT), and real-time voice-to-voice capabilities in your applications.
|
|
9
|
+
|
|
10
|
+
## Key Features
|
|
11
|
+
|
|
12
|
+
- Standardized API across different voice providers
|
|
13
|
+
- Support for multiple voice services
|
|
14
|
+
- Voice-to-voice interactions using events for continuous audio streaming
|
|
15
|
+
- Composable voice providers for mixing TTS and STT services
|
|
16
|
+
|
|
17
|
+
## Adding Voice to Agents
|
|
18
|
+
|
|
19
|
+
To learn how to integrate voice capabilities into your agents, check out the [Adding Voice to Agents](../agents/03-adding-voice.mdx) documentation. This section covers how to use both single and multiple voice providers, as well as real-time interactions.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Example of Using a Single Voice Provider
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { OpenAIVoice } from "@mastra/voice-openai";
|
|
26
|
+
|
|
27
|
+
// Initialize OpenAI voice for TTS
|
|
28
|
+
const voice = new OpenAIVoice({
|
|
29
|
+
speechModel: {
|
|
30
|
+
name: "tts-1-hd", // Specify the TTS model
|
|
31
|
+
apiKey: process.env.OPENAI_API_KEY, // Your OpenAI API key
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Convert text to speech
|
|
36
|
+
const audioStream = await voice.speak("Hello! How can I assist you today?", {
|
|
37
|
+
speaker: "default", // Optional: specify a speaker
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Play the audio response
|
|
41
|
+
playAudio(audioStream);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Example of Using Multiple Voice Providers
|
|
45
|
+
This example demonstrates how to create and use two different voice providers in Mastra: OpenAI for speech-to-text (STT) and PlayAI for text-to-speech (TTS).
|
|
46
|
+
|
|
47
|
+
Start by creating instances of the voice providers with any necessary configuration.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { OpenAIVoice } from "@mastra/voice-openai";
|
|
51
|
+
import { PlayAIVoice } from "@mastra/voice-playai";
|
|
52
|
+
import { CompositeVoice } from "@mastra/core/voice";
|
|
53
|
+
|
|
54
|
+
// Initialize OpenAI voice for STT
|
|
55
|
+
const listeningProvider = new OpenAIVoice({
|
|
56
|
+
listeningModel: {
|
|
57
|
+
name: "whisper-1",
|
|
58
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Initialize PlayAI voice for TTS
|
|
63
|
+
const speakingProvider = new PlayAIVoice({
|
|
64
|
+
speechModel: {
|
|
65
|
+
name: "playai-voice",
|
|
66
|
+
apiKey: process.env.PLAYAI_API_KEY,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Combine the providers using CompositeVoice
|
|
71
|
+
const voice = new CompositeVoice({
|
|
72
|
+
listeningProvider,
|
|
73
|
+
speakingProvider,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Implement voice interactions using the combined voice provider
|
|
77
|
+
const audioStream = getMicrophoneStream(); // Assume this function gets audio input
|
|
78
|
+
const transcript = await voice.listen(audioStream);
|
|
79
|
+
|
|
80
|
+
// Log the transcribed text
|
|
81
|
+
console.log("Transcribed text:", transcript);
|
|
82
|
+
|
|
83
|
+
// Convert text to speech
|
|
84
|
+
const responseAudio = await voice.speak(`You said: ${transcript}`, {
|
|
85
|
+
speaker: "default", // Optional: specify a speaker
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Play the audio response
|
|
89
|
+
playAudio(responseAudio);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Real-time Capabilities
|
|
93
|
+
|
|
94
|
+
Many voice providers support real-time speech-to-speech interactions through WebSocket connections, enabling:
|
|
95
|
+
|
|
96
|
+
- Live voice conversations with AI
|
|
97
|
+
- Streaming transcription
|
|
98
|
+
- Real-time text-to-speech synthesis
|
|
99
|
+
- Tool usage during conversations
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
## Voice Configuration
|
|
103
|
+
|
|
104
|
+
Voice providers can be configured with different models and options:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
const voice = new OpenAIVoice({
|
|
108
|
+
speechModel: {
|
|
109
|
+
name: "tts-1-hd",
|
|
110
|
+
apiKey: process.env.OPENAI_API_KEY
|
|
111
|
+
},
|
|
112
|
+
listeningModel: {
|
|
113
|
+
name: "whisper-1"
|
|
114
|
+
},
|
|
115
|
+
speaker: "alloy"
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Available Voice Providers
|
|
120
|
+
|
|
121
|
+
Mastra supports a variety of voice providers, including:
|
|
122
|
+
|
|
123
|
+
- OpenAI
|
|
124
|
+
- PlayAI
|
|
125
|
+
- Murf
|
|
126
|
+
- ElevenLabs
|
|
127
|
+
- [More](https://github.com/mastra-ai/mastra/tree/main/voice)
|
|
128
|
+
|
|
129
|
+
## More Resources
|
|
130
|
+
|
|
131
|
+
- [CompositeVoice](../reference/voice/composite-voice.mdx)
|
|
132
|
+
- [MastraVoice](../reference/voice/mastra-voice.mdx)
|
|
133
|
+
- [OpenAI Voice](../reference/voice/openai.mdx)
|
|
134
|
+
- [PlayAI Voice](../reference/voice/playai.mdx)
|
|
135
|
+
- [Voice Examples](../../examples/voice/)
|