Skip to Content
ExamplesWorkflows (vNext)Using a Tool/Agent as a Step

Tool/Agent as a Workflow step ✅

This example demonstrates how to create and integrate a tool or an agent as a workflow step. Kastrax provides a createStep helper function which accepts either a step or agent and returns an object which satisfies the Step interface.

Define Interop Workflow ✅

Defines a workflow which takes an agent and tool as a step.

workflows/interop-workflow.ts
import { createWorkflow, createStep } from '@kastrax/core/workflows/vNext' import { weatherTool } from '../tools' import { weatherReporterAgent } from '../agents' import { z } from 'zod' const fetchWeather = createStep(weatherTool) const reportWeather = createStep(weatherReporterAgent) const weatherWorkflow = createWorkflow({ steps: [fetchWeather, reportWeather], id: 'weather-workflow', inputSchema: z.object({ location: z.string().describe('The city to get the weather for'), }), outputSchema: z.object({ text: z.string(), }), }) .then(fetchWeather) .then( createStep({ id: 'report-weather', inputSchema: fetchWeather.outputSchema, outputSchema: z.object({ text: z.string(), }), execute: async ({ inputData, kastrax }) => { const prompt = 'Forecast data: ' + JSON.stringify(inputData) const agent = kastrax.getAgent('weatherReporterAgent') const result = await agent.generate([ { role: 'user', content: prompt, }, ]) return { text: result.text } }, }) ) weatherWorkflow.commit() export { weatherWorkflow }

Register Workflow instance with Kastrax class ✅

Register the workflow with the kastrax instance.

index.ts
import { Kastrax } from '@kastrax/core/kastrax' import { createLogger } from '@kastrax/core/logger' import { weatherWorkflow } from './workflows' const kastrax = new Kastrax({ vnext_workflows: { weatherWorkflow, }, logger: createLogger({ name: 'Kastrax', level: 'info', }), }) export { kastrax }

Execute the workflow ✅

Here, we’ll get the weather workflow from the kastrax instance, then create a run and execute the created run with the required inputData.

exec.ts
import { kastrax } from "./" const workflow = kastrax.vnext_getWorkflow('weatherWorkflow') const run = workflow.createRun() const result = await run.start({ inputData: { location: "Lagos" } }) console.dir(result, { depth: null })
Last updated on