Better handling of newly saved conversations

When a new conversation is created in the chat view's
`persistConversation`, we now set `rootMessages` appropriately.
This commit is contained in:
Matt Low 2024-06-09 18:42:17 +00:00
parent a2c860252f
commit a22119f738
3 changed files with 7 additions and 3 deletions

View File

@ -35,6 +35,7 @@ type (
msgMessagesLoaded []models.Message msgMessagesLoaded []models.Message
// sent when the conversation has been persisted, triggers a reload of contents // sent when the conversation has been persisted, triggers a reload of contents
msgConversationPersisted struct { msgConversationPersisted struct {
isNew bool
conversation *models.Conversation conversation *models.Conversation
messages []models.Message messages []models.Message
} }

View File

@ -203,14 +203,14 @@ func (m *Model) persistConversation() tea.Cmd {
messages := m.messages messages := m.messages
var err error var err error
if m.conversation.ID == 0 { if conversation.ID == 0 {
return func() tea.Msg { return func() tea.Msg {
// Start a new conversation with all messages so far // Start a new conversation with all messages so far
conversation, messages, err = m.Shared.Ctx.Store.StartConversation(messages...) conversation, messages, err = m.Shared.Ctx.Store.StartConversation(messages...)
if err != nil { if err != nil {
return shared.MsgError(fmt.Errorf("Could not start new conversation: %v", err)) return shared.MsgError(fmt.Errorf("Could not start new conversation: %v", err))
} }
return msgConversationPersisted{conversation, messages} return msgConversationPersisted{true, conversation, messages}
} }
} }
@ -239,7 +239,7 @@ func (m *Model) persistConversation() tea.Cmd {
return fmt.Errorf("Error: no messages to reply to") return fmt.Errorf("Error: no messages to reply to")
} }
} }
return msgConversationPersisted{conversation, messages} return msgConversationPersisted{false, conversation, messages}
} }
} }

View File

@ -166,6 +166,9 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case msgConversationPersisted: case msgConversationPersisted:
m.conversation = msg.conversation m.conversation = msg.conversation
m.messages = msg.messages m.messages = msg.messages
if msg.isNew {
m.rootMessages = []models.Message{m.messages[0]}
}
m.rebuildMessageCache() m.rebuildMessageCache()
m.updateContent() m.updateContent()
case msgMessageCloned: case msgMessageCloned: