File size: 17,915 Bytes
88c4c60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | "use client";
import { useState, useEffect, useCallback } from "react";
import Card from "@/shared/components/Card";
import Button from "@/shared/components/Button";
import Drawer from "@/shared/components/Drawer";
import Pagination from "@/shared/components/Pagination";
import { cn } from "@/shared/utils/cn";
import { AI_PROVIDERS, getProviderByAlias } from "@/shared/constants/providers";
let providerNameCache = null;
let providerNodesCache = null;
async function fetchProviderNames() {
if (providerNameCache && providerNodesCache) {
return { providerNameCache, providerNodesCache };
}
const nodesRes = await fetch("/api/provider-nodes");
const nodesData = await nodesRes.json();
const nodes = nodesData.nodes || [];
providerNodesCache = {};
for (const node of nodes) {
providerNodesCache[node.id] = node.name;
}
providerNameCache = {
...AI_PROVIDERS,
...providerNodesCache
};
return { providerNameCache, providerNodesCache };
}
function getProviderName(providerId, cache) {
if (!providerId) return providerId;
if (!cache) return providerId;
const cached = cache[providerId];
if (typeof cached === 'string') {
return cached;
}
if (cached?.name) {
return cached.name;
}
const providerConfig = getProviderByAlias(providerId) || AI_PROVIDERS[providerId];
return providerConfig?.name || providerId;
}
function CollapsibleSection({ title, children, defaultOpen = false, icon = null }) {
const [isOpen, setIsOpen] = useState(defaultOpen);
return (
<div className="border border-black/5 dark:border-white/5 rounded-lg overflow-hidden">
<button
type="button"
onClick={() => setIsOpen(!isOpen)}
className="w-full flex items-center justify-between p-3 bg-black/[0.02] dark:bg-white/[0.02] hover:bg-black/[0.04] dark:hover:bg-white/[0.04] transition-colors"
>
<div className="flex items-center gap-2">
{icon && <span className="material-symbols-outlined text-[18px] text-text-muted">{icon}</span>}
<span className="font-semibold text-sm text-text-main">{title}</span>
</div>
<span className={cn(
"material-symbols-outlined text-[20px] text-text-muted transition-transform duration-200",
isOpen ? "rotate-90" : ""
)}>
chevron_right
</span>
</button>
{isOpen && (
<div className="p-4 border-t border-black/5 dark:border-white/5">
{children}
</div>
)}
</div>
);
}
function getInputTokens(tokens) {
const prompt = tokens?.prompt_tokens || tokens?.input_tokens || 0;
const cache = tokens?.cached_tokens || tokens?.cache_read_input_tokens || 0;
return prompt < cache ? cache : prompt;
}
export default function RequestDetailsTab() {
const [details, setDetails] = useState([]);
const [pagination, setPagination] = useState({
page: 1,
pageSize: 20,
totalItems: 0,
totalPages: 0
});
const [loading, setLoading] = useState(false);
const [selectedDetail, setSelectedDetail] = useState(null);
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
const [providers, setProviders] = useState([]);
const [providerNameCache, setProviderNameCache] = useState(null);
const [filters, setFilters] = useState({
provider: "",
startDate: "",
endDate: ""
});
const fetchProviders = useCallback(async () => {
try {
const res = await fetch("/api/usage/providers");
const data = await res.json();
setProviders(data.providers || []);
const cache = await fetchProviderNames();
setProviderNameCache(cache.providerNameCache);
} catch (error) {
console.error("Failed to fetch providers:", error);
}
}, []);
const fetchDetails = useCallback(async () => {
setLoading(true);
try {
const params = new URLSearchParams({
page: pagination.page.toString(),
pageSize: pagination.pageSize.toString()
});
if (filters.provider) params.append("provider", filters.provider);
if (filters.startDate) params.append("startDate", filters.startDate);
if (filters.endDate) params.append("endDate", filters.endDate);
const res = await fetch(`/api/usage/request-details?${params}`);
const data = await res.json();
setDetails(data.details || []);
setPagination(prev => ({ ...prev, ...data.pagination }));
} catch (error) {
console.error("Failed to fetch request details:", error);
} finally {
setLoading(false);
}
}, [pagination.page, pagination.pageSize, filters]);
useEffect(() => {
fetchProviders();
}, [fetchProviders]);
useEffect(() => {
fetchDetails();
}, [fetchDetails]);
const handleViewDetail = (detail) => {
setSelectedDetail(detail);
setIsDrawerOpen(true);
};
const handlePageChange = (newPage) => {
setPagination(prev => ({ ...prev, page: newPage }));
};
const handlePageSizeChange = (newPageSize) => {
setPagination(prev => ({ ...prev, pageSize: newPageSize, page: 1 }));
};
const handleClearFilters = () => {
setFilters({ provider: "", startDate: "", endDate: "" });
};
return (
<div className="flex min-w-0 flex-col gap-6">
<Card padding="md">
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
<div className="flex min-w-0 flex-col gap-2">
<label htmlFor="provider-filter" className="text-sm font-medium text-text-main">Provider</label>
<select
id="provider-filter"
value={filters.provider}
onChange={(e) => setFilters({ ...filters, provider: e.target.value })}
className={cn(
"h-9 px-3 rounded-lg border border-black/10 dark:border-white/10 bg-surface",
"text-sm text-text-main focus:outline-none focus:ring-2 focus:ring-primary/20",
"w-full min-w-0 cursor-pointer"
)}
style={{ colorScheme: 'auto' }}
>
<option value="">All Providers</option>
{providers.map((provider) => (
<option key={provider.id} value={provider.id}>
{provider.name}
</option>
))}
</select>
</div>
<div className="flex min-w-0 flex-col gap-2">
<label htmlFor="start-date-filter" className="text-sm font-medium text-text-main">Start Date</label>
<input
id="start-date-filter"
type="datetime-local"
value={filters.startDate}
onChange={(e) => setFilters({ ...filters, startDate: e.target.value })}
className={cn(
"h-9 px-3 rounded-lg border border-black/10 dark:border-white/10 bg-surface",
"w-full min-w-0 text-sm text-text-main focus:outline-none focus:ring-2 focus:ring-primary/20"
)}
/>
</div>
<div className="flex min-w-0 flex-col gap-2">
<label htmlFor="end-date-filter" className="text-sm font-medium text-text-main">End Date</label>
<input
id="end-date-filter"
type="datetime-local"
value={filters.endDate}
onChange={(e) => setFilters({ ...filters, endDate: e.target.value })}
className={cn(
"h-9 px-3 rounded-lg border border-black/10 dark:border-white/10 bg-surface",
"w-full min-w-0 text-sm text-text-main focus:outline-none focus:ring-2 focus:ring-primary/20"
)}
/>
</div>
<div className="flex min-w-0 flex-col gap-2 sm:col-span-2 lg:col-span-1">
<span className="hidden text-sm font-medium text-text-main opacity-0 lg:block" aria-hidden="true">Clear</span>
<Button
variant="ghost"
onClick={handleClearFilters}
disabled={!filters.provider && !filters.startDate && !filters.endDate}
className="w-full"
>
Clear Filters
</Button>
</div>
</div>
</Card>
<Card padding="none">
<div className="overflow-x-auto">
<table className="w-full min-w-[880px]">
<thead>
<tr className="border-b border-black/5 dark:border-white/5">
<th className="text-left p-4 text-sm font-semibold text-text-main">Timestamp</th>
<th className="text-left p-4 text-sm font-semibold text-text-main">Model</th>
<th className="text-left p-4 text-sm font-semibold text-text-main">Provider</th>
<th className="text-right p-4 text-sm font-semibold text-text-main">Input Tokens</th>
<th className="text-right p-4 text-sm font-semibold text-text-main">Output Tokens</th>
<th className="text-left p-4 text-sm font-semibold text-text-main">Latency</th>
<th className="text-center p-4 text-sm font-semibold text-text-main">Action</th>
</tr>
</thead>
<tbody>
{loading ? (
<tr>
<td colSpan="7" className="p-8 text-center text-text-muted">
<div className="flex items-center justify-center gap-2">
<span className="material-symbols-outlined animate-spin text-[20px]">progress_activity</span>
Loading...
</div>
</td>
</tr>
) : details.length === 0 ? (
<tr>
<td colSpan="7" className="p-8 text-center text-text-muted">
No request details found
</td>
</tr>
) : (
details.map((detail, index) => (
<tr
key={`${detail.id}-${index}`}
className="border-b border-black/5 dark:border-white/5 last:border-b-0 hover:bg-black/[0.02] dark:hover:bg-white/[0.02] transition-colors"
>
<td className="whitespace-nowrap p-4 text-sm text-text-main">
{new Date(detail.timestamp).toLocaleString()}
</td>
<td className="max-w-[260px] truncate p-4 font-mono text-sm text-text-main">
{detail.model}
</td>
<td className="max-w-[180px] truncate p-4 text-sm text-text-main">
<span className="font-medium">
{getProviderName(detail.provider, providerNameCache)}
</span>
</td>
<td className="p-4 text-sm text-text-main text-right font-mono">
{getInputTokens(detail.tokens).toLocaleString()}
</td>
<td className="p-4 text-sm text-text-main text-right font-mono">
{detail.tokens?.completion_tokens?.toLocaleString() || 0}
</td>
<td className="p-4 text-sm text-text-muted">
<div className="flex flex-col gap-0.5">
<div>TTFT: <span className="font-mono">{detail.latency?.ttft || 0}ms</span></div>
<div>Total: <span className="font-mono">{detail.latency?.total || 0}ms</span></div>
</div>
</td>
<td className="p-4 text-center">
<Button
variant="outline"
size="sm"
onClick={() => handleViewDetail(detail)}
>
Detail
</Button>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
{!loading && details.length > 0 && (
<div className="border-t border-black/5 dark:border-white/5">
<Pagination
currentPage={pagination.page}
pageSize={pagination.pageSize}
totalItems={pagination.totalItems}
onPageChange={handlePageChange}
onPageSizeChange={handlePageSizeChange}
/>
</div>
)}
</Card>
<Drawer
isOpen={isDrawerOpen}
onClose={() => setIsDrawerOpen(false)}
title="Request Details"
width="lg"
>
{selectedDetail && (
<div className="space-y-6">
<div className="grid min-w-0 grid-cols-1 gap-4 text-sm sm:grid-cols-2">
<div>
<span className="text-text-muted">ID:</span>{" "}
<span className="break-all font-mono text-text-main">{selectedDetail.id}</span>
</div>
<div>
<span className="text-text-muted">Timestamp:</span>{" "}
<span className="text-text-main">{new Date(selectedDetail.timestamp).toLocaleString()}</span>
</div>
<div>
<span className="text-text-muted">Provider:</span>{" "}
<span className="text-text-main font-medium">{getProviderName(selectedDetail.provider, providerNameCache)}</span>
</div>
<div>
<span className="text-text-muted">Model:</span>{" "}
<span className="text-text-main font-mono">{selectedDetail.model}</span>
</div>
<div>
<span className="text-text-muted">Status:</span>{" "}
<span className={cn(
"font-medium",
selectedDetail.status === "success" ? "text-green-600" : "text-red-600"
)}>
{selectedDetail.status}
</span>
</div>
<div>
<span className="text-text-muted">Latency:</span>{" "}
<span className="text-text-main font-mono">
TTFT {selectedDetail.latency?.ttft || 0}ms / Total {selectedDetail.latency?.total || 0}ms
</span>
</div>
<div>
<span className="text-text-muted">Input Tokens:</span>{" "}
<span className="text-text-main font-mono">
{getInputTokens(selectedDetail.tokens).toLocaleString()}
</span>
</div>
<div>
<span className="text-text-muted">Output Tokens:</span>{" "}
<span className="text-text-main font-mono">
{selectedDetail.tokens?.completion_tokens?.toLocaleString() || 0}
</span>
</div>
</div>
<div className="space-y-4">
<CollapsibleSection title="1. Client Request (Input)" defaultOpen={true} icon="input">
<pre className="max-h-[300px] max-w-full overflow-auto rounded-lg border border-black/5 bg-black/5 p-3 font-mono text-xs text-text-main dark:border-white/5 dark:bg-white/5 sm:p-4">
{JSON.stringify(selectedDetail.request, null, 2)}
</pre>
</CollapsibleSection>
{selectedDetail.providerRequest && (
<CollapsibleSection title="2. Provider Request (Translated)" icon="translate">
<pre className="max-h-[300px] max-w-full overflow-auto rounded-lg border border-black/5 bg-black/5 p-3 font-mono text-xs text-text-main dark:border-white/5 dark:bg-white/5 sm:p-4">
{JSON.stringify(selectedDetail.providerRequest, null, 2)}
</pre>
</CollapsibleSection>
)}
{selectedDetail.providerResponse && (
<CollapsibleSection title="3. Provider Response (Raw)" icon="data_object">
<pre className="max-h-[300px] max-w-full overflow-auto rounded-lg border border-black/5 bg-black/5 p-3 font-mono text-xs text-text-main dark:border-white/5 dark:bg-white/5 sm:p-4">
{typeof selectedDetail.providerResponse === 'object'
? JSON.stringify(selectedDetail.providerResponse, null, 2)
: selectedDetail.providerResponse
}
</pre>
</CollapsibleSection>
)}
<CollapsibleSection title="4. Client Response (Final)" defaultOpen={true} icon="output">
{selectedDetail.response?.thinking && (
<div className="mb-4">
<h4 className="font-semibold text-text-main mb-2 flex items-center gap-2 text-xs uppercase tracking-wide opacity-70">
<span className="material-symbols-outlined text-[16px]">psychology</span>
Thinking Process
</h4>
<pre className="max-h-[200px] max-w-full overflow-auto rounded-lg border border-amber-200 bg-amber-50 p-3 font-mono text-xs text-amber-900 dark:border-amber-800 dark:bg-amber-950/30 dark:text-amber-100 sm:p-4">
{selectedDetail.response.thinking}
</pre>
</div>
)}
<h4 className="font-semibold text-text-main mb-2 text-xs uppercase tracking-wide opacity-70">
Content
</h4>
<pre className="max-h-[300px] max-w-full overflow-auto rounded-lg border border-black/5 bg-black/5 p-3 font-mono text-xs text-text-main dark:border-white/5 dark:bg-white/5 sm:p-4">
{selectedDetail.response?.content || "[No content]"}
</pre>
</CollapsibleSection>
</div>
</div>
)}
</Drawer>
</div>
);
}
|