Improve message ID tracking in streaming chat processing
- Updated ScanTokens method to preserve and reuse existing message IDs - Modified streaming chat to track and pass current message ID during token scanning - Ensured consistent message ID generation when not explicitly provided
This commit is contained in:
parent
75d55ae13b
commit
5a38cf77c1
2 changed files with 8 additions and 3 deletions
|
|
@ -283,6 +283,7 @@ func (ast *Assistant) streamChat(
|
|||
|
||||
errorRaw := ""
|
||||
isFirst := true
|
||||
currentMessageID := ""
|
||||
err := ast.Chat(c.Request.Context(), messages, options, func(data []byte) int {
|
||||
select {
|
||||
case <-clientBreak:
|
||||
|
|
@ -321,7 +322,8 @@ func (ast *Assistant) streamChat(
|
|||
msg.AppendTo(contents) // Append content and send message
|
||||
|
||||
// Scan the tokens
|
||||
contents.ScanTokens(func(token string, id string, begin bool, text string, tails string) {
|
||||
contents.ScanTokens(currentMessageID, func(token string, id string, begin bool, text string, tails string) {
|
||||
currentMessageID = id
|
||||
msg.ID = id
|
||||
msg.Type = token
|
||||
msg.Text = "" // clear the text
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ func NewContents() *Contents {
|
|||
}
|
||||
|
||||
// ScanTokens scan the tokens
|
||||
func (c *Contents) ScanTokens(cb func(token string, id string, begin bool, text string, tails string)) {
|
||||
func (c *Contents) ScanTokens(currentID string, cb func(token string, id string, begin bool, text string, tails string)) {
|
||||
|
||||
text := strings.TrimSpace(c.Text())
|
||||
|
||||
|
|
@ -79,7 +79,10 @@ func (c *Contents) ScanTokens(cb func(token string, id string, begin bool, text
|
|||
for name, token := range tokens {
|
||||
if index := strings.Index(text, token[0]); index >= 0 {
|
||||
c.token = name
|
||||
c.id = uuid.New().String()
|
||||
c.id = currentID
|
||||
if c.id == "" {
|
||||
c.id = uuid.New().String()
|
||||
}
|
||||
cb(name, c.id, true, text, "") // call the callback
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue