Cleaned up assistant cursor handling
This commit is contained in:
parent
3fde58b77d
commit
c30e652103
@ -93,7 +93,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
// append chunk to existing message
|
// append chunk to existing message
|
||||||
m.setMessageContents(last, m.messages[last].Content+msg.Content)
|
m.setMessageContents(last, m.messages[last].Content+msg.Content)
|
||||||
} else {
|
} else {
|
||||||
// use chunk in new message
|
// use chunk in a new message
|
||||||
m.addMessage(api.Message{
|
m.addMessage(api.Message{
|
||||||
Role: api.MessageRoleAssistant,
|
Role: api.MessageRoleAssistant,
|
||||||
Content: msg.Content,
|
Content: msg.Content,
|
||||||
@ -177,6 +177,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
case cursor.BlinkMsg:
|
case cursor.BlinkMsg:
|
||||||
if m.state == pendingResponse {
|
if m.state == pendingResponse {
|
||||||
// ensure we show the updated "wait for response" cursor blink state
|
// ensure we show the updated "wait for response" cursor blink state
|
||||||
|
last := len(m.messages)-1
|
||||||
|
m.messageCache[last] = m.renderMessage(last)
|
||||||
m.updateContent()
|
m.updateContent()
|
||||||
}
|
}
|
||||||
case msgConversationPersisted:
|
case msgConversationPersisted:
|
||||||
|
@ -124,6 +124,9 @@ func (m *Model) renderMessageHeading(i int, message *api.Message) string {
|
|||||||
return messageHeadingStyle.Render(prefix + user + suffix)
|
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 {
|
func (m *Model) renderMessage(i int) string {
|
||||||
msg := &m.messages[i]
|
msg := &m.messages[i]
|
||||||
|
|
||||||
@ -138,8 +141,11 @@ func (m *Model) renderMessage(i int) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show the assistant's cursor
|
isLast := i == len(m.messages)-1
|
||||||
if m.state == pendingResponse && i == len(m.messages)-1 && msg.Role == api.MessageRoleAssistant {
|
isAssistant := msg.Role == api.MessageRoleAssistant
|
||||||
|
|
||||||
|
if m.state == pendingResponse && isLast && isAssistant {
|
||||||
|
// Show the assistant's cursor
|
||||||
sb.WriteString(m.replyCursor.View())
|
sb.WriteString(m.replyCursor.View())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,22 +242,14 @@ func (m *Model) conversationMessagesView() string {
|
|||||||
sb.WriteString("\n")
|
sb.WriteString("\n")
|
||||||
lineCnt += lipgloss.Height(heading)
|
lineCnt += lipgloss.Height(heading)
|
||||||
|
|
||||||
var rendered string
|
rendered := m.messageCache[i]
|
||||||
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]
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.WriteString(rendered)
|
sb.WriteString(rendered)
|
||||||
sb.WriteString("\n")
|
sb.WriteString("\n")
|
||||||
lineCnt += lipgloss.Height(rendered)
|
lineCnt += lipgloss.Height(rendered)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render a placeholder for the incoming assistant reply
|
// 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{
|
heading := m.renderMessageHeading(-1, &api.Message{
|
||||||
Role: api.MessageRoleAssistant,
|
Role: api.MessageRoleAssistant,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user