reordering

This commit is contained in:
Ettore Di Giacinto
2025-02-25 22:18:08 +01:00
parent d73fd545b2
commit 296734ba3b
46 changed files with 84 additions and 85 deletions

View File

@@ -0,0 +1,68 @@
package actions
import (
"context"
"fmt"
"io"
"net/http"
"github.com/mudler/local-agent-framework/core/action"
"github.com/sashabaranov/go-openai/jsonschema"
"jaytaylor.com/html2text"
)
func NewBrowse(config map[string]string) *BrowseAction {
return &BrowseAction{}
}
type BrowseAction struct{}
func (a *BrowseAction) Run(ctx context.Context, params action.ActionParams) (string, error) {
result := struct {
URL string `json:"url"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
// download page with http.Client
client := &http.Client{}
req, err := http.NewRequest("GET", result.URL, nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
pagebyte, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
rendered, err := html2text.FromString(string(pagebyte), html2text.Options{PrettyTables: true})
if err != nil {
return "", err
}
return fmt.Sprintf("The webpage '%s' content is:\n%s", result.URL, rendered), nil
}
func (a *BrowseAction) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "browse",
Description: "Use this tool to visit an URL. It browse a website page and return the text content.",
Properties: map[string]jsonschema.Definition{
"url": {
Type: jsonschema.String,
Description: "The website URL.",
},
},
Required: []string{"url"},
}
}

View File

@@ -0,0 +1,90 @@
package actions
import (
"context"
"fmt"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/core/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(ctx context.Context, 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 close the issue in.",
},
"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

@@ -0,0 +1,92 @@
package actions
import (
"context"
"fmt"
"log/slog"
"strings"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/core/action"
"github.com/sashabaranov/go-openai/jsonschema"
)
type GithubIssuesLabeler struct {
token string
availableLabels []string
context context.Context
client *github.Client
}
func NewGithubIssueLabeler(ctx context.Context, config map[string]string) *GithubIssuesLabeler {
client := github.NewClient(nil).WithAuthToken(config["token"])
// Get available labels
availableLabels := []string{"bug", "enhancement"}
if config["availableLabels"] != "" {
availableLabels = strings.Split(config["availableLabels"], ",")
}
return &GithubIssuesLabeler{
client: client,
token: config["token"],
context: ctx,
availableLabels: availableLabels,
}
}
func (g *GithubIssuesLabeler) Run(ctx context.Context, params action.ActionParams) (string, error) {
result := struct {
Repository string `json:"repository"`
Owner string `json:"owner"`
Label string `json:"label"`
IssueNumber int `json:"issue_number"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
labels, _, err := g.client.Issues.AddLabelsToIssue(g.context, result.Owner, result.Repository, result.IssueNumber, []string{result.Label})
//labelsNames := []string{}
for _, l := range labels {
slog.Info("Label added:", l.Name)
//labelsNames = append(labelsNames, l.GetName())
}
resultString := fmt.Sprintf("Added label '%s' to issue %d in repository %s/%s", result.Label, result.IssueNumber, result.Owner, result.Repository)
if err != nil {
resultString = fmt.Sprintf("Error adding label '%s' to issue %d in repository %s/%s: %v", result.Label, result.IssueNumber, result.Owner, result.Repository, err)
}
return resultString, err
}
func (g *GithubIssuesLabeler) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "add_label_to_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{
"issue_number": {
Type: jsonschema.Number,
Description: "The number of the issue to add the label to.",
},
"repository": {
Type: jsonschema.String,
Description: "The repository to add the label to.",
},
"owner": {
Type: jsonschema.String,
Description: "The owner of the repository.",
},
"label": {
Type: jsonschema.String,
Description: "The label to add to the issue.",
Enum: g.availableLabels,
},
},
Required: []string{"issue_number", "repository", "owner", "label"},
}
}

View File

@@ -0,0 +1,82 @@
package actions
import (
"context"
"fmt"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/core/action"
"github.com/sashabaranov/go-openai/jsonschema"
)
type GithubIssuesOpener struct {
token string
context context.Context
client *github.Client
}
func NewGithubIssueOpener(ctx context.Context, config map[string]string) *GithubIssuesOpener {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssuesOpener{
client: client,
token: config["token"],
context: ctx,
}
}
func (g *GithubIssuesOpener) Run(ctx context.Context, params action.ActionParams) (string, error) {
result := struct {
Title string `json:"title"`
Body string `json:"text"`
Repository string `json:"repository"`
Owner string `json:"owner"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
issue := &github.IssueRequest{
Title: &result.Title,
Body: &result.Body,
}
resultString := ""
createdIssue, _, err := g.client.Issues.Create(g.context, result.Owner, result.Repository, issue)
if err != nil {
resultString = fmt.Sprintf("Error creating issue: %v", err)
} else {
resultString = fmt.Sprintf("Created issue %d in repository %s/%s", createdIssue.GetNumber(), result.Owner, result.Repository)
}
return resultString, err
}
func (g *GithubIssuesOpener) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "create_github_issue",
Description: "Create a new issue on a GitHub repository.",
Properties: map[string]jsonschema.Definition{
"text": {
Type: jsonschema.String,
Description: "The text of the new issue",
},
"title": {
Type: jsonschema.String,
Description: "The title of the issue.",
},
"owner": {
Type: jsonschema.String,
Description: "The owner of the repository.",
},
"repository": {
Type: jsonschema.String,
Description: "The repository where to create the issue.",
},
},
Required: []string{"title", "text", "owner", "repository"},
}
}

View File

@@ -0,0 +1,83 @@
package actions
import (
"context"
"fmt"
"log/slog"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/core/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(ctx context.Context, 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)
return resultString, err
}
for _, i := range issues.Issues {
slog.Info("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"},
}
}

View File

@@ -0,0 +1,50 @@
package actions
import (
"context"
"fmt"
"github.com/mudler/local-agent-framework/core/action"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/tmc/langchaingo/tools/scraper"
)
func NewScraper(config map[string]string) *ScraperAction {
return &ScraperAction{}
}
type ScraperAction struct{}
func (a *ScraperAction) Run(ctx context.Context, params action.ActionParams) (string, error) {
result := struct {
URL string `json:"url"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
scraper, err := scraper.New()
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
return scraper.Call(ctx, result.URL)
}
func (a *ScraperAction) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "scrape",
Description: "Scrapes a full website content and returns the entire site data.",
Properties: map[string]jsonschema.Definition{
"url": {
Type: jsonschema.String,
Description: "The website URL.",
},
},
Required: []string{"url"},
}
}

View File

@@ -0,0 +1,62 @@
package actions
import (
"context"
"fmt"
"log/slog"
"github.com/mudler/local-agent-framework/core/action"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/tmc/langchaingo/tools/duckduckgo"
)
func NewSearch(config map[string]string) *SearchAction {
results := config["results"]
intResult := 1
// decode int from string
if results != "" {
_, err := fmt.Sscanf(results, "%d", &intResult)
if err != nil {
fmt.Printf("error: %v", err)
}
}
slog.Info("Search action with results: ", "results", intResult)
return &SearchAction{results: intResult}
}
type SearchAction struct{ results int }
func (a *SearchAction) Run(ctx context.Context, params action.ActionParams) (string, error) {
result := struct {
Query string `json:"query"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
ddg, err := duckduckgo.New(a.results, "LocalAgent")
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
return ddg.Call(ctx, result.Query)
}
func (a *SearchAction) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "search_internet",
Description: "Search the internet for something.",
Properties: map[string]jsonschema.Definition{
"query": {
Type: jsonschema.String,
Description: "The query to search for.",
},
},
Required: []string{"query"},
}
}

View File

@@ -0,0 +1,78 @@
package actions
import (
"context"
"fmt"
"net/smtp"
"github.com/mudler/local-agent-framework/core/action"
"github.com/sashabaranov/go-openai/jsonschema"
)
func NewSendMail(config map[string]string) *SendMailAction {
return &SendMailAction{
username: config["username"],
password: config["password"],
email: config["email"],
smtpHost: config["smtpHost"],
smtpPort: config["smtpPort"],
}
}
type SendMailAction struct {
username string
password string
email string
smtpHost string
smtpPort string
}
func (a *SendMailAction) Run(ctx context.Context, params action.ActionParams) (string, error) {
result := struct {
Message string `json:"message"`
To string `json:"to"`
Subject string `json:"subject"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
// Authentication.
auth := smtp.PlainAuth("", a.email, a.password, a.smtpHost)
// Sending email.
err = smtp.SendMail(
fmt.Sprintf("%s:%s", a.smtpHost, a.smtpPort),
auth, a.email, []string{
result.To,
}, []byte(result.Message))
if err != nil {
return "", err
}
return fmt.Sprintf("Email sent to %s", result.To), nil
}
func (a *SendMailAction) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "send_email",
Description: "Send an email.",
Properties: map[string]jsonschema.Definition{
"to": {
Type: jsonschema.String,
Description: "The email address to send the email to.",
},
"subject": {
Type: jsonschema.String,
Description: "The subject of the email.",
},
"message": {
Type: jsonschema.String,
Description: "The message to send.",
},
},
Required: []string{"to", "subject", "message"},
}
}

View File

@@ -0,0 +1,44 @@
package actions
import (
"context"
"fmt"
"github.com/mudler/local-agent-framework/core/action"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/tmc/langchaingo/tools/wikipedia"
)
func NewWikipedia(config map[string]string) *WikipediaAction {
return &WikipediaAction{}
}
type WikipediaAction struct{}
func (a *WikipediaAction) Run(ctx context.Context, params action.ActionParams) (string, error) {
result := struct {
Query string `json:"query"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
wiki := wikipedia.New("LocalAgent")
return wiki.Call(ctx, result.Query)
}
func (a *WikipediaAction) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "wikipedia",
Description: "Find wikipedia pages using the wikipedia api",
Properties: map[string]jsonschema.Definition{
"query": {
Type: jsonschema.String,
Description: "The website URL.",
},
},
Required: []string{"query"},
}
}

View File

@@ -0,0 +1,117 @@
package connectors
import (
"strings"
"github.com/bwmarrin/discordgo"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/pkg/xlog"
)
type Discord struct {
token string
defaultChannel string
}
// NewDiscord creates a new Discord connector
// with the given configuration
// - token: Discord token
// - defaultChannel: Discord channel to always answer even if not mentioned
func NewDiscord(config map[string]string) *Discord {
return &Discord{
token: config["token"],
defaultChannel: config["defaultChannel"],
}
}
func (d *Discord) AgentResultCallback() func(state agent.ActionState) {
return func(state agent.ActionState) {
// Send the result to the bot
}
}
func (d *Discord) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
return func(state agent.ActionCurrentState) bool {
// Send the reasoning to the bot
return true
}
}
func (d *Discord) Start(a *agent.Agent) {
Token := d.token
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New(Token)
if err != nil {
xlog.Info("error creating Discord session,", err)
return
}
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(d.messageCreate(a))
// In this example, we only care about receiving message events.
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages | discordgo.IntentMessageContent
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
xlog.Info("error opening connection,", err)
return
}
go func() {
xlog.Info("Discord bot is now running. Press CTRL-C to exit.")
<-a.Context().Done()
dg.Close()
xlog.Info("Discord bot is now stopped.")
}()
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to.
func (d *Discord) messageCreate(a *agent.Agent) func(s *discordgo.Session, m *discordgo.MessageCreate) {
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
interact := func() {
//m := m.ContentWithMentionsReplaced()
content := m.Content
content = strings.ReplaceAll(content, "<@"+s.State.User.ID+"> ", "")
xlog.Info("Received message", "content", content)
job := a.Ask(
agent.WithText(
content,
),
)
if job.Error != nil {
xlog.Info("error asking agent,", job.Error)
return
}
xlog.Info("Response", "response", job.Response)
_, err := s.ChannelMessageSend(m.ChannelID, job.Response)
if err != nil {
xlog.Info("error sending message,", err)
}
}
// Interact if we are mentioned
for _, mention := range m.Mentions {
if mention.ID == s.State.User.ID {
go interact()
return
}
}
// Or we are in the default channel (if one is set!)
if d.defaultChannel != "" && m.ChannelID == d.defaultChannel {
go interact()
return
}
}
}

View File

@@ -0,0 +1,196 @@
package connectors
import (
"fmt"
"strings"
"time"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/pkg/xlog"
"github.com/sashabaranov/go-openai"
)
type GithubIssues struct {
token string
repository string
owner string
replyIfNoReplies bool
agent *agent.Agent
pollInterval time.Duration
client *github.Client
}
// NewGithubIssueWatcher creates a new GithubIssues connector
// with the given configuration
// - token: Github token
// - repository: Github repository name
// - owner: Github repository owner
// - replyIfNoReplies: If true, the bot will reply to issues with no comments
func NewGithubIssueWatcher(config map[string]string) *GithubIssues {
client := github.NewClient(nil).WithAuthToken(config["token"])
replyIfNoReplies := false
if config["replyIfNoReplies"] == "true" {
replyIfNoReplies = true
}
interval, err := time.ParseDuration(config["pollInterval"])
if err != nil {
interval = 10 * time.Minute
}
return &GithubIssues{
client: client,
token: config["token"],
repository: config["repository"],
owner: config["owner"],
replyIfNoReplies: replyIfNoReplies,
pollInterval: interval,
}
}
func (g *GithubIssues) AgentResultCallback() func(state agent.ActionState) {
return func(state agent.ActionState) {
// Send the result to the bot
}
}
func (g *GithubIssues) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
return func(state agent.ActionCurrentState) bool {
// Send the reasoning to the bot
return true
}
}
func (g *GithubIssues) Start(a *agent.Agent) {
// Start the connector
g.agent = a
go func() {
ticker := time.NewTicker(g.pollInterval)
for {
select {
case <-ticker.C:
xlog.Info("Looking into github issues...")
g.issuesService()
case <-a.Context().Done():
xlog.Info("GithubIssues connector is now stopping")
return
}
}
}()
}
func (g *GithubIssues) issuesService() {
user, _, err := g.client.Users.Get(g.agent.Context(), "")
if err != nil {
fmt.Printf("\nerror: %v\n", err)
return
}
issues, _, err := g.client.Issues.ListByRepo(
g.agent.Context(),
g.owner,
g.repository,
&github.IssueListByRepoOptions{})
if err != nil {
xlog.Info("Error listing issues", err)
}
for _, issue := range issues {
// Do something with the issue
if issue.IsPullRequest() {
continue
}
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() {
xlog.Info("Ignoring issue opened by the bot")
continue
}
messages := []openai.ChatCompletionMessage{
{
Role: "system",
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". The issue is the issue number %d. Current labels: %+v`, userName, issue.GetTitle(), g.repository, g.owner, issue.GetNumber(), labels),
},
{
Role: "user",
Content: issue.GetBody(),
},
}
comments, _, _ := g.client.Issues.ListComments(g.agent.Context(), g.owner, g.repository, issue.GetNumber(),
&github.IssueListCommentsOptions{})
mustAnswer := false
botAnsweredAlready := false
for i, comment := range comments {
role := "user"
if comment.GetUser().GetLogin() == user.GetLogin() {
botAnsweredAlready = true
role = "assistant"
}
messages = append(messages, openai.ChatCompletionMessage{
Role: role,
Content: comment.GetBody(),
})
// if last comment is from the user and mentions the bot username, we must answer
if comment.User.GetName() != user.GetLogin() && len(comments)-1 == i {
if strings.Contains(comment.GetBody(), fmt.Sprintf("@%s", user.GetLogin())) {
xlog.Info("Bot was mentioned in the last comment")
mustAnswer = true
}
}
}
if len(comments) == 0 || !botAnsweredAlready {
// if no comments, or bot didn't answer yet, we must answer
xlog.Info("No comments, or bot didn't answer yet",
"comments", len(comments),
"botAnsweredAlready", botAnsweredAlready,
"agent", g.agent.Character.Name,
)
mustAnswer = true
}
if len(comments) != 0 && g.replyIfNoReplies {
xlog.Info("Ignoring issue with comments", "issue", issue.GetNumber(), "agent", g.agent.Character.Name)
mustAnswer = false
}
if !mustAnswer {
continue
}
res := g.agent.Ask(
agent.WithConversationHistory(messages),
)
if res.Error != nil {
xlog.Error("Error asking", "error", res.Error, "agent", g.agent.Character.Name)
return
}
_, _, err := g.client.Issues.CreateComment(
g.agent.Context(),
g.owner, g.repository,
issue.GetNumber(), &github.IssueComment{
Body: github.String(res.Response),
},
)
if err != nil {
xlog.Error("Error creating comment", "error", err, "agent", g.agent.Character.Name)
}
}
}

View File

@@ -0,0 +1,196 @@
package connectors
import (
"fmt"
"strings"
"time"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/pkg/xlog"
"github.com/sashabaranov/go-openai"
)
type GithubPRs struct {
token string
repository string
owner string
replyIfNoReplies bool
agent *agent.Agent
pollInterval time.Duration
client *github.Client
}
// NewGithubIssueWatcher creates a new GithubPRs connector
// with the given configuration
// - token: Github token
// - repository: Github repository name
// - owner: Github repository owner
// - replyIfNoReplies: If true, the bot will reply to issues with no comments
func NewGithubPRWatcher(config map[string]string) *GithubPRs {
client := github.NewClient(nil).WithAuthToken(config["token"])
replyIfNoReplies := false
if config["replyIfNoReplies"] == "true" {
replyIfNoReplies = true
}
interval, err := time.ParseDuration(config["pollInterval"])
if err != nil {
interval = 10 * time.Minute
}
return &GithubPRs{
client: client,
token: config["token"],
repository: config["repository"],
owner: config["owner"],
replyIfNoReplies: replyIfNoReplies,
pollInterval: interval,
}
}
func (g *GithubPRs) AgentResultCallback() func(state agent.ActionState) {
return func(state agent.ActionState) {
// Send the result to the bot
}
}
func (g *GithubPRs) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
return func(state agent.ActionCurrentState) bool {
// Send the reasoning to the bot
return true
}
}
func (g *GithubPRs) Start(a *agent.Agent) {
// Start the connector
g.agent = a
go func() {
ticker := time.NewTicker(g.pollInterval)
for {
select {
case <-ticker.C:
xlog.Info("Looking into github Prs...")
g.prService()
case <-a.Context().Done():
xlog.Info("GithubPRs connector is now stopping")
return
}
}
}()
}
func (g *GithubPRs) prService() {
user, _, err := g.client.Users.Get(g.agent.Context(), "")
if err != nil {
fmt.Printf("\nerror: %v\n", err)
return
}
issues, _, err := g.client.Issues.ListByRepo(
g.agent.Context(),
g.owner,
g.repository,
&github.IssueListByRepoOptions{})
if err != nil {
xlog.Info("Error listing issues", err)
}
for _, issue := range issues {
// Do something if not an PR
if !issue.IsPullRequest() {
continue
}
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() {
xlog.Info("Ignoring issue opened by the bot")
continue
}
messages := []openai.ChatCompletionMessage{
{
Role: "system",
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". The issue is the issue number %d. Current labels: %+v`, userName, issue.GetTitle(), g.repository, g.owner, issue.GetNumber(), labels),
},
{
Role: "user",
Content: issue.GetBody(),
},
}
comments, _, _ := g.client.Issues.ListComments(g.agent.Context(), g.owner, g.repository, issue.GetNumber(),
&github.IssueListCommentsOptions{})
mustAnswer := false
botAnsweredAlready := false
for i, comment := range comments {
role := "user"
if comment.GetUser().GetLogin() == user.GetLogin() {
botAnsweredAlready = true
role = "assistant"
}
messages = append(messages, openai.ChatCompletionMessage{
Role: role,
Content: comment.GetBody(),
})
// if last comment is from the user and mentions the bot username, we must answer
if comment.User.GetName() != user.GetLogin() && len(comments)-1 == i {
if strings.Contains(comment.GetBody(), fmt.Sprintf("@%s", user.GetLogin())) {
xlog.Info("Bot was mentioned in the last comment")
mustAnswer = true
}
}
}
if len(comments) == 0 || !botAnsweredAlready {
// if no comments, or bot didn't answer yet, we must answer
xlog.Info("No comments, or bot didn't answer yet",
"comments", len(comments),
"botAnsweredAlready", botAnsweredAlready,
"agent", g.agent.Character.Name,
)
mustAnswer = true
}
if len(comments) != 0 && g.replyIfNoReplies {
xlog.Info("Ignoring issue with comments", "issue", issue.GetNumber(), "agent", g.agent.Character.Name)
mustAnswer = false
}
if !mustAnswer {
continue
}
res := g.agent.Ask(
agent.WithConversationHistory(messages),
)
if res.Error != nil {
xlog.Error("Error asking", "error", res.Error, "agent", g.agent.Character.Name)
return
}
_, _, err := g.client.Issues.CreateComment(
g.agent.Context(),
g.owner, g.repository,
issue.GetNumber(), &github.IssueComment{
Body: github.String(res.Response),
},
)
if err != nil {
xlog.Error("Error creating comment", "error", err, "agent", g.agent.Character.Name)
}
}
}

View File

@@ -0,0 +1,155 @@
package connectors
import (
"fmt"
"log"
"os"
"strings"
"github.com/mudler/local-agent-framework/pkg/xlog"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/slack-go/slack/socketmode"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
)
type Slack struct {
appToken string
botToken string
channelID string
alwaysReply bool
}
func NewSlack(config map[string]string) *Slack {
return &Slack{
appToken: config["appToken"],
botToken: config["botToken"],
channelID: config["channelID"],
alwaysReply: config["alwaysReply"] == "true",
}
}
func (t *Slack) AgentResultCallback() func(state agent.ActionState) {
return func(state agent.ActionState) {
// Send the result to the bot
}
}
func (t *Slack) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
return func(state agent.ActionCurrentState) bool {
// Send the reasoning to the bot
return true
}
}
func (t *Slack) Start(a *agent.Agent) {
api := slack.New(
t.botToken,
// slack.OptionDebug(true),
slack.OptionLog(log.New(os.Stdout, "api: ", log.Lshortfile|log.LstdFlags)),
slack.OptionAppLevelToken(t.appToken),
)
client := socketmode.New(
api,
//socketmode.OptionDebug(true),
//socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
)
go func() {
for evt := range client.Events {
switch evt.Type {
case socketmode.EventTypeConnecting:
xlog.Info("Connecting to Slack with Socket Mode...")
case socketmode.EventTypeConnectionError:
xlog.Info("Connection failed. Retrying later...")
case socketmode.EventTypeConnected:
xlog.Info("Connected to Slack with Socket Mode.")
case socketmode.EventTypeEventsAPI:
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
fmt.Printf("Ignored %+v\n", evt)
continue
}
fmt.Printf("Event received: %+v\n", eventsAPIEvent)
client.Ack(*evt.Request)
switch eventsAPIEvent.Type {
case slackevents.CallbackEvent:
innerEvent := eventsAPIEvent.InnerEvent
b, err := api.AuthTest()
if err != nil {
fmt.Printf("Error getting auth test: %v", err)
}
switch ev := innerEvent.Data.(type) {
case *slackevents.MessageEvent:
if t.channelID == "" && !t.alwaysReply || // If we have set alwaysReply and no channelID
t.channelID != ev.Channel { // If we have a channelID and it's not the same as the event channel
// Skip messages from other channels
xlog.Info("Skipping reply to channel", ev.Channel, t.channelID)
continue
}
if b.UserID == ev.User {
// Skip messages from ourselves
continue
}
message := ev.Text
go func() {
res := a.Ask(
agent.WithText(message),
)
_, _, err = api.PostMessage(ev.Channel,
slack.MsgOptionText(res.Response, false),
slack.MsgOptionPostMessageParameters(slack.PostMessageParameters{LinkNames: 1}))
if err != nil {
fmt.Printf("Error posting message: %v", err)
}
}()
case *slackevents.AppMentionEvent:
if b.UserID == ev.User {
// Skip messages from ourselves
continue
}
message := ev.Text
// strip our id from the message
message = strings.ReplaceAll(message, "<@"+b.UserID+"> ", "")
xlog.Info("Message", message)
go func() {
res := a.Ask(
agent.WithText(message),
)
_, _, err = api.PostMessage(ev.Channel,
slack.MsgOptionText(res.Response, false),
slack.MsgOptionPostMessageParameters(slack.PostMessageParameters{LinkNames: 1}))
if err != nil {
fmt.Printf("Error posting message: %v", err)
}
}()
case *slackevents.MemberJoinedChannelEvent:
fmt.Printf("user %q joined to channel %q", ev.User, ev.Channel)
}
default:
client.Debugf("unsupported Events API event received")
}
default:
fmt.Fprintf(os.Stderr, "Unexpected event type received: %s\n", evt.Type)
}
}
}()
client.RunContext(a.Context())
}

View File

@@ -0,0 +1,93 @@
package connectors
import (
"context"
"errors"
"os"
"os/signal"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/mudler/local-agent-framework/core/agent"
)
type Telegram struct {
Token string
lastChatID int64
bot *bot.Bot
agent *agent.Agent
}
// Send any text message to the bot after the bot has been started
func (t *Telegram) AgentResultCallback() func(state agent.ActionState) {
return func(state agent.ActionState) {
t.bot.SetMyDescription(t.agent.Context(), &bot.SetMyDescriptionParams{
Description: state.Reasoning,
})
}
}
func (t *Telegram) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
return func(state agent.ActionCurrentState) bool {
t.bot.SetMyDescription(t.agent.Context(), &bot.SetMyDescriptionParams{
Description: state.Reasoning,
})
return true
}
}
func (t *Telegram) Start(a *agent.Agent) {
ctx, cancel := signal.NotifyContext(a.Context(), os.Interrupt)
defer cancel()
opts := []bot.Option{
bot.WithDefaultHandler(func(ctx context.Context, b *bot.Bot, update *models.Update) {
go func() {
res := a.Ask(
agent.WithText(
update.Message.Text,
),
)
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: res.Response,
})
t.lastChatID = update.Message.Chat.ID
}()
}),
}
b, err := bot.New(t.Token, opts...)
if err != nil {
panic(err)
}
t.bot = b
t.agent = a
go func() {
for m := range a.ConversationChannel() {
if t.lastChatID == 0 {
continue
}
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: t.lastChatID,
Text: m.Content,
})
}
}()
b.Start(ctx)
}
func NewTelegramConnector(config map[string]string) (*Telegram, error) {
token, ok := config["token"]
if !ok {
return nil, errors.New("token is required")
}
return &Telegram{
Token: token,
}, nil
}