Skip to content

Commit 1eef3d0

Browse files
fix: run tools in order (#48)
* fix: run tools in order * fix(lint): remove not needed append
1 parent 335c92e commit 1eef3d0

File tree

1 file changed

+83
-111
lines changed

1 file changed

+83
-111
lines changed

agent.go

Lines changed: 83 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"maps"
1010
"slices"
11-
"sync"
1211
)
1312

1413
// StepResult represents the result of a single step in an agent execution.
@@ -619,127 +618,100 @@ func (a *agent) executeTools(ctx context.Context, allTools []AgentTool, toolCall
619618
toolMap[tool.Info().Name] = tool
620619
}
621620

622-
// Execute all tool calls in parallel
623-
results := make([]ToolResultContent, len(toolCalls))
624-
executeErrors := make([]error, len(toolCalls))
625-
626-
var wg sync.WaitGroup
627-
628-
for i, toolCall := range toolCalls {
629-
wg.Add(1)
630-
go func(index int, call ToolCallContent) {
631-
defer wg.Done()
632-
633-
// Skip invalid tool calls - create error result
634-
if call.Invalid {
635-
results[index] = ToolResultContent{
636-
ToolCallID: call.ToolCallID,
637-
ToolName: call.ToolName,
638-
Result: ToolResultOutputContentError{
639-
Error: call.ValidationError,
640-
},
641-
ProviderExecuted: false,
642-
}
643-
if toolResultCallback != nil {
644-
err := toolResultCallback(results[index])
645-
if err != nil {
646-
executeErrors[index] = err
647-
}
648-
}
649-
650-
return
621+
// Execute all tool calls sequentially in order
622+
results := make([]ToolResultContent, 0, len(toolCalls))
623+
624+
for _, toolCall := range toolCalls {
625+
// Skip invalid tool calls - create error result
626+
if toolCall.Invalid {
627+
result := ToolResultContent{
628+
ToolCallID: toolCall.ToolCallID,
629+
ToolName: toolCall.ToolName,
630+
Result: ToolResultOutputContentError{
631+
Error: toolCall.ValidationError,
632+
},
633+
ProviderExecuted: false,
651634
}
652-
653-
tool, exists := toolMap[call.ToolName]
654-
if !exists {
655-
results[index] = ToolResultContent{
656-
ToolCallID: call.ToolCallID,
657-
ToolName: call.ToolName,
658-
Result: ToolResultOutputContentError{
659-
Error: errors.New("Error: Tool not found: " + call.ToolName),
660-
},
661-
ProviderExecuted: false,
635+
results = append(results, result)
636+
if toolResultCallback != nil {
637+
if err := toolResultCallback(result); err != nil {
638+
return nil, err
662639
}
663-
664-
if toolResultCallback != nil {
665-
err := toolResultCallback(results[index])
666-
if err != nil {
667-
executeErrors[index] = err
668-
}
669-
}
670-
return
671640
}
641+
continue
642+
}
672643

673-
// Execute the tool
674-
result, err := tool.Run(ctx, ToolCall{
675-
ID: call.ToolCallID,
676-
Name: call.ToolName,
677-
Input: call.Input,
678-
})
679-
if err != nil {
680-
results[index] = ToolResultContent{
681-
ToolCallID: call.ToolCallID,
682-
ToolName: call.ToolName,
683-
Result: ToolResultOutputContentError{
684-
Error: err,
685-
},
686-
ClientMetadata: result.Metadata,
687-
ProviderExecuted: false,
688-
}
689-
if toolResultCallback != nil {
690-
cbErr := toolResultCallback(results[index])
691-
if cbErr != nil {
692-
executeErrors[index] = cbErr
693-
}
694-
}
695-
executeErrors[index] = err
696-
return
644+
tool, exists := toolMap[toolCall.ToolName]
645+
if !exists {
646+
result := ToolResultContent{
647+
ToolCallID: toolCall.ToolCallID,
648+
ToolName: toolCall.ToolName,
649+
Result: ToolResultOutputContentError{
650+
Error: errors.New("Error: Tool not found: " + toolCall.ToolName),
651+
},
652+
ProviderExecuted: false,
697653
}
698-
699-
if result.IsError {
700-
results[index] = ToolResultContent{
701-
ToolCallID: call.ToolCallID,
702-
ToolName: call.ToolName,
703-
Result: ToolResultOutputContentError{
704-
Error: errors.New(result.Content),
705-
},
706-
ClientMetadata: result.Metadata,
707-
ProviderExecuted: false,
708-
}
709-
710-
if toolResultCallback != nil {
711-
err := toolResultCallback(results[index])
712-
if err != nil {
713-
executeErrors[index] = err
714-
}
715-
}
716-
} else {
717-
results[index] = ToolResultContent{
718-
ToolCallID: call.ToolCallID,
719-
ToolName: toolCall.ToolName,
720-
Result: ToolResultOutputContentText{
721-
Text: result.Content,
722-
},
723-
ClientMetadata: result.Metadata,
724-
ProviderExecuted: false,
725-
}
726-
if toolResultCallback != nil {
727-
err := toolResultCallback(results[index])
728-
if err != nil {
729-
executeErrors[index] = err
730-
}
654+
results = append(results, result)
655+
if toolResultCallback != nil {
656+
if err := toolResultCallback(result); err != nil {
657+
return nil, err
731658
}
732659
}
733-
}(i, toolCall)
734-
}
735-
736-
// Wait for all tool executions to complete
737-
wg.Wait()
660+
continue
661+
}
738662

739-
for _, err := range executeErrors {
663+
// Execute the tool
664+
toolResult, err := tool.Run(ctx, ToolCall{
665+
ID: toolCall.ToolCallID,
666+
Name: toolCall.ToolName,
667+
Input: toolCall.Input,
668+
})
740669
if err != nil {
670+
result := ToolResultContent{
671+
ToolCallID: toolCall.ToolCallID,
672+
ToolName: toolCall.ToolName,
673+
Result: ToolResultOutputContentError{
674+
Error: err,
675+
},
676+
ClientMetadata: toolResult.Metadata,
677+
ProviderExecuted: false,
678+
}
679+
if toolResultCallback != nil {
680+
if cbErr := toolResultCallback(result); cbErr != nil {
681+
return nil, cbErr
682+
}
683+
}
741684
return nil, err
742685
}
686+
687+
var result ToolResultContent
688+
if toolResult.IsError {
689+
result = ToolResultContent{
690+
ToolCallID: toolCall.ToolCallID,
691+
ToolName: toolCall.ToolName,
692+
Result: ToolResultOutputContentError{
693+
Error: errors.New(toolResult.Content),
694+
},
695+
ClientMetadata: toolResult.Metadata,
696+
ProviderExecuted: false,
697+
}
698+
} else {
699+
result = ToolResultContent{
700+
ToolCallID: toolCall.ToolCallID,
701+
ToolName: toolCall.ToolName,
702+
Result: ToolResultOutputContentText{
703+
Text: toolResult.Content,
704+
},
705+
ClientMetadata: toolResult.Metadata,
706+
ProviderExecuted: false,
707+
}
708+
}
709+
results = append(results, result)
710+
if toolResultCallback != nil {
711+
if err := toolResultCallback(result); err != nil {
712+
return nil, err
713+
}
714+
}
743715
}
744716

745717
return results, nil

0 commit comments

Comments
 (0)