diff --git a/src/matcher.ts b/src/matcher.ts index 8ef0849..20dabe3 100644 --- a/src/matcher.ts +++ b/src/matcher.ts @@ -12,7 +12,7 @@ import { AdaptiveCard, } from "./messages"; import { IWebchatConfig } from "./messages/types"; -import { getChannelPayload } from "./utils"; +import { getChannelPayload, isOnlyEscapeSequence } from "./utils"; import { IMessage, IWebchatTemplateAttachment } from "@cognigy/socket-client"; import { IAdaptiveCardMessage } from "@cognigy/socket-client/lib/interfaces/messageData"; import { XAppSubmitMessage } from "./messages/xApp"; @@ -183,13 +183,14 @@ const defaultConfig: MatchConfig[] = [ if (Array.isArray(message?.text)) { return message?.text.length > 0; } - // Handle messages from LLMs if it only contains any escape sequences and markdown is disabled + + // Handle messages from LLMs if it only contains any escape sequences and collation is disabled if ( - message.text?.match?.(/^(?:[\n\t\r\f\b\v\s])+$/)?.length && - !config?.settings.behavior.renderMarkdown && - !config?.settings.behavior.collateStreamedOutputs - ) + isOnlyEscapeSequence(message.text) && + !config?.settings?.behavior?.collateStreamedOutputs + ) { return false; + } return message?.text !== null && message?.text !== undefined && message?.text !== ""; }, diff --git a/src/utils.ts b/src/utils.ts index 5baf340..b775306 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -241,3 +241,14 @@ export const moveFocusToMessageFocusTarget = (dataMessageId: string) => { } }, 0); }; + +const ESCAPE_SEQUENCE_REGEX = /^(?:[\n\t\r\f\b\v\s])+$/u; + +/** + * Checks if a string consists only of whitespace or escape sequences. + * @param text The string to check. + * @returns True if the string contains only whitespace or escape sequences; otherwise, false. + */ +export function isOnlyEscapeSequence(text: unknown): boolean { + return typeof text === "string" && ESCAPE_SEQUENCE_REGEX.test(text); +} diff --git a/test/demo.tsx b/test/demo.tsx index f5dc497..1c96fed 100644 --- a/test/demo.tsx +++ b/test/demo.tsx @@ -482,6 +482,19 @@ const screens: TScreen[] = [ text: 0, }, }, + { + message: { + text: " \n\n", + source: "bot", + }, + config: { + settings: { + behavior: { + renderMarkdown: true, + }, + }, + }, + }, { // Data-only message should not render anything message: {