<commit_before>module WitBot
  class MessageThread
    attr_accessor :metadata

    attr_reader :id, :messages, :bot_messages

    def initialize(id=SecureRandom.uuid)
      @id = id
      @messages = ActiveSupport::OrderedHash.new
      @bot_messages = ActiveSupport::OrderedHash.new
      @metadata = nil
    end

    def message(id)
      messages[id]
    end

    def messages_list(user: true, bot: false)
      bot_messages = bot ? @bot_messages.values : []
      user_messages = user ? @messages.values : []
      user_messages + bot_messages
    end

    def create_message(text, id=SecureRandom.uuid)
      @messages[id] = WitBot::Message.new self, text, id: id
    end

    def create_bot_message(text, id=SecureRandom.uuid)
      @bot_messages[id] = WitBot::Bot::Message.new self, text, id: id
    end

    def context
      @context ||= Context.new
    end

    def reset_context
      @context = Context.new
    end

    def to_hash
      {
        id: @id,
        context: @context,
        messages: {
          bot: @bot_messages,
          user: @messages
        },
        metadata: @metadata
      }
    end

    def from_hash(json)
      m = json[:messages]
      @messages = WitBot::Message.many_from_hash(self, m[:user])
      @bot_messages = WitBot::Message.many_from_hash(self, m[:bot])

      @context = Context.from_hash json[:context]
      @metadata = json[:metadata]
      self
    end

    def self.from_hash(json)
      json = json.with_indifferent_access
      self.new(json[:id]).from_hash json
    end

    def ==(other)
      self.id == other.id &&
          self.context == other.context &&
          self.bot_messages == other.bot_messages &&
          self.messages == other.messages &&
          self.metadata == other.metadata
    end

    def equals_without_messages(other)
      self.id == other.id &&<commit_msg>Only try to parse context IF there is actual context.<commit_after>module WitBot
  class MessageThread
    attr_accessor :metadata

    attr_reader :id, :messages, :bot_messages

    def initialize(id=SecureRandom.uuid)
      @id = id
      @messages = ActiveSupport::OrderedHash.new
      @bot_messages = ActiveSupport::OrderedHash.new
      @metadata = nil
    end

    def message(id)
      messages[id]
    end

    def messages_list(user: true, bot: false)
      bot_messages = bot ? @bot_messages.values : []
      user_messages = user ? @messages.values : []
      user_messages + bot_messages
    end

    def create_message(text, id=SecureRandom.uuid)
      @messages[id] = WitBot::Message.new self, text, id: id
    end

    def create_bot_message(text, id=SecureRandom.uuid)
      @bot_messages[id] = WitBot::Bot::Message.new self, text, id: id
    end

    def context
      @context ||= Context.new
    end

    def reset_context
      @context = Context.new
    end

    def to_hash
      {
        id: @id,
        context: @context,
        messages: {
          bot: @bot_messages,
          user: @messages
        },
        metadata: @metadata
      }
    end

    def from_hash(json)
      m = json[:messages]
      @messages = WitBot::Message.many_from_hash(self, m[:user])
      @bot_messages = WitBot::Message.many_from_hash(self, m[:bot])

      @context = Context.from_hash json[:context] if json[:context]
      @metadata = json[:metadata]
      self
    end

    def self.from_hash(json)
      json = json.with_indifferent_access
      self.new(json[:id]).from_hash json
    end

    def ==(other)
      self.id == other.id &&
          self.context == other.context &&
          self.bot_messages == other.bot_messages &&
          self.messages == other.messages &&
          self.metadata == other.metadata
    end

    def equals_without_messages(other)
      self.id == other.id &&
{'fraction_non_alphanumeric': 0.1076923076923077, 'fraction_numerical': 0.0, 'mean_word_length': 2.382062780269058, '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': '3814415', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1140, 'n_words': 259}