Execute 顺序执行所有子 Agent(仅一次)
(ctx context.Context, message string)
| 66 | |
| 67 | // Execute 顺序执行所有子 Agent(仅一次) |
| 68 | func (a *SequentialAgent) Execute(ctx context.Context, message string) *stream.Reader[*session.Event] { |
| 69 | reader, writer := stream.Pipe[*session.Event](10) |
| 70 | |
| 71 | go func() { |
| 72 | defer writer.Close() |
| 73 | |
| 74 | // 顺序执行所有子 Agent |
| 75 | for i, subAgent := range a.subAgents { |
| 76 | branch := fmt.Sprintf("%s.%s", a.name, subAgent.Name()) |
| 77 | |
| 78 | subReader := subAgent.Execute(ctx, message) |
| 79 | for { |
| 80 | event, err := subReader.Recv() |
| 81 | if err != nil { |
| 82 | if errors.Is(err, io.EOF) { |
| 83 | break |
| 84 | } |
| 85 | // 传递错误 |
| 86 | writer.Send(nil, err) |
| 87 | return // 遇到错误停止 |
| 88 | } |
| 89 | |
| 90 | // 丰富事件信息 |
| 91 | enrichedEvent := a.enrichSequentialEvent(event, branch, i) |
| 92 | |
| 93 | // 传递事件 |
| 94 | if writer.Send(enrichedEvent, nil) { |
| 95 | return // 客户端取消 |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // 检查上下文取消 |
| 100 | if ctx.Err() != nil { |
| 101 | writer.Send(nil, ctx.Err()) |
| 102 | return |
| 103 | } |
| 104 | } |
| 105 | }() |
| 106 | |
| 107 | return reader |
| 108 | } |
| 109 | |
| 110 | // enrichSequentialEvent 丰富顺序执行事件信息 |
| 111 | func (a *SequentialAgent) enrichSequentialEvent(event *session.Event, branch string, index int) *session.Event { |