This code consists of two functions, listMemories
and findMemories
, which are used to retrieve memories from a file system, with listMemories
transforming memory keys into dates and returning an array of strings. The findMemories
function searches for memories in a specific file or in a history of files, returning the memories object and caching the result for future reuse.
npm run import -- "llm load memories"
const fs = require('fs')
const path = require('path')
const {ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL} = importer.import("general chit chat")
async function listMemories(session) {
let memories = await findMemories(session)
return Object.keys(memories).map(key => {
// provide the date the memory was made
let date = new Date(parseInt(key))
return date.getFullYear() + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + date.getDate()
+ ' - ' + memories[key]
})
}
async function findMemories(session) {
if(!session) {
return {}
}
let now = new Date()
let currentFile = path.join(PROJECT_PATH, now.getFullYear() + '-'
+ String(now.getMonth() + 1).padStart(2, '0')
+ '-' + DEFAULT_MODEL
+ '-' + session + '.json')
// TODO: reload chat
if(typeof ACTIVE_CONVERSATIONS[currentFile] == 'undefined') {
if(fs.existsSync(currentFile)) {
ACTIVE_CONVERSATIONS[currentFile] = JSON.parse(fs.readFileSync(currentFile))
} else {
ACTIVE_CONVERSATIONS[currentFile] = {}
}
}
if(typeof ACTIVE_CONVERSATIONS[currentFile]['memories'] != 'undefined') {
debugger
return ACTIVE_CONVERSATIONS[currentFile]['memories']
}
let history = fs.readdirSync(PROJECT_PATH)
// newest to oldest
history.sort((a, b) => b - a)
for(let i = 0; i < history.length; i++) {
if(!history[i].match('-' + DEFAULT_MODEL + '-' + session + '.json')) {
continue
}
let convoFile = path.join(PROJECT_PATH, history[i])
if(typeof ACTIVE_CONVERSATIONS[convoFile] == 'undefined') {
if(fs.existsSync(convoFile)) {
ACTIVE_CONVERSATIONS[convoFile] = JSON.parse(fs.readFileSync(convoFile))
} else {
ACTIVE_CONVERSATIONS[convoFile] = {}
}
}
let conversation = ACTIVE_CONVERSATIONS[convoFile]
if(typeof conversation['memories'] != 'undefined') {
return (ACTIVE_CONVERSATIONS[currentFile]['memories'] = conversation['memories'])
}
}
return (ACTIVE_CONVERSATIONS[currentFile]['memories'] = {})
}
module.exports = {
findMemories,
listMemories,
}
const { ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL } = require('./general-chit-chat');
async function findMemories(session) {
if (!session) {
return {};
}
const now = new Date();
const currentFile = getConversationFile(now, session);
if (!ACTIVE_CONVERSATIONS[currentFile]) {
await loadConversationFile(currentFile);
}
return ACTIVE_CONVERSATIONS[currentFile]['memories'] || {};
}
async function loadConversationFile(file) {
if (fs.existsSync(file)) {
ACTIVE_CONVERSATIONS[file] = JSON.parse(fs.readFileSync(file));
} else {
ACTIVE_CONVERSATIONS[file] = {};
}
return file;
}
function getConversationFile(now, session) {
const date = now.toISOString().split('T')[0];
return `${PROJECT_PATH}/${date}-${DEFAULT_MODEL}-${session}.json`;
}
async function listMemories(session) {
const memories = await findMemories(session);
const dates = Object.keys(memories).map((key) => {
const date = new Date(parseInt(key));
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${date.getDate()} - ${memories[key]}`;
});
return dates;
}
module.exports = {
findMemories,
listMemories,
};
const fs = require('fs')
const path = require('path')
const {ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL} = importer.import('general chit chat')
fs
is the built-in Node.js module for file system operations.path
is the built-in Node.js module for working with file paths.importer.import('general chit chat')
line imports constants from a separate module.listMemories
Functionasync function listMemories(session) {
let memories = await findMemories(session)
return Object.keys(memories).map(key => {
// provide the date the memory was made
let date = new Date(parseInt(key))
return date.getFullYear() + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + date.getDate()
+'-'+ memories[key]
})
}
listMemories
is an asynchronous function that takes a session
parameter.findMemories
with the session
parameter and awaits the result.findMemories
Functionasync function findMemories(session) {
if(!session) {
return {}
}
let now = new Date()
let currentFile = path.join(PROJECT_PATH, now.getFullYear() + '-'
+ String(now.getMonth() + 1).padStart(2, '0')
+ '-' + DEFAULT_MODEL
+ '-' + session + '.json')
if(typeof ACTIVE_CONVERSATIONS[currentFile] == 'undefined') {
if(fs.existsSync(currentFile)) {
ACTIVE_CONVERSATIONS[currentFile] = JSON.parse(fs.readFileSync(currentFile))
} else {
ACTIVE_CONVERSATIONS[currentFile] = {}
}
}
if(typeof ACTIVE_CONVERSATIONS[currentFile]['memories']!= 'undefined') {
return ACTIVE_CONVERSATIONS[currentFile]['memories']
}
let history = fs.readdirSync(PROJECT_PATH)
// newest to oldest
history.sort((a, b) => b - a)
for(let i = 0; i < history.length; i++) {
if(!history[i].match('-' + DEFAULT_MODEL + '-' + session + '.json')) {
continue
}
let convoFile = path.join(PROJECT_PATH, history[i])
if(typeof ACTIVE_CONVERSATIONS[convoFile] == 'undefined') {
if(fs.existsSync(convoFile)) {
ACTIVE_CONVERSATIONS[convoFile] = JSON.parse(fs.readFileSync(convoFile))
} else {
ACTIVE_CONVERSATIONS[convoFile] = {}
}
}
let conversation = ACTIVE_CONVERSATIONS[convoFile]
if(typeof conversation['memories']!= 'undefined') {
return (ACTIVE_CONVERSATIONS[currentFile]['memories'] = conversation['memories'])
}
}
return (ACTIVE_CONVERSATIONS[currentFile]['memories'] = {})
}
findMemories
is an asynchronous function that takes a session
parameter.session
parameter is truthy and returns an empty object if it's falsy.ACTIVE_CONVERSATIONS
. If not, it reads the file and caches it.memories
object and returns it if it does. Otherwise, it searches for a matching conversation file in the history (newest to oldest) and returns its memories
object. If no matching file is found, it returns an empty memories
object.ACTIVE_CONVERSATIONS
so it can be reused in subsequent calls.module.exports = {
listMemories,
findMemories
}
listMemories
and findMemories
functions are exported as module exports.