<commit_before>const _ = require('lodash');
const botpack = require('serverless-botpack-lib');
const ms = require('ms');
const openwhisk = require('openwhisk');
const uuid = require('uuid/v4');

exports.main = (params) => {
  const bot = botpack(params);
  const ow = openwhisk();

  const archiveOutput = (payload) => {
    const newPayload = _.cloneDeep(payload);

    _.set(newPayload, 'output.sent', _
      .chain(newPayload)
      .get('output.sent', [])
      .concat(_.omit(newPayload.output, 'sent'))
      .value());

    _.set(newPayload, 'output', _.pick(newPayload.output, 'sent'));

    return Promise.resolve(newPayload);
  }

  const enrichParams = () => {
    // Generate payload id if not existing
    if (!_.get(params, 'payload.id')) {
      _.set(params, 'payload.id', uuid());
    }

    // Prepare payload.output
    _.set(params, 'payload.output.timestamp', Date.now() / 1000 | 0)

    _.set(params, 'payload.output.channel',
      _.get(params, 'payload.output.channel') ||
      _.get(params, 'channel') ||
      _.get(params, 'payload.input.channel'));

    _.set(params, 'payload.output.user',
      _.get(params, 'payload.output.user') ||
      _.get(params, 'user') ||
      _.get(params, `payload.conversationcontext.user.${params.payload.output.channel}_id`));

    _.set(params, 'payload.output.intent',
      _.get(params, 'payload.output.intent') ||
      _.get(params, 'intent'));

    _.set(params, 'payload.output.locale',
      _.get(params, 'payload.output.locale') ||
      _.get(params, 'locale') ||
      _.get(params, 'payload.conversationcontext.user.locale'));

    _.set(params, 'payload.output.context',
      _.get(params, 'payload.output.context') ||
      _.get(params, 'context'));

    return Promise.resolve(params);
  }

  const generateMessage = (payload) => {
    const invokeParams = {
      name: `${_.get(params.config, 'openwhisk.package')}/core-transform`,
      blocking: true,
      result: true,
      params: { payload }
    }

    return ow.actions.invoke(invokeParams)
      .then(result => {
        if (result.statusCode !== 200) {
          return Promise.reject({
            statusCode: 503,
            error: {
              message: 'The core-transform action did not respond with a valid result.',
              parameters: {
                result
              }
            }
          })
        } else {
          _.set(payload, 'output.message', _.get(result, 'result.output.message'));
          return payload;
        }
      });
  }

  const callOutputConnector = (payload) => {
    const connector = _
      .chain(params.config.connectors.output)
      .filter({ channel: payload.output.channel })
      .first()
      .value();

    if (connector) {
      const call = (messages) => {
        const message = _.head(messages);
        const remaining = _.tail(messages);

        if (_.isObject(message) && message.wait) {
          const milliseconds = ms(message.wait);

          if (milliseconds > 30000) {
            return Promise.reject({
              statusCode: 500,
              error: {
                message: 'The wait timeout is longer than 30s, this is not allowed',
                parameters: {
                  wait: milliseconds,
                  wait_text: message.wait
                }
              }
            })
          } else {
            if (message.typing) {
              return call([{ typing_on: true }])
                .then(() => {
                  return new Promise((resolve, reject) => {
                    setTimeout(resolve, milliseconds);
                  });
                })
                .then(() => {
                  return call(_.concat([{ typing_off: true }], remaining));
                });
            } else {
              return new Promise((resolve, reject) => {
                setTimeout(resolve, milliseconds);
              }).then(() => {
                return call(remaining);<commit_msg>added logging.<commit_after>const _ = require('lodash');
const botpack = require('serverless-botpack-lib');
const ms = require('ms');
const openwhisk = require('openwhisk');
const uuid = require('uuid/v4');

exports.main = (params) => {
  const bot = botpack(params);
  const ow = openwhisk();

  const archiveOutput = (payload) => {
    const newPayload = _.cloneDeep(payload);

    _.set(newPayload, 'output.sent', _
      .chain(newPayload)
      .get('output.sent', [])
      .concat(_.omit(newPayload.output, 'sent'))
      .value());

    _.set(newPayload, 'output', _.pick(newPayload.output, 'sent'));

    return Promise.resolve(newPayload);
  }

  const enrichParams = () => {
    // Generate payload id if not existing
    if (!_.get(params, 'payload.id')) {
      _.set(params, 'payload.id', uuid());
    }

    // Prepare payload.output
    _.set(params, 'payload.output.timestamp', Date.now() / 1000 | 0)

    _.set(params, 'payload.output.channel',
      _.get(params, 'payload.output.channel') ||
      _.get(params, 'channel') ||
      _.get(params, 'payload.input.channel'));

    _.set(params, 'payload.output.user',
      _.get(params, 'payload.output.user') ||
      _.get(params, 'user') ||
      _.get(params, `payload.conversationcontext.user.${params.payload.output.channel}_id`));

    _.set(params, 'payload.output.intent',
      _.get(params, 'payload.output.intent') ||
      _.get(params, 'intent'));

    _.set(params, 'payload.output.locale',
      _.get(params, 'payload.output.locale') ||
      _.get(params, 'locale') ||
      _.get(params, 'payload.conversationcontext.user.locale'));

    _.set(params, 'payload.output.context',
      _.get(params, 'payload.output.context') ||
      _.get(params, 'context'));

    return Promise.resolve(params);
  }

  const generateMessage = (payload) => {
    const invokeParams = {
      name: `${_.get(params.config, 'openwhisk.package')}/core-transform`,
      blocking: true,
      result: true,
      params: { payload }
    }

    return ow.actions.invoke(invokeParams)
      .then(result => {
        if (result.statusCode !== 200) {
          return Promise.reject({
            statusCode: 503,
            error: {
              message: 'The core-transform action did not respond with a valid result.',
              parameters: {
                result
              }
            }
          })
        } else {
          _.set(payload, 'output.message', _.get(result, 'result.output.message'));
          return payload;
        }
      });
  }

  const callOutputConnector = (payload) => {
    const connector = _
      .chain(params.config.connectors.output)
      .filter({ channel: payload.output.channel })
      .first()
      .value();

    if (connector) {
      const call = (messages) => {
        const message = _.head(messages);
        const remaining = _.tail(messages);

        console.log(JSON.stringify(message));
        console.log('---');
        console.log(JSON.stringify(messages));

        if (_.isObject(message) && message.wait) {
          const milliseconds = ms(message.wait);

          if (milliseconds > 30000) {
            return Promise.reject({
              statusCode: 500,
              error: {
                message: 'The wait timeout is longer than 30s, this is not allowed',
                parameters: {
                  wait: milliseconds,
                  wait_text: message.wait
                }
              }
            })
          } else {
            if (message.typing) {
              return call([{ typing_on: true }])
                .then(() => {
                  return new Promise((resolve, reject) => {
                    setTimeout(resolve, milliseconds);
                  });
                })
                .then(() => {
                  return call(_.concat([{ typing_off: true }], remaining));
                });
            } else {
              return new Promise((resolve, reject) => {
                setTimeout(resolve, milliseconds);
              }).then(() => {
                return call(remaining);
{'fraction_non_alphanumeric': 0.1522577435004354, 'fraction_numerical': 0.005473317576813037, 'mean_word_length': 1.963509030593439, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '4983214', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2195, 'n_words': 459}