tui: persist new conversations as well

This commit is contained in:
Matt Low 2024-03-13 23:05:42 +00:00
parent ac0e380244
commit 383d34f311

View File

@ -63,7 +63,7 @@ type (
// sent when response is finished being received // sent when response is finished being received
msgResponseEnd string msgResponseEnd string
// sent on each completed reply // sent on each completed reply
msgReply models.Message msgAssistantReply models.Message
// sent when a conversation is (re)loaded // sent when a conversation is (re)loaded
msgConversationLoaded *models.Conversation msgConversationLoaded *models.Conversation
// send when a conversation's messages are laoded // 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() m.updateContent()
cmds = append(cmds, m.waitForChunk()) // wait for the next chunk cmds = append(cmds, m.waitForChunk()) // wait for the next chunk
case msgReply: case msgAssistantReply:
// the last reply that was being worked on is finished // the last reply that was being worked on is finished
reply := models.Message(msg) reply := models.Message(msg)
last := len(m.messages) - 1 last := len(m.messages) - 1
@ -168,8 +168,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.addMessage(reply) m.addMessage(reply)
} }
if m.persistence && m.conversation != nil && m.conversation.ID > 0 { if m.persistence {
cmds = append(cmds, m.persistRecentMessages()) 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() m.updateContent()
@ -280,7 +288,8 @@ func initialModel(ctx *lmcli.Context, convShortname string) model {
m := model{ m := model{
ctx: ctx, ctx: ctx,
convShortname: convShortname, convShortname: convShortname,
persistence: true, conversation: &models.Conversation{},
persistence: true,
replyChan: make(chan models.Message), replyChan: make(chan models.Message),
replyChunkChan: make(chan string), replyChunkChan: make(chan string),
@ -327,7 +336,15 @@ func (m *model) handleInputKey(msg tea.KeyMsg) tea.Cmd {
Content: userInput, 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 // ensure all messages up to the one we're about to add are
// persistent // persistent
cmd := m.persistRecentMessages() cmd := m.persistRecentMessages()
@ -383,7 +400,7 @@ func (m *model) loadMessages(c *models.Conversation) tea.Cmd {
func (m *model) waitForReply() tea.Cmd { func (m *model) waitForReply() tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
return msgReply(<-m.replyChan) return msgAssistantReply(<-m.replyChan)
} }
} }