openclaw/src/auto-reply/reply/inbound-text.ts
zerone0x 861be9c936 fix(whatsapp): normalize literal \n escape sequences in outbound messages
Some LLM providers output literal \n characters (backslash-n) instead of actual
newlines. This causes WhatsApp to display the escape sequences as text rather
than rendering line breaks.

Add normalizeOutboundTextNewlines() to convert literal \n, \r\n, and \r escape
sequences to actual newlines before sending messages via the WhatsApp adapter.

Fixes #3082

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-28 14:09:59 +08:00

18 lines
802 B
TypeScript

export function normalizeInboundTextNewlines(input: string): string {
return input.replaceAll("\r\n", "\n").replaceAll("\r", "\n").replaceAll("\\n", "\n");
}
/**
* Normalize outbound text newlines before sending to channels.
* Converts literal `\n` escape sequences to actual newlines.
* Some LLM providers may output literal `\n` in their responses,
* which causes WhatsApp (and potentially other channels) to display
* the escape sequence as text instead of rendering a line break.
*/
export function normalizeOutboundTextNewlines(input: string): string {
if (!input) return input;
// Convert literal \n (backslash-n) sequences to actual newlines.
// Also handle \r\n and \r for consistency.
return input.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n").replaceAll("\\n", "\n");
}