From 5a38cf77c144c09bceab62dcd847372bb1bf9855 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 4 Feb 2025 19:22:45 +0800 Subject: [PATCH] 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 --- neo/assistant/api.go | 4 +++- neo/message/contents.go | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/neo/assistant/api.go b/neo/assistant/api.go index d6285d3d..6c4188f6 100644 --- a/neo/assistant/api.go +++ b/neo/assistant/api.go @@ -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 diff --git a/neo/message/contents.go b/neo/message/contents.go index 10c30785..6aaa00fa 100644 --- a/neo/message/contents.go +++ b/neo/message/contents.go @@ -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 } }