List Manipulation

Adjusting a WorkingMemory

WorkingMemory is a container for memories with functions for immutable transformation.

WorkingMemory instances are immutable. This means that once an instance is created, it cannot be modified. Instead of changing the existing instance, methods that alter the state of the WorkingMemory (such as withMemory, map, filter, etc.) return a new instance of WorkingMemory with the modifications applied. This approach helps ensure safety and predictability, especially in asynchronous environments.

CognitiveSteps are used to create new WorkingMemory instances using an LLM, and have their own documentation.

WorkingMemory provide a variety of methods to manipulate memories and return a new WorkingMemory.

  • withMemory(memory) - adds a new memory to the WorkingMemory and returns a new instance
  • slice(begin, end) - returns a new WorkingMemory with memories sliced between the begin and end indices.
  • splice(start, deleteCount, ...replacementItems) - returns a new WorkingMemory with memories spliced and (optionally, replaced).
  • map(callback) - applies a callback function to each memory, returning a new WorkingMemory with the results.
  • asyncMap(callback) - Applies a provided asynchronous function to each memory, producing a new WorkingMemory instance with the results.
  • filter(callback) - filters memories based on a callback function, returning a new WorkingMemory with the filtered results.
  • find(callback) - returns the first memory that satisfies the provided testing function, encapsulated in a new WorkingMemory.
  • concat(mem: WorkingMemory | Memory[]) - merges two instances of WorkingMemory into one, returning a new instance.
  • prepend(otherWorkingMemory) - Prepends the memories of another WorkingMemory (or an array of Memory objects) to the memories of the current WorkingMemory instance, returning a new instance.
  • withMonologue(content) - Adds a memory with the role of Assistant and the provided content to the WorkingMemory, returning a new instance.
  • withRegion(regionName, ...memories) - Adds or replaces a set of memories marked as being a part of a region in the WorkingMemory
  • orderRegions(...regionNames) - Reorders the regions of the WorkingMemory (the 'default' region will match all memories without a region).
  • withoutRegions(...regionNames) - Returns a WorkingMemory with the memories from the regionNames listed removed. (see region)

Constructor and Initialization Options

To create a new instance of WorkingMemory, you can use its constructor with the following options:

interface WorkingMemoryInitOptions {
  soulName: string;
  memories?: InputMemory[];
  processor?: ProcessorSpecification;
  postCloneTransformation?: (workingMemory: WorkingMemory) => WorkingMemory;
}
  • soulName: A unique identifier for the soul (or context) the memory belongs to.
  • memories: An optional array of InputMemory objects to initialize the working memory with.

Example:

const workingMemory = new WorkingMemory({
  soulName: "ExampleSoul",
  memories: [{ role: "User", content: "Hello, world!" }],
});