import { useRef, useState } from "react"; import { Button, Segmented } from "antd"; import copyToClipboard from "copy-to-clipboard"; import { Copy, Trash2 } from "lucide-react"; import { canvasThemes } from "@/lib/canvas-theme"; import type { AgentEventLog } from "@/stores/use-agent-store"; import { formatLogJson, formatLogText, type AgentLogContext } from "./agent-event-formatters"; export function AgentLogView({ logs, theme, context, onClear, onCopied, onCopyBlocked, }: { logs: AgentEventLog[]; theme: (typeof canvasThemes)[keyof typeof canvasThemes]; context: AgentLogContext; onClear: () => void; onCopied: (text: string) => void; onCopyBlocked: (text: string) => void; }) { const [mode, setMode] = useState<"text" | "json">("text"); const textareaRef = useRef(null); const content = mode === "text" ? formatLogText(logs, context) : formatLogJson(logs, context); const lastError = [...logs].reverse().find((item) => /错误|失败|error/i.test(`${item.title}\n${item.text}`)); const copy = async (value = content, tip = "日志已复制") => { if (await copyToClipboard(value)) { onCopied(tip); return; } textareaRef.current?.focus(); textareaRef.current?.select(); onCopyBlocked("已选中日志,请手动复制"); }; return (
运行日志
setMode(value as "text" | "json")} options={[ { label: "排查日志", value: "text" }, { label: "原始 JSON", value: "json" }, ]} />
{logs.length} 条