import React, { useState, useEffect } from 'react'; import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from './ui/card'; import { Button } from './ui/button'; import { Badge } from './ui/badge'; import { Input } from './ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select'; import { Tabs, TabsContent, TabsList, TabsTrigger } from './ui/tabs'; import { Alert, AlertDescription, AlertTitle } from './ui/alert'; import { Progress } from './ui/progress'; import { Separator } from './ui/separator'; import { Database, Upload, Download, Search, Filter, Trash2, Clock, HardDrive, Brain, Zap, Shield, TrendingUp } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; import { useWallet } from '@/hooks/use-wallet'; interface MemoryRecord { cid: string; agentId: string; memoryType: string; priority: string; version: number; timestamp: string; size: number; tags: string[]; accessCount: number; lastAccessed: string; expiresAt?: string; parentCid?: string; verified: boolean; } interface MemoryStats { totalMemories: number; totalSizeBytes: number; totalSizeMB: number; byType: Record; byPriority: Record; totalAccessCount: number; averageAccessCount: number; agentCount: number; } const MemoryManager: React.FC = () => { const { toast } = useToast(); const { isConnected, address } = useWallet(); const [memories, setMemories] = useState([]); const [filteredMemories, setFilteredMemories] = useState([]); const [stats, setStats] = useState(null); const [searchQuery, setSearchQuery] = useState(''); const [selectedType, setSelectedType] = useState('all'); const [selectedPriority, setSelectedPriority] = useState('all'); const [loading, setLoading] = useState(true); const [activeTab, setActiveTab] = useState('memories'); // Mock data for demonstration const mockMemories: MemoryRecord[] = [ { cid: 'QmAbc123...', agentId: 'agent_001', memoryType: 'experience', priority: 'high', version: 3, timestamp: '2024-01-15T10:30:00Z', size: 2048576, tags: ['training', 'nlp', 'conversation'], accessCount: 45, lastAccessed: '2024-01-20T14:15:00Z', verified: true }, { cid: 'QmDef456...', agentId: 'agent_001', memoryType: 'policy_weights', priority: 'critical', version: 7, timestamp: '2024-01-18T09:45:00Z', size: 1048576, tags: ['model', 'weights', 'reinforcement'], accessCount: 128, lastAccessed: '2024-01-22T16:30:00Z', verified: true }, { cid: 'QmGhi789...', agentId: 'agent_002', memoryType: 'knowledge_graph', priority: 'medium', version: 1, timestamp: '2024-01-20T11:20:00Z', size: 5242880, tags: ['knowledge', 'graph', 'vision'], accessCount: 23, lastAccessed: '2024-01-21T13:45:00Z', verified: false }, { cid: 'QmJkl012...', agentId: 'agent_002', memoryType: 'training_data', priority: 'low', version: 2, timestamp: '2024-01-22T08:15:00Z', size: 10485760, tags: ['data', 'images', 'classification'], accessCount: 8, lastAccessed: '2024-01-22T08:15:00Z', expiresAt: '2024-02-22T08:15:00Z', verified: true } ]; const mockStats: MemoryStats = { totalMemories: 4, totalSizeBytes: 18874368, totalSizeMB: 18.0, byType: { 'experience': 1, 'policy_weights': 1, 'knowledge_graph': 1, 'training_data': 1 }, byPriority: { 'critical': 1, 'high': 1, 'medium': 1, 'low': 1 }, totalAccessCount: 204, averageAccessCount: 51, agentCount: 2 }; useEffect(() => { // Load mock data setTimeout(() => { setMemories(mockMemories); setFilteredMemories(mockMemories); setStats(mockStats); setLoading(false); }, 1000); }, []); useEffect(() => { filterMemories(); }, [searchQuery, selectedType, selectedPriority, memories]); const filterMemories = () => { let filtered = memories.filter(memory => { // Search query filter if (searchQuery) { const query = searchQuery.toLowerCase(); const matchesSearch = memory.cid.toLowerCase().includes(query) || memory.memoryType.toLowerCase().includes(query) || memory.tags.some(tag => tag.toLowerCase().includes(query)) || memory.agentId.toLowerCase().includes(query); if (!matchesSearch) return false; } // Type filter if (selectedType !== 'all' && memory.memoryType !== selectedType) { return false; } // Priority filter if (selectedPriority !== 'all' && memory.priority !== selectedPriority) { return false; } return true; }); setFilteredMemories(filtered); }; const handleDownload = async (memory: MemoryRecord) => { if (!isConnected) { toast({ title: "Wallet Not Connected", description: "Please connect your wallet to download memories", variant: "destructive" }); return; } try { // Simulate download process toast({ title: "Download Started", description: `Downloading memory ${memory.cid}...`, variant: "default" }); // Simulate download completion setTimeout(() => { toast({ title: "Download Complete", description: `Memory ${memory.cid} downloaded successfully`, variant: "default" }); }, 2000); } catch (error) { toast({ title: "Download Failed", description: "There was an error downloading the memory", variant: "destructive" }); } }; const handleDelete = async (memory: MemoryRecord) => { if (!isConnected) { toast({ title: "Wallet Not Connected", description: "Please connect your wallet to delete memories", variant: "destructive" }); return; } try { // Remove from local state setMemories(prev => prev.filter(m => m.cid !== memory.cid)); toast({ title: "Memory Deleted", description: `Memory ${memory.cid} has been deleted`, variant: "default" }); } catch (error) { toast({ title: "Delete Failed", description: "There was an error deleting the memory", variant: "destructive" }); } }; const getPriorityColor = (priority: string) => { switch (priority) { case 'critical': return 'bg-red-500'; case 'high': return 'bg-orange-500'; case 'medium': return 'bg-yellow-500'; case 'low': return 'bg-green-500'; default: return 'bg-gray-500'; } }; const getTypeIcon = (type: string) => { switch (type) { case 'experience': return ; case 'policy_weights': return ; case 'knowledge_graph': return ; case 'training_data': return ; default: return ; } }; const formatSize = (bytes: number) => { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; const memoryTypes = Array.from(new Set(memories.map(m => m.memoryType))); const priorities = ['critical', 'high', 'medium', 'low']; return (

Memory Manager

Manage and monitor your agent's persistent memory storage

{stats?.totalMemories || 0} Memories {stats?.totalSizeMB.toFixed(1) || 0} MB
Memories Statistics Settings {/* Search and Filters */} Search & Filter
setSearchQuery(e.target.value)} className="w-full" />
{/* Memory List */} {loading ? (

Loading memories...

) : filteredMemories.length === 0 ? ( No memories found Try adjusting your search criteria or filters to find memories. ) : (
{filteredMemories.map((memory) => (
{getTypeIcon(memory.memoryType)}

{memory.cid}

{memory.memoryType.replace('_', ' ')}
{memory.priority} priority {memory.verified && ( )}
{memory.tags.map(tag => ( {tag} ))}
{formatSize(memory.size)}
{memory.accessCount} accesses
Version {memory.version}
{memory.agentId}
Created: {new Date(memory.timestamp).toLocaleDateString()} {memory.lastAccessed !== memory.timestamp && ( <> • Last accessed: {new Date(memory.lastAccessed).toLocaleDateString()} )} {memory.expiresAt && ( <> • Expires: {new Date(memory.expiresAt).toLocaleDateString()} )}
))}
)}
{stats ? (
Storage Overview
Total Memories {stats.totalMemories}
Total Size {stats.totalSizeMB.toFixed(1)} MB
Agent Count {stats.agentCount}
Access Statistics
Total Accesses {stats.totalAccessCount}
Average Accesses {stats.averageAccessCount.toFixed(1)}
Memory Types {Object.entries(stats.byType).map(([type, count]) => (
{getTypeIcon(type)} {type.replace('_', ' ')}
{count}
))}
Priority Distribution {Object.entries(stats.byPriority).map(([priority, count]) => (
{priority}
{count}
))}
) : (

Loading statistics...

)}
Memory Settings Configure memory management settings and preferences Coming Soon Memory management settings will be available in the next update.
); }; export default MemoryManager;