fix(tools): dont error if AI invested a tool

This commit is contained in:
2025-04-05 15:04:09 +01:00
parent e101070851
commit 5c5df168ad
2 changed files with 26 additions and 8 deletions

View File

@ -22,6 +22,8 @@ type ToolsHandlers struct {
var NoToolCallError = errors.New("An assistant tool call with no tool calls was provided.")
const NonExistantTool = "This tool does not exist"
func (handler ToolsHandlers) Handle(info ToolHandlerInfo, toolCallMessage AgentAssistantToolCall) ([]AgentTextMessage, error) {
if len(toolCallMessage.ToolCalls) == 0 {
return []AgentTextMessage{}, NoToolCallError
@ -33,19 +35,21 @@ func (handler ToolsHandlers) Handle(info ToolHandlerInfo, toolCallMessage AgentA
fnName := toolCall.Function.Name
arguments := toolCall.Function.Arguments
fnHandler, exists := handler.handlers[fnName]
if !exists {
return []AgentTextMessage{}, errors.New("Could not find tool with this name.")
}
res, err := fnHandler.Fn(info, arguments, toolCallMessage.ToolCalls[0])
responseMessage := AgentTextMessage{
Role: "tool",
Name: fnName,
ToolCallId: toolCall.Id,
}
fnHandler, exists := handler.handlers[fnName]
if !exists {
responseMessage.Content = NonExistantTool
responses[i] = responseMessage
continue
}
res, err := fnHandler.Fn(info, arguments, toolCallMessage.ToolCalls[0])
if err != nil {
responseMessage.Content = err.Error()
} else {

View File

@ -151,6 +151,14 @@ func (suite *ToolTestSuite) TestMultipleToolCallsWithErrors() {
{
Index: 1,
Id: "2",
Function: FunctionCall{
Name: "non-existant",
Arguments: "",
},
},
{
Index: 2,
Id: "3",
Function: FunctionCall{
Name: "a",
Arguments: "no-error",
@ -170,8 +178,14 @@ func (suite *ToolTestSuite) TestMultipleToolCallsWithErrors() {
},
{
Role: "tool",
Content: "\"no-error\"",
Content: "This tool does not exist",
ToolCallId: "2",
Name: "non-existant",
},
{
Role: "tool",
Content: "\"no-error\"",
ToolCallId: "3",
Name: "a",
},
})