Compare commits

..

No commits in common. "437997872a8d831cbdcf0eb4d8b4f483cfeb05e2" and "5c1248184b96357e03462c7d0db1a0fc7435bcd4" have entirely different histories.

2 changed files with 21 additions and 87 deletions

View File

@ -66,7 +66,7 @@ func RetryCmd(ctx *lmcli.Context) *cobra.Command {
},
}
cmd.Flags().Int("offset", 0, "Offset from the last message to retry from.")
cmd.Flags().Int("offset", 1, "Offset from the last message retry from.")
applyPromptFlags(ctx, cmd)
return cmd

View File

@ -9,14 +9,12 @@ import (
cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util"
models "git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wordwrap"
"github.com/muesli/reflow/wrap"
"gopkg.in/yaml.v2"
)
@ -81,10 +79,9 @@ type chatModel struct {
messageOffsets []int
// ui elements
content viewport.Model
input textarea.Model
spinner spinner.Model
replyCursor cursor.Model // cursor to indicate incoming response
content viewport.Model
input textarea.Model
spinner spinner.Model
}
func newChatModel(tui *model) chatModel {
@ -120,12 +117,8 @@ func newChatModel(tui *model) chatModel {
FPS: time.Second / 3,
},
)),
replyCursor: cursor.New(),
}
m.replyCursor.SetChar(" ")
m.replyCursor.Focus()
system := tui.ctx.GetSystemPrompt()
if system != "" {
m.messages = []models.Message{{
@ -227,6 +220,8 @@ func (m *chatModel) handleInput(msg tea.KeyMsg) (bool, tea.Cmd) {
func (m chatModel) Init() tea.Cmd {
return tea.Batch(
textarea.Blink,
m.spinner.Tick,
m.waitForChunk(),
m.waitForReply(),
)
@ -246,13 +241,9 @@ func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case msgStateEnter:
// wake up spinners and cursors
cmds = append(cmds, cursor.Blink, m.spinner.Tick)
if m.opts.convShortname != "" && m.conversation.ShortName.String != m.opts.convShortname {
cmds = append(cmds, m.loadConversation(m.opts.convShortname))
}
m.rebuildMessageCache()
m.updateContent()
case tea.WindowSizeMsg:
@ -284,16 +275,10 @@ func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) {
m.content.GotoBottom()
case msgResponseChunk:
chunk := string(msg)
if chunk == "" {
break
}
last := len(m.messages) - 1
if last >= 0 && m.messages[last].Role.IsAssistant() {
// append chunk to existing message
m.setMessageContents(last, m.messages[last].Content+chunk)
} else {
// use chunk in new message
m.addMessage(models.Message{
Role: models.MessageRoleAssistant,
Content: chunk,
@ -302,10 +287,6 @@ func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) {
m.updateContent()
cmds = append(cmds, m.waitForChunk()) // wait for the next chunk
// show cursor and reset blink interval (simulate typing)
m.replyCursor.Blink = false
cmds = append(cmds, m.replyCursor.BlinkCmd())
m.tokenCount++
m.elapsed = time.Now().Sub(m.startTime)
case msgAssistantReply:
@ -351,7 +332,6 @@ func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) {
m.waitingForReply = false
m.status = "Press ctrl+s to send"
m.err = error(msg)
m.updateContent()
case msgConversationTitleChanged:
title := string(msg)
m.conversation.Title = title
@ -361,11 +341,6 @@ func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) {
cmds = append(cmds, wrapError(err))
}
}
case cursor.BlinkMsg:
if m.waitingForReply {
// ensure we show updated "wait for response" cursor blink state
m.updateContent()
}
}
var cmd tea.Cmd
@ -373,10 +348,6 @@ func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) {
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()
inputCaptured := false
@ -471,10 +442,9 @@ func (m *chatModel) handleMessagesKey(msg tea.KeyMsg) (bool, tea.Cmd) {
}
m.messages = m.messages[:m.selectedMessage+1]
m.messageCache = m.messageCache[:m.selectedMessage+1]
cmd := m.promptLLM()
m.updateContent()
m.content.GotoBottom()
return true, cmd
return true, m.promptLLM()
}
return false, nil
}
@ -494,10 +464,6 @@ func (m *chatModel) handleInputKey(msg tea.KeyMsg) (bool, tea.Cmd) {
m.input.Blur()
return true, nil
case "ctrl+s":
if m.waitingForReply {
return false, nil
}
input := strings.TrimSpace(m.input.Value())
if input == "" {
return true, nil
@ -521,10 +487,9 @@ func (m *chatModel) handleInputKey(msg tea.KeyMsg) (bool, tea.Cmd) {
}
}
cmd := m.promptLLM()
m.updateContent()
m.content.GotoBottom()
return true, cmd
return true, m.promptLLM()
case "ctrl+e":
cmd := openTempfileEditor("message.*.md", m.input.Value(), "# Edit your input below\n")
m.editorTarget = input
@ -571,10 +536,7 @@ func (m *chatModel) renderMessageHeading(i int, message *models.Message) string
return messageHeadingStyle.Render(prefix + user + suffix)
}
func (m *chatModel) renderMessage(i int) string {
msg := &m.messages[i]
// Write message contents
func (m *chatModel) renderMessage(msg *models.Message) string {
sb := &strings.Builder{}
sb.Grow(len(msg.Content) * 2)
if msg.Content != "" {
@ -585,12 +547,6 @@ func (m *chatModel) renderMessage(i int) string {
}
}
// Show the assistant's cursor
if m.waitingForReply && i == len(m.messages)-1 {
sb.WriteString(m.replyCursor.View())
}
// Write tool call info
var toolString string
switch msg.Role {
case models.MessageRoleToolCall:
@ -648,11 +604,8 @@ func (m *chatModel) renderMessage(i int) string {
content := strings.TrimRight(sb.String(), "\n")
if m.wrap {
wrapWidth := m.content.Width - messageStyle.GetHorizontalPadding()
// first we word-wrap text to slightly less than desired width (since
// wordwrap seems to have an off-by-1 issue), then hard wrap at
// desired with
content = wrap.String(wordwrap.String(content, wrapWidth-2), wrapWidth)
wrapWidth := m.content.Width - messageStyle.GetHorizontalPadding() - 2
content = wordwrap.String(content, wrapWidth)
}
return messageStyle.Width(0).Render(content)
@ -683,18 +636,10 @@ func (m *chatModel) conversationMessagesView() string {
sb.WriteString("\n")
lineCnt += lipgloss.Height(heading)
var rendered string
if m.waitingForReply && i == len(m.messages)-1 {
// do a direct render of final (assistant) message to handle the
// assistant cursor blink
rendered = m.renderMessage(i)
} else {
rendered = m.messageCache[i]
}
sb.WriteString(rendered)
cached := m.messageCache[i]
sb.WriteString(cached)
sb.WriteString("\n")
lineCnt += lipgloss.Height(rendered)
lineCnt += lipgloss.Height(cached)
}
return sb.String()
@ -767,12 +712,12 @@ func (m *chatModel) setMessage(i int, msg models.Message) {
panic("i out of range")
}
m.messages[i] = msg
m.messageCache[i] = m.renderMessage(i)
m.messageCache[i] = m.renderMessage(&msg)
}
func (m *chatModel) addMessage(msg models.Message) {
m.messages = append(m.messages, msg)
m.messageCache = append(m.messageCache, m.renderMessage(len(m.messages)-1))
m.messageCache = append(m.messageCache, m.renderMessage(&msg))
}
func (m *chatModel) setMessageContents(i int, content string) {
@ -780,13 +725,13 @@ func (m *chatModel) setMessageContents(i int, content string) {
panic("i out of range")
}
m.messages[i].Content = content
m.messageCache[i] = m.renderMessage(i)
m.messageCache[i] = m.renderMessage(&m.messages[i])
}
func (m *chatModel) rebuildMessageCache() {
m.messageCache = make([]string, len(m.messages))
for i := range m.messages {
m.messageCache[i] = m.renderMessage(i)
for i, msg := range m.messages {
m.messageCache[i] = m.renderMessage(&msg)
}
}
@ -847,7 +792,7 @@ func (m *chatModel) persistConversation() error {
if err != nil {
return err
}
} else if i > 0 {
} else if i > 0 {
// messages is new, so add it as a reply to previous message
saved, err := m.ctx.Store.Reply(&m.messages[i-1], m.messages[i])
if err != nil {
@ -888,19 +833,8 @@ func (m *chatModel) waitForChunk() tea.Cmd {
func (m *chatModel) promptLLM() tea.Cmd {
m.waitingForReply = true
m.replyCursor.Blink = false
m.status = "Press ctrl+c to cancel"
toPrompt := m.messages
// Add response placeholder message
if m.messages[len(m.messages)-1].Role != models.MessageRoleAssistant {
m.addMessage(models.Message{
Role: models.MessageRoleAssistant,
Content: "",
})
}
m.tokenCount = 0
m.startTime = time.Now()
m.elapsed = 0
@ -934,7 +868,7 @@ func (m *chatModel) promptLLM() tea.Cmd {
}()
resp, err := completionProvider.CreateChatCompletionStream(
ctx, requestParams, toPrompt, replyHandler, m.replyChunkChan,
ctx, requestParams, m.messages, replyHandler, m.replyChunkChan,
)
if err != nil && !canceled {