"use client"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useTranslations } from "next-intl"; import Badge from "@/shared/components/Badge"; import Card from "@/shared/components/Card"; import { Skeleton } from "@/shared/components/Loading"; import { cn } from "@/shared/utils/cn"; type CallLogOption = { id: string; timestamp: string | null; status: number; model: string | null; requestedModel: string | null; provider: string | null; comboName: string | null; duration: number; }; type AnalyticsTranslator = ((key: string, values?: Record) => string) & { has?: (key: string) => boolean; }; function analyticsText(t: AnalyticsTranslator, key: string, fallback: string) { return typeof t.has === "function" && t.has(key) ? t(key) : fallback; } type ExplanationFactor = { name: string; value: string; status: "positive" | "warning" | "negative" | "neutral"; weight: number; contribution: number; details: string; }; type ExplainTarget = { id: string; timestamp: string | null; status: number; provider: string | null; model: string | null; comboStepId: string | null; comboExecutionKey: string | null; durationMs: number; outcome: "selected" | "related"; reason: string; }; type ReplayFactor = { key: string; value: number; weight: number; contribution: number; source: string; note?: string; }; type ReplayCandidate = { executionKey: string; stepId: string | null; provider: string; model: string; connectionId: string | null; label: string | null; rank: number; score: number; isRuntimeSelected: boolean; wouldSelectNow: boolean; factors: ReplayFactor[]; signals: { quotaRemainingPct: number | null; projectedQuotaRemainingPct: number | null; successRate: number | null; avgLatencyMs: number | null; forecastRisk: string | null; autopilotIssueCount: number; }; }; type DecisionReplay = { runtime: { source: "call_logs"; exact: true; selectedCallLogId: string; comboName: string | null; comboStepId: string | null; comboExecutionKey: string | null; provider: string | null; model: string | null; connectionId: string | null; status: number; timestamp: string | null; durationMs: number; }; recompute: null | { source: "comboScoringInspector"; method: "read_only_recompute"; exactRuntimeReplay: false; asOf: string; timeRange: "24h"; horizon: "7d"; comboId: string; comboName: string; strategy: string; taskType: "default"; recomputedSelectedExecutionKey: string | null; runtimeSelectedRank: number | null; runtimeSelectedScore: number | null; alignment: | "matches_recomputed_top_target" | "differs_from_recomputed_top_target" | "runtime_target_missing_from_recompute" | "not_combo_routed"; candidates: ReplayCandidate[]; warnings: string[]; }; warnings: string[]; }; type RouteExplainabilityResponse = { requestId: string; routeType: "combo" | "direct"; confidence: "high" | "medium" | "low"; summary: string; comboUsed: string | null; providerSelected: string | null; modelUsed: string | null; score: number; latencyActual: number; decision: { status: number; factors: ExplanationFactor[]; fallbacksTriggered: ExplainTarget[]; }; request: { timestamp: string | null; requestedModel: string | null; requestType: string | null; sourceFormat: string | null; targetFormat: string | null; cacheSource: string | null; apiKeyName: string | null; }; selectedTarget: { provider: string | null; model: string | null; account: string | null; connectionId: string | null; comboStepId: string | null; comboExecutionKey: string | null; durationMs: number; status: number; tokensIn: number; tokensOut: number; }; targetStats: { sampleSize: number; successRate: number; avgLatencyMs: number; lastStatus: "ok" | "error" | null; lastUsedAt: string | null; }; relatedTargets: ExplainTarget[]; evidence: Array<{ label: string; value: string; tone: ExplanationFactor["status"] }>; recommendations: string[]; limitations: string[]; decisionReplay?: DecisionReplay; }; function formatDate(value: string | null) { if (!value) return "n/a"; const date = new Date(value); if (Number.isNaN(date.getTime())) return value; return new Intl.DateTimeFormat(undefined, { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }).format(date); } function formatDuration(value: number) { if (!Number.isFinite(value) || value <= 0) return "n/a"; if (value >= 1000) return `${(value / 1000).toFixed(1)}s`; return `${Math.round(value)}ms`; } function getToneVariant(tone: ExplanationFactor["status"]) { if (tone === "positive") return "success" as const; if (tone === "warning") return "warning" as const; if (tone === "negative") return "error" as const; return "default" as const; } function getStatusVariant(status: number) { if (status >= 200 && status < 400) return "success" as const; if (status >= 400) return "error" as const; return "default" as const; } function RouteMetric({ icon, label, value }: { icon: string; label: string; value: string }) { return (
{icon} {label}
{value}
); } function ExplainabilitySkeleton() { return (
); } function FactorCard({ factor }: { factor: ExplanationFactor }) { const contributionPct = Math.round(factor.contribution * 100); const weightPct = Math.round(factor.weight * 100); return (
{factor.name}
{factor.value}
{contributionPct}%
Weight {weightPct}% · {factor.details}
); } function TargetTimeline({ targets }: { targets: ExplainTarget[] }) { if (targets.length === 0) { return
No related target evidence persisted yet.
; } return (
{targets.map((target) => (
{target.provider || "unknown"} / {target.model || "unknown"} {target.outcome === "selected" ? ( Selected ) : null}
{formatDate(target.timestamp)} · {target.comboStepId || "no step id"}
{target.reason}
HTTP {target.status || "n/a"} {formatDuration(target.durationMs)}
))}
); } function replayAlignmentLabel(alignment: NonNullable["alignment"]) { if (alignment === "matches_recomputed_top_target") return "Matches current top target"; if (alignment === "differs_from_recomputed_top_target") return "Differs from current top"; if (alignment === "runtime_target_missing_from_recompute") return "Target missing now"; return "Not combo routed"; } function replayAlignmentVariant(alignment: NonNullable["alignment"]) { if (alignment === "matches_recomputed_top_target") return "success" as const; if (alignment === "differs_from_recomputed_top_target") return "warning" as const; if (alignment === "runtime_target_missing_from_recompute") return "error" as const; return "default" as const; } function WhyThisTargetCard({ replay }: { replay: DecisionReplay | undefined }) { if (!replay) return null; const recompute = replay.recompute; const candidates = recompute?.candidates ?? []; return (
Exact runtime log
{replay.runtime.provider || "unknown"} / {replay.runtime.model || "unknown"}
{formatDate(replay.runtime.timestamp)} · {replay.runtime.comboStepId || "no step"}
HTTP {replay.runtime.status || "n/a"} call_logs exact
{recompute ? (
Read-only recompute
{recompute.comboName} · {recompute.strategy} · {recompute.timeRange} /{" "} {recompute.horizon}
{replayAlignmentLabel(recompute.alignment)}
) : (
No combo candidate ranking can be recomputed for this request.
)} {candidates.length > 0 ? (
{candidates.slice(0, 5).map((candidate) => (
#{candidate.rank} {candidate.provider} / {candidate.model} {candidate.isRuntimeSelected ? ( Runtime ) : null} {candidate.wouldSelectNow ? ( Top now ) : null}
{candidate.label || candidate.stepId || candidate.executionKey}
{Math.round(candidate.score * 100)}%
))}
) : null} {replay.warnings.length > 0 ? (
    {replay.warnings.map((warning) => (
  • info {warning}
  • ))}
) : null}
); } export default function RouteExplainabilityTab({ initialRequestId = "", }: { initialRequestId?: string; }) { const t = useTranslations("analytics") as AnalyticsTranslator; const [logs, setLogs] = useState([]); const [selectedId, setSelectedId] = useState(initialRequestId); const [explanation, setExplanation] = useState(null); const [logsLoading, setLogsLoading] = useState(true); const [explanationLoading, setExplanationLoading] = useState(false); const [error, setError] = useState(null); const fetchLogs = useCallback( async (signal?: AbortSignal) => { setLogsLoading(true); try { const response = await fetch("/api/usage/call-logs?limit=75", { signal, cache: "no-store", }); if (!response.ok) throw new Error("Failed to fetch request logs"); const data = (await response.json()) as CallLogOption[]; setLogs(data); setSelectedId((current) => { const preferredId = current || initialRequestId; if (preferredId && data.some((log) => log.id === preferredId)) { return preferredId; } return data[0]?.id || ""; }); setError(null); } catch (fetchError) { if ((fetchError as Error).name === "AbortError") return; setError(fetchError instanceof Error ? fetchError.message : "Failed to fetch request logs"); setLogs([]); } finally { if (!signal?.aborted) setLogsLoading(false); } }, [initialRequestId] ); const fetchExplanation = useCallback(async (requestId: string, signal?: AbortSignal) => { if (!requestId) return; setExplanationLoading(true); try { const response = await fetch(`/api/usage/route-explain/${encodeURIComponent(requestId)}`, { signal, cache: "no-store", }); if (!response.ok) throw new Error("Failed to explain route"); const data = (await response.json()) as RouteExplainabilityResponse; setExplanation(data); setError(null); } catch (fetchError) { if ((fetchError as Error).name === "AbortError") return; setError(fetchError instanceof Error ? fetchError.message : "Failed to explain route"); setExplanation(null); } finally { if (!signal?.aborted) setExplanationLoading(false); } }, []); useEffect(() => { const controller = new AbortController(); fetchLogs(controller.signal); return () => controller.abort(); }, [fetchLogs]); useEffect(() => { if (!selectedId) return; const controller = new AbortController(); fetchExplanation(selectedId, controller.signal); return () => controller.abort(); }, [fetchExplanation, selectedId]); useEffect(() => { if (!selectedId || typeof window === "undefined") return; const url = new URL(window.location.href); if ( url.searchParams.get("tab") === "route-trace" || url.searchParams.get("tab") === "route-explain" ) { url.searchParams.set("tab", "route-trace"); url.searchParams.set("id", selectedId); window.history.replaceState(null, "", url.toString()); } }, [selectedId]); const selectedLog = useMemo( () => logs.find((log) => log.id === selectedId) || null, [logs, selectedId] ); return (

{analyticsText(t, "routeTraceTitle", "Route Trace View")}

{analyticsText( t, "routeTraceDescription", "Inspect the persisted request trace: selected target, routing factors, fallback evidence, current scoring replay, latency, tokens and target health." )}

{logsLoading || explanationLoading ? : null} {!logsLoading && !explanationLoading && error ? (
route_off
Unable to load route explanation
{error}
) : null} {!logsLoading && !explanationLoading && !error && logs.length === 0 ? (
route
No request logs available
Send traffic through OmniRoute first. Route explanations are generated from persisted structured call logs.
) : null} {!logsLoading && !explanationLoading && explanation ? (
{explanation.routeType} HTTP {explanation.selectedTarget.status} {explanation.confidence} confidence

{explanation.summary}

{[ ["Provider", explanation.selectedTarget.provider || "n/a"], ["Model", explanation.selectedTarget.model || "n/a"], ["Account", explanation.selectedTarget.account || "n/a"], ["Connection", explanation.selectedTarget.connectionId || "n/a"], ["Combo", explanation.comboUsed || "Direct"], ["Step", explanation.selectedTarget.comboStepId || "n/a"], [ "Tokens", `${explanation.selectedTarget.tokensIn.toLocaleString()} in · ${explanation.selectedTarget.tokensOut.toLocaleString()} out`, ], ].map(([label, value]) => (
{label} {value}
))}
{explanation.evidence.map((item) => (
{item.label} {item.value}
))}
{explanation.decision.factors.map((factor) => ( ))}
    {explanation.recommendations.map((item) => (
  • check_circle {item}
  • ))}
{explanation.limitations.length > 0 ? (
    {explanation.limitations.map((item) => (
  • info {item}
  • ))}
) : (
No known limitations for this explanation.
)}
) : null}
); }