Add issue closer

This commit is contained in:
mudler
2024-04-11 17:05:41 +02:00
parent fa39ae93f1
commit 3295942e59
6 changed files with 143 additions and 27 deletions

View File

@@ -245,6 +245,8 @@ func (a *Agent) consumeJob(job *Job, role string) {
// return // return
} }
if len(results) != 0 {
formatResults := "" formatResults := ""
for _, r := range results { for _, r := range results {
formatResults += fmt.Sprintf("- %s \n", r) formatResults += fmt.Sprintf("- %s \n", r)
@@ -253,12 +255,18 @@ func (a *Agent) consumeJob(job *Job, role string) {
fmt.Println("Found similar strings in KB:") fmt.Println("Found similar strings in KB:")
fmt.Println(formatResults) fmt.Println(formatResults)
} }
a.currentConversation = append(a.currentConversation, // a.currentConversation = append(a.currentConversation,
openai.ChatCompletionMessage{ // openai.ChatCompletionMessage{
// Role: "system",
// Content: fmt.Sprintf("Given the user input you have the following in memory:\n%s", formatResults),
// },
// )
a.currentConversation = append([]openai.ChatCompletionMessage{
{
Role: "system", Role: "system",
Content: fmt.Sprintf("Given the user input you have the following in memory:\n%s", formatResults), Content: fmt.Sprintf("Given the user input you have the following in memory:\n%s", formatResults),
}, }}, a.currentConversation...)
) }
} }
} }

View File

@@ -136,9 +136,10 @@ const (
ActionSearch = "search" ActionSearch = "search"
ActionGithubIssueLabeler = "github-issue-labeler" ActionGithubIssueLabeler = "github-issue-labeler"
ActionGithubIssueOpener = "github-issue-opener" ActionGithubIssueOpener = "github-issue-opener"
ActionGithubIssueCloser = "github-issue-closer"
) )
var AvailableActions = []string{ActionSearch, ActionGithubIssueLabeler, ActionGithubIssueOpener} var AvailableActions = []string{ActionSearch, ActionGithubIssueLabeler, ActionGithubIssueOpener, ActionGithubIssueCloser}
func (a *AgentConfig) availableActions(ctx context.Context) []Action { func (a *AgentConfig) availableActions(ctx context.Context) []Action {
actions := []Action{} actions := []Action{}
@@ -148,7 +149,7 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action {
var config map[string]string var config map[string]string
if err := json.Unmarshal([]byte(action.Config), &config); err != nil { if err := json.Unmarshal([]byte(action.Config), &config); err != nil {
fmt.Println("Error unmarshalling connector config", err) fmt.Println("Error unmarshalling action config", err)
continue continue
} }
fmt.Println("Config", config) fmt.Println("Config", config)
@@ -160,6 +161,8 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action {
actions = append(actions, external.NewGithubIssueLabeler(ctx, config)) actions = append(actions, external.NewGithubIssueLabeler(ctx, config))
case ActionGithubIssueOpener: case ActionGithubIssueOpener:
actions = append(actions, external.NewGithubIssueOpener(ctx, config)) actions = append(actions, external.NewGithubIssueOpener(ctx, config))
case ActionGithubIssueCloser:
actions = append(actions, external.NewGithubIssueCloser(ctx, config))
} }
} }
@@ -206,7 +209,7 @@ func (a *AgentConfig) availableConnectors() []Connector {
case ConnectorDiscord: case ConnectorDiscord:
connectors = append(connectors, connector.NewDiscord(config)) connectors = append(connectors, connector.NewDiscord(config))
case ConnectorGithubIssues: case ConnectorGithubIssues:
connectors = append(connectors, connector.NewGithub(config)) connectors = append(connectors, connector.NewGithubIssueWatcher(config))
} }
} }
return connectors return connectors
@@ -407,7 +410,7 @@ func (a *AgentPool) Remove(name string) error {
} }
func (a *AgentPool) Save() error { func (a *AgentPool) Save() error {
data, err := json.Marshal(a.pool) data, err := json.MarshalIndent(a.pool, "", " ")
if err != nil { if err != nil {
return err return err
} }

View File

@@ -19,7 +19,7 @@ type GithubIssues struct {
client *github.Client client *github.Client
} }
func NewGithub(config map[string]string) *GithubIssues { func NewGithubIssueWatcher(config map[string]string) *GithubIssues {
client := github.NewClient(nil).WithAuthToken(config["token"]) client := github.NewClient(nil).WithAuthToken(config["token"])
interval, err := time.ParseDuration(config["pollInterval"]) interval, err := time.ParseDuration(config["pollInterval"])
if err != nil { if err != nil {
@@ -57,7 +57,7 @@ func (g *GithubIssues) Start(a *agent.Agent) {
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
fmt.Println("Fire in da hole!") fmt.Println("Looking into github issues...")
g.issuesService() g.issuesService()
case <-a.Context().Done(): case <-a.Context().Done():
return return
@@ -86,12 +86,27 @@ func (g *GithubIssues) issuesService() {
if issue.IsPullRequest() { if issue.IsPullRequest() {
continue continue
} }
userName := *issue.User.Name labels := []string{}
for _, l := range issue.Labels {
labels = append(labels, l.GetName())
}
// Get user that opened the issue
userNameLogin := issue.GetUser().Login
userName := ""
if userNameLogin != nil {
userName = *userNameLogin
}
if userName == user.GetLogin() {
fmt.Println("Ignoring issue opened by the bot")
continue
}
messages := []openai.ChatCompletionMessage{ messages := []openai.ChatCompletionMessage{
{ {
Role: "system", Role: "system",
Content: fmt.Sprintf( Content: fmt.Sprintf(
`This is a conversation with an user ("%s") that opened a Github issue with title "%s" in the repository "%s" owned by "%s" .`, userName, issue.GetTitle(), g.repository, g.owner), `This is a conversation with an user ("%s") that opened a Github issue with title "%s" in the repository "%s" owned by "%s". The issue is the issue number %d. Current labels: %+v`, userName, issue.GetTitle(), g.repository, g.owner, issue.GetNumber(), labels),
}, },
{ {
Role: "user", Role: "user",

90
external/githubissuecloser.go vendored Normal file
View File

@@ -0,0 +1,90 @@
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 GithubIssuesCloser struct {
token string
context context.Context
client *github.Client
}
func NewGithubIssueCloser(ctx context.Context, config map[string]string) *GithubIssuesCloser {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssuesCloser{
client: client,
token: config["token"],
context: ctx,
}
}
func (g *GithubIssuesCloser) Run(params action.ActionParams) (string, error) {
result := struct {
Repository string `json:"repository"`
Owner string `json:"owner"`
IssueNumber int `json:"issue_number"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
// _, _, err = g.client.Issues.CreateComment(
// g.context,
// result.Owner, result.Repository,
// result.IssueNumber, &github.IssueComment{
// Body: &result.Text,
// },
// )
// if err != nil {
// fmt.Printf("error: %v", err)
// return "", err
// }
_, _, err = g.client.Issues.Edit(g.context, result.Owner, result.Repository, result.IssueNumber, &github.IssueRequest{
State: github.String("closed"),
})
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
resultString := fmt.Sprintf("Closed issue %d in repository %s/%s", result.IssueNumber, result.Owner, result.Repository)
if err != nil {
resultString = fmt.Sprintf("Error closing issue %d in repository %s/%s: %v", result.IssueNumber, result.Owner, result.Repository, err)
}
return resultString, err
}
func (g *GithubIssuesCloser) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "close_github_issue",
Description: "Closes a Github issue.",
Properties: map[string]jsonschema.Definition{
"repository": {
Type: jsonschema.String,
Description: "The repository to add the label to.",
},
"owner": {
Type: jsonschema.String,
Description: "The owner of the repository.",
},
"issue_number": {
Type: jsonschema.Number,
Description: "The issue number to close",
},
},
Required: []string{"issue_number", "repository", "owner"},
}
}

View File

@@ -66,7 +66,7 @@ func (g *GithubIssuesLabeler) Run(params action.ActionParams) (string, error) {
func (g *GithubIssuesLabeler) Definition() action.ActionDefinition { func (g *GithubIssuesLabeler) Definition() action.ActionDefinition {
return action.ActionDefinition{ return action.ActionDefinition{
Name: "add_label_to_github_issue", Name: "add_label_to_github_issue",
Description: "Add a label to a Github issue.", Description: "Add a label to a Github issue. You might want to assign labels to issues to categorize them.",
Properties: map[string]jsonschema.Definition{ Properties: map[string]jsonschema.Definition{
"issue_number": { "issue_number": {
Type: jsonschema.Number, Type: jsonschema.Number,

View File

@@ -28,7 +28,7 @@ func NewGithubIssueOpener(ctx context.Context, config map[string]string) *Github
func (g *GithubIssuesOpener) Run(params action.ActionParams) (string, error) { func (g *GithubIssuesOpener) Run(params action.ActionParams) (string, error) {
result := struct { result := struct {
Title string `json:"title"` Title string `json:"title"`
Body string `json:"body"` Body string `json:"text"`
Repository string `json:"repository"` Repository string `json:"repository"`
Owner string `json:"owner"` Owner string `json:"owner"`
}{} }{}
@@ -60,9 +60,9 @@ func (g *GithubIssuesOpener) Definition() action.ActionDefinition {
Name: "create_github_issue", Name: "create_github_issue",
Description: "Create a new issue on a GitHub repository.", Description: "Create a new issue on a GitHub repository.",
Properties: map[string]jsonschema.Definition{ Properties: map[string]jsonschema.Definition{
"body": { "text": {
Type: jsonschema.String, Type: jsonschema.String,
Description: "The number of the issue to add the label to.", Description: "The text of the new issue",
}, },
"title": { "title": {
Type: jsonschema.String, Type: jsonschema.String,
@@ -74,9 +74,9 @@ func (g *GithubIssuesOpener) Definition() action.ActionDefinition {
}, },
"repository": { "repository": {
Type: jsonschema.String, Type: jsonschema.String,
Description: "The repository to create the issue in.", Description: "The repository where to create the issue.",
}, },
}, },
Required: []string{"title", "body", "owner", "repository"}, Required: []string{"title", "text", "owner", "repository"},
} }
} }