<commit_before>'use strict';

const helpers = require('../../helpers');
const logger = require('../../logger');
const graphql = require('../../graphql');

module.exports = function getRivescript() {
  return async (req, res) => {
    try {
      const resetCache = req.query.cache === 'false';

      if (resetCache === true || !helpers.rivescript.isBotReady()) {
        await helpers.rivescript.loadBot(resetCache);
      } else {
        const isCurrent = helpers.rivescript.isRivescriptCurrent();

        if (!isCurrent) {
          await helpers.rivescript.loadBot();
        }
      }

      const dynamicTriggers = await graphql.fetchConversationTriggers();

      console.log(dynamicTriggers);

      const data = helpers.rivescript.getDeparsedRivescript();

      logger.debug('data.topics.random', { count: data.topics.random.length });

      Object.keys(data.topics).forEach((rivescriptTopicName) => {
        data.topics[rivescriptTopicName].forEach((rivescriptTopicTrigger, index) => {
           const replyText = rivescriptTopicTrigger.reply[0];
           const macro = helpers.macro.getMacro(replyText);

           const current = data.topics[rivescriptTopicName][index];

           current.reply = macro ? macro.text : replyText;
           current.macroName = macro ? macro.name : null;

           const dynamicTrigger = dynamicTriggers.find(item => item.trigger === current.trigger);
  
           current.hardcoded = !dynamicTrigger;
        });
      });

      return res.send({ data });
    } catch (err) {
      return helpers.sendErrorResponse(res, err);
    }
  };
};<commit_msg>Backwards compatible<commit_after>'use strict';

const helpers = require('../../helpers');
const logger = require('../../logger');
const graphql = require('../../graphql');

module.exports = function getRivescript() {
  return async (req, res) => {
    try {
      const resetCache = req.query.cache === 'false';

      if (resetCache === true || !helpers.rivescript.isBotReady()) {
        await helpers.rivescript.loadBot(resetCache);
      } else {
        const isCurrent = helpers.rivescript.isRivescriptCurrent();

        if (!isCurrent) {
          await helpers.rivescript.loadBot();
        }
      }

      const dynamicTriggers = await graphql.fetchConversationTriggers();

      console.log(dynamicTriggers);

      const data = helpers.rivescript.getDeparsedRivescript();

      logger.debug('data.topics.random', { count: data.topics.random.length });

      Object.keys(data.topics).forEach((rivescriptTopicName) => {
        data.topics[rivescriptTopicName].forEach((rivescriptTopicTrigger, index) => {
           const replyText = rivescriptTopicTrigger.reply[0];
           const macro = helpers.macro.getMacro(replyText);

           const current = data.topics[rivescriptTopicName][index];

           current.reply = macro ? [macro.text] : [replyText];
           current.macroName = macro ? macro.name : null;

           const dynamicTrigger = dynamicTriggers.find(item => item.trigger === current.trigger);

           current.hardcoded = !dynamicTrigger;
        });
      });

      return res.send({ data });
    } catch (err) {
      return helpers.sendErrorResponse(res, err);
    }
  };
};
{'fraction_non_alphanumeric': 0.13989476942123183, 'fraction_numerical': 0.0006190034045187249, 'mean_word_length': 2.7845433255269323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '650491', 'n_tokens_mistral': 938, 'n_tokens_neox': 905, 'n_words': 167}