test(tools): starting test suite for tools
This commit is contained in:
@@ -266,7 +266,12 @@ func (client AgentClient) Process(info ToolHandlerInfo, request AgentRequestBody
|
||||
var err error
|
||||
|
||||
for {
|
||||
err = client.ToolHandler.Handle(info, &request)
|
||||
toolCall, ok := request.Messages[len(request.Messages)-1].(AgentAssistantToolCall)
|
||||
if !ok {
|
||||
return errors.New("Latest message isnt a tool call. TODO")
|
||||
}
|
||||
|
||||
_, err = client.ToolHandler.Handle(info, toolCall)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package client
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -21,37 +20,26 @@ type ToolsHandlers struct {
|
||||
handlers *map[string]ToolHandler
|
||||
}
|
||||
|
||||
func (handler ToolsHandlers) Handle(info ToolHandlerInfo, request *AgentRequestBody) error {
|
||||
agentMessage := request.Messages[len(request.Messages)-1]
|
||||
|
||||
toolCall, ok := agentMessage.(AgentAssistantToolCall)
|
||||
if !ok {
|
||||
return errors.New("Latest message was not a tool call.")
|
||||
}
|
||||
|
||||
fnName := toolCall.ToolCalls[0].Function.Name
|
||||
arguments := toolCall.ToolCalls[0].Function.Arguments
|
||||
func (handler ToolsHandlers) Handle(info ToolHandlerInfo, toolCallMessage AgentAssistantToolCall) (AgentTextMessage, error) {
|
||||
fnName := toolCallMessage.ToolCalls[0].Function.Name
|
||||
arguments := toolCallMessage.ToolCalls[0].Function.Arguments
|
||||
|
||||
fnHandler, exists := (*handler.handlers)[fnName]
|
||||
if !exists {
|
||||
return errors.New("Could not find tool with this name.")
|
||||
return AgentTextMessage{}, errors.New("Could not find tool with this name.")
|
||||
}
|
||||
|
||||
log.Printf("Calling: %s\n", fnName)
|
||||
res, err := fnHandler.Fn(info, arguments, toolCall.ToolCalls[0])
|
||||
res, err := fnHandler.Fn(info, arguments, toolCallMessage.ToolCalls[0])
|
||||
if err != nil {
|
||||
return err
|
||||
return AgentTextMessage{}, err
|
||||
}
|
||||
log.Println(res)
|
||||
|
||||
request.AddText(AgentTextMessage{
|
||||
return AgentTextMessage{
|
||||
Role: "tool",
|
||||
Name: fnName,
|
||||
Content: res,
|
||||
ToolCallId: toolCall.ToolCalls[0].Id,
|
||||
})
|
||||
|
||||
return nil
|
||||
ToolCallId: toolCallMessage.ToolCalls[0].Id,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (handler ToolsHandlers) AddTool(name string, fn func(info ToolHandlerInfo, args string, call ToolCall) (any, error)) {
|
||||
|
||||
47
backend/agents/client/tools_test.go
Normal file
47
backend/agents/client/tools_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSingleToolCall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tools := ToolsHandlers{
|
||||
handlers: &map[string]ToolHandler{},
|
||||
}
|
||||
|
||||
tools.AddTool("a", func(info ToolHandlerInfo, args string, call ToolCall) (any, error) {
|
||||
return true, nil
|
||||
})
|
||||
|
||||
response, err := tools.Handle(
|
||||
ToolHandlerInfo{
|
||||
UserId: uuid.Nil,
|
||||
ImageId: uuid.Nil,
|
||||
},
|
||||
AgentAssistantToolCall{
|
||||
Role: "assistant",
|
||||
Content: "",
|
||||
ToolCalls: []ToolCall{{
|
||||
Index: 0,
|
||||
Id: "1",
|
||||
Function: FunctionCall{
|
||||
Name: "a",
|
||||
Arguments: "",
|
||||
},
|
||||
}},
|
||||
})
|
||||
|
||||
if assert.NoError(err, "Tool call shouldnt return an error") {
|
||||
assert.EqualValues(response, AgentTextMessage{
|
||||
Role: "tool",
|
||||
Content: "true",
|
||||
ToolCallId: "1",
|
||||
Name: "a",
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user