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:
Max 2025-02-04 19:22:45 +08:00
parent 75d55ae13b
commit 5a38cf77c1
2 changed files with 8 additions and 3 deletions

View file

@ -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

View file

@ -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
}
}