WorkingMemory Regions

WorkingMemory has the concept of memory regions which are a light weight way to update and order working memory in manipulable sections (think core memory, or conversational summary, etc).

WorkingMemory regions
+--------------------+
|      Core          |
+--------------------+
|     Summary        |
+--------------------+
|     default        |
+--------------------+

A simple example is keeping the core region at the front of WorkingMemory updated:

let workingMemory = new WorkingMemory({ soulName: 'Alice' })
 
workingMemory = workingMemory.withMemory({
  role: ChatMessageRoleEnum.User,
  content: "Hello!",
})
 
let withCore = workingMemory.withRegion("core", {
  role: ChatMessageRoleEnum.core,
  content: "You are a helpful assistant",
})
// withCore will now have 2 working memories, with "core" at the beginning
// of the memories list (since regions default to the beginning)
 
withCore = workingMemory.withRegion("core", {
  role: ChatMessageRoleEnum.core,
  content: "You are a an evil henchman",
})
// now the helpful assistant message has been replaced by the evil henchman memory

Ordering Regions

Keep your various regions ordered using the orderRegions function. Any memories not marked as part of a region are part of the 'default' region.

let workingMemory = new WorkingMemory({ soulName: 'Alice' })
 
workingMemory = workingMemory.withMemory({
  role: ChatMessageRoleEnum.User,
  content: `Interlocutor said: "Hello!"`,
})
 
const withCore = workingMemory.withRegion("core", {
  role: ChatMessageRoleEnum.core,
  content: "You are a helpful assistant",
})
// withcore will now have 2 working memories, with "core" at the
// beginning of the memories list (since regions default to the beginning)
 
const withSummary = withCore.withRegion("summary", {
  role: ChatMessageRoleEnum.Assistant,
  content: "This is a summary",
})
// summary is added to the front of the working memory by default too
// now the memory would look like:
/**
WorkingMemory regions
+--------------------+
|     summary        |
+--------------------+
|      core          |
+--------------------+
|     default        |
+--------------------+
*/
 
 
const orderedMemory = withSummary.orderRegions("core", "summary", "default")
// the orderedMemory WorkingMemory now has the core region first, followed by summary, followed by the Hello!
/**
WorkingMemory regions
+--------------------+
|      core          |
+--------------------+
|      summary       |
+--------------------+
|     default        |
+--------------------+
*/
 
 
const reverseOrderd = withSummary.orderRegios("default", "summary", "core")
// now reverseOrderd starts with the hello, followed by summary and the last memory is the core message.
/**
WorkingMemory regions
+--------------------+
|      default       |
+--------------------+
|      summary       |
+--------------------+
|        core        |
+--------------------+
*/
 

Removing Regions

You can remove entire regions of the memory using the removeRegions function.

let workingMemory = new WorkingMemory({ soulName: 'Alice' })
workingMemory = workingMemory.withMonologue("Alice thought to herself about flowers.")
 
const withImpulses = workingMemory.withRegion("impulses", {
  role: ChatMessageRoleEnum.System,
  content: `Alice thought: "I like to tip objects off of high counter-tops"`,
})
const withoutImpulses = workingMemory.withoutRegions("impulses")
 
// withoutImpulses is now a working memory with only the monologue in it