Files
UHD_Media_Tool/apps/web/app/components/agentui/tool-result.tsx
T
rootandClaude Opus 5.5 901b01d1a1 fix(sites): HHClub parsing, resource list layout and request restraint
HHClub (憨憨) was checked read-only against a real account (one detail
request; list, user data and health came from the product's own fetch).

Parsing fixes:
- list titles carried the site's new-torrent marker "[新]" (58 of 100
  rows; the detail page title has none); stripped with a replace filter
- refreshing a detail overwrote seeders / leechers / snatched / category
  name / H&R with null because the HHClub div detail page lacks them;
  detail values now fall back to the list snapshot (mergeDetailOverList),
  and an unknown detail promotion keeps the list promotion
- level "贵宾" is VIP (umt.vipLevels)
- sanitized minimal fixture HHANCLUB_LIST_PAGE_2026 + tests; README marks
  HHClub as verified 2026-09-29
Size, counts, category ids/names, +08:00 time and subtitle were correct.

Request restraint:
- site definitions may carry umt.defaults; merged with settings.sites
  (longer intervals, fewer pages): new settings defaultSnapshotMaxPages 1,
  defaultHealthCheckIntervalSec 6 h, defaultDetailCacheSec 24 h; default
  burst 2. HHClub: snapshot 60 min page 1 only, health 6 h, search
  concurrency 1, detail cache 24 h, 1 token / 3 s burst 2
- periodic / manual snapshots are capped to that page count; scheduled
  health checks skip sites checked within the interval; detail refresh is
  served from cache inside the window; live searches on one site queue
- every rate-limiter token is counted per site per hour
  (site_request_count, migration 0046); GET /sites/{id}/request-stats;
  site overview and resource list show requests in the last 24 hours

Resource list: two-layer rows. Main line: title + spec tags (episodes,
resolution, source, codec, HDR, group, 国语/中字, site tags) + promotion
with time left + H&R. Second line: subtitle (one line, ellipsis) and
fixed-width category / size / seed-leech-snatched / upload time
(relative, absolute on hover) columns, stale badge, inventory overlay;
folds into a card on phones.

Also aligns identity_candidate.normalized_title order in db types with
the migrations (schema drift test).

Takes effect after the stack is rebuilt (migration 0046).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-29 15:18:15 +08:00

136 lines
5.5 KiB
TypeScript

// Adapted from agentui (https://github.com/ashish200729/agentui), components/agents/tool-result.tsx
// Copyright (c) 2026 Saurabh Chauhan. MIT License, see ./LICENSE-agentui.md
// 改造:状态色 blue/emerald/rose → 中性/success/destructive(并配文字);ActionSwapRollText 滚字去掉;
// 折叠用 Disclosure;新增 `badges` 槽(scope/风险/幂等等审计徽标)与 `summary`(收起时的一行结果);
// 默认不在完成后自动展开历史卡片(回放时 defaultOpen=false),运行中的卡片展开、完成后按 collapseOnComplete 收起。
import { Ban, Braces, ChevronDown, CircleCheck, CircleX, LoaderCircle, SquareTerminal, Wrench } from "lucide-react";
import { type ReactNode, useCallback, useEffect, useId, useRef, useState } from "react";
import { cn } from "~/lib/utils.ts";
import { CodeBlock, prettyMaybeJson } from "./code-block.tsx";
import { CHEVRON_CLASS, Disclosure } from "./disclosure.tsx";
export type ToolResultStatus = "running" | "success" | "error" | "cancelled";
export type ToolResultKind = "terminal" | "request" | "custom";
const STATUS: Record<ToolResultStatus, { label: string; cls: string }> = {
running: { label: "执行中", cls: "text-muted-foreground" },
success: { label: "成功", cls: "text-success" },
error: { label: "失败", cls: "text-destructive" },
cancelled: { label: "已取消", cls: "text-muted-foreground" },
};
function StatusIcon({ status }: { status: ToolResultStatus }) {
if (status === "running") return <LoaderCircle className="size-3 animate-spin motion-reduce:animate-none" />;
if (status === "success") return <CircleCheck className="size-3" />;
if (status === "error") return <CircleX className="size-3" />;
return <Ban className="size-3" />;
}
function KindIcon({ kind }: { kind: ToolResultKind }) {
if (kind === "terminal") return <SquareTerminal className="size-4" />;
if (kind === "request") return <Braces className="size-4" />;
return <Wrench className="size-4" />;
}
export interface ToolResultProps {
/** 能力/工具 ID(等宽显示) */
tool: ReactNode;
title: ReactNode;
children?: ReactNode;
status?: ToolResultStatus;
kind?: ToolResultKind;
/** 耗时等 */
meta?: ReactNode;
/** 标题行下方的徽标 */
badges?: ReactNode;
/** 收起时显示的一行摘要 */
summary?: ReactNode;
icon?: ReactNode;
defaultOpen?: boolean;
collapseOnComplete?: boolean;
className?: string;
}
export function ToolResult({
tool,
title,
children,
status = "running",
kind = "custom",
meta,
badges,
summary,
icon,
defaultOpen = status === "running",
collapseOnComplete = true,
className,
}: ToolResultProps) {
const id = useId();
const [open, setOpenState] = useState(defaultOpen);
const prev = useRef(status);
const setOpen = useCallback((v: boolean) => setOpenState(v), []);
useEffect(() => {
if (prev.current !== "running" && status === "running") setOpen(true);
if (prev.current === "running" && status !== "running" && collapseOnComplete) setOpen(false);
prev.current = status;
}, [collapseOnComplete, setOpen, status]);
const st = STATUS[status];
return (
<div
data-state={status}
aria-busy={status === "running"}
className={cn("w-full min-w-0 rounded-xl border border-border/70 text-sm", className)}
>
<button
id={`${id}-t`}
type="button"
aria-expanded={open}
aria-controls={`${id}-c`}
onClick={() => setOpen(!open)}
className="group flex min-h-10 w-full min-w-0 cursor-pointer items-center gap-2 rounded-xl px-3 py-1.5 text-left outline-none transition-colors duration-micro hover:bg-foreground/[0.03] focus-visible:ring-2 focus-visible:ring-ring"
>
<span aria-hidden className="grid size-4 shrink-0 place-items-center text-muted-foreground">
{icon ?? <KindIcon kind={kind} />}
</span>
<span className="flex min-w-0 flex-1 flex-wrap items-baseline gap-x-2 gap-y-0.5">
<span className="min-w-0 truncate text-caption font-medium text-foreground/90">{title}</span>
<span className="min-w-0 truncate font-mono text-label text-muted-foreground">{tool}</span>
{meta ? <span className="shrink-0 text-label text-muted-foreground tabular-nums">{meta}</span> : null}
</span>
<span className={cn("inline-flex shrink-0 items-center gap-1 text-label font-medium", st.cls)}>
<StatusIcon status={status} />
{st.label}
</span>
<ChevronDown aria-hidden className={cn("size-3.5", CHEVRON_CLASS, open && "rotate-180")} />
</button>
{badges ? <div className="flex flex-wrap items-center gap-1 px-3 pb-2 pl-9">{badges}</div> : null}
{!open && summary ? (
<p className="-mt-1 truncate px-3 pb-2 pl-9 text-meta text-muted-foreground">{summary}</p>
) : null}
<Disclosure id={`${id}-c`} role="region" aria-labelledby={`${id}-t`} open={open}>
<div className="space-y-2 border-t border-border/60 px-3 py-2.5">{children}</div>
</Disclosure>
</div>
);
}
/** 工具输入/输出块:JSON 自动缩进,长内容块内滚动 */
export function ToolResultOutput({
label,
children,
tone,
}: {
label: ReactNode;
children: string;
tone?: "destructive";
}) {
const { text, language } = prettyMaybeJson(children);
return (
<div className="min-w-0">
<p className="mb-1 text-meta text-muted-foreground">{label}</p>
<CodeBlock code={text} language={language} bare tone={tone === "destructive" ? "destructive" : "default"} />
</div>
);
}