Cleaned up assistant cursor handling

This commit is contained in:
Matt Low 2024-06-21 05:52:59 +00:00
parent 3fde58b77d
commit c30e652103
2 changed files with 13 additions and 13 deletions

View File

@ -93,7 +93,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
// append chunk to existing message
m.setMessageContents(last, m.messages[last].Content+msg.Content)
} else {
// use chunk in new message
// use chunk in a new message
m.addMessage(api.Message{
Role: api.MessageRoleAssistant,
Content: msg.Content,
@ -177,6 +177,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case cursor.BlinkMsg:
if m.state == pendingResponse {
// ensure we show the updated "wait for response" cursor blink state
last := len(m.messages)-1
m.messageCache[last] = m.renderMessage(last)
m.updateContent()
}
case msgConversationPersisted:

View File

@ -124,6 +124,9 @@ func (m *Model) renderMessageHeading(i int, message *api.Message) string {
return messageHeadingStyle.Render(prefix + user + suffix)
}
// renderMessages renders the message at the given index as it should be shown
// *at this moment* - we render differently depending on the current application
// state (window size, etc, etc).
func (m *Model) renderMessage(i int) string {
msg := &m.messages[i]
@ -138,8 +141,11 @@ func (m *Model) renderMessage(i int) string {
}
}
// Show the assistant's cursor
if m.state == pendingResponse && i == len(m.messages)-1 && msg.Role == api.MessageRoleAssistant {
isLast := i == len(m.messages)-1
isAssistant := msg.Role == api.MessageRoleAssistant
if m.state == pendingResponse && isLast && isAssistant {
// Show the assistant's cursor
sb.WriteString(m.replyCursor.View())
}
@ -236,22 +242,14 @@ func (m *Model) conversationMessagesView() string {
sb.WriteString("\n")
lineCnt += lipgloss.Height(heading)
var rendered string
if m.state == pendingResponse && 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]
}
rendered := m.messageCache[i]
sb.WriteString(rendered)
sb.WriteString("\n")
lineCnt += lipgloss.Height(rendered)
}
// Render a placeholder for the incoming assistant reply
if m.state == pendingResponse && (len(m.messages) == 0 || m.messages[len(m.messages)-1].Role != api.MessageRoleAssistant) {
if m.state == pendingResponse && m.messages[len(m.messages)-1].Role != api.MessageRoleAssistant {
heading := m.renderMessageHeading(-1, &api.Message{
Role: api.MessageRoleAssistant,
})