Skip to content

Instantly share code, notes, and snippets.

@cocodrino
Created April 25, 2025 01:35
Show Gist options
  • Save cocodrino/ec1db7ad81edb8dfd37f5df2f7f324d7 to your computer and use it in GitHub Desktop.
Save cocodrino/ec1db7ad81edb8dfd37f5df2f7f324d7 to your computer and use it in GitHub Desktop.
mastra agentic workflow sample
import { Mastra } from "@mastra/core";
import { Agent, Step, Workflow } from "@mastra/core/workflows";
import { z } from "zod";
import { openai } from "@ai-sdk/openai";
// 1. Definición de Agentes :cite[1]:cite[7]
const researcher = new Agent({
name: "Investigador",
instructions: "Investiga temas técnicos y devuelve 5 puntos clave en formato markdown",
model: openai("gpt-4o-mini"),
});
const summarizer = new Agent({
name: "Resumidor",
instructions: "Genera resúmenes concisos de 3 líneas en español",
model: openai("gpt-4o-mini"),
});
const translator = new Agent({
name: "Traductor",
instructions: "Traduce textos técnicos del inglés al español manteniendo el significado preciso",
model: openai("claude-3-opus"),
});
// 2. Definición de Pasos del Workflow :cite[1]:cite[7]
const researchStep = new Step({
id: "investigacion",
description: "Recopila información técnica",
inputSchema: z.object({
topic: z.string().describe("Tema de investigación"),
}),
execute: async ({ input }) => {
return researcher.generate([
{ role: "user", content: `Investiga sobre ${input.topic} y devuelve 5 puntos clave` }
]);
},
});
const summaryStep = new Step({
id: "resumen",
description: "Genera un resumen ejecutivo",
inputSchema: z.object({
researchData: z.string(),
}),
execute: async ({ input }) => {
return summarizer.generate([
{ role: "user", content: `Resume este texto en 3 líneas: ${input.researchData}` }
]);
},
});
const translationStep = new Step({
id: "traduccion",
description: "Traduce el contenido al español",
inputSchema: z.object({
summaryText: z.string(),
}),
execute: async ({ input }) => {
return translator.generate([
{ role: "user", content: `Traduce a español: ${input.summaryText}` }
]);
},
});
// 3. Configuración del Workflow :cite[1]:cite[3]
const workflow = new Workflow({
name: "workflow-investigacion",
triggerSchema: z.object({
topic: z.string().describe("Tema principal para investigación"),
}),
})
.step(researchStep)
.then(summaryStep)
.then(translationStep)
.commit();
// 4. Ejecución del Workflow :cite[1]:cite[7]
const mastra = new Mastra({
workflows: { workflow },
});
async function main() {
const result = await mastra.runWorkflow("workflow-investigacion", {
topic: "Agentic Workflows en Mastra",
});
console.log("Resultado Final:\n", result.translationStep.output);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment