Back to Mastra

Adding a Third Step

docs/src/course/04-workflows/09-adding-a-third-step.md

2025-12-181.8 KB
Original Source

Adding a Third Step

Let's extend your workflow by adding a third step that generates a summary of the content.

Creating the Summary Step

Add this new step to your workflow file:

typescript
const generateSummaryStep = createStep({
  id: 'generate-summary',
  description: 'Creates a summary of the content',
  inputSchema: z.object({
    content: z.string(),
    type: z.string(),
    wordCount: z.number(),
    metadata: z.object({
      readingTime: z.number(),
      difficulty: z.enum(['easy', 'medium', 'hard']),
      processedAt: z.string(),
    }),
  }),
  outputSchema: z.object({
    content: z.string(),
    type: z.string(),
    wordCount: z.number(),
    metadata: z.object({
      readingTime: z.number(),
      difficulty: z.enum(['easy', 'medium', 'hard']),
      processedAt: z.string(),
    }),
    summary: z.string(),
  }),
  execute: async ({ inputData }) => {
    const { content, type, wordCount, metadata } = inputData

    // Create a simple summary from first sentence
    const sentences = content.split(/[.!?]+/).filter(s => s.trim().length > 0)
    const firstSentence = sentences[0]?.trim() + '.'

    // Generate summary based on content length
    let summary = firstSentence
    if (wordCount > 50) {
      summary += ` This ${type} contains ${wordCount} words and takes approximately ${metadata.readingTime} minute(s) to read.`
    }

    console.log(`📝 Generated summary: ${summary.length} characters`)

    return {
      content,
      type,
      wordCount,
      metadata,
      summary,
    }
  },
})

Understanding the Pattern

Notice how this step:

  • Takes the output from the previous step as input
  • Adds new data (summary) while preserving existing data
  • Follows the same structure as other steps

Your third step is ready! Next, you'll update the workflow to include all three steps.