From 8749bdf000ef3f0a45167d02d0c1c991c8c05e25 Mon Sep 17 00:00:00 2001 From: Flavien David Date: Tue, 18 Jun 2024 08:42:22 +0200 Subject: [PATCH] Safer streaming chunk parsing in Slack (#5693) * Safer streaming chunk parsing in Slack * :book: --- .../slack/chat/stream_conversation_handler.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/connectors/src/connectors/slack/chat/stream_conversation_handler.ts b/connectors/src/connectors/slack/chat/stream_conversation_handler.ts index cc2552cf4252..c33edeb9ebd0 100644 --- a/connectors/src/connectors/slack/chat/stream_conversation_handler.ts +++ b/connectors/src/connectors/slack/chat/stream_conversation_handler.ts @@ -125,9 +125,11 @@ export async function streamConversationToSlack( } lastSentDate = new Date(); - const finalAnswer = slackifyMarkdown( - normalizeContentForSlack(annotateCitations(fullAnswer, action)) - ); + const finalAnswer = safelyPrepareAnswer(fullAnswer, action); + // If the answer cannot be prepared safely, skip processing these tokens. + if (!finalAnswer) { + break; + } // if the message is too long, we avoid the update entirely (to reduce // rate limiting) the previous update will have shown the "..." and the @@ -174,6 +176,26 @@ export async function streamConversationToSlack( return new Err(new Error("Failed to get the final answer from Dust")); } +/** + * Safely prepare the answer by normalizing the content for Slack and converting it to Markdown. + * In streaming mode, partial links might trigger errors in the `slackifyMarkdown` function. + * This function handles such errors gracefully, ensuring that the full text will be displayed + * once a valid URL is available. + */ +function safelyPrepareAnswer( + text: string, + action: AgentActionType | null +): string | null { + const rawAnswer = normalizeContentForSlack(annotateCitations(text, action)); + + try { + return slackifyMarkdown(rawAnswer); + } catch (err) { + // It's safe to swallow the error as we'll catch up once a valid URL is fully received. + return null; + } +} + function normalizeContentForSlack(content: string): string { // Remove language hint from code blocks. return content.replace(/```[a-z\-_]*\n/g, "```\n");