diff --git a/internal/im/dingtalk/adapter.go b/internal/im/dingtalk/adapter.go index a39c45d0..f76ba9bd 100644 --- a/internal/im/dingtalk/adapter.go +++ b/internal/im/dingtalk/adapter.go @@ -174,6 +174,60 @@ type fileMessageContent struct { FileName string `json:"fileName"` } +// richTextMessageContent is the payload DingTalk sends for msgtype=richText. +// Text and pictures are interleaved in their original display order. The +// unified IM message currently carries one downloadable file, so we retain the +// first picture while preserving all text fragments for the QA query. +type richTextMessageContent struct { + RichText []richTextElement `json:"richText"` +} + +type richTextElement struct { + Text string `json:"text"` + Type string `json:"type"` + DownloadCode string `json:"downloadCode"` + PictureDownloadCode string `json:"pictureDownloadCode"` +} + +type parsedRichText struct { + Text string + DownloadCode string + PictureCount int +} + +func parseRichTextContent(msgtype string, content json.RawMessage) (parsedRichText, bool) { + if !strings.EqualFold(strings.TrimSpace(msgtype), "richText") { + return parsedRichText{}, false + } + + var payload richTextMessageContent + if len(content) == 0 || json.Unmarshal(content, &payload) != nil { + return parsedRichText{}, false + } + + textParts := make([]string, 0, len(payload.RichText)) + result := parsedRichText{} + for _, item := range payload.RichText { + if text := strings.TrimSpace(item.Text); text != "" { + textParts = append(textParts, text) + } + + if !strings.EqualFold(strings.TrimSpace(item.Type), "picture") && + item.DownloadCode == "" && item.PictureDownloadCode == "" { + continue + } + result.PictureCount++ + if result.DownloadCode == "" { + result.DownloadCode = item.DownloadCode + if result.DownloadCode == "" { + result.DownloadCode = item.PictureDownloadCode + } + } + } + result.Text = strings.Join(textParts, "\n") + return result, true +} + // parseFileContent maps a DingTalk msgtype + content object to WeKnora's file // message fields. Returns ok=false for non-file/picture message types so the // caller keeps its text handling. Picture messages have no fileName; the IM @@ -286,6 +340,7 @@ func parseCallbackMessage(msg *callbackMessage) *im.IncomingMessage { extra := map[string]string{ "session_webhook": msg.SessionWebhook, + "raw_msgtype": msg.Msgtype, } incoming := &im.IncomingMessage{ Platform: im.PlatformDingtalk, @@ -297,7 +352,9 @@ func parseCallbackMessage(msg *callbackMessage) *im.IncomingMessage { Extra: extra, } - if msgType, fileName, downloadCode, ok := parseFileContent(msg.Msgtype, msg.Content); ok { + if rich, ok := parseRichTextContent(msg.Msgtype, msg.Content); ok { + applyRichText(incoming, extra, rich, msg.MsgID, msg.RobotCode) + } else if msgType, fileName, downloadCode, ok := parseFileContent(msg.Msgtype, msg.Content); ok { incoming.MessageType = msgType incoming.FileName = defaultFileName(msgType, fileName, msg.MsgID) incoming.FileKey = downloadCode @@ -311,6 +368,26 @@ func parseCallbackMessage(msg *callbackMessage) *im.IncomingMessage { return incoming } +func applyRichText(incoming *im.IncomingMessage, extra map[string]string, rich parsedRichText, msgID, robotCode string) { + incoming.Content = rich.Text + if rich.DownloadCode != "" { + incoming.MessageType = im.MessageTypeImage + } else { + incoming.MessageType = im.MessageTypeText + } + + if rich.PictureCount == 0 { + return + } + extra["rich_text_picture_count"] = strconv.Itoa(rich.PictureCount) + if rich.DownloadCode == "" { + return + } + incoming.FileKey = rich.DownloadCode + incoming.FileName = defaultFileName(im.MessageTypeImage, "", msgID) + extra["robot_code"] = robotCode +} + // defaultFileName gives picture messages (which carry no fileName) a name with a // real stem derived from the message ID, mirroring the WeCom adapter. File // messages keep their original name; if missing, fall back to the message ID so @@ -374,6 +451,7 @@ func streamToIncoming(data *chatbot.BotCallbackDataModel, fallbackRobotCode stri extra := map[string]string{ "session_webhook": data.SessionWebhook, + "raw_msgtype": data.Msgtype, } incoming := &im.IncomingMessage{ Platform: im.PlatformDingtalk, @@ -394,7 +472,9 @@ func streamToIncoming(data *chatbot.BotCallbackDataModel, fallbackRobotCode stri } } - if msgType, fileName, downloadCode, ok := parseFileContent(data.Msgtype, contentRaw); ok { + if rich, ok := parseRichTextContent(data.Msgtype, contentRaw); ok { + applyRichText(incoming, extra, rich, data.MsgId, fallbackRobotCode) + } else if msgType, fileName, downloadCode, ok := parseFileContent(data.Msgtype, contentRaw); ok { incoming.MessageType = msgType incoming.FileName = defaultFileName(msgType, fileName, data.MsgId) incoming.FileKey = downloadCode diff --git a/internal/im/dingtalk/adapter_test.go b/internal/im/dingtalk/adapter_test.go index 216de9ba..28749ef6 100644 --- a/internal/im/dingtalk/adapter_test.go +++ b/internal/im/dingtalk/adapter_test.go @@ -180,6 +180,65 @@ func TestParseCallbackMessage_TextStillWorks(t *testing.T) { } } +func TestParseCallbackMessage_RichTextPreservesTextAndPictureMetadata(t *testing.T) { + msg := &callbackMessage{ + MsgID: "rich-1", + Msgtype: "richText", + RobotCode: "robot-123", + Content: json.RawMessage(`{ + "richText":[ + {"text":" 这个问题示例图如下: "}, + {"pictureDownloadCode":"PIC-FALLBACK","downloadCode":"PIC-CODE","type":"picture"}, + {"text":"通过以上示意图可以明白完整的交互流程"} + ] + }`), + } + + got := parseCallbackMessage(msg) + if got.MessageType != im.MessageTypeImage { + t.Fatalf("MessageType = %q, want %q", got.MessageType, im.MessageTypeImage) + } + if got.Content != "这个问题示例图如下:\n通过以上示意图可以明白完整的交互流程" { + t.Errorf("Content = %q", got.Content) + } + if got.FileKey != "PIC-CODE" { + t.Errorf("FileKey = %q, want PIC-CODE", got.FileKey) + } + if got.FileName != "rich-1.png" { + t.Errorf("FileName = %q, want rich-1.png", got.FileName) + } + if got.Extra["raw_msgtype"] != "richText" { + t.Errorf("raw_msgtype = %q, want richText", got.Extra["raw_msgtype"]) + } + if got.Extra["rich_text_picture_count"] != "1" { + t.Errorf("rich_text_picture_count = %q, want 1", got.Extra["rich_text_picture_count"]) + } + if got.Extra["robot_code"] != "robot-123" { + t.Errorf("robot_code = %q, want robot-123", got.Extra["robot_code"]) + } +} + +func TestParseCallbackMessage_RichTextPictureOnlyUsesImagePath(t *testing.T) { + msg := &callbackMessage{ + MsgID: "rich-picture", + Msgtype: "richText", + Content: json.RawMessage(`{ + "richText":[{"pictureDownloadCode":"PIC-CODE","type":"picture"}] + }`), + } + + got := parseCallbackMessage(msg) + if got.MessageType != im.MessageTypeImage { + t.Fatalf("MessageType = %q, want %q", got.MessageType, im.MessageTypeImage) + } + if got.Content != "" { + t.Errorf("Content = %q, want empty", got.Content) + } + if got.FileKey != "PIC-CODE" { + t.Errorf("FileKey = %q, want PIC-CODE", got.FileKey) + } +} + func TestStreamToIncoming_File(t *testing.T) { data := &chatbot.BotCallbackDataModel{ MsgId: "m3", @@ -241,3 +300,33 @@ func TestStreamToIncoming_TextStillWorks(t *testing.T) { t.Errorf("Content = %q, want %q", got.Content, "hi") } } + +func TestStreamToIncoming_RichTextPreservesTextAndPictureMetadata(t *testing.T) { + data := &chatbot.BotCallbackDataModel{ + MsgId: "stream-rich", + Msgtype: "richText", + Content: map[string]interface{}{ + "richText": []interface{}{ + map[string]interface{}{"text": "这种场景下,AI 去纹身去不干净"}, + map[string]interface{}{"downloadCode": "STREAM-PIC", "type": "picture"}, + }, + }, + } + + got := streamToIncoming(data, "client-app-key") + if got.MessageType != im.MessageTypeImage { + t.Fatalf("MessageType = %q, want %q", got.MessageType, im.MessageTypeImage) + } + if got.Content != "这种场景下,AI 去纹身去不干净" { + t.Errorf("Content = %q", got.Content) + } + if got.FileKey != "STREAM-PIC" { + t.Errorf("FileKey = %q, want STREAM-PIC", got.FileKey) + } + if got.Extra["raw_msgtype"] != "richText" { + t.Errorf("raw_msgtype = %q, want richText", got.Extra["raw_msgtype"]) + } + if got.Extra["robot_code"] != "client-app-key" { + t.Errorf("robot_code = %q, want client-app-key", got.Extra["robot_code"]) + } +} diff --git a/internal/im/service.go b/internal/im/service.go index 5c832d31..f9295b41 100644 --- a/internal/im/service.go +++ b/internal/im/service.go @@ -1690,8 +1690,8 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann logger.Infof(ctx, "[IM] HandleMessage: channel=%s platform=%s user=%s chat=%s msgtype=%s content_len=%d", channelID, msg.Platform, msg.UserID, msg.ChatID, msg.MessageType, len(msg.Content)) - logger.Debugf(ctx, "[IM] HandleMessage detail: msgid=%s filekey=%s filename=%s", - msg.MessageID, msg.FileKey, msg.FileName) + logger.Debugf(ctx, "[IM] HandleMessage detail: msgid=%s raw_msgtype=%s filekey=%s filename=%s", + msg.MessageID, msg.Extra["raw_msgtype"], msg.FileKey, msg.FileName) // ── File/Image message handling ── // File messages use the normal QA path as well. A configured knowledge base @@ -1702,6 +1702,22 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann msg.Content = fileMessageQAContent(msg) } + // Never send an empty query into rewrite/intent classification. Some IM + // platforms deliver unsupported rich message shapes with no normalized text; + // allowing those through makes the model infer an unrelated intent from an + // empty query (for example, rewriting it as a greeting). + if hint, empty := emptyIncomingMessageReply(msg); empty { + logger.Infof(ctx, "[IM] Skipping QA for message without content: type=%s raw_type=%s", + msg.MessageType, msg.Extra["raw_msgtype"]) + if err := adapter.SendReply(ctx, msg, &ReplyMessage{ + Content: hint, + IsFinal: true, + }); err != nil { + logger.Warnf(ctx, "[IM] Failed to send empty-message hint reply: %v", err) + } + return nil + } + // 1. Get tenant tenant, err := s.tenantService.GetTenantByID(ctx, tenantID) if err != nil { @@ -1832,6 +1848,13 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann return nil } +func emptyIncomingMessageReply(msg *IncomingMessage) (string, bool) { + if msg == nil || strings.TrimSpace(msg.Content) != "" { + return "", false + } + return "未能识别这条消息中的文字内容。请改用纯文本发送;图片或文件请单独发送。", true +} + func (s *Service) persistIMLastRequestState(ctx context.Context, sessionID, agentID string, customAgent *types.CustomAgent, kbIDs []string) { state := buildIMLastRequestState(agentID, customAgent, kbIDs) if err := s.sessionService.UpdateSessionLastRequestState(logger.CloneContext(context.WithoutCancel(ctx)), sessionID, state); err != nil { diff --git a/internal/im/session_test.go b/internal/im/session_test.go index 513cd9e0..25f7a05e 100644 --- a/internal/im/session_test.go +++ b/internal/im/session_test.go @@ -151,6 +151,39 @@ func TestIncomingMessageThreadID(t *testing.T) { } } +func TestEmptyIncomingMessageReply(t *testing.T) { + tests := []struct { + name string + msg *IncomingMessage + wantEmpty bool + wantHint string + }{ + { + name: "text content is accepted", + msg: &IncomingMessage{MessageType: MessageTypeText, Content: " hello "}, + wantEmpty: false, + }, + { + name: "blank text is rejected", + msg: &IncomingMessage{MessageType: MessageTypeText, Content: " \n\t "}, + wantEmpty: true, + wantHint: "未能识别这条消息中的文字内容。请改用纯文本发送;图片或文件请单独发送。", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotHint, gotEmpty := emptyIncomingMessageReply(tt.msg) + if gotEmpty != tt.wantEmpty { + t.Fatalf("empty = %v, want %v", gotEmpty, tt.wantEmpty) + } + if gotHint != tt.wantHint { + t.Fatalf("hint = %q, want %q", gotHint, tt.wantHint) + } + }) + } +} + func TestBuildIMLastRequestStateFromAgent(t *testing.T) { agent := &types.CustomAgent{ ID: "agent-1",