Skip to content

Long term memory tool#19

Open
sharananurag998 wants to merge 3 commits into
mainfrom
vk/317b-session-me
Open

Long term memory tool#19
sharananurag998 wants to merge 3 commits into
mainfrom
vk/317b-session-me

Conversation

@sharananurag998
Copy link
Copy Markdown
Collaborator

No description provided.

I've successfully implemented a **Session Memory Tool** that provides lightweight key-value storage for storing and recalling data during a session. Here's what was created:

### 📁 Files Created:

1. **`src/adk/tools/sessionMemoryTool.ts`** - Core implementation with 5 memory operations:
   - `storeMemoryTool()` - Store key-value pairs with overwrite protection
   - `recallMemoryTool()` - Recall values with default fallback
   - `listMemoryKeysTool()` - List keys with optional pattern filtering
   - `deleteMemoryTool()` - Delete specific keys
   - `clearMemoryTool()` - Clear all session memory

2. **`examples/session_memory.ts`** - Comprehensive example showing:
   - Storing user preferences (currency: INR)
   - Using stored preferences in tool execution
   - Multi-step shopping cart workflow
   - Complex data structure storage

3. **`test-session-memory.js`** - Test suite validating all features

### ✨ Key Features:

- **Session Isolation**: Each session has its own memory space
- **Persistence**: Optionally saves to disk (`.session-memory.json`)
- **Type Safety**: Full TypeScript support
- **Overwrite Protection**: Prevents accidental data loss
- **Pattern Matching**: Filter keys by regex patterns
- **Default Values**: Fallback for missing keys
- **Complex Data**: Supports objects, arrays, and nested structures

### 🎯 Example Usage:

The example demonstrates a practical multi-step workflow where:
1. User preference for INR currency is stored
2. Currency converter automatically uses the stored preference
3. Shopping cart maintains state across operations
4. Total is calculated in the user's preferred currency

All tests pass successfully, demonstrating the tool works as expected!
I've successfully created a **core session memory tool** in `src/tools/sessionMemoryTool.ts` that provides lightweight key-value storage during sessions.

### 📁 Files Created:

1. **`src/tools/sessionMemoryTool.ts`** - Core tool implementation with 5 memory operations:
   - `storeMemoryTool` - Store with overwrite protection
   - `recallMemoryTool` - Recall with default fallback
   - `listMemoryKeysTool` - List keys with pattern filtering
   - `deleteMemoryTool` - Delete specific keys
   - `clearMemoryTool` - Clear all session memory

2. **`examples/session_memory.ts`** - Comprehensive example demonstrating:
   - Storing preferences (currency: INR)
   - Using preferences in tool execution
   - Multi-step shopping cart workflow
   - Complex data structures

### 🎯 Key Features:

- **Core Tool Interface**: Uses the standard `Tool<A, Ctx>` type from `src/core/types.ts`
- **Session Isolation**: Each session has independent memory
- **Persistence**: Saves to `.session-memory.json`
- **Type Safety**: Full TypeScript with Zod schemas
- **JSON Returns**: All tools return JSON strings as per core tool pattern

### ✨ Example Usage:
```typescript
// Store preference
await storeMemoryTool.execute(
  { key: 'preferred_currency', value: 'INR' },
  { sessionId: 'demo-001' }
);

// Recall later
const result = await recallMemoryTool.execute(
  { key: 'preferred_currency' },
  { sessionId: 'demo-001' }
);
// Returns: { value: 'INR', ... }
```

All tests pass successfully! The tool is ready for integration into JAF agents.
I've successfully created an enhanced session memory tool with **vector database support** and **hybrid search capabilities**!

### 📁 Files Created:

1. **`src/tools/sessionMemoryVectorTool.ts`** - Enhanced implementation with:
   - **Vector DB backends**: FAISS/Vectra support (configurable)
   - **Hybrid search**: Combines keyword and semantic similarity
   - **6 core tools**:
     - `storeFactTool` - Store facts with embeddings
     - `searchFactsTool` - Hybrid search with configurable strategy
     - `findSimilarFactsTool` - Find semantically similar facts
     - `listFactsTool` - List with category/tag filtering
     - `deleteFactTool` - Remove facts
     - `getMemoryStatsTool` - Memory statistics

2. **`examples/session_memory_vector.ts`** - Comprehensive demo showing:
   - Storing facts with categories and tags
   - Semantic search demonstrations
   - Finding similar facts
   - Comparing search strategies (keyword vs semantic vs hybrid)
   - Backend switching (JSON vs Vectra)

### 🎯 Key Features:

#### **Configurable Storage Backends:**
- **JSON** - Simple file storage (keyword search only)
- **VECTRA** - Local vector index with embeddings
- **FAISS** - High-performance vector similarity (ready to integrate)

#### **Search Strategies:**
- **Keyword** - Traditional text matching
- **Semantic** - Vector similarity search
- **Hybrid** - Weighted combination (configurable alpha)

#### **Configuration Options:**
```typescript
configureVectorMemory({
  backend: StorageBackend.VECTRA,
  searchStrategy: SearchStrategy.HYBRID,
  embeddingModel: 'Xenova/all-MiniLM-L6-v2',
  hybridAlpha: 0.6, // 0=keyword, 1=semantic
  vectorDimensions: 384
});
```

### ✨ Usage Example:
```typescript
// Store a fact with embedding
await storeFactTool.execute({
  key: 'react-framework',
  value: { framework: 'React', company: 'Meta' },
  text: 'React is a JavaScript library for building UIs',
  category: 'programming',
  tags: ['javascript', 'frontend']
}, context);

// Semantic search
const results = await searchFactsTool.execute({
  query: 'web development frontend',
  strategy: SearchStrategy.HYBRID,
  limit: 5
}, context);

// Find similar facts
const similar = await findSimilarFactsTool.execute({
  key: 'react-framework',
  limit: 3
}, context);
```

### 🚀 Performance:
- **Embeddings**: Uses Xenova/transformers for local embeddings (no API needed)
- **Vector Index**: Vectra provides fast local similarity search
- **Hybrid Search**: Balances precision (keyword) with recall (semantic)
- **Session Isolation**: Each session has independent vector index

The tool successfully passes all tests and provides a powerful, configurable system for storing and retrieving facts with both keyword and semantic search capabilities!
@sharananurag998 sharananurag998 changed the title Long term memory tol Long term memory tool Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants