From 6e47978d1b33ad5b46f58a7452a0b75a76edf536 Mon Sep 17 00:00:00 2001 From: haozi Date: Sun, 14 Jun 2026 12:07:37 +0800 Subject: [PATCH] fix: feishu output issues (topic_group, UTF-8 truncation, ThreadID, json.Marshal error handling) --- internal/bot/feishu/feishu.go | 17 ++++++++++++++--- internal/bot/render.go | 7 ++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/bot/feishu/feishu.go b/internal/bot/feishu/feishu.go index cb62a1371..9953b6ea4 100644 --- a/internal/bot/feishu/feishu.go +++ b/internal/bot/feishu/feishu.go @@ -58,6 +58,7 @@ type feishuMsgEvent struct { MessageID string `json:"message_id"` RootID string `json:"root_id"` ParentID string `json:"parent_id"` + ThreadID string `json:"thread_id"` ChatID string `json:"chat_id"` ChatType string `json:"chat_type"` MsgType string `json:"msg_type"` @@ -432,9 +433,9 @@ func (a *adapter) handleMessage(msg feishuMsgEvent) { return } - // @mention gating:仅在群聊中检查是否 @了 bot + // @mention gating:仅在群聊或话题群中检查是否 @了 bot chatType := bot.ChatDM - if msg.ChatType == "group" { + if msg.ChatType == "group" || msg.ChatType == "topic_group" { chatType = bot.ChatGroup if a.cfg.RequireMention && len(msg.Mentions) == 0 { a.logger.Info("feishu message ignored", "reason", "missing_mention", "chat", logHash(msg.ChatID), "message", logHash(msg.MessageID)) @@ -442,6 +443,11 @@ func (a *adapter) handleMessage(msg feishuMsgEvent) { } } + threadID := firstNonEmpty(msg.ThreadID, msg.RootID) + if threadID == "" && msg.ChatType == "topic_group" { + threadID = msg.ChatID + } + ib := bot.InboundMessage{ Platform: bot.PlatformFeishu, ChatType: chatType, @@ -450,6 +456,7 @@ func (a *adapter) handleMessage(msg feishuMsgEvent) { UserName: "", Text: content.Text, MessageID: msg.MessageID, + ThreadID: threadID, } // 获取用户信息填充用户名 @@ -623,7 +630,11 @@ func (a *adapter) sendCard(ctx context.Context, msg bot.OutboundMessage) (bot.Se "elements": elements, } - cardJSON, _ := json.Marshal(cardPayload) + cardJSON, err := json.Marshal(cardPayload) + if err != nil { + a.logger.Warn("feishu card marshal error", "err", err) + return bot.SendResult{}, fmt.Errorf("feishu card marshal: %w", err) + } return a.sendSDKContent(ctx, msg, larkim.MsgTypeInteractive, string(cardJSON)) } diff --git a/internal/bot/render.go b/internal/bot/render.go index 3ccce879d..ec296a84a 100644 --- a/internal/bot/render.go +++ b/internal/bot/render.go @@ -93,10 +93,11 @@ func (s *renderSink) Emit(e event.Event) { if e.Tool.Err != "" { fmt.Fprintf(&s.buf, "\n❌ %s 出错: %s", name, e.Tool.Err) } else { - // 截断输出 + // 截断输出(按字符数而非字节数,避免截断多字节 UTF-8 字符) output := e.Tool.Output - if len(output) > 500 { - output = output[:500] + "\n... (已截断)" + runes := []rune(output) + if len(runes) > 500 { + output = string(runes[:500]) + "\n... (已截断)" } fmt.Fprintf(&s.buf, "\n✅ %s 完成", name) if output != "" {