11// Qqbot plugin module implements mention behavior.
2+ import {
3+ compileSafeRegexDetailed ,
4+ type SafeRegexRejectReason ,
5+ } from "openclaw/plugin-sdk/security-runtime" ;
6+ import { debugWarn } from "../utils/log.js" ;
27export interface RawMention {
38 is_you ?: boolean ;
49 bot ?: boolean ;
@@ -23,6 +28,60 @@ interface HasAnyMentionInput {
2328}
2429
2530const MENTION_TAG_RE = / < @ ! ? \w + > / ;
31+ const MENTION_PATTERN_FLAGS = "i" ;
32+ const MAX_MENTION_PATTERN_CACHE_KEYS = 256 ;
33+ const MAX_MENTION_PATTERN_WARNING_KEYS = 256 ;
34+ const mentionPatternCompileCache = new Map < string , RegExp [ ] > ( ) ;
35+ const rejectedMentionPatternWarningCache = new Set < string > ( ) ;
36+
37+ type MentionPatternRejectReason = Exclude < SafeRegexRejectReason , "empty" > ;
38+
39+ function warnRejectedMentionPattern ( pattern : string , reason : MentionPatternRejectReason ) : void {
40+ const key = `${ MENTION_PATTERN_FLAGS } ::${ reason } ::${ pattern } ` ;
41+ if ( rejectedMentionPatternWarningCache . has ( key ) ) {
42+ return ;
43+ }
44+ rejectedMentionPatternWarningCache . add ( key ) ;
45+ if ( rejectedMentionPatternWarningCache . size > MAX_MENTION_PATTERN_WARNING_KEYS ) {
46+ rejectedMentionPatternWarningCache . clear ( ) ;
47+ rejectedMentionPatternWarningCache . add ( key ) ;
48+ }
49+ debugWarn ( `qqbot: mentionPattern rejected (${ reason } ): ${ pattern } ` ) ;
50+ }
51+
52+ function cacheMentionPatterns ( cacheKey : string , regexes : RegExp [ ] ) : RegExp [ ] {
53+ mentionPatternCompileCache . set ( cacheKey , regexes ) ;
54+ if ( mentionPatternCompileCache . size > MAX_MENTION_PATTERN_CACHE_KEYS ) {
55+ mentionPatternCompileCache . clear ( ) ;
56+ mentionPatternCompileCache . set ( cacheKey , regexes ) ;
57+ }
58+ return regexes ;
59+ }
60+
61+ function compileMentionPatterns ( patterns : string [ ] ) : RegExp [ ] {
62+ if ( patterns . length === 0 ) {
63+ return [ ] ;
64+ }
65+ const cacheKey = patterns . join ( "\u001f" ) ;
66+ const cached = mentionPatternCompileCache . get ( cacheKey ) ;
67+ if ( cached ) {
68+ return cached ;
69+ }
70+
71+ const regexes : RegExp [ ] = [ ] ;
72+ for ( const pattern of patterns ) {
73+ const result = compileSafeRegexDetailed ( pattern , MENTION_PATTERN_FLAGS ) ;
74+ if ( result . reason === "empty" ) {
75+ continue ;
76+ }
77+ if ( result . regex ) {
78+ regexes . push ( result . regex ) ;
79+ continue ;
80+ }
81+ warnRejectedMentionPattern ( result . source , result . reason ) ;
82+ }
83+ return cacheMentionPatterns ( cacheKey , regexes ) ;
84+ }
2685
2786export function detectWasMentioned ( input : DetectWasMentionedInput ) : boolean {
2887 const { eventType, mentions, content, mentionPatterns } = input ;
@@ -36,15 +95,10 @@ export function detectWasMentioned(input: DetectWasMentionedInput): boolean {
3695 }
3796
3897 if ( mentionPatterns ?. length && content ) {
39- for ( const pattern of mentionPatterns ) {
40- if ( ! pattern ) {
41- continue ;
98+ for ( const regex of compileMentionPatterns ( mentionPatterns ) ) {
99+ if ( regex . test ( content ) ) {
100+ return true ;
42101 }
43- try {
44- if ( new RegExp ( pattern , "i" ) . test ( content ) ) {
45- return true ;
46- }
47- } catch { }
48102 }
49103 }
50104
0 commit comments