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.
CognitiveStep
s 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 theWorkingMemory
and returns a new instanceslice(begin, end)
- returns a newWorkingMemory
with memories sliced between the begin and end indices.splice(start, deleteCount, ...replacementItems)
- returns a newWorkingMemory
with memories spliced and (optionally, replaced).map(callback)
- applies a callback function to each memory, returning a newWorkingMemory
with the results.asyncMap(callback)
- Applies a provided asynchronous function to each memory, producing a newWorkingMemory
instance with the results.filter(callback)
- filters memories based on a callback function, returning a newWorkingMemory
with the filtered results.find(callback)
- returns the first memory that satisfies the provided testing function, encapsulated in a newWorkingMemory
.concat(mem: WorkingMemory | Memory[])
- merges two instances ofWorkingMemory
into one, returning a new instance.prepend(otherWorkingMemory)
- Prepends the memories of anotherWorkingMemory
(or an array of Memory objects) to the memories of the currentWorkingMemory
instance, returning a new instance.withMonologue(content)
- Adds a memory with the role of Assistant and the provided content to theWorkingMemory
, returning a new instance.withRegion(regionName, ...memories)
- Adds or replaces a set of memories marked as being a part of a region in theWorkingMemory
orderRegions(...regionNames)
- Reorders the regions of theWorkingMemory
(the 'default' region will match all memories without a region).withoutRegions(...regionNames)
- Returns aWorkingMemory
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 ofInputMemory
objects to initialize the working memory with.
Example:
const workingMemory = new WorkingMemory({
soulName: "ExampleSoul",
memories: [{ role: "User", content: "Hello, world!" }],
});