Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 !== "";
},
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
13 changes: 13 additions & 0 deletions test/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Loading