fix(im): anti-hallucination for non-text quotes and unprocessable media messages
- Replace content placeholders with LLM instructions for non-text quotes (NonTextType field) - Intercept image/file messages on channels without KB config - Wrap text quotes in <quoted_message> XML tags with escape sanitization - Add nil safety to isQuoteFromBot, move debug logging to callers with real ctx - Improve reply copy to guide users toward channel KB configuration
This commit is contained in:
@@ -573,8 +573,8 @@ func (c *LongConnClient) handleCallback(ctx context.Context, frame wsFrame) {
|
||||
if incoming != nil && msg.Quote != nil {
|
||||
incoming.Quote = buildQuotedMessage(msg.Quote, msg.AiBotID)
|
||||
if incoming.Quote != nil {
|
||||
logger.Infof(ctx, "[WeCom] Quote detected: msgid=%s sender=%s is_bot=%v content_len=%d",
|
||||
msg.Quote.MsgID, msg.Quote.From.UserID, incoming.Quote.IsBotMessage, len(incoming.Quote.Content))
|
||||
logger.Infof(ctx, "[WeCom] Quote detected: msgid=%s sender=%s is_bot=%v content_len=%d non_text_type=%s",
|
||||
msg.Quote.MsgID, msg.Quote.From.UserID, incoming.Quote.IsBotMessage, len(incoming.Quote.Content), incoming.Quote.NonTextType)
|
||||
// Debug: log raw IDs for bot identity verification during initial rollout
|
||||
logger.Debugf(ctx, "[WeCom] Quote identity debug: quote.from.userid=%q quote.aibotid=%q msg.aibotid=%q",
|
||||
msg.Quote.From.UserID, msg.Quote.AiBotID, msg.AiBotID)
|
||||
|
||||
+18
-19
@@ -7,7 +7,9 @@ import (
|
||||
)
|
||||
|
||||
// extractQuoteContent extracts text content from a quoted botMessage.
|
||||
// For non-text message types, returns a descriptive placeholder.
|
||||
// Only returns actual textual content. Non-text types (image, file, video, etc.)
|
||||
// return empty string so that buildQuotedMessage discards them — injecting
|
||||
// "cannot see image" placeholders causes LLM hallucination from conversation history.
|
||||
func extractQuoteContent(quote *botMessage) string {
|
||||
if quote == nil {
|
||||
return ""
|
||||
@@ -16,34 +18,28 @@ func extractQuoteContent(quote *botMessage) string {
|
||||
case "text":
|
||||
return quote.Text.Content
|
||||
case "voice":
|
||||
if quote.Voice.Content != "" {
|
||||
return quote.Voice.Content
|
||||
}
|
||||
return "[语音消息]"
|
||||
return quote.Voice.Content // STT result; empty if recognition failed
|
||||
case "mixed":
|
||||
// Only keep text parts; skip images/other non-text items entirely.
|
||||
var parts []string
|
||||
for _, item := range quote.Mixed.MsgItem {
|
||||
if item.MsgType == "text" && item.Text.Content != "" {
|
||||
parts = append(parts, item.Text.Content)
|
||||
} else if item.MsgType == "image" {
|
||||
parts = append(parts, "[图片]")
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n")
|
||||
case "image":
|
||||
return "[图片]"
|
||||
case "file":
|
||||
return "[文件]"
|
||||
case "video":
|
||||
return "[视频]"
|
||||
default:
|
||||
return "[消息]"
|
||||
// image, file, video, unknown — no textual content available, discard.
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// isQuoteFromBot determines whether a quoted message was sent by the bot.
|
||||
// Uses two comparison strategies since WeCom's field mapping is undocumented.
|
||||
func isQuoteFromBot(quote *botMessage, aiBotID string) bool {
|
||||
if quote == nil {
|
||||
return false
|
||||
}
|
||||
if quote.From.UserID != "" && aiBotID != "" && quote.From.UserID == aiBotID {
|
||||
return true
|
||||
}
|
||||
@@ -54,19 +50,22 @@ func isQuoteFromBot(quote *botMessage, aiBotID string) bool {
|
||||
}
|
||||
|
||||
// buildQuotedMessage constructs a QuotedMessage from a botMessage quote.
|
||||
// Returns nil if the quote is nil or has no extractable content.
|
||||
// Returns nil only if quote itself is nil.
|
||||
// For non-text quotes (image, file, video), Content is empty and NonTextType
|
||||
// is set so downstream can generate an LLM instruction instead of a placeholder.
|
||||
func buildQuotedMessage(quote *botMessage, aiBotID string) *im.QuotedMessage {
|
||||
if quote == nil {
|
||||
return nil
|
||||
}
|
||||
content := extractQuoteContent(quote)
|
||||
if content == "" {
|
||||
return nil
|
||||
}
|
||||
return &im.QuotedMessage{
|
||||
result := &im.QuotedMessage{
|
||||
MessageID: quote.MsgID,
|
||||
Content: content,
|
||||
SenderID: quote.From.UserID,
|
||||
IsBotMessage: isQuoteFromBot(quote, aiBotID),
|
||||
}
|
||||
if content == "" {
|
||||
result.NonTextType = quote.MsgType
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -28,30 +28,30 @@ func TestExtractQuoteContent(t *testing.T) {
|
||||
{
|
||||
name: "voice without STT content",
|
||||
msg: &botMessage{MsgType: "voice"},
|
||||
want: "[语音消息]",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "image",
|
||||
name: "image returns empty (discarded to prevent hallucination)",
|
||||
msg: &botMessage{MsgType: "image"},
|
||||
want: "[图片]",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "file",
|
||||
name: "file returns empty",
|
||||
msg: &botMessage{MsgType: "file"},
|
||||
want: "[文件]",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "video",
|
||||
name: "video returns empty",
|
||||
msg: &botMessage{MsgType: "video"},
|
||||
want: "[视频]",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "unknown type",
|
||||
name: "unknown type returns empty",
|
||||
msg: &botMessage{MsgType: "location"},
|
||||
want: "[消息]",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "mixed text and image",
|
||||
name: "mixed text and image keeps only text",
|
||||
msg: &botMessage{
|
||||
MsgType: "mixed",
|
||||
Mixed: struct{ MsgItem []botMixedItem `json:"msg_item"` }{
|
||||
@@ -62,10 +62,10 @@ func TestExtractQuoteContent(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "part1\n[图片]\npart2",
|
||||
want: "part1\npart2",
|
||||
},
|
||||
{
|
||||
name: "mixed with only images",
|
||||
name: "mixed with only images returns empty",
|
||||
msg: &botMessage{
|
||||
MsgType: "mixed",
|
||||
Mixed: struct{ MsgItem []botMixedItem `json:"msg_item"` }{
|
||||
@@ -74,7 +74,7 @@ func TestExtractQuoteContent(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "[图片]",
|
||||
want: "",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -95,6 +95,12 @@ func TestIsQuoteFromBot(t *testing.T) {
|
||||
aiBotID string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "nil quote",
|
||||
quote: nil,
|
||||
aiBotID: "bot123",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "matching from.userid",
|
||||
quote: &botMessage{From: struct{ UserID string `json:"userid"` }{UserID: "bot123"}, AiBotID: ""},
|
||||
@@ -162,6 +168,42 @@ func TestBuildQuotedMessage(t *testing.T) {
|
||||
if !got.IsBotMessage {
|
||||
t.Error("IsBotMessage = false, want true")
|
||||
}
|
||||
if got.NonTextType != "" {
|
||||
t.Errorf("NonTextType = %q, want empty for text quote", got.NonTextType)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("image quote sets NonTextType", func(t *testing.T) {
|
||||
msg := &botMessage{
|
||||
MsgID: "msg-img",
|
||||
MsgType: "image",
|
||||
From: struct{ UserID string `json:"userid"` }{UserID: "user456"},
|
||||
}
|
||||
got := buildQuotedMessage(msg, "bot1")
|
||||
if got == nil {
|
||||
t.Fatal("expected non-nil for image quote")
|
||||
}
|
||||
if got.Content != "" {
|
||||
t.Errorf("Content = %q, want empty", got.Content)
|
||||
}
|
||||
if got.NonTextType != "image" {
|
||||
t.Errorf("NonTextType = %q, want %q", got.NonTextType, "image")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("video quote sets NonTextType", func(t *testing.T) {
|
||||
msg := &botMessage{
|
||||
MsgID: "msg-vid",
|
||||
MsgType: "video",
|
||||
From: struct{ UserID string `json:"userid"` }{UserID: "user456"},
|
||||
}
|
||||
got := buildQuotedMessage(msg, "bot1")
|
||||
if got == nil {
|
||||
t.Fatal("expected non-nil for video quote")
|
||||
}
|
||||
if got.NonTextType != "video" {
|
||||
t.Errorf("NonTextType = %q, want %q", got.NonTextType, "video")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("user quote is not bot", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user