nestedExample 嵌套工作流示例
(ctx context.Context)
| 164 | |
| 165 | // nestedExample 嵌套工作流示例 |
| 166 | func nestedExample(ctx context.Context) { |
| 167 | // 第一步:并行收集多个数据源 |
| 168 | dataCollectors := []workflow.Agent{ |
| 169 | NewMockAgent("Source1", "数据源1"), |
| 170 | NewMockAgent("Source2", "数据源2"), |
| 171 | NewMockAgent("Source3", "数据源3"), |
| 172 | } |
| 173 | parallelCollector, _ := workflow.NewParallelAgent(workflow.ParallelConfig{ |
| 174 | Name: "ParallelCollector", |
| 175 | SubAgents: dataCollectors, |
| 176 | }) |
| 177 | |
| 178 | // 第二步:分析 |
| 179 | analyzer := NewMockAgent("Analyzer", "数据分析") |
| 180 | |
| 181 | // 第三步:报告 |
| 182 | reporter := NewMockAgent("Reporter", "生成报告") |
| 183 | |
| 184 | // 组合成顺序工作流 |
| 185 | sequential, err := workflow.NewSequentialAgent(workflow.SequentialConfig{ |
| 186 | Name: "NestedWorkflow", |
| 187 | SubAgents: []workflow.Agent{ |
| 188 | parallelCollector, // 并行收集 |
| 189 | analyzer, // 分析 |
| 190 | reporter, // 报告 |
| 191 | }, |
| 192 | }) |
| 193 | if err != nil { |
| 194 | log.Fatal(err) |
| 195 | } |
| 196 | |
| 197 | // 执行 |
| 198 | fmt.Println("开始嵌套工作流:") |
| 199 | reader := sequential.Execute(ctx, "综合数据分析") |
| 200 | for { |
| 201 | event, err := reader.Recv() |
| 202 | if err != nil { |
| 203 | if errors.Is(err, io.EOF) { |
| 204 | break |
| 205 | } |
| 206 | log.Printf("错误: %v", err) |
| 207 | break |
| 208 | } |
| 209 | printEvent(event) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // ============================================================ |
| 214 | // Mock Agent 实现(用于演示) |
no test coverage detected