<commit_before>'use strict';

const Rx = require('rx-lite');
const Wit = require('node-wit').Wit;

class WitAi {
  constructor(witToken, speaker, logger) {
    // This will contain all user sessions
    // Each session has an entry:
    // sessionId -> {
    //   channel: messageChannel,
    //   user: messageUser,
    //   context: sessionState
    // }
    this.sessions = {};

    this.logger = logger;
    const actions = {
      say(sessionId, context, message, cb) {
        const session = this.sessions[sessionId] || {};
        speaker.sayMessage(session.channel, message).finally(cb).subscribe();
      },
      merge(sessionId, context, entities, message, cb) {
        cb(context);
      },
      error(sessionId, context, err) {
        logger.error(err);

        const session = this.sessions[sessionId] || {};
        speaker.sayError(session.channel, err).subscribe();
      }
    };
    this.wit = new Wit(witToken, actions, logger);
  }

  runActions(message) {
    const sessionId = this._findOrCreateSession(message);
    const text = message.match[0];
    const context = this.sessions[sessionId].context;

    return Rx.Observable
      .fromNodeCallback(this.wit.runActions)(sessionId, text, context)
      .finally(() => {<commit_msg>Fix issue with wit-ai variable binding<commit_after>'use strict';

const Rx = require('rx-lite');
const Wit = require('node-wit').Wit;

class WitAi {
  constructor(witToken, speaker, logger) {
    // This will contain all user sessions
    // Each session has an entry:
    // sessionId -> {
    //   channel: messageChannel,
    //   user: messageUser,
    //   context: sessionState
    // }
    this.sessions = {};

    this.logger = logger;
    const actions = {
      say: (sessionId, context, message, cb) => {
        const session = this.sessions[sessionId] || {};
        speaker.sayMessage(session.channel, message).finally(cb).subscribe();
      },
      merge: (sessionId, context, entities, message, cb) => {
        cb(context);
      },
      error: (sessionId, context, err) => {
        logger.error(err);

        const session = this.sessions[sessionId] || {};
        speaker.sayError(session.channel, err).subscribe();
      }
    };
    this.wit = new Wit(witToken, actions, logger);
  }

  runActions(message) {
    const sessionId = this._findOrCreateSession(message);
    const text = message.match[0];
    const context = this.sessions[sessionId].context;

    return Rx.Observable
      .fromNodeCallback(this.wit.runActions)(sessionId, text, context)
      .finally(() => {
{'fraction_non_alphanumeric': 0.141295862607338, 'fraction_numerical': 0.00078064012490242, 'mean_word_length': 2.7198838896952107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '1593093', 'n_tokens_mistral': 781, 'n_tokens_neox': 763, 'n_words': 170}