From 02e2e423226ec9643c83b371c78e3e468e044737 Mon Sep 17 00:00:00 2001 From: nzambello Date: Wed, 11 Sep 2024 12:57:28 +0300 Subject: [PATCH] refactor: hide output tags in chat, strip them for TTS --- src/components/ChatBubble/ChatBubble.css | 11 ++++++---- .../ChatBubble/ChatBubble.stories.tsx | 13 ++++++++++++ src/components/MemoriWidget/MemoriWidget.tsx | 3 ++- src/helpers/utils.test.ts | 21 ++++++++++++++++++- src/helpers/utils.ts | 12 +++++++++++ 5 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/components/ChatBubble/ChatBubble.css b/src/components/ChatBubble/ChatBubble.css index eb048a66..c1771a78 100644 --- a/src/components/ChatBubble/ChatBubble.css +++ b/src/components/ChatBubble/ChatBubble.css @@ -101,7 +101,7 @@ } } -.memori-chat--bubble p+p { +.memori-chat--bubble p + p { margin-top: 0.5em; } @@ -266,7 +266,6 @@ } @keyframes wave { - 0%, 60%, 100% { @@ -282,6 +281,10 @@ width: 100%; } -.memori-chat--bubble mjx-container[jax='CHTML'][display='true'] { +.memori-chat--bubble output { + display: none; +} + +.dot mjx-container[jax='CHTML'][display='true'] { text-align: left; -} \ No newline at end of file +} diff --git a/src/components/ChatBubble/ChatBubble.stories.tsx b/src/components/ChatBubble/ChatBubble.stories.tsx index b498ce5d..f83694e6 100644 --- a/src/components/ChatBubble/ChatBubble.stories.tsx +++ b/src/components/ChatBubble/ChatBubble.stories.tsx @@ -298,6 +298,19 @@ WithMarkdownCode.args = { }, }; +export const WithOutputCode = Template.bind({}); +WithOutputCode.args = { + memori, + apiUrl: 'https://backend.memori.ai', + tenant, + message: { + fromUser: false, + text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\n```markdown\n# titolo\n```\n\nSed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n{ "data": [ 0, 1, 2 ] }', + initial: false, + generatedByAI: true, + }, +}; + export const WithMarkdownTable = Template.bind({}); WithMarkdownTable.args = { memori, diff --git a/src/components/MemoriWidget/MemoriWidget.tsx b/src/components/MemoriWidget/MemoriWidget.tsx index d9da6348..b86108e4 100644 --- a/src/components/MemoriWidget/MemoriWidget.tsx +++ b/src/components/MemoriWidget/MemoriWidget.tsx @@ -72,6 +72,7 @@ import { stripEmojis, escapeHTML, stripMarkdown, + stripOutputTags, } from '../../helpers/utils'; import { anonTag } from '../../helpers/constants'; import { getErrori18nKey } from '../../helpers/error'; @@ -1919,7 +1920,7 @@ const MemoriWidget = ({ `${replaceTextWithPhonemes( - escapeHTML(stripMarkdown(stripEmojis(text))), + escapeHTML(stripOutputTags(stripMarkdown(stripEmojis(text)))), userLang.toLowerCase() )}`, result => { diff --git a/src/helpers/utils.test.ts b/src/helpers/utils.test.ts index e9fcc210..b054ef62 100644 --- a/src/helpers/utils.test.ts +++ b/src/helpers/utils.test.ts @@ -1,4 +1,9 @@ -import { difference, stripEmojis, stripMarkdown } from './utils'; +import { + difference, + stripEmojis, + stripMarkdown, + stripOutputTags, +} from './utils'; describe('Utils/difference', () => { it('should return the difference between two objects with numeric values', () => { @@ -120,3 +125,17 @@ describe('utils/stripMarkdown', () => { expect(result).toEqual(''); }); }); + +describe('utils/stripOutputTags', () => { + it('should remove output tag', () => { + const result = stripOutputTags('test\npippo'); + expect(result).toEqual('test\n'); + }); + + it('should remove output tag with class', () => { + const result = stripOutputTags( + 'test\npippo' + ); + expect(result).toEqual('test\n'); + }); +}); diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index f587cc6c..8ffc2e6b 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -180,6 +180,18 @@ export const stripMarkdown = (text: string) => { return text; }; +export const stripOutputTags = (text: string): string => { + let hasTags = text.includes(''); + + if (!hasTags) return text; + + let output = text.split(''); + let textBefore = output[0].split(' { const el = document.createElement('textarea'); el.textContent = text;