feat(agent): shared state, allow to track conversations globally (#148)

* feat(agent): shared state, allow to track conversations globally

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Cleanup

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* track conversations initiated by the bot

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

---------

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2025-05-11 22:23:01 +02:00
committed by GitHub
parent 2b07dd79ec
commit c23e655f44
63 changed files with 290 additions and 316 deletions

View File

@@ -2,8 +2,8 @@ package connectors
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/mudler/LocalAGI/core/agent"
@@ -14,9 +14,8 @@ import (
)
type Discord struct {
token string
defaultChannel string
conversationTracker *ConversationTracker[string]
token string
defaultChannel string
}
// NewDiscord creates a new Discord connector
@@ -25,11 +24,6 @@ type Discord struct {
// - defaultChannel: Discord channel to always answer even if not mentioned
func NewDiscord(config map[string]string) *Discord {
duration, err := time.ParseDuration(config["lastMessageDuration"])
if err != nil {
duration = 5 * time.Minute
}
token := config["token"]
if !strings.HasPrefix(token, "Bot ") {
@@ -37,9 +31,8 @@ func NewDiscord(config map[string]string) *Discord {
}
return &Discord{
conversationTracker: NewConversationTracker[string](duration),
token: token,
defaultChannel: config["defaultChannel"],
token: token,
defaultChannel: config["defaultChannel"],
}
}
@@ -157,12 +150,12 @@ func (d *Discord) handleThreadMessage(a *agent.Agent, s *discordgo.Session, m *d
func (d *Discord) handleChannelMessage(a *agent.Agent, s *discordgo.Session, m *discordgo.MessageCreate) {
d.conversationTracker.AddMessage(m.ChannelID, openai.ChatCompletionMessage{
a.SharedState().ConversationTracker.AddMessage(fmt.Sprintf("discord:%s", m.ChannelID), openai.ChatCompletionMessage{
Role: "user",
Content: m.Content,
})
conv := d.conversationTracker.GetConversation(m.ChannelID)
conv := a.SharedState().ConversationTracker.GetConversation(fmt.Sprintf("discord:%s", m.ChannelID))
jobResult := a.Ask(
types.WithConversationHistory(conv),
@@ -173,7 +166,7 @@ func (d *Discord) handleChannelMessage(a *agent.Agent, s *discordgo.Session, m *
return
}
d.conversationTracker.AddMessage(m.ChannelID, openai.ChatCompletionMessage{
a.SharedState().ConversationTracker.AddMessage(fmt.Sprintf("discord:%s", m.ChannelID), openai.ChatCompletionMessage{
Role: "assistant",
Content: jobResult.Response,
})