"use client"; import * as React from "react"; import { ChevronLeftIcon, ChevronRightIcon, Loader2Icon, SearchIcon, TerminalIcon } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "@/components/ui/sheet"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { api } from "@/lib/api"; import type { CommandRecord } from "@/lib/types"; import { cn } from "@/lib/utils"; function fmtTime(ts: string) { return new Date(ts).toLocaleString("zh-CN", { month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", }); } // toolInput renders a tool's raw input for display. Bash's {"command":"..."} is // unwrapped to the bare command; other tools show their pretty-printed JSON args. function toolInput(raw: string): string { try { const obj = JSON.parse(raw); if (obj && typeof obj.command === "string") return obj.command; return JSON.stringify(obj, null, 2); } catch { /* not JSON */ } return raw; } // truncate clips a string to maxLen characters. function truncate(s: string, maxLen: number): string { const first = s.split("\n")[0]; // single-line preview if (first.length <= maxLen) return first; return first.slice(0, maxLen) + "…"; } const PAGE_SIZES = [25, 50, 100]; const CMD_MAX_LEN = 80; export default function CommandsPage() { const [page, setPage] = React.useState(0); const [size, setSize] = React.useState(50); const [query, setQuery] = React.useState(""); const [queryQ, setQueryQ] = React.useState(""); const [taskFilter, setTaskFilter] = React.useState(""); const [commands, setCommands] = React.useState([]); const [total, setTotal] = React.useState(0); const [loading, setLoading] = React.useState(false); // Selected execution is rendered in a right-side detail sheet. const [selected, setSelected] = React.useState(null); // Debounce search input. React.useEffect(() => { const t = setTimeout(() => setQueryQ(query.trim()), 300); return () => clearTimeout(t); }, [query]); // Reset page on filter change. // biome-ignore lint/correctness/useExhaustiveDependencies: every filter change intentionally resets pagination. React.useEffect(() => { setPage(0); }, [queryQ, taskFilter, size]); // Load data. React.useEffect(() => { let alive = true; setLoading(true); api .commands({ task: taskFilter || undefined, q: queryQ || undefined, page, size }) .then((r) => { if (!alive) return; setCommands(r.commands ?? []); setTotal(r.total ?? 0); }) .catch(() => { if (!alive) return; setCommands([]); setTotal(0); }) .finally(() => alive && setLoading(false)); return () => { alive = false; }; }, [page, size, queryQ, taskFilter]); const totalPages = Math.max(1, Math.ceil(total / size)); const rangeStart = total === 0 ? 0 : page * size + 1; const rangeEnd = page * size + commands.length; return (
{/* Header */}

工具执行

{total}
{/* Toolbar */}
setQuery(e.target.value)} className="h-8 pl-8" />
setTaskFilter(e.target.value.replace(/\D/g, ""))} />
{rangeStart}–{rangeEnd} / {total} {page + 1} / {totalPages}
{/* History table */}
时间 任务 Worker 工具 输入 状态 {loading && commands.length === 0 ? ( ) : commands.length === 0 ? ( 暂无工具执行记录 ) : ( commands.map((cmd) => ( setSelected(cmd)} > {fmtTime(cmd.created_at)} #{cmd.exploration_id} {cmd.worker || "-"} {cmd.tool || "-"} {truncate(toolInput(cmd.command), CMD_MAX_LEN)} {cmd.is_error ? ( 失败 ) : ( 成功 )} )) )}
!open && setSelected(null)}> {selected && ( <> 工具执行详情 {fmtTime(selected.created_at)}
#{selected.exploration_id} {selected.worker || "-"} {selected.tool || "-"} {selected.is_error ? ( 失败 ) : ( 成功 )}
输入 Input
                      {toolInput(selected.command)}
                    
输出 Output
                      {selected.output || "(空)"}
                    
)}
); }