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-09-15 18:48:45 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/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 when a conversation is (re)loaded
|
2024-06-08 15:28:29 -06:00
|
|
|
msgConversationLoaded struct {
|
2024-06-12 02:35:07 -06:00
|
|
|
conversation *api.Conversation
|
|
|
|
rootMessages []api.Message
|
2024-06-08 15:28:29 -06:00
|
|
|
}
|
|
|
|
// sent when a new conversation title generated
|
|
|
|
msgConversationTitleGenerated string
|
|
|
|
// 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-12 02:35:07 -06:00
|
|
|
conversation *api.Conversation
|
|
|
|
messages []api.Message
|
2024-06-08 15:28:29 -06:00
|
|
|
}
|
2024-06-12 02:35:07 -06:00
|
|
|
// sent when a conversation's messages are laoded
|
|
|
|
msgMessagesLoaded []api.Message
|
|
|
|
// a special case of common.MsgError that stops the response waiting animation
|
|
|
|
msgChatResponseError error
|
|
|
|
// sent on each chunk received from LLM
|
|
|
|
msgChatResponseChunk api.Chunk
|
|
|
|
// sent on each completed reply
|
|
|
|
msgChatResponse *api.Message
|
|
|
|
// sent when the response is canceled
|
|
|
|
msgChatResponseCanceled struct{}
|
|
|
|
// sent when results from a tool call are returned
|
|
|
|
msgToolResults []api.ToolResult
|
2024-06-08 15:28:29 -06:00
|
|
|
// sent when the given message is made the new selected reply of its parent
|
2024-06-12 02:35:07 -06:00
|
|
|
msgSelectedReplyCycled *api.Message
|
2024-06-08 15:28:29 -06:00
|
|
|
// sent when the given message is made the new selected root of the current conversation
|
2024-06-12 02:35:07 -06:00
|
|
|
msgSelectedRootCycled *api.Message
|
2024-06-08 15:28:29 -06:00
|
|
|
// sent when a message's contents are updated and saved
|
2024-06-12 02:35:07 -06:00
|
|
|
msgMessageUpdated *api.Message
|
2024-06-08 15:28:29 -06:00
|
|
|
// sent when a message is cloned, with the cloned message
|
2024-06-12 02:35:07 -06:00
|
|
|
msgMessageCloned *api.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-09-16 09:40:04 -06:00
|
|
|
*shared.ViewState
|
2024-05-30 01:04:55 -06:00
|
|
|
shared.Sections
|
2024-03-31 17:51:45 -06:00
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
// App state
|
|
|
|
App *model.AppModel
|
|
|
|
|
|
|
|
// Chat view 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
|
|
|
selectedMessage int
|
|
|
|
editorTarget editorTarget
|
|
|
|
stopSignal chan struct{}
|
2024-06-12 02:35:07 -06:00
|
|
|
replyChan chan api.Message
|
|
|
|
chatReplyChunks chan api.Chunk
|
2024-03-31 17:51:45 -06:00
|
|
|
persistence bool // whether we will save new messages in the conversation
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
// UI state
|
2024-03-31 17:51:45 -06:00
|
|
|
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-09-16 09:40:04 -06:00
|
|
|
func (m Model) Initialized() bool {
|
|
|
|
return m.ViewState.Initialized
|
|
|
|
}
|
|
|
|
|
|
|
|
func Chat(app *model.AppModel, shared shared.ViewState) shared.ViewModel {
|
2024-05-30 00:44:40 -06:00
|
|
|
m := Model{
|
2024-09-15 18:48:45 -06:00
|
|
|
App: app,
|
2024-09-16 09:40:04 -06:00
|
|
|
ViewState: &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
|
|
|
persistence: true,
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
stopSignal: make(chan struct{}),
|
|
|
|
replyChan: make(chan api.Message),
|
|
|
|
chatReplyChunks: 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-09-15 18:48:45 -06:00
|
|
|
system := app.Ctx.DefaultSystemPrompt()
|
2024-06-23 12:57:08 -06:00
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
agent := app.Ctx.GetAgent(app.Ctx.Config.Defaults.Agent)
|
2024-06-23 12:57:08 -06:00
|
|
|
if agent != nil && agent.SystemPrompt != "" {
|
|
|
|
system = agent.SystemPrompt
|
|
|
|
}
|
|
|
|
|
2024-05-07 02:07:48 -06:00
|
|
|
if system != "" {
|
2024-09-15 18:48:45 -06:00
|
|
|
m.App.Messages = api.ApplySystemPrompt(m.App.Messages, system, false)
|
2024-05-07 02:07:48 -06:00
|
|
|
}
|
|
|
|
|
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-09-16 09:40:04 -06:00
|
|
|
m.ViewState.Initialized = true
|
2024-03-31 17:51:45 -06:00
|
|
|
return tea.Batch(
|
2024-06-08 15:28:29 -06:00
|
|
|
m.waitForResponseChunk(),
|
2024-03-31 17:51:45 -06:00
|
|
|
)
|
|
|
|
}
|