runSubAgent 运行单个子 Agent
(
ctx context.Context,
agent Agent,
branch string,
index int,
message string,
results chan<- result,
done <-chan struct{},
)
| 110 | |
| 111 | // runSubAgent 运行单个子 Agent |
| 112 | func (a *ParallelAgent) runSubAgent( |
| 113 | ctx context.Context, |
| 114 | agent Agent, |
| 115 | branch string, |
| 116 | index int, |
| 117 | message string, |
| 118 | results chan<- result, |
| 119 | done <-chan struct{}, |
| 120 | ) error { |
| 121 | agentReader := agent.Execute(ctx, message) |
| 122 | for { |
| 123 | event, err := agentReader.Recv() |
| 124 | if err != nil { |
| 125 | if errors.Is(err, io.EOF) { |
| 126 | break |
| 127 | } |
| 128 | select { |
| 129 | case <-done: |
| 130 | return nil |
| 131 | case results <- result{err: err}: |
| 132 | } |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | select { |
| 137 | case <-done: |
| 138 | return nil // 客户端取消 |
| 139 | case <-ctx.Done(): |
| 140 | select { |
| 141 | case <-done: |
| 142 | case results <- result{err: ctx.Err()}: |
| 143 | } |
| 144 | return ctx.Err() |
| 145 | case results <- result{ |
| 146 | event: a.enrichEvent(event, branch, index), |
| 147 | err: nil, |
| 148 | }: |
| 149 | } |
| 150 | } |
| 151 | return nil |
| 152 | } |
| 153 | |
| 154 | // enrichEvent 丰富事件信息,添加 branch 和元数据 |
| 155 | func (a *ParallelAgent) enrichEvent(event *session.Event, branch string, index int) *session.Event { |
no test coverage detected