This code is a Node.js module that imports necessary modules and variables, defines functions for retrieving recent messages from conversations, and implements file system operations for loading and storing conversation data. The module exports the messageRecents
function, along with other variables and constants, allowing it to be used in other applications.
npm run import -- "general chit chat"
const path = require('path')
const fs = require('fs')
const PROJECT_PATH = path.join(__dirname, '..', 'Resources', 'Projects', 'conversations')
const ACTIVE_CONVERSATIONS = {}
const DEFAULT_MODEL = process.env.DEFAULT_MODEL || 'Default'
// TODO: load some previous summaries and contents
async function messageRecents(session) {
if(!session) {
return ''
}
let now = new Date()
let convoFile = path.join(PROJECT_PATH, now.getFullYear() + '-'
+ String(now.getMonth() + 1).padStart(2, '0')
+ '-' + DEFAULT_MODEL
+ '-' + session + '.json')
// TODO: reload chat
if(typeof ACTIVE_CONVERSATIONS[convoFile] == 'undefined') {
if(fs.existsSync(convoFile)) {
ACTIVE_CONVERSATIONS[convoFile] = JSON.parse(fs.readFileSync(convoFile))
} else {
ACTIVE_CONVERSATIONS[convoFile] = {}
}
}
let messageTimes = Object.keys(ACTIVE_CONVERSATIONS[convoFile])
.filter(k => k != 'summaries' && k != 'memories')
messageTimes.sort((a, b) => b - a)
if(messageTimes.length == 0) {
return ''
}
let count = 0
let messages = '\nCurrent date: ' + (new Date).toISOString()
+ '\nOur recent conversion:\n'
for(let i = 0; i < messageTimes.length && count < 25; i++) {
let message = ACTIVE_CONVERSATIONS[convoFile][messageTimes[i]]
let useSummary = i > 10 || messages.length > 1024 || (message.content && message.content.length > 1024)
let useKeywords = !message.content
messages += (message.user ? 'User: ' : 'AI: ')
+ (useKeywords ? message.keywords : (useSummary ? message.summary : message.content))
+ '\n\n'
if(messages.length > 2048) {
break
}
count++
}
return messages
}
module.exports = {
messageRecents,
PROJECT_PATH,
ACTIVE_CONVERSATIONS,
DEFAULT_MODEL
}
const path = require('path');
const fs = require('fs').promises;
const PROJECT_PATH = path.join(__dirname, '..', 'Resources', 'Projects', 'conversations');
const DEFAULT_MODEL = process.env.DEFAULT_MODEL || 'Default';
const MAX_MESSAGES = 25;
const MAX_MESSAGE_LENGTH = 1024;
const conversations = {};
async function loadConversationFile(file) {
try {
const content = await fs.readFile(file, 'utf8');
return JSON.parse(content);
} catch (err) {
if (err.code!== 'ENOENT') {
throw err;
}
return {};
}
}
async function messageRecents(session) {
if (!session) {
return '';
}
const now = new Date();
const convoFile = path.join(PROJECT_PATH, now.getFullYear(), pad(now.getMonth() + 1), DEFAULT_MODEL, session + '.json');
if (!conversations[convoFile]) {
conversations[convoFile] = await loadConversationFile(convoFile);
}
const messageTimes = Object.keys(conversations[convoFile])
.filter(k => k!=='summaries' && k!=='memories')
.sort((a, b) => b - a);
if (messageTimes.length === 0) {
return '';
}
const messages = `Current date: ${now.toISOString()}\nOur recent conversion:\n`;
let count = 0;
for (let i = 0; i < messageTimes.length && count < MAX_MESSAGES; i++) {
const message = conversations[convoFile][messageTimes[i]];
const useSummary = i > 10 || messages.length > 1024 || (message.content && message.content.length > MAX_MESSAGE_LENGTH);
const useKeywords =!message.content;
messages += (message.user? 'User:': 'AI: ') + (useKeywords? message.keywords : (useSummary? message.summary : message.content)) + '\n\n';
if (messages.length > 2048) {
break;
}
count++;
}
return messages;
}
module.exports = {
messageRecents,
};
function pad(num) {
return String(num).padStart(2, '0');
}
Code Breakdown
const path = require('path')
and const fs = require('fs')
: Import Node.js modules for working with file paths and file system operations.const PROJECT_PATH
: The path to the project directory.const ACTIVE_CONVERSATIONS
: An object to store active conversations.const DEFAULT_MODEL
: The default model to use, set to the value of the DEFAULT_MODEL
environment variable or 'Default' if not set.messageRecents(session)
: An asynchronous function that retrieves recent messages from a conversation.
session
: The session ID of the conversation.load some previous summaries and contents
: A TODO comment indicating that previous summaries and contents need to be loaded.messageRecents(session)
:
fs.existsSync(convoFile)
and fs.readFileSync(convoFile)
: Use file system operations to check if a file exists and read its contents.JSON.parse()
and JSON.stringify()
: Use JavaScript functions to parse and stringifying JSON data.module.exports = { messageRecents, PROJECT_PATH, ACTIVE_CONVERSATIONS, DEFAULT_MODEL }
: Exports the messageRecents
function and the PROJECT_PATH
, ACTIVE_CONVERSATIONS
, and DEFAULT_MODEL
variables as a module.