okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petadmin: show what each request's decisions would cost on Jev8af1784

main
5.6 KiB68 linesraw
1import { useEffect, useRef, useState } from "react"
2import { RefreshCw } from "lucide-react"
3import { Button } from "@/components/ui/button"
4import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
5import { Empty, Notice, Status } from "@/components/common"
6import { TraceView } from "@/components/trace"
7import { api, message, number, time, usd } from "@/lib/api"
8import type { RequestRow } from "@/lib/types"
9
10function description(row: RequestRow) {
11  if (!row.detail) return "no query recorded"
12  if (typeof row.detail === "string") return row.detail
13  return row.detail.query || row.detail.urls?.join(", ") || "no query recorded"
14}
15
16export function Activity({ revision, refresh }: { revision: number; refresh: () => void }) {
17  const [page, setPage] = useState<{ rows: RequestRow[]; nextBefore?: number } | null>(null)
18  const [cursors, setCursors] = useState<number[]>([])
19  const [endpoint, setEndpoint] = useState("")
20  const [errorsOnly, setErrorsOnly] = useState(false)
21  const [error, setError] = useState("")
22  const [busy, setBusy] = useState(false)
23  const [selected, setSelected] = useState<RequestRow | null>(null)
24  const inspector = useRef<HTMLElement>(null)
25  useEffect(() => {
26    if (selected) { inspector.current?.focus({ preventScroll: true }); inspector.current?.scrollIntoView({ block: "start" }) }
27  }, [selected])
28  const before = cursors.at(-1)
29  useEffect(() => {
30    const abort = new AbortController()
31    const query = new URLSearchParams({ endpoint, errors: String(errorsOnly) })
32    if (before) query.set("before", String(before))
33    setBusy(true); setError("")
34    api<{ rows: RequestRow[]; nextBefore?: number }>(`requests?${query}`, { signal: abort.signal })
35      .then(setPage).catch(error => { if (!abort.signal.aborted) setError(message(error)) })
36      .finally(() => { if (!abort.signal.aborted) setBusy(false) })
37    return () => abort.abort()
38  }, [before, endpoint, errorsOnly, revision])
39  return <div className="space-y-6">
40    <div className="section-toolbar"><div><h2>request activity</h2><p className="muted">shared history from http, mcp, and this playground.</p></div>
41      <Button variant="outline" onClick={() => { setCursors([]); refresh() }} disabled={busy}><RefreshCw aria-hidden="true" />refresh</Button></div>
42    <div className="flex flex-wrap items-center gap-4">
43      <label className="inline-control">endpoint<select value={endpoint} onChange={e => { setEndpoint(e.target.value); setCursors([]) }}>
44        <option value="">all endpoints</option><option>search</option><option>contents</option><option>answer</option>
45      </select></label>
46      <label className="inline-control"><input type="checkbox" checked={errorsOnly} onChange={e => { setErrorsOnly(e.target.checked); setCursors([]) }} />request errors only</label>
47      {busy && <span role="status" className="muted">loading…</span>}
48    </div>
49    {error && <Notice>{error}</Notice>}
50    <p id="table-scroll-help" className="muted text-xs sm:hidden">scroll the table horizontally for outcome, jev cost, caller, and time. keyboard: focus the table and use arrow keys.</p>
51    <div className="table-frame" aria-busy={busy}><Table aria-label="request activity" containerProps={{ role: "region", "aria-label": "scrollable request activity", "aria-describedby": "table-scroll-help", tabIndex: 0 }}>
52      <TableHeader><TableRow><TableHead>query / urls</TableHead><TableHead>endpoint</TableHead><TableHead>outcome</TableHead><TableHead>jev cost</TableHead><TableHead>caller</TableHead><TableHead>time</TableHead></TableRow></TableHeader>
53      <TableBody>{page?.rows.map(row => <TableRow key={row.id} data-state={selected?.id === row.id ? "selected" : undefined}>
54        <TableCell><button className="request-link" onClick={() => setSelected(row)}>{description(row)}</button><div className="muted font-mono text-xs mt-1">{row.requestId.slice(0, 12)}</div></TableCell>
55        <TableCell>{row.endpoint}</TableCell><TableCell><Status error={row.outcome !== "ok"}>{row.outcome}</Status></TableCell>
56        <TableCell className="whitespace-nowrap">{row.decisionCalls ? <>{usd(row.jevUsd ?? 0)}<div className="muted text-xs mt-1" title={row.decisionModels ?? undefined}>{row.decisionCalls} calls · {number(row.inputTokens ?? 0)} tok</div></> : <span className="muted">—</span>}</TableCell>
57        <TableCell className="muted">{row.caller || "—"}</TableCell><TableCell className="muted whitespace-nowrap">{time(row.createdAt)}</TableCell>
58      </TableRow>)}</TableBody>
59    </Table>
60    {page?.rows.length === 0 && <Empty>{errorsOnly ? "no matching request errors. model retries are visible inside each request." : "no matching requests yet. run a query in the playground to start a trace."}</Empty>}</div>
61    <div className="section-toolbar"><p className="muted">30 requests per page. “ok” means the request completed, not that every url succeeded.</p><div className="flex gap-2">
62      <Button variant="outline" size="sm" disabled={busy || !cursors.length} onClick={() => setCursors(cursors.slice(0, -1))}>newer</Button>
63      <Button variant="outline" size="sm" disabled={busy || !page?.nextBefore} onClick={() => setCursors([...cursors, page!.nextBefore!])}>older</Button>
64    </div></div>
65    {selected && <section ref={inspector} tabIndex={-1} className="inspector" aria-label="selected request"><div className="section-toolbar"><div><h2>request inspection</h2><p className="muted break-all font-mono text-xs mt-1">{selected.requestId}</p></div><Button variant="ghost" size="sm" onClick={() => setSelected(null)}>close</Button></div>
66      <p className="my-4 break-words">{description(selected)}</p><TraceView key={selected.requestId} id={selected.requestId} /></section>}
67  </div>
68}