mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(feishu): dedupe mention regex escaping
This commit is contained in:
38
extensions/feishu/src/bot.stripBotMention.test.ts
Normal file
38
extensions/feishu/src/bot.stripBotMention.test.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { stripBotMention, type FeishuMessageEvent } from "./bot.js";
|
||||
|
||||
type Mentions = FeishuMessageEvent["message"]["mentions"];
|
||||
|
||||
describe("stripBotMention", () => {
|
||||
it("returns original text when mentions are missing", () => {
|
||||
expect(stripBotMention("hello world", undefined)).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("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("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("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("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");
|
||||
});
|
||||
});
|
||||
@@ -7,13 +7,20 @@ import {
|
||||
DEFAULT_GROUP_HISTORY_LIMIT,
|
||||
type HistoryEntry,
|
||||
} from "openclaw/plugin-sdk";
|
||||
import type { FeishuMessageContext, FeishuMediaInfo, ResolvedFeishuAccount } from "./types.js";
|
||||
import type { DynamicAgentCreationConfig } from "./types.js";
|
||||
import { resolveFeishuAccount } from "./accounts.js";
|
||||
import { createFeishuClient } from "./client.js";
|
||||
import { tryRecordMessage } from "./dedup.js";
|
||||
import { maybeCreateDynamicAgent } from "./dynamic-agent.js";
|
||||
import { normalizeFeishuExternalKey } from "./external-keys.js";
|
||||
import { downloadMessageResourceFeishu } from "./media.js";
|
||||
import { extractMentionTargets, extractMessageBody, isMentionForwardRequest } from "./mention.js";
|
||||
import {
|
||||
escapeRegExp,
|
||||
extractMentionTargets,
|
||||
extractMessageBody,
|
||||
isMentionForwardRequest,
|
||||
} from "./mention.js";
|
||||
import {
|
||||
resolveFeishuGroupConfig,
|
||||
resolveFeishuReplyPolicy,
|
||||
@@ -23,8 +30,6 @@ import {
|
||||
import { createFeishuReplyDispatcher } from "./reply-dispatcher.js";
|
||||
import { getFeishuRuntime } from "./runtime.js";
|
||||
import { getMessageFeishu, sendMessageFeishu } from "./send.js";
|
||||
import type { FeishuMessageContext, FeishuMediaInfo, ResolvedFeishuAccount } from "./types.js";
|
||||
import type { DynamicAgentCreationConfig } from "./types.js";
|
||||
|
||||
// --- Permission error extraction ---
|
||||
// Extract permission grant URL from Feishu API error response.
|
||||
@@ -199,21 +204,17 @@ function checkBotMentioned(event: FeishuMessageEvent, botOpenId?: string): boole
|
||||
return false;
|
||||
}
|
||||
|
||||
function escapeRegExp(s: string): string {
|
||||
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
function stripBotMention(
|
||||
export function stripBotMention(
|
||||
text: string,
|
||||
mentions?: FeishuMessageEvent["message"]["mentions"],
|
||||
): string {
|
||||
if (!mentions || mentions.length === 0) return text;
|
||||
let result = text;
|
||||
for (const mention of mentions) {
|
||||
result = result.replace(new RegExp(`@${escapeRegExp(mention.name)}\\s*`, "g"), "").trim();
|
||||
result = result.replace(new RegExp(escapeRegExp(mention.key), "g"), "").trim();
|
||||
result = result.replace(new RegExp(`@${escapeRegExp(mention.name)}\\s*`, "g"), "");
|
||||
result = result.replace(new RegExp(escapeRegExp(mention.key), "g"), "");
|
||||
}
|
||||
return result;
|
||||
return result.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import type { FeishuMessageEvent } from "./bot.js";
|
||||
|
||||
/**
|
||||
* Escape regex metacharacters so user-controlled mention fields are treated literally.
|
||||
*/
|
||||
export function escapeRegExp(input: string): string {
|
||||
return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
/**
|
||||
* Mention target user info
|
||||
*/
|
||||
@@ -67,7 +74,7 @@ export function extractMessageBody(text: string, allMentionKeys: string[]): stri
|
||||
|
||||
// Remove all @ placeholders
|
||||
for (const key of allMentionKeys) {
|
||||
result = result.replace(new RegExp(key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"), "");
|
||||
result = result.replace(new RegExp(escapeRegExp(key), "g"), "");
|
||||
}
|
||||
|
||||
return result.replace(/\s+/g, " ").trim();
|
||||
|
||||
Reference in New Issue
Block a user