brians resume | update insterests resume sheet | brians resume | Search

The messageHistory function is an asynchronous function that retrieves relevant history files, extracts keywords and timestamps, loads conversations, and asks the user for input to filter matching timestamps. It then logs the filtered matching timestamps, providing a list of related and unique timestamps that closely match the subject.

Run example

npm run import -- "load message history"

load message history

const {ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL} = importer.import("general chit chat")
const relevantHistory = importer.import("relevant llm history")
const relevantKeywords = importer.import("relevant llm keywords")
const matchingTimestamps = importer.import("relevant history timestamps")


async function messageHistory(promptModel, session, prompt) {
  
  let historyFiles = await relevantHistory(promptModel, session, prompt)

  let matching = await relevantKeywords(promptModel, session, prompt)

  let relevantTimestamps = await matchingTimestamps(promptModel, session, prompt, matching)

  let loadedConversations = Object.keys(ACTIVE_CONVERSATIONS)
    .filter(key => key.match('-' + DEFAULT_MODEL + '-' + session + '.json'))
  loadedConversations.sort((a, b) => b - a)
  let matchesClosely = 'Current date: ' + (new Date).toISOString() 
    + '\nOur recent topics:\n'
  for(let i = 0; i < loadedConversations.length; i++) {
    let conversation = ACTIVE_CONVERSATIONS[loadedConversations[i]]
    let timestamps = Object.keys(conversation).filter(k => k != 'summaries' && k != 'memories')
    timestamps.sort((a, b) => b - a)
    for(let j = 0; j < timestamps.length; j++) {
      let message = conversation[timestamps[j]]
      let topics = matching.filter(key => message.keywords.match(key))
      if(!prompt.match(timestamps[j]) && !relevantTimestamps.includes(timestamps[j])) {
        continue
      }

      matchesClosely += new Date(parseInt(timestamps[j])).toISOString() 
        + ' - ' + topics.join(', ') 
        + (message.summary ? (' - ' + message.summary) : '') 
        + '\n'
    }
  }

  let q4 = matchesClosely + '\nDo any of these times match the subject very closely:\n' 
    + prompt + '\nOnly respond with related and unique timestamps, no explanations.'
  console.log('User: ' + q4)
  let a4 = await promptModel(q4)
  console.log('AI: ' + a4)

  let matchingClosely = relevantTimestamps
    .filter(time => a4.match(time) || a4.match(new Date(parseInt(time)).toISOString()))

  console.log(matchesClosely)

  messages = 'Current date: ' + (new Date).toISOString() 
  messages += '\nOur most recent conversation:\n'
  
  let count = 0
  for(let i = 0; i < loadedConversations.length && count < 25; i++) {
    let conversation = ACTIVE_CONVERSATIONS[loadedConversations[i]]
    let timestamps = Object.keys(conversation).filter(k => k != 'summaries' && k != 'memories')
    timestamps.sort((a, b) => b - a)
    for(let j = 0; j < timestamps.length && count < 25; j++) {
      let message = conversation[timestamps[j]]

      if(!prompt.match(timestamps[j]) && !matchingClosely.includes(timestamps[j])) {
        continue
      }

      let useSummary = i > 10 || messages.length > 2048 || (message.content && message.content.length > 2048)
      let useKeywords = !message.content
      messages += (message.user ? 'User: ' : 'AI: ') 
        + (useKeywords ? message.keywords : (useSummary ? message.summary : message.content))
        + '\n\n'
      if(messages.length > 4096) {
        break
      }
      count++
    }
    if(messages.length > 4096) {
      break
    }
  }

  count = 0
  for(let i = 0; i < loadedConversations.length && count < 25; i++) {
    let conversation = ACTIVE_CONVERSATIONS[loadedConversations[i]]
    let timestamps = Object.keys(conversation).filter(k => k != 'summaries' && k != 'memories')
    timestamps.sort((a, b) => b - a)
    for(let j = 0; j < timestamps.length && count < 25; j++) {
      let message = conversation[timestamps[j]]

      if(!prompt.match(timestamps[j]) && !relevantTimestamps.includes(timestamps[j])) {
        continue
      }

      let useSummary = i > 10 || messages.length > 2048 || (message.content && message.content.length > 2048)
      let useKeywords = !message.content
      messages += (message.user ? 'User: ' : 'AI: ') 
        + (useKeywords ? message.keywords : (useSummary ? message.summary : message.content))
        + '\n\n'
      if(messages.length > 4096) {
        break
      }
      count++
    }
    if(messages.length > 4096) {
      break
    }
  }

  for(let i = 0; i < loadedConversations.length && count < 25; i++) {
    let conversation = ACTIVE_CONVERSATIONS[loadedConversations[i]]
    let timestamps = Object.keys(conversation).filter(k => k != 'summaries' && k != 'memories')
    timestamps.sort((a, b) => b - a)
    for(let j = 0; j < timestamps.length && count < 25; j++) {
      let message = conversation[timestamps[j]]

      if(!prompt.match(timestamps[j]) && matching.filter(key => message.keywords.match(key)).length == 0) {
        continue
      }

      let useSummary = i > 10 || messages.length > 2048 || (message.content && message.content.length > 2048)
      let useKeywords = !message.content
      messages += (message.user ? 'User: ' : 'AI: ') 
        + (useKeywords ? message.keywords : (useSummary ? message.summary : message.content))
        + '\n\n'
      if(messages.length > 4096) {
        break
      }
      count++
    }
    if(messages.length > 4096) {
      break
    }
  }

  return matchesClosely 
    + '\nConversation categories:\n' + matching.join('\n')
    + '\nClosely matching message IDs:\n' 
    + relevantTimestamps.join('\n') 
    + '\n' + messages
}

module.exports = messageHistory

What the code could have been:

const { ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL } = importer.import('general chit chat')
const { relevantHistory, relevantKeywords, matchingTimestamps } = importer.import('relevant llm history')
const { getLatestConversation } = importer.import('conversation helpers')

/**
 * Retrieve the message history for a given prompt.
 * 
 * @param {string} promptModel The model to use for the prompt.
 * @param {string} session The session ID.
 * @param {string} prompt The prompt to search for.
 * @returns {string} The message history and conversation categories.
 */
async function messageHistory(promptModel, session, prompt) {
  // Retrieve the relevant history files, keywords, and timestamps
  const historyFiles = await relevantHistory(promptModel, session, prompt)
  const matchingKeywords = await relevantKeywords(promptModel, session, prompt)
  const relevantTimestamps = await matchingTimestamps(promptModel, session, prompt, matchingKeywords)

  // Get the latest conversation files and sort them in descending order
  const loadedConversations = Object.keys(ACTIVE_CONVERSATIONS)
   .filter(conversation => conversation.match(`-${DEFAULT_MODEL}-${session}.json`))
   .sort((a, b) => b.localeCompare(a))

  // Create a string to store the matches closely
  const matchesClosely = `Current date: ${new Date().toISOString()}\nOur recent topics:\n`

  // Iterate over the conversations and find matching messages
  for (const conversation of loadedConversations) {
    const conversationData = ACTIVE_CONVERSATIONS[conversation]
    const timestamps = Object.keys(conversationData).filter(key => key!=='summaries' && key!=='memories')
     .sort((a, b) => b.localeCompare(a))

    for (const timestamp of timestamps) {
      const message = conversationData[timestamp]
      const topics = matchingKeywords.filter(key => message.keywords.match(key))

      // Skip if the prompt doesn't match or if the timestamp is not relevant
      if (!prompt.match(timestamp) &&!relevantTimestamps.includes(timestamp)) continue

      // Add the message to the matches closely string
      matchesClosely += `${new Date(parseInt(timestamp)).toISOString()} - ${topics.join(', ')}${message.summary? ` - ${message.summary}` : ''}\n`
    }
  }

  // Ask the user if any of the timestamps match the prompt closely
  const q4 = `${matchesClosely}\nDo any of these times match the subject very closely:\n${prompt}\nOnly respond with related and unique timestamps, no explanations.`
  console.log('User:', q4)
  const a4 = await promptModel(q4)
  console.log('AI:', a4)

  // Filter the relevant timestamps to match the user's response
  const matchingClosely = relevantTimestamps.filter(time => a4.match(time) || a4.match(new Date(parseInt(time)).toISOString()))

  // Create a string to store the conversation history
  let messages = `Current date: ${new Date().toISOString()}\nOur most recent conversation:\n`

  // Get the latest conversation or use the first one if none
  const latestConversation = getLatestConversation(loadedConversations)
  const conversationData = latestConversation? ACTIVE_CONVERSATIONS[latestConversation] : ACTIVE_CONVERSATIONS[loadedConversations[0]]

  // Get the timestamps for the latest conversation
  const latestTimestamps = Object.keys(conversationData).filter(key => key!=='summaries' && key!=='memories')
   .sort((a, b) => b.localeCompare(a))

  // Iterate over the timestamps and add the messages to the conversation history
  for (const timestamp of latestTimestamps) {
    const message = conversationData[timestamp]

    // Skip if the message doesn't match the prompt or if it's not in the matching closely timestamps
    if (!prompt.match(timestamp) &&!matchingClosely.includes(timestamp)) continue

    // Determine if to use the summary or content of the message
    const useSummary = latestConversation? true : message.content && message.content.length > 2048
    const useKeywords =!message.content

    // Add the message to the conversation history string
    messages += (message.user? 'User:': 'AI: ') + (useKeywords? message.keywords : (useSummary? message.summary : message.content)) + '\n\n'
  }

  // Log the conversation history and return the matches closely and conversation categories
  console.log(messages)
  return matchesClosely + '\nConversation categories:\n' + matchingKeywords.join('\n') + '\nClosely matching message IDs:\n' + relevantTimestamps.join('\n') + '\n' + messages
}

module.exports = messageHistory

Function Breakdown

Importing Modules and Constants

The code starts by importing various modules and constants using the importer object.

const {ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL} = importer.import('general chit chat')
const relevantHistory = importer.import('relevant llm history')
const relevantKeywords = importer.import('relevant llm keywords')
const matchingTimestamps = importer.import('relevant history timestamps')

messageHistory Function

The messageHistory function is an asynchronous function that takes three parameters: promptModel, session, and prompt.

async function messageHistory(promptModel, session, prompt) {
  //...
}

Function Body

  1. Loading History Files: The function loads relevant history files using the relevantHistory function.
let historyFiles = await relevantHistory(promptModel, session, prompt)
  1. Extracting Relevant Keywords: It extracts relevant keywords from the prompt using the relevantKeywords function.
let matching = await relevantKeywords(promptModel, session, prompt)
  1. Extracting Matching Timestamps: It extracts matching timestamps from the history files using the matchingTimestamps function.
let relevantTimestamps = await matchingTimestamps(promptModel, session, prompt, matching)
  1. Loading Conversations: It loads conversations from the ACTIVE_CONVERSATIONS object that match the current session and model.
let loadedConversations = Object.keys(ACTIVE_CONVERSATIONS)
 .filter(key => key.match('-' + DEFAULT_MODEL + '-' + session + '.json'))
loadedConversations.sort((a, b) => b - a)
  1. Building Matches List: It builds a list of matching timestamps and topics from the loaded conversations.
let matchesClosely = 'Current date:'+ (new Date).toISOString() 
  + '\nOur recent topics:\n'
for(let i = 0; i < loadedConversations.length; i++) {
  //...
}
  1. Asking User for Input: It asks the user if any of the matching timestamps match the subject very closely.
let q4 = matchesClosely + '\nDo any of these times match the subject very closely:\n' 
  + prompt + '\nOnly respond with related and unique timestamps, no explanations.'
console.log('User:'+ q4)
let a4 = await promptModel(q4)
console.log('AI:'+ a4)
  1. Filtering Matching Timestamps: It filters the matching timestamps based on the user's input.
let matchingClosely = relevantTimestamps
 .filter(time => a4.match(time) || a4.match(new Date(parseInt(time)).toISOString()))
  1. Logging Results: It logs the filtered matching timestamps.
console.log(matchesClosely)