From 383d34f31107d8492178a2beda66d977afb08c5b Mon Sep 17 00:00:00 2001 From: Matt Low Date: Wed, 13 Mar 2024 23:05:42 +0000 Subject: [PATCH] tui: persist new conversations as well --- pkg/tui/tui.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pkg/tui/tui.go b/pkg/tui/tui.go index c9df9c5..9e1c828 100644 --- a/pkg/tui/tui.go +++ b/pkg/tui/tui.go @@ -63,7 +63,7 @@ type ( // sent when response is finished being received msgResponseEnd string // sent on each completed reply - msgReply models.Message + msgAssistantReply models.Message // sent when a conversation is (re)loaded msgConversationLoaded *models.Conversation // send when a conversation's messages are laoded @@ -155,7 +155,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } m.updateContent() cmds = append(cmds, m.waitForChunk()) // wait for the next chunk - case msgReply: + case msgAssistantReply: // the last reply that was being worked on is finished reply := models.Message(msg) last := len(m.messages) - 1 @@ -168,8 +168,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.addMessage(reply) } - if m.persistence && m.conversation != nil && m.conversation.ID > 0 { - cmds = append(cmds, m.persistRecentMessages()) + if m.persistence { + var err error + if m.conversation.ID == 0 { + err = m.ctx.Store.SaveConversation(m.conversation) + } + if err != nil { + cmds = append(cmds, wrapError(err)) + } else { + cmds = append(cmds, m.persistRecentMessages()) + } } m.updateContent() @@ -280,7 +288,8 @@ func initialModel(ctx *lmcli.Context, convShortname string) model { m := model{ ctx: ctx, convShortname: convShortname, - persistence: true, + conversation: &models.Conversation{}, + persistence: true, replyChan: make(chan models.Message), replyChunkChan: make(chan string), @@ -327,7 +336,15 @@ func (m *model) handleInputKey(msg tea.KeyMsg) tea.Cmd { Content: userInput, } - if m.persistence && m.conversation != nil && m.conversation.ID > 0 { + if m.persistence { + var err error + if m.conversation.ID == 0 { + err = m.ctx.Store.SaveConversation(m.conversation) + } + if err != nil { + return wrapError(err) + } + // ensure all messages up to the one we're about to add are // persistent cmd := m.persistRecentMessages() @@ -383,7 +400,7 @@ func (m *model) loadMessages(c *models.Conversation) tea.Cmd { func (m *model) waitForReply() tea.Cmd { return func() tea.Msg { - return msgReply(<-m.replyChan) + return msgAssistantReply(<-m.replyChan) } }