UNPKG

@flowlab/all

Version:

A cool library focusing on handling various flows

90 lines (78 loc) 3.49 kB
import { FlowLabEngine, BaseNode, INodeContext } from '../main/src'; // Adjust import path // --- 1. (可选) 定义自定义节点 --- class MyDataNode extends BaseNode { readonly metadata = { id: 'my-data-node', description: 'Processes data' }; async execute(context: INodeContext): Promise<void> { context.log(`Processing data for: ${context.input.userId}`); const result = context.input.value * 2; // Set output on context context.output = { processedValue: result }; // Set a workflow variable context.setVariable('lastResult', result); context.log('Processing complete.'); } } // --- 2. 创建和配置引擎 --- const engine = new FlowLabEngine( // persistence: new MemoryPersistence(), // 可选 ); // --- 3. 注册节点 --- // 注册类实例 engine.registerNode(new MyDataNode()); // 注册简单函数 engine.registerNode('simple-log', async (context) => { console.log('[SIMPLE LOG]:', context.input.message, 'Var:', context.getVariable('lastResult')); }); // --- 4. 定义工作流 --- const definition = engine.defineWorkflow('my-processing-flow', 'My Data Processing') .addStep({ id: 'step1', nodeId: 'my-data-node', inputMapping: { // Map workflow input to node input 'userId': 'input.user', 'value': 'input.initialValue' }, outputMapping: { // Map node output back to variables 'variables.step1Output': 'output.processedValue' }, retryOptions: { maxRetries: 1, delayMs: 100 }, nextStepId: 'step2' }) .addStep({ id: 'step2', nodeId: 'simple-log', inputMapping: { 'message': 'variables.step1Output' // Use output from step1 (stored in variable) } // No nextStepId, this is the end }) .setStartStep('step1'); // (可选) 显式注册定义,如果需要通过 ID 执行或持久化 engine.registerDefinition(definition); // --- 5. 执行工作流 --- async function main() { const executor = engine.createExecutor(); const initialData = { user: 'user-abc', initialValue: 10 }; console.log('\n--- Running workflow by Definition Instance ---'); const result1 = await executor.run(definition, initialData, { tenantId: 'tenant-1' }); console.log('Workflow 1 Status:', result1.status); console.log('Workflow 1 Variables:', result1.variables); console.log('\n--- Running workflow by ID ---'); // Ensure definition is registered or loaded if using ID const result2 = await executor.run('my-processing-flow', { user: 'user-xyz', initialValue: 5 }, { userId: 'runner-007' }); console.log('Workflow 2 Status:', result2.status); console.log('Workflow 2 Variables:', result2.variables); // --- 5b. 使用快捷方式运行简单流程 --- console.log('\n--- Running simple workflow using runWorkflow ---'); const simpleResult = await engine.runWorkflow( (wf) => { // Define workflow inline wf.addStep({ id: 'log1', nodeId: 'simple-log', input: { message: 'Inline Step 1' }, nextStepId: 'log2' }) .addStep({ id: 'log2', nodeId: 'simple-log', input: { message: 'Inline Step 2' } }) .setStartStep('log1'); }, {}, // No specific initial input needed for this simple one { traceId: 'simple-trace' } ); console.log('Simple Workflow Status:', simpleResult.status); } main().catch(console.error);