mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 10:42:43 +00:00
perf(runtime): trim hot-path allocations and cache channel plugin lookups
This commit is contained in:
@@ -3,6 +3,31 @@ import { escapeRegExp } from "../utils.js";
|
||||
export const HEARTBEAT_TOKEN = "HEARTBEAT_OK";
|
||||
export const SILENT_REPLY_TOKEN = "NO_REPLY";
|
||||
|
||||
const silentExactRegexByToken = new Map<string, RegExp>();
|
||||
const silentTrailingRegexByToken = new Map<string, RegExp>();
|
||||
|
||||
function getSilentExactRegex(token: string): RegExp {
|
||||
const cached = silentExactRegexByToken.get(token);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const escaped = escapeRegExp(token);
|
||||
const regex = new RegExp(`^\\s*${escaped}\\s*$`);
|
||||
silentExactRegexByToken.set(token, regex);
|
||||
return regex;
|
||||
}
|
||||
|
||||
function getSilentTrailingRegex(token: string): RegExp {
|
||||
const cached = silentTrailingRegexByToken.get(token);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const escaped = escapeRegExp(token);
|
||||
const regex = new RegExp(`(?:^|\\s+|\\*+)${escaped}\\s*$`);
|
||||
silentTrailingRegexByToken.set(token, regex);
|
||||
return regex;
|
||||
}
|
||||
|
||||
export function isSilentReplyText(
|
||||
text: string | undefined,
|
||||
token: string = SILENT_REPLY_TOKEN,
|
||||
@@ -10,11 +35,9 @@ export function isSilentReplyText(
|
||||
if (!text) {
|
||||
return false;
|
||||
}
|
||||
const escaped = escapeRegExp(token);
|
||||
// Match only the exact silent token with optional surrounding whitespace.
|
||||
// This prevents
|
||||
// substantive replies ending with NO_REPLY from being suppressed (#19537).
|
||||
return new RegExp(`^\\s*${escaped}\\s*$`).test(text);
|
||||
// This prevents substantive replies ending with NO_REPLY from being suppressed (#19537).
|
||||
return getSilentExactRegex(token).test(text);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,8 +46,7 @@ export function isSilentReplyText(
|
||||
* If the result is empty, the entire message should be treated as silent.
|
||||
*/
|
||||
export function stripSilentToken(text: string, token: string = SILENT_REPLY_TOKEN): string {
|
||||
const escaped = escapeRegExp(token);
|
||||
return text.replace(new RegExp(`(?:^|\\s+|\\*+)${escaped}\\s*$`), "").trim();
|
||||
return text.replace(getSilentTrailingRegex(token), "").trim();
|
||||
}
|
||||
|
||||
export function isSilentReplyPrefixText(
|
||||
|
||||
Reference in New Issue
Block a user