fix: 修复历史消息重复和[review]标记残留

- 移除 fullText 中的 "[review]" 调试标记
- 有审查消息时不再重复保存完整文本到数据库
- 清理已有脏数据

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-23 10:59:09 +08:00
parent 31be1b71eb
commit 0c1bbff7b4
@@ -231,6 +231,7 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
var fullText string var fullText string
var msgID string var msgID string
var hasReview bool // 是否有审查消息(避免重复持久化)
var segments []ws.VoiceSegment // 收集断句信息 var segments []ws.VoiceSegment // 收集断句信息
for scanner.Scan() { for scanner.Scan() {
@@ -327,7 +328,7 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
} }
} }
fullText += "[review]" hasReview = true
continue continue
} }
@@ -411,8 +412,9 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
Timestamp: time.Now().UnixMilli(), Timestamp: time.Now().UnixMilli(),
}) })
// 持久化 AI 回复到数据库(在 WebSocket 发送之前) // 持久化 AI 回复到数据库
if fullText != "" { // 如果有审查消息,每条已单独持久化,跳过 fullText 以避免重复
if !hasReview && fullText != "" {
if h.sessionStore != nil && h.sessionStore.IsAvailable() { if h.sessionStore != nil && h.sessionStore.IsAvailable() {
if err := h.sessionStore.AddMessage(client.SessionID, "assistant", fullText); err != nil { if err := h.sessionStore.AddMessage(client.SessionID, "assistant", fullText); err != nil {
log.Printf("[chat] 持久化 AI 回复失败: %v", err) log.Printf("[chat] 持久化 AI 回复失败: %v", err)
@@ -426,7 +428,12 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
Timestamp: time.Now().UnixMilli(), Timestamp: time.Now().UnixMilli(),
}) })
} }
h.hub.RecordMessage(client.SessionID, "assistant", fullText) // RecordMessage 使用不带 [review] 标记的文本
recordText := fullText
if hasReview {
recordText = strings.ReplaceAll(fullText, "[review]", "")
}
h.hub.RecordMessage(client.SessionID, "assistant", recordText)
// 设置会话状态为 idle // 设置会话状态为 idle
h.hub.UpdateSessionState(client.SessionID, "idle") h.hub.UpdateSessionState(client.SessionID, "idle")