mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 07:21:00 +08:00
- Add visualize agent pipeline (analysis → code generation → review) - Add VisualizeCapability with request config and registry wiring - Add frontend VisualizeConfigPanel, VisualizationViewer components - Add chart.js and react-chartjs-2 dependencies - Improve math_animator YON_IMAGE anchor block prompts (en/zh) - Add minimax model override (supports_response_format: false) - Suppress noisy uvicorn WebSocket connection logs - Add selective HTTP access logging middleware (non-200 only) Made-with: Cursor
181 lines
5.7 KiB
TypeScript
181 lines
5.7 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
import { Code2, Copy, Check } from "lucide-react";
|
|
import { useTranslation } from "react-i18next";
|
|
import type { VisualizeResult } from "@/lib/visualize-types";
|
|
|
|
function ChartJsRenderer({ config }: { config: string }) {
|
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
const chartRef = useRef<unknown>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
|
|
async function render() {
|
|
if (!canvasRef.current) return;
|
|
|
|
try {
|
|
const ChartModule = await import("chart.js/auto");
|
|
const Chart = ChartModule.default;
|
|
|
|
if (chartRef.current) {
|
|
(chartRef.current as InstanceType<typeof Chart>).destroy();
|
|
chartRef.current = null;
|
|
}
|
|
|
|
// eslint-disable-next-line no-new-func
|
|
const parsedConfig = new Function(`"use strict"; return (${config});`)();
|
|
|
|
if (cancelled) return;
|
|
|
|
chartRef.current = new Chart(canvasRef.current, parsedConfig);
|
|
setError(null);
|
|
} catch (err) {
|
|
if (!cancelled) {
|
|
setError(err instanceof Error ? err.message : "Failed to render chart");
|
|
}
|
|
}
|
|
}
|
|
|
|
void render();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
if (chartRef.current) {
|
|
(chartRef.current as { destroy: () => void }).destroy();
|
|
chartRef.current = null;
|
|
}
|
|
};
|
|
}, [config]);
|
|
|
|
if (error) {
|
|
return (
|
|
<div className="rounded-lg border border-red-200 bg-red-50 p-4 dark:border-red-900/60 dark:bg-red-950/30">
|
|
<p className="text-sm font-medium text-red-600 dark:text-red-400">
|
|
Chart rendering error
|
|
</p>
|
|
<pre className="mt-2 whitespace-pre-wrap text-xs text-red-500">{error}</pre>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="relative w-full" style={{ maxHeight: 480 }}>
|
|
<canvas ref={canvasRef} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function SvgRenderer({ svg }: { svg: string }) {
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const sanitizedSvg = useMemo(() => {
|
|
const trimmed = svg.trim();
|
|
if (!trimmed.startsWith("<svg")) {
|
|
setError("Invalid SVG: does not start with <svg");
|
|
return "";
|
|
}
|
|
setError(null);
|
|
return trimmed;
|
|
}, [svg]);
|
|
|
|
if (error) {
|
|
return (
|
|
<div className="rounded-lg border border-red-200 bg-red-50 p-4 dark:border-red-900/60 dark:bg-red-950/30">
|
|
<p className="text-sm font-medium text-red-600 dark:text-red-400">
|
|
SVG rendering error
|
|
</p>
|
|
<pre className="mt-2 whitespace-pre-wrap text-xs text-red-500">{error}</pre>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div
|
|
ref={containerRef}
|
|
className="flex justify-center overflow-x-auto"
|
|
dangerouslySetInnerHTML={{ __html: sanitizedSvg }}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export default function VisualizationViewer({
|
|
result,
|
|
}: {
|
|
result: VisualizeResult;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const [showCode, setShowCode] = useState(false);
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
const handleCopy = async () => {
|
|
try {
|
|
await navigator.clipboard.writeText(result.code.content);
|
|
setCopied(true);
|
|
setTimeout(() => setCopied(false), 2000);
|
|
} catch {
|
|
/* clipboard API may be unavailable */
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-3">
|
|
{/* Visualization area */}
|
|
<div className="overflow-hidden rounded-xl border border-[var(--border)] bg-[var(--background)] p-4">
|
|
{result.render_type === "svg" ? (
|
|
<SvgRenderer svg={result.code.content} />
|
|
) : (
|
|
<ChartJsRenderer config={result.code.content} />
|
|
)}
|
|
</div>
|
|
|
|
{/* Toolbar */}
|
|
<div className="flex items-center gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowCode((prev) => !prev)}
|
|
className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)] px-2.5 py-1.5 text-[11px] font-medium text-[var(--muted-foreground)] transition-colors hover:text-[var(--foreground)]"
|
|
>
|
|
<Code2 size={12} strokeWidth={1.8} />
|
|
{showCode ? t("Hide code") : t("Show code")}
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={handleCopy}
|
|
className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)] px-2.5 py-1.5 text-[11px] font-medium text-[var(--muted-foreground)] transition-colors hover:text-[var(--foreground)]"
|
|
>
|
|
{copied ? <Check size={12} strokeWidth={1.8} /> : <Copy size={12} strokeWidth={1.8} />}
|
|
{copied ? t("Copied") : t("Copy code")}
|
|
</button>
|
|
|
|
<span className="ml-auto text-[10px] uppercase tracking-wider text-[var(--muted-foreground)]/50">
|
|
{result.render_type === "svg" ? "SVG" : `Chart.js · ${result.analysis.chart_type || "chart"}`}
|
|
</span>
|
|
</div>
|
|
|
|
{/* Code panel */}
|
|
{showCode && (
|
|
<div className="overflow-hidden rounded-xl border border-[var(--border)] bg-[#1f2937]">
|
|
<div className="border-b border-white/10 px-3 py-2 text-[11px] font-medium uppercase tracking-wider text-[#9ca3af]">
|
|
{result.code.language}
|
|
</div>
|
|
<pre className="max-h-80 overflow-auto p-4 text-[13px] leading-relaxed text-[#d1d5db]">
|
|
<code>{result.code.content}</code>
|
|
</pre>
|
|
</div>
|
|
)}
|
|
|
|
{/* Review notes */}
|
|
{result.review.changed && result.review.review_notes && (
|
|
<p className="text-[11px] text-[var(--muted-foreground)]">
|
|
{t("Review")}: {result.review.review_notes}
|
|
</p>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|