|
| 1 | +# @kaibanjs/workflow Examples |
| 2 | + |
| 3 | +This directory contains comprehensive examples demonstrating the capabilities of the @kaibanjs/workflow engine. Each example focuses on specific features and patterns. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +All examples can be run using `npx tsx`: |
| 8 | + |
| 9 | +```bash |
| 10 | +# Run all examples |
| 11 | +npx tsx src/examples/basic-workflow-patterns.ts |
| 12 | +npx tsx src/examples/state-management-events.ts |
| 13 | +npx tsx src/examples/streaming-workflows.ts |
| 14 | +``` |
| 15 | + |
| 16 | +## Examples Overview |
| 17 | + |
| 18 | +### 1. [Basic Workflow Patterns](./basic-workflow-patterns.md) |
| 19 | + |
| 20 | +**File**: `basic-workflow-patterns.ts` |
| 21 | + |
| 22 | +Demonstrates the fundamental workflow patterns: |
| 23 | + |
| 24 | +- **Sequential Execution** - Steps executed one after another |
| 25 | +- **Parallel Execution** - Multiple steps executed simultaneously |
| 26 | +- **Conditional Branching** - Different paths based on conditions |
| 27 | +- **Foreach Loops** - Processing arrays with concurrency control |
| 28 | +- **Nested Workflows** - Workflows within workflows |
| 29 | + |
| 30 | +**Key Learning**: Understanding the core workflow building blocks and execution patterns. |
| 31 | + |
| 32 | +```bash |
| 33 | +npx tsx src/examples/basic-workflow-patterns.ts |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. [State Management and Events](./state-management-events.md) |
| 37 | + |
| 38 | +**File**: `state-management-events.ts` |
| 39 | + |
| 40 | +Shows how to monitor workflow execution in real-time: |
| 41 | + |
| 42 | +- **Real-time State Monitoring** - Track workflow and step status changes |
| 43 | +- **Event Subscription** - Subscribe to workflow events using `watch()` |
| 44 | +- **Suspend and Resume** - Handle interactive workflows that require user input |
| 45 | +- **Comprehensive Event Tracking** - Monitor all aspects of workflow execution |
| 46 | + |
| 47 | +**Key Learning**: Building reactive applications that respond to workflow state changes. |
| 48 | + |
| 49 | +```bash |
| 50 | +npx tsx src/examples/state-management-events.ts |
| 51 | +``` |
| 52 | + |
| 53 | +### 3. [Streaming Workflows](./streaming-workflows.md) |
| 54 | + |
| 55 | +**File**: `streaming-workflows.ts` |
| 56 | + |
| 57 | +Demonstrates streaming capabilities using ReadableStream: |
| 58 | + |
| 59 | +- **Basic Streaming** - Simple workflow execution with real-time event streaming |
| 60 | +- **Parallel Streaming** - Monitoring parallel step execution |
| 61 | +- **Conditional Streaming** - Streaming with conditional branching |
| 62 | +- **Suspendable Streaming** - Handling suspended workflows in streams |
| 63 | +- **Custom Stream Processing** - Processing streams with custom logic |
| 64 | + |
| 65 | +**Key Learning**: Building real-time applications with streaming workflow data. |
| 66 | + |
| 67 | +```bash |
| 68 | +npx tsx src/examples/streaming-workflows.ts |
| 69 | +``` |
| 70 | + |
| 71 | +## Example Progression |
| 72 | + |
| 73 | +The examples are designed to be consumed in order: |
| 74 | + |
| 75 | +1. **Start with Basic Patterns** - Learn the fundamental concepts |
| 76 | +2. **Move to State Management** - Understand monitoring and events |
| 77 | +3. **Explore Streaming** - Build real-time applications |
| 78 | + |
| 79 | +## Common Patterns Across Examples |
| 80 | + |
| 81 | +### Workflow Creation Pattern |
| 82 | + |
| 83 | +```typescript |
| 84 | +const workflow = createWorkflow({ |
| 85 | + id: 'my-workflow', |
| 86 | + inputSchema: z.object({ |
| 87 | + /* input schema */ |
| 88 | + }), |
| 89 | + outputSchema: z.object({ |
| 90 | + /* output schema */ |
| 91 | + }), |
| 92 | +}); |
| 93 | + |
| 94 | +workflow.then(step1).then(step2); |
| 95 | +workflow.commit(); |
| 96 | +``` |
| 97 | + |
| 98 | +### Step Creation Pattern |
| 99 | + |
| 100 | +```typescript |
| 101 | +const step = createStep({ |
| 102 | + id: 'my-step', |
| 103 | + inputSchema: z.object({ |
| 104 | + /* input schema */ |
| 105 | + }), |
| 106 | + outputSchema: z.object({ |
| 107 | + /* output schema */ |
| 108 | + }), |
| 109 | + execute: async ({ inputData }) => { |
| 110 | + // Step logic here |
| 111 | + return result; |
| 112 | + }, |
| 113 | +}); |
| 114 | +``` |
| 115 | + |
| 116 | +### Execution Pattern |
| 117 | + |
| 118 | +```typescript |
| 119 | +const run = workflow.createRun(); |
| 120 | +const result = await run.start({ |
| 121 | + inputData: { |
| 122 | + /* input data */ |
| 123 | + }, |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +### Monitoring Pattern |
| 128 | + |
| 129 | +```typescript |
| 130 | +const unsubscribe = run.watch((event) => { |
| 131 | + console.log('Event:', event); |
| 132 | +}); |
| 133 | +``` |
| 134 | + |
| 135 | +### Streaming Pattern |
| 136 | + |
| 137 | +```typescript |
| 138 | +const { stream, getWorkflowState } = run.stream({ |
| 139 | + inputData: { |
| 140 | + /* input data */ |
| 141 | + }, |
| 142 | +}); |
| 143 | +const events = await readStream(stream); |
| 144 | +const finalResult = await getWorkflowState(); |
| 145 | +``` |
| 146 | + |
| 147 | +## Key Concepts Demonstrated |
| 148 | + |
| 149 | +### 1. Type Safety |
| 150 | + |
| 151 | +- Zod schemas for input/output validation |
| 152 | +- TypeScript integration for compile-time safety |
| 153 | +- Runtime validation with detailed error messages |
| 154 | + |
| 155 | +### 2. Execution Patterns |
| 156 | + |
| 157 | +- Sequential execution with `.then()` |
| 158 | +- Parallel execution with `.parallel()` |
| 159 | +- Conditional execution with `.branch()` |
| 160 | +- Loop execution with `.foreach()`, `.dowhile()`, `.dountil()` |
| 161 | + |
| 162 | +### 3. Data Flow |
| 163 | + |
| 164 | +- Data transformation with `.map()` |
| 165 | +- Step result access with `getStepResult()` |
| 166 | +- Initial data access with `getInitData()` |
| 167 | + |
| 168 | +### 4. State Management |
| 169 | + |
| 170 | +- Real-time event subscription |
| 171 | +- Workflow and step status tracking |
| 172 | +- Suspend and resume functionality |
| 173 | + |
| 174 | +### 5. Streaming |
| 175 | + |
| 176 | +- ReadableStream integration |
| 177 | +- Real-time event processing |
| 178 | +- Custom stream handling |
| 179 | + |
| 180 | +## Expected Output Structure |
| 181 | + |
| 182 | +All examples follow a consistent output pattern: |
| 183 | + |
| 184 | +``` |
| 185 | +[timestamp] === EXAMPLE NAME === |
| 186 | +[timestamp] Starting workflow execution... |
| 187 | +[timestamp] Step execution logs... |
| 188 | +[timestamp] Workflow result: { status: "completed", result: {...} } |
| 189 | +[timestamp] === EXAMPLE COMPLETED === |
| 190 | +``` |
| 191 | + |
| 192 | +## Troubleshooting |
| 193 | + |
| 194 | +### Common Issues |
| 195 | + |
| 196 | +1. **TypeScript Errors**: Ensure you have the latest TypeScript version |
| 197 | +2. **Import Errors**: Check that the workflow package is properly installed |
| 198 | +3. **Schema Validation Errors**: Verify input data matches the defined schemas |
| 199 | +4. **Stream Reading Errors**: Always use try/finally to release stream readers |
| 200 | + |
| 201 | +### Debug Tips |
| 202 | + |
| 203 | +1. **Enable Verbose Logging**: Add console.log statements in step execute functions |
| 204 | +2. **Check Event Flow**: Use the watch() method to monitor all events |
| 205 | +3. **Validate Schemas**: Ensure input/output schemas are correctly defined |
| 206 | +4. **Test Step by Step**: Run individual steps to isolate issues |
| 207 | + |
| 208 | +## Next Steps |
| 209 | + |
| 210 | +After completing these examples: |
| 211 | + |
| 212 | +1. **Build Your Own Workflows** - Apply the patterns to your use cases |
| 213 | +2. **Explore Advanced Features** - Check the main documentation for more features |
| 214 | +3. **Integrate with Your Application** - Use workflows in your existing codebase |
| 215 | +4. **Contribute Examples** - Share your workflow patterns with the community |
| 216 | + |
| 217 | +## Additional Resources |
| 218 | + |
| 219 | +- [Main Documentation](../../README.md) - Complete API reference |
| 220 | +- [Test Files](../../run.test.ts) - Comprehensive test cases |
| 221 | +- [Type Definitions](../types.ts) - TypeScript type definitions |
| 222 | +- [Source Code](../workflow.ts) - Implementation details |
| 223 | + |
| 224 | +## Contributing |
| 225 | + |
| 226 | +To add new examples: |
| 227 | + |
| 228 | +1. Create a new TypeScript file in this directory |
| 229 | +2. Follow the existing naming and structure patterns |
| 230 | +3. Create a corresponding README.md file |
| 231 | +4. Update this main README.md to include the new example |
| 232 | +5. Ensure the example can be run with `npx tsx` |
| 233 | + |
| 234 | +## License |
| 235 | + |
| 236 | +MIT - Same as the main package |
0 commit comments