tui: scroll content view with output

clean up msgResponseChunk handling
This commit is contained in:
Matt Low 2024-03-12 23:31:48 +00:00
parent 51de2b7079
commit 8613719b58

View File

@ -125,19 +125,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.updateContent() m.updateContent()
case msgResponseChunk: case msgResponseChunk:
chunk := string(msg) chunk := string(msg)
if len(m.messages) > 0 { last := len(m.messages) - 1
i := len(m.messages) - 1 if last >= 0 && m.messages[last].Role == models.MessageRoleAssistant {
switch m.messages[i].Role { m.messages[last].Content += chunk
case models.MessageRoleAssistant: } else {
m.messages[i].Content += chunk
default:
m.messages = append(m.messages, models.Message{ m.messages = append(m.messages, models.Message{
Role: models.MessageRoleAssistant, Role: models.MessageRoleAssistant,
Content: chunk, Content: chunk,
}) })
} }
m.updateContent() m.updateContent()
}
cmd = waitForChunk(m.replyChan) // wait for the next chunk cmd = waitForChunk(m.replyChan) // wait for the next chunk
case msgResponseEnd: case msgResponseEnd:
m.replyCancelFunc = nil m.replyCancelFunc = nil
@ -315,7 +312,12 @@ func (m *model) updateContent() {
sb.WriteString("\n\n") sb.WriteString("\n\n")
} }
} }
atBottom := m.content.AtBottom()
m.content.SetContent(sb.String()) m.content.SetContent(sb.String())
if atBottom {
// if we were at bottom before the update, scroll with the output
m.content.GotoBottom()
}
} }
func (m model) inputView() string { func (m model) inputView() string {