<commit_before>(function(){
  
  var chat = {
    messageToSend: '',
    templateResponse: Handlebars.compile( $("#message-response-template").html()),
    init: function() {
      this.cacheDOM();
      this.bindEvents();
      this.render();
    },
    cacheDOM: function() {
      this.$chatHistory = $('.chat-history');
      this.$button = $('button');
      this.$textarea = $('#message-to-send');
      this.$chatHistoryList =  this.$chatHistory.find('ul');
    },
    bindEvents: function() {
      this.$button.on('click', this.addMessage.bind(this));
      this.$textarea.on('keyup', this.addMessageEnter.bind(this));
    },
    render: function() {
      this.scrollToBottom();
      if (this.messageToSend.trim() !== '') {
        var template = Handlebars.compile( $("#message-template").html());
        var context = { 
          messageOutput: this.messageToSend,
          time: this.getCurrentTime()
        };

        this.$chatHistoryList.append(template(context));
        this.scrollToBottom();
        this.$textarea.val('');
        
        // responses
        var query = this.messageToSend.trim();
        this.getSusiResponse(query);
      }
      
    },
    addMessage: function() {
      this.messageToSend = this.$textarea.val()
      this.render();         
    },
    addMessageEnter: function(event) {
        // enter was pressed
        if (event.keyCode === 13) {
          if (event.shiftKey) { //enter + shift
            $(this).val( $(this.target).val() + "\n" );
          } else {
            this.addMessage();
          }
        }
    },
    scrollToBottom: function() {
       this.$chatHistory.scrollTop(this.$chatHistory[0].scrollHeight);
    },
    getCurrentTime: function() {
        return new Date().toLocaleTimeString().
                replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
    },
    processSusiData: function(data) {
      var contextResponse = { 
        response: data.answers[0].actions[0].expression,
        time: this.getCurrentTime()
      };
      this.$chatHistoryList.append(this.templateResponse(contextResponse));
      this.scrollToBottom();
    },
    getSusiResponse: function(queryString) {
      var _super = this;
      $.ajax({
        url: 'https://loklak.org/api/susi.json?callback=p&q='+encodeURIComponent(queryString),
        dataType: 'jsonp',
        jsonpCallback: 'p',
        jsonp: 'callback',
        crossDomain: true,
        timeout: 10000,
        success: function (data) {
          _super.processSusiData(data);
        }
      });
    }
  };

  
  chat.init();
  
  var searchFilter = {
    options: { valueNames: ['name'] },
    init: function() {
      var userList = new List('people-list', this.options);
      var noItems = $('<li id="no-items-found">No items found</li>');
      
      userList.on('updated', function(list) {
        if (list.matchingItems.length === 0) {
          $(list.list).append(noItems);<commit_msg>added hack to susi to make it possible to have the code for local susi server development: If the asksusi html is opened locally in a file path in the browser, then the js tries to get an answer from a local loklak_server api. With fail-over to loklak.org<commit_after>(function(){
  
  var chat = {
    messageToSend: '',
    templateResponse: Handlebars.compile( $("#message-response-template").html()),
    init: function() {
      this.cacheDOM();
      this.bindEvents();
      this.render();
    },
    cacheDOM: function() {
      this.$chatHistory = $('.chat-history');
      this.$button = $('button');
      this.$textarea = $('#message-to-send');
      this.$chatHistoryList =  this.$chatHistory.find('ul');
    },
    bindEvents: function() {
      this.$button.on('click', this.addMessage.bind(this));
      this.$textarea.on('keyup', this.addMessageEnter.bind(this));
    },
    render: function() {
      this.scrollToBottom();
      if (this.messageToSend.trim() !== '') {
        var template = Handlebars.compile( $("#message-template").html());
        var context = { 
          messageOutput: this.messageToSend,
          time: this.getCurrentTime()
        };

        this.$chatHistoryList.append(template(context));
        this.scrollToBottom();
        this.$textarea.val('');
        
        // responses
        var query = this.messageToSend.trim();
        this.getSusiResponse(query);
      }
      
    },
    addMessage: function() {
      this.messageToSend = this.$textarea.val()
      this.render();         
    },
    addMessageEnter: function(event) {
        // enter was pressed
        if (event.keyCode === 13) {
          if (event.shiftKey) { //enter + shift
            $(this).val( $(this.target).val() + "\n" );
          } else {
            this.addMessage();
          }
        }
    },
    scrollToBottom: function() {
       this.$chatHistory.scrollTop(this.$chatHistory[0].scrollHeight);
    },
    getCurrentTime: function() {
        return new Date().toLocaleTimeString().
                replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
    },
    processSusiData: function(data) {
      var contextResponse = { 
        response: data.answers[0].actions[0].expression,
        time: this.getCurrentTime()
      };
      this.$chatHistoryList.append(this.templateResponse(contextResponse));
      this.scrollToBottom();
    },
    susiapipath: '/api/susi.json?callback=p&q=',
    localhost:'http://127.0.0.1:9000',
    remotehost:'https://loklak.org',
    getSusiResponse: function(queryString) {
      var _super = this;
      $.ajax({
        url: (window.location.protocol == 'file:' ? _super.localhost : _super.remotehost) + _super.susiapipath + encodeURIComponent(queryString),
        dataType: 'jsonp',
        jsonpCallback: 'p',
        jsonp: 'callback',
        crossDomain: true,
        timeout: window.location.protocol == 'file:' ? 1000 : 10000,
        success: function (data) {
          _super.processSusiData(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
          $.ajax({
            url: _super.remotehost + _super.susiapipath + encodeURIComponent(queryString),
            dataType: 'jsonp',
            jsonpCallback: 'p',
            jsonp: 'callback',
            crossDomain: true,
            success: function (data) {
              data.answers[0].actions[0].expression = '* ' + data.answers[0].actions[0].expression
              _super.processSusiData(data);
            }
          });
        } 
      });
    }
  };

  
  chat.init();
  
  var searchFilter = {
    options: { valueNames: ['name'] },
    init: function() {
      var userList = new List('people-list', this.options);
      var noItems = $('<li id="no-items-found">No items found</li>');
      
      userList.on('updated', function(list) {
        if (list.matchingItems.length === 0) {
          $(list.list).append(noItems);
{'fraction_non_alphanumeric': 0.16196158323632132, 'fraction_numerical': 0.006984866123399301, 'mean_word_length': 2.271299381247025, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '7502524', 'n_tokens_mistral': 2074, 'n_tokens_neox': 2011, 'n_words': 361}