2024-06-02 16:40:46 -06:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
2024-10-19 20:38:42 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/conversation"
|
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"
|
|
|
|
tuiutil "git.mlow.ca/mlow/lmcli/pkg/tui/util"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
func (m *Model) handleInput(msg tea.KeyMsg) tea.Cmd {
|
2024-06-02 16:40:46 -06:00
|
|
|
switch m.focus {
|
|
|
|
case focusInput:
|
2024-09-16 09:40:04 -06:00
|
|
|
cmd := m.handleInputKey(msg)
|
|
|
|
if cmd != nil {
|
|
|
|
return cmd
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
case focusMessages:
|
2024-09-16 09:40:04 -06:00
|
|
|
cmd := m.handleMessagesKey(msg)
|
|
|
|
if cmd != nil {
|
|
|
|
return cmd
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg.String() {
|
|
|
|
case "esc":
|
2024-06-08 15:58:39 -06:00
|
|
|
if m.state == pendingResponse {
|
2024-06-02 16:40:46 -06:00
|
|
|
m.stopSignal <- struct{}{}
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
return func() tea.Msg {
|
|
|
|
return shared.MsgViewChange(shared.ViewConversations)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
case "ctrl+c":
|
2024-06-08 15:58:39 -06:00
|
|
|
if m.state == pendingResponse {
|
2024-06-02 16:40:46 -06:00
|
|
|
m.stopSignal <- struct{}{}
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-30 14:21:51 -06:00
|
|
|
case "ctrl+g":
|
|
|
|
if m.state == pendingResponse {
|
|
|
|
m.stopSignal <- struct{}{}
|
|
|
|
return shared.KeyHandled(msg)
|
|
|
|
}
|
|
|
|
return func() tea.Msg {
|
|
|
|
return shared.MsgViewChange(shared.ViewSettings)
|
|
|
|
}
|
2024-06-02 16:40:46 -06:00
|
|
|
case "ctrl+p":
|
|
|
|
m.persistence = !m.persistence
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
case "ctrl+t":
|
2024-09-30 14:21:51 -06:00
|
|
|
m.showDetails = !m.showDetails
|
2024-06-02 16:40:46 -06:00
|
|
|
m.rebuildMessageCache()
|
|
|
|
m.updateContent()
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
case "ctrl+w":
|
|
|
|
m.wrap = !m.wrap
|
|
|
|
m.rebuildMessageCache()
|
|
|
|
m.updateContent()
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-09-20 20:47:03 -06:00
|
|
|
case "ctrl+n":
|
2024-09-22 21:00:03 -06:00
|
|
|
m.App.NewConversation()
|
2024-09-20 20:47:03 -06:00
|
|
|
m.rebuildMessageCache()
|
|
|
|
m.updateContent()
|
|
|
|
return shared.KeyHandled(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
return nil
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-09-30 14:21:51 -06:00
|
|
|
func (m *Model) scrollSelection(dir int) {
|
2024-10-19 20:38:42 -06:00
|
|
|
if m.selectedMessage+dir < 0 || m.selectedMessage+dir >= len(m.App.Messages) {
|
2024-09-30 14:21:51 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newIdx := m.selectedMessage
|
2024-10-19 20:38:42 -06:00
|
|
|
for i := newIdx + dir; i >= 0 && i < len(m.App.Messages); i += dir {
|
2024-09-30 14:21:51 -06:00
|
|
|
if !m.showDetails && m.App.Messages[i].Role.IsSystem() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newIdx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if newIdx != m.selectedMessage {
|
|
|
|
m.selectedMessage = newIdx
|
|
|
|
m.updateContent()
|
|
|
|
}
|
2024-09-30 21:38:15 -06:00
|
|
|
yOffset := m.messageOffsets[m.selectedMessage]
|
|
|
|
tuiutil.ScrollIntoView(&m.content, yOffset, m.content.Height/2)
|
2024-09-30 14:21:51 -06:00
|
|
|
}
|
|
|
|
|
2024-06-02 16:40:46 -06:00
|
|
|
// handleMessagesKey handles input when the messages pane is focused
|
2024-09-16 09:40:04 -06:00
|
|
|
func (m *Model) handleMessagesKey(msg tea.KeyMsg) tea.Cmd {
|
2024-06-02 16:40:46 -06:00
|
|
|
switch msg.String() {
|
|
|
|
case "tab", "enter":
|
|
|
|
m.focus = focusInput
|
|
|
|
m.updateContent()
|
|
|
|
m.input.Focus()
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
case "e":
|
2024-09-15 18:48:45 -06:00
|
|
|
if m.selectedMessage < len(m.App.Messages) {
|
2024-06-08 15:28:29 -06:00
|
|
|
m.editorTarget = selectedMessage
|
2024-09-16 09:40:04 -06:00
|
|
|
return tuiutil.OpenTempfileEditor(
|
2024-06-08 15:28:29 -06:00
|
|
|
"message.*.md",
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.Messages[m.selectedMessage].Content,
|
2024-06-08 15:28:29 -06:00
|
|
|
"# Edit the message below\n",
|
|
|
|
)
|
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
return nil
|
2024-09-30 14:21:51 -06:00
|
|
|
case "ctrl+k", "ctrl+up":
|
|
|
|
if m.selectedMessage > 0 {
|
|
|
|
m.scrollSelection(-1)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-09-30 14:21:51 -06:00
|
|
|
case "ctrl+j", "ctrl+down":
|
|
|
|
if m.selectedMessage < len(m.App.Messages)-1 {
|
|
|
|
m.scrollSelection(1)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-09-30 14:21:51 -06:00
|
|
|
case "ctrl+h", "ctrl+left", "ctrl+l", "ctrl+right":
|
2024-09-15 18:48:45 -06:00
|
|
|
dir := model.CyclePrev
|
2024-09-30 14:21:51 -06:00
|
|
|
if msg.String() == "ctrl+l" || msg.String() == "ctrl+right" {
|
2024-09-15 18:48:45 -06:00
|
|
|
dir = model.CycleNext
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-06-08 15:28:29 -06:00
|
|
|
var cmd tea.Cmd
|
2024-06-02 16:40:46 -06:00
|
|
|
if m.selectedMessage == 0 {
|
2024-09-15 18:48:45 -06:00
|
|
|
cmd = m.cycleSelectedRoot(m.App.Conversation, dir)
|
2024-06-02 16:40:46 -06:00
|
|
|
} else if m.selectedMessage > 0 {
|
2024-09-15 18:48:45 -06:00
|
|
|
cmd = m.cycleSelectedReply(&m.App.Messages[m.selectedMessage-1], dir)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
return cmd
|
2024-06-02 16:40:46 -06:00
|
|
|
case "ctrl+r":
|
2024-09-30 14:21:51 -06:00
|
|
|
// prompt the model with all messages up to and including the selected message
|
2024-09-15 18:48:45 -06:00
|
|
|
if m.state == idle && m.selectedMessage < len(m.App.Messages) {
|
|
|
|
m.App.Messages = m.App.Messages[:m.selectedMessage+1]
|
2024-06-08 15:58:39 -06:00
|
|
|
m.messageCache = m.messageCache[:m.selectedMessage+1]
|
|
|
|
cmd := m.promptLLM()
|
|
|
|
m.updateContent()
|
|
|
|
m.content.GotoBottom()
|
2024-09-16 09:40:04 -06:00
|
|
|
return cmd
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
2024-09-30 14:21:51 -06:00
|
|
|
return nil
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleInputKey handles input when the input textarea is focused
|
2024-09-16 09:40:04 -06:00
|
|
|
func (m *Model) handleInputKey(msg tea.KeyMsg) tea.Cmd {
|
2024-06-02 16:40:46 -06:00
|
|
|
switch msg.String() {
|
|
|
|
case "esc":
|
|
|
|
m.focus = focusMessages
|
2024-09-15 18:48:45 -06:00
|
|
|
if len(m.App.Messages) > 0 {
|
|
|
|
if m.selectedMessage < 0 || m.selectedMessage >= len(m.App.Messages) {
|
|
|
|
m.selectedMessage = len(m.App.Messages) - 1
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
offset := m.messageOffsets[m.selectedMessage]
|
|
|
|
tuiutil.ScrollIntoView(&m.content, offset, m.content.Height/2)
|
|
|
|
}
|
|
|
|
m.updateContent()
|
|
|
|
m.input.Blur()
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
case "ctrl+s":
|
2024-06-08 15:58:39 -06:00
|
|
|
// TODO: call a "handleSend" function which returns a tea.Cmd
|
|
|
|
if m.state != idle {
|
2024-09-16 09:40:04 -06:00
|
|
|
return nil
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
input := strings.TrimSpace(m.input.Value())
|
|
|
|
if input == "" {
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.KeyHandled(msg)
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
if len(m.App.Messages) > 0 && m.App.Messages[len(m.App.Messages)-1].Role == api.MessageRoleUser {
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.WrapError(fmt.Errorf("Can't reply to a user message"))
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2024-10-19 20:38:42 -06:00
|
|
|
m.addMessage(conversation.Message{
|
2024-06-12 02:35:07 -06:00
|
|
|
Role: api.MessageRoleUser,
|
2024-06-02 16:40:46 -06:00
|
|
|
Content: input,
|
|
|
|
})
|
|
|
|
|
|
|
|
m.input.SetValue("")
|
|
|
|
|
2024-06-08 15:28:29 -06:00
|
|
|
var cmds []tea.Cmd
|
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-06-08 15:28:29 -06:00
|
|
|
cmds = append(cmds, m.promptLLM())
|
|
|
|
|
2024-06-02 16:40:46 -06:00
|
|
|
m.updateContent()
|
|
|
|
m.content.GotoBottom()
|
2024-09-16 09:40:04 -06:00
|
|
|
return tea.Batch(cmds...)
|
2024-06-02 16:40:46 -06:00
|
|
|
case "ctrl+e":
|
|
|
|
cmd := tuiutil.OpenTempfileEditor("message.*.md", m.input.Value(), "# Edit your input below\n")
|
|
|
|
m.editorTarget = input
|
2024-09-16 09:40:04 -06:00
|
|
|
return cmd
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
return nil
|
2024-06-02 16:40:46 -06:00
|
|
|
}
|