Feat: export chat to json file;

This commit is contained in:
Grail Finder
2024-12-08 14:18:16 +03:00
parent 55010bb704
commit bdd40ea8df
3 changed files with 30 additions and 4 deletions

View File

@@ -69,7 +69,7 @@ func (m RoleMsg) ToText(i int) string {
default: default:
icon = fmt.Sprintf("(%d) <%s>: ", i, m.Role) icon = fmt.Sprintf("(%d) <%s>: ", i, m.Role)
} }
textMsg := fmt.Sprintf("%s\n%s\n", icon, m.Content) textMsg := fmt.Sprintf("[-:-:u]%s[-:-:-]\n%s\n", icon, m.Content)
return strings.ReplaceAll(textMsg, "\n\n", "\n") return strings.ReplaceAll(textMsg, "\n\n", "\n")
} }

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"strings" "strings"
"time" "time"
@@ -25,6 +26,14 @@ func historyToSJSON(msgs []models.RoleMsg) (string, error) {
return string(data), nil return string(data), nil
} }
func exportChat() error {
data, err := json.MarshalIndent(chatBody.Messages, "", " ")
if err != nil {
return err
}
return os.WriteFile(activeChatName+".json", data, 0666)
}
func updateStorageChat(name string, msgs []models.RoleMsg) error { func updateStorageChat(name string, msgs []models.RoleMsg) error {
var err error var err error
chat, ok := chatMap[name] chat, ok := chatMap[name]

23
tui.go
View File

@@ -37,6 +37,7 @@ var (
[yellow]F7[white]: copy last msg to clipboard (linux xclip) [yellow]F7[white]: copy last msg to clipboard (linux xclip)
[yellow]F8[white]: copy n msg to clipboard (linux xclip) [yellow]F8[white]: copy n msg to clipboard (linux xclip)
[yellow]Ctrl+s[white]: choose/replace system prompt [yellow]Ctrl+s[white]: choose/replace system prompt
[yellow]Ctrl+e[white]: export chat to json file
Press Enter to go back Press Enter to go back
` `
@@ -168,6 +169,7 @@ func init() {
applyCharCard(cc) applyCharCard(cc)
// replace textview // replace textview
textView.SetText(chatToText(cfg.ShowSys)) textView.SetText(chatToText(cfg.ShowSys))
colorText()
sysModal.ClearButtons() sysModal.ClearButtons()
pages.RemovePage("sys") pages.RemovePage("sys")
app.SetFocus(textArea) app.SetFocus(textArea)
@@ -319,10 +321,12 @@ func init() {
if strings.HasSuffix(text, cfg.AssistantIcon) { if strings.HasSuffix(text, cfg.AssistantIcon) {
logger.Info("deleting assistant icon", "icon", cfg.AssistantIcon) logger.Info("deleting assistant icon", "icon", cfg.AssistantIcon)
textView.SetText(strings.TrimSuffix(text, cfg.AssistantIcon)) textView.SetText(strings.TrimSuffix(text, cfg.AssistantIcon))
colorText()
return nil return nil
} }
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1] chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
textView.SetText(chatToText(cfg.ShowSys)) textView.SetText(chatToText(cfg.ShowSys))
colorText()
return nil return nil
} }
if event.Key() == tcell.KeyF4 { if event.Key() == tcell.KeyF4 {
@@ -335,6 +339,7 @@ func init() {
// switch cfg.ShowSys // switch cfg.ShowSys
cfg.ShowSys = !cfg.ShowSys cfg.ShowSys = !cfg.ShowSys
textView.SetText(chatToText(cfg.ShowSys)) textView.SetText(chatToText(cfg.ShowSys))
colorText()
} }
if event.Key() == tcell.KeyF6 { if event.Key() == tcell.KeyF6 {
interruptResp = true interruptResp = true
@@ -370,8 +375,12 @@ func init() {
return nil return nil
} }
if event.Key() == tcell.KeyCtrlE { if event.Key() == tcell.KeyCtrlE {
textArea.SetText("pressed ctrl+e", true) // export loaded chat into json file
colorText() if err := exportChat(); err != nil {
logger.Error("failed to export chat;", "error", err, "chat_name", activeChatName)
return nil
}
notifyUser("exported chat", "chat: "+activeChatName+" was exported")
return nil return nil
} }
if event.Key() == tcell.KeyCtrlA { if event.Key() == tcell.KeyCtrlA {
@@ -406,8 +415,16 @@ func init() {
msgText := textArea.GetText() msgText := textArea.GetText()
// TODO: check whose message was latest (user icon / assistant) // TODO: check whose message was latest (user icon / assistant)
// in order to decide if assistant new icon is needed // in order to decide if assistant new icon is needed
nl := "\n"
prevText := textView.GetText(true)
// strings.LastIndex()
// newline is not needed is prev msg ends with one
if strings.HasSuffix(prevText, nl) {
nl = ""
}
if msgText != "" { if msgText != "" {
fmt.Fprintf(textView, "\n(%d) <user>: \n%s\n", len(chatBody.Messages), msgText) fmt.Fprintf(textView, "%s[-:-:u](%d) <%s>: [-:-:-]\n%s\n",
nl, len(chatBody.Messages), cfg.UserRole, msgText)
textArea.SetText("", true) textArea.SetText("", true)
textView.ScrollToEnd() textView.ScrollToEnd()
colorText() colorText()