3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-06 10:37:19 +00:00

Merge pull request #520 from seeyebe/fix/loose-matching-false-positives

Fix loose matching false positives in MatchWordsTrigger
This commit is contained in:
Miikka 2025-06-01 23:17:32 +03:00 committed by GitHub
commit 0f6bbd52df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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