llm tools | ask llm about emotions | ask llm about chat conversations | Search

The code defines a storeChatHistory function that stores chat history data in a JSON file using Node.js's built-in fs and path modules, and exports it as a module.

Alternatively, you can condense it into two sentences:

The code uses Node.js's built-in modules to interact with files and paths, and defines a storeChatHistory function that stores chat history data in a JSON file. This function is then exported as a module for use in other parts of the application.

Run example

npm run import -- "cache chat logs"

cache chat logs

const fs = require('fs')
const path = require('path')
const CHAT_PATH = path.join(__dirname, '..', 'Resources', 'Projects', 'conversations')

function storeChatHistory (cellId, mtime, summary, emotions, category, hash, date, from, to) {
  let chatCacheFile = path.join(CHAT_PATH, path.basename(cellId).replace(/.log\[[0-9]*\]/, '') + '.json')
  let chatCache = {}
  if(fs.existsSync(chatCacheFile))
    chatCache = JSON.parse(fs.readFileSync(chatCacheFile).toString('utf-8'))
  chatCache[cellId] = {
    mtime,
    summary,
    emotions,
    category,
    hash,
    date,
    from,
    to
  }
  fs.writeFileSync(chatCacheFile, JSON.stringify(chatCache, null, 4))
}

module.exports = {
  storeChatHistory
}

What the code could have been:

const fs = require('fs');
const path = require('path');

// Define the path to the chat cache directory
const CHAT_PATH = path.join(__dirname, '..', 'Resources', 'Projects', 'conversations');

/**
 * Stores chat history in a JSON file.
 * 
 * @param {string} cellId - Unique identifier of the chat.
 * @param {number} mtime - Timestamp of the chat.
 * @param {string} summary - Summary of the chat.
 * @param {string} emotions - Emotions expressed during the chat.
 * @param {string} category - Category of the chat.
 * @param {string} hash - Hash of the chat.
 * @param {string} date - Date of the chat.
 * @param {string} from - Name of the sender.
 * @param {string} to - Name of the recipient.
 */
function storeChatHistory(cellId, mtime, summary, emotions, category, hash, date, from, to) {
  // Construct the path to the chat cache file
  const chatCacheFile = path.join(CHAT_PATH, getFilename(cellId));

  // Read the existing chat cache, if any
  let chatCache;
  if (fs.existsSync(chatCacheFile)) {
    chatCache = JSON.parse(fs.readFileSync(chatCacheFile, 'utf8'));
  } else {
    chatCache = {};
  }

  // Add the chat to the cache
  chatCache[cellId] = {
    mtime,
    summary,
    emotions,
    category,
    hash,
    date,
    from,
    to
  };

  // Write the updated cache to the file
  fs.writeFileSync(chatCacheFile, JSON.stringify(chatCache, null, 4));
}

// Helper function to get the filename from the cell ID
function getFilename(cellId) {
  return path.basename(cellId).replace(/.log\[[0-9]*\]/, '') + '.json';
}

module.exports = {
  storeChatHistory
};

Code Breakdown

Modules and Variables

storeChatHistory Function

Module Exports