|
| 1 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import { useI18n } from "../i18n"; |
| 3 | +import { IconRefresh } from "../icons"; |
| 4 | +import { Switch } from "../ui"; |
| 5 | + |
| 6 | +interface DebugSettings { |
| 7 | + enabled: boolean; |
| 8 | + usage: boolean; |
| 9 | + runtimeOverride: Partial<Record<"debug" | "usage", boolean>>; |
| 10 | + env: Record<"debug" | "usage", boolean>; |
| 11 | +} |
| 12 | + |
| 13 | +interface DebugLogEntry { |
| 14 | + seq: number; |
| 15 | + at: number; |
| 16 | + line: string; |
| 17 | +} |
| 18 | + |
| 19 | +type LogStream = "provider" | "usage"; |
| 20 | + |
| 21 | +export default function Debug({ apiBase }: { apiBase: string }) { |
| 22 | + const { t } = useI18n(); |
| 23 | + const [debug, setDebug] = useState<DebugSettings | null>(null); |
| 24 | + const [debugBusy, setDebugBusy] = useState(false); |
| 25 | + const [stream, setStream] = useState<LogStream>("provider"); |
| 26 | + const [lines, setLines] = useState<string[]>([]); |
| 27 | + const [follow, setFollow] = useState(true); |
| 28 | + const [refreshing, setRefreshing] = useState(false); |
| 29 | + const afterRef = useRef(0); |
| 30 | + const logRef = useRef<HTMLPreElement | null>(null); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const fetchDebug = async () => { |
| 34 | + try { |
| 35 | + const res = await fetch(`${apiBase}/api/debug`); |
| 36 | + if (res.ok) setDebug(await res.json()); |
| 37 | + } catch { /* ignore */ } |
| 38 | + }; |
| 39 | + void fetchDebug(); |
| 40 | + const interval = setInterval(() => void fetchDebug(), 2000); |
| 41 | + return () => clearInterval(interval); |
| 42 | + }, [apiBase]); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (!debug) return; |
| 46 | + if (debug.enabled && stream === "usage" && !debug.usage) setStream("provider"); |
| 47 | + if (debug.usage && stream === "provider" && !debug.enabled) setStream("usage"); |
| 48 | + }, [debug, stream]); |
| 49 | + |
| 50 | + const streamEnabled = stream === "provider" ? !!debug?.enabled : !!debug?.usage; |
| 51 | + const logsPath = stream === "provider" ? `${apiBase}/api/debug/logs` : `${apiBase}/api/debug/usage-logs`; |
| 52 | + |
| 53 | + const fetchLogs = useCallback(async (initial: boolean) => { |
| 54 | + if (!streamEnabled) { |
| 55 | + setLines([]); |
| 56 | + afterRef.current = 0; |
| 57 | + return; |
| 58 | + } |
| 59 | + setRefreshing(true); |
| 60 | + try { |
| 61 | + const params = new URLSearchParams({ limit: "500" }); |
| 62 | + if (!initial && afterRef.current > 0) params.set("after", String(afterRef.current)); |
| 63 | + const res = await fetch(`${logsPath}?${params}`); |
| 64 | + if (!res.ok) return; |
| 65 | + const entries = await res.json() as DebugLogEntry[]; |
| 66 | + if (entries.length === 0) return; |
| 67 | + setLines(prev => initial |
| 68 | + ? entries.map(entry => entry.line) |
| 69 | + : [...prev, ...entries.map(entry => entry.line)].slice(-2000)); |
| 70 | + afterRef.current = entries[entries.length - 1]!.seq; |
| 71 | + } catch { /* ignore */ } finally { |
| 72 | + setRefreshing(false); |
| 73 | + } |
| 74 | + }, [logsPath, streamEnabled]); |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + afterRef.current = 0; |
| 78 | + setLines([]); |
| 79 | + void fetchLogs(true); |
| 80 | + }, [stream, streamEnabled, fetchLogs]); |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + if (!follow || !streamEnabled) return; |
| 84 | + const interval = setInterval(() => void fetchLogs(false), 1000); |
| 85 | + return () => clearInterval(interval); |
| 86 | + }, [follow, streamEnabled, fetchLogs]); |
| 87 | + |
| 88 | + useEffect(() => { |
| 89 | + if (!follow || !logRef.current) return; |
| 90 | + logRef.current.scrollTop = logRef.current.scrollHeight; |
| 91 | + }, [lines, follow]); |
| 92 | + |
| 93 | + const setDebugFlag = async (flag: "debug" | "usage", enabled: boolean) => { |
| 94 | + setDebugBusy(true); |
| 95 | + try { |
| 96 | + const res = await fetch(`${apiBase}/api/debug`, { |
| 97 | + method: "PUT", |
| 98 | + headers: { "content-type": "application/json" }, |
| 99 | + body: JSON.stringify({ [flag]: enabled }), |
| 100 | + }); |
| 101 | + if (res.ok) setDebug(await res.json()); |
| 102 | + } catch { /* ignore */ } finally { |
| 103 | + setDebugBusy(false); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + const resetDebug = async () => { |
| 108 | + setDebugBusy(true); |
| 109 | + try { |
| 110 | + const res = await fetch(`${apiBase}/api/debug`, { |
| 111 | + method: "PUT", |
| 112 | + headers: { "content-type": "application/json" }, |
| 113 | + body: JSON.stringify({ reset: true }), |
| 114 | + }); |
| 115 | + if (res.ok) setDebug(await res.json()); |
| 116 | + } catch { /* ignore */ } finally { |
| 117 | + setDebugBusy(false); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + return ( |
| 122 | + <> |
| 123 | + <div className="page-head"> |
| 124 | + <h2>{t("debug.title")}</h2> |
| 125 | + <div style={{ display: "inline-flex", alignItems: "center", gap: 12 }}> |
| 126 | + <button |
| 127 | + type="button" |
| 128 | + className="btn btn-ghost btn-sm" |
| 129 | + disabled={refreshing || !streamEnabled} |
| 130 | + onClick={() => void fetchLogs(true)} |
| 131 | + > |
| 132 | + <IconRefresh /> {t("debug.refresh")} |
| 133 | + </button> |
| 134 | + <label className="muted" style={{ fontSize: 13, cursor: "pointer", display: "inline-flex", alignItems: "center", gap: 6 }}> |
| 135 | + <input type="checkbox" checked={follow} onChange={e => setFollow(e.target.checked)} /> |
| 136 | + {t("debug.follow")} |
| 137 | + </label> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + <p className="page-sub">{t("debug.subtitle")}</p> |
| 141 | + |
| 142 | + {!debug ? ( |
| 143 | + <div className="empty">{t("debug.loading")}</div> |
| 144 | + ) : ( |
| 145 | + <div className="card" style={{ marginBottom: 16, padding: "12px 14px" }}> |
| 146 | + <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 12, flexWrap: "wrap" }}> |
| 147 | + <div style={{ display: "flex", flexWrap: "wrap", gap: 16 }}> |
| 148 | + {(["debug", "usage"] as const).map(flag => { |
| 149 | + const checked = flag === "debug" ? debug.enabled : debug.usage; |
| 150 | + return ( |
| 151 | + <div key={flag} style={{ display: "inline-flex", alignItems: "center", gap: 10, minWidth: 220 }}> |
| 152 | + <Switch |
| 153 | + on={checked} |
| 154 | + disabled={debugBusy} |
| 155 | + label={t(`debug.${flag}`)} |
| 156 | + onClick={() => void setDebugFlag(flag, !checked)} |
| 157 | + /> |
| 158 | + <span style={{ fontSize: 13 }}>{t(`debug.${flag}`)}</span> |
| 159 | + </div> |
| 160 | + ); |
| 161 | + })} |
| 162 | + </div> |
| 163 | + <button type="button" className="btn btn-ghost btn-sm" disabled={debugBusy} onClick={() => void resetDebug()}> |
| 164 | + {t("debug.reset")} |
| 165 | + </button> |
| 166 | + </div> |
| 167 | + |
| 168 | + {(debug.enabled || debug.usage) && ( |
| 169 | + <div style={{ display: "inline-flex", gap: 6, marginTop: 12 }}> |
| 170 | + {debug.enabled && ( |
| 171 | + <button |
| 172 | + type="button" |
| 173 | + className={`btn btn-sm${stream === "provider" ? " btn-primary" : " btn-ghost"}`} |
| 174 | + onClick={() => setStream("provider")} |
| 175 | + > |
| 176 | + {t("debug.streamProvider")} |
| 177 | + </button> |
| 178 | + )} |
| 179 | + {debug.usage && ( |
| 180 | + <button |
| 181 | + type="button" |
| 182 | + className={`btn btn-sm${stream === "usage" ? " btn-primary" : " btn-ghost"}`} |
| 183 | + onClick={() => setStream("usage")} |
| 184 | + > |
| 185 | + {t("debug.streamUsage")} |
| 186 | + </button> |
| 187 | + )} |
| 188 | + </div> |
| 189 | + )} |
| 190 | + </div> |
| 191 | + )} |
| 192 | + |
| 193 | + {debug && !streamEnabled ? ( |
| 194 | + <div className="empty"> |
| 195 | + <div style={{ fontWeight: 600, marginBottom: 6 }}>{t("debug.emptyTitle")}</div> |
| 196 | + <div className="muted" style={{ fontSize: 13, maxWidth: 560 }}>{t("debug.empty")}</div> |
| 197 | + </div> |
| 198 | + ) : debug && streamEnabled && lines.length === 0 ? ( |
| 199 | + <div className="empty"> |
| 200 | + <div style={{ fontWeight: 600, marginBottom: 6 }}>{t("debug.noLinesTitle")}</div> |
| 201 | + <div className="muted" style={{ fontSize: 13, maxWidth: 560 }}>{t("debug.noLines")}</div> |
| 202 | + </div> |
| 203 | + ) : debug && streamEnabled ? ( |
| 204 | + <pre ref={logRef} className="log-detail-json" style={{ maxHeight: "calc(100vh - 280px)" }}>{lines.join("\n")}</pre> |
| 205 | + ) : null} |
| 206 | + </> |
| 207 | + ); |
| 208 | +} |
0 commit comments