components/linyi/tool-result.tsx
lucide-reactmotion
"use client"; import * as React from "react";import { AnimatePresence, motion } from "motion/react";import { Check, ChevronDown, Copy, Loader2, RotateCw } from "lucide-react";import { cn } from "@/lib/cn";import { SPRING_SMOOTH } from "@/lib/motion"; export type ToolResultStatus = "idle" | "running" | "success" | "error"; export interface ToolResultProps { tool: string; status?: ToolResultStatus; duration?: string; input?: Record<string, unknown> | string; output?: string; open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; onRetry?: () => void; onCopy?: (payload: string) => void; compact?: boolean; className?: string;} const STATUS_META: Record< ToolResultStatus, { label: string; dotClass: string; badgeClass: string }> = { idle: { label: "等待中", dotClass: "bg-line-strong", badgeClass: "bg-well text-fg-muted border-line", }, running: { label: "执行中", dotClass: "bg-accent", badgeClass: "bg-accent-soft text-accent border-accent-border", }, success: { label: "成功", dotClass: "bg-success", badgeClass: "bg-success-soft text-success border-success-border", }, error: { label: "异常", dotClass: "bg-danger", badgeClass: "bg-danger-soft text-danger border-danger-border", },}; export function ToolResult({ tool, status = "success", duration, input, output, open: controlledOpen, defaultOpen = true, onOpenChange, onRetry, onCopy, compact = false, className,}: ToolResultProps) { const [internalOpen, setInternalOpen] = React.useState(defaultOpen); const [copied, setCopied] = React.useState(false); const timerRef = React.useRef<number | null>(null); const open = controlledOpen ?? internalOpen; const toggleOpen = () => { const next = !open; if (controlledOpen === undefined) setInternalOpen(next); onOpenChange?.(next); }; React.useEffect(() => { return () => { if (timerRef.current !== null) window.clearTimeout(timerRef.current); }; }, []); const formattedInput = React.useMemo(() => { if (!input) return null; if (typeof input === "string") return input; return JSON.stringify(input, null, 2); }, [input]); const copyPayload = async () => { const text = [ formattedInput ? `Input:\n${formattedInput}` : "", output ? `Output:\n${output}` : "", ] .filter(Boolean) .join("\n\n"); if (!text) return; try { await navigator.clipboard?.writeText(text); } catch { // Fallback gracefully when clipboard permission is unavailable } onCopy?.(text); setCopied(true); if (timerRef.current !== null) window.clearTimeout(timerRef.current); timerRef.current = window.setTimeout(() => setCopied(false), 1400); }; const meta = STATUS_META[status]; return ( <div data-status={status} className={cn( "w-full rounded-[13px] overflow-hidden bg-surface border border-line shadow-panel", className )} > <button type="button" aria-expanded={open} onClick={toggleOpen} className="w-full min-h-[38px] px-3 flex items-center justify-between gap-2 cursor-pointer hover:bg-surface-2 transition-colors" > <span className="flex items-center gap-2 min-w-0 font-mono-num text-[11px] text-fg"> {status === "running" ? ( <Loader2 className="w-3.5 h-3.5 text-accent animate-spin flex-shrink-0" aria-hidden="true" /> ) : ( <span className={cn("w-1.5 h-1.5 rounded-pill flex-shrink-0", meta.dotClass)} aria-hidden="true" /> )} <span className="truncate">{tool}</span> {!compact && ( <span className={cn( "px-1.5 py-0.5 rounded-pill border text-[10px] font-sans leading-none", meta.badgeClass )} > {meta.label} </span> )} </span> <span className="flex items-center gap-2 flex-shrink-0"> {duration && <span className="font-mono-num text-[10px] text-fg-muted">{duration}</span>} <ChevronDown className={cn( "w-3.5 h-3.5 text-fg-muted transition-transform duration-200", open && "rotate-180" )} aria-hidden="true" /> </span> </button> <AnimatePresence initial={false}> {open && ( <motion.div initial={{ height: 0, opacity: 0 }} animate={{ height: "auto", opacity: 1 }} exit={{ height: 0, opacity: 0 }} transition={SPRING_SMOOTH} className="overflow-hidden" > <div className={cn("border-t border-line", compact ? "p-2.5" : "p-3 space-y-2.5")}> {!compact && formattedInput && ( <div className="space-y-1"> <div className="flex items-center justify-between"> <span className="font-mono-num text-[10px] text-fg-muted">输入参数</span> <button type="button" onClick={copyPayload} className="inline-flex items-center gap-1 px-2 h-6 rounded-pill text-[10px] text-fg-secondary hover:text-fg hover:bg-well cursor-pointer transition-colors" > {copied ? ( <Check className="w-3 h-3 text-success" aria-hidden="true" /> ) : ( <Copy className="w-3 h-3" aria-hidden="true" /> )} <span>{copied ? "已复制" : "复制"}</span> </button> </div> <pre className="m-0 rounded-control bg-surface-2 border border-line px-2.5 py-2 font-mono-num text-[10px] text-fg-secondary overflow-x-auto no-scrollbar"> <code>{formattedInput}</code> </pre> </div> )} {output && ( <div className="space-y-1"> <div className="flex items-center justify-between gap-2"> {!compact && ( <span className="font-mono-num text-[10px] text-fg-muted block">返回结果</span> )} {!compact && status === "error" && onRetry && ( <button type="button" onClick={onRetry} className="inline-flex items-center gap-1 px-2 h-6 rounded-pill text-[10px] text-danger hover:bg-danger-soft cursor-pointer transition-colors" > <RotateCw className="w-3 h-3" aria-hidden="true" /> <span>重试调用</span> </button> )} </div> <div role={status === "error" ? "alert" : undefined} className={cn( "rounded-control px-2.5 py-1.5 font-mono-num text-[10px] leading-relaxed border", status === "error" ? "bg-danger-soft text-danger border-danger-border" : "bg-well text-fg-secondary border-line-well" )} > {output} </div> </div> )} </div> </motion.div> )} </AnimatePresence> </div> );}