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 eca120cde6
commit 97f81a0cbb
1 changed files with 14 additions and 12 deletions

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 m.messages = append(m.messages, models.Message{
default: Role: models.MessageRoleAssistant,
m.messages = append(m.messages, models.Message{ Content: chunk,
Role: models.MessageRoleAssistant, })
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 {