Automatically save all conversations

This commit is contained in:
Ettore Di Giacinto
2025-03-04 22:22:16 +01:00
parent 758a73e8ab
commit d288755444
6 changed files with 79 additions and 8 deletions

View File

@@ -2,6 +2,9 @@ package agent
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/mudler/LocalAgent/pkg/xlog"
"github.com/sashabaranov/go-openai"
@@ -60,9 +63,25 @@ func (a *Agent) knowledgeBaseLookup() {
}}, a.currentConversation...)
}
func (a *Agent) saveCurrentConversationInMemory() {
func (a *Agent) saveConversation(m Messages, prefix string) error {
if a.options.conversationsPath == "" {
return nil
}
dateTime := time.Now().Format("2006-01-02-15-04-05")
fileName := a.Character.Name + "-" + dateTime + ".json"
if prefix != "" {
fileName = prefix + "-" + fileName
}
os.MkdirAll(a.options.conversationsPath, os.ModePerm)
return m.Save(filepath.Join(a.options.conversationsPath, fileName))
}
func (a *Agent) saveCurrentConversation() {
if err := a.saveConversation(a.currentConversation, ""); err != nil {
xlog.Error("Error saving conversation", "error", err)
}
if !a.options.enableLongTermMemory && !a.options.enableSummaryMemory {
xlog.Debug("Long term memory is disabled", "agent", a.Character.Name)
return