Add search action

This commit is contained in:
mudler
2024-04-11 19:32:36 +02:00
parent 3295942e59
commit 3170ee0ba2
3 changed files with 97 additions and 7 deletions

View File

@@ -137,9 +137,16 @@ const (
ActionGithubIssueLabeler = "github-issue-labeler" ActionGithubIssueLabeler = "github-issue-labeler"
ActionGithubIssueOpener = "github-issue-opener" ActionGithubIssueOpener = "github-issue-opener"
ActionGithubIssueCloser = "github-issue-closer" 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 { func (a *AgentConfig) availableActions(ctx context.Context) []Action {
actions := []Action{} actions := []Action{}
@@ -163,6 +170,8 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action {
actions = append(actions, external.NewGithubIssueOpener(ctx, config)) actions = append(actions, external.NewGithubIssueOpener(ctx, config))
case ActionGithubIssueCloser: case ActionGithubIssueCloser:
actions = append(actions, external.NewGithubIssueCloser(ctx, config)) actions = append(actions, external.NewGithubIssueCloser(ctx, config))
case ActionGithubIssueSearcher:
actions = append(actions, external.NewGithubIssueSearch(ctx, config))
} }
} }

View File

@@ -41,7 +41,7 @@ func (g *GithubIssuesCloser) Run(params action.ActionParams) (string, error) {
// g.context, // g.context,
// result.Owner, result.Repository, // result.Owner, result.Repository,
// result.IssueNumber, &github.IssueComment{ // result.IssueNumber, &github.IssueComment{
// Body: &result.Text, // //Body: &result.Text,
// }, // },
// ) // )
// if err != nil { // if err != nil {
@@ -74,7 +74,7 @@ func (g *GithubIssuesCloser) Definition() action.ActionDefinition {
Properties: map[string]jsonschema.Definition{ Properties: map[string]jsonschema.Definition{
"repository": { "repository": {
Type: jsonschema.String, Type: jsonschema.String,
Description: "The repository to add the label to.", Description: "The repository to close the issue in.",
}, },
"owner": { "owner": {
Type: jsonschema.String, Type: jsonschema.String,

81
external/githubissuesearch.go vendored Normal file
View File

@@ -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"},
}
}