测试 2: 所有步骤类型
(ctx context.Context)
| 78 | |
| 79 | // 测试 2: 所有步骤类型 |
| 80 | func testAllStepTypes(ctx context.Context) { |
| 81 | wf := workflow.New("AllSteps").WithStream() |
| 82 | |
| 83 | // FunctionStep |
| 84 | wf.AddStep(workflow.NewFunctionStep("function", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 85 | return &workflow.StepOutput{ |
| 86 | Content: map[string]any{"type": "function", "value": 1}, |
| 87 | Metadata: make(map[string]any), |
| 88 | }, nil |
| 89 | })) |
| 90 | |
| 91 | // ConditionStep |
| 92 | trueStep := workflow.NewFunctionStep("true", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 93 | return &workflow.StepOutput{Content: "条件为真", Metadata: make(map[string]any)}, nil |
| 94 | }) |
| 95 | falseStep := workflow.NewFunctionStep("false", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 96 | return &workflow.StepOutput{Content: "条件为假", Metadata: make(map[string]any)}, nil |
| 97 | }) |
| 98 | condStep := workflow.NewConditionStep("condition", func(input *workflow.StepInput) bool { |
| 99 | if m, ok := input.PreviousStepContent.(map[string]any); ok { |
| 100 | if v, ok := m["value"].(int); ok { |
| 101 | return v > 0 |
| 102 | } |
| 103 | } |
| 104 | return false |
| 105 | }, trueStep, falseStep) |
| 106 | wf.AddStep(condStep) |
| 107 | |
| 108 | // LoopStep |
| 109 | loopBody := workflow.NewFunctionStep("loop_body", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 110 | return &workflow.StepOutput{Content: "循环迭代", Metadata: make(map[string]any)}, nil |
| 111 | }) |
| 112 | wf.AddStep(workflow.NewLoopStep("loop", loopBody, 2)) |
| 113 | |
| 114 | // ParallelStep |
| 115 | task1 := workflow.NewFunctionStep("task1", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 116 | time.Sleep(5 * time.Millisecond) |
| 117 | return &workflow.StepOutput{Content: "任务1", Metadata: make(map[string]any)}, nil |
| 118 | }) |
| 119 | task2 := workflow.NewFunctionStep("task2", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 120 | time.Sleep(5 * time.Millisecond) |
| 121 | return &workflow.StepOutput{Content: "任务2", Metadata: make(map[string]any)}, nil |
| 122 | }) |
| 123 | wf.AddStep(workflow.NewParallelStep("parallel", task1, task2)) |
| 124 | |
| 125 | // StepsGroup |
| 126 | groupStep1 := workflow.NewFunctionStep("g1", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 127 | return &workflow.StepOutput{Content: "组步骤1", Metadata: make(map[string]any)}, nil |
| 128 | }) |
| 129 | groupStep2 := workflow.NewFunctionStep("g2", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 130 | return &workflow.StepOutput{Content: "组步骤2", Metadata: make(map[string]any)}, nil |
| 131 | }) |
| 132 | wf.AddStep(workflow.NewStepsGroup("group", groupStep1, groupStep2)) |
| 133 | |
| 134 | input := &workflow.WorkflowInput{Input: "测试所有类型"} |
| 135 | stepCount := 0 |
| 136 | reader := wf.Execute(ctx, input) |
| 137 | for { |
no test coverage detected