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 := ""
|
errorRaw := ""
|
||||||
isFirst := true
|
isFirst := true
|
||||||
|
currentMessageID := ""
|
||||||
err := ast.Chat(c.Request.Context(), messages, options, func(data []byte) int {
|
err := ast.Chat(c.Request.Context(), messages, options, func(data []byte) int {
|
||||||
select {
|
select {
|
||||||
case <-clientBreak:
|
case <-clientBreak:
|
||||||
|
|
@ -321,7 +322,8 @@ func (ast *Assistant) streamChat(
|
||||||
msg.AppendTo(contents) // Append content and send message
|
msg.AppendTo(contents) // Append content and send message
|
||||||
|
|
||||||
// Scan the tokens
|
// 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.ID = id
|
||||||
msg.Type = token
|
msg.Type = token
|
||||||
msg.Text = "" // clear the text
|
msg.Text = "" // clear the text
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ func NewContents() *Contents {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScanTokens scan the tokens
|
// 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())
|
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 {
|
for name, token := range tokens {
|
||||||
if index := strings.Index(text, token[0]); index >= 0 {
|
if index := strings.Index(text, token[0]); index >= 0 {
|
||||||
c.token = name
|
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
|
cb(name, c.id, true, text, "") // call the callback
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue