Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(im): tighten DingTalk rich-text follow-up handling
wizardchen81b4ab9e98f6
diff --git a/internal/im/dingtalk/adapter.go b/internal/im/dingtalk/adapter.goindex f76ba9bdb..554fe027d 100644--- a/internal/im/dingtalk/adapter.go+++ b/internal/im/dingtalk/adapter.go@@ -212,22 +212,48 @@ func parseRichTextContent(msgtype string, content json.RawMessage) (parsedRichTetextParts = append(textParts, text)}- if !strings.EqualFold(strings.TrimSpace(item.Type), "picture") &&- item.DownloadCode == "" && item.PictureDownloadCode == "" {+ code := pictureDownloadCode(item)+ if code == "" {continue}result.PictureCount++if result.DownloadCode == "" {- result.DownloadCode = item.DownloadCode- if result.DownloadCode == "" {- result.DownloadCode = item.PictureDownloadCode- }+ result.DownloadCode = code}}result.Text = strings.Join(textParts, "\n")return result, true}+func pictureDownloadCode(item richTextElement) string {+ if !strings.EqualFold(strings.TrimSpace(item.Type), "picture") {+ return ""+ }+ if item.DownloadCode != "" {+ return item.DownloadCode+ }+ return item.PictureDownloadCode+}++type audioMessageContent struct {+ Recognition string `json:"recognition"`+}++func parseAudioContent(msgtype string, content json.RawMessage) (string, bool) {+ if !strings.EqualFold(strings.TrimSpace(msgtype), "audio") {+ return "", false+ }+ var payload audioMessageContent+ if len(content) == 0 || json.Unmarshal(content, &payload) != nil {+ return "", false+ }+ text := strings.TrimSpace(payload.Recognition)+ if text == "" {+ return "", false+ }+ return text, 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@@ -359,6 +385,9 @@ func parseCallbackMessage(msg *callbackMessage) *im.IncomingMessage {incoming.FileName = defaultFileName(msgType, fileName, msg.MsgID)incoming.FileKey = downloadCodeextra["robot_code"] = msg.RobotCode+ } else if text, ok := parseAudioContent(msg.Msgtype, msg.Content); ok {+ incoming.MessageType = im.MessageTypeText+ incoming.Content = text} else {incoming.MessageType = im.MessageTypeTextif msg.Text != nil {@@ -386,6 +415,17 @@ func applyRichText(incoming *im.IncomingMessage, extra map[string]string, rich pincoming.FileKey = rich.DownloadCodeincoming.FileName = defaultFileName(im.MessageTypeImage, "", msgID)extra["robot_code"] = robotCode+ if rich.PictureCount > 1 {+ incoming.Content = appendDroppedPictureHint(incoming.Content, rich.PictureCount)+ }+}++func appendDroppedPictureHint(text string, pictureCount int) string {+ hint := fmt.Sprintf("(该消息共 %d 张图片,当前仅处理第一张)", pictureCount)+ if strings.TrimSpace(text) == "" {+ return hint+ }+ return text + "\n" + hint}// defaultFileName gives picture messages (which carry no fileName) a name with a@@ -479,6 +519,9 @@ func streamToIncoming(data *chatbot.BotCallbackDataModel, fallbackRobotCode striincoming.FileName = defaultFileName(msgType, fileName, data.MsgId)incoming.FileKey = downloadCodeextra["robot_code"] = fallbackRobotCode+ } else if text, ok := parseAudioContent(data.Msgtype, contentRaw); ok {+ incoming.MessageType = im.MessageTypeText+ incoming.Content = text} else {incoming.MessageType = im.MessageTypeTextincoming.Content = strings.TrimSpace(data.Text.Content)diff --git a/internal/im/dingtalk/adapter_test.go b/internal/im/dingtalk/adapter_test.goindex 28749ef64..1c33cac2d 100644--- a/internal/im/dingtalk/adapter_test.go+++ b/internal/im/dingtalk/adapter_test.go@@ -2,6 +2,7 @@ package dingtalkimport ("encoding/json"+ "strings""testing""github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"@@ -239,6 +240,142 @@ func TestParseCallbackMessage_RichTextPictureOnlyUsesImagePath(t *testing.T) {}}+func TestParseCallbackMessage_RichTextTextOnly(t *testing.T) {+ msg := &callbackMessage{+ MsgID: "rich-text",+ Msgtype: "richText",+ Content: json.RawMessage(`{"richText":[{"text":" 只发文字 "}]}`),+ }++ got := parseCallbackMessage(msg)+ if got.MessageType != im.MessageTypeText {+ t.Fatalf("MessageType = %q, want %q", got.MessageType, im.MessageTypeText)+ }+ if got.Content != "只发文字" {+ t.Errorf("Content = %q, want 只发文字", got.Content)+ }+ if got.FileKey != "" {+ t.Errorf("FileKey = %q, want empty", got.FileKey)+ }+}++func TestParseCallbackMessage_RichTextOfficialPictureSample(t *testing.T) {+ msg := &callbackMessage{+ MsgID: "official-rich",+ Msgtype: "richText",+ RobotCode: "robot-123",+ Content: json.RawMessage(`{+ "richText":[+ {"text":"你好"},+ {"downloadCode":"OFFICIAL-PIC","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 你好", got.Content)+ }+ if got.FileKey != "OFFICIAL-PIC" {+ t.Errorf("FileKey = %q, want OFFICIAL-PIC", got.FileKey)+ }+}++func TestParseCallbackMessage_RichTextKeepsFirstPictureAndHints(t *testing.T) {+ msg := &callbackMessage{+ MsgID: "rich-multi",+ Msgtype: "richText",+ Content: json.RawMessage(`{+ "richText":[+ {"text":"对比这几张图"},+ {"downloadCode":"PIC-1","type":"picture"},+ {"downloadCode":"PIC-2","type":"picture"},+ {"downloadCode":"PIC-3","type":"picture"}+ ]+ }`),+ }++ got := parseCallbackMessage(msg)+ if got.FileKey != "PIC-1" {+ t.Errorf("FileKey = %q, want PIC-1", got.FileKey)+ }+ if got.Extra["rich_text_picture_count"] != "3" {+ t.Errorf("rich_text_picture_count = %q, want 3", got.Extra["rich_text_picture_count"])+ }+ if !strings.Contains(got.Content, "对比这几张图") || !strings.Contains(got.Content, "共 3 张图片") {+ t.Errorf("Content = %q, want text plus dropped-picture hint", got.Content)+ }+}++func TestParseCallbackMessage_RichTextIgnoresDownloadCodeWithoutPictureType(t *testing.T) {+ msg := &callbackMessage{+ MsgID: "rich-notype",+ Msgtype: "richText",+ Content: json.RawMessage(`{"richText":[{"text":"hello","downloadCode":"NOT-A-PIC"}]}`),+ }++ got := parseCallbackMessage(msg)+ if got.MessageType != im.MessageTypeText {+ t.Fatalf("MessageType = %q, want %q", got.MessageType, im.MessageTypeText)+ }+ if got.FileKey != "" {+ t.Errorf("FileKey = %q, want empty", got.FileKey)+ }+}++func TestParseCallbackMessage_RichTextEmptyOrInvalidFallsBackToEmptyText(t *testing.T) {+ empty := parseCallbackMessage(&callbackMessage{+ MsgID: "rich-empty",+ Msgtype: "richText",+ Content: json.RawMessage(`{"richText":[]}`),+ })+ if empty.MessageType != im.MessageTypeText || empty.Content != "" {+ t.Errorf("empty richText: type=%q content=%q", empty.MessageType, empty.Content)+ }++ invalid := parseCallbackMessage(&callbackMessage{+ MsgID: "rich-bad",+ Msgtype: "richText",+ Content: json.RawMessage(`not-json`),+ })+ if invalid.MessageType != im.MessageTypeText || invalid.Content != "" {+ t.Errorf("invalid richText: type=%q content=%q", invalid.MessageType, invalid.Content)+ }+}++func TestParseCallbackMessage_AudioUsesRecognition(t *testing.T) {+ msg := &callbackMessage{+ MsgID: "audio-1",+ Msgtype: "audio",+ Content: json.RawMessage(`{"duration":4000,"downloadCode":"AUD-CODE","recognition":" 钉钉,让进步发生 "}`),+ }++ got := parseCallbackMessage(msg)+ if got.MessageType != im.MessageTypeText {+ t.Fatalf("MessageType = %q, want %q", got.MessageType, im.MessageTypeText)+ }+ if got.Content != "钉钉,让进步发生" {+ t.Errorf("Content = %q, want recognition text", got.Content)+ }+ if got.Extra["raw_msgtype"] != "audio" {+ t.Errorf("raw_msgtype = %q, want audio", got.Extra["raw_msgtype"])+ }+}++func TestParseCallbackMessage_AudioWithoutRecognitionStaysEmpty(t *testing.T) {+ got := parseCallbackMessage(&callbackMessage{+ MsgID: "audio-empty",+ Msgtype: "audio",+ Content: json.RawMessage(`{"duration":1000,"downloadCode":"AUD-CODE"}`),+ })+ if got.MessageType != im.MessageTypeText || got.Content != "" {+ t.Errorf("audio without recognition: type=%q content=%q", got.MessageType, got.Content)+ }+}+func TestStreamToIncoming_File(t *testing.T) {data := &chatbot.BotCallbackDataModel{MsgId: "m3",@@ -330,3 +467,22 @@ func TestStreamToIncoming_RichTextPreservesTextAndPictureMetadata(t *testing.T)t.Errorf("robot_code = %q, want client-app-key", got.Extra["robot_code"])}}++func TestStreamToIncoming_AudioUsesRecognition(t *testing.T) {+ data := &chatbot.BotCallbackDataModel{+ MsgId: "stream-audio",+ Msgtype: "audio",+ Content: map[string]interface{}{+ "recognition": "语音转写结果",+ "downloadCode": "STREAM-AUD",+ },+ }++ got := streamToIncoming(data, "client-app-key")+ if got.MessageType != im.MessageTypeText {+ t.Fatalf("MessageType = %q, want %q", got.MessageType, im.MessageTypeText)+ }+ if got.Content != "语音转写结果" {+ t.Errorf("Content = %q, want recognition text", got.Content)+ }+}diff --git a/internal/im/file_message_test.go b/internal/im/file_message_test.goindex af4be5971..3653fb612 100644--- a/internal/im/file_message_test.go+++ b/internal/im/file_message_test.go@@ -53,6 +53,80 @@ func TestFileMessageQAContent(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: "未能识别这条消息中的文字内容。请改用纯文本发送;图片或文件请单独发送。",+ },+ {+ name: "image without caption is accepted before QA content fill",+ msg: &IncomingMessage{MessageType: MessageTypeImage, FileKey: "pic-1", FileName: "pic-1.png"},+ wantEmpty: false,+ },+ {+ name: "file without caption is accepted before QA content fill",+ msg: &IncomingMessage{MessageType: MessageTypeFile, FileKey: "file-1", FileName: "spec.pdf"},+ wantEmpty: false,+ },+ {+ name: "file key is treated as an attachment even if type stays text",+ msg: &IncomingMessage{MessageType: MessageTypeText, FileKey: "pic-1"},+ wantEmpty: false,+ },+ {+ name: "audio without recognition uses a voice-specific hint",+ msg: &IncomingMessage{+ MessageType: MessageTypeText,+ Extra: map[string]string{"raw_msgtype": "audio"},+ },+ wantEmpty: true,+ wantHint: "未能识别这条语音中的文字内容。请改用纯文本发送,或再说一遍。",+ },+ {+ name: "video uses an unsupported-type hint",+ msg: &IncomingMessage{+ MessageType: MessageTypeText,+ Extra: map[string]string{"raw_msgtype": "video"},+ },+ 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 TestEmptyIncomingMessageReplyAfterFileQAContent(t *testing.T) {+ msg := &IncomingMessage{MessageType: MessageTypeImage, FileName: "shot.png"}+ msg.Content = fileMessageQAContent(msg)+ if hint, empty := emptyIncomingMessageReply(msg); empty {+ t.Fatalf("image after fileMessageQAContent was rejected: hint=%q", hint)+ }+}+func TestApplyIMAttachmentTruncationByLineCount(t *testing.T) {lines := make([]string, maxIMAttachmentLines+1)for i := range lines {diff --git a/internal/im/service.go b/internal/im/service.goindex f9295b41a..3f6dacd15 100644--- a/internal/im/service.go+++ b/internal/im/service.go@@ -1852,7 +1852,27 @@ func emptyIncomingMessageReply(msg *IncomingMessage) (string, bool) {if msg == nil || strings.TrimSpace(msg.Content) != "" {return "", false}- return "未能识别这条消息中的文字内容。请改用纯文本发送;图片或文件请单独发送。", true+ // Image/file events are allowed to arrive without a caption. Do not depend+ // on fileMessageQAContent having already filled Content — a later reorder+ // of HandleMessage must not reject attachments.+ hasAttachment := msg.MessageType == MessageTypeFile ||+ msg.MessageType == MessageTypeImage ||+ strings.TrimSpace(msg.FileKey) != ""+ if hasAttachment {+ return "", false+ }+ rawType := ""+ if msg.Extra != nil {+ rawType = strings.ToLower(strings.TrimSpace(msg.Extra["raw_msgtype"]))+ }+ switch rawType {+ case "audio":+ return "未能识别这条语音中的文字内容。请改用纯文本发送,或再说一遍。", true+ case "video":+ return "暂不支持视频消息。请改用纯文本发送;图片或文件请单独发送。", true+ default:+ return "未能识别这条消息中的文字内容。请改用纯文本发送;图片或文件请单独发送。", true+ }}func (s *Service) persistIMLastRequestState(ctx context.Context, sessionID, agentID string, customAgent *types.CustomAgent, kbIDs []string) {diff --git a/internal/im/session_test.go b/internal/im/session_test.goindex 25f7a05ea..513cd9e0d 100644--- a/internal/im/session_test.go+++ b/internal/im/session_test.go@@ -151,39 +151,6 @@ 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",