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