Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

50.9 KB
Raw Blame
"use client";

/** System health diagnostics and independently refreshed local concurrency gates. */

import { useState, useEffect, useCallback } from "react";

import { Card } from "@/shared/components";
import { AI_PROVIDERS } from "@/shared/constants/providers";
import { getProviderDisplayName } from "@/lib/display/names";
import { useProviderNodeMap, resolveProviderName } from "@/lib/display/useProviderNodeMap";
import { compareTr } from "@/shared/utils/turkishText";
import { useLocale, useTranslations } from "next-intl";
import TelemetryCard from "./TelemetryCard";
import ProviderHealthAutopilotCard from "./ProviderHealthAutopilotCard";
import ProviderHealthMatrixCard from "./ProviderHealthMatrixCard";
import ConcurrencyQueuesCard from "./ConcurrencyQueuesCard";

function formatUptime(seconds) {
  const d = Math.floor(seconds / 86400);
  const h = Math.floor((seconds % 86400) / 3600);
  const m = Math.floor((seconds % 3600) / 60);
  if (d > 0) return `${d}d ${h}h ${m}m`;
  if (h > 0) return `${h}h ${m}m`;
  return `${m}m`;
}

function formatBytes(bytes) {
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}

function formatRelativeTime(timestamp) {
  if (!timestamp || !Number.isFinite(timestamp)) return null;
  const diffMs = Math.max(0, Date.now() - timestamp);
  const diffMinutes = Math.floor(diffMs / 60000);
  if (diffMinutes < 1) return "<1m";
  if (diffMinutes < 60) return `${diffMinutes}m`;
  const diffHours = Math.floor(diffMinutes / 60);
  if (diffHours < 24) return `${diffHours}h`;
  const diffDays = Math.floor(diffHours / 24);
  return `${diffDays}d`;
}

const CB_STYLES = {
  CLOSED: { bg: "bg-green-500/10", text: "text-green-500", labelKey: "healthy" },
  OPEN: { bg: "bg-red-500/10", text: "text-red-500", labelKey: "down" },
  HALF_OPEN: { bg: "bg-amber-500/10", text: "text-amber-500", labelKey: "recovering" },
};

export default function HealthPage() {
  return (
    <div className="space-y-6">
      <ConcurrencyQueuesCard />
      <HealthSnapshot />
    </div>
  );
}

function HealthSnapshot() {
  const locale = useLocale();
  const t = useTranslations("health");
  const tc = useTranslations("common");
  const tp = useTranslations("providers");
  const nodeMap = useProviderNodeMap();
  const [data, setData] = useState(null);
  const [dbHealth, setDbHealth] = useState(null);
  const [dbHealthError, setDbHealthError] = useState(null);
  const [error, setError] = useState(null);
  const [lastRefresh, setLastRefresh] = useState(null);
  const [cache, setCache] = useState(null);
  const [signatureCache, setSignatureCache] = useState(null);
  const [degradation, setDegradation] = useState(null);
  const [resetting, setResetting] = useState(false);
  const [repairingDb, setRepairingDb] = useState(false);
  const [unblocking, setUnblocking] = useState(false);
  const [unblockingKey, setUnblockingKey] = useState<string | null>(null);
  const [showAdvanced, setShowAdvanced] = useState(false);

  const fetchHealth = useCallback(async () => {
    try {
      const res = await fetch("/api/monitoring/health");
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      const json = await res.json();
      setData(json);
      setError(null);
      setLastRefresh(new Date());
    } catch (err) {
      setError(err.message);
    }
  }, []);

  const fetchDbHealth = useCallback(async () => {
    try {
      const res = await fetch("/api/db/health");
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      const json = await res.json();
      setDbHealth(json);
      setDbHealthError(null);
    } catch (err) {
      setDbHealthError(err.message);
    }
  }, []);

  // Fetch cache, signature cache, and degradation stats.
  const fetchExtras = useCallback(async () => {
    const results = await Promise.allSettled([
      fetch("/api/cache/stats").then((r) => r.json()),
      fetch("/api/rate-limits").then((r) => r.json()),
      fetch("/api/health/degradation").then((r) => r.json()),
    ]);
    if (results[0].status === "fulfilled") setCache(results[0].value);
    if (results[1].status === "fulfilled" && results[1].value.cacheStats) {
      setSignatureCache(results[1].value.cacheStats);
    }
    if (results[2].status === "fulfilled") setDegradation(results[2].value);
  }, []);

  useEffect(() => {
    const initialFetch = setTimeout(() => {
      void fetchHealth();
      void fetchExtras();
      void fetchDbHealth();
    }, 0);
    const interval = setInterval(() => {
      void fetchHealth();
      void fetchExtras();
      void fetchDbHealth();
    }, 15000);
    return () => {
      clearTimeout(initialFetch);
      clearInterval(interval);
    };
  }, [fetchHealth, fetchExtras, fetchDbHealth]);

  const handleResetHealth = async () => {
    if (!confirm(t("resetConfirm"))) return;
    setResetting(true);
    try {
      const res = await fetch("/api/monitoring/health", { method: "DELETE" });
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      // Refresh health data immediately
      await fetchHealth();
      await fetchExtras();
    } catch (err) {
      console.error("Failed to reset health:", err);
    } finally {
      setResetting(false);
    }
  };

  const handleUnblockAll = async () => {
    setUnblocking(true);
    try {
      const res = await fetch("/api/resilience/model-cooldowns", {
        method: "DELETE",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ all: true }),
      });
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      await fetchHealth();
    } catch (err) {
      console.error("Failed to unblock all models:", err);
    } finally {
      setUnblocking(false);
    }
  };

  const handleUnblockOne = async (provider: string, model: string) => {
    const key = `${provider}::${model}`;
    setUnblockingKey(key);
    try {
      const res = await fetch("/api/resilience/model-cooldowns", {
        method: "DELETE",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ provider, model }),
      });
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      await fetchHealth();
    } catch (err) {
      console.error(`Failed to unblock ${provider}/${model}:`, err);
    } finally {
      setUnblockingKey(null);
    }
  };

  const handleRepairDb = async () => {
    setRepairingDb(true);
    try {
      const res = await fetch("/api/db/health", { method: "POST" });
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      const json = await res.json();
      setDbHealth(json);
      setDbHealthError(null);
      await fetchHealth();
      await fetchExtras();
    } catch (err) {
      console.error("Failed to repair database health:", err);
      setDbHealthError(err.message);
    } finally {
      setRepairingDb(false);
    }
  };

  const fmtMs = (ms) =>
    ms != null ? t("millisecondsShort", { value: Math.round(ms) }) : t("notAvailable");

  if (!data && !error) {
    return (
      <div className="flex items-center justify-center min-h-100">
        <div className="text-center">
          <div className="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-primary" />
          <p className="text-text-muted mt-4">{t("loadingHealth")}</p>
        </div>
      </div>
    );
  }

  if (error && !data) {
    return (
      <div>
        <div className="bg-red-500/10 border border-red-500/30 rounded-xl p-6 text-center">
          <span className="material-symbols-outlined text-red-500 text-[32px] mb-2">error</span>
          <p className="text-red-400">{t("failedToLoad", { error })}</p>
          <button
            onClick={fetchHealth}
            className="mt-4 px-4 py-2 rounded-lg bg-primary/10 text-primary text-sm hover:bg-primary/20 transition-colors"
          >
            {t("retry")}
          </button>
        </div>
      </div>
    );
  }

  const {
    system,
    providerHealth,
    providerSummary,
    rateLimitStatus,
    learnedLimits,
    lockouts,
    sessions,
    quotaMonitor,
  } = data;
  const cbEntries = Object.entries(providerHealth || {});
  const lockoutEntries = Object.entries(lockouts || {});

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-end gap-3">
        {lastRefresh && (
          <span className="text-xs text-text-muted">
            {t("updatedAt", { time: lastRefresh.toLocaleTimeString(locale) })}
          </span>
        )}
        <button
          onClick={() => {
            fetchHealth();
            fetchExtras();
            fetchDbHealth();
          }}
          className="p-2 rounded-lg bg-surface hover:bg-surface/80 text-text-muted hover:text-text-main transition-colors"
          title={tc("refresh")}
        >
          <span className="material-symbols-outlined text-[18px]">refresh</span>
        </button>
      </div>

      {/* Status Banner */}
      {/* Verdict Header */}
      <div className="mb-8">
        <h1 className="text-3xl font-bold mb-2">
          {data.status === "healthy"
            ? t("healthVerdictReady")
            : data.status === "cooling"
              ? t("healthVerdictCoolingDown")
              : t("healthVerdictActionRequired")}
        </h1>
        <p className="text-text-muted text-lg">{t("healthSubtitle")}</p>
      </div>

      {/* Status Details */}
      <div
        role="status"
        aria-live="polite"
        className={`rounded-xl p-4 flex items-center gap-3 ${
          data.status === "healthy"
            ? "bg-green-500/10 border border-green-500/20"
            : "bg-red-500/10 border border-red-500/20"
        }`}
      >
        <span
          className={`material-symbols-outlined text-[24px] ${
            data.status === "healthy" ? "text-green-500" : "text-red-500"
          }`}
        >
          {data.status === "healthy" ? "check_circle" : "error"}
        </span>
        <span className={data.status === "healthy" ? "text-green-400" : "text-red-400"}>
          {data.status === "healthy" ? t("allOperational") : t("issuesDetected")}
        </span>
      </div>

      {/* Advanced Diagnostics Section */}
      <div className="mt-8">
        <div className="flex justify-between items-center mb-4">
          <h2 className="text-xl font-semibold">{t("advancedDiagnosticsTitle")}</h2>
          <button
            onClick={() => setShowAdvanced(!showAdvanced)}
            className="text-primary hover:underline"
          >
            {showAdvanced ? t("hide") : t("show")}
          </button>
        </div>
        <div className={showAdvanced ? "block" : "hidden"}>
          <TelemetryCard />
          <ProviderHealthAutopilotCard />
          <ProviderHealthMatrixCard />
        </div>
      </div>

      <Card className="p-5">
        <div className="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
          <div>
            <div className="flex items-center gap-3 mb-2">
              <div
                className={`flex items-center justify-center size-9 rounded-lg ${
                  dbHealth?.isHealthy
                    ? "bg-green-500/10 text-green-500"
                    : "bg-amber-500/10 text-amber-500"
                }`}
              >
                <span className="material-symbols-outlined text-[18px]">database</span>
              </div>
              <div>
                <h2 className="text-lg font-semibold text-text-main">{t("databaseHealth")}</h2>
                <p className="text-sm text-text-muted">{t("databaseHealthDescription")}</p>
              </div>
            </div>
            <div className="grid grid-cols-1 sm:grid-cols-3 gap-3 mt-4">
              <div className="rounded-xl border border-border bg-surface/50 p-3">
                <p className="text-xs uppercase tracking-wide text-text-muted">{t("status")}</p>
                <p
                  className={`mt-1 text-sm font-medium ${
                    dbHealth?.isHealthy ? "text-green-400" : "text-amber-400"
                  }`}
                >
                  {dbHealth?.isHealthy ? t("healthy") : t("attentionNeeded")}
                </p>
              </div>
              <div className="rounded-xl border border-border bg-surface/50 p-3">
                <p className="text-xs uppercase tracking-wide text-text-muted">{t("issues")}</p>
                <p className="mt-1 text-sm font-medium text-text-main">
                  {dbHealth?.issues?.length ?? 0}
                </p>
              </div>
              <div className="rounded-xl border border-border bg-surface/50 p-3">
                <p className="text-xs uppercase tracking-wide text-text-muted">{t("repairs")}</p>
                <p className="mt-1 text-sm font-medium text-text-main">
                  {dbHealth?.repairedCount ?? 0}
                </p>
              </div>
            </div>
          </div>
          <div className="flex flex-col items-stretch gap-2 min-w-45">
            <button
              onClick={handleRepairDb}
              disabled={repairingDb}
              className="px-4 py-2 rounded-lg bg-primary/10 text-primary text-sm hover:bg-primary/20 transition-colors disabled:opacity-60 disabled:cursor-not-allowed"
            >
              {repairingDb ? t("repairing") : t("runAutoRepair")}
            </button>
            {dbHealth?.backupCreated && (
              <p className="text-xs text-text-muted">{t("repairBackupCreated")}</p>
            )}
            {dbHealthError && <p className="text-xs text-red-400">{dbHealthError}</p>}
          </div>
        </div>
        {Array.isArray(dbHealth?.issues) && dbHealth.issues.length > 0 && (
          <div className="mt-4 space-y-2">
            {dbHealth.issues.map((issue, index) => (
              <div
                key={`${issue.table}-${issue.type}-${index}`}
                className="rounded-xl border border-amber-500/20 bg-amber-500/5 px-3 py-2"
              >
                <div className="flex items-center justify-between gap-3">
                  <p className="text-sm text-text-main">{issue.description}</p>
                  <span className="text-xs text-amber-400">{issue.count}</span>
                </div>
                <p className="text-xs text-text-muted mt-1">
                  {issue.table} · {issue.type}
                </p>
              </div>
            ))}
          </div>
        )}
      </Card>

      {/* System Info Cards */}
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
        <Card className="p-4">
          <div className="flex items-center gap-3 mb-2">
            <div className="flex items-center justify-center size-8 rounded-lg bg-primary/10 text-primary">
              <span className="material-symbols-outlined text-[18px]">timer</span>
            </div>
            <span className="text-sm text-text-muted">{t("uptime")}</span>
          </div>
          <p className="text-xl font-semibold text-text-main">{formatUptime(system.uptime)}</p>
        </Card>

        <Card className="p-4">
          <div className="flex items-center gap-3 mb-2">
            <div className="flex items-center justify-center size-8 rounded-lg bg-blue-500/10 text-blue-500">
              <span className="material-symbols-outlined text-[18px]">info</span>
            </div>
            <span className="text-sm text-text-muted">{t("version")}</span>
          </div>
          <p className="text-xl font-semibold text-text-main">v{system.version}</p>
          <p className="text-xs text-text-muted mt-1">
            {t("nodeVersion", { version: system.nodeVersion })}
          </p>
        </Card>

        <Card className="p-4">
          <div className="flex items-center gap-3 mb-2">
            <div className="flex items-center justify-center size-8 rounded-lg bg-purple-500/10 text-purple-500">
              <span className="material-symbols-outlined text-[18px]">memory</span>
            </div>
            <span className="text-sm text-text-muted">{t("memoryRss")}</span>
          </div>
          <p className="text-xl font-semibold text-text-main">
            {formatBytes(system.memoryUsage?.rss || 0)}
          </p>
          <p className="text-xs text-text-muted mt-1">
            {t("heap")}: {formatBytes(system.memoryUsage?.heapUsed || 0)} /{" "}
            {formatBytes(system.memoryUsage?.heapTotal || 0)}
          </p>
        </Card>

        <Card className="p-4">
          <div className="flex items-center gap-3 mb-2">
            <div className="flex items-center justify-center size-8 rounded-lg bg-amber-500/10 text-amber-500">
              <span className="material-symbols-outlined text-[18px]">dns</span>
            </div>
            <span className="text-sm text-text-muted">{t("providers")}</span>
          </div>
          <p className="text-xl font-semibold text-text-main">
            {providerSummary?.configuredCount ?? cbEntries.length}
          </p>
          <p
            className="text-[11px] text-text-muted mt-1 inline-flex items-center gap-1"
            title={t("configuredProvidersHint")}
          >
            {t("configuredProvidersLabel")}
            <span className="material-symbols-outlined text-[12px]" aria-hidden="true">
              help
            </span>
          </p>
          <p
            className="text-xs text-text-muted inline-flex items-center gap-1"
            title={t("activeProvidersHint")}
          >
            {t("activeProviders", { count: providerSummary?.activeCount ?? 0 })}
            <span className="material-symbols-outlined text-[12px]" aria-hidden="true">
              info
            </span>
          </p>
          <p
            className="text-xs text-text-muted inline-flex items-center gap-1"
            title={t("monitoredProvidersHint")}
          >
            {t("monitoredProviders", {
              count: providerSummary?.monitoredCount ?? cbEntries.length,
            })}
            <span className="material-symbols-outlined text-[12px]" aria-hidden="true">
              info
            </span>
          </p>
        </Card>
      </div>

      {/* Session & Quota Observability */}
      <div className="grid grid-cols-1 xl:grid-cols-2 gap-4">
        <Card className="p-5">
          <div className="flex items-center justify-between mb-4">
            <h2 className="text-lg font-semibold text-text-main flex items-center gap-2">
              <span className="material-symbols-outlined text-[20px] text-primary">groups</span>
              {t("sessionActivity")}
            </h2>
            <span className="text-xs text-text-muted">
              {t("activeCount", { count: sessions?.activeCount ?? 0 })}
            </span>
          </div>
          <div className="grid grid-cols-2 gap-3 mb-4">
            <div className="rounded-xl border border-border/40 bg-surface/30 p-3">
              <div className="text-xs text-text-muted">{t("stickyBoundSessions")}</div>
              <div className="text-2xl font-semibold text-text-main mt-1">
                {sessions?.stickyBoundCount ?? 0}
              </div>
            </div>
            <div className="rounded-xl border border-border/40 bg-surface/30 p-3">
              <div className="text-xs text-text-muted">{t("sessionsByApiKey")}</div>
              <div className="text-2xl font-semibold text-text-main mt-1">
                {Object.keys(sessions?.byApiKey || {}).length}
              </div>
            </div>
          </div>
          {sessions?.top?.length > 0 ? (
            <div className="space-y-2">
              {sessions.top.slice(0, 5).map((session: any) => (
                <div
                  key={session.sessionId}
                  className="rounded-lg border border-border/30 bg-surface/20 p-3 flex items-center justify-between gap-3"
                >
                  <div className="min-w-0">
                    <div className="font-mono text-xs text-text-main truncate">
                      {session.sessionId}
                    </div>
                    <div className="text-xs text-text-muted mt-1">
                      {t("requestCount", { count: session.requestCount })}
                      {session.connectionId ? ` • ${session.connectionId.slice(0, 8)}…` : ""}
                    </div>
                  </div>
                  <div className="text-right text-xs text-text-muted shrink-0">
                    <div>
                      {t("idleSeconds", { count: Math.round((session.idleMs || 0) / 1000) })}
                    </div>
                    <div>{t("ageSeconds", { count: Math.round((session.ageMs || 0) / 1000) })}</div>
                  </div>
                </div>
              ))}
            </div>
          ) : (
            <p className="text-sm text-text-muted">{t("noActiveSessionsTracked")}</p>
          )}
        </Card>

        <Card className="p-5">
          <div className="flex items-center justify-between mb-4">
            <h2 className="text-lg font-semibold text-text-main flex items-center gap-2">
              <span className="material-symbols-outlined text-[20px] text-primary">radar</span>
              {t("quotaMonitors")}
            </h2>
            <span className="text-xs text-text-muted">
              {t("activeCount", { count: quotaMonitor?.active ?? 0 })}
            </span>
          </div>
          <div className="grid grid-cols-2 md:grid-cols-4 gap-3 mb-4">
            <div className="rounded-xl border border-border/40 bg-surface/30 p-3">
              <div className="text-xs text-text-muted">{t("alerting")}</div>
              <div className="text-2xl font-semibold text-amber-400 mt-1">
                {quotaMonitor?.alerting ?? 0}
              </div>
            </div>
            <div className="rounded-xl border border-border/40 bg-surface/30 p-3">
              <div className="text-xs text-text-muted">{t("limitExhausted")}</div>
              <div className="text-2xl font-semibold text-red-400 mt-1">
                {quotaMonitor?.exhausted ?? 0}
              </div>
            </div>
            <div className="rounded-xl border border-border/40 bg-surface/30 p-3">
              <div className="text-xs text-text-muted">{t("errors")}</div>
              <div className="text-2xl font-semibold text-orange-400 mt-1">
                {quotaMonitor?.errors ?? 0}
              </div>
            </div>
            <div className="rounded-xl border border-border/40 bg-surface/30 p-3">
              <div className="text-xs text-text-muted">{t("providers")}</div>
              <div className="text-2xl font-semibold text-text-main mt-1">
                {Object.keys(quotaMonitor?.byProvider || {}).length}
              </div>
            </div>
          </div>
          {quotaMonitor?.monitors?.length > 0 ? (
            <div className="space-y-2">
              {quotaMonitor.monitors.slice(0, 5).map((monitor: any) => (
                <div
                  key={`${monitor.sessionId}:${monitor.accountId}`}
                  className="rounded-lg border border-border/30 bg-surface/20 p-3 flex items-center justify-between gap-3"
                >
                  <div className="min-w-0">
                    <div className="text-sm font-medium text-text-main truncate">
                      {monitor.provider} • {monitor.accountId.slice(0, 8)}…
                    </div>
                    <div className="text-xs text-text-muted mt-1 truncate">
                      {monitor.sessionId} • {monitor.status}
                    </div>
                  </div>
                  <div className="text-right text-xs shrink-0">
                    <div
                      className={
                        monitor.status === "exhausted"
                          ? "text-red-400"
                          : monitor.status === "warning"
                            ? "text-amber-400"
                            : monitor.status === "error"
                              ? "text-orange-400"
                              : "text-text-main"
                      }
                    >
                      {typeof monitor.lastQuotaPercent === "number"
                        ? `${Math.round(monitor.lastQuotaPercent * 100)}%`
                        : "—"}
                    </div>
                    <div className="text-text-muted">
                      {monitor.nextPollDelayMs
                        ? `${Math.round(monitor.nextPollDelayMs / 1000)}s`
                        : "—"}
                    </div>
                  </div>
                </div>
              ))}
            </div>
          ) : (
            <p className="text-sm text-text-muted">{t("noSessionQuotaMonitorsActive")}</p>
          )}
        </Card>
      </div>

      {/* Graceful Degradation Status */}
      {degradation && degradation.features && degradation.features.length > 0 && (
        <Card className="p-5" role="region" aria-label={t("gracefulDegradationStatus")}>
          <div className="flex items-center justify-between mb-4">
            <h2 className="text-lg font-semibold text-text-main flex items-center gap-2">
              <span className="material-symbols-outlined text-[20px] text-primary">healing</span>
              {t("gracefulDegradationStatus")}
            </h2>
            <div className="flex items-center gap-3 text-xs text-text-muted font-medium">
              <span className="px-2 py-0.5 rounded bg-green-500/10 text-green-400">
                {t("degradationFull")}: {degradation.summary.full}
              </span>
              <span className="px-2 py-0.5 rounded bg-amber-500/10 text-amber-500">
                {t("degradationReduced")}: {degradation.summary.reduced}
              </span>
              <span className="px-2 py-0.5 rounded bg-orange-500/10 text-orange-500">
                {t("degradationMinimal")}: {degradation.summary.minimal}
              </span>
              <span className="px-2 py-0.5 rounded bg-red-500/10 text-red-500">
                {t("degradationDefault")}: {degradation.summary.default}
              </span>
            </div>
          </div>
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
            {degradation.features.map((feat: any) => {
              const bg =
                feat.level === "full"
                  ? "bg-green-500/5 border-green-500/10"
                  : feat.level === "reduced"
                    ? "bg-amber-500/5 border-amber-500/20"
                    : feat.level === "minimal"
                      ? "bg-orange-500/5 border-orange-500/20"
                      : "bg-red-500/5 border-red-500/20";
              const dot =
                feat.level === "full"
                  ? "bg-green-500"
                  : feat.level === "reduced"
                    ? "bg-amber-500"
                    : feat.level === "minimal"
                      ? "bg-orange-500"
                      : "bg-red-500";
              return (
                <div
                  key={feat.feature}
                  className={`rounded-lg p-3 border ${bg} flex flex-col gap-2`}
                >
                  <div className="flex items-center justify-between">
                    <span className="text-sm font-semibold capitalize flex items-center gap-2 text-(--text-primary,#fff)">
                      <span className={`w-2 h-2 rounded-full ${dot}`}></span>
                      {feat.feature}
                    </span>
                    <span className="text-xs uppercase tracking-wider font-bold opacity-70">
                      {feat.level}
                    </span>
                  </div>
                  <div className="text-xs text-(--text-secondary,#aaa)">{feat.capability}</div>
                  {feat.reason && (
                    <div
                      className="text-[10px] text-red-300 mt-1 bg-red-900/20 p-1.5 rounded"
                      title={feat.reason}
                    >
                      {feat.reason.length > 80 ? feat.reason.substring(0, 80) + "..." : feat.reason}
                    </div>
                  )}
                  <div className="text-[10px] text-(--text-muted,#666) text-right mt-1">
                    {t("sinceTime", {
                      time: new Date(feat.since).toLocaleTimeString(locale),
                    })}
                  </div>
                </div>
              );
            })}
          </div>
        </Card>
      )}

      {/* Cache Cards */}
      <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
        {/* Prompt Cache Card */}
        <Card className="p-4">
          <h3 className="text-sm font-semibold text-text-muted mb-3 flex items-center gap-2">
            <span className="material-symbols-outlined text-[18px]">cached</span>
            {t("promptCache")}
          </h3>
          {cache ? (
            <div className="space-y-2 text-sm">
              <div className="flex justify-between">
                <span className="text-text-muted">{t("entries")}</span>
                <span className="font-mono">
                  {cache.size}/{cache.maxSize}
                </span>
              </div>
              <div className="flex justify-between">
                <span className="text-text-muted">{t("hitRate")}</span>
                <span className="font-mono">{cache.hitRate?.toFixed(1) ?? 0}%</span>
              </div>
              <div className="flex justify-between">
                <span className="text-text-muted">{t("hitsMisses")}</span>
                <span className="font-mono">
                  {cache.hits ?? 0} / {cache.misses ?? 0}
                </span>
              </div>
            </div>
          ) : (
            <p className="text-sm text-text-muted">{t("noDataYet")}</p>
          )}
        </Card>

        {/* Signature Cache Card */}
        <Card className="p-4">
          <h3 className="text-sm font-semibold text-text-muted mb-3 flex items-center gap-2">
            <span className="material-symbols-outlined text-[18px]">database</span>
            {t("signatureCache")}
          </h3>
          {signatureCache ? (
            <div className="grid grid-cols-2 gap-2">
              {[
                {
                  label: t("signatureDefaults"),
                  value: signatureCache.defaultCount,
                  color: "text-text-muted",
                },
                {
                  label: t("signatureTool"),
                  value: `${signatureCache.tool.entries}/${signatureCache.tool.patterns}`,
                  color: "text-blue-400",
                },
                {
                  label: t("signatureFamily"),
                  value: `${signatureCache.family.entries}/${signatureCache.family.patterns}`,
                  color: "text-purple-400",
                },
                {
                  label: t("signatureSession"),
                  value: `${signatureCache.session.entries}/${signatureCache.session.patterns}`,
                  color: "text-cyan-400",
                },
              ].map(({ label, value, color }) => (
                <div
                  key={label}
                  className="text-center p-2 rounded-lg bg-surface/30 border border-border/30"
                >
                  <p className={`text-lg font-bold tabular-nums ${color}`}>{value}</p>
                  <p className="text-xs text-text-muted mt-0.5">{label}</p>
                </div>
              ))}
            </div>
          ) : (
            <p className="text-sm text-text-muted">{t("noDataYet")}</p>
          )}
        </Card>
      </div>

      {/* Provider Health */}
      <Card className="p-5" role="region" aria-label={t("providerHealthStatusAria")}>
        <div className="flex items-center justify-between mb-4">
          <h2 className="text-lg font-semibold text-text-main flex items-center gap-2">
            <span className="material-symbols-outlined text-[20px] text-primary">
              health_and_safety
            </span>
            {t("providerHealth")}
          </h2>
          <div className="flex items-center gap-3">
            {cbEntries.some(([, cb]: [string, any]) => cb.state !== "CLOSED") && (
              <button
                onClick={handleResetHealth}
                disabled={resetting}
                className={`flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-semibold transition-all ${
                  resetting
                    ? "bg-surface/50 text-text-muted cursor-wait"
                    : "bg-red-500/10 text-red-400 hover:bg-red-500/20 hover:text-red-300 border border-red-500/20"
                }`}
                title={t("resetAllTitle")}
              >
                {resetting ? (
                  <>
                    <span className="material-symbols-outlined text-[14px] animate-spin">
                      progress_activity
                    </span>
                    {t("resetting")}
                  </>
                ) : (
                  <>
                    <span className="material-symbols-outlined text-[14px]">restart_alt</span>
                    {t("resetAll")}
                  </>
                )}
              </button>
            )}
            {cbEntries.length > 0 && (
              <div className="flex items-center gap-3 text-xs text-text-muted">
                <span className="flex items-center gap-1">
                  <span className="size-2 rounded-full bg-green-500" /> {t("healthy")}
                </span>
                <span className="flex items-center gap-1">
                  <span className="size-2 rounded-full bg-amber-500" /> {t("recovering")}
                </span>
                <span className="flex items-center gap-1">
                  <span className="size-2 rounded-full bg-red-500" /> {t("down")}
                </span>
              </div>
            )}
          </div>
        </div>
        {cbEntries.length === 0 ? (
          <p className="text-sm text-text-muted text-center py-4">{t("noCBData")}</p>
        ) : (
          (() => {
            const unhealthy = cbEntries.filter(([, cb]: [string, any]) => cb.state !== "CLOSED");
            const healthy = cbEntries.filter(([, cb]: [string, any]) => cb.state === "CLOSED");
            return (
              <div className="space-y-4">
                {/* Unhealthy providers first */}
                {unhealthy.length > 0 && (
                  <div className="space-y-2">
                    <p className="text-xs font-medium text-red-400 uppercase tracking-wide">
                      {t("issuesLabel")}
                    </p>
                    {unhealthy.map(([provider, cb]: [string, any]) => {
                      const style = CB_STYLES[cb.state] || CB_STYLES.OPEN;
                      const providerInfo = AI_PROVIDERS[provider];
                      const displayName = getProviderDisplayName(
                        provider,
                        nodeMap.get(provider) ?? providerInfo
                      );
                      return (
                        <div
                          key={provider}
                          className={`rounded-lg p-3 ${style.bg} border border-white/5 flex items-center gap-3`}
                        >
                          <div
                            className="size-8 rounded-lg flex items-center justify-center shrink-0 text-xs font-bold"
                            style={{
                              backgroundColor: `${providerInfo?.color || "#888"}15`,
                              color: providerInfo?.color || "#888",
                            }}
                          >
                            {providerInfo?.textIcon || provider.slice(0, 2).toUpperCase()}
                          </div>
                          <div className="flex-1 min-w-0">
                            <div className="flex items-center gap-2">
                              <span className="text-sm font-medium text-text-main truncate">
                                {displayName}
                              </span>
                              <span
                                className={`text-xs font-semibold px-1.5 py-0.5 rounded ${style.bg} ${style.text}`}
                              >
                                {t(style.labelKey)}
                              </span>
                            </div>
                            <div className="text-xs text-text-muted mt-0.5">
                              {cb.failures === 1
                                ? t("failures", { count: cb.failures })
                                : t("failuresPlural", { count: cb.failures })}
                              {Number(cb.retryAfterMs) > 0 && (
                                <span className="ml-2">
                                  · {t("retryIn", { duration: fmtMs(cb.retryAfterMs) })}
                                </span>
                              )}
                              {cb.lastFailure && (
                                <span className="ml-2">
                                  · {t("lastFailure")}:{" "}
                                  {new Date(cb.lastFailure).toLocaleTimeString(locale)}
                                </span>
                              )}
                            </div>
                          </div>
                        </div>
                      );
                    })}
                  </div>
                )}

                {/* Healthy providers in compact grid */}
                {healthy.length > 0 && (
                  <div>
                    {unhealthy.length > 0 && (
                      <p className="text-xs font-medium text-green-400 uppercase tracking-wide mb-2">
                        {t("operational")}
                      </p>
                    )}
                    <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-2">
                      {healthy.map(([provider]) => {
                        const providerInfo = AI_PROVIDERS[provider];
                        const displayName = getProviderDisplayName(
                          provider,
                          nodeMap.get(provider) ?? providerInfo
                        );
                        return (
                          <div
                            key={provider}
                            className="rounded-lg p-2.5 bg-green-500/5 border border-white/5 flex items-center gap-2"
                          >
                            <span className="size-2 rounded-full bg-green-500 shrink-0" />
                            <span
                              className="text-xs font-medium text-text-main truncate"
                              title={displayName}
                            >
                              {displayName}
                            </span>
                          </div>
                        );
                      })}
                    </div>
                  </div>
                )}
              </div>
            );
          })()
        )}
      </Card>

      {/* Rate Limit Status */}
      {rateLimitStatus &&
        Object.keys(rateLimitStatus).length > 0 &&
        (() => {
          // Parse rate limit keys ("provider:connectionId" or "provider:connectionId:model")
          const parseKey = (key) => {
            const parts = key.split(":");
            const providerId = parts[0];
            const connectionId = parts[1] || "";
            const model = parts.slice(2).join(":") || null;

            // Resolve friendly name — prefer user-given name from provider node map
            let providerInfo = AI_PROVIDERS[providerId];
            const displayName = resolveProviderName(providerId, nodeMap);

            return { providerId, displayName, providerInfo, connectionId, model };
          };

          // Group entries by provider for a cleaner display
          const entries = Object.entries(rateLimitStatus).map(([key, status]: [string, any]) => ({
            key,
            ...parseKey(key),
            status,
          }));

          // Sort: active (queued/running > 0) first, then alphabetically
          entries.sort((a, b) => {
            const aActive = (a.status.queued || 0) + (a.status.running || 0);
            const bActive = (b.status.queued || 0) + (b.status.running || 0);
            if (aActive !== bActive) return bActive - aActive;
            return compareTr(a.displayName, b.displayName);
          });

          return (
            <Card className="p-5">
              <div className="flex items-center justify-between mb-4">
                <h2 className="text-lg font-semibold text-text-main flex items-center gap-2">
                  <span className="material-symbols-outlined text-[20px] text-amber-500">
                    speed
                  </span>
                  {t("rateLimitStatus")}
                </h2>
                <span className="text-xs text-text-muted">
                  {entries.length === 1
                    ? t("activeLimiters", { count: entries.length })
                    : t("activeLimitersPlural", { count: entries.length })}
                </span>
              </div>
              <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3">
                {entries.map(
                  ({ key, displayName, providerInfo, connectionId, model, status }: any) => {
                    const learned = learnedLimits?.[key] || null;
                    const isActive = (status.queued || 0) + (status.running || 0) > 0;
                    const isQueued = (status.queued || 0) > 0;
                    const learnedLimit =
                      typeof learned?.limit === "number" && learned.limit > 0
                        ? learned.limit
                        : null;
                    const learnedRemaining =
                      typeof learned?.remaining === "number" ? learned.remaining : null;
                    const learnedMinTime =
                      typeof learned?.minTime === "number" && learned.minTime > 0
                        ? learned.minTime
                        : null;
                    const learnedLastUpdated =
                      typeof learned?.lastUpdated === "number" ? learned.lastUpdated : null;
                    const lowRemaining =
                      learnedLimit != null &&
                      learnedRemaining != null &&
                      learnedRemaining / learnedLimit <= 0.1;
                    const exhausted = learnedRemaining != null && learnedRemaining <= 0;
                    const quotaProgress =
                      learnedLimit != null && learnedRemaining != null
                        ? Math.max(0, Math.min(100, (learnedRemaining / learnedLimit) * 100))
                        : null;
                    return (
                      <div
                        key={key}
                        className={`rounded-lg p-3 border transition-colors ${
                          exhausted
                            ? "bg-red-500/5 border-red-500/20"
                            : isQueued || lowRemaining
                              ? "bg-amber-500/5 border-amber-500/20"
                              : isActive
                                ? "bg-blue-500/5 border-blue-500/15"
                                : "bg-surface/30 border-white/5"
                        }`}
                        title={key}
                      >
                        <div className="flex items-center gap-2.5 mb-2">
                          <div
                            className="size-7 rounded-md flex items-center justify-center shrink-0 text-[10px] font-bold"
                            style={{
                              backgroundColor: `${providerInfo?.color || "#888"}15`,
                              color: providerInfo?.color || "#888",
                            }}
                          >
                            {providerInfo?.textIcon || displayName.slice(0, 2).toUpperCase()}
                          </div>
                          <div className="flex-1 min-w-0">
                            <p className="text-sm font-medium text-text-main truncate">
                              {displayName}
                            </p>
                            {connectionId && (
                              <p className="text-[10px] text-text-muted font-mono truncate">
                                {connectionId.length > 12
                                  ? connectionId.slice(0, 8) + "…"
                                  : connectionId}
                                {model && (
                                  <span className="ml-1 text-text-muted/60">· {model}</span>
                                )}
                              </p>
                            )}
                          </div>
                          <span
                            className={`inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-semibold ${
                              exhausted
                                ? "bg-red-500/15 text-red-400"
                                : isQueued || lowRemaining
                                  ? "bg-amber-500/15 text-amber-400"
                                  : isActive
                                    ? "bg-blue-500/15 text-blue-400"
                                    : "bg-green-500/10 text-green-400"
                            }`}
                          >
                            {exhausted
                              ? t("limitExhausted")
                              : isQueued || lowRemaining
                                ? t("queued")
                                : isActive
                                  ? tc("active")
                                  : t("ok")}
                          </span>
                        </div>
                        {quotaProgress != null && (
                          <div className="mb-3">
                            <div className="mb-1 flex items-center justify-between text-[11px] text-text-muted">
                              <span>{t("learnedFromHeaders")}</span>
                              <span>
                                {t("remainingOfLimit", {
                                  remaining: learnedRemaining,
                                  limit: learnedLimit,
                                })}
                              </span>
                            </div>
                            <div className="h-2 overflow-hidden rounded-full bg-surface/70">
                              <div
                                className={`h-full rounded-full ${
                                  exhausted
                                    ? "bg-red-500"
                                    : lowRemaining
                                      ? "bg-amber-500"
                                      : "bg-emerald-500"
                                }`}
                                style={{ width: `${quotaProgress}%` }}
                              />
                            </div>
                          </div>
                        )}
                        <div className="flex items-center gap-3 text-[11px] text-text-muted">
                          <span className="flex items-center gap-1">
                            <span className="material-symbols-outlined text-[12px]">schedule</span>
                            {t("queuedCount", { count: status.queued || 0 })}
                          </span>
                          <span className="flex items-center gap-1">
                            <span className="material-symbols-outlined text-[12px]">
                              play_arrow
                            </span>
                            {t("runningCount", { count: status.running || 0 })}
                          </span>
                        </div>
                        {(learnedMinTime != null || learnedLastUpdated != null) && (
                          <div className="mt-3 space-y-1 text-[11px] text-text-muted">
                            {learnedMinTime != null && (
                              <p>{t("throttleStatus", { value: `${learnedMinTime}ms/req` })}</p>
                            )}
                            {learnedLastUpdated != null && (
                              <p>
                                {t("lastHeaderUpdate", {
                                  age: formatRelativeTime(learnedLastUpdated) || t("notAvailable"),
                                })}
                              </p>
                            )}
                          </div>
                        )}
                      </div>
                    );
                  }
                )}
              </div>
            </Card>
          );
        })()}

      {/* Active Lockouts */}
      {lockoutEntries.length > 0 && (
        <Card className="p-5">
          <div className="flex items-center justify-between mb-4">
            <h2 className="text-lg font-semibold text-text-main flex items-center gap-2">
              <span className="material-symbols-outlined text-[20px] text-red-500">lock</span>
              {t("activeLockouts")}
            </h2>
            <button
              onClick={handleUnblockAll}
              disabled={unblocking}
              className="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-lg
                bg-amber-500/10 border border-amber-500/30 text-amber-600
                hover:bg-amber-500/15 hover:border-amber-500/50
                dark:text-amber-400 transition-all duration-200
                disabled:opacity-50 disabled:cursor-not-allowed"
            >
              <span className="material-symbols-outlined text-[16px]">lock_open</span>
              {unblocking ? "Unblocking..." : "Unblock all"}
            </button>
          </div>
          <div className="space-y-2">
            {lockoutEntries.map(([key, lockout]: [string, any]) => {
              const lockProvider = lockout.provider as string;
              const lockModel = lockout.model as string;
              const lockKey = `${lockProvider}::${lockModel}`;
              return (
                <div
                  key={key}
                  className="rounded-lg p-3 bg-red-500/5 border border-red-500/10 flex items-center justify-between"
                >
                  <div className="min-w-0">
                    <span className="text-sm font-medium text-text-main">
                      {lockProvider}/{lockModel}
                    </span>
                    {lockout.reason && (
                      <span className="text-xs text-text-muted ml-2">({lockout.reason})</span>
                    )}
                    {lockout.until && (
                      <span className="text-xs text-red-400 ml-2">
                        until {new Date(lockout.until).toLocaleTimeString()}
                      </span>
                    )}
                  </div>
                  <button
                    onClick={() => handleUnblockOne(lockProvider, lockModel)}
                    disabled={unblockingKey === lockKey}
                    className="flex items-center gap-1 px-2.5 py-1 text-xs font-medium rounded-lg
                      bg-amber-500/10 border border-amber-500/20 text-amber-600
                      hover:bg-amber-500/15 hover:border-amber-500/40
                      dark:text-amber-400 transition-all duration-200
                      disabled:opacity-50 disabled:cursor-not-allowed flex-shrink-0"
                  >
                    <span className="material-symbols-outlined text-[14px]">lock_open</span>
                    {unblockingKey === lockKey ? "..." : "Unblock"}
                  </button>
                </div>
              );
            })}
          </div>
        </Card>
      )}
    </div>
  );
}