Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | import { useCallback, useEffect, useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { Brain, Search, Trash2, Download, Star, Tag, Clock, ChevronDown, ChevronUp, } from 'lucide-react'; import { cardHover, defaultTransition } from '../lib/animations'; import { Button, Badge, EmptyState } from './ui'; import { intelligenceClient, type MemoryEntry, type MemoryType, type MemoryStats, } from '../lib/intelligence-client'; import { useChatStore } from '../store/chatStore'; const TYPE_LABELS: Record<MemoryType, { label: string; emoji: string; color: string }> = { fact: { label: '事实', emoji: '📋', color: 'bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-300' }, preference: { label: '偏好', emoji: '⭐', color: 'bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-300' }, lesson: { label: '经验', emoji: '💡', color: 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-300' }, context: { label: '上下文', emoji: '📌', color: 'bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-300' }, task: { label: '任务', emoji: '📝', color: 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-300' }, }; export function MemoryPanel() { const { currentAgent } = useChatStore(); const agentId = currentAgent?.id || 'zclaw-main'; const [memories, setMemories] = useState<MemoryEntry[]>([]); const [stats, setStats] = useState<MemoryStats | null>(null); const [searchQuery, setSearchQuery] = useState(''); const [filterType, setFilterType] = useState<MemoryType | 'all'>('all'); const [expandedId, setExpandedId] = useState<string | null>(null); const [isExporting, setIsExporting] = useState(false); const loadMemories = useCallback(async () => { const typeFilter = filterType !== 'all' ? { type: filterType as MemoryType } : {}; if (searchQuery.trim()) { const results = await intelligenceClient.memory.search({ agentId, query: searchQuery, limit: 50, ...typeFilter, }); setMemories(results); } else { const results = await intelligenceClient.memory.search({ agentId, limit: 50, ...typeFilter, }); setMemories(results); } const s = await intelligenceClient.memory.stats(); setStats(s); }, [agentId, searchQuery, filterType]); useEffect(() => { loadMemories(); }, [loadMemories]); const handleDelete = async (id: string) => { await intelligenceClient.memory.delete(id); loadMemories(); }; const handleExport = async () => { setIsExporting(true); try { const memories = await intelligenceClient.memory.export(); const filtered = memories.filter(m => m.agentId === agentId); const md = filtered.map(m => `## [${m.type}] ${m.content}\n` + `- 重要度: ${m.importance}\n` + `- 标签: ${m.tags.join(', ') || '无'}\n` + `- 创建时间: ${m.createdAt}\n` ).join('\n---\n\n'); const blob = new Blob([md || '# 无记忆数据'], { type: 'text/markdown' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `zclaw-memory-${agentId}-${new Date().toISOString().slice(0, 10)}.md`; a.click(); URL.revokeObjectURL(url); } finally { setIsExporting(false); } }; const handlePrune = async () => { // Find old, low-importance memories and delete them const memories = await intelligenceClient.memory.search({ agentId, minImportance: 0, limit: 1000, }); const thirtyDaysAgo = Date.now() - 30 * 24 * 60 * 60 * 1000; const toDelete = memories.filter(m => new Date(m.createdAt).getTime() < thirtyDaysAgo && m.importance < 3 ); for (const m of toDelete) { await intelligenceClient.memory.delete(m.id); } if (toDelete.length > 0) { loadMemories(); } }; return ( <div className="space-y-3"> {/* Stats */} <motion.div whileHover={cardHover} transition={defaultTransition} className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-3 shadow-sm" > <div className="flex items-center justify-between mb-2"> <h3 className="text-sm font-semibold text-gray-900 dark:text-gray-100 flex items-center gap-1.5"> <Brain className="w-4 h-4 text-orange-500" /> Agent 记忆 </h3> <div className="flex gap-1"> <Button variant="ghost" size="sm" onClick={handleExport} disabled={isExporting} title="导出记忆" aria-label="Export memories" className="p-1" > <Download className="w-3.5 h-3.5" /> </Button> <Button variant="ghost" size="sm" onClick={handlePrune} title="清理旧记忆" aria-label="Prune old memories" className="p-1" > <Trash2 className="w-3.5 h-3.5" /> </Button> </div> </div> {stats && ( <div className="grid grid-cols-3 gap-2 text-center"> <div className="bg-gray-50 dark:bg-gray-700/50 rounded-lg p-1.5"> <div className="text-lg font-bold text-orange-600">{stats.totalEntries}</div> <div className="text-[10px] text-gray-500">总记忆</div> </div> <div className="bg-gray-50 dark:bg-gray-700/50 rounded-lg p-1.5"> <div className="text-lg font-bold text-blue-600">{stats.byType['fact'] || 0}</div> <div className="text-[10px] text-gray-500">事实</div> </div> <div className="bg-gray-50 dark:bg-gray-700/50 rounded-lg p-1.5"> <div className="text-lg font-bold text-amber-600">{stats.byType['preference'] || 0}</div> <div className="text-[10px] text-gray-500">偏好</div> </div> </div> )} </motion.div> {/* Search & Filter */} <div className="space-y-2"> <div className="relative"> <Search className="absolute left-2.5 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-gray-400" /> <input type="text" value={searchQuery} onChange={(e) => setSearchQuery(e.target.value)} placeholder="搜索记忆..." className="w-full text-sm border border-gray-200 dark:border-gray-600 rounded-lg pl-8 pr-3 py-1.5 bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-1 focus:ring-orange-400" /> </div> <div className="flex gap-1 flex-wrap"> <FilterChip label="全部" active={filterType === 'all'} onClick={() => setFilterType('all')} /> {(Object.keys(TYPE_LABELS) as MemoryType[]).map((type) => ( <FilterChip key={type} label={`${TYPE_LABELS[type].emoji} ${TYPE_LABELS[type].label}`} active={filterType === type} onClick={() => setFilterType(type)} /> ))} </div> </div> {/* Memory List */} <div className="space-y-2"> {memories.length > 0 ? ( <AnimatePresence initial={false}> {memories.map((entry) => ( <MemoryCard key={entry.id} entry={entry} expanded={expandedId === entry.id} onToggle={() => setExpandedId(expandedId === entry.id ? null : entry.id)} onDelete={() => handleDelete(entry.id)} /> ))} </AnimatePresence> ) : ( <EmptyState icon={<Brain className="w-8 h-8" />} title={searchQuery ? '未找到匹配的记忆' : '暂无记忆'} description={searchQuery ? '尝试不同的搜索词' : '与 Agent 交流后,记忆会自动积累'} className="py-6" /> )} </div> </div> ); } function MemoryCard({ entry, expanded, onToggle, onDelete, }: { entry: MemoryEntry; expanded: boolean; onToggle: () => void; onDelete: () => void; }) { const typeInfo = TYPE_LABELS[entry.type]; const importanceStars = Math.round(entry.importance / 2); const timeAgo = getTimeAgo(entry.createdAt); return ( <motion.div layout initial={{ opacity: 0, y: 8 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -8 }} className="rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm overflow-hidden" > <div className="px-3 py-2 cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors" onClick={onToggle} > <div className="flex items-start gap-2"> <span className={`text-[10px] font-medium px-1.5 py-0.5 rounded-full ${typeInfo.color}`}> {typeInfo.emoji} {typeInfo.label} </span> <div className="flex-1 min-w-0"> <p className="text-sm text-gray-800 dark:text-gray-200 line-clamp-2">{entry.content}</p> </div> {expanded ? ( <ChevronUp className="w-3.5 h-3.5 text-gray-400 flex-shrink-0 mt-0.5" /> ) : ( <ChevronDown className="w-3.5 h-3.5 text-gray-400 flex-shrink-0 mt-0.5" /> )} </div> <div className="flex items-center gap-3 mt-1.5 text-[10px] text-gray-500 dark:text-gray-400"> <span className="flex items-center gap-0.5"> <Star className="w-3 h-3" /> {'★'.repeat(importanceStars)}{'☆'.repeat(5 - importanceStars)} </span> <span className="flex items-center gap-0.5"> <Clock className="w-3 h-3" /> {timeAgo} </span> {entry.tags.length > 0 && ( <span className="flex items-center gap-0.5"> <Tag className="w-3 h-3" /> {entry.tags.slice(0, 2).join(', ')} </span> )} </div> </div> <AnimatePresence> {expanded && ( <motion.div initial={{ height: 0, opacity: 0 }} animate={{ height: 'auto', opacity: 1 }} exit={{ height: 0, opacity: 0 }} transition={{ duration: 0.2 }} className="border-t border-gray-100 dark:border-gray-700" > <div className="px-3 py-2 space-y-2 text-xs"> <div className="grid grid-cols-2 gap-2"> <div> <span className="text-gray-500">重要性</span> <span className="ml-1 font-medium">{entry.importance}/10</span> </div> <div> <span className="text-gray-500">来源</span> <span className="ml-1 font-medium">{entry.source === 'auto' ? '自动' : entry.source === 'user' ? '用户' : '反思'}</span> </div> <div> <span className="text-gray-500">访问</span> <span className="ml-1 font-medium">{entry.accessCount}次</span> </div> <div> <span className="text-gray-500">创建</span> <span className="ml-1 font-medium">{new Date(entry.createdAt).toLocaleDateString('zh-CN')}</span> </div> </div> {entry.tags.length > 0 && ( <div className="flex flex-wrap gap-1"> {entry.tags.map((tag) => ( <Badge key={tag} variant="default">{tag}</Badge> ))} </div> )} <div className="flex justify-end"> <Button variant="ghost" size="sm" onClick={(e: React.MouseEvent) => { e.stopPropagation(); onDelete(); }} className="text-red-500 hover:text-red-700 hover:bg-red-50 dark:hover:bg-red-900/20 text-xs px-2 py-1" > <Trash2 className="w-3 h-3 mr-1" /> 删除 </Button> </div> </div> </motion.div> )} </AnimatePresence> </motion.div> ); } function FilterChip({ label, active, onClick, }: { label: string; active: boolean; onClick: () => void; }) { return ( <button onClick={onClick} className={`text-[10px] px-2 py-0.5 rounded-full border transition-colors ${ active ? 'bg-orange-100 border-orange-300 text-orange-700 dark:bg-orange-900/30 dark:border-orange-700 dark:text-orange-300' : 'bg-gray-50 border-gray-200 text-gray-600 hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-400' }`} > {label} </button> ); } function getTimeAgo(isoDate: string): string { const now = Date.now(); const then = new Date(isoDate).getTime(); const diffMs = now - then; const minutes = Math.floor(diffMs / 60_000); if (minutes < 1) return '刚刚'; if (minutes < 60) return `${minutes}分钟前`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours}小时前`; const days = Math.floor(hours / 24); if (days < 30) return `${days}天前`; const months = Math.floor(days / 30); return `${months}个月前`; } |