default-memory
Classes#
DefaultMemory#
A specialized agent responsible for managing, storing, and retrieving memories within the agent system.
MemoryAgent serves as a bridge between application logic and memory storage/retrieval mechanisms. It delegates the actual memory operations to specialized recorder and retriever agents that are attached as skills. This agent doesn't directly process messages like other agents but instead provides memory management capabilities to the system.
Extends#
Indexable#
[key
: symbol
]: () => string
| () => Promise
<void
>
Constructors#
Constructor#
new DefaultMemory(
options
):DefaultMemory
Parameters#
Parameter | Type |
---|---|
|
Returns#
Overrides#
Properties#
storage#
storage:
MemoryStorage
DefaultMemoryRetriever#
Abstract base class for agents that retrieve memories from storage.
The MemoryRetriever serves as a foundation for implementing specific memory retrieval mechanisms. Implementations of this class are responsible for:
- Querying a memory storage backend to find relevant memories
- Filtering memories based on search criteria
- Limiting the number of results returned
- Potentially implementing sorting, ranking, or relevance-based retrieval
Custom implementations should extend this class and provide concrete implementations of the process method to handle the actual retrieval logic.
Extends#
Indexable#
[key
: symbol
]: () => string
| () => Promise
<void
>
Constructors#
Constructor#
new DefaultMemoryRetriever(
options
):DefaultMemoryRetriever
Parameters#
Parameter | Type |
---|---|
|
Returns#
Overrides#
`MemoryRetriever`.`constructor`
Methods#
process()#
process(
input
,options
):Promise
<`MemoryRetrieverOutput`>
Core processing method of the agent, must be implemented in subclasses
This is the main functionality implementation of the agent, processing input and generating output. Can return various types of results:
- Regular object response
- Streaming response
- Async generator
- Another agent instance (transfer agent)
Parameters#
Parameter | Type | Description |
---|---|---|
| Input message | |
| Options for agent invocation |
Returns#
Promise
<`MemoryRetrieverOutput`>
Processing result
Examples#
Example of returning a direct object:
class DirectResponseAgent extends Agent {
process(input: Message): Message {
// Process input and return a direct object response
return {
text: `Hello, I received your message: ${JSON.stringify(input)}`,
confidence: 0.95,
timestamp: new Date().toISOString(),
};
}
}
const agent = new DirectResponseAgent();
const result = await agent.invoke({ message: "Hello" });
console.log(result); // { text: "Hello, I received your message: { message: 'Hello' }", confidence: 0.95, timestamp: "2023-10-01T12:00:00Z" }
Example of returning a streaming response:
class StreamResponseAgent extends Agent {
process(_input: Message): AgentResponseStream<Message> {
// Return a ReadableStream as a streaming response
return new ReadableStream({
start(controller) {
controller.enqueue(textDelta({ text: "Hello" }));
controller.enqueue(textDelta({ text: ", " }));
controller.enqueue(textDelta({ text: "This" }));
controller.enqueue(textDelta({ text: " is" }));
controller.enqueue(textDelta({ text: "..." }));
controller.close();
},
});
}
}
const agent = new StreamResponseAgent();
const stream = await agent.invoke({ message: "Hello" }, { streaming: true });
let fullText = "";
for await (const chunk of stream) {
if (isAgentResponseDelta(chunk)) {
const text = chunk.delta.text?.text;
if (text) fullText += text;
}
}
console.log(fullText); // Output: "Hello, This is..."
Example of using an async generator:
class AsyncGeneratorAgent extends Agent {
async *process(
_input: Message,
_options: AgentInvokeOptions,
): AgentProcessAsyncGenerator<Message> {
// Use async generator to produce streaming results
yield textDelta({ message: "This" });
yield textDelta({ message: "," });
yield textDelta({ message: " " });
yield textDelta({ message: "This" });
yield textDelta({ message: " " });
yield textDelta({ message: "is" });
yield textDelta({ message: "..." });
// Optional return a JSON object at the end
return { time: new Date().toISOString() };
}
}
const agent = new AsyncGeneratorAgent();
const stream = await agent.invoke({ message: "Hello" }, { streaming: true });
const message: string[] = [];
let json: Message | undefined;
for await (const chunk of stream) {
if (isAgentResponseDelta(chunk)) {
const text = chunk.delta.text?.message;
if (text) message.push(text);
if (chunk.delta.json) json = chunk.delta.json;
}
}
console.log(message); // Output: ["This", ",", " ", "This", " ", "is", "..."]
console.log(json); // Output: { time: "2023-10-01T12:00:00Z" }
Example of transfer to another agent:
class SpecialistAgent extends Agent {
process(_input: Message): Message {
return {
response: "This is a specialist response",
expertise: "technical",
};
}
}
class MainAgent extends Agent {
process(_input: Message): Agent {
// Create a specialized agent for handling technical issues
return new SpecialistAgent();
}
}
const aigne = new AIGNE({});
const mainAgent = new MainAgent();
const result = await aigne.invoke(mainAgent, { message: "technical question" });
console.log(result); // { response: "This is a specialist response", expertise: "technical" }
Overrides#
DefaultMemoryRecorder#
Abstract base class for agents that record and store memories.
The MemoryRecorder serves as a foundation for implementing specific memory storage mechanisms. Implementations of this class are responsible for:
- Converting input content into standardized memory objects
- Assigning unique IDs to new memories
- Storing memories in an appropriate backend (database, file system, etc.)
- Ensuring proper timestamping of memories
Custom implementations should extend this class and provide concrete implementations of the process method to handle the actual storage logic.
Extends#
Indexable#
[key
: symbol
]: () => string
| () => Promise
<void
>
Constructors#
Constructor#
new DefaultMemoryRecorder(
options
):DefaultMemoryRecorder
Parameters#
Parameter | Type |
---|---|
|
Returns#
Overrides#
`MemoryRecorder`.`constructor`
Methods#
process()#
process(
input
,options
):Promise
<`MemoryRecorderOutput`>
Core processing method of the agent, must be implemented in subclasses
This is the main functionality implementation of the agent, processing input and generating output. Can return various types of results:
- Regular object response
- Streaming response
- Async generator
- Another agent instance (transfer agent)
Parameters#
Parameter | Type | Description |
---|---|---|
| Input message | |
| Options for agent invocation |
Returns#
Promise
<`MemoryRecorderOutput`>
Processing result
Examples#
Example of returning a direct object:
class DirectResponseAgent extends Agent {
process(input: Message): Message {
// Process input and return a direct object response
return {
text: `Hello, I received your message: ${JSON.stringify(input)}`,
confidence: 0.95,
timestamp: new Date().toISOString(),
};
}
}
const agent = new DirectResponseAgent();
const result = await agent.invoke({ message: "Hello" });
console.log(result); // { text: "Hello, I received your message: { message: 'Hello' }", confidence: 0.95, timestamp: "2023-10-01T12:00:00Z" }
Example of returning a streaming response:
class StreamResponseAgent extends Agent {
process(_input: Message): AgentResponseStream<Message> {
// Return a ReadableStream as a streaming response
return new ReadableStream({
start(controller) {
controller.enqueue(textDelta({ text: "Hello" }));
controller.enqueue(textDelta({ text: ", " }));
controller.enqueue(textDelta({ text: "This" }));
controller.enqueue(textDelta({ text: " is" }));
controller.enqueue(textDelta({ text: "..." }));
controller.close();
},
});
}
}
const agent = new StreamResponseAgent();
const stream = await agent.invoke({ message: "Hello" }, { streaming: true });
let fullText = "";
for await (const chunk of stream) {
if (isAgentResponseDelta(chunk)) {
const text = chunk.delta.text?.text;
if (text) fullText += text;
}
}
console.log(fullText); // Output: "Hello, This is..."
Example of using an async generator:
class AsyncGeneratorAgent extends Agent {
async *process(
_input: Message,
_options: AgentInvokeOptions,
): AgentProcessAsyncGenerator<Message> {
// Use async generator to produce streaming results
yield textDelta({ message: "This" });
yield textDelta({ message: "," });
yield textDelta({ message: " " });
yield textDelta({ message: "This" });
yield textDelta({ message: " " });
yield textDelta({ message: "is" });
yield textDelta({ message: "..." });
// Optional return a JSON object at the end
return { time: new Date().toISOString() };
}
}
const agent = new AsyncGeneratorAgent();
const stream = await agent.invoke({ message: "Hello" }, { streaming: true });
const message: string[] = [];
let json: Message | undefined;
for await (const chunk of stream) {
if (isAgentResponseDelta(chunk)) {
const text = chunk.delta.text?.message;
if (text) message.push(text);
if (chunk.delta.json) json = chunk.delta.json;
}
}
console.log(message); // Output: ["This", ",", " ", "This", " ", "is", "..."]
console.log(json); // Output: { time: "2023-10-01T12:00:00Z" }
Example of transfer to another agent:
class SpecialistAgent extends Agent {
process(_input: Message): Message {
return {
response: "This is a specialist response",
expertise: "technical",
};
}
}
class MainAgent extends Agent {
process(_input: Message): Agent {
// Create a specialized agent for handling technical issues
return new SpecialistAgent();
}
}
const aigne = new AIGNE({});
const mainAgent = new MainAgent();
const result = await aigne.invoke(mainAgent, { message: "technical question" });
console.log(result); // { response: "This is a specialist response", expertise: "technical" }
Overrides#
Interfaces#
DefaultMemoryOptions#
Extends#
Partial
<`MemoryAgentOptions`>.Omit
<DefaultMemoryRecorderOptions
,"storage"
| keyof `AgentOptions`>.Omit
<DefaultMemoryRetrieverOptions
,"storage"
| keyof `AgentOptions`>
Properties#
Property | Type |
---|---|
|
|
DefaultMemoryRetrieverOptions#
Configuration options for an agent
Extends#
Properties#
Property | Type |
---|---|
|
|
|
|
|
|
|
|
|
|
| ( |
| ( |
| ( |
DefaultMemoryRecorderOptions#
Configuration options for an agent
Extends#
Properties#
Property | Type |
---|---|
|
|
|
|
|
|