Private
Public Access
1
0

Wrap chunk content in a Chunk type

Preparing to include additional information with each chunk (e.g. token
count)
This commit is contained in:
2024-06-08 23:37:58 +00:00
parent c963747066
commit d2d946b776
8 changed files with 35 additions and 20 deletions

View File

@@ -4,6 +4,7 @@ import (
"time"
models "git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
"git.mlow.ca/mlow/lmcli/pkg/lmcli/provider"
"git.mlow.ca/mlow/lmcli/pkg/tui/shared"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/spinner"
@@ -16,7 +17,7 @@ import (
// custom tea.Msg types
type (
// sent on each chunk received from LLM
msgResponseChunk string
msgResponseChunk provider.Chunk
// sent when response is finished being received
msgResponseEnd string
// a special case of common.MsgError that stops the response waiting animation
@@ -82,7 +83,7 @@ type Model struct {
editorTarget editorTarget
stopSignal chan struct{}
replyChan chan models.Message
replyChunkChan chan string
replyChunkChan chan provider.Chunk
persistence bool // whether we will save new messages in the conversation
// ui state
@@ -114,7 +115,7 @@ func Chat(shared shared.Shared) Model {
stopSignal: make(chan struct{}),
replyChan: make(chan models.Message),
replyChunkChan: make(chan string),
replyChunkChan: make(chan provider.Chunk),
wrap: true,
selectedMessage: -1,

View File

@@ -90,20 +90,19 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case msgResponseChunk:
cmds = append(cmds, m.waitForResponseChunk()) // wait for the next chunk
chunk := string(msg)
if chunk == "" {
if msg.Content == "" {
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)
m.setMessageContents(last, m.messages[last].Content+msg.Content)
} else {
// use chunk in new message
m.addMessage(models.Message{
Role: models.MessageRoleAssistant,
Content: chunk,
Content: msg.Content,
})
}
m.updateContent()