Files
openclaw/src/gateway/control-reply-text.ts
Pinghuachiu 68630a9e6d fix(gateway): suppress announce/reply skip chat leakage (#51739)
Merged via squash.

Prepared head SHA: 2f53f3b0b7
Co-authored-by: Pinghuachiu <9033138+Pinghuachiu@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
2026-04-08 15:18:57 -07:00

63 lines
1.7 KiB
TypeScript

import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
const SUPPRESSED_CONTROL_REPLY_TOKENS = [
SILENT_REPLY_TOKEN,
"ANNOUNCE_SKIP",
"REPLY_SKIP",
] as const;
const MIN_BARE_PREFIX_LENGTH_BY_TOKEN: Readonly<
Record<(typeof SUPPRESSED_CONTROL_REPLY_TOKENS)[number], number>
> = {
[SILENT_REPLY_TOKEN]: 2,
ANNOUNCE_SKIP: 3,
REPLY_SKIP: 3,
};
function normalizeSuppressedControlReplyFragment(text: string): string {
const trimmed = text.trim();
if (!trimmed) {
return "";
}
const normalized = trimmed.toUpperCase();
if (/[^A-Z_]/.test(normalized)) {
return "";
}
return normalized;
}
/**
* Return true when a chat-visible reply is exactly an internal control token.
*/
export function isSuppressedControlReplyText(text: string): boolean {
const normalized = text.trim();
return SUPPRESSED_CONTROL_REPLY_TOKENS.some((token) => isSilentReplyText(normalized, token));
}
/**
* Return true when streamed assistant text looks like the leading fragment of a control token.
*/
export function isSuppressedControlReplyLeadFragment(text: string): boolean {
const trimmed = text.trim();
const normalized = normalizeSuppressedControlReplyFragment(text);
if (!normalized) {
return false;
}
return SUPPRESSED_CONTROL_REPLY_TOKENS.some((token) => {
const tokenUpper = token.toUpperCase();
if (normalized === tokenUpper) {
return false;
}
if (!tokenUpper.startsWith(normalized)) {
return false;
}
if (normalized.includes("_")) {
return true;
}
if (token !== SILENT_REPLY_TOKEN && trimmed !== trimmed.toUpperCase()) {
return false;
}
return normalized.length >= MIN_BARE_PREFIX_LENGTH_BY_TOKEN[token];
});
}