Skip to content

Commit

Permalink
Added get tools params
Browse files Browse the repository at this point in the history
  • Loading branch information
conneroisu committed Oct 22, 2024
1 parent 26e913d commit 22bda71
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 27 deletions.
1 change: 0 additions & 1 deletion extensions/composio/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# composio

Compose AI is a powerful tool for creating complex and high-quality compositions of ai tools. This package provides a client for the composio api easily accessible through the groq-go library.

104 changes: 81 additions & 23 deletions extensions/composio/composio.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,48 @@ type (
}
// ComposerOption is an option for the composio client.
ComposerOption func(*Composio)
// Tool represents a composio tool.
Tool struct {
groq.Tool
Enum string `json:"enum"`
Tags []string `json:"tags"`
Logo string `json:"logo"`
AppID string `json:"appId"`
AppName string `json:"appName"`
DisplayName string `json:"displayName"`
Response struct {
Properties struct {
Data struct {
Title string `json:"title"`
Type string `json:"type"`
} `json:"data"`
Successful struct {
Description string `json:"description"`
Title string `json:"title"`
Type string `json:"type"`
} `json:"successful"`
Error struct {
AnyOf []struct {
Type string `json:"type"`
} `json:"anyOf"`
Default any `json:"default"`
Description string `json:"description"`
Title string `json:"title"`
} `json:"error"`
} `json:"properties"`
Required []string `json:"required"`
Title string `json:"title"`
Type string `json:"type"`
} `json:"response"`
Deprecated bool `json:"deprecated"`
}
// ToolsParams represents the parameters for the tools request.
ToolsParams struct {
App string `url:"appNames"`
Tags string `url:"tags"`
EntityID string `url:"user_uuid"`
UseCase string `url:"useCase"`
}
)

// NewComposer creates a new composio client.
Expand All @@ -53,15 +95,44 @@ func NewComposer(apiKey string, opts ...ComposerOption) (*Composio, error) {
for _, opt := range opts {
opt(c)
}
if c.client == nil {
c.client = &http.Client{}
}
if c.logger == nil {
c.logger = slog.Default()
}
return c, nil
}

// GetTools returns the tools for the composio client.
func (c *Composio) GetTools(params ToolsParams) ([]Tool, error) {
url := fmt.Sprintf("%s/actions", c.baseURL)
if params.App != "" {
url = fmt.Sprintf("%s?appNames=%s", url, params.App)
}
if params.Tags != "" {
url = fmt.Sprintf("%s?tags=%s", url, params.Tags)
}
if params.EntityID != "" {
url = fmt.Sprintf("%s?user_uuid=%s", url, params.EntityID)
}
if params.UseCase != "" {
url = fmt.Sprintf("%s?useCase=%s", url, params.UseCase)
}
req, err := builders.NewRequest(
context.Background(),
c.header,
http.MethodGet,
url,
builders.WithBody(nil),
)
if err != nil {
return nil, err
}
var tools struct {
Tools []Tool `json:"items"`
}
err = c.doRequest(req, &tools)
if err != nil {
return nil, err
}
c.logger.Debug("tools", "toolslen", len(tools.Tools))
return tools.Tools, nil
}
func (c *Composio) doRequest(req *http.Request, v interface{}) error {
req.Header.Set("Accept", "application/json")
contentType := req.Header.Get("Content-Type")
Expand Down Expand Up @@ -93,22 +164,9 @@ func (c *Composio) doRequest(req *http.Request, v interface{}) error {
}
}

// GetTools returns the tools for the composio client.
func (c *Composio) GetTools() ([]groq.Tool, error) {
req, err := builders.NewRequest(
context.Background(),
c.header,
http.MethodGet,
fmt.Sprintf("%s/actions", c.baseURL),
builders.WithBody(nil),
)
if err != nil {
return nil, err
}
var tools []groq.Tool
err = c.doRequest(req, &tools)
if err != nil {
return nil, err
// WithLogger sets the logger for the composio client.
func WithLogger(logger *slog.Logger) ComposerOption {

Check warning on line 168 in extensions/composio/composio.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 26 lines is too similar to extensions/e2b/sandbox.go:566
return func(c *Composio) {
c.logger = logger
}
return tools, nil
}

Check warning on line 172 in extensions/composio/composio.go

View check run for this annotation

Codeac.io / Codeac Code Quality

package-comments

should have a package comment
27 changes: 27 additions & 0 deletions extensions/composio/composio_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
package composio

import (
"testing"

"github.com/conneroisu/groq-go/pkg/test"
"github.com/stretchr/testify/assert"
)

// TestGetTools tests the ability of the composio client to get tools.
func TestGetTools(t *testing.T) {
if !test.IsUnitTest() {
t.Skip()
}
a := assert.New(t)
key, err := test.GetAPIKey("COMPOSIO_API_KEY")
a.NoError(err)
client, err := NewComposer(
key,
WithLogger(test.DefaultLogger),
)
a.NoError(err)
ts, err := client.GetTools(ToolsParams{
Tags: "Authentication",
})
a.NoError(err)
a.NotEmpty(ts)
}
7 changes: 4 additions & 3 deletions extensions/composio/test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
curl --request GET \
--url https://backend.composio.dev/api/v2/actions \
--header 'X-API-Key: awcv1natkyr6336c1x2f8q' > composio.json
echo "APIKEY: $COMPOSIO_API_KEY"
apikey=$COMPOSIO_API_KEY
curl --request GET --url https://backend.composio.dev/api/v2/actions?tags=Authentication --header 'X-API-Key: '$apikey \
> composio.json
34 changes: 34 additions & 0 deletions pkg/test/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package test

import (
"fmt"
"log/slog"
"net/http"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -60,3 +63,34 @@ func (t *TokenRoundTripper) RoundTrip(
func IsUnitTest() bool {
return os.Getenv("UNIT") != ""
}

// GetAPIKey returns the api key.
func GetAPIKey(key string) (string, error) {
apiKey := os.Getenv(key)
if apiKey == "" {
return "", fmt.Errorf("api key is required")
}
return apiKey, nil
}

// DefaultLogger is a default logger.
var DefaultLogger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == "time" {
return slog.Attr{}
}
if a.Key == "level" {
return slog.Attr{}
}
if a.Key == slog.SourceKey {
str := a.Value.String()

Check warning on line 88 in pkg/test/helpers.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 11 lines is too similar to extensions/toolhouse/toolhouse_test.go:16
split := strings.Split(str, "/")
if len(split) > 2 {
a.Value = slog.StringValue(strings.Join(split[len(split)-2:], "/"))
a.Value = slog.StringValue(strings.Replace(a.Value.String(), "}", "", -1))
}

Check warning on line 93 in pkg/test/helpers.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 6 lines is too similar to extensions/toolhouse/toolhouse_test.go:26
}
return a
}}))

0 comments on commit 22bda71

Please sign in to comment.