From 5f780287aecedf08f94b6a1e0ae2a8822683a3fc Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Tue, 10 Dec 2024 17:55:10 +0300 Subject: Fix: display and upsert correct roles and icons --- README.md | 4 ++-- bot.go | 13 ++++++++----- config/config.go | 4 ++++ models/models.go | 11 ++++++----- pngmeta/metareader_test.go | 2 +- session.go | 40 ++++++++++++++++++++++------------------ storage/storage.go | 4 ++-- tui.go | 12 ++++++++---- 8 files changed, 53 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index d9598ba..4eeea47 100644 --- a/README.md +++ b/README.md @@ -49,5 +49,5 @@ - no selection focus on modal sys buttons after opening it a second time; (cannot reproduce) + - chat should contain char in it (one to many: char: []chats); + - all page names should be vars; -- normal case regen omits assistant icon; -- user icon (and role?) from config is not used; +- normal case regen omits assistant icon; + +- user icon (and role?) from config is not used; + diff --git a/bot.go b/bot.go index eb10e1d..b815be9 100644 --- a/bot.go +++ b/bot.go @@ -115,7 +115,8 @@ func chatRound(userMsg, role string, tv *tview.TextView, regen bool) { return } go sendMsgToLLM(reader) - if userMsg != "" && !regen { // no need to write assistant icon since we continue old message + // if userMsg != "" && !regen { // no need to write assistant icon since we continue old message + if userMsg != "" || regen { fmt.Fprintf(tv, "(%d) ", len(chatBody.Messages)) fmt.Fprint(tv, cfg.AssistantIcon) fmt.Fprint(tv, "\n") @@ -181,7 +182,7 @@ func chatToTextSlice(showSys bool) []string { if !showSys && (msg.Role != cfg.AssistantRole && msg.Role != cfg.UserRole) { continue } - resp[i] = msg.ToText(i) + resp[i] = msg.ToText(i, cfg) } return resp } @@ -193,6 +194,8 @@ func chatToText(showSys bool) string { func applyCharCard(cc *models.CharCard) { cfg.AssistantRole = cc.Role + // TODO: need map role->icon + cfg.AssistantIcon = "<" + cc.Role + ">: " // try to load last active chat history, err := loadAgentsLastChat(cfg.AssistantRole) if err != nil { @@ -254,12 +257,12 @@ func charToStart(agentName string) bool { // } func init() { - cfg = config.LoadConfigOrDefault("config.example.toml") + cfg = config.LoadConfigOrDefault("config.toml") defaultStarter = []models.RoleMsg{ {Role: "system", Content: basicSysMsg}, {Role: cfg.AssistantRole, Content: defaultFirstMsg}, } - file, err := os.OpenFile(cfg.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + logfile, err := os.OpenFile(cfg.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { logger.Error("failed to open log file", "error", err, "filename", cfg.LogFile) return @@ -273,7 +276,7 @@ func init() { basicCard.Role = cfg.AssistantRole toolCard.Role = cfg.AssistantRole // - logger = slog.New(slog.NewTextHandler(file, nil)) + logger = slog.New(slog.NewTextHandler(logfile, nil)) store = storage.NewProviderSQL("test.db", logger) // https://github.com/coreydaley/ggerganov-llama.cpp/blob/master/examples/server/README.md // load all chats in memory diff --git a/config/config.go b/config/config.go index 0561d51..074bfda 100644 --- a/config/config.go +++ b/config/config.go @@ -34,8 +34,12 @@ func LoadConfigOrDefault(fn string) *Config { config.UserRole = "user" config.ToolRole = "tool" config.AssistantRole = "assistant" + config.AssistantIcon = ": " + config.UserIcon = ": " + config.UserIcon = ": " config.SysDir = "sysprompts" config.ChunkLimit = 8192 } + // if any value is empty fill with default return config } diff --git a/models/models.go b/models/models.go index 82ff63d..2c5c99c 100644 --- a/models/models.go +++ b/models/models.go @@ -1,6 +1,7 @@ package models import ( + "elefant/config" "fmt" "strings" ) @@ -55,21 +56,21 @@ type RoleMsg struct { Content string `json:"content"` } -func (m RoleMsg) ToText(i int) string { +func (m RoleMsg) ToText(i int, cfg *config.Config) string { icon := "" switch m.Role { case "assistant": - icon = fmt.Sprintf("(%d) <🤖>: ", i) + icon = fmt.Sprintf("(%d) %s", i, cfg.AssistantIcon) case "user": - icon = fmt.Sprintf("(%d) : ", i) + icon = fmt.Sprintf("(%d) %s", i, cfg.UserIcon) case "system": icon = fmt.Sprintf("(%d) : ", i) case "tool": - icon = fmt.Sprintf("(%d) : ", i) + icon = fmt.Sprintf("(%d) %s", i, cfg.ToolIcon) default: icon = fmt.Sprintf("(%d) <%s>: ", i, m.Role) } - textMsg := fmt.Sprintf("[-:-:u]%s[-:-:-]\n%s\n", icon, m.Content) + textMsg := fmt.Sprintf("[-:-:b]%s[-:-:-]\n%s\n", icon, m.Content) return strings.ReplaceAll(textMsg, "\n\n", "\n") } diff --git a/pngmeta/metareader_test.go b/pngmeta/metareader_test.go index 51dadc6..798fa6d 100644 --- a/pngmeta/metareader_test.go +++ b/pngmeta/metareader_test.go @@ -27,7 +27,7 @@ func TestReadMeta(t *testing.T) { if err != nil { t.Errorf("Expected no error, but got %v\n", err) } - fmt.Printf("%+v\n", v.Simplify("Adam")) + fmt.Printf("%+v\n", v.Simplify("Adam", tc.Filename)) }) } } diff --git a/session.go b/session.go index 21b5b9e..e34c98c 100644 --- a/session.go +++ b/session.go @@ -57,12 +57,12 @@ 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 @@ -98,33 +98,37 @@ func loadAgentsLastChat(agent string) ([]models.RoleMsg, error) { } func loadOldChatOrGetNew() []models.RoleMsg { - newChat := &models.Chat{ - ID: 0, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - } - newChat.Name = fmt.Sprintf("%d_%v", newChat.ID, newChat.CreatedAt.Unix()) // 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 + // TODO: update assistant icon + cfg.AssistantIcon = "<" + chat.Agent + ">: " return history } diff --git a/storage/storage.go b/storage/storage.go index 9a1595c..b876dbc 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -56,8 +56,8 @@ func (p ProviderSQL) GetLastChatByAgent(agent string) (*models.Chat, error) { func (p ProviderSQL) UpsertChat(chat *models.Chat) (*models.Chat, error) { // Prepare the SQL statement query := ` - INSERT OR REPLACE INTO chats (id, name, msgs, created_at, updated_at) - VALUES (:id, :name, :msgs, :created_at, :updated_at) + INSERT OR REPLACE INTO chats (id, name, msgs, agent, created_at, updated_at) + VALUES (:id, :name, :msgs, :agent, :created_at, :updated_at) RETURNING *;` stmt, err := p.db.PrepareNamed(query) if err != nil { diff --git a/tui.go b/tui.go index fd1946a..9493ffb 100644 --- a/tui.go +++ b/tui.go @@ -92,7 +92,6 @@ func startNewChat() { chatMap[newChat.Name] = newChat updateStatusLine() colorText() - return } func init() { @@ -313,7 +312,10 @@ func init() { textView.SetText(chatToText(cfg.ShowSys)) colorText() textView.ScrollToEnd() - initSysCards() + _, err = initSysCards() + if err != nil { + logger.Error("failed to init sys cards", "error", err) + } app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyF1 { chatList, err := loadHistoryChats() @@ -400,7 +402,9 @@ func init() { logger.Error("failed to export chat;", "error", err, "chat_name", activeChatName) return nil } - notifyUser("exported chat", "chat: "+activeChatName+" was exported") + if err := notifyUser("exported chat", "chat: "+activeChatName+" was exported"); err != nil { + logger.Error("failed to send notification", "error", err) + } return nil } if event.Key() == tcell.KeyCtrlA { @@ -442,7 +446,7 @@ func init() { nl = "" } if msgText != "" { - fmt.Fprintf(textView, "%s[-:-:u](%d) <%s>: [-:-:-]\n%s\n", + fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n", nl, len(chatBody.Messages), cfg.UserRole, msgText) textArea.SetText("", true) textView.ScrollToEnd() -- cgit v1.2.3