<commit_before>const _ = require('lodash');

// Should be persisted
const sessionStorage = new Map();

// TODO: move elsewhere
const ENTITIES = [
    'persons',
    'nationalities',
    'facilities',
    'organisations',
    'locations',
    'locations',
    'products',
    'events',
    'art',
    'languages',
    'dates',
    'times',
    'percentages',
    'money',
    'quantities',
    'ordinal',
    'cardinal'
];

const constants = {
    MESSAGES: '_messages',
    ENTITY_HISTORY: '_entityHistory',
    LATEST_ENTITIES: '_latestEntities',
    REPLIES: '_replies',
    HISTORY_DEPTH: 5
};

const now = () => new Date().toISOString();

const getUser = ctx => _.get(ctx, '_user.user');

const newReply = (newContext, reply) => {
    console.log('Context got new reply');
    console.log(JSON.stringify(newContext, null, 2));

    const user = getUser(newContext);
    const latestContext = sessionStorage.get(user);

    if (latestContext &&
        latestContext._question &&
        latestContext._question.answered) {

    }

    console.log('latestContext', JSON.stringify(latestContext, null, 2));

    if (latestContext === undefined) {
        sessionStorage.set(user, newContext);
        return newContext;
    } else {
        const updatedContext = Object.assign({}, latestContext, newContext);
        sessionStorage.set(user, updatedContext);
        console.log('Updated context');
        console.log(JSON.stringify(updatedContext, null, 2));
        return updatedContext;
    }




    // const contextEntry = { timestamp: now(), reply };
    // if (context[constants.MESSAGES] === undefined) {
    //     context[constants.MESSAGES] = [contextEntry];
    // }
    // else {
    //     const messages = context[constants.MESSAGES];
    //     const lastMessage = messages[0];
    //     if (lastMessage[constants.REPLIES] === undefined) {
    //         lastMessage[constants.REPLIES] = [contextEntry];
    //     } else {
    //         lastMessage[constants.REPLIES].unshift(contextEntry);
    //     }
    // }
};

const newMessage = (newContext, message) => {
    console.log('Context got new message');
    console.log(JSON.stringify(newContext, null, 2));

    const user = getUser(newContext);
    const latestContext = sessionStorage.get(user);

    console.log('latestContext', JSON.stringify(latestContext, null, 2));

    if (latestContext === undefined) {
        newContext[constants.ENTITY_HISTORY] = [message.entities];
        newContext[constants.LATEST_ENTITIES] = message.entities;
        sessionStorage.set(user, newContext);
        return newContext;
    } else {
        if (Array.isArray(latestContext[constants.ENTITY_HISTORY])) {
            latestContext[constants.ENTITY_HISTORY].unshift(message.entities);
        } else {
            latestContext[constants.ENTITY_HISTORY] = [message.entities];
        }
        if (latestContext[constants.ENTITY_HISTORY].length > constants.HISTORY_DEPTH) {
            latestContext[constants.ENTITY_HISTORY].pop();<commit_msg>Removed debug noise<commit_after>const _ = require('lodash');

// Should be persisted
const sessionStorage = new Map();

// TODO: move elsewhere
const ENTITIES = [
    'persons',
    'nationalities',
    'facilities',
    'organisations',
    'locations',
    'locations',
    'products',
    'events',
    'art',
    'languages',
    'dates',
    'times',
    'percentages',
    'money',
    'quantities',
    'ordinal',
    'cardinal'
];

const constants = {
    MESSAGES: '_messages',
    ENTITY_HISTORY: '_entityHistory',
    LATEST_ENTITIES: '_latestEntities',
    REPLIES: '_replies',
    HISTORY_DEPTH: 5
};

const now = () => new Date().toISOString();

const getUser = ctx => _.get(ctx, '_user.user');

const newReply = (newContext, reply) => {
    const user = getUser(newContext);
    const latestContext = sessionStorage.get(user);

    if (latestContext &&
        latestContext._question &&
        latestContext._question.answered) {

    }

    if (latestContext === undefined) {
        sessionStorage.set(user, newContext);
        return newContext;
    } else {
        const updatedContext = Object.assign({}, latestContext, newContext);
        sessionStorage.set(user, updatedContext);
        return updatedContext;
    }
};

const newMessage = (newContext, message) => {
    console.log('Context got new message');

    const user = getUser(newContext);
    const latestContext = sessionStorage.get(user);

    if (latestContext === undefined) {
        newContext[constants.ENTITY_HISTORY] = [message.entities];
        newContext[constants.LATEST_ENTITIES] = message.entities;
        sessionStorage.set(user, newContext);
        return newContext;
    } else {
        if (Array.isArray(latestContext[constants.ENTITY_HISTORY])) {
            latestContext[constants.ENTITY_HISTORY].unshift(message.entities);
        } else {
            latestContext[constants.ENTITY_HISTORY] = [message.entities];
        }
        if (latestContext[constants.ENTITY_HISTORY].length > constants.HISTORY_DEPTH) {
            latestContext[constants.ENTITY_HISTORY].pop();
{'fraction_non_alphanumeric': 0.13462669018224574, 'fraction_numerical': 0.0015677052714089751, 'mean_word_length': 3.0188976377952756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '872780', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1522, 'n_words': 286}