Agents Memory Client
Classes
Classes
AgentMemoryClient
Agent Memory Client
Manages conversation memory using Redis Agent Memory Server (AMS)
Constructor
constructor(config: AgentMemoryClientConfig)
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
config | AgentMemoryClientConfig | Yes | - Client configuration |
Methods
healthCheck
Check if Agent Memory Server is healthy
healthCheck(): Promise<boolean>
Returns:
Promise<boolean> - Promise<boolean> - True if healthy, false otherwise
readWorkingMemory
Read working memory from AMS
readWorkingMemory(sessionId: string, namespace?: string | undefined): Promise<AmsMemory | null>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
sessionId | string | Yes | - Session ID to retrieve memory for |
namespace | string | undefined | No | - Optional namespace (defaults to configured defaultNamespace) |
Returns:
Promise<AmsMemory \| null> - Promise<AmsMemory | null> - Memory object or null if not found
replaceWorkingMemory
Replace working memory in AMS
Uses PUT /v1/working-memory/{session_id}
replaceWorkingMemory(sessionId: string, contextWindowMax: number, memory: AmsMemory, namespace?: string | undefined): Promise<AmsMemory | null>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
sessionId | string | Yes | - Session ID to update |
contextWindowMax | number | Yes | - Maximum context window size in tokens |
memory | AmsMemory | Yes | - Memory object to store |
namespace | string | undefined | No | - Optional namespace (defaults to configured defaultNamespace) |
Returns:
Promise<AmsMemory \| null> - Promise<AmsMemory | null> - Updated memory object or null on failure
removeWorkingMemory
Remove working memory from AMS
Uses DELETE /v1/working-memory/{session_id}?namespace=xxx
removeWorkingMemory(sessionId: string, namespace?: string | undefined): Promise<boolean>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
sessionId | string | Yes | - Session ID to remove |
namespace | string | undefined | No | - Optional namespace (defaults to configured defaultNamespace) |
Returns:
Promise<boolean> - Promise<boolean> - True if removed successfully, false otherwise
addMessage
Add a message to AMS and get updated memory
This method:
- GETs existing memory for the session
- Appends the new message to the messages array
- PUTs the updated memory back to AMS
AMS will automatically summarize if context window is exceeded.
addMessage(sessionId: string, namespace: string, role: AmsRole, content: string, contextWindowMax?: number | undefined): Promise<AmsMemory | null>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
sessionId | string | Yes | - Session ID to add message to |
namespace | string | Yes | - Namespace for this session |
role | AmsRole | Yes | - Role of the message (user, assistant, system) |
content | string | Yes | - Content of the message |
contextWindowMax | number | undefined | No | - Optional context window max (uses config default if not provided) |
Returns:
Promise<AmsMemory \| null> - Promise<AmsMemory | null> - Updated memory object or null on failure
getMemoryPrompt
Get memory-enhanced prompt ready for LLM
Uses AMS Memory Prompt API to build messages with conversation context POST /v1/memory/prompt
getMemoryPrompt(params: MemoryPromptRequest): Promise<MemoryPromptResponse | null>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
params | MemoryPromptRequest | Yes | - Memory prompt parameters |
Returns:
Promise<MemoryPromptResponse \| null> - Promise<MemoryPromptResponse | null> - Messages array or null on failure
listSessions
List all sessions for a namespace
GET /v1/working-memory?namespace=xxx
listSessions(namespace: string, options?: ListSessionsOptions | undefined): Promise<SessionListResponse>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
namespace | string | Yes | - Namespace to list sessions for |
options | ListSessionsOptions | undefined | No | - Optional filtering and pagination |
Returns:
Promise<SessionListResponse> - Promise<SessionListResponse> - List of sessions
createLongTermMemories
Create one or more long-term memories
POST /v1/long-term-memory/
createLongTermMemories(memories: CreateLongTermMemoryParams[]): Promise<LongTermMemory[]>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
memories | CreateLongTermMemoryParams[] | Yes | - Array of memories to create |
Returns:
Promise<LongTermMemory[]> - Promise<LongTermMemory[]> - Created memories
createLongTermMemory
Create a single long-term memory
Convenience wrapper around createLongTermMemories
createLongTermMemory(params: CreateLongTermMemoryParams): Promise<LongTermMemory | null>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
params | CreateLongTermMemoryParams | Yes | - Memory parameters |
Returns:
Promise<LongTermMemory \| null> - Promise<LongTermMemory | null> - Created memory or null on failure
searchLongTermMemory
Search long-term memories
POST /v1/long-term-memory/search
searchLongTermMemory(params: SearchLongTermMemoryParams): Promise<SearchLongTermMemoryResponse>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
params | SearchLongTermMemoryParams | Yes | - Search parameters |
Returns:
Promise<SearchLongTermMemoryResponse> - Promise<SearchLongTermMemoryResponse> - Search results
forgetMemories
Forget (delete) long-term memories based on policy
POST /v1/long-term-memory/forget
forgetMemories(params: ForgetMemoriesParams): Promise<ForgetMemoriesResponse>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
params | ForgetMemoriesParams | Yes | - Forget parameters with policy and filters |
Returns:
Promise<ForgetMemoriesResponse> - Promise<ForgetMemoriesResponse> - Deletion results
deleteLongTermMemory
Delete a specific long-term memory by ID
Convenience method that uses forgetMemories with a specific memory ID filter
deleteLongTermMemory(memoryId: string, namespace: string): Promise<boolean>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
memoryId | string | Yes | - ID of the memory to delete |
namespace | string | Yes | - Namespace the memory belongs to |
Returns:
Promise<boolean> - Promise<boolean> - True if deleted successfully
searchAllMemory
Search across all memory types (working memory and long-term memory)
Performs parallel searches across both memory stores and combines results. For working memory, performs client-side text matching on session messages. For long-term memory, uses semantic vector search via AMS API.
searchAllMemory(params: SearchAllMemoryParams): Promise<SearchAllMemoryResponse>
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
params | SearchAllMemoryParams | Yes | - Search parameters |
Returns:
Promise<SearchAllMemoryResponse> - Promise<SearchAllMemoryResponse> - Combined search results
Examples:
const results = await client.searchAllMemory({
query: 'user preferences for dark mode',
namespace: 'org-123',
includeLongTermMemory: true,
includeWorkingMemory: true,
limit: 10,
});
results.results.forEach(result => {
console.log(`[${result.source}] ${result.text} (score: ${result.score})`);
});
buildContextPrompt
Build a formatted context prompt from memory
Useful for constructing prompts with conversation history
buildContextPrompt(memory: AmsMemory): string
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
memory | AmsMemory | Yes | - Memory object from AMS |
Returns:
string - string - Formatted context prompt
getConfig
Get the current configuration
getConfig(): Readonly<Required<Omit<AgentMemoryClientConfig, "authToken">> & Pick<AgentMemoryClientConfig, "authToken">>
Returns:
Readonly<Required<Omit<AgentMemoryClientConfig, "authToken">> & Pick<AgentMemoryClientConfig, "authToken">> - AgentMemoryClientConfig - Current configuration
Examples:
const client = new AgentMemoryClient({
baseUrl: 'http://agent-memory-server:8000',
contextWindowMax: 10000,
});
// Check health
const isHealthy = await client.healthCheck();
// Add a message
const memory = await client.addMessage(
'session-123',
'default',
AmsRole.USER,
'Hello, how are you?'
);
// Read working memory
const currentMemory = await client.readWorkingMemory('session-123', 'default');