summaryrefslogtreecommitdiff
path: root/session.go
diff options
context:
space:
mode:
Diffstat (limited to 'session.go')
-rw-r--r--session.go99
1 files changed, 75 insertions, 24 deletions
diff --git a/session.go b/session.go
index 23d725d..dbfa645 100644
--- a/session.go
+++ b/session.go
@@ -1,10 +1,13 @@
package main
import (
- "elefant/models"
+ "gf-lt/models"
"encoding/json"
+ "errors"
"fmt"
+ "os"
"os/exec"
+ "path/filepath"
"strings"
"time"
)
@@ -13,18 +16,44 @@ var (
chatMap = make(map[string]*models.Chat)
)
-func historyToSJSON(msgs []models.MessagesStory) (string, error) {
+func historyToSJSON(msgs []models.RoleMsg) (string, error) {
data, err := json.Marshal(msgs)
if err != nil {
return "", err
}
if data == nil {
- return "", fmt.Errorf("nil data")
+ return "", errors.New("nil data")
}
return string(data), nil
}
-func updateStorageChat(name string, msgs []models.MessagesStory) error {
+func exportChat() error {
+ data, err := json.MarshalIndent(chatBody.Messages, "", " ")
+ if err != nil {
+ return err
+ }
+ return os.WriteFile(activeChatName+".json", data, 0666)
+}
+
+func importChat(filename string) error {
+ data, err := os.ReadFile(filename)
+ if err != nil {
+ return err
+ }
+ messages := []models.RoleMsg{}
+ if err := json.Unmarshal(data, &messages); err != nil {
+ return err
+ }
+ activeChatName = filepath.Base(filename)
+ chatBody.Messages = messages
+ cfg.AssistantRole = messages[1].Role
+ if cfg.AssistantRole == cfg.UserRole {
+ cfg.AssistantRole = messages[2].Role
+ }
+ return nil
+}
+
+func updateStorageChat(name string, msgs []models.RoleMsg) error {
var err error
chat, ok := chatMap[name]
if !ok {
@@ -37,6 +66,7 @@ func updateStorageChat(name string, msgs []models.MessagesStory) error {
return err
}
chat.UpdatedAt = time.Now()
+ // if new chat will create id
_, err = store.UpsertChat(chat)
return err
}
@@ -46,56 +76,77 @@ func loadHistoryChats() ([]string, error) {
if err != nil {
return nil, err
}
- resp := []string{}
- for _, chat := range chats {
+ resp := make([]string, len(chats))
+ for i, chat := range chats {
if chat.Name == "" {
- chat.Name = fmt.Sprintf("%d_%v", chat.ID, chat.CreatedAt.Unix())
+ chat.Name = fmt.Sprintf("%d_%v", chat.ID, chat.Agent)
}
- resp = append(resp, chat.Name)
+ resp[i] = chat.Name
chatMap[chat.Name] = &chat
}
return resp, nil
}
-func loadHistoryChat(chatName string) ([]models.MessagesStory, error) {
+func loadHistoryChat(chatName string) ([]models.RoleMsg, error) {
chat, ok := chatMap[chatName]
if !ok {
- err := fmt.Errorf("failed to read chat")
+ err := errors.New("failed to read chat")
logger.Error("failed to read chat", "name", chatName)
return nil, err
}
activeChatName = chatName
+ cfg.AssistantRole = chat.Agent
return chat.ToHistory()
}
-func loadOldChatOrGetNew() []models.MessagesStory {
- newChat := &models.Chat{
- ID: 0,
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
+func loadAgentsLastChat(agent string) ([]models.RoleMsg, error) {
+ chat, err := store.GetLastChatByAgent(agent)
+ if err != nil {
+ return nil, err
}
- newChat.Name = fmt.Sprintf("%d_%v", newChat.ID, newChat.CreatedAt.Unix())
+ history, err := chat.ToHistory()
+ if err != nil {
+ return nil, err
+ }
+ if chat.Name == "" {
+ logger.Warn("empty chat name", "id", chat.ID)
+ chat.Name = fmt.Sprintf("%s_%d", chat.Agent, chat.ID)
+ }
+ chatMap[chat.Name] = chat
+ activeChatName = chat.Name
+ return history, nil
+}
+
+func loadOldChatOrGetNew() []models.RoleMsg {
// find last chat
chat, err := store.GetLastChat()
if err != nil {
logger.Warn("failed to load history chat", "error", err)
- activeChatName = newChat.Name
- chatMap[newChat.Name] = newChat
+ chat := &models.Chat{
+ ID: 0,
+ CreatedAt: time.Now(),
+ UpdatedAt: time.Now(),
+ Agent: cfg.AssistantRole,
+ }
+ chat.Name = fmt.Sprintf("%s_%v", chat.Agent, chat.CreatedAt.Unix())
+ activeChatName = chat.Name
+ chatMap[chat.Name] = chat
return defaultStarter
}
history, err := chat.ToHistory()
if err != nil {
logger.Warn("failed to load history chat", "error", err)
- activeChatName = newChat.Name
- chatMap[newChat.Name] = newChat
+ activeChatName = chat.Name
+ chatMap[chat.Name] = chat
return defaultStarter
}
- if chat.Name == "" {
- logger.Warn("empty chat name", "id", chat.ID)
- chat.Name = fmt.Sprintf("%d_%v", chat.ID, chat.CreatedAt.Unix())
- }
+ // if chat.Name == "" {
+ // logger.Warn("empty chat name", "id", chat.ID)
+ // chat.Name = fmt.Sprintf("%s_%v", chat.Agent, chat.CreatedAt.Unix())
+ // }
chatMap[chat.Name] = chat
activeChatName = chat.Name
+ cfg.AssistantRole = chat.Agent
return history
}