feat(tools): return error to agent if any happened

This commit is contained in:
2025-04-05 14:58:38 +01:00
parent 1cafc31e0a
commit d78f34a7aa
2 changed files with 71 additions and 12 deletions

View File

@ -39,16 +39,20 @@ func (handler ToolsHandlers) Handle(info ToolHandlerInfo, toolCallMessage AgentA
}
res, err := fnHandler.Fn(info, arguments, toolCallMessage.ToolCalls[0])
if err != nil {
return []AgentTextMessage{}, err
}
responses[i] = AgentTextMessage{
responseMessage := AgentTextMessage{
Role: "tool",
Name: fnName,
Content: res,
ToolCallId: toolCall.Id,
}
if err != nil {
responseMessage.Content = err.Error()
} else {
responseMessage.Content = res
}
responses[i] = responseMessage
}
return responses, nil

View File

@ -1,6 +1,7 @@
package client
import (
"errors"
"testing"
"github.com/google/uuid"
@ -19,7 +20,11 @@ func (suite *ToolTestSuite) SetupTest() {
}
suite.handler.AddTool("a", func(info ToolHandlerInfo, args string, call ToolCall) (any, error) {
return true, nil
return args, nil
})
suite.handler.AddTool("error", func(info ToolHandlerInfo, args string, call ToolCall) (any, error) {
return false, errors.New("I will always error")
})
}
@ -40,7 +45,7 @@ func (suite *ToolTestSuite) TestSingleToolCall() {
Id: "1",
Function: FunctionCall{
Name: "a",
Arguments: "",
Arguments: "return",
},
}},
})
@ -49,7 +54,7 @@ func (suite *ToolTestSuite) TestSingleToolCall() {
assert.EqualValues(response, []AgentTextMessage{{
Role: "tool",
Content: "true",
Content: "\"return\"",
ToolCallId: "1",
Name: "a",
}})
@ -90,7 +95,7 @@ func (suite *ToolTestSuite) TestMultipleToolCalls() {
Id: "1",
Function: FunctionCall{
Name: "a",
Arguments: "",
Arguments: "first-call",
},
},
{
@ -98,7 +103,7 @@ func (suite *ToolTestSuite) TestMultipleToolCalls() {
Id: "2",
Function: FunctionCall{
Name: "a",
Arguments: "",
Arguments: "second-call",
},
},
},
@ -109,13 +114,63 @@ func (suite *ToolTestSuite) TestMultipleToolCalls() {
assert.EqualValues(response, []AgentTextMessage{
{
Role: "tool",
Content: "true",
Content: "\"first-call\"",
ToolCallId: "1",
Name: "a",
},
{
Role: "tool",
Content: "true",
Content: "\"second-call\"",
ToolCallId: "2",
Name: "a",
},
})
}
func (suite *ToolTestSuite) TestMultipleToolCallsWithErrors() {
assert := suite.Assert()
require := suite.Require()
response, err := suite.handler.Handle(
ToolHandlerInfo{
UserId: uuid.Nil,
ImageId: uuid.Nil,
},
AgentAssistantToolCall{
Role: "assistant",
Content: "",
ToolCalls: []ToolCall{
{
Index: 0,
Id: "1",
Function: FunctionCall{
Name: "error",
Arguments: "",
},
},
{
Index: 1,
Id: "2",
Function: FunctionCall{
Name: "a",
Arguments: "no-error",
},
},
},
})
require.NoError(err, "Tool call shouldnt return an error")
assert.EqualValues(response, []AgentTextMessage{
{
Role: "tool",
Content: "I will always error",
ToolCallId: "1",
Name: "error",
},
{
Role: "tool",
Content: "\"no-error\"",
ToolCallId: "2",
Name: "a",
},