uniform logging

This commit is contained in:
Ettore Di Giacinto
2024-04-19 00:02:00 +02:00
parent 2cba2eafe6
commit 08563c3286
13 changed files with 178 additions and 108 deletions

View File

@@ -1,11 +1,11 @@
package connector
import (
"log/slog"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/mudler/local-agent-framework/agent"
"github.com/mudler/local-agent-framework/xlog"
)
type Discord struct {
@@ -39,7 +39,7 @@ func (d *Discord) Start(a *agent.Agent) {
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New(Token)
if err != nil {
slog.Info("error creating Discord session,", err)
xlog.Info("error creating Discord session,", err)
return
}
@@ -52,15 +52,15 @@ func (d *Discord) Start(a *agent.Agent) {
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
slog.Info("error opening connection,", err)
xlog.Info("error opening connection,", err)
return
}
go func() {
slog.Info("Discord bot is now running. Press CTRL-C to exit.")
xlog.Info("Discord bot is now running. Press CTRL-C to exit.")
<-a.Context().Done()
dg.Close()
slog.Info("Discord bot is now stopped.")
xlog.Info("Discord bot is now stopped.")
}()
}
@@ -78,21 +78,21 @@ func (d *Discord) messageCreate(a *agent.Agent) func(s *discordgo.Session, m *di
content := m.Content
content = strings.ReplaceAll(content, "<@"+s.State.User.ID+"> ", "")
slog.Info("Received message", "content", content)
xlog.Info("Received message", "content", content)
job := a.Ask(
agent.WithText(
content,
),
)
if job.Error != nil {
slog.Info("error asking agent,", job.Error)
xlog.Info("error asking agent,", job.Error)
return
}
slog.Info("Response", "response", job.Response)
xlog.Info("Response", "response", job.Response)
_, err := s.ChannelMessageSend(m.ChannelID, job.Response)
if err != nil {
slog.Info("error sending message,", err)
xlog.Info("error sending message,", err)
}
}

View File

@@ -2,12 +2,13 @@ package connector
import (
"fmt"
"log/slog"
"strings"
"time"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/agent"
"github.com/mudler/local-agent-framework/xlog"
"github.com/sashabaranov/go-openai"
)
@@ -58,10 +59,10 @@ func (g *GithubIssues) Start(a *agent.Agent) {
for {
select {
case <-ticker.C:
slog.Info("Looking into github issues...")
xlog.Info("Looking into github issues...")
g.issuesService()
case <-a.Context().Done():
slog.Info("GithubIssues connector is now stopping")
xlog.Info("GithubIssues connector is now stopping")
return
}
}
@@ -81,7 +82,7 @@ func (g *GithubIssues) issuesService() {
g.repository,
&github.IssueListByRepoOptions{})
if err != nil {
slog.Info("Error listing issues", err)
xlog.Info("Error listing issues", err)
}
for _, issue := range issues {
// Do something with the issue
@@ -101,7 +102,7 @@ func (g *GithubIssues) issuesService() {
}
if userName == user.GetLogin() {
slog.Info("Ignoring issue opened by the bot")
xlog.Info("Ignoring issue opened by the bot")
continue
}
messages := []openai.ChatCompletionMessage{
@@ -135,7 +136,7 @@ func (g *GithubIssues) issuesService() {
// 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())) {
slog.Info("Bot was mentioned in the last comment")
xlog.Info("Bot was mentioned in the last comment")
mustAnswer = true
}
}
@@ -143,9 +144,9 @@ func (g *GithubIssues) issuesService() {
if len(comments) == 0 || !botAnsweredAlready {
// if no comments, or bot didn't answer yet, we must answer
slog.Info("No comments, or bot didn't answer yet")
slog.Info("Comments:", len(comments))
slog.Info("Bot answered already", botAnsweredAlready)
xlog.Info("No comments, or bot didn't answer yet")
xlog.Info("Comments:", len(comments))
xlog.Info("Bot answered already", botAnsweredAlready)
mustAnswer = true
}
@@ -157,7 +158,7 @@ func (g *GithubIssues) issuesService() {
agent.WithConversationHistory(messages),
)
if res.Error != nil {
slog.Error("Error asking", "error", res.Error)
xlog.Error("Error asking", "error", res.Error)
return
}
@@ -169,7 +170,7 @@ func (g *GithubIssues) issuesService() {
},
)
if err != nil {
slog.Error("Error creating comment", "error", err)
xlog.Error("Error creating comment", "error", err)
}
}
}

View File

@@ -3,10 +3,11 @@ package connector
import (
"fmt"
"log"
"log/slog"
"os"
"strings"
"github.com/mudler/local-agent-framework/xlog"
"github.com/mudler/local-agent-framework/agent"
"github.com/slack-go/slack/socketmode"
@@ -61,11 +62,11 @@ func (t *Slack) Start(a *agent.Agent) {
for evt := range client.Events {
switch evt.Type {
case socketmode.EventTypeConnecting:
slog.Info("Connecting to Slack with Socket Mode...")
xlog.Info("Connecting to Slack with Socket Mode...")
case socketmode.EventTypeConnectionError:
slog.Info("Connection failed. Retrying later...")
xlog.Info("Connection failed. Retrying later...")
case socketmode.EventTypeConnected:
slog.Info("Connected to Slack with Socket Mode.")
xlog.Info("Connected to Slack with Socket Mode.")
case socketmode.EventTypeEventsAPI:
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
@@ -92,7 +93,7 @@ func (t *Slack) Start(a *agent.Agent) {
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
slog.Info("Skipping reply to channel", ev.Channel, t.channelID)
xlog.Info("Skipping reply to channel", ev.Channel, t.channelID)
continue
}
@@ -124,7 +125,7 @@ func (t *Slack) Start(a *agent.Agent) {
// strip our id from the message
message = strings.ReplaceAll(message, "<@"+b.UserID+"> ", "")
slog.Info("Message", message)
xlog.Info("Message", message)
go func() {
res := a.Ask(