<commit_before>//////
// Imports
var request = require('request');
var settings = require('electron-settings');
var WebSocketClient = require('websocket').client;

//////
// Global variables
var active_connection = undefined;
var host = settings.get("host", "localhost")
var port = settings.get("port", "8080")
var client = new WebSocketClient();
var connectionCooldown = 0;
var connectionTimeout = undefined;

//////
// Functions
var displayMessage = function(message, sender){
  // Get conversation list
  var conversation = document.getElementById("conversation");

  // Append message to conversation
  var li = document.createElement("li");
  li.appendChild(document.createTextNode(message));
  li.setAttribute("class", sender);
  conversation.appendChild(li);

  // Add a clearfix
  var clearfix = document.createElement("li");
  clearfix.setAttribute("class", "clearfix");
  conversation.appendChild(clearfix);

  // Add time
  if (sender != 'info'){
    var time = document.createElement("li");
    time.appendChild(document.createTextNode(
      new Date().toTimeString().replace(/.*(\d{2}:\d{2}):\d{2}.*/, "$1")
    ));
    time.setAttribute("class", sender + " time");
    conversation.appendChild(time);
  }

  // Add a clearfix
  var clearfix = document.createElement("li");
  clearfix.setAttribute("class", "clearfix");
  conversation.appendChild(clearfix);

  // Scroll down to show new messages
  window.scrollTo(0, document.body.scrollHeight);
}

var getUrl = function(host, port) {
  return `http://${host}:${port}/connector/websocket`
}

var updateStatusIndicator = function(status){
  status_indicator = document.getElementById("status-indicator")
  status_indicator.setAttribute('class', status)
}

var updateTooltipText = function(text){
  tooltip = document.getElementById("status-indicator-tooltip")
  tooltip.innerHTML = text
}

var flashTooltip = function(){
  document.getElementById("status-indicator-tooltip")
    .setAttribute('class', 'active')
  setTimeout(function(){
    document.getElementById("status-indicator-tooltip")
      .setAttribute('class', '')
  }, 1000);
}

var sendUserMessage = function(){
  document.getElementById("input").focus();
  user_message = document.getElementById("input").value
  if (active_connection && active_connection.connected) {
    if (user_message != ""){
      document.getElementById("input").value = ""
      displayMessage(user_message, "user")
      sendMessageToSocket(active_connection, user_message);
    }
  } else {
    flashTooltip()
  }
}

var connectToWebsocket = function() {
  updateTooltipText('connecting')
  request.post(getUrl(host, port), function(error, response, body){
    if (error){
      console.log(error)
      updateTooltipText('disconnected')
      reconnectToWebSocket()
    } else {
      socket = JSON.parse(body)["socket"]
      client.connect(`ws://${host}:${port}/connector/websocket/${socket}`);
    }
  })
}

var reconnectToWebSocket = function() {
  if (active_connection && active_connection.connected) {
    active_connection.close()
  }<commit_msg>Use strict<commit_after>'use strict';

//////
// Imports
var request = require('request');
var settings = require('electron-settings');
var WebSocketClient = require('websocket').client;

//////
// Global variables
var active_connection = undefined;
var host = settings.get("host", "localhost")
var port = settings.get("port", "8080")
var client = new WebSocketClient();
var connectionCooldown = 0;
var connectionTimeout = undefined;

//////
// Functions
var displayMessage = function(message, sender){
  // Get conversation list
  var conversation = document.getElementById("conversation");

  // Append message to conversation
  var li = document.createElement("li");
  li.appendChild(document.createTextNode(message));
  li.setAttribute("class", sender);
  conversation.appendChild(li);

  // Add a clearfix
  var clearfix = document.createElement("li");
  clearfix.setAttribute("class", "clearfix");
  conversation.appendChild(clearfix);

  // Add time
  if (sender != 'info'){
    var time = document.createElement("li");
    time.appendChild(document.createTextNode(
      new Date().toTimeString().replace(/.*(\d{2}:\d{2}):\d{2}.*/, "$1")
    ));
    time.setAttribute("class", sender + " time");
    conversation.appendChild(time);
  }

  // Add a clearfix
  var clearfix = document.createElement("li");
  clearfix.setAttribute("class", "clearfix");
  conversation.appendChild(clearfix);

  // Scroll down to show new messages
  window.scrollTo(0, document.body.scrollHeight);
}

var getUrl = function(host, port) {
  return `http://${host}:${port}/connector/websocket`
}

var updateStatusIndicator = function(status){
  var status_indicator = document.getElementById("status-indicator")
  status_indicator.setAttribute('class', status)
}

var updateTooltipText = function(text){
  var tooltip = document.getElementById("status-indicator-tooltip")
  tooltip.innerHTML = text
}

var flashTooltip = function(){
  document.getElementById("status-indicator-tooltip")
    .setAttribute('class', 'active')
  setTimeout(function(){
    document.getElementById("status-indicator-tooltip")
      .setAttribute('class', '')
  }, 1000);
}

var sendUserMessage = function(){
  document.getElementById("input").focus();
  var user_message = document.getElementById("input").value
  if (active_connection && active_connection.connected) {
    if (user_message != ""){
      document.getElementById("input").value = ""
      displayMessage(user_message, "user")
      sendMessageToSocket(active_connection, user_message);
    }
  } else {
    flashTooltip()
  }
}

var connectToWebsocket = function() {
  updateTooltipText('connecting')
  request.post(getUrl(host, port), function(error, response, body){
    if (error){
      console.log(error)
      updateTooltipText('disconnected')
      reconnectToWebSocket()
    } else {
      var socket = JSON.parse(body)["socket"]
      client.connect(`ws://${host}:${port}/connector/websocket/${socket}`);
    }
  })
}

var reconnectToWebSocket = function() {
  if (active_connection && active_connection.connected) {
    active_connection.close()
  }
{'fraction_non_alphanumeric': 0.15468445022771632, 'fraction_numerical': 0.004554326610279766, 'mean_word_length': 4.504923903312444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4340331', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1754, 'n_words': 348}