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

@@ -3,7 +3,8 @@ package main
import (
"context"
"encoding/json"
"log/slog"
"github.com/mudler/local-agent-framework/xlog"
. "github.com/mudler/local-agent-framework/agent"
"github.com/mudler/local-agent-framework/external"
@@ -36,14 +37,14 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action {
actions := []Action{}
for _, action := range a.Actions {
slog.Info("Set Action", action)
xlog.Info("Set Action", action)
var config map[string]string
if err := json.Unmarshal([]byte(action.Config), &config); err != nil {
slog.Info("Error unmarshalling action config", err)
xlog.Info("Error unmarshalling action config", err)
continue
}
slog.Info("Config", config)
xlog.Info("Config", config)
switch action.Name {
case ActionSearch:

View File

@@ -4,13 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"sort"
"sync"
"time"
"github.com/mudler/local-agent-framework/xlog"
. "github.com/mudler/local-agent-framework/agent"
)
@@ -140,15 +141,15 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
connectors := config.availableConnectors()
slog.Info("Creating agent", name)
slog.Info("Model", model)
slog.Info("API URL", a.apiURL)
xlog.Info("Creating agent", name)
xlog.Info("Model", model)
xlog.Info("API URL", a.apiURL)
actions := config.availableActions(ctx)
stateFile, characterFile := a.stateFiles(name)
slog.Info("Actions", actions)
xlog.Info("Actions", actions)
opts := []Option{
WithModel(model),
WithLLMAPIURL(a.apiURL),
@@ -163,7 +164,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
WithTimeout(timeout),
WithRAGDB(a.ragDB),
WithAgentReasoningCallback(func(state ActionCurrentState) bool {
slog.Info("Reasoning", state.Reasoning)
xlog.Info("Reasoning", state.Reasoning)
manager.Send(
NewMessage(
fmt.Sprintf(`Thinking: %s`, htmlIfy(state.Reasoning)),
@@ -186,7 +187,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
a.agentStatus[name].addResult(state)
a.Unlock()
slog.Info("Reasoning", state.Reasoning)
xlog.Info("Reasoning", state.Reasoning)
text := fmt.Sprintf(`Reasoning: %s
Action taken: %+v
@@ -212,9 +213,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
if config.HUD {
opts = append(opts, EnableHUD)
}
if os.Getenv("DEBUG") != "" {
opts = append(opts, LogLevel(slog.LevelDebug))
}
if config.StandaloneJob {
opts = append(opts, EnableStandaloneJob)
}
@@ -240,7 +239,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
opts = append(opts, EnableKnowledgeBaseWithResults(config.KnowledgeBaseResults))
}
slog.Info("Starting agent", "name", name, "config", config)
xlog.Info("Starting agent", "name", name, "config", config)
agent, err := New(opts...)
if err != nil {
return err
@@ -251,7 +250,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
go func() {
if err := agent.Run(); err != nil {
slog.Info("Agent stop: ", err.Error())
xlog.Info("Agent stop: ", err.Error())
}
}()
@@ -302,7 +301,7 @@ func (a *AgentPool) Start(name string) error {
if err != nil {
return fmt.Errorf("agent %s failed to start: %w", name, err)
}
slog.Info("Agent started", "name", name)
xlog.Info("Agent started", "name", name)
return nil
}
if config, ok := a.pool[name]; ok {

View File

@@ -4,10 +4,11 @@ import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"os"
"github.com/mudler/local-agent-framework/xlog"
. "github.com/mudler/local-agent-framework/agent"
"github.com/donseba/go-htmx"
@@ -54,7 +55,7 @@ func (a *App) KnowledgeBaseFile(db *InMemoryDatabase) func(c *fiber.Ctx) error {
return err
}
slog.Info("File uploaded to: " + destination)
xlog.Info("File uploaded to: " + destination)
fmt.Printf("Payload: %+v\n", payload)
content, err := readPdf(destination) // Read local pdf file
@@ -62,7 +63,7 @@ func (a *App) KnowledgeBaseFile(db *InMemoryDatabase) func(c *fiber.Ctx) error {
panic(err)
}
slog.Info("Content is", content)
xlog.Info("Content is", content)
chunkSize := defaultChunkSize
if payload.ChunkSize > 0 {
chunkSize = payload.ChunkSize
@@ -131,7 +132,7 @@ func (a *App) Notify(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
if err := pool.Remove(c.Params("name")); err != nil {
slog.Info("Error removing agent", err)
xlog.Info("Error removing agent", err)
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
return c.Redirect("/agents")
@@ -140,7 +141,7 @@ func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Pause(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
slog.Info("Pausing agent", c.Params("name"))
xlog.Info("Pausing agent", c.Params("name"))
agent := pool.GetAgent(c.Params("name"))
if agent != nil {
agent.Pause()
@@ -218,7 +219,7 @@ func (a *App) ImportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
return err
}
slog.Info("Importing agent", config.Name)
xlog.Info("Importing agent", config.Name)
if config.Name == "" {
c.Status(http.StatusBadRequest).SendString("Name is required")
@@ -258,16 +259,16 @@ func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
go func() {
agent := pool.GetAgent(agentName)
if agent == nil {
slog.Info("Agent not found in pool", c.Params("name"))
xlog.Info("Agent not found in pool", c.Params("name"))
return
}
res := agent.Ask(
WithText(query),
)
if res.Error != nil {
slog.Error("Error asking agent", "agent", agentName, "error", res.Error)
xlog.Error("Error asking agent", "agent", agentName, "error", res.Error)
} else {
slog.Info("we got a response from the agent", "agent", agentName, "response", res.Response)
xlog.Info("we got a response from the agent", "agent", agentName, "response", res.Response)
}
manager.Send(
NewMessage(

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(

View File

@@ -2,7 +2,8 @@ package main
import (
"encoding/json"
"log/slog"
"github.com/mudler/local-agent-framework/xlog"
. "github.com/mudler/local-agent-framework/agent"
@@ -34,20 +35,20 @@ func (a *AgentConfig) availableConnectors() []Connector {
connectors := []Connector{}
for _, c := range a.Connector {
slog.Info("Set Connector", c)
xlog.Info("Set Connector", c)
var config map[string]string
if err := json.Unmarshal([]byte(c.Config), &config); err != nil {
slog.Info("Error unmarshalling connector config", err)
xlog.Info("Error unmarshalling connector config", err)
continue
}
slog.Info("Config", config)
xlog.Info("Config", config)
switch c.Type {
case ConnectorTelegram:
cc, err := connector.NewTelegramConnector(config)
if err != nil {
slog.Info("Error creating telegram connector", err)
xlog.Info("Error creating telegram connector", err)
continue
}

View File

@@ -3,10 +3,11 @@ package main
import (
"embed"
"log"
"log/slog"
"net/http"
"os"
"github.com/mudler/local-agent-framework/xlog"
"github.com/donseba/go-htmx"
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
@@ -79,9 +80,9 @@ func main() {
}
if len(db.Database) > 0 && kbdisableIndexing != "true" {
slog.Info("Loading knowledgebase from disk, to skip run with KBDISABLEINDEX=true")
xlog.Info("Loading knowledgebase from disk, to skip run with KBDISABLEINDEX=true")
if err := db.SaveToStore(); err != nil {
slog.Info("Error storing in the KB", err)
xlog.Info("Error storing in the KB", err)
}
}

View File

@@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"github.com/mudler/local-agent-framework/xlog"
"net/http"
"os"
"path/filepath"
@@ -121,7 +123,7 @@ func getWebPage(url string) (string, error) {
func Sitemap(url string) (res []string, err error) {
err = sitemap.ParseFromSite(url, func(e sitemap.Entry) error {
slog.Info("Sitemap page: " + e.GetLocation())
xlog.Info("Sitemap page: " + e.GetLocation())
content, err := getWebPage(e.GetLocation())
if err == nil {
res = append(res, content)
@@ -134,10 +136,10 @@ func Sitemap(url string) (res []string, err error) {
func WebsiteToKB(website string, chunkSize int, db *InMemoryDatabase) {
content, err := Sitemap(website)
if err != nil {
slog.Info("Error walking sitemap for website", err)
xlog.Info("Error walking sitemap for website", err)
}
slog.Info("Found pages: ", len(content))
slog.Info("ChunkSize: ", chunkSize)
xlog.Info("Found pages: ", len(content))
xlog.Info("ChunkSize: ", chunkSize)
StringsToKB(db, chunkSize, content...)
}
@@ -145,9 +147,9 @@ func WebsiteToKB(website string, chunkSize int, db *InMemoryDatabase) {
func StringsToKB(db *InMemoryDatabase, chunkSize int, content ...string) {
for _, c := range content {
chunks := splitParagraphIntoChunks(c, chunkSize)
slog.Info("chunks: ", len(chunks))
xlog.Info("chunks: ", len(chunks))
for _, chunk := range chunks {
slog.Info("Chunk size: ", len(chunk))
xlog.Info("Chunk size: ", len(chunk))
db.AddEntry(chunk)
}
@@ -155,7 +157,7 @@ func StringsToKB(db *InMemoryDatabase, chunkSize int, content ...string) {
}
if err := db.SaveToStore(); err != nil {
slog.Info("Error storing in the KB", err)
xlog.Info("Error storing in the KB", err)
}
}