import React from 'react'; import { PROVIDER_META, STATUS_TEXT, WARN_STATUSES, FAIL_STATUSES, shortModel, formatLatency, describeRoute } from '../lib/providers'; const WARN_COLOR = '#fb923c'; // orange: rate-limited / try again later const RED = '#f87171'; /** * Merges the static chain (/providers) with the route a request actually took, * so every station knows whether it answered, failed, or wasn't needed. */ function stationStates(chain, engine) { const byId = Object.fromEntries((engine?.attempts || []).map((a) => [a.provider, a])); const winnerIndex = chain.findIndex((p) => byId[p.provider]?.status === 'ok'); return chain.map((p, i) => { const attempt = byId[p.provider]; if (attempt) return { ...p, status: attempt.status, detail: attempt.detail, latency: attempt.latency_ms }; if (!p.configured) return { ...p, status: 'unconfigured', detail: 'no API key set' }; if (engine && winnerIndex !== -1 && i > winnerIndex) return { ...p, status: 'standby', detail: 'not needed for this request' }; if (p.cooldown_seconds > 0) return { ...p, status: 'cooldown', detail: `rate-limited, retry in ${p.cooldown_seconds}s` }; return { ...p, status: 'ready', detail: 'ready' }; }); } function statusColor(station) { if (WARN_STATUSES.has(station.status)) return WARN_COLOR; if (FAIL_STATUSES.has(station.status)) return RED; return PROVIDER_META[station.provider]?.color || '#94a3b8'; } function Dot({ station, size = 14 }) { const color = PROVIDER_META[station.provider]?.color || '#94a3b8'; const { status } = station; const base = { width: size, height: size }; if (status === 'ok') { return ( ); } if (status === 'ready') { return ; } if (status === 'standby') { return ; } if (status === 'unconfigured') { return ; } const ring = WARN_STATUSES.has(status) ? WARN_COLOR : RED; return ( ); } /** * The Groq -> Gemini -> NVIDIA fallback chain drawn as a tiny graph. * - variant "full": stations with model names and a route caption * - variant "compact": a one-line pill for headers */ const EngineChain = ({ chain, engine, busy = false, variant = 'full', className = '' }) => { const stations = stationStates(chain, engine); if (variant === 'compact') { return (
{stations.map((s, i) => ( {i > 0 && ›} {s.name} ))}
); } const tried = new Set((engine?.attempts || []).map((a) => a.provider)); return (
{/* Travelling light while a request is in flight */} {busy && (
)} {stations.map((s, i) => { // A lit connector means the request was handed off along it const handedOff = i > 0 && tried.has(s.provider) && stations[i - 1].status !== 'ok'; return ( {i > 0 && (
)}
{s.name} {shortModel(s.model)} {s.status === 'ok' && s.latency != null ? formatLatency(s.latency) : STATUS_TEXT[s.status] || s.status}
); })}
); }; export default EngineChain;