测试 4: WorkflowAgent
(ctx context.Context)
| 246 | |
| 247 | // 测试 4: WorkflowAgent |
| 248 | func testWorkflowAgent(ctx context.Context) { |
| 249 | // 创建一个简单的 workflow |
| 250 | wf := workflow.New("AgentWorkflow") |
| 251 | |
| 252 | wf.AddStep(workflow.NewFunctionStep("process", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 253 | return &workflow.StepOutput{ |
| 254 | Content: fmt.Sprintf("处理完成: %v", input.Input), |
| 255 | Metadata: make(map[string]any), |
| 256 | }, nil |
| 257 | })) |
| 258 | |
| 259 | // 创建 WorkflowAgent |
| 260 | agent := workflow.NewWorkflowAgent("gpt-4", "", true, 5) |
| 261 | agent.AttachWorkflow(wf) |
| 262 | |
| 263 | fmt.Println(" 测试同步执行:") |
| 264 | result, err := agent.Run(ctx, "测试查询") |
| 265 | if err != nil { |
| 266 | fmt.Printf(" ❌ 错误: %v\n", err) |
| 267 | } else { |
| 268 | fmt.Printf(" ✅ 结果: %v\n", result) |
| 269 | } |
| 270 | |
| 271 | // 测试流式执行 |
| 272 | fmt.Println(" 测试流式执行:") |
| 273 | eventCount := 0 |
| 274 | for event := range agent.RunStream(ctx, "流式测试") { |
| 275 | eventCount++ |
| 276 | if event.Type == workflow.AgentEventComplete { |
| 277 | fmt.Printf(" ✅ 流式完成, 收到 %d 个事件\n", eventCount) |
| 278 | } |
| 279 | if event.Error != nil { |
| 280 | fmt.Printf(" ❌ 错误: %v\n", event.Error) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // 测试 AgenticExecute |
| 285 | fmt.Println(" 测试 AgenticExecute:") |
| 286 | result2, err := wf.AgenticExecute(ctx, agent, "Agentic 查询") |
| 287 | if err != nil { |
| 288 | fmt.Printf(" ❌ 错误: %v\n", err) |
| 289 | } else { |
| 290 | fmt.Printf(" ✅ 结果: %v\n", result2) |
| 291 | } |
| 292 | } |
no test coverage detected