Basic Workflow Example
A simple 3-step workflow demonstrating MDWR fundamentals without memory integration - perfect for learning the basics.
Overview
This example shows:
- Step definition and chaining
- Decision structure
- State management between steps
- Conditional execution
Code
import { Workflow } from "@mdwr/sdk";
const basicWorkflow = Workflow.create("basic_v1")
.step("validate_input", async (ctx) => {
// Step 1: Validate input data
const { userId, amount } = ctx.input;
if (!userId || !amount) {
return {
decision: "reject",
reason: "Missing required fields: userId and amount",
confidence: 1.0,
};
}
if (amount <= 0) {
return {
decision: "reject",
reason: "Amount must be greater than zero",
confidence: 1.0,
};
}
return {
decision: "approve",
reason: "Input validation passed",
confidence: 1.0,
metadata: {
userId,
amount,
},
};
}, true)
.step("process_data", async (ctx) => {
// Step 2: Process the validated data
// Only runs if previous step approved
if (ctx.state.validate_input?.decision !== "approve") {
return {
decision: "skip",
reason: "Skipping due to validation failure",
confidence: 1.0,
};
}
const amount = ctx.input.amount;
const processedAmount = amount * 1.1; // Example: add 10% fee
return {
decision: "completed",
reason: `Processed amount: $${amount} -> $${processedAmount.toFixed(2)}`,
confidence: 1.0,
metadata: {
originalAmount: amount,
processedAmount,
fee: processedAmount - amount,
},
};
}, true)
.step("finalize", async (ctx) => {
// Step 3: Finalize the workflow
// Only runs if processing completed
if (ctx.state.process_data?.decision !== "completed") {
return {
decision: "skip",
reason: "Skipping finalization due to processing failure",
confidence: 1.0,
};
}
const result = ctx.state.process_data.metadata;
return {
decision: "success",
reason: "Workflow completed successfully",
confidence: 1.0,
metadata: {
finalResult: result,
completedAt: new Date().toISOString(),
},
};
}, true);
export default basicWorkflow;
Running the Example
# Deploy the workflow
npx ts-node scripts/deploy.ts
# Execute with sample input
npx ts-node scripts/execute.ts basic_v1
Input Example
{
"userId": "user123",
"amount": 100
}
What This Demonstrates
- Step Chaining: Steps execute in order
- Decision Structure: Each step returns
decision,reason,confidence - State Access: Steps access previous step results via
ctx.state - Conditional Execution: Steps can skip based on previous decisions
Next Steps
- Refund Workflow - See memory integration
- Creating Workflows - Learn more patterns
- Examples Index - View all examples