"use client"; import { useEffect, useState } from "react"; import { useTranslations } from "next-intl"; type ActiveRequestRow = { model: string; provider: string; account: string; startedAt: number; runningTimeMs: number; count: number; clientEndpoint?: string | null; clientRequest?: unknown; providerRequest?: unknown; providerUrl?: string | null; }; function formatDuration(ms: number): string { const totalSeconds = Math.max(0, Math.floor(ms / 1000)); const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; if (minutes <= 0) return `${seconds}s`; if (minutes < 60) return `${minutes}m ${seconds}s`; const hours = Math.floor(minutes / 60); return `${hours}h ${minutes % 60}m`; } export default function ActiveRequestsPanel() { const t = useTranslations("logs"); const [rows, setRows] = useState([]); const [loading, setLoading] = useState(true); const [selectedRow, setSelectedRow] = useState(null); useEffect(() => { let cancelled = false; const load = async () => { try { const res = await fetch("/api/logs/active", { cache: "no-store" }); if (!res.ok) return; const data = await res.json(); if (!cancelled) { setRows(Array.isArray(data.activeRequests) ? data.activeRequests : []); } } catch (error) { if (!cancelled) { console.error("Failed to load active requests:", error); } } finally { if (!cancelled) { setLoading(false); } } }; load(); const interval = setInterval(load, 3000); return () => { cancelled = true; clearInterval(interval); }; }, []); const handleClearAll = async () => { if (!window.confirm(t("confirmClearActiveRequests") || "Clear all active requests?")) return; try { const res = await fetch("/api/logs/active", { method: "DELETE" }); if (res.ok) { setRows([]); setSelectedRow(null); } } catch (error) { console.error("Failed to clear active requests:", error); } }; if (!loading && rows.length === 0) { return null; } return (

{t("runningRequests")}

{t("runningRequestsDesc")}

{loading ? t("loading") : t("activeCount", { count: rows.length })}
{rows.length > 0 && ( )}
{rows.map((row) => ( ))}
{t("model")} {t("provider")} {t("account")} {t("elapsed")} {t("count")} {t("payloads")}
{row.model} {row.provider} {row.account} {formatDuration(row.runningTimeMs)} {row.count}
{selectedRow && (

{selectedRow.provider} / {selectedRow.model}

{t("runningRequestDetailMeta", { account: selectedRow.account, elapsed: formatDuration(selectedRow.runningTimeMs), })}

{t("clientPayload")}

{selectedRow.clientEndpoint || t("notAvailable")}

                  {JSON.stringify(selectedRow.clientRequest || {}, null, 2)}
                
{t("upstreamPayload")}

{selectedRow.providerUrl || t("upstreamNotSentYet")}

                  {JSON.stringify(selectedRow.providerRequest || {}, null, 2)}
                
)}
); }