2024-05-30 00:44:40 -06:00
|
|
|
package chat
|
2024-03-30 20:03:53 -06:00
|
|
|
|
|
|
|
import (
|
2024-03-31 17:51:45 -06:00
|
|
|
"time"
|
2024-03-30 20:03:53 -06:00
|
|
|
|
|
|
|
models "git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
|
2024-05-30 00:44:40 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/shared"
|
2024-05-22 10:25:16 -06:00
|
|
|
"github.com/charmbracelet/bubbles/cursor"
|
2024-03-31 17:51:45 -06:00
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
|
|
|
"github.com/charmbracelet/bubbles/textarea"
|
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
2024-03-30 20:03:53 -06:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
)
|
|
|
|
|
2024-03-30 20:29:35 -06:00
|
|
|
type focusState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
focusInput focusState = iota
|
|
|
|
focusMessages
|
|
|
|
)
|
|
|
|
|
|
|
|
type editorTarget int
|
|
|
|
|
|
|
|
const (
|
|
|
|
input editorTarget = iota
|
|
|
|
selectedMessage
|
|
|
|
)
|
|
|
|
|
2024-03-30 20:03:53 -06:00
|
|
|
// custom tea.Msg types
|
|
|
|
type (
|
|
|
|
// sent on each chunk received from LLM
|
|
|
|
msgResponseChunk string
|
|
|
|
// sent when response is finished being received
|
|
|
|
msgResponseEnd string
|
2024-05-30 00:44:40 -06:00
|
|
|
// a special case of common.MsgError that stops the response waiting animation
|
2024-03-30 20:03:53 -06:00
|
|
|
msgResponseError error
|
|
|
|
// sent on each completed reply
|
|
|
|
msgAssistantReply models.Message
|
|
|
|
// sent when a conversation is (re)loaded
|
|
|
|
msgConversationLoaded *models.Conversation
|
|
|
|
// sent when a new conversation title is set
|
|
|
|
msgConversationTitleChanged string
|
|
|
|
// sent when a conversation's messages are laoded
|
|
|
|
msgMessagesLoaded []models.Message
|
|
|
|
)
|
|
|
|
|
2024-05-30 00:44:40 -06:00
|
|
|
type Model struct {
|
|
|
|
shared.State
|
2024-05-30 01:04:55 -06:00
|
|
|
shared.Sections
|
2024-03-31 17:51:45 -06:00
|
|
|
|
|
|
|
// app state
|
|
|
|
conversation *models.Conversation
|
2024-05-28 00:34:11 -06:00
|
|
|
rootMessages []models.Message
|
2024-03-31 17:51:45 -06:00
|
|
|
messages []models.Message
|
|
|
|
selectedMessage int
|
|
|
|
waitingForReply bool
|
|
|
|
editorTarget editorTarget
|
|
|
|
stopSignal chan struct{}
|
|
|
|
replyChan chan models.Message
|
|
|
|
replyChunkChan chan string
|
|
|
|
persistence bool // whether we will save new messages in the conversation
|
|
|
|
|
|
|
|
// ui state
|
|
|
|
focus focusState
|
|
|
|
wrap bool // whether message content is wrapped to viewport width
|
|
|
|
status string // a general status message
|
|
|
|
showToolResults bool // whether tool calls and results are shown
|
|
|
|
messageCache []string // cache of syntax highlighted and wrapped message content
|
|
|
|
messageOffsets []int
|
|
|
|
|
|
|
|
// ui elements
|
2024-05-22 10:25:16 -06:00
|
|
|
content viewport.Model
|
|
|
|
input textarea.Model
|
|
|
|
spinner spinner.Model
|
|
|
|
replyCursor cursor.Model // cursor to indicate incoming response
|
2024-06-02 16:40:46 -06:00
|
|
|
|
|
|
|
// metrics
|
|
|
|
tokenCount uint
|
|
|
|
startTime time.Time
|
|
|
|
elapsed time.Duration
|
2024-03-31 17:51:45 -06:00
|
|
|
}
|
|
|
|
|
2024-05-30 01:18:31 -06:00
|
|
|
func Chat(state shared.State) Model {
|
2024-05-30 00:44:40 -06:00
|
|
|
m := Model{
|
2024-05-30 01:18:31 -06:00
|
|
|
State: state,
|
2024-03-31 17:51:45 -06:00
|
|
|
|
|
|
|
conversation: &models.Conversation{},
|
|
|
|
persistence: true,
|
|
|
|
|
|
|
|
stopSignal: make(chan struct{}),
|
|
|
|
replyChan: make(chan models.Message),
|
|
|
|
replyChunkChan: make(chan string),
|
|
|
|
|
|
|
|
wrap: true,
|
|
|
|
selectedMessage: -1,
|
|
|
|
|
|
|
|
content: viewport.New(0, 0),
|
|
|
|
input: textarea.New(),
|
|
|
|
spinner: spinner.New(spinner.WithSpinner(
|
|
|
|
spinner.Spinner{
|
|
|
|
Frames: []string{
|
|
|
|
". ",
|
|
|
|
".. ",
|
|
|
|
"...",
|
|
|
|
".. ",
|
|
|
|
". ",
|
|
|
|
" ",
|
|
|
|
},
|
|
|
|
FPS: time.Second / 3,
|
|
|
|
},
|
|
|
|
)),
|
2024-05-22 10:25:16 -06:00
|
|
|
replyCursor: cursor.New(),
|
2024-03-31 17:51:45 -06:00
|
|
|
}
|
|
|
|
|
2024-05-22 10:25:16 -06:00
|
|
|
m.replyCursor.SetChar(" ")
|
|
|
|
m.replyCursor.Focus()
|
|
|
|
|
2024-05-30 01:18:31 -06:00
|
|
|
system := state.Ctx.GetSystemPrompt()
|
2024-05-07 02:07:48 -06:00
|
|
|
if system != "" {
|
|
|
|
m.messages = []models.Message{{
|
|
|
|
Role: models.MessageRoleSystem,
|
|
|
|
Content: system,
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:51:45 -06:00
|
|
|
m.input.Focus()
|
|
|
|
m.input.MaxHeight = 0
|
|
|
|
m.input.CharLimit = 0
|
|
|
|
m.input.ShowLineNumbers = false
|
|
|
|
m.input.Placeholder = "Enter a message"
|
|
|
|
|
|
|
|
m.input.FocusedStyle.CursorLine = lipgloss.NewStyle()
|
|
|
|
m.input.FocusedStyle.Base = inputFocusedStyle
|
|
|
|
m.input.BlurredStyle.Base = inputBlurredStyle
|
|
|
|
|
|
|
|
m.waitingForReply = false
|
|
|
|
m.status = "Press ctrl+s to send"
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2024-05-30 00:44:40 -06:00
|
|
|
func (m Model) Init() tea.Cmd {
|
2024-03-31 17:51:45 -06:00
|
|
|
return tea.Batch(
|
|
|
|
m.waitForChunk(),
|
|
|
|
m.waitForReply(),
|
|
|
|
)
|
|
|
|
}
|