From 9083a3f2e3085bb9ee83d795d19a1dc513d2dd28 Mon Sep 17 00:00:00 2001 From: Jealous Date: Tue, 3 Mar 2026 12:40:17 +0800 Subject: [PATCH] fix(feishu): normalize all mentions in inbound agent context (#30252) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(feishu): normalize all mentions in inbound agent context Convert Feishu mention placeholders to explicit tags (including bot mentions), add mention semantics hints for the model, and remove unused mentionMessageBody parsing to keep context handling consistent. Co-Authored-By: Claude Sonnet 4.6 * fix(feishu): use replacer callback and escape only < > in normalizeMentions Switch String.replace to a function replacer to prevent $ sequences in display names from being interpolated as replacement patterns. Narrow escaping to < and > only — & does not need escaping in LLM prompt tag bodies and escaping it degrades readability (e.g. R&D → R&D). Co-Authored-By: Claude Sonnet 4.6 * fix(feishu): only use open_id in normalizeMentions tag, drop user_id fallback When a mention has no open_id, degrade to @name instead of emitting . This keeps the tag user_id space exclusively open_id, so the bot self-reference hint (which uses botOpenId) is always consistent with what appears in the tags. Co-Authored-By: Claude Sonnet 4.6 * fix(feishu): register mention strip pattern for tags in channel dock Add mentions.stripPatterns to feishuPlugin so that normalizeCommandBody receives a slash-clean string after normalizeMentions replaces Feishu placeholders with name tags. Without this, group slash commands like @Bot /help had their leading / obscured by the tag prefix and no longer triggered command handlers. Pattern mirrors the approach used by Slack (<@[^>]+>) and Discord (<@!?\d+>). Co-Authored-By: Claude Sonnet 4.6 * fix(feishu): strip bot mention in p2p to preserve DM slash commands In p2p messages the bot mention is a pure addressing prefix; converting it to breaks slash commands because buildCommandContext skips stripMentions for DMs. Extend normalizeMentions with a stripKeys set and populate it with bot mention keys in p2p, so @Bot /help arrives as /help. Non-bot mentions (mention-forward targets) are still normalized to tags in both p2p and group contexts. Co-Authored-By: Claude Sonnet 4.6 * Changelog: note Feishu inbound mention normalization --------- Co-authored-by: Claude Sonnet 4.6 Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com> --- CHANGELOG.md | 1 + .../feishu/src/bot.stripBotMention.test.ts | 128 +++++++++++++++--- extensions/feishu/src/bot.ts | 58 +++++--- extensions/feishu/src/channel.ts | 3 + extensions/feishu/src/types.ts | 3 +- 5 files changed, 153 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdf798406be..6ebcfaefb2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Docs: https://docs.openclaw.ai - Plugin command/runtime hardening: validate and normalize plugin command name/description at registration boundaries, and guard Telegram native menu normalization paths so malformed plugin command specs cannot crash startup (`trim` on undefined). (#31997) Fixes #31944. Thanks @liuxiaopai-ai. - Telegram: guard duplicate-token checks and gateway startup token normalization when account tokens are missing, preventing `token.trim()` crashes during status/start flows. (#31973) Thanks @ningding97. - Discord/lifecycle startup status: push an immediate `connected` status snapshot when the gateway is already connected before lifecycle debug listeners attach, with abort-guarding to avoid contradictory status flips during pre-aborted startup. (#32336) Thanks @mitchmcalister. +- Feishu/inbound mention normalization: preserve all inbound mention semantics by normalizing Feishu mention placeholders into explicit `name` tags (instead of stripping them), improving multi-mention context fidelity in agent prompts while retaining bot/self mention disambiguation. (#30252) Thanks @Lanfei. - Feishu/multi-app mention routing: guard mention detection in multi-bot groups by validating mention display name alongside bot `open_id`, preventing false-positive self-mentions from Feishu WebSocket remapping so only the actually mentioned bot responds under `requireMention`. (#30315) Thanks @teaguexiao. - Feishu/session-memory hook parity: trigger the shared `before_reset` session-memory hook path when Feishu `/new` and `/reset` commands execute so reset flows preserve memory behavior consistent with other channels. (#31437) Thanks @Linux2010. - Feishu/LINE group system prompts: forward per-group `systemPrompt` config into inbound context `GroupSystemPrompt` for Feishu and LINE group/room events so configured group-specific behavior actually applies at dispatch time. (#31713) Thanks @whiskyboy. diff --git a/extensions/feishu/src/bot.stripBotMention.test.ts b/extensions/feishu/src/bot.stripBotMention.test.ts index 98016115a1b..543af29a0eb 100644 --- a/extensions/feishu/src/bot.stripBotMention.test.ts +++ b/extensions/feishu/src/bot.stripBotMention.test.ts @@ -1,38 +1,122 @@ import { describe, expect, it } from "vitest"; -import { stripBotMention, type FeishuMessageEvent } from "./bot.js"; +import { parseFeishuMessageEvent } from "./bot.js"; -type Mentions = FeishuMessageEvent["message"]["mentions"]; +function makeEvent( + text: string, + mentions?: Array<{ key: string; name: string; id: { open_id?: string; user_id?: string } }>, + chatType: "p2p" | "group" = "p2p", +) { + return { + sender: { sender_id: { user_id: "u1", open_id: "ou_sender" } }, + message: { + message_id: "msg_1", + chat_id: "oc_chat1", + chat_type: chatType, + message_type: "text", + content: JSON.stringify({ text }), + mentions, + }, + }; +} -describe("stripBotMention", () => { +const BOT_OPEN_ID = "ou_bot"; + +describe("normalizeMentions (via parseFeishuMessageEvent)", () => { it("returns original text when mentions are missing", () => { - expect(stripBotMention("hello world", undefined)).toBe("hello world"); + const ctx = parseFeishuMessageEvent(makeEvent("hello world", undefined) as any, BOT_OPEN_ID); + expect(ctx.content).toBe("hello world"); }); - it("strips mention name and key for normal mentions", () => { - const mentions: Mentions = [{ key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }]; - expect(stripBotMention("@Bot hello @_bot_1", mentions)).toBe("hello"); + it("strips bot mention in p2p (addressing prefix, not semantic content)", () => { + const ctx = parseFeishuMessageEvent( + makeEvent("@_bot_1 hello", [ + { key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }, + ]) as any, + BOT_OPEN_ID, + ); + expect(ctx.content).toBe("hello"); }); - it("treats mention.name regex metacharacters as literal text", () => { - const mentions: Mentions = [{ key: "@_bot_1", name: ".*", id: { open_id: "ou_bot" } }]; - expect(stripBotMention("@NotBot hello", mentions)).toBe("@NotBot hello"); + it("normalizes bot mention to tag in group (semantic content)", () => { + const ctx = parseFeishuMessageEvent( + makeEvent( + "@_bot_1 hello", + [{ key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }], + "group", + ) as any, + BOT_OPEN_ID, + ); + expect(ctx.content).toBe('Bot hello'); }); - it("treats mention.key regex metacharacters as literal text", () => { - const mentions: Mentions = [{ key: ".*", name: "Bot", id: { open_id: "ou_bot" } }]; - expect(stripBotMention("hello world", mentions)).toBe("hello world"); + it("strips bot mention but normalizes other mentions in p2p (mention-forward)", () => { + const ctx = parseFeishuMessageEvent( + makeEvent("@_bot_1 @_user_alice hello", [ + { key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }, + { key: "@_user_alice", name: "Alice", id: { open_id: "ou_alice" } }, + ]) as any, + BOT_OPEN_ID, + ); + expect(ctx.content).toBe('Alice hello'); }); - it("trims once after all mention replacements", () => { - const mentions: Mentions = [{ key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }]; - expect(stripBotMention(" @_bot_1 hello ", mentions)).toBe("hello"); + it("falls back to @name when open_id is absent", () => { + const ctx = parseFeishuMessageEvent( + makeEvent("@_user_1 hi", [ + { key: "@_user_1", name: "Alice", id: { user_id: "uid_alice" } }, + ]) as any, + BOT_OPEN_ID, + ); + expect(ctx.content).toBe("@Alice hi"); }); - it("strips multiple mentions in one pass", () => { - const mentions: Mentions = [ - { key: "@_bot_1", name: "Bot One", id: { open_id: "ou_bot_1" } }, - { key: "@_bot_2", name: "Bot Two", id: { open_id: "ou_bot_2" } }, - ]; - expect(stripBotMention("@Bot One @_bot_1 hi @Bot Two @_bot_2", mentions)).toBe("hi"); + it("falls back to plain @name when no id is present", () => { + const ctx = parseFeishuMessageEvent( + makeEvent("@_unknown hey", [{ key: "@_unknown", name: "Nobody", id: {} }]) as any, + BOT_OPEN_ID, + ); + expect(ctx.content).toBe("@Nobody hey"); + }); + + it("treats mention key regex metacharacters as literal text", () => { + const ctx = parseFeishuMessageEvent( + makeEvent("hello world", [{ key: ".*", name: "Bot", id: { open_id: "ou_bot" } }]) as any, + BOT_OPEN_ID, + ); + expect(ctx.content).toBe("hello world"); + }); + + it("normalizes multiple mentions in one pass", () => { + const ctx = parseFeishuMessageEvent( + makeEvent("@_bot_1 hi @_user_2", [ + { key: "@_bot_1", name: "Bot One", id: { open_id: "ou_bot_1" } }, + { key: "@_user_2", name: "User Two", id: { open_id: "ou_user_2" } }, + ]) as any, + BOT_OPEN_ID, + ); + expect(ctx.content).toBe( + 'Bot One hi User Two', + ); + }); + + it("treats $ in display name as literal (no replacement-pattern interpolation)", () => { + const ctx = parseFeishuMessageEvent( + makeEvent("@_user_1 hi", [ + { key: "@_user_1", name: "$& the user", id: { open_id: "ou_x" } }, + ]) as any, + BOT_OPEN_ID, + ); + // $ is preserved literally (no $& pattern substitution); & is not escaped in tag body + expect(ctx.content).toBe('$& the user hi'); + }); + + it("escapes < and > in mention name to protect tag structure", () => { + const ctx = parseFeishuMessageEvent( + makeEvent("@_user_1 test", [ + { key: "@_user_1", name: "