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.
npm run import -- "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
}
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
};
fs
: The built-in Node.js fs
(File System) module for interacting with files.path
: The built-in Node.js path
module for working with file paths.CHAT_PATH
: A constant representing the path to a directory containing chat history files.storeChatHistory
FunctioncellId
: The ID of the chat.mtime
: The modification time of the chat.summary
: A summary of the chat.emotions
: Emotions expressed during the chat.category
: The category of the chat.hash
: A hash of the chat.date
: The date of the chat.from
: The sender of the chat.to
: The recipient of the chat.cellId
.storeChatHistory
function is exported as a module.