Skip to main content

TDAILongTermMemory

Enterprise-grade long-term memory implementation using TDAI services for cloud-based persistent storage with basic semantic search capabilities.

Overview

TDAILongTermMemory provides reliable long-term memory capabilities with:
  • Strategy-based Organization: Categorize memories by type and purpose
  • Cloud Persistence: Reliable storage with TDAI infrastructure
  • Basic Semantic Search: Content-based retrieval capabilities
  • Batch Operations: Efficient bulk memory operations
  • Enterprise Integration: Compatible with existing TDAI deployments

Configuration

import { TDAILongTermMemory, MemoryClient } from '@ag-kit/agents/storage';

// Initialize with TDAI client
const client = new MemoryClient({
  apiKey: 'your-tdai-api-key',
  baseUrl: 'https://api.tdai.com'
});

const longTermMemory = new TDAILongTermMemory({
  client: client,
  sessionId: 'user-session-123'
});

Configuration Parameters

config
TDAILongTermMemoryConfig
required
TDAI configuration object

Core Interface Methods

TDAILongTermMemory implements the standard long-term memory interface methods. For complete API documentation including record(), recordBatch(), retrieve(), delete(), update(), clear(), and other methods, see the Memory Service API Reference.

Usage Examples

Basic Memory Operations

// Record memory with strategy categorization
await longTermMemory.record({
  id: 'mem-1',
  content: 'User is interested in machine learning and AI',
  strategy: 'interests',
  metadata: {
    confidence: 0.8,
    source: 'conversation',
    tags: ['ml', 'ai', 'technology']
  },
  createdAt: new Date()
});

// Batch record multiple memories
const memories = [
  { 
    id: 'mem-2',
    content: 'User prefers email notifications', 
    strategy: 'preferences', 
    metadata: { source: 'settings' },
    createdAt: new Date()
  },
  { 
    id: 'mem-3',
    content: 'User works in healthcare industry', 
    strategy: 'profile', 
    metadata: { confidence: 0.9 },
    createdAt: new Date()
  }
];
await longTermMemory.recordBatch(memories);

Memory Retrieval

// Retrieve memories with filtering
const profileMemories = await longTermMemory.retrieve({
  strategy: 'profile',
  limit: 10,
  orderBy: { createdAt: 'desc' }
});

// Semantic search using TDAI's search capabilities
const searchResults = await longTermMemory.semanticSearch('user preferences', {
  limit: 5,
  strategy: ['preferences', 'settings'],
  threshold: 0.6
});

// Complex filtering
const recentMemories = await longTermMemory.retrieve({
  strategy: ['interests', 'preferences'],
  limit: 20,
  filters: { 
    confidence: { $gte: 0.7 },
    createdAt: { $gte: new Date('2024-01-01') }
  }
});

Memory Management

// Update memory content
await longTermMemory.update('mem-1', {
  content: 'User is very interested in machine learning and deep learning',
  metadata: { 
    confidence: 0.9, 
    updated_reason: 'reinforced_interest',
    updatedAt: new Date()
  }
});

// Get related memories (basic similarity-based)
const relatedMemories = await longTermMemory.getRelatedMemories('mem-1', 2);

// Clear memories by strategy
await longTermMemory.clear('temporary');

// Delete specific memory
await longTermMemory.delete('mem-2');

// Delete by query
await longTermMemory.delete({
  strategy: 'session_temp',
  filters: { createdAt: { $lt: new Date('2024-01-01') } }
});

Features

Strategy-Based Organization

  • Categorize memories by type (preferences, facts, interests, etc.)
  • Filter and retrieve by strategy
  • Organize memories for different use cases

Cloud Persistence

  • Reliable storage with TDAI infrastructure
  • Automatic backup and recovery
  • Cross-session persistence
  • Enterprise-grade security

Basic Search Capabilities

  • Content-based similarity search
  • Strategy filtering
  • Metadata-based queries
  • Sorting and pagination

Batch Operations

  • Efficient bulk recording
  • Batch updates and deletions
  • Optimized for large datasets

Limitations

Manual Memory Extraction

Unlike Mem0LongTermMemory, TDAI implementation requires manual memory extraction:
// Manual extraction example
function extractMemoriesFromConversation(messages: Message[]): MemoryEntity[] {
  const memories: MemoryEntity[] = [];
  
  for (const message of messages) {
    if (message.role === 'user') {
      // Custom logic to identify important information
      if (message.content.includes('prefer') || message.content.includes('like')) {
        memories.push({
          id: `mem-${Date.now()}`,
          strategy: 'preferences',
          content: message.content,
          metadata: { source: 'conversation', confidence: 0.7 },
          createdAt: new Date()
        });
      }
    }
  }
  
  return memories;
}

// Use manual extraction
const messages = await shortTermMemory.list();
const extractedMemories = extractMemoriesFromConversation(messages);
await longTermMemory.recordBatch(extractedMemories);

Basic Consolidation

  • No automatic memory consolidation
  • Manual deduplication required
  • Limited relationship mapping

Search Limitations

  • Basic semantic search capabilities
  • No advanced vector similarity
  • Limited graph relationship support

Best Practices

  1. Define Clear Strategies: Use consistent strategy names across your application
  2. Include Metadata: Add confidence scores, sources, and tags for better organization
  3. Regular Cleanup: Implement periodic cleanup of outdated memories
  4. Manual Extraction: Develop robust logic for extracting important information
  5. Batch Operations: Use batch methods for better performance with large datasets
  6. Error Handling: Implement proper error handling for network operations

Integration with Short-term Memory

import { InMemoryMemory, TDAILongTermMemory } from '@ag-kit/agents/storage';

class MemoryManager {
  private shortTerm: InMemoryMemory;
  private longTerm: TDAILongTermMemory;

  constructor() {
    this.shortTerm = new InMemoryMemory();
    this.longTerm = new TDAILongTermMemory({ /* config */ });
  }

  async processConversation(sessionId: string) {
    // Get recent conversation
    const recentMessages = await this.shortTerm.list({ 
      sessionId, 
      limit: 50 
    });

    // Extract important memories manually
    const importantMemories = this.extractImportantInfo(recentMessages);

    // Store in long-term memory
    if (importantMemories.length > 0) {
      await this.longTerm.recordBatch(importantMemories);
    }

    // Retrieve relevant context for next conversation
    const context = await this.longTerm.semanticSearch('user context', {
      limit: 10,
      threshold: 0.6
    });

    return context;
  }

  private extractImportantInfo(messages: any[]): MemoryEntity[] {
    // Implement your extraction logic here
    return [];
  }
}