测试 3: Router 路由
(ctx context.Context)
| 159 | |
| 160 | // 测试 3: Router 路由 |
| 161 | func testRouter(ctx context.Context) { |
| 162 | // 创建路由目标步骤 |
| 163 | routeA := workflow.NewFunctionStep("route_a", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 164 | return &workflow.StepOutput{Content: "路由A执行", Metadata: make(map[string]any)}, nil |
| 165 | }) |
| 166 | |
| 167 | routeB := workflow.NewFunctionStep("route_b", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 168 | return &workflow.StepOutput{Content: "路由B执行", Metadata: make(map[string]any)}, nil |
| 169 | }) |
| 170 | |
| 171 | finalStep := workflow.NewFunctionStep("final", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 172 | return &workflow.StepOutput{ |
| 173 | Content: fmt.Sprintf("最终结果: %v", input.PreviousStepContent), |
| 174 | Metadata: make(map[string]any), |
| 175 | }, nil |
| 176 | }) |
| 177 | |
| 178 | // 测试 SimpleRouter |
| 179 | fmt.Println(" 测试 SimpleRouter:") |
| 180 | simpleRouter := workflow.SimpleRouter("simple_router", |
| 181 | func(input *workflow.StepInput) string { |
| 182 | if inputStr, ok := input.Input.(string); ok { |
| 183 | if len(inputStr) > 10 { |
| 184 | return "route_a" |
| 185 | } |
| 186 | } |
| 187 | return "route_b" |
| 188 | }, |
| 189 | map[string]workflow.Step{ |
| 190 | "route_a": routeA, |
| 191 | "route_b": routeB, |
| 192 | }, |
| 193 | ) |
| 194 | |
| 195 | wf1 := workflow.New("SimpleRouterTest").AddStep(simpleRouter) |
| 196 | input1 := &workflow.WorkflowInput{Input: "short"} |
| 197 | |
| 198 | reader1 := wf1.Execute(ctx, input1) |
| 199 | for { |
| 200 | event, err := reader1.Recv() |
| 201 | if err != nil { |
| 202 | if errors.Is(err, io.EOF) { |
| 203 | break |
| 204 | } |
| 205 | fmt.Printf(" ❌ 错误: %v\n", err) |
| 206 | continue |
| 207 | } |
| 208 | if event.Type == workflow.EventWorkflowCompleted { |
| 209 | fmt.Printf(" ✅ SimpleRouter 完成: %v\n", |
| 210 | event.Data.(map[string]any)["output"]) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // 测试 ChainRouter |
| 215 | fmt.Println(" 测试 ChainRouter:") |
| 216 | chainRouter := workflow.ChainRouter("chain_router", |
| 217 | func(input *workflow.StepInput) []string { |
| 218 | return []string{"route_a", "final"} |
no test coverage detected