3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-05 18:27:18 +00:00

fix: improve loose matching regex to reduce false positives in matchWords trigger

This commit is contained in:
seeyebe 2025-06-01 20:32:35 +03:00
parent f5806932af
commit 375cab7cbd

View file

@ -31,7 +31,6 @@ const configSchema = z.strictObject({
export const MatchWordsTrigger = automodTrigger<MatchResultType>()({
configSchema,
async match({ pluginData, context, triggerConfig: trigger }) {
if (!context.message) {
return;
@ -39,21 +38,37 @@ export const MatchWordsTrigger = automodTrigger<MatchResultType>()({
if (!regexCache.has(trigger)) {
const looseMatchingThreshold = Math.min(Math.max(trigger.loose_matching_threshold, 1), 64);
const patterns = trigger.words.map((word) => {
let pattern = trigger.loose_matching
? [...word].map((c) => escapeStringRegexp(c)).join(`(?:\\s*|.{0,${looseMatchingThreshold}})`)
: escapeStringRegexp(word);
let pattern;
if (trigger.loose_matching) {
pattern = [...word]
.map((c) => escapeStringRegexp(c))
.join(`[\\s\\-_.,!?]{0,${looseMatchingThreshold}}`);
} else {
pattern = escapeStringRegexp(word);
}
if (trigger.only_full_words) {
pattern = `\\b${pattern}\\b`;
if (trigger.loose_matching) {
pattern = `\\b(?:${pattern})\\b`;
} else {
pattern = `\\b${pattern}\\b`;
}
}
return pattern;
});
const mergedRegex = new RegExp(patterns.map((p) => `(?:${p})`).join("|"), trigger.case_sensitive ? "" : "i");
const mergedRegex = new RegExp(
patterns.map((p) => `(${p})`).join("|"),
trigger.case_sensitive ? "" : "i"
);
regexCache.set(trigger, [mergedRegex]);
}
const regexes = regexCache.get(trigger)!;
for await (let [type, str] of matchMultipleTextTypesOnMessage(pluginData, trigger, context.message)) {
@ -66,11 +81,14 @@ export const MatchWordsTrigger = automodTrigger<MatchResultType>()({
}
for (const regex of regexes) {
if (regex.test(str)) {
const match = regex.exec(str);
if (match) {
const matchedWord = match.slice(1).find(group => group !== undefined) || "";
return {
extra: {
type,
word: "",
word: matchedWord,
},
};
}
@ -82,6 +100,7 @@ export const MatchWordsTrigger = automodTrigger<MatchResultType>()({
renderMatchInformation({ pluginData, contexts, matchResult }) {
const partialSummary = getTextMatchPartialSummary(pluginData, matchResult.extra.type, contexts[0]);
return `Matched word in ${partialSummary}`;
const wordInfo = matchResult.extra.word ? ` (matched: "${matchResult.extra.word}")` : "";
return `Matched word in ${partialSummary}${wordInfo}`;
},
});
});