Spaces:
Running
Running
File size: 14,036 Bytes
2e9dd8e b23f51c cdcf600 b23f51c 2e9dd8e cdcf600 2e9dd8e b23f51c cdcf600 2e9dd8e cdcf600 ef1f403 b23f51c 2e9dd8e ef1f403 2e9dd8e b23f51c ef1f403 b23f51c 2e9dd8e ef1f403 2e9dd8e cdcf600 ef1f403 2e9dd8e eb8640b 2e9dd8e ef1f403 2e9dd8e cdcf600 2e9dd8e ef1f403 2e9dd8e cdcf600 ef1f403 2e9dd8e cdcf600 2e9dd8e cdcf600 2e9dd8e ef1f403 cdcf600 b23f51c cdcf600 b23f51c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | import { useState, useEffect, useCallback, useMemo } from 'react';
import { apiFetch } from '../api';
import './MemoryExplorer.css';
function MemoryExplorer() {
const [search, setSearch] = useState('');
const [memories, setMemories] = useState([]);
const [graphData, setGraphData] = useState({ nodes: [], edges: [] });
const [selectedId, setSelectedId] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [isSearching, setIsSearching] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [activeFilter, setActiveFilter] = useState('all');
const [insightEntity, setInsightEntity] = useState(null);
const fetchData = useCallback(async (query = '') => {
const isSearch = query.trim().length > 0;
if (isSearch) setIsSearching(true);
else setIsLoading(true);
try {
const memoryUrl = isSearch
? `/api/v1/memory/search?q=${encodeURIComponent(query)}`
: '/api/v1/memory/sensory';
const [memRes, graphRes] = await Promise.all([
apiFetch(memoryUrl),
apiFetch('/api/v1/graph')
]);
if (memRes.ok) {
const data = await memRes.json();
const results = data.memories || [];
setMemories(results);
if (results.length > 0) {
if (!selectedId || !results.find(m => m.id === selectedId)) {
setSelectedId(results[0].id);
}
} else {
setSelectedId(null);
}
}
if (graphRes.ok) {
const gData = await graphRes.json();
setGraphData(gData);
}
} catch (error) {
console.error('Neural synchronization failure', error);
} finally {
setIsLoading(false);
setIsSearching(false);
}
}, [selectedId]);
const handlePurge = async (id) => {
if (!window.confirm("Are you sure you want to purge this memory chunk from the neural cortex? This action is irreversible.")) return;
setIsDeleting(true);
try {
const res = await apiFetch(`/api/v1/memory/${id}`, { method: 'DELETE' });
if (res.ok) {
setMemories(prev => prev.filter(m => m.id !== id));
setSelectedId(null);
fetchData(search);
}
} catch (error) {
console.error('Purge failed', error);
} finally {
setIsDeleting(false);
}
};
useEffect(() => {
fetchData();
}, []);
useEffect(() => {
const timer = setTimeout(() => {
fetchData(search);
}, 500);
return () => clearTimeout(timer);
}, [search]);
const filteredMemories = useMemo(() => {
if (activeFilter === 'all') return memories;
return memories.filter(m => {
const type = m.metadata?.type;
if (activeFilter === 'episodic') return type === 'conversation' || type === 'chat_exchange';
if (activeFilter === 'semantic') return type === 'concept' || type === 'note';
if (activeFilter === 'sleep') return type === 'sleep_summary';
return true;
});
}, [memories, activeFilter]);
const selectedMemory = useMemo(() =>
memories.find(m => m.id === selectedId) || memories[0],
[memories, selectedId]
);
const linkedEntities = useMemo(() => {
if (!selectedMemory || !graphData.nodes) return [];
const content = selectedMemory.content.toLowerCase();
return graphData.nodes.filter(node =>
content.includes(node.id.toLowerCase()) ||
(node.label && content.includes(node.label.toLowerCase()))
);
}, [selectedMemory, graphData]);
const getTitle = (content) => {
if (!content) return 'Untitled Memory Chunks';
const lines = content.split('\n');
const firstLine = lines[0].trim();
if (firstLine.length > 40) return firstLine.substring(0, 37) + '...';
return firstLine || 'Untitled Memory Chunks';
};
const getMemoryTheme = (type) => {
switch (type) {
case 'conversation': return { color: '#3b82f6', label: 'Episodic' };
case 'chat_exchange': return { color: '#8b5cf6', label: 'Social' };
case 'concept': return { color: '#ff6b35', label: 'Semantic' };
case 'note': return { color: '#10b981', label: 'Explicit' };
case 'sleep_summary': return { color: '#f59e0b', label: 'Consolidated' };
default: return { color: '#94a3b8', label: 'Raw Sensory' };
}
};
return (
<div className="memory-view-grid fade-in">
{/* ── SIDEBAR: Memory Feed ── */}
<section className="memory-surface list-surface">
<div className="memory-toolbar">
<div className="memory-search-wrap">
<span className="material-icons">{isSearching ? 'sync' : 'search'}</span>
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Query sensory cortex..."
/>
</div>
<div className="filter-tabs">
{['all', 'episodic', 'semantic', 'sleep'].map(f => (
<button
key={f}
className={`filter-tab ${activeFilter === f ? 'active' : ''}`}
onClick={() => setActiveFilter(f)}
>
{f}
</button>
))}
</div>
<div className="density-stats">
<span className="material-icons" style={{fontSize: '14px'}}>dns</span>
<span>{filteredMemories.length} Active Nodes</span>
</div>
</div>
<div className="memory-list">
{isLoading ? (
<div className="empty-detail">
<span className="material-icons pulse">settings_input_component</span>
<p style={{fontSize: '0.7rem'}}>Calibrating neural paths...</p>
</div>
) : filteredMemories.length === 0 ? (
<div className="empty-detail" style={{ height: '100%', gap: '8px' }}>
<span className="material-icons" style={{ fontSize: '32px', opacity: 0.5 }}>folder_open</span>
<p style={{ fontSize: '0.8rem', color: '#aaa', fontWeight: 600 }}>No memories found</p>
<p style={{ fontSize: '0.7rem', color: '#bbb', textAlign: 'center' }}>Query the sensory cortex or select another layer above.</p>
</div>
) : (
filteredMemories.map((memory) => {
const theme = getMemoryTheme(memory.metadata?.type);
return (
<button
key={memory.id}
className={`memory-row ${memory.id === selectedId ? 'active' : ''}`}
onClick={() => setSelectedId(memory.id)}
>
<div
className="memory-dot"
style={{ backgroundColor: theme.color }}
/>
<div className="memory-row-copy">
<strong>{getTitle(memory.content)}</strong>
<div className="memory-row-meta">
<span style={{ color: theme.color }}>{theme.label}</span>
<span>•</span>
<span>{memory.id.substring(0, 8)}</span>
</div>
</div>
</button>
);
})
)}
</div>
</section>
{/* ── MAIN: Diagnostic Detail ── */}
<section className="memory-surface detail-surface" style={{position: 'relative'}}>
{selectedMemory ? (
<div className="fade-in">
<div className="memory-header-meta">
<div className="memory-type-pill" style={{
color: getMemoryTheme(selectedMemory.metadata?.type).color,
background: `${getMemoryTheme(selectedMemory.metadata?.type).color}15`
}}>
Layer: {getMemoryTheme(selectedMemory.metadata?.type).label}
</div>
<div className="memory-id-badge" style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
<span>UUID: {selectedMemory.id}</span>
<button
className="purge-btn"
onClick={() => handlePurge(selectedMemory.id)}
disabled={isDeleting}
>
<span className="material-icons" style={{fontSize: '14px'}}>{isDeleting ? 'sync' : 'delete_forever'}</span>
Purge
</button>
</div>
</div>
<h1 className="memory-detail-title">{getTitle(selectedMemory.content)}</h1>
<p className="memory-detail-date">
<span className="material-icons" style={{fontSize: '14px'}}>event</span>
Encoded: {selectedMemory.metadata?.timestamp ? new Date(selectedMemory.metadata.timestamp).toLocaleString() : 'System Boot Sequence'}
</p>
<div className="memory-content-box">
{selectedMemory.content}
</div>
{/* Instrument Metrics */}
<div className="instrument-grid">
<div className="instrument-card">
<h4>Retrieval Salience</h4>
<div className="meter-track">
<div className="meter-fill" style={{
width: `${Math.round((selectedMemory.similarity || 0.85) * 100)}%`,
backgroundColor: getMemoryTheme(selectedMemory.metadata?.type).color
}} />
</div>
<div className="metric-value">{(selectedMemory.similarity || 0.85).toFixed(4)}</div>
</div>
<div className="instrument-card">
<h4>Graph Associations</h4>
<div className="meter-track">
<div className="meter-fill" style={{
width: `${Math.min(linkedEntities.length * 20, 100)}%`,
backgroundColor: '#ff6b35'
}} />
</div>
<div className="metric-value">{linkedEntities.length} Links</div>
</div>
<div className="instrument-card">
<h4>Entropy Score</h4>
<div className="meter-track">
<div className="meter-fill" style={{
width: '42%',
backgroundColor: '#10b981'
}} />
</div>
<div className="metric-value">0.4281</div>
</div>
</div>
{/* Knowledge Links */}
<div className="neighborhood-section">
<div className="section-label">
<span className="material-icons" style={{fontSize: '16px'}}>account_tree</span>
Knowledge Graph Intersections (Click for Insights)
</div>
<div className="neighborhood-grid">
{linkedEntities.map(node => (
<button
key={node.id}
className="neighbor-card"
onClick={() => setInsightEntity(node)}
>
<strong>{node.id}</strong>
<span>Connections: {node.connections || 0}</span>
</button>
))}
{linkedEntities.length === 0 && (
<p style={{fontSize: '0.75rem', color: '#999', fontStyle: 'italic'}}>No direct semantic associations detected in current graph state.</p>
)}
</div>
</div>
{/* Raw Metadata */}
<div className="neighborhood-section">
<div className="section-label">
<span className="material-icons" style={{fontSize: '16px'}}>terminal</span>
Raw Metadata Explorer
</div>
<div className="metadata-grid">
{Object.entries(selectedMemory.metadata || {}).map(([key, val]) => (
<div key={key} className="metadata-tag">
<span>{key}</span>
<span>{String(val)}</span>
</div>
))}
</div>
</div>
{/* Neural Insight Overlay */}
{insightEntity && (
<div className="neural-insight-overlay fade-in">
<div className="neural-insight-modal">
<div className="insight-header">
<span className="material-icons">psychology</span>
<h3>Neural Insight: {insightEntity.id}</h3>
<button className="insight-close" onClick={() => setInsightEntity(null)}>
<span className="material-icons">close</span>
</button>
</div>
<div className="insight-body">
<div className="insight-stat">
<label>Semantic Weight</label>
<strong>{insightEntity.connections * 12.5} nm</strong>
</div>
<div className="insight-stat">
<label>Active Connections</label>
<strong>{insightEntity.connections} Nodes</strong>
</div>
<p>This entity is a core semantic node within your current knowledge base. It facilitates retrieval of related episodic contexts and strengthens the reasoning cortex.</p>
</div>
<button className="insight-btn" onClick={() => setInsightEntity(null)}>Close Diagnostic</button>
</div>
</div>
)}
</div>
) : (
<div className="empty-detail">
<span className="material-icons pulse">sensors</span>
<h3>No Active Focus</h3>
<p style={{maxWidth: '280px', margin: '0 auto', fontSize: '0.85rem'}}>Select a sensory chunk from the feed to begin deep-layer diagnostic analysis.</p>
</div>
)}
</section>
</div>
);
}
export default MemoryExplorer;
|