Package com.tnsai.memory


package com.tnsai.memory
Memory management and persistence for agent conversation history.

This package provides abstractions and implementations for storing and retrieving agent memory, including conversation history and contextual information.

Memory Architecture

 ┌─────────────────────────────────────────────────────────────┐
 │                      MemoryStore                             │
 │                    (Interface)                               │
 └──────────────────────────┬──────────────────────────────────┘
                            │
           ┌────────────────┼────────────────┐
           │                │                │
           ▼                ▼                ▼
    ┌─────────────┐  ┌─────────────┐  ┌─────────────┐
    │InMemoryStore│  │FileMemory   │  │ (Custom)    │
    │  (Default)  │  │   Store     │  │             │
    └─────────────┘  └─────────────┘  └─────────────┘
 

Memory Store Implementations

ImplementationPersistenceUse Case
InMemoryStoreVolatile (lost on restart)Development, stateless agents
FileMemoryStoreFile-basedSimple persistence

Usage Example


 // Default in-memory store (agent uses this by default)
 public class MyAgent extends Agent {
     // Uses InMemoryStore automatically
 }

 // Custom memory store
 public class PersistentAgent extends Agent {
     @Override
     protected MemoryStore getMemoryStore() {
         return new FileMemoryStore("./agent-memory.json");
     }
 }

 // Direct usage
 MemoryStore store = new InMemoryStore();
 store.add(ChatMessage.user("Hello"));
 store.add(ChatMessage.assistant("Hi there!"));
 List<ChatMessage> history = store.getHistory();
 

Memory Operations

  • add: Add a message to memory
  • getHistory: Retrieve conversation history
  • clear: Clear all stored messages
  • getLastN: Get the last N messages

Memory Context Window

Memory stores can be configured to limit the number of messages retained, helping manage LLM context window limits.

Thread Safety

All built-in memory store implementations are thread-safe, using appropriate synchronization for concurrent access.

See Also: