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

@@ -15,28 +15,22 @@ import (
)
type IRC struct {
server string
port string
nickname string
channel string
conn *irc.Connection
alwaysReply bool
conversationTracker *ConversationTracker[string]
server string
port string
nickname string
channel string
conn *irc.Connection
alwaysReply bool
}
func NewIRC(config map[string]string) *IRC {
duration, err := time.ParseDuration(config["lastMessageDuration"])
if err != nil {
duration = 5 * time.Minute
}
return &IRC{
server: config["server"],
port: config["port"],
nickname: config["nickname"],
channel: config["channel"],
alwaysReply: config["alwaysReply"] == "true",
conversationTracker: NewConversationTracker[string](duration),
server: config["server"],
port: config["port"],
nickname: config["nickname"],
channel: config["channel"],
alwaysReply: config["alwaysReply"] == "true",
}
}
@@ -115,7 +109,7 @@ func (i *IRC) Start(a *agent.Agent) {
cleanedMessage := cleanUpMessage(message, i.nickname)
go func() {
conv := i.conversationTracker.GetConversation(channel)
conv := a.SharedState().ConversationTracker.GetConversation(fmt.Sprintf("irc:%s", channel))
conv = append(conv,
openai.ChatCompletionMessage{
@@ -125,7 +119,7 @@ func (i *IRC) Start(a *agent.Agent) {
)
// Update the conversation history
i.conversationTracker.AddMessage(channel, openai.ChatCompletionMessage{
a.SharedState().ConversationTracker.AddMessage(fmt.Sprintf("irc:%s", channel), openai.ChatCompletionMessage{
Content: cleanedMessage,
Role: "user",
})
@@ -140,7 +134,7 @@ func (i *IRC) Start(a *agent.Agent) {
}
// Update the conversation history
i.conversationTracker.AddMessage(channel, openai.ChatCompletionMessage{
a.SharedState().ConversationTracker.AddMessage(fmt.Sprintf("irc:%s", channel), openai.ChatCompletionMessage{
Content: res.Response,
Role: "assistant",
})
@@ -209,7 +203,7 @@ func (i *IRC) Start(a *agent.Agent) {
// Start the IRC client in a goroutine
go i.conn.Loop()
go func() {
select {
select {
case <-a.Context().Done():
i.conn.Quit()
return
@@ -249,11 +243,5 @@ func IRCConfigMeta() []config.Field {
Label: "Always Reply",
Type: config.FieldTypeCheckbox,
},
{
Name: "lastMessageDuration",
Label: "Last Message Duration",
Type: config.FieldTypeText,
DefaultValue: "5m",
},
}
}