@@ -44,9 +44,10 @@ type Agent struct {
4444// Result represents the final output of an agent after processing user input and executing any tool calls.
4545type Result struct {
4646 // Parts holds the multimodal content of the final assistant message.
47- Parts []llm.ContentPart
48- HandoffAgent * Agent
49- Summary * trace.RunSummary
47+ Parts []llm.ContentPart
48+ // HandoffAgents lists the agents that were handed work; more than one means fan-out.
49+ HandoffAgents []* Agent
50+ Summary * trace.RunSummary
5051}
5152
5253// TextContent returns the concatenation of all text parts in the result.
@@ -185,7 +186,7 @@ func (a *Agent) Run(ctx context.Context, parts ...llm.ContentPart) (result *Resu
185186 ctx = trace .WithTracer (ctx , a .tracer )
186187 ctx = trace .WithAgentName (ctx , a .name )
187188 stats := newRunStats (a .name )
188- handoffAgentName := ""
189+ var handoffAgentNames [] string
189190
190191 session , sessionIndex , err := a .prepareSession (ctx , parts , stats )
191192 if err != nil {
@@ -218,7 +219,7 @@ func (a *Agent) Run(ctx context.Context, parts ...llm.ContentPart) (result *Resu
218219 Timestamp : time .Now (),
219220 })
220221
221- summary := stats .summary (iteration , handoffAgentName , err )
222+ summary := stats .summary (iteration , handoffAgentNames , err )
222223 if result != nil {
223224 result .Summary = summary
224225 }
@@ -249,13 +250,15 @@ func (a *Agent) Run(ctx context.Context, parts ...llm.ContentPart) (result *Resu
249250 }
250251
251252 session = iterationResult .session
252- if iterationResult .handoffAgent != nil {
253- handoffAgentName = iterationResult .handoffAgent .Name ()
253+ if len (iterationResult .handoffAgents ) > 0 {
254+ for _ , ha := range iterationResult .handoffAgents {
255+ handoffAgentNames = append (handoffAgentNames , ha .Name ())
256+ }
254257 }
255258
256259 // If finalMessage is nil, it means the agent executed tool calls and needs to call the LLM again.
257260 if iterationResult .lastMessage != nil {
258- return & Result {Parts : iterationResult .lastMessage .Parts , HandoffAgent : iterationResult .handoffAgent }, nil
261+ return & Result {Parts : iterationResult .lastMessage .Parts , HandoffAgents : iterationResult .handoffAgents }, nil
259262 }
260263 }
261264}
@@ -307,9 +310,9 @@ func (a *Agent) saveSession(ctx context.Context, messages []llm.Message, session
307310
308311// agentIteration represents the result of one iteration of the agent loop.
309312type agentIteration struct {
310- session []llm.Message
311- lastMessage * llm.Message
312- handoffAgent * Agent
313+ session []llm.Message
314+ lastMessage * llm.Message
315+ handoffAgents [] * Agent
313316}
314317
315318// handleAgentIteration executes one iteration of the agent loop: it calls the LLM with the current messages,
@@ -345,26 +348,23 @@ func (a *Agent) handleAgentIteration(ctx context.Context, session []llm.Message,
345348 }
346349 wg .Wait ()
347350
348- // Append results in order; find the first handoff, if any.
349- var handoffAgent * Agent
351+ // Append results in order; collect all handoffs (fan-out when more than one).
352+ var handoffAgents []* Agent
353+ var handoffMsg * llm.Message
350354 for i , result := range results {
351355 session = append (session , * result )
352- if hAgent , ok := a .handoffs [toolCalls [i ].Function .Name ]; ok && handoffAgent == nil {
353- handoffAgent = hAgent
356+ if hAgent , ok := a .handoffs [toolCalls [i ].Function .Name ]; ok {
357+ handoffAgents = append (handoffAgents , hAgent )
358+ if handoffMsg == nil {
359+ handoffMsg = result
360+ }
354361 }
355362 }
356363
357- if handoffAgent != nil {
358- // All tool results are preserved in session. Return the handoff tool's
364+ if len ( handoffAgents ) > 0 {
365+ // All tool results are preserved in session. Return the first handoff tool's
359366 // result as lastMessage so callers receive it in Result.Parts.
360- var handoffMsg * llm.Message
361- for i , toolCall := range toolCalls {
362- if _ , ok := a .handoffs [toolCall .Function .Name ]; ok {
363- handoffMsg = results [i ]
364- break
365- }
366- }
367- return agentIteration {session : session , lastMessage : handoffMsg , handoffAgent : handoffAgent }, nil
367+ return agentIteration {session : session , lastMessage : handoffMsg , handoffAgents : handoffAgents }, nil
368368 }
369369
370370 return agentIteration {session : session }, nil
0 commit comments