Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve merging of chained items #720

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
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
85 changes: 64 additions & 21 deletions lambda/es-proxy-layer/lib/fulfillment-event/mergeNext.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,78 @@
const _ = require('lodash');
const qnabot = require('qnabot/logging');

function mergeNext(hit1, hit2) {
if (hit1 === undefined) {
return hit2;
}
qnabot.debug('Merge chained items');
// merge plaintext answer
if (hit1 && hit1.a) {
hit2.a = hit1.a + hit2.a;
}
// merge markdown, if present in both items
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');
if (md1 && md2) {
_.set(hit2, 'alt.markdown', `${md1}\n${md2}`);
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 one or both items; skip markdown merge');
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 <speak> tags
qnabot.debug('SSML from first hit:', ssml1)
ssml1_to_merge = ssml1.replace(/<speak>|<\/speak>/g, '');
}
// merge SSML, if present in both items
let ssml1 = _.get(hit1, 'alt.ssml');
let ssml2 = _.get(hit2, 'alt.ssml');
if (ssml1 && ssml2) {

// get SSML from second item
const ssml2 = _.get(hit2, 'alt.ssml');
let ssml2_to_merge = hit2.a
if (ssml2) {
// strip <speak> tags
ssml1 = ssml1.replace(/<speak>|<\/speak>/g, '');
ssml2 = ssml2.replace(/<speak>|<\/speak>/g, '');
qnabot.debug('SSML from second hit:', ssml2)
ssml2_to_merge = ssml2.replace(/<speak>|<\/speak>/g, '');
}

// merge SSML, if present in either item
if (ssml1 || ssml2) {
// concatenate, and re-wrap with <speak> tags
_.set(hit2, 'alt.ssml', `<speak>${ssml1} ${ssml2}</speak>`);
const ssml_merged = `<speak>${ssml1_to_merge} ${ssml2_to_merge}</speak>`
qnabot.debug('Merged SSML:', ssml_merged)
_.set(hit2, 'alt.ssml', ssml_merged);
} else {
qnabot.debug('SSML field missing from one or both items; skip SSML merge');
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;
}

// 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
Expand Down
14 changes: 7 additions & 7 deletions lambda/fulfillment/lib/middleware/lexRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ async function handleRequest(req, res, botName, botAlias) {
const lexv2response = await lexV2ClientRequester(params);
qnabot.log(`Lex V2 response: ${JSON.stringify(lexv2response)}`);
response.message = _.get(lexv2response, 'messages[0].content', '');
response.messages = {}
for (const message of _.get(lexv2response, 'messages', [])) {
response.messages[message.contentType] = message.content
}

// lex v2 FallbackIntent match means it failed to fill desired slot(s).
if (lexv2response.sessionState.intent.name === 'FallbackIntent'
|| lexv2response.sessionState.intent.state === 'Failed') {
Expand Down Expand Up @@ -277,13 +282,8 @@ async function processResponse(req, res, hook, msg) {

const botResp = await handleRequest(req, res, hook, 'live');
qnabot.log(`botResp: ${JSON.stringify(botResp, null, 2)}`);
let plainMessage = botResp.message;
let ssmlMessage;
// if messsage contains SSML tags, strip tags for plain text, but preserve tags for SSML
if (plainMessage?.includes('<speak>')) {
ssmlMessage = botResp.message;
plainMessage = plainMessage.replace(/<\/?[^>]+(>|$)/g, ''); // NOSONAR - javascript:S5852 - input is user controlled and we have a limit on the number of characters
}
let plainMessage = _.get(botResp, 'messages.PlainText', '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to confirm that the messages.Plaintext and messages.ssml do return the plaintext and ssml message separately now

let ssmlMessage = _.get(botResp, 'messages.SSML', '');
let elicitResponseLoopCount = _.get(res, 'session.qnabotcontext.elicitResponse.loopCount', 0);

switch (botResp.dialogState) {
Expand Down
40 changes: 20 additions & 20 deletions templates/examples/examples/responsebots-lexv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -245,7 +245,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -363,7 +363,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -468,7 +468,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -598,7 +598,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -743,7 +743,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -854,7 +854,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -953,7 +953,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -1120,7 +1120,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -1312,7 +1312,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -1491,7 +1491,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -1598,7 +1598,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -1692,7 +1692,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -1807,7 +1807,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -1909,7 +1909,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -2016,7 +2016,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -2110,7 +2110,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -2218,7 +2218,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -2324,7 +2324,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down Expand Up @@ -2435,7 +2435,7 @@ exports.resources = {
IntentClosingSetting: {
ClosingResponse: {
MessageGroupsList: [{
Message: { PlainTextMessage: { Value: 'OK. ' } },
Message: { PlainTextMessage: { Value: 'OK.' } },
}],
},
},
Expand Down