File size: 16,867 Bytes
7fcdb70 |
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 |
import React, { useState, useEffect, useRef } from 'react';
import { AppBar, Toolbar, Box, Typography, Chip, IconButton, CircularProgress, keyframes, Button } from '@mui/material';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import LightModeOutlined from '@mui/icons-material/LightModeOutlined';
import DarkModeOutlined from '@mui/icons-material/DarkModeOutlined';
import CheckIcon from '@mui/icons-material/Check';
import CloseIcon from '@mui/icons-material/Close';
import AccessTimeIcon from '@mui/icons-material/AccessTime';
import InputIcon from '@mui/icons-material/Input';
import OutputIcon from '@mui/icons-material/Output';
import SmartToyIcon from '@mui/icons-material/SmartToy';
import FormatListNumberedIcon from '@mui/icons-material/FormatListNumbered';
import HourglassEmptyIcon from '@mui/icons-material/HourglassEmpty';
import StopCircleIcon from '@mui/icons-material/StopCircle';
import { useAgentStore, selectTrace, selectError, selectIsDarkMode, selectMetadata, selectIsConnectingToE2B, selectFinalStep } from '@/stores/agentStore';
interface HeaderProps {
isAgentProcessing: boolean;
onBackToHome?: () => void;
}
// Animation for the running task border - smooth oscillation (primary)
const borderPulse = keyframes`
0%, 100% {
border-color: rgba(79, 134, 198, 0.5);
box-shadow: 0 0 0 0 rgba(79, 134, 198, 0.3);
}
50% {
border-color: rgba(79, 134, 198, 1);
box-shadow: 0 0 8px 2px rgba(79, 134, 198, 0.4);
}
`;
// Animation for the background glow (primary)
const backgroundPulse = keyframes`
0%, 100% {
background-color: rgba(79, 134, 198, 0.08);
}
50% {
background-color: rgba(79, 134, 198, 0.15);
}
`;
// Animation for token flash - smooth glow effect
const tokenFlash = keyframes`
0% {
filter: brightness(1);
text-shadow: none;
}
25% {
filter: brightness(1.4);
text-shadow: 0 0 8px rgba(79, 134, 198, 0.6);
}
100% {
filter: brightness(1);
text-shadow: none;
}
`;
// Animation for token icon flash
const iconFlash = keyframes`
0% {
filter: brightness(1);
transform: scale(1);
}
25% {
filter: brightness(1.6);
transform: scale(1.15);
}
100% {
filter: brightness(1);
transform: scale(1);
}
`;
export const Header: React.FC<HeaderProps> = ({ isAgentProcessing, onBackToHome }) => {
const trace = useAgentStore(selectTrace);
const error = useAgentStore(selectError);
const finalStep = useAgentStore(selectFinalStep);
const isDarkMode = useAgentStore(selectIsDarkMode);
const toggleDarkMode = useAgentStore((state) => state.toggleDarkMode);
const metadata = useAgentStore(selectMetadata);
const isConnectingToE2B = useAgentStore(selectIsConnectingToE2B);
const [elapsedTime, setElapsedTime] = useState(0);
const [inputTokenFlash, setInputTokenFlash] = useState(false);
const [outputTokenFlash, setOutputTokenFlash] = useState(false);
const prevInputTokens = useRef(0);
const prevOutputTokens = useRef(0);
// Update elapsed time every 100ms when agent is processing
useEffect(() => {
if (isAgentProcessing && trace?.timestamp) {
const interval = setInterval(() => {
const now = new Date();
const startTime = new Date(trace.timestamp);
const elapsed = (now.getTime() - startTime.getTime()) / 1000;
setElapsedTime(elapsed);
}, 100);
return () => clearInterval(interval);
} else if (metadata && metadata.duration > 0) {
setElapsedTime(metadata.duration);
}
}, [isAgentProcessing, trace?.timestamp, metadata]);
// Detect token changes and trigger flash animation
useEffect(() => {
if (metadata) {
// Input tokens changed
if (metadata.inputTokensUsed > prevInputTokens.current && prevInputTokens.current > 0) {
setInputTokenFlash(true);
setTimeout(() => setInputTokenFlash(false), 800);
}
prevInputTokens.current = metadata.inputTokensUsed;
// Output tokens changed
if (metadata.outputTokensUsed > prevOutputTokens.current && prevOutputTokens.current > 0) {
setOutputTokenFlash(true);
setTimeout(() => setOutputTokenFlash(false), 800);
}
prevOutputTokens.current = metadata.outputTokensUsed;
}
}, [metadata?.inputTokensUsed, metadata?.outputTokensUsed]);
// Determine task status - Use finalStep as source of truth
const getTaskStatus = () => {
// If we have a final step, use its type
if (finalStep) {
switch (finalStep.type) {
case 'failure':
return { label: 'Task failed', color: 'error', icon: <CloseIcon sx={{ fontSize: 16, color: 'error.main' }} /> };
case 'stopped':
return { label: 'Task stopped', color: 'warning', icon: <StopCircleIcon sx={{ fontSize: 16, color: 'warning.main' }} /> };
case 'max_steps_reached':
return { label: 'Max steps reached', color: 'warning', icon: <HourglassEmptyIcon sx={{ fontSize: 16, color: 'warning.main' }} /> };
case 'success':
return { label: 'Completed', color: 'success', icon: <CheckIcon sx={{ fontSize: 16, color: 'success.main' }} /> };
}
}
// Otherwise check running states
if (isConnectingToE2B) return { label: 'Starting...', color: 'primary', icon: <CircularProgress size={16} thickness={5} sx={{ color: 'primary.main' }} /> };
if (isAgentProcessing || trace?.isRunning) return { label: 'Running', color: 'primary', icon: <CircularProgress size={16} thickness={5} sx={{ color: 'primary.main' }} /> };
return { label: 'Ready', color: 'default', icon: <CheckIcon sx={{ fontSize: 16, color: 'text.secondary' }} /> };
};
const taskStatus = getTaskStatus();
// Extract model name from modelId (e.g., "Qwen/Qwen3-VL-8B-Instruct" -> "Qwen3-VL-8B-Instruct")
const modelName = trace?.modelId?.split('/').pop() || 'Unknown Model';
// Handler for emergency stop
const handleEmergencyStop = () => {
const stopTask = (window as Window & { __stopCurrentTask?: () => void }).__stopCurrentTask;
if (stopTask) {
stopTask();
}
};
return (
<AppBar
position="static"
elevation={0}
sx={{
backgroundColor: 'background.paper',
borderBottom: '1px solid',
borderColor: 'divider',
}}
>
<Toolbar disableGutters sx={{ px: 2, py: 2.5, flexDirection: 'column', alignItems: 'stretch', gap: 0 }}>
{/* First row: Back button + Task info + Connection Status */}
<Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', width: '100%', gap: 3 }}>
{/* Left side: Back button + Task info */}
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5, flex: 1, minWidth: 0 }}>
<IconButton
onClick={onBackToHome}
size="small"
sx={{
color: 'primary.main',
backgroundColor: 'primary.50',
border: '1px solid',
borderColor: 'primary.200',
cursor: 'pointer',
'&:hover': {
backgroundColor: 'primary.100',
borderColor: 'primary.main',
},
}}
>
<ArrowBackIcon fontSize="small" />
</IconButton>
<Typography
variant="body2"
sx={{
color: 'text.primary',
fontWeight: 700,
fontSize: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
}}
>
{trace?.instruction || 'No task running'}
</Typography>
</Box>
{/* Right side: Emergency Stop + Dark Mode */}
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
{/* Emergency Stop Button - Only show when agent is processing */}
{isAgentProcessing && (
<Button
onClick={handleEmergencyStop}
variant="outlined"
size="small"
startIcon={<StopCircleIcon />}
sx={{
color: 'error.main',
borderColor: 'error.main',
backgroundColor: 'transparent',
fontWeight: 600,
fontSize: '0.8rem',
px: 1.5,
py: 0.5,
textTransform: 'none',
'&:hover': {
backgroundColor: 'error.50',
borderColor: 'error.dark',
},
}}
>
Stop
</Button>
)}
<IconButton
onClick={toggleDarkMode}
size="small"
sx={{
color: 'primary.main',
backgroundColor: 'primary.50',
border: '1px solid',
borderColor: 'primary.200',
'&:hover': {
backgroundColor: 'primary.100',
borderColor: 'primary.main',
},
}}
>
{isDarkMode ? <LightModeOutlined fontSize="small" /> : <DarkModeOutlined fontSize="small" />}
</IconButton>
</Box>
</Box>
{/* Second row: Status + Model + Metadata - Only show when we have trace data */}
{trace && (
<Box
sx={{
display: 'flex',
alignItems: 'center',
gap: 1.5,
pl: 5.5,
pr: 1,
pt: .5,
mt: .5,
}}
>
{/* Status Badge - Compact */}
<Box
sx={{
display: 'flex',
alignItems: 'center',
gap: 0.5,
px: 1,
py: 0.25,
borderRadius: 1,
backgroundColor:
taskStatus.color === 'primary' ? 'primary.50' :
taskStatus.color === 'success' ? 'success.50' :
taskStatus.color === 'error' ? 'error.50' :
taskStatus.color === 'warning' ? 'warning.50' :
'action.hover',
border: '1px solid',
borderColor:
taskStatus.color === 'primary' ? 'primary.main' :
taskStatus.color === 'success' ? 'success.main' :
taskStatus.color === 'error' ? 'error.main' :
taskStatus.color === 'warning' ? 'warning.main' :
'divider',
}}
>
{taskStatus.icon}
<Typography
variant="caption"
sx={{
fontSize: '0.7rem',
fontWeight: 700,
color:
taskStatus.color === 'primary' ? 'primary.main' :
taskStatus.color === 'success' ? 'success.main' :
taskStatus.color === 'error' ? 'error.main' :
taskStatus.color === 'warning' ? 'warning.main' :
'text.primary',
}}
>
{taskStatus.label}
</Typography>
</Box>
{/* Divider */}
<Box sx={{ width: '1px', height: 16, backgroundColor: 'divider' }} />
{/* Model */}
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<SmartToyIcon sx={{ fontSize: '0.85rem', color: 'primary.main' }} />
<Typography
variant="caption"
sx={{
fontSize: '0.75rem',
fontWeight: 600,
color: 'text.primary',
}}
>
{modelName}
</Typography>
</Box>
{/* Steps Count */}
{metadata && (
<>
<Box sx={{ width: '1px', height: 16, backgroundColor: 'divider' }} />
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<Typography
variant="caption"
sx={{
fontSize: '0.75rem',
fontWeight: 700,
color: 'text.primary',
mr: 0.5,
}}
>
{metadata.numberOfSteps}
</Typography>
<Typography
variant="caption"
sx={{
fontSize: '0.7rem',
fontWeight: 400,
color: 'text.secondary',
}}
>
{metadata.numberOfSteps === 1 ? 'Step' : 'Steps'}
</Typography>
</Box>
</>
)}
{/* Time */}
{(isAgentProcessing || metadata) && (
<>
<Box sx={{ width: '1px', height: 16, backgroundColor: 'divider' }} />
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<AccessTimeIcon sx={{ fontSize: '0.85rem', color: 'primary.main' }} />
<Typography
variant="caption"
sx={{
fontSize: '0.75rem',
fontWeight: 700,
color: 'text.primary',
minWidth: '45px',
textAlign: 'left',
}}
>
{elapsedTime.toFixed(1)}s
</Typography>
</Box>
</>
)}
{/* Input Tokens */}
{metadata && metadata.inputTokensUsed > 0 && (
<>
<Box sx={{ width: '1px', height: 16, backgroundColor: 'divider' }} />
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<InputIcon
sx={{
fontSize: '0.85rem',
color: 'primary.main',
transition: 'all 0.2s ease',
animation: inputTokenFlash ? `${iconFlash} 0.8s ease-out` : 'none',
}}
/>
<Box
sx={{
transition: 'all 0.2s ease',
animation: inputTokenFlash ? `${tokenFlash} 0.8s ease-out` : 'none',
}}
>
<Typography
variant="caption"
sx={{
fontSize: '0.75rem',
fontWeight: 700,
color: 'text.primary',
}}
>
{metadata.inputTokensUsed.toLocaleString()}
</Typography>
</Box>
</Box>
</>
)}
{/* Output Tokens */}
{metadata && metadata.outputTokensUsed > 0 && (
<>
<Box sx={{ width: '1px', height: 16, backgroundColor: 'divider' }} />
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<OutputIcon
sx={{
fontSize: '0.85rem',
color: 'primary.main',
transition: 'all 0.2s ease',
animation: outputTokenFlash ? `${iconFlash} 0.8s ease-out` : 'none',
}}
/>
<Box
sx={{
transition: 'all 0.2s ease',
animation: outputTokenFlash ? `${tokenFlash} 0.8s ease-out` : 'none',
}}
>
<Typography
variant="caption"
sx={{
fontSize: '0.75rem',
fontWeight: 700,
color: 'text.primary',
}}
>
{metadata.outputTokensUsed.toLocaleString()}
</Typography>
</Box>
</Box>
</>
)}
</Box>
)}
</Toolbar>
</AppBar>
);
};
|