From e4b6f4ff0d49416e8dd70c7b6239576d15068407 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Mon, 27 May 2024 09:23:53 -0300 Subject: [PATCH] correction in message formatting when generated by AI as markdown in typebot --- CHANGELOG.md | 3 ++ .../typebot/services/typebot.service.ts | 29 ++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4f2197c7..35cab59a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ * Now in the manager, when logging in with the client's apikey, the listing only shows the instance corresponding to the provided apikey (only with MongoDB) * New global mode for rabbitmq events +### Fixed +* Correction in message formatting when generated by AI as markdown in typebot + # 1.7.5 (2024-05-21 08:50) ### Fixed diff --git a/src/api/integrations/typebot/services/typebot.service.ts b/src/api/integrations/typebot/services/typebot.service.ts index eae67117d..a79f9d3df 100644 --- a/src/api/integrations/typebot/services/typebot.service.ts +++ b/src/api/integrations/typebot/services/typebot.service.ts @@ -519,18 +519,32 @@ export class TypebotService { text += element.text; } - if ( - element.children && - (element.type === 'p' || - element.type === 'a' || - element.type === 'inline-variable' || - element.type === 'variable') - ) { + if (element.children && element.type !== 'a') { for (const child of element.children) { text += applyFormatting(child); } } + if (element.type === 'p') { + text = text.trim() + '\n'; + } + + if (element.type === 'ol') { + text = + '\n' + + text + .split('\n') + .map((line, index) => (line ? `${index + 1}. ${line}` : '')) + .join('\n'); + } + + if (element.type === 'li') { + text = text + .split('\n') + .map((line) => (line ? ` ${line}` : '')) + .join('\n'); + } + let formats = ''; if (element.bold) { @@ -558,6 +572,7 @@ export class TypebotService { for (const message of messages) { if (message.type === 'text') { let formattedText = ''; + console.log('message.content', message.content); for (const richText of message.content.richText) { for (const element of richText.children) {