async function evaluateConditionalChaining()

in lambda/es-proxy-layer/lib/query.js [300:374]


async function evaluateConditionalChaining(req, res, hit, conditionalChaining) {
    qnabot.log("evaluateConditionalChaining req: ", JSON.stringify(req, null, 2));
    qnabot.log("evaluateConditionalChaining res: ", JSON.stringify(res, null, 2));
    qnabot.log("evaluateConditionalChaining hit: ", JSON.stringify(hit, null, 2));
    // decrypt conditionalChaining
    conditionalChaining = encryptor.decrypt(conditionalChaining);
    qnabot.log("Decrypted Chained document rule specified:", conditionalChaining);
    var next_q;
    // If chaining rule a lambda, or an expression?
    if (conditionalChaining.toLowerCase().startsWith("lambda::")) {
        // Chaining rule is a Lambda function
        var lambdaName = conditionalChaining.split("::")[1] ;
        var payload;
        [req, res, payload] = await invokeLambda (lambdaName, req, res);
        qnabot.log("Chaining Rule Lambda response payload: ", payload);
        try {
            payload = JSON.parse(payload);
        } catch (e) {
            // response is not JSON
        }
        if (_.get(payload,"req") && _.get(payload,"res")) {
            next_q = _.get(payload,"req.question");
        }
        else {
            qnabot.log("Chaining Rules Lambda did not return session event in response.");
            qnabot.log("assume response is a simple string containing next_q value");
            next_q = payload ;
        }
    } else {
        // create chaining rule safeEval context, aligned with Handlebars context
        const SessionAttributes = (arg) => _.get(SessionAttributes, arg, undefined);
        _.assign(SessionAttributes, res.session);
        const context={
            LexOrAlexa: req._type,
            UserInfo:req._userInfo, 
            SessionAttributes,
            Settings: req._settings,
            Question: req.question,
            OrigQuestion: _.get(req,"_event.origQuestion",req.question),
            PreviousQuestion: _.get(req, "session.qnabotcontext.previous.q", false),
            Sentiment: req.sentiment,
        };
        qnabot.log("Evaluating:", conditionalChaining);
        // safely evaluate conditionalChaining expression.. throws an exception if there is a syntax error
        next_q = safeEval(conditionalChaining, context);
    }
    qnabot.log("Chained document rule evaluated to:", next_q);
    req.question = next_q;
    var hit2;
    [req, res, hit2] = await get_hit(req, res);
    // if the question we are chaining to, also has conditional chaining, be sure to navigate set up
    // next user input to elicitResponse from this lex Bot.
    if (hit2) {
        const responsebot_hook = _.get(hit2, "elicitResponse.responsebot_hook", undefined);
        const responsebot_session_namespace = _.get(hit2, "elicitResponse.response_sessionattr_namespace", undefined);
        const chaining_configuration = _.get(hit2, "conditionalChaining", undefined);
        var elicitResponse = {} ;
        if (responsebot_hook && responsebot_session_namespace) {
            elicitResponse.responsebot = responsebot_hook;
            elicitResponse.namespace = responsebot_session_namespace;
            elicitResponse.chainingConfig = chaining_configuration;
            _.set(res.session, res.session.elicitResponseNamespace + ".boterror", undefined );
        } else {
            elicitResponse.responsebot = undefined;
            elicitResponse.namespace = undefined;
            elicitResponse.chainingConfig = chaining_configuration;
        }
        _.set(res.session,'qnabotcontext.elicitResponse',elicitResponse);
        var mergedhit = merge_next(hit, hit2);
        return [req, res, mergedhit] ;
    } else {
        qnabot.log("WARNING: No documents found for evaluated chaining rule:", next_q);
        return [req, res, hit];
    }
}