Execute 循环执行子 Agent
(ctx context.Context, message string)
| 81 | |
| 82 | // Execute 循环执行子 Agent |
| 83 | func (a *LoopAgent) Execute(ctx context.Context, message string) *stream.Reader[*session.Event] { |
| 84 | reader, writer := stream.Pipe[*session.Event](10) |
| 85 | |
| 86 | go func() { |
| 87 | defer writer.Close() |
| 88 | |
| 89 | iteration := uint(0) |
| 90 | |
| 91 | for { |
| 92 | // 检查最大迭代次数 |
| 93 | if a.maxIterations > 0 { |
| 94 | if iteration >= a.maxIterations { |
| 95 | return |
| 96 | } |
| 97 | iteration++ |
| 98 | } |
| 99 | |
| 100 | // 顺序执行所有子 Agent |
| 101 | shouldExit := false |
| 102 | for i, subAgent := range a.subAgents { |
| 103 | branch := fmt.Sprintf("%s.%s.iter%d", a.name, subAgent.Name(), iteration) |
| 104 | |
| 105 | subReader := subAgent.Execute(ctx, message) |
| 106 | for { |
| 107 | event, err := subReader.Recv() |
| 108 | if err != nil { |
| 109 | if errors.Is(err, io.EOF) { |
| 110 | break |
| 111 | } |
| 112 | // 传递错误 |
| 113 | writer.Send(nil, err) |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | // 丰富事件信息 |
| 118 | enrichedEvent := a.enrichEvent(event, branch, iteration, i) |
| 119 | |
| 120 | // 传递事件 |
| 121 | if writer.Send(enrichedEvent, nil) { |
| 122 | return // 客户端取消 |
| 123 | } |
| 124 | |
| 125 | // 检查停止条件 |
| 126 | if a.shouldStop(enrichedEvent) { |
| 127 | shouldExit = true |
| 128 | break |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if shouldExit { |
| 133 | return |
| 134 | } |
| 135 | |
| 136 | // 检查上下文取消 |
| 137 | if ctx.Err() != nil { |
| 138 | writer.Send(nil, ctx.Err()) |
| 139 | return |
| 140 | } |
no test coverage detected