mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-07-05 18:27:18 +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:
commit
0f6bbd52df
1 changed files with 29 additions and 10 deletions
|
@ -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}`;
|
||||
},
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue