llm chat | add conversation context | llm save memories | Search

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.

Run example

npm run import -- "llm load memories"

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,
}

What the code could have been:

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,
};

Code Breakdown

Imported Modules and Constants

const fs = require('fs')
const path = require('path')
const {ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL} = importer.import('general chit chat')

listMemories Function

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]
  })
}

findMemories Function

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')

  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'] = {})
}

Module Exports

module.exports = {
  listMemories,
  findMemories
}