fix: 修复历史消息重复和[review]标记残留
- 移除 fullText 中的 "[review]" 调试标记 - 有审查消息时不再重复保存完整文本到数据库 - 清理已有脏数据 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -231,6 +231,7 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
|
||||
|
||||
var fullText string
|
||||
var msgID string
|
||||
var hasReview bool // 是否有审查消息(避免重复持久化)
|
||||
var segments []ws.VoiceSegment // 收集断句信息
|
||||
|
||||
for scanner.Scan() {
|
||||
@@ -327,7 +328,7 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
fullText += "[review]"
|
||||
hasReview = true
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -411,8 +412,9 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
|
||||
Timestamp: time.Now().UnixMilli(),
|
||||
})
|
||||
|
||||
// 持久化 AI 回复到数据库(在 WebSocket 发送之前)
|
||||
if fullText != "" {
|
||||
// 持久化 AI 回复到数据库
|
||||
// 如果有审查消息,每条已单独持久化,跳过 fullText 以避免重复
|
||||
if !hasReview && fullText != "" {
|
||||
if h.sessionStore != nil && h.sessionStore.IsAvailable() {
|
||||
if err := h.sessionStore.AddMessage(client.SessionID, "assistant", fullText); err != nil {
|
||||
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(),
|
||||
})
|
||||
}
|
||||
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
|
||||
h.hub.UpdateSessionState(client.SessionID, "idle")
|
||||
|
||||
Reference in New Issue
Block a user