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
1 changed files with 24 additions and 7 deletions

View File

@ -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,9 +168,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.addMessage(reply)
}
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 {
cmds = append(cmds, wrapError(err))
} else {
cmds = append(cmds, m.persistRecentMessages())
}
}
m.updateContent()
cmds = append(cmds, m.waitForReply())
@ -280,6 +288,7 @@ func initialModel(ctx *lmcli.Context, convShortname string) model {
m := model{
ctx: ctx,
convShortname: convShortname,
conversation: &models.Conversation{},
persistence: true,
replyChan: make(chan models.Message),
@ -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)
}
}