diff options
Diffstat (limited to 'tables.go')
-rw-r--r-- | tables.go | 242 |
1 files changed, 174 insertions, 68 deletions
@@ -7,16 +7,16 @@ import ( "strings" "time" - "elefant/models" - "elefant/pngmeta" - "elefant/rag" + "gf-lt/models" + "gf-lt/pngmeta" + "gf-lt/rag" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func makeChatTable(chatMap map[string]models.Chat) *tview.Table { - actions := []string{"load", "rename", "delete", "update card"} + actions := []string{"load", "rename", "delete", "update card", "move sysprompt onto 1st msg", "new_chat_from_card"} chatList := make([]string, len(chatMap)) i := 0 for name := range chatMap { @@ -26,9 +26,7 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table { rows, cols := len(chatMap), len(actions)+2 chatActTable := tview.NewTable(). SetBorders(true) - // for chatName, chat := range chatMap { for r := 0; r < rows; r++ { - // r := 0 for c := 0; c < cols; c++ { color := tcell.ColorWhite switch c { @@ -49,7 +47,6 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table { SetAlign(tview.AlignCenter)) } } - // r++ } chatActTable.Select(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) { if key == tcell.KeyEsc || key == tcell.KeyF1 { @@ -65,7 +62,6 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table { chatActTable.SetSelectable(false, false) selectedChat := chatList[row] defer pages.RemovePage(historyPage) - // notification := fmt.Sprintf("chat: %s; action: %s", selectedChat, tc.Text) switch tc.Text { case "load": history, err := loadHistoryChat(selectedChat) @@ -114,12 +110,12 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table { } return } - if chatBody.Messages[0].Role != "system" || chatBody.Messages[1].Role != agentName { - if err := notifyUser("error", "unexpected chat structure; card: "+agentName); err != nil { - logger.Warn("failed ot notify", "error", err) - } - return - } + // if chatBody.Messages[0].Role != "system" || chatBody.Messages[1].Role != agentName { + // if err := notifyUser("error", "unexpected chat structure; card: "+agentName); err != nil { + // logger.Warn("failed ot notify", "error", err) + // } + // return + // } // change sys_prompt + first msg cc.SysPrompt = chatBody.Messages[0].Content cc.FirstMsg = chatBody.Messages[1].Content @@ -128,6 +124,40 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table { "error", err) } return + case "move sysprompt onto 1st msg": + chatBody.Messages[1].Content = chatBody.Messages[0].Content + chatBody.Messages[1].Content + chatBody.Messages[0].Content = rpDefenitionSysMsg + textView.SetText(chatToText(cfg.ShowSys)) + activeChatName = selectedChat + pages.RemovePage(historyPage) + return + case "new_chat_from_card": + // Reread card from file and start fresh chat + fi := strings.Index(selectedChat, "_") + agentName := selectedChat[fi+1:] + cc, ok := sysMap[agentName] + if !ok { + logger.Warn("no such card", "agent", agentName) + if err := notifyUser("error", "no such card: "+agentName); err != nil { + logger.Warn("failed to notify", "error", err) + } + return + } + // Reload card from disk + newCard, err := pngmeta.ReadCard(cc.FilePath, cfg.UserRole) + if err != nil { + logger.Error("failed to reload charcard", "path", cc.FilePath, "error", err) + if err := notifyUser("error", "failed to reload card: "+cc.FilePath); err != nil { + logger.Warn("failed to notify", "error", err) + } + return + } + // Update sysMap with fresh card data + sysMap[agentName] = newCard + applyCharCard(newCard) + startNewChat() + pages.RemovePage(historyPage) + return default: return } @@ -135,7 +165,7 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table { return chatActTable } -// func makeRAGTable(fileList []string) *tview.Table { +// nolint:unused func makeRAGTable(fileList []string) *tview.Flex { actions := []string{"load", "delete"} rows, cols := len(fileList), len(actions)+1 @@ -237,59 +267,59 @@ func makeRAGTable(fileList []string) *tview.Flex { return ragflex } -func makeLoadedRAGTable(fileList []string) *tview.Table { - actions := []string{"delete"} - rows, cols := len(fileList), len(actions)+1 - fileTable := tview.NewTable(). - SetBorders(true) - for r := 0; r < rows; r++ { - for c := 0; c < cols; c++ { - color := tcell.ColorWhite - if c < 1 { - fileTable.SetCell(r, c, - tview.NewTableCell(fileList[r]). - SetTextColor(color). - SetAlign(tview.AlignCenter)) - } else { - fileTable.SetCell(r, c, - tview.NewTableCell(actions[c-1]). - SetTextColor(color). - SetAlign(tview.AlignCenter)) - } - } - } - fileTable.Select(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) { - if key == tcell.KeyEsc || key == tcell.KeyF1 { - pages.RemovePage(RAGPage) - return - } - if key == tcell.KeyEnter { - fileTable.SetSelectable(true, true) - } - }).SetSelectedFunc(func(row int, column int) { - defer pages.RemovePage(RAGPage) - tc := fileTable.GetCell(row, column) - tc.SetTextColor(tcell.ColorRed) - fileTable.SetSelectable(false, false) - fpath := fileList[row] - // notification := fmt.Sprintf("chat: %s; action: %s", fpath, tc.Text) - switch tc.Text { - case "delete": - if err := ragger.RemoveFile(fpath); err != nil { - logger.Error("failed to delete file", "filename", fpath, "error", err) - return - } - if err := notifyUser("chat deleted", fpath+" was deleted"); err != nil { - logger.Error("failed to send notification", "error", err) - } - return - default: - // pages.RemovePage(RAGPage) - return - } - }) - return fileTable -} +// func makeLoadedRAGTable(fileList []string) *tview.Table { +// actions := []string{"delete"} +// rows, cols := len(fileList), len(actions)+1 +// fileTable := tview.NewTable(). +// SetBorders(true) +// for r := 0; r < rows; r++ { +// for c := 0; c < cols; c++ { +// color := tcell.ColorWhite +// if c < 1 { +// fileTable.SetCell(r, c, +// tview.NewTableCell(fileList[r]). +// SetTextColor(color). +// SetAlign(tview.AlignCenter)) +// } else { +// fileTable.SetCell(r, c, +// tview.NewTableCell(actions[c-1]). +// SetTextColor(color). +// SetAlign(tview.AlignCenter)) +// } +// } +// } +// fileTable.Select(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) { +// if key == tcell.KeyEsc || key == tcell.KeyF1 { +// pages.RemovePage(RAGPage) +// return +// } +// if key == tcell.KeyEnter { +// fileTable.SetSelectable(true, true) +// } +// }).SetSelectedFunc(func(row int, column int) { +// defer pages.RemovePage(RAGPage) +// tc := fileTable.GetCell(row, column) +// tc.SetTextColor(tcell.ColorRed) +// fileTable.SetSelectable(false, false) +// fpath := fileList[row] +// // notification := fmt.Sprintf("chat: %s; action: %s", fpath, tc.Text) +// switch tc.Text { +// case "delete": +// if err := ragger.RemoveFile(fpath); err != nil { +// logger.Error("failed to delete file", "filename", fpath, "error", err) +// return +// } +// if err := notifyUser("chat deleted", fpath+" was deleted"); err != nil { +// logger.Error("failed to send notification", "error", err) +// } +// return +// default: +// // pages.RemovePage(RAGPage) +// return +// } +// }) +// return fileTable +// } func makeAgentTable(agentList []string) *tview.Table { actions := []string{"load"} @@ -427,3 +457,79 @@ func makeCodeBlockTable(codeBlocks []string) *tview.Table { }) return table } + +func makeImportChatTable(filenames []string) *tview.Table { + actions := []string{"load"} + rows, cols := len(filenames), len(actions)+1 + chatActTable := tview.NewTable(). + SetBorders(true) + for r := 0; r < rows; r++ { + for c := 0; c < cols; c++ { + color := tcell.ColorWhite + if c < 1 { + chatActTable.SetCell(r, c, + tview.NewTableCell(filenames[r]). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + } else { + chatActTable.SetCell(r, c, + tview.NewTableCell(actions[c-1]). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + } + } + } + chatActTable.Select(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEsc || key == tcell.KeyF1 { + pages.RemovePage(historyPage) + return + } + if key == tcell.KeyEnter { + chatActTable.SetSelectable(true, true) + } + }).SetSelectedFunc(func(row int, column int) { + tc := chatActTable.GetCell(row, column) + tc.SetTextColor(tcell.ColorRed) + chatActTable.SetSelectable(false, false) + selected := filenames[row] + // notification := fmt.Sprintf("chat: %s; action: %s", selectedChat, tc.Text) + switch tc.Text { + case "load": + if err := importChat(selected); err != nil { + logger.Warn("failed to import chat", "filename", selected) + pages.RemovePage(historyPage) + return + } + colorText() + updateStatusLine() + // redraw the text in text area + textView.SetText(chatToText(cfg.ShowSys)) + pages.RemovePage(historyPage) + app.SetFocus(textArea) + return + case "rename": + pages.RemovePage(historyPage) + pages.AddPage(renamePage, renameWindow, true, true) + return + case "delete": + sc, ok := chatMap[selected] + if !ok { + // no chat found + pages.RemovePage(historyPage) + return + } + if err := store.RemoveChat(sc.ID); err != nil { + logger.Error("failed to remove chat from db", "chat_id", sc.ID, "chat_name", sc.Name) + } + if err := notifyUser("chat deleted", selected+" was deleted"); err != nil { + logger.Error("failed to send notification", "error", err) + } + pages.RemovePage(historyPage) + return + default: + pages.RemovePage(historyPage) + return + } + }) + return chatActTable +} |