diff --git a/pkg/tui/model/model.go b/pkg/tui/model/model.go index c91e518..60132ec 100644 --- a/pkg/tui/model/model.go +++ b/pkg/tui/model/model.go @@ -47,6 +47,14 @@ func (m *AppModel) LoadConversations() (error, []LoadedConversation) { return nil, conversations } +func (a *AppModel) LoadConversationRootMessages() ([]api.Message, error) { + messages, err := a.Ctx.Store.RootMessages(a.Conversation.ID) + if err != nil { + return nil, fmt.Errorf("Could not load conversation root messages: %v %v", a.Conversation.SelectedRoot, err) + } + return messages, nil +} + func (a *AppModel) LoadConversationMessages() ([]api.Message, error) { messages, err := a.Ctx.Store.PathToLeaf(a.Conversation.SelectedRoot) if err != nil { diff --git a/pkg/tui/views/chat/chat.go b/pkg/tui/views/chat/chat.go index 699c4f2..2465c4f 100644 --- a/pkg/tui/views/chat/chat.go +++ b/pkg/tui/views/chat/chat.go @@ -16,11 +16,6 @@ import ( // custom tea.Msg types type ( - // sent when a conversation is (re)loaded - msgConversationLoaded struct { - conversation *api.Conversation - rootMessages []api.Message - } // sent when a new conversation title generated msgConversationTitleGenerated string // sent when the conversation has been persisted, triggers a reload of contents @@ -30,7 +25,10 @@ type ( messages []api.Message } // sent when a conversation's messages are laoded - msgMessagesLoaded []api.Message + msgConversationMessagesLoaded struct { + messages []api.Message + rootMessages []api.Message + } // a special case of common.MsgError that stops the response waiting animation msgChatResponseError error // sent on each chunk received from LLM diff --git a/pkg/tui/views/chat/cmds.go b/pkg/tui/views/chat/cmds.go index ae1bf74..4fccbf0 100644 --- a/pkg/tui/views/chat/cmds.go +++ b/pkg/tui/views/chat/cmds.go @@ -21,7 +21,13 @@ func (m *Model) loadConversationMessages() tea.Cmd { if err != nil { return shared.MsgError(err) } - return msgMessagesLoaded(messages) + rootMessages, err := m.App.LoadConversationRootMessages() + if err != nil { + return shared.MsgError(err) + } + return msgConversationMessagesLoaded{ + messages, rootMessages, + } } } diff --git a/pkg/tui/views/chat/update.go b/pkg/tui/views/chat/update.go index 87ff9da..fb87285 100644 --- a/pkg/tui/views/chat/update.go +++ b/pkg/tui/views/chat/update.go @@ -94,17 +94,11 @@ func (m Model) Update(msg tea.Msg) (shared.ViewModel, tea.Cmd) { } } } - case msgConversationLoaded: - m.App.Conversation = msg.conversation + case msgConversationMessagesLoaded: m.App.RootMessages = msg.rootMessages - m.selectedMessage = -1 - if len(m.App.RootMessages) > 0 { - cmds = append(cmds, m.loadConversationMessages()) - } - case msgMessagesLoaded: - m.App.Messages = msg + m.App.Messages = msg.messages if m.selectedMessage == -1 { - m.selectedMessage = len(msg) - 1 + m.selectedMessage = len(msg.messages) - 1 } else { m.selectedMessage = min(m.selectedMessage, len(m.App.Messages)) }