Spaces:
Sleeping
Sleeping
File size: 7,039 Bytes
6cdce85 |
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 |
'use client';
import { useEffect, useState, useCallback } from 'react';
import { Loader2, Server, Cpu, Zap, Clock, AlertCircle } from 'lucide-react';
import { clsx } from 'clsx';
import type { StatusResponse } from '@/app/api/status/route';
type LoadingPhase =
| 'sending' // Initial request sent
| 'cold_start' // Workers starting up
| 'initializing' // Model loading
| 'processing' // Actively generating
| 'streaming'; // Receiving response
interface LoadingStatusProps {
isLoading: boolean;
hasStartedStreaming: boolean;
onStatusChange?: (status: StatusResponse | null) => void;
}
const PHASE_CONFIG: Record<LoadingPhase, {
icon: React.ElementType;
label: string;
color: string;
pulseColor: string;
}> = {
sending: {
icon: Zap,
label: 'Sending request...',
color: 'text-blue-400',
pulseColor: 'bg-blue-400',
},
cold_start: {
icon: Server,
label: 'Starting worker...',
color: 'text-amber-400',
pulseColor: 'bg-amber-400',
},
initializing: {
icon: Cpu,
label: 'Loading model...',
color: 'text-purple-400',
pulseColor: 'bg-purple-400',
},
processing: {
icon: Loader2,
label: 'Generating response...',
color: 'text-teal-400',
pulseColor: 'bg-teal-400',
},
streaming: {
icon: Zap,
label: 'Receiving...',
color: 'text-emerald-400',
pulseColor: 'bg-emerald-400',
},
};
export function LoadingStatus({ isLoading, hasStartedStreaming, onStatusChange }: LoadingStatusProps) {
const [phase, setPhase] = useState<LoadingPhase>('sending');
const [elapsedTime, setElapsedTime] = useState(0);
const [estimatedWait, setEstimatedWait] = useState<number | undefined>();
const [statusMessage, setStatusMessage] = useState<string>('');
// Poll for status while loading
const checkStatus = useCallback(async () => {
try {
const response = await fetch('/api/status');
if (response.ok) {
const status: StatusResponse = await response.json();
onStatusChange?.(status);
// Map status to phase
if (status.status === 'cold_start') {
setPhase('cold_start');
setStatusMessage(status.message);
} else if (status.status === 'initializing') {
setPhase('initializing');
setStatusMessage(status.message);
} else if (status.status === 'processing') {
setPhase('processing');
setStatusMessage(status.message);
}
if (status.estimatedWait) {
setEstimatedWait(status.estimatedWait);
}
}
} catch {
// Silently fail, keep current phase
}
}, [onStatusChange]);
// Start polling when loading starts
useEffect(() => {
if (!isLoading) {
setPhase('sending');
setElapsedTime(0);
setEstimatedWait(undefined);
setStatusMessage('');
return;
}
// Initial status check
checkStatus();
// Poll every 2 seconds while loading and not streaming
const statusInterval = setInterval(() => {
if (!hasStartedStreaming) {
checkStatus();
}
}, 2000);
// Track elapsed time
const timeInterval = setInterval(() => {
setElapsedTime((prev) => prev + 1);
}, 1000);
return () => {
clearInterval(statusInterval);
clearInterval(timeInterval);
};
}, [isLoading, hasStartedStreaming, checkStatus]);
// Update phase based on streaming state
useEffect(() => {
if (hasStartedStreaming) {
setPhase('streaming');
}
}, [hasStartedStreaming]);
// After 3 seconds without response, likely a cold start
useEffect(() => {
if (isLoading && !hasStartedStreaming && elapsedTime >= 3 && phase === 'sending') {
setPhase('cold_start');
}
}, [isLoading, hasStartedStreaming, elapsedTime, phase]);
if (!isLoading) return null;
const config = PHASE_CONFIG[phase];
const Icon = config.icon;
const showEstimate = estimatedWait && phase !== 'streaming';
// Calculate progress percentage
const progress = estimatedWait ? Math.min((elapsedTime / estimatedWait) * 100, 95) : undefined;
return (
<div className="flex flex-col gap-2">
{/* Main status indicator */}
<div className="flex items-center gap-3">
{/* Animated icon */}
<div className="relative">
<div className={clsx(
'w-8 h-8 rounded-lg flex items-center justify-center',
'bg-zinc-800/80 border border-zinc-700/50'
)}>
<Icon className={clsx(
'w-4 h-4',
config.color,
phase !== 'streaming' && 'animate-pulse'
)} />
</div>
{/* Pulse effect for cold start */}
{(phase === 'cold_start' || phase === 'initializing') && (
<span className={clsx(
'absolute -top-0.5 -right-0.5 w-2.5 h-2.5 rounded-full',
config.pulseColor,
'animate-ping opacity-75'
)} />
)}
</div>
{/* Status text */}
<div className="flex-1">
<div className={clsx('text-sm font-medium', config.color)}>
{statusMessage || config.label}
</div>
{/* Time indicator */}
<div className="flex items-center gap-2 text-xs text-zinc-500">
<Clock className="w-3 h-3" />
<span>{formatTime(elapsedTime)}</span>
{showEstimate && (
<>
<span className="text-zinc-600">•</span>
<span>~{formatTime(Math.max(0, estimatedWait - elapsedTime))} remaining</span>
</>
)}
</div>
</div>
</div>
{/* Progress bar for cold start */}
{showEstimate && progress !== undefined && (
<div className="h-1 bg-zinc-800 rounded-full overflow-hidden">
<div
className={clsx(
'h-full rounded-full transition-all duration-1000',
phase === 'cold_start' && 'bg-amber-500/50',
phase === 'initializing' && 'bg-purple-500/50',
phase === 'processing' && 'bg-teal-500/50'
)}
style={{ width: `${progress}%` }}
/>
</div>
)}
{/* Cold start explanation */}
{phase === 'cold_start' && elapsedTime >= 5 && (
<div className="flex items-start gap-2 p-2 bg-amber-500/5 border border-amber-500/20 rounded-lg text-xs">
<AlertCircle className="w-3.5 h-3.5 text-amber-400 mt-0.5 flex-shrink-0" />
<p className="text-zinc-400">
<span className="text-amber-400 font-medium">Cold start detected.</span>{' '}
The model is scaling up from zero. This typically takes 30-60 seconds on first request.
</p>
</div>
)}
</div>
);
}
function formatTime(seconds: number): string {
if (seconds < 60) {
return `${seconds}s`;
}
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${mins}m ${secs}s`;
}
|