From f6b484c94ab9dc639c6ccab3f916f56cf7a86719 Mon Sep 17 00:00:00 2001 From: nullkey Date: Mon, 13 Apr 2026 14:18:22 +0800 Subject: [PATCH] fix(im): strip and citation tags before sending to IM platforms IM platforms (WeCom, Feishu, Slack, etc.) cannot render the inline citation tags produced by the agent pipeline, causing raw tags like to appear in user-facing messages. Strip these tags in the IM service layer before sending, while preserving the original content in the database for web frontend rendering. --- internal/im/service.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/im/service.go b/internal/im/service.go index 73fe43c1..cec15c1b 100644 --- a/internal/im/service.go +++ b/internal/im/service.go @@ -7,6 +7,7 @@ import ( "io" "mime/multipart" "net/textproto" + "regexp" "strings" "sync" "time" @@ -42,6 +43,16 @@ const ( streamFlushInterval = 300 * time.Millisecond ) +// imCitationTagRe matches inline citation tags produced by the agent pipeline. +// These tags are rendered as interactive UI in the web frontend but are meaningless +// in IM platforms, so they must be stripped before sending. +var imCitationTagRe = regexp.MustCompile(`<(?:kb|web)\b[^>]*/?>`) + +// stripIMCitationTags removes and inline citation tags from s. +func stripIMCitationTags(s string) string { + return imCitationTagRe.ReplaceAllString(s, "") +} + const ( // wsLeaderTTL is the TTL for the Redis key used for WebSocket leader election. wsLeaderTTL = 15 * time.Second @@ -1662,7 +1673,7 @@ func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage, bufMu.Unlock() if chunk != "" { - if err := streamer.SendStreamChunk(ctx, msg, streamID, chunk); err != nil { + if err := streamer.SendStreamChunk(ctx, msg, streamID, stripIMCitationTags(chunk)); err != nil { logger.Warnf(ctx, "[IM] SendStreamChunk failed: %v", err) } } @@ -1870,14 +1881,16 @@ func (s *Service) runQA(ctx context.Context, session *types.Session, query strin answer = "抱歉,我暂时无法回答这个问题。" } - // Update assistant message with the final answer content + // Update assistant message with the full answer (including citation tags for web rendering). assistantMsg.Content = answer assistantMsg.IsCompleted = true if err := s.messageService.UpdateMessage(ctx, assistantMsg); err != nil { logger.Warnf(ctx, "[IM] Failed to update assistant message: %v", err) } - return answer, nil + // Strip citation tags before returning to the IM adapter — IM platforms cannot + // render / and would display them as raw text. + return stripIMCitationTags(answer), nil } // ── CRUD operations for IM channels ──