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
|
|
|
|
2024-06-09 10:42:53 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// custom tea.Msg types
|
|
|
|
type (
|
|
|
|
// sent on each chunk received from LLM
|
2024-06-09 10:42:53 -06:00
|
|
|
msgResponseChunk api.Chunk
|
2024-03-30 20:03:53 -06:00
|
|
|
// 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
|
2024-06-08 15:28:29 -06:00
|
|
|
msgResponse models.Message
|
2024-03-30 20:03:53 -06:00
|
|
|
// sent when a conversation is (re)loaded
|
2024-06-08 15:28:29 -06:00
|
|
|
msgConversationLoaded struct {
|
|
|
|
conversation *models.Conversation
|
|
|
|
rootMessages []models.Message
|
|
|
|
}
|
|
|
|
// sent when a new conversation title generated
|
|
|
|
msgConversationTitleGenerated string
|
2024-03-30 20:03:53 -06:00
|
|
|
// sent when a conversation's messages are laoded
|
|
|
|
msgMessagesLoaded []models.Message
|
2024-06-08 15:28:29 -06:00
|
|
|
// sent when the conversation has been persisted, triggers a reload of contents
|
|
|
|
msgConversationPersisted struct {
|
2024-06-09 12:42:17 -06:00
|
|
|
isNew bool
|
2024-06-08 15:28:29 -06:00
|
|
|
conversation *models.Conversation
|
|
|
|
messages []models.Message
|
|
|
|
}
|
|
|
|
// sent when the given message is made the new selected reply of its parent
|
|
|
|
msgSelectedReplyCycled *models.Message
|
|
|
|
// sent when the given message is made the new selected root of the current conversation
|
|
|
|
msgSelectedRootCycled *models.Message
|
|
|
|
// sent when a message's contents are updated and saved
|
|
|
|
msgMessageUpdated *models.Message
|
|
|
|
// sent when a message is cloned, with the cloned message
|
|
|
|
msgMessageCloned *models.Message
|
2024-03-30 20:03:53 -06:00
|
|
|
)
|
|
|
|
|
2024-06-08 15:58:39 -06:00
|
|
|
type focusState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
focusInput focusState = iota
|
|
|
|
focusMessages
|
|
|
|
)
|
|
|
|
|
|
|
|
type editorTarget int
|
|
|
|
|
|
|
|
const (
|
|
|
|
input editorTarget = iota
|
|
|
|
selectedMessage
|
|
|
|
)
|
|
|
|
|
|
|
|
type state int
|
|
|
|
|
|
|
|
const (
|
|
|
|
idle state = iota
|
|
|
|
loading
|
|
|
|
pendingResponse
|
|
|
|
)
|
|
|
|
|
2024-05-30 00:44:40 -06:00
|
|
|
type Model struct {
|
2024-06-08 16:01:16 -06:00
|
|
|
shared.Shared
|
2024-05-30 01:04:55 -06:00
|
|
|
shared.Sections
|
2024-03-31 17:51:45 -06:00
|
|
|
|
|
|
|
// app state
|
2024-06-08 15:58:39 -06:00
|
|
|
state state // current overall status of the view
|
2024-03-31 17:51:45 -06:00
|
|
|
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
|
|
|
|
editorTarget editorTarget
|
|
|
|
stopSignal chan struct{}
|
|
|
|
replyChan chan models.Message
|
2024-06-09 10:42:53 -06:00
|
|
|
replyChunkChan chan api.Chunk
|
2024-03-31 17:51:45 -06:00
|
|
|
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
|
|
|
|
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-06-08 16:01:16 -06:00
|
|
|
func Chat(shared shared.Shared) Model {
|
2024-05-30 00:44:40 -06:00
|
|
|
m := Model{
|
2024-06-08 16:01:16 -06:00
|
|
|
Shared: shared,
|
2024-03-31 17:51:45 -06:00
|
|
|
|
2024-06-08 15:58:39 -06:00
|
|
|
state: idle,
|
2024-03-31 17:51:45 -06:00
|
|
|
conversation: &models.Conversation{},
|
|
|
|
persistence: true,
|
|
|
|
|
|
|
|
stopSignal: make(chan struct{}),
|
|
|
|
replyChan: make(chan models.Message),
|
2024-06-09 10:42:53 -06:00
|
|
|
replyChunkChan: make(chan api.Chunk),
|
2024-03-31 17:51:45 -06:00
|
|
|
|
|
|
|
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-06-08 16:01:16 -06:00
|
|
|
system := shared.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
|
|
|
|
|
|
|
|
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(
|
2024-06-08 15:28:29 -06:00
|
|
|
m.waitForResponseChunk(),
|
|
|
|
m.waitForResponse(),
|
2024-03-31 17:51:45 -06:00
|
|
|
)
|
|
|
|
}
|