From 3170ee0ba21d48f3c7e453f2c849f42ad815a990 Mon Sep 17 00:00:00 2001 From: mudler Date: Thu, 11 Apr 2024 19:32:36 +0200 Subject: [PATCH] Add search action --- example/webui/agentpool.go | 19 +++++--- external/githubissuecloser.go | 4 +- external/githubissuesearch.go | 81 +++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 external/githubissuesearch.go diff --git a/example/webui/agentpool.go b/example/webui/agentpool.go index 287d4d0..f5d42f0 100644 --- a/example/webui/agentpool.go +++ b/example/webui/agentpool.go @@ -133,13 +133,20 @@ const ( ConnectorGithubIssues = "github-issues" // Actions - ActionSearch = "search" - ActionGithubIssueLabeler = "github-issue-labeler" - ActionGithubIssueOpener = "github-issue-opener" - ActionGithubIssueCloser = "github-issue-closer" + ActionSearch = "search" + ActionGithubIssueLabeler = "github-issue-labeler" + ActionGithubIssueOpener = "github-issue-opener" + ActionGithubIssueCloser = "github-issue-closer" + ActionGithubIssueSearcher = "github-issue-searcher" ) -var AvailableActions = []string{ActionSearch, ActionGithubIssueLabeler, ActionGithubIssueOpener, ActionGithubIssueCloser} +var AvailableActions = []string{ + ActionSearch, + ActionGithubIssueLabeler, + ActionGithubIssueOpener, + ActionGithubIssueCloser, + ActionGithubIssueSearcher, +} func (a *AgentConfig) availableActions(ctx context.Context) []Action { actions := []Action{} @@ -163,6 +170,8 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action { actions = append(actions, external.NewGithubIssueOpener(ctx, config)) case ActionGithubIssueCloser: actions = append(actions, external.NewGithubIssueCloser(ctx, config)) + case ActionGithubIssueSearcher: + actions = append(actions, external.NewGithubIssueSearch(ctx, config)) } } diff --git a/external/githubissuecloser.go b/external/githubissuecloser.go index 65b7597..77d89da 100644 --- a/external/githubissuecloser.go +++ b/external/githubissuecloser.go @@ -41,7 +41,7 @@ func (g *GithubIssuesCloser) Run(params action.ActionParams) (string, error) { // g.context, // result.Owner, result.Repository, // result.IssueNumber, &github.IssueComment{ - // Body: &result.Text, + // //Body: &result.Text, // }, // ) // if err != nil { @@ -74,7 +74,7 @@ func (g *GithubIssuesCloser) Definition() action.ActionDefinition { Properties: map[string]jsonschema.Definition{ "repository": { Type: jsonschema.String, - Description: "The repository to add the label to.", + Description: "The repository to close the issue in.", }, "owner": { Type: jsonschema.String, diff --git a/external/githubissuesearch.go b/external/githubissuesearch.go new file mode 100644 index 0000000..7cbab4f --- /dev/null +++ b/external/githubissuesearch.go @@ -0,0 +1,81 @@ +package external + +import ( + "context" + "fmt" + + "github.com/google/go-github/v61/github" + "github.com/mudler/local-agent-framework/action" + "github.com/sashabaranov/go-openai/jsonschema" +) + +type GithubIssueSearch struct { + token string + context context.Context + client *github.Client +} + +func NewGithubIssueSearch(ctx context.Context, config map[string]string) *GithubIssueSearch { + client := github.NewClient(nil).WithAuthToken(config["token"]) + + return &GithubIssueSearch{ + client: client, + token: config["token"], + context: ctx, + } +} + +func (g *GithubIssueSearch) Run(params action.ActionParams) (string, error) { + result := struct { + Query string `json:"query"` + Repository string `json:"repository"` + Owner string `json:"owner"` + }{} + err := params.Unmarshal(&result) + if err != nil { + fmt.Printf("error: %v", err) + + return "", err + } + + query := fmt.Sprintf("%s in:%s user:%s", result.Query, result.Repository, result.Owner) + resultString := "" + issues, _, err := g.client.Search.Issues(g.context, query, &github.SearchOptions{ + ListOptions: github.ListOptions{PerPage: 5}, + Order: "desc", + //Sort: "created", + }) + if err != nil { + resultString = fmt.Sprintf("Error listing issues: %v", err) + } + for _, i := range issues.Issues { + fmt.Println("Issue found:", i.GetTitle()) + resultString += fmt.Sprintf("Issue found: %s\n", i.GetTitle()) + resultString += fmt.Sprintf("URL: %s\n", i.GetHTMLURL()) + // resultString += fmt.Sprintf("Body: %s\n", i.GetBody()) + } + + return resultString, err +} + +func (g *GithubIssueSearch) Definition() action.ActionDefinition { + return action.ActionDefinition{ + Name: "search_github_issue", + Description: "Search between github issues", + Properties: map[string]jsonschema.Definition{ + "query": { + Type: jsonschema.String, + Description: "The text to search for", + }, + "repository": { + Type: jsonschema.String, + Description: "The repository to search in", + }, + "owner": { + Type: jsonschema.String, + Description: "The owner of the repository", + }, + }, + Required: []string{"text", "repository", "owner"}, + } +}