Files
openclaw/src/auto-reply/tokens.ts
Peter Steinberger a6a742f3d0 fix(auto-reply): land #31080 from @scoootscooob
Co-authored-by: scoootscooob <zhentongfan@gmail.com>
2026-03-02 01:17:42 +00:00

49 lines
1.4 KiB
TypeScript

import { escapeRegExp } from "../utils.js";
export const HEARTBEAT_TOKEN = "HEARTBEAT_OK";
export const SILENT_REPLY_TOKEN = "NO_REPLY";
export function isSilentReplyText(
text: string | undefined,
token: string = SILENT_REPLY_TOKEN,
): boolean {
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);
}
/**
* Strip a trailing silent reply token from mixed-content text.
* Returns the remaining text with the token removed (trimmed).
* 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();
}
export function isSilentReplyPrefixText(
text: string | undefined,
token: string = SILENT_REPLY_TOKEN,
): boolean {
if (!text) {
return false;
}
const normalized = text.trimStart().toUpperCase();
if (!normalized) {
return false;
}
if (!normalized.includes("_")) {
return false;
}
if (/[^A-Z_]/.test(normalized)) {
return false;
}
return token.toUpperCase().startsWith(normalized);
}