Skip to content

Commit

Permalink
feat: add tool package
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst committed Aug 18, 2024
1 parent 30c7203 commit ca354ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pkg/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func LoadTool(file string) (*Tool, error) {
}

// LoadTools loads a list of tools from a directory
func LoadTools(dir string) ([]*Tool, error) {
func LoadTools(dir string) (map[string]*Tool, error) {
// Get the list of json files in the directory
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
Expand All @@ -61,7 +61,7 @@ func LoadTools(dir string) ([]*Tool, error) {
}

// Load the tools concurrently, pool of 10 goroutines max
var tools []*Tool
tools := make(map[string]*Tool, len(files))
lock := sync.Mutex{}
eg := new(errgroup.Group)
eg.SetLimit(10)
Expand All @@ -75,7 +75,7 @@ func LoadTools(dir string) ([]*Tool, error) {

lock.Lock()
defer lock.Unlock()
tools = append(tools, tool)
tools[tool.Function.Name] = tool
return nil
})
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/tool/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func TestLoadTools(t *testing.T) {
t.Run("nominal", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
tools := []*Tool{
{
tools := map[string]*Tool{
"echo": {
Cmd: "echo",
Args: []string{"Hello, World!"},
Tool: api.Tool{
Expand All @@ -112,25 +112,24 @@ func TestLoadTools(t *testing.T) {
},
},
},
{
Cmd: "echo",
Args: []string{"Goodbye, World!"},
"cat": {
Cmd: "cat",
Args: []string{"file.txt"},
Tool: api.Tool{
Type: "function",
Function: api.ToolFunction{
Name: "echo",
Description: "Echoes a message",
Name: "cat",
Description: "Cats a file",
},
},
},
}
for i, tool := range tools {
for _, tool := range tools {
f, err := os.CreateTemp(dir, "*_tool.json")
require.NoError(t, err)
defer f.Close()
err = json.NewEncoder(f).Encode(tool)
require.NoError(t, err)
tools[i] = tool
}

actual, err := LoadTools(dir)
Expand Down

0 comments on commit ca354ee

Please sign in to comment.