2024-06-02 16:40:46 -06:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
2024-09-15 18:48:45 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/model"
|
2024-06-02 16:40:46 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/shared"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
func (m *Model) setMessage(i int, msg api.Message) {
|
2024-09-15 18:48:45 -06:00
|
|
|
if i >= len(m.App.Messages) {
|
2024-06-02 16:40:46 -06:00
|
|
|
panic("i out of range")
|
|
|
|
}
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.Messages[i] = msg
|
2024-06-02 16:40:46 -06:00
|
|
|
m.messageCache[i] = m.renderMessage(i)
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
func (m *Model) addMessage(msg api.Message) {
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.Messages = append(m.App.Messages, msg)
|
|
|
|
m.messageCache = append(m.messageCache, m.renderMessage(len(m.App.Messages)-1))
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) setMessageContents(i int, content string) {
|
2024-09-15 18:48:45 -06:00
|
|
|
if i >= len(m.App.Messages) {
|
2024-06-02 16:40:46 -06:00
|
|
|
panic("i out of range")
|
|
|
|
}
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.Messages[i].Content = content
|
2024-06-02 16:40:46 -06:00
|
|
|
m.messageCache[i] = m.renderMessage(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) rebuildMessageCache() {
|
2024-09-15 18:48:45 -06:00
|
|
|
m.messageCache = make([]string, len(m.App.Messages))
|
|
|
|
for i := range m.App.Messages {
|
2024-06-02 16:40:46 -06:00
|
|
|
m.messageCache[i] = m.renderMessage(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) updateContent() {
|
|
|
|
atBottom := m.content.AtBottom()
|
|
|
|
m.content.SetContent(m.conversationMessagesView())
|
|
|
|
if atBottom {
|
|
|
|
m.content.GotoBottom()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 15:28:29 -06:00
|
|
|
func (m *Model) loadConversationMessages() tea.Cmd {
|
2024-06-02 16:40:46 -06:00
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
messages, err := m.App.LoadConversationMessages()
|
2024-06-02 16:40:46 -06:00
|
|
|
if err != nil {
|
2024-09-15 18:48:45 -06:00
|
|
|
return shared.MsgError(err)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
return msgMessagesLoaded(messages)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) generateConversationTitle() tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
title, err := m.App.GenerateConversationTitle(m.App.Messages)
|
2024-06-02 16:40:46 -06:00
|
|
|
if err != nil {
|
|
|
|
return shared.MsgError(err)
|
|
|
|
}
|
2024-06-08 15:28:29 -06:00
|
|
|
return msgConversationTitleGenerated(title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
func (m *Model) updateConversationTitle(conversation *api.Conversation) tea.Cmd {
|
2024-06-08 15:28:29 -06:00
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
err := m.App.UpdateConversationTitle(conversation)
|
2024-06-08 15:28:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return shared.WrapError(err)
|
|
|
|
}
|
|
|
|
return nil
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
func (m *Model) cloneMessage(message api.Message, selected bool) tea.Cmd {
|
2024-06-08 15:28:29 -06:00
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
msg, err := m.App.CloneMessage(message, selected)
|
2024-06-08 15:28:29 -06:00
|
|
|
if err != nil {
|
2024-09-15 18:48:45 -06:00
|
|
|
return shared.WrapError(err)
|
2024-06-08 15:28:29 -06:00
|
|
|
}
|
|
|
|
return msgMessageCloned(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
func (m *Model) updateMessageContent(message *api.Message) tea.Cmd {
|
2024-06-08 15:28:29 -06:00
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
err := m.App.UpdateMessageContent(message)
|
2024-06-08 15:28:29 -06:00
|
|
|
if err != nil {
|
2024-09-15 18:48:45 -06:00
|
|
|
return shared.WrapError(err)
|
2024-06-08 15:28:29 -06:00
|
|
|
}
|
|
|
|
return msgMessageUpdated(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
func (m *Model) cycleSelectedRoot(conv *api.Conversation, dir model.MessageCycleDirection) tea.Cmd {
|
|
|
|
if len(m.App.RootMessages) < 2 {
|
2024-06-08 15:28:29 -06:00
|
|
|
return nil
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-06-08 15:28:29 -06:00
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
nextRoot, err := m.App.CycleSelectedRoot(conv, m.App.RootMessages, dir)
|
2024-06-08 15:28:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return shared.WrapError(err)
|
|
|
|
}
|
|
|
|
return msgSelectedRootCycled(nextRoot)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
func (m *Model) cycleSelectedReply(message *api.Message, dir model.MessageCycleDirection) tea.Cmd {
|
2024-06-02 16:40:46 -06:00
|
|
|
if len(message.Replies) < 2 {
|
2024-06-08 15:28:29 -06:00
|
|
|
return nil
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-06-08 15:28:29 -06:00
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
nextReply, err := m.App.CycleSelectedReply(message, dir)
|
2024-06-08 15:28:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return shared.WrapError(err)
|
|
|
|
}
|
|
|
|
return msgSelectedReplyCycled(nextReply)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 15:28:29 -06:00
|
|
|
func (m *Model) persistConversation() tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
conversation, messages, err := m.App.PersistConversation(m.App.Conversation, m.App.Messages)
|
|
|
|
if err != nil {
|
|
|
|
return shared.MsgError(err)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-15 18:48:45 -06:00
|
|
|
return msgConversationPersisted{conversation.ID == 0, conversation, messages}
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
func (m *Model) executeToolCalls(toolCalls []api.ToolCall) tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
results, err := m.App.ExecuteToolCalls(toolCalls)
|
2024-06-12 02:35:07 -06:00
|
|
|
if err != nil {
|
|
|
|
return shared.MsgError(err)
|
|
|
|
}
|
|
|
|
return msgToolResults(results)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 16:40:46 -06:00
|
|
|
func (m *Model) promptLLM() tea.Cmd {
|
2024-06-08 15:58:39 -06:00
|
|
|
m.state = pendingResponse
|
2024-06-02 16:40:46 -06:00
|
|
|
m.replyCursor.Blink = false
|
|
|
|
|
|
|
|
m.startTime = time.Now()
|
|
|
|
m.elapsed = 0
|
2024-06-12 02:35:07 -06:00
|
|
|
m.tokenCount = 0
|
2024-06-02 16:40:46 -06:00
|
|
|
|
|
|
|
return func() tea.Msg {
|
2024-09-15 18:48:45 -06:00
|
|
|
resp, err := m.App.PromptLLM(m.App.Messages, m.chatReplyChunks, m.stopSignal)
|
2024-06-12 02:35:07 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return msgChatResponseError(err)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
return msgChatResponse(resp)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|