fix(memory): dedupe episode write on duplicate streaming Done
AgentFinalAnswer may carry Done:true more than once (e.g. finish chunk plus EOF). Guard AddEpisode with sync.Once so memory persists once per turn, consistent with qa.go completion handling. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package chatpipeline
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/event"
|
"github.com/Tencent/WeKnora/internal/event"
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
@@ -118,6 +119,7 @@ func (p *MemoryPlugin) handleStorage(
|
|||||||
// If streaming, subscribe to events
|
// If streaming, subscribe to events
|
||||||
if chatManage.EventBus != nil {
|
if chatManage.EventBus != nil {
|
||||||
var fullResponse string
|
var fullResponse string
|
||||||
|
var storeOnce sync.Once
|
||||||
userID := chatManage.UserID
|
userID := chatManage.UserID
|
||||||
sessionID := chatManage.SessionID
|
sessionID := chatManage.SessionID
|
||||||
bgCtx := context.WithoutCancel(ctx)
|
bgCtx := context.WithoutCancel(ctx)
|
||||||
@@ -129,15 +131,19 @@ func (p *MemoryPlugin) handleStorage(
|
|||||||
}
|
}
|
||||||
fullResponse += data.Content
|
fullResponse += data.Content
|
||||||
if data.Done {
|
if data.Done {
|
||||||
messages := []types.Message{
|
// Stream layer may emit Done:true twice (e.g. finish_reason chunk + EOF sentinel).
|
||||||
{Role: "user", Content: chatManage.Query},
|
// qa.go dedupes with completionHandled; keep memory writes consistent with one episode only.
|
||||||
{Role: "assistant", Content: fullResponse},
|
storeOnce.Do(func() {
|
||||||
}
|
messages := []types.Message{
|
||||||
go func() {
|
{Role: "user", Content: chatManage.Query},
|
||||||
if err := p.memoryService.AddEpisode(bgCtx, userID, sessionID, messages); err != nil {
|
{Role: "assistant", Content: fullResponse},
|
||||||
logger.Errorf(bgCtx, "failed to add episode: %v", err)
|
|
||||||
}
|
}
|
||||||
}()
|
go func() {
|
||||||
|
if err := p.memoryService.AddEpisode(bgCtx, userID, sessionID, messages); err != nil {
|
||||||
|
logger.Errorf(bgCtx, "failed to add episode: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user