@mastra/core
Version:
52 lines (39 loc) • 1.93 kB
Markdown
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
# Workflows
Mastra discovers project-level workflows from `.ts` and `.js` files under `src/mastra/workflows/`. This convention isn't scoped to one agent: every file with a default export becomes a registered workflow, and the filename becomes the workflow key.
Use this page for the file-based convention. For steps, schemas, control flow, streaming, state, and workflow execution, see [Workflows overview](https://mastra.ai/docs/workflows/overview).
## Quickstart
Create a file under `src/mastra/workflows/`, build the workflow with [`createWorkflow()`](https://mastra.ai/reference/workflows/workflow), and default-export it.
```typescript
import { createStep, createWorkflow } from '@mastra/core/workflows'
import { z } from 'zod'
const collectInputs = createStep({
id: 'collect-inputs',
inputSchema: z.object({ topic: z.string() }),
outputSchema: z.object({ topic: z.string(), audience: z.string() }),
execute: async ({ inputData }) => ({
topic: inputData.topic,
audience: 'engineering',
}),
})
const writeSummary = createStep({
id: 'write-summary',
inputSchema: z.object({ topic: z.string(), audience: z.string() }),
outputSchema: z.object({ report: z.string() }),
execute: async ({ inputData }) => ({
report: `Daily report about ${inputData.topic} for ${inputData.audience}.`,
}),
})
const dailyReport = createWorkflow({
id: 'daily-report',
inputSchema: z.object({ topic: z.string() }),
outputSchema: z.object({ report: z.string() }),
})
.then(collectInputs)
.then(writeSummary)
.commit()
export default dailyReport
```
Mastra registers this workflow under the `daily-report` key.
## Precedence with code
File-based workflows merge with workflows registered in [`new Mastra()`](https://mastra.ai/reference/core/mastra-class). On a key collision, the code-registered workflow wins.