From 5278727c51b07e58c823c939a01a1e12ddb914ed Mon Sep 17 00:00:00 2001 From: John Costa Date: Sat, 5 Apr 2025 14:58:38 +0100 Subject: [PATCH] feat(tools): return error to agent if any happened --- backend/agents/client/tools.go | 14 +++--- backend/agents/client/tools_test.go | 69 ++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/backend/agents/client/tools.go b/backend/agents/client/tools.go index 0e259ec..193fe92 100644 --- a/backend/agents/client/tools.go +++ b/backend/agents/client/tools.go @@ -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 diff --git a/backend/agents/client/tools_test.go b/backend/agents/client/tools_test.go index 35423ff..cb07ece 100644 --- a/backend/agents/client/tools_test.go +++ b/backend/agents/client/tools_test.go @@ -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", },