2024-06-02 16:40:46 -06:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
2024-06-02 16:40:46 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/shared"
|
|
|
|
tuiutil "git.mlow.ca/mlow/lmcli/pkg/tui/util"
|
|
|
|
"github.com/charmbracelet/bubbles/cursor"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
2024-09-16 08:04:08 -06:00
|
|
|
func (m *Model) setMessage(i int, msg api.Message) {
|
|
|
|
if i >= len(m.App.Messages) {
|
|
|
|
panic("i out of range")
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-16 08:04:08 -06:00
|
|
|
m.App.Messages[i] = msg
|
|
|
|
m.messageCache[i] = m.renderMessage(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) addMessage(msg api.Message) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-09-16 08:04:08 -06:00
|
|
|
func (m *Model) setMessageContents(i int, content string) {
|
|
|
|
if i >= len(m.App.Messages) {
|
|
|
|
panic("i out of range")
|
|
|
|
}
|
|
|
|
m.App.Messages[i].Content = content
|
|
|
|
m.messageCache[i] = m.renderMessage(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) rebuildMessageCache() {
|
|
|
|
m.messageCache = make([]string, len(m.App.Messages))
|
|
|
|
for i := range m.App.Messages {
|
|
|
|
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-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-22 17:34:53 -06:00
|
|
|
func (m *Model) Update(msg tea.Msg) (shared.ViewModel, tea.Cmd) {
|
2024-09-16 09:40:04 -06:00
|
|
|
inputHandled := false
|
|
|
|
|
2024-06-02 16:40:46 -06:00
|
|
|
var cmds []tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
2024-09-16 09:40:04 -06:00
|
|
|
case tea.KeyMsg:
|
|
|
|
cmd := m.handleInput(msg)
|
|
|
|
if cmd != nil {
|
|
|
|
inputHandled = true
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
2024-06-08 15:28:29 -06:00
|
|
|
case tea.WindowSizeMsg:
|
2024-09-16 08:04:08 -06:00
|
|
|
m.Width, m.Height = msg.Width, msg.Height
|
|
|
|
m.content.Width = msg.Width
|
|
|
|
m.input.SetWidth(msg.Width - m.input.FocusedStyle.Base.GetHorizontalFrameSize())
|
|
|
|
if len(m.App.Messages) > 0 {
|
|
|
|
m.rebuildMessageCache()
|
|
|
|
m.updateContent()
|
|
|
|
}
|
2024-06-02 16:40:46 -06:00
|
|
|
case shared.MsgViewEnter:
|
|
|
|
// wake up spinners and cursors
|
|
|
|
cmds = append(cmds, cursor.Blink, m.spinner.Tick)
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
// Refresh view
|
2024-06-02 16:40:46 -06:00
|
|
|
m.rebuildMessageCache()
|
|
|
|
m.updateContent()
|
2024-09-15 18:48:45 -06:00
|
|
|
|
|
|
|
if m.App.Conversation != nil && m.App.Conversation.ID > 0 {
|
|
|
|
// (re)load conversation contents
|
|
|
|
cmds = append(cmds, m.loadConversationMessages())
|
|
|
|
}
|
2024-06-02 16:40:46 -06:00
|
|
|
case tuiutil.MsgTempfileEditorClosed:
|
|
|
|
contents := string(msg)
|
|
|
|
switch m.editorTarget {
|
|
|
|
case input:
|
|
|
|
m.input.SetValue(contents)
|
|
|
|
case selectedMessage:
|
2024-09-15 18:48:45 -06:00
|
|
|
toEdit := m.App.Messages[m.selectedMessage]
|
2024-06-08 15:28:29 -06:00
|
|
|
if toEdit.Content != contents {
|
|
|
|
toEdit.Content = contents
|
|
|
|
m.setMessage(m.selectedMessage, toEdit)
|
|
|
|
if m.persistence && toEdit.ID > 0 {
|
|
|
|
// create clone of message with its new contents
|
|
|
|
cmds = append(cmds, m.cloneMessage(toEdit, true))
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-20 20:32:54 -06:00
|
|
|
case msgConversationMessagesLoaded:
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.RootMessages = msg.rootMessages
|
2024-09-20 20:32:54 -06:00
|
|
|
m.App.Messages = msg.messages
|
2024-06-08 15:28:29 -06:00
|
|
|
if m.selectedMessage == -1 {
|
2024-09-20 20:32:54 -06:00
|
|
|
m.selectedMessage = len(msg.messages) - 1
|
2024-06-08 15:28:29 -06:00
|
|
|
} else {
|
2024-09-15 18:48:45 -06:00
|
|
|
m.selectedMessage = min(m.selectedMessage, len(m.App.Messages))
|
2024-06-08 15:28:29 -06:00
|
|
|
}
|
2024-06-02 16:40:46 -06:00
|
|
|
m.rebuildMessageCache()
|
|
|
|
m.updateContent()
|
2024-06-12 02:35:07 -06:00
|
|
|
case msgChatResponseChunk:
|
2024-06-08 15:28:29 -06:00
|
|
|
cmds = append(cmds, m.waitForResponseChunk()) // wait for the next chunk
|
2024-06-02 16:40:46 -06:00
|
|
|
|
2024-06-08 17:37:58 -06:00
|
|
|
if msg.Content == "" {
|
2024-06-02 16:40:46 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
last := len(m.App.Messages) - 1
|
|
|
|
if last >= 0 && m.App.Messages[last].Role.IsAssistant() {
|
2024-06-02 16:40:46 -06:00
|
|
|
// append chunk to existing message
|
2024-09-15 18:48:45 -06:00
|
|
|
m.setMessageContents(last, m.App.Messages[last].Content+msg.Content)
|
2024-06-02 16:40:46 -06:00
|
|
|
} else {
|
2024-06-20 23:52:59 -06:00
|
|
|
// use chunk in a new message
|
2024-06-12 02:35:07 -06:00
|
|
|
m.addMessage(api.Message{
|
|
|
|
Role: api.MessageRoleAssistant,
|
2024-06-08 17:37:58 -06:00
|
|
|
Content: msg.Content,
|
2024-06-02 16:40:46 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
m.updateContent()
|
|
|
|
|
|
|
|
// show cursor and reset blink interval (simulate typing)
|
|
|
|
m.replyCursor.Blink = false
|
|
|
|
cmds = append(cmds, m.replyCursor.BlinkCmd())
|
|
|
|
|
2024-06-09 14:45:18 -06:00
|
|
|
m.tokenCount += msg.TokenCount
|
2024-06-02 16:40:46 -06:00
|
|
|
m.elapsed = time.Now().Sub(m.startTime)
|
2024-06-12 02:35:07 -06:00
|
|
|
case msgChatResponse:
|
|
|
|
m.state = idle
|
2024-06-02 16:40:46 -06:00
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
reply := (*api.Message)(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
reply.Content = strings.TrimSpace(reply.Content)
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
last := len(m.App.Messages) - 1
|
2024-06-02 16:40:46 -06:00
|
|
|
if last < 0 {
|
|
|
|
panic("Unexpected empty messages handling msgAssistantReply")
|
|
|
|
}
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
if m.App.Messages[last].Role.IsAssistant() {
|
2024-06-12 02:35:07 -06:00
|
|
|
// TODO: handle continuations gracefully - some models support them well, others fail horribly.
|
|
|
|
m.setMessage(last, *reply)
|
2024-06-02 16:40:46 -06:00
|
|
|
} else {
|
2024-06-12 02:35:07 -06:00
|
|
|
m.addMessage(*reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch reply.Role {
|
|
|
|
case api.MessageRoleToolCall:
|
|
|
|
// TODO: user confirmation before execution
|
|
|
|
// m.state = waitingForConfirmation
|
|
|
|
cmds = append(cmds, m.executeToolCalls(reply.ToolCalls))
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.persistence {
|
2024-06-08 15:28:29 -06:00
|
|
|
cmds = append(cmds, m.persistConversation())
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
if m.App.Conversation.Title == "" {
|
2024-06-02 16:40:46 -06:00
|
|
|
cmds = append(cmds, m.generateConversationTitle())
|
|
|
|
}
|
|
|
|
|
|
|
|
m.updateContent()
|
2024-06-12 02:35:07 -06:00
|
|
|
case msgChatResponseCanceled:
|
2024-06-08 15:58:39 -06:00
|
|
|
m.state = idle
|
2024-06-02 16:40:46 -06:00
|
|
|
m.updateContent()
|
2024-06-12 02:35:07 -06:00
|
|
|
case msgChatResponseError:
|
2024-06-08 15:58:39 -06:00
|
|
|
m.state = idle
|
2024-06-12 02:35:07 -06:00
|
|
|
m.updateContent()
|
2024-09-22 21:37:01 -06:00
|
|
|
return m, shared.WrapError(msg.Err)
|
2024-06-12 02:35:07 -06:00
|
|
|
case msgToolResults:
|
2024-09-15 18:48:45 -06:00
|
|
|
last := len(m.App.Messages) - 1
|
2024-06-12 02:35:07 -06:00
|
|
|
if last < 0 {
|
|
|
|
panic("Unexpected empty messages handling msgAssistantReply")
|
|
|
|
}
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
if m.App.Messages[last].Role != api.MessageRoleToolCall {
|
2024-06-12 02:35:07 -06:00
|
|
|
panic("Previous message not a tool call, unexpected")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.addMessage(api.Message{
|
|
|
|
Role: api.MessageRoleToolResult,
|
|
|
|
ToolResults: api.ToolResults(msg),
|
|
|
|
})
|
|
|
|
|
|
|
|
if m.persistence {
|
|
|
|
cmds = append(cmds, m.persistConversation())
|
|
|
|
}
|
|
|
|
|
2024-06-02 16:40:46 -06:00
|
|
|
m.updateContent()
|
2024-06-08 15:28:29 -06:00
|
|
|
case msgConversationTitleGenerated:
|
2024-06-02 16:40:46 -06:00
|
|
|
title := string(msg)
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.Conversation.Title = title
|
2024-06-02 16:40:46 -06:00
|
|
|
if m.persistence {
|
2024-09-15 18:48:45 -06:00
|
|
|
cmds = append(cmds, m.updateConversationTitle(m.App.Conversation))
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
case cursor.BlinkMsg:
|
2024-06-08 15:58:39 -06:00
|
|
|
if m.state == pendingResponse {
|
2024-06-08 15:28:29 -06:00
|
|
|
// ensure we show the updated "wait for response" cursor blink state
|
2024-09-15 18:48:45 -06:00
|
|
|
last := len(m.App.Messages) - 1
|
2024-06-20 23:52:59 -06:00
|
|
|
m.messageCache[last] = m.renderMessage(last)
|
2024-06-02 16:40:46 -06:00
|
|
|
m.updateContent()
|
|
|
|
}
|
2024-06-08 15:28:29 -06:00
|
|
|
case msgConversationPersisted:
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.Conversation = msg.conversation
|
|
|
|
m.App.Messages = msg.messages
|
2024-06-09 12:42:17 -06:00
|
|
|
if msg.isNew {
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.RootMessages = []api.Message{m.App.Messages[0]}
|
2024-06-09 12:42:17 -06:00
|
|
|
}
|
2024-06-08 15:28:29 -06:00
|
|
|
m.rebuildMessageCache()
|
|
|
|
m.updateContent()
|
|
|
|
case msgMessageCloned:
|
|
|
|
if msg.Parent == nil {
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.Conversation = msg.Conversation
|
|
|
|
m.App.RootMessages = append(m.App.RootMessages, *msg)
|
2024-06-08 15:28:29 -06:00
|
|
|
}
|
|
|
|
cmds = append(cmds, m.loadConversationMessages())
|
|
|
|
case msgSelectedRootCycled, msgSelectedReplyCycled, msgMessageUpdated:
|
|
|
|
cmds = append(cmds, m.loadConversationMessages())
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.spinner, cmd = m.spinner.Update(msg)
|
|
|
|
if cmd != nil {
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
m.replyCursor, cmd = m.replyCursor.Update(msg)
|
|
|
|
if cmd != nil {
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
prevInputLineCnt := m.input.LineCount()
|
2024-09-16 09:40:04 -06:00
|
|
|
|
|
|
|
if !inputHandled {
|
|
|
|
m.input, cmd = m.input.Update(msg)
|
|
|
|
if cmd != nil {
|
|
|
|
inputHandled = true
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
if !inputHandled {
|
2024-06-02 16:40:46 -06:00
|
|
|
m.content, cmd = m.content.Update(msg)
|
|
|
|
if cmd != nil {
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a pretty nasty hack to ensure the input area viewport doesn't
|
|
|
|
// scroll below its content, which can happen when the input viewport
|
|
|
|
// height has grown, or previously entered lines have been deleted
|
|
|
|
if prevInputLineCnt != m.input.LineCount() {
|
|
|
|
// dist is the distance we'd need to scroll up from the current cursor
|
|
|
|
// position to position the last input line at the bottom of the
|
|
|
|
// viewport. if negative, we're already scrolled above the bottom
|
|
|
|
dist := m.input.Line() - (m.input.LineCount() - m.input.Height())
|
|
|
|
if dist > 0 {
|
|
|
|
for i := 0; i < dist; i++ {
|
|
|
|
// move cursor up until content reaches the bottom of the viewport
|
|
|
|
m.input.CursorUp()
|
|
|
|
}
|
2024-06-08 15:28:29 -06:00
|
|
|
m.input, _ = m.input.Update(nil)
|
2024-06-02 16:40:46 -06:00
|
|
|
for i := 0; i < dist; i++ {
|
|
|
|
// move cursor back down to its previous position
|
|
|
|
m.input.CursorDown()
|
|
|
|
}
|
2024-06-08 15:28:29 -06:00
|
|
|
m.input, _ = m.input.Update(nil)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
if len(cmds) > 0 {
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
return m, nil
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|