From 9a491d74dd9de667458fa77bee4f48091d8209eb Mon Sep 17 00:00:00 2001 From: Adam Mendlik Date: Tue, 23 Jan 2024 07:55:56 -0700 Subject: [PATCH] Merge SSML and Markdown if present in either chained item --- .../lib/fulfillment-event/mergeNext.js | 83 ++++++++++++++----- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/lambda/es-proxy-layer/lib/fulfillment-event/mergeNext.js b/lambda/es-proxy-layer/lib/fulfillment-event/mergeNext.js index baa9b885..2062b491 100644 --- a/lambda/es-proxy-layer/lib/fulfillment-event/mergeNext.js +++ b/lambda/es-proxy-layer/lib/fulfillment-event/mergeNext.js @@ -14,35 +14,78 @@ const _ = require('lodash'); const qnabot = require('qnabot/logging'); +function mergeMarkdown(hit1, hit2) { + // get markdown from first item + const md1 = _.get(hit1, 'alt.markdown'); + let md1_to_merge = hit1.a + if (md1) { + md1_to_merge = md1 + } + qnabot.debug('markdown from first hit:', md1_to_merge) + + // get markdown from second item + const md2 = _.get(hit2, 'alt.markdown'); + let md2_to_merge = hit2.a + if (md2) { + md2_to_merge = md2 + } + qnabot.debug('markdown from second hit:', md2_to_merge) + + // merge markdown, if present in either item + if (md1 || md2) { + const md_merged = `${md1_to_merge}\n${md2_to_merge}` + qnabot.debug('Merged markdown:', md_merged) + _.set(hit2, 'alt.markdown', md_merged); + } else { + qnabot.debug('Markdown field missing from both items. Skipping markdown merge'); + } +} + +function mergeSSML(hit1, hit2) { + // get SSML from first item + const ssml1 = _.get(hit1, 'alt.ssml'); + let ssml1_to_merge = hit1.a + if (ssml1) { + // strip tags + qnabot.debug('SSML from first hit:', ssml1) + ssml1_to_merge = ssml1.replace(/|<\/speak>/g, ''); + } + + // get SSML from second item + const ssml2 = _.get(hit2, 'alt.ssml'); + let ssml2_to_merge = hit2.a + if (ssml2) { + // strip tags + qnabot.debug('SSML from second hit:', ssml2) + ssml2_to_merge = ssml2.replace(/|<\/speak>/g, ''); + } + + // merge SSML, if present in either item + if (ssml1 || ssml2) { + // concatenate, and re-wrap with tags + const ssml_merged = `${ssml1_to_merge} ${ssml2_to_merge}` + qnabot.debug('Merged SSML:', ssml_merged) + _.set(hit2, 'alt.ssml', ssml_merged); + } else { + qnabot.debug('SSML field missing from both items. Skipping SSML merge'); + } +} + function mergeNext(hit1, hit2) { if (hit1 === undefined) { return hit2; } qnabot.debug('Merge chained items'); + + // merge alternate answers + mergeMarkdown(hit1, hit2) + mergeSSML(hit1, hit2) + // merge plaintext answer if (hit1 && hit1.a) { hit2.a = hit1.a + ' ' + hit2.a; } - // merge markdown, if present in both items - const md1 = _.get(hit1, 'alt.markdown'); - const md2 = _.get(hit2, 'alt.markdown'); - if (md1 && md2) { - _.set(hit2, 'alt.markdown', `${md1}\n${md2}`); - } else { - qnabot.debug('Markdown field missing from one or both items; skip markdown merge'); - } - // merge SSML, if present in both items - let ssml1 = _.get(hit1, 'alt.ssml'); - let ssml2 = _.get(hit2, 'alt.ssml'); - if (ssml1 && ssml2) { - // strip tags - ssml1 = ssml1.replace(/|<\/speak>/g, ''); - ssml2 = ssml2.replace(/|<\/speak>/g, ''); - // concatenate, and re-wrap with tags - _.set(hit2, 'alt.ssml', `${ssml1} ${ssml2}`); - } else { - qnabot.debug('SSML field missing from one or both items; skip SSML merge'); - } + // build arrays of Lambda Hooks and arguments let lambdahooks = _.get(hit1, 'lambdahooks', []); // if hits1 doesn't have a lambdahooks field (no previous merge), then initialize using 'l' and 'args' from hit 1