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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | /** * HeartbeatConfig - Configuration UI for periodic proactive checks * * Allows users to configure: * - Heartbeat interval (default 30 minutes) * - Enable/disable built-in check items * - Quiet hours (no notifications during sleep time) * - Proactivity level (silent/light/standard/autonomous) * * Part of ZCLAW L4 Self-Evolution capability. */ import { useState, useCallback, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { Heart, Settings, Clock, Moon, Sun, Volume2, VolumeX, AlertTriangle, CheckCircle, Info, RefreshCw, } from 'lucide-react'; import { intelligenceClient, type HeartbeatConfig as HeartbeatConfigType, type HeartbeatResult, type HeartbeatAlert, } from '../lib/intelligence-client'; // === Default Config === const DEFAULT_HEARTBEAT_CONFIG: HeartbeatConfigType = { enabled: false, interval_minutes: 30, quiet_hours_start: null, quiet_hours_end: null, notify_channel: 'ui', proactivity_level: 'standard', max_alerts_per_tick: 5, }; // === Types === interface HeartbeatConfigProps { className?: string; onConfigChange?: (config: HeartbeatConfigType) => void; } type ProactivityLevel = 'silent' | 'light' | 'standard' | 'autonomous'; // === Proactivity Level Config === const PROACTIVITY_CONFIG: Record<ProactivityLevel, { label: string; description: string; icon: typeof Moon }> = { silent: { label: '静默', description: '从不主动推送,仅被动响应', icon: VolumeX, }, light: { label: '轻度', description: '仅紧急事项推送(如定时任务完成)', icon: Volume2, }, standard: { label: '标准', description: '定期巡检 + 任务通知 + 建议推送', icon: AlertTriangle, }, autonomous: { label: '自主', description: 'Agent 自行判断何时推送', icon: Heart, }, }; // === Check Item Config === interface CheckItemConfig { id: string; name: string; description: string; enabled: boolean; } const BUILT_IN_CHECKS: CheckItemConfig[] = [ { id: 'pending-tasks', name: '待办任务检查', description: '检查是否有未完成的任务需要跟进', enabled: true, }, { id: 'memory-health', name: '记忆健康检查', description: '检查记忆存储是否过大需要清理', enabled: true, }, { id: 'idle-greeting', name: '空闲问候', description: '长时间未使用时发送简短问候', enabled: false, }, ]; // === Components === function ProactivityLevelSelector({ value, onChange, }: { value: ProactivityLevel; onChange: (level: ProactivityLevel) => void; }) { return ( <div className="grid grid-cols-2 gap-2"> {(Object.keys(PROACTIVITY_CONFIG) as ProactivityLevel[]).map((level) => { const config = PROACTIVITY_CONFIG[level]; const Icon = config.icon; const isSelected = value === level; return ( <button key={level} onClick={() => onChange(level)} className={`flex items-start gap-2 p-3 rounded-lg border transition-all text-left ${ isSelected ? 'border-purple-500 bg-purple-50 dark:bg-purple-900/30' : 'border-gray-200 dark:border-gray-700 hover:border-gray-300 dark:hover:border-gray-600' }`} > <Icon className={`w-4 h-4 mt-0.5 flex-shrink-0 ${ isSelected ? 'text-purple-500' : 'text-gray-400' }`} /> <div> <div className={`text-sm font-medium ${ isSelected ? 'text-purple-700 dark:text-purple-400' : 'text-gray-700 dark:text-gray-300' }`} > {config.label} </div> <div className="text-xs text-gray-500 dark:text-gray-400 mt-0.5"> {config.description} </div> </div> </button> ); })} </div> ); } function QuietHoursConfig({ start, end, onStartChange, onEndChange, enabled, onToggle, }: { start?: string; end?: string; onStartChange: (time: string) => void; onEndChange: (time: string) => void; enabled: boolean; onToggle: (enabled: boolean) => void; }) { return ( <div className="space-y-3"> <div className="flex items-center justify-between"> <div className="flex items-center gap-2"> <Moon className="w-4 h-4 text-indigo-500" /> <span className="text-sm font-medium text-gray-700 dark:text-gray-300">免打扰时段</span> </div> <button onClick={() => onToggle(!enabled)} className={`relative w-10 h-5 rounded-full transition-colors ${ enabled ? 'bg-purple-500' : 'bg-gray-300 dark:bg-gray-600' }`} > <motion.div animate={{ x: enabled ? 20 : 0 }} className="absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full shadow" /> </button> </div> <AnimatePresence> {enabled && ( <motion.div initial={{ height: 0, opacity: 0 }} animate={{ height: 'auto', opacity: 1 }} exit={{ height: 0, opacity: 0 }} className="flex items-center gap-3 pl-6" > <div className="flex items-center gap-2"> <Sun className="w-3 h-3 text-gray-400" /> <input type="time" value={end || '08:00'} onChange={(e) => onEndChange(e.target.value)} className="px-2 py-1 text-sm border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100" /> </div> <span className="text-gray-400">至</span> <div className="flex items-center gap-2"> <Moon className="w-3 h-3 text-gray-400" /> <input type="time" value={start || '22:00'} onChange={(e) => onStartChange(e.target.value)} className="px-2 py-1 text-sm border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100" /> </div> </motion.div> )} </AnimatePresence> </div> ); } function CheckItemToggle({ item, onToggle, }: { item: CheckItemConfig; onToggle: (enabled: boolean) => void; }) { return ( <div className="flex items-center justify-between py-2"> <div className="flex-1 min-w-0"> <div className="text-sm font-medium text-gray-700 dark:text-gray-300"> {item.name} </div> <div className="text-xs text-gray-500 dark:text-gray-400 truncate"> {item.description} </div> </div> <button onClick={() => onToggle(!item.enabled)} className={`relative w-9 h-5 rounded-full transition-colors ${ item.enabled ? 'bg-green-500' : 'bg-gray-300 dark:bg-gray-600' }`} > <motion.div animate={{ x: item.enabled ? 18 : 0 }} className="absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full shadow" /> </button> </div> ); } // === Main Component === export function HeartbeatConfig({ className = '', onConfigChange }: HeartbeatConfigProps) { const [config, setConfig] = useState<HeartbeatConfigType>(DEFAULT_HEARTBEAT_CONFIG); const [checkItems, setCheckItems] = useState<CheckItemConfig[]>(BUILT_IN_CHECKS); const [lastResult, setLastResult] = useState<HeartbeatResult | null>(null); const [isTesting, setIsTesting] = useState(false); const [hasChanges, setHasChanges] = useState(false); // Load saved config useEffect(() => { const saved = localStorage.getItem('zclaw-heartbeat-config'); if (saved) { try { const parsed = JSON.parse(saved); setConfig({ ...DEFAULT_HEARTBEAT_CONFIG, ...parsed }); } catch { // Use defaults } } const savedChecks = localStorage.getItem('zclaw-heartbeat-checks'); if (savedChecks) { try { setCheckItems(JSON.parse(savedChecks)); } catch { // Use defaults } } }, []); const updateConfig = useCallback( (updates: Partial<HeartbeatConfigType>) => { setConfig((prev) => { const next = { ...prev, ...updates }; setHasChanges(true); onConfigChange?.(next); return next; }); }, [onConfigChange] ); const toggleCheckItem = useCallback((id: string, enabled: boolean) => { setCheckItems((prev) => { const next = prev.map((item) => item.id === id ? { ...item, enabled } : item ); setHasChanges(true); return next; }); }, []); const handleSave = useCallback(() => { localStorage.setItem('zclaw-heartbeat-config', JSON.stringify(config)); localStorage.setItem('zclaw-heartbeat-checks', JSON.stringify(checkItems)); setHasChanges(false); }, [config, checkItems]); const handleTestHeartbeat = useCallback(async () => { setIsTesting(true); try { await intelligenceClient.heartbeat.init('zclaw-main', config); const result = await intelligenceClient.heartbeat.tick('zclaw-main'); setLastResult(result); } catch (error) { console.error('[HeartbeatConfig] Test failed:', error); } finally { setIsTesting(false); } }, [config]); return ( <div className={`flex flex-col h-full ${className}`}> {/* Header */} <div className="flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700"> <div className="flex items-center gap-2"> <Heart className="w-5 h-5 text-pink-500" /> <h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100">心跳配置</h2> </div> <div className="flex items-center gap-2"> <button onClick={handleTestHeartbeat} disabled={isTesting || !config.enabled} className="flex items-center gap-1 px-3 py-1.5 text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 disabled:opacity-50" > <RefreshCw className={`w-4 h-4 ${isTesting ? 'animate-spin' : ''}`} /> 测试 </button> <button onClick={handleSave} disabled={!hasChanges} className="px-3 py-1.5 text-sm bg-pink-500 hover:bg-pink-600 disabled:bg-gray-300 disabled:cursor-not-allowed text-white rounded-lg transition-colors" > 保存 </button> </div> </div> {/* Content */} <div className="flex-1 overflow-y-auto p-4 space-y-6"> {/* Enable Toggle */} <div className="flex items-center justify-between p-4 bg-gray-50 dark:bg-gray-800/50 rounded-lg"> <div className="flex items-center gap-3"> <div className={`w-10 h-10 rounded-full flex items-center justify-center ${ config.enabled ? 'bg-pink-100 dark:bg-pink-900/30' : 'bg-gray-200 dark:bg-gray-700' }`} > <Heart className={`w-5 h-5 ${ config.enabled ? 'text-pink-500' : 'text-gray-400' }`} /> </div> <div> <div className="text-sm font-medium text-gray-900 dark:text-gray-100"> 启用主动巡检 </div> <div className="text-xs text-gray-500 dark:text-gray-400"> Agent 将定期检查并主动推送通知 </div> </div> </div> <button onClick={() => updateConfig({ enabled: !config.enabled })} className={`relative w-12 h-6 rounded-full transition-colors ${ config.enabled ? 'bg-pink-500' : 'bg-gray-300 dark:bg-gray-600' }`} > <motion.div animate={{ x: config.enabled ? 26 : 0 }} className="absolute top-0.5 left-0.5 w-5 h-5 bg-white rounded-full shadow" /> </button> </div> <AnimatePresence> {config.enabled && ( <motion.div initial={{ height: 0, opacity: 0 }} animate={{ height: 'auto', opacity: 1 }} exit={{ height: 0, opacity: 0 }} className="space-y-6" > {/* Interval */} <div className="space-y-2"> <div className="flex items-center gap-2"> <Clock className="w-4 h-4 text-gray-500" /> <span className="text-sm font-medium text-gray-700 dark:text-gray-300"> 巡检间隔 </span> </div> <div className="flex items-center gap-2 pl-6"> <input type="range" min="5" max="120" step="5" value={config.interval_minutes} onChange={(e) => updateConfig({ interval_minutes: parseInt(e.target.value) })} className="flex-1 h-2 bg-gray-200 dark:bg-gray-700 rounded-lg appearance-none cursor-pointer accent-pink-500" /> <span className="text-sm font-medium text-gray-900 dark:text-gray-100 w-16 text-right"> {config.interval_minutes} 分钟 </span> </div> </div> {/* Proactivity Level */} <div className="space-y-2"> <div className="flex items-center gap-2"> <AlertTriangle className="w-4 h-4 text-gray-500" /> <span className="text-sm font-medium text-gray-700 dark:text-gray-300"> 主动性级别 </span> </div> <div className="pl-6"> <ProactivityLevelSelector value={config.proactivity_level} onChange={(level) => updateConfig({ proactivity_level: level })} /> </div> </div> {/* Quiet Hours */} <div className="space-y-2"> <QuietHoursConfig start={config.quiet_hours_start ?? undefined} end={config.quiet_hours_end ?? undefined} enabled={!!config.quiet_hours_start} onStartChange={(time) => updateConfig({ quiet_hours_start: time })} onEndChange={(time) => updateConfig({ quiet_hours_end: time })} onToggle={(enabled) => updateConfig({ quiet_hours_start: enabled ? '22:00' : null, quiet_hours_end: enabled ? '08:00' : null, }) } /> </div> {/* Check Items */} <div className="space-y-2"> <div className="flex items-center gap-2"> <Settings className="w-4 h-4 text-gray-500" /> <span className="text-sm font-medium text-gray-700 dark:text-gray-300"> 检查项目 </span> </div> <div className="pl-6 space-y-1 border-l-2 border-gray-200 dark:border-gray-700"> {checkItems.map((item) => ( <CheckItemToggle key={item.id} item={item} onToggle={(enabled) => toggleCheckItem(item.id, enabled)} /> ))} </div> </div> {/* Last Result */} {lastResult && ( <div className="p-3 bg-gray-50 dark:bg-gray-800/50 rounded-lg"> <div className="flex items-center gap-2 mb-2"> {lastResult.status === 'ok' ? ( <CheckCircle className="w-4 h-4 text-green-500" /> ) : ( <AlertTriangle className="w-4 h-4 text-yellow-500" /> )} <span className="text-sm font-medium text-gray-700 dark:text-gray-300"> 上次测试结果 </span> </div> <div className="text-xs text-gray-500 dark:text-gray-400"> 检查了 {lastResult.checked_items} 项 {lastResult.alerts.length > 0 && ` · ${lastResult.alerts.length} 个提醒`} </div> {lastResult.alerts.length > 0 && ( <div className="mt-2 space-y-1"> {lastResult.alerts.map((alert: HeartbeatAlert, i: number) => ( <div key={i} className={`text-xs p-2 rounded ${ alert.urgency === 'high' ? 'bg-red-50 dark:bg-red-900/20 text-red-600 dark:text-red-400' : alert.urgency === 'medium' ? 'bg-yellow-50 dark:bg-yellow-900/20 text-yellow-600 dark:text-yellow-400' : 'bg-blue-50 dark:bg-blue-900/20 text-blue-600 dark:text-blue-400' }`} > <span className="font-medium">{alert.title}:</span> {alert.content} </div> ))} </div> )} </div> )} </motion.div> )} </AnimatePresence> {/* Info */} <div className="flex items-start gap-2 p-3 bg-blue-50 dark:bg-blue-900/20 rounded-lg text-xs text-blue-600 dark:text-blue-400"> <Info className="w-4 h-4 flex-shrink-0 mt-0.5" /> <p> 心跳机制让 Agent 具备主动意识,能够定期检查任务状态、记忆健康度等,并根据主动性级别推送通知。 在"自主"模式下,Agent 将自行判断是否需要通知你。 </p> </div> </div> </div> ); } export default HeartbeatConfig; |