mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 07:39:53 +08:00
Capability infrastructure:
- New capabilities/_shared.emit_capability_result helper fills the
result envelope (label, summary, payload, render hints, UsageTracker
totals) consistently across modes.
- StatusI18n loads capability status copy from
capabilities/prompts/{en,zh}/<name>.yaml; legacy main.yaml strings
removed.
- New capabilities_settings router + service-config module power the
/settings/capabilities admin page (live capability list, defaults,
per-capability overrides).
Visualize + Animator merge:
- Visualize's AnalysisAgent now picks one of six render_types:
svg / chartjs / mermaid / html (text pipeline) or manim_video /
manim_image (Manim subprocess). The result envelope carries
render_type so the frontend delegates to the right viewer.
- math_animator capability is folded behind Visualize; the standalone
MathAnimatorConfigPanel.tsx is removed. The Animator sidebar entry
is gone, but Manim outputs and existing records remain unaffected.
Settings UI:
- /settings page split into appearance / capabilities / embedding /
llm / mcp / memory / search / status / tools tabs with a shared
layout and items index.
- New /settings/tools page lists chat-mountable tools and surfaces
availability (e.g. gh for github_query) via a new tools router.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
382 lines
13 KiB
TypeScript
382 lines
13 KiB
TypeScript
"use client";
|
|
|
|
import dynamic from "next/dynamic";
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
import { Code2, Copy, Check, ExternalLink, Maximize2, X } from "lucide-react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Mermaid } from "@/components/Mermaid";
|
|
import { prepareIframeHtml } from "@/lib/iframe-html";
|
|
import { isManimResult, type VisualizeResult } from "@/lib/visualize-types";
|
|
|
|
const MathAnimatorViewer = dynamic(
|
|
() => import("@/components/math-animator/MathAnimatorViewer"),
|
|
{ ssr: false },
|
|
);
|
|
|
|
function stripCodeFence(source: string): string {
|
|
const trimmed = source.trim();
|
|
const fenced = trimmed.match(
|
|
/^```(?:json|javascript|js)?\s*([\s\S]*?)\s*```$/i,
|
|
);
|
|
return fenced ? fenced[1].trim() : trimmed;
|
|
}
|
|
|
|
function parseChartConfig(source: string): unknown {
|
|
const raw = stripCodeFence(source);
|
|
try {
|
|
return JSON.parse(raw);
|
|
} catch {
|
|
const jsonish = raw
|
|
.replace(/([{,]\s*)([A-Za-z_$][\w$]*)\s*:/g, '$1"$2":')
|
|
.replace(/'([^'\\]*(?:\\.[^'\\]*)*)'/g, (_match, value: string) =>
|
|
JSON.stringify(value.replace(/\\'/g, "'")),
|
|
)
|
|
.replace(/,\s*([}\]])/g, "$1");
|
|
return JSON.parse(jsonish);
|
|
}
|
|
}
|
|
|
|
function ChartJsRenderer({ config }: { config: string }) {
|
|
const { t } = useTranslation();
|
|
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;
|
|
}
|
|
|
|
const parsedConfig = parseChartConfig(config) as ConstructorParameters<
|
|
typeof Chart
|
|
>[1];
|
|
|
|
if (cancelled) return;
|
|
|
|
chartRef.current = new Chart(canvasRef.current, parsedConfig);
|
|
setError(null);
|
|
} catch (err) {
|
|
if (!cancelled) {
|
|
setError(
|
|
err instanceof Error ? err.message : t("Failed to render chart"),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
void render();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
if (chartRef.current) {
|
|
(chartRef.current as { destroy: () => void }).destroy();
|
|
chartRef.current = null;
|
|
}
|
|
};
|
|
}, [config, t]);
|
|
|
|
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">
|
|
{t("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 HtmlRenderer({ html }: { html: string }) {
|
|
const { t } = useTranslation();
|
|
const iframeRef = useRef<HTMLIFrameElement>(null);
|
|
|
|
const prepared = useMemo(() => prepareIframeHtml(html || ""), [html]);
|
|
|
|
useEffect(() => {
|
|
const iframe = iframeRef.current;
|
|
if (!iframe) return;
|
|
iframe.srcdoc = prepared;
|
|
}, [prepared]);
|
|
|
|
const handleOpenInNewTab = () => {
|
|
try {
|
|
const contentUrl = URL.createObjectURL(
|
|
new Blob([prepared], { type: "text/html" }),
|
|
);
|
|
const wrapper = `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Visualization</title><style>html,body,iframe{height:100%;width:100%;margin:0;border:0;}</style></head><body><iframe sandbox="allow-scripts" src="${contentUrl}"></iframe></body></html>`;
|
|
const url = URL.createObjectURL(
|
|
new Blob([wrapper], { type: "text/html" }),
|
|
);
|
|
window.open(url, "_blank", "noopener,noreferrer");
|
|
setTimeout(() => {
|
|
URL.revokeObjectURL(url);
|
|
URL.revokeObjectURL(contentUrl);
|
|
}, 60_000);
|
|
} catch {
|
|
/* no-op */
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="relative w-full">
|
|
<button
|
|
type="button"
|
|
onClick={handleOpenInNewTab}
|
|
className="absolute right-2 top-2 z-10 inline-flex items-center gap-1 rounded-md border border-[var(--border)] bg-[var(--background)]/90 px-2 py-1 text-[10px] font-medium text-[var(--muted-foreground)] backdrop-blur transition-colors hover:text-[var(--foreground)]"
|
|
title={t("Open in new tab")}
|
|
>
|
|
<ExternalLink size={10} strokeWidth={1.8} />
|
|
{t("Open")}
|
|
</button>
|
|
<iframe
|
|
ref={iframeRef}
|
|
title={t("HTML visualization")}
|
|
sandbox="allow-scripts"
|
|
className="w-full rounded-lg border border-[var(--border)] bg-[var(--card)]"
|
|
style={{ minHeight: 480, height: 560 }}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function SvgRenderer({ svg }: { svg: string }) {
|
|
const { t } = useTranslation();
|
|
const trimmedSvg = svg.trim();
|
|
const error = trimmedSvg.startsWith("<svg")
|
|
? null
|
|
: t("Invalid SVG: does not start with <svg");
|
|
const svgUrl = useMemo(
|
|
() =>
|
|
error
|
|
? ""
|
|
: `data:image/svg+xml;charset=utf-8,${encodeURIComponent(trimmedSvg)}`,
|
|
[error, trimmedSvg],
|
|
);
|
|
|
|
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">
|
|
{t("SVG rendering error")}
|
|
</p>
|
|
<pre className="mt-2 whitespace-pre-wrap text-xs text-red-500">
|
|
{error}
|
|
</pre>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="flex justify-center overflow-x-auto">
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img src={svgUrl} alt={t("SVG visualization")} className="max-w-full" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
type TextResult = Extract<
|
|
VisualizeResult,
|
|
{ render_type: "svg" | "chartjs" | "mermaid" | "html" }
|
|
>;
|
|
|
|
function renderTextVisualization(result: TextResult) {
|
|
if (result.render_type === "svg") {
|
|
return <SvgRenderer svg={result.code.content} />;
|
|
}
|
|
if (result.render_type === "mermaid") {
|
|
return <Mermaid chart={result.code.content} />;
|
|
}
|
|
if (result.render_type === "html") {
|
|
return <HtmlRenderer html={result.code.content} />;
|
|
}
|
|
return <ChartJsRenderer config={result.code.content} />;
|
|
}
|
|
|
|
export default function VisualizationViewer({
|
|
result,
|
|
}: {
|
|
result: VisualizeResult;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
|
|
// All hooks must run unconditionally before any early return — React
|
|
// requires a stable hook order across renders. The text-path body below
|
|
// is the only consumer of these states; the manim path returns earlier
|
|
// and ignores them.
|
|
const [showCode, setShowCode] = useState(false);
|
|
const [copied, setCopied] = useState(false);
|
|
const [fullscreen, setFullscreen] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!fullscreen) return;
|
|
const onKey = (e: KeyboardEvent) => {
|
|
if (e.key === "Escape") setFullscreen(false);
|
|
};
|
|
document.addEventListener("keydown", onKey);
|
|
const prevOverflow = document.body.style.overflow;
|
|
document.body.style.overflow = "hidden";
|
|
return () => {
|
|
document.removeEventListener("keydown", onKey);
|
|
document.body.style.overflow = prevOverflow;
|
|
};
|
|
}, [fullscreen]);
|
|
|
|
if (isManimResult(result)) {
|
|
return <MathAnimatorViewer result={result.manim} />;
|
|
}
|
|
|
|
// TypeScript narrows ``result`` to the text-only variant from here on.
|
|
// HTML iframe already provides its own "Open in new tab" affordance; the
|
|
// sandboxed iframe also doesn't behave well inside a re-rendered modal.
|
|
const supportsFullscreen = result.render_type !== "html";
|
|
|
|
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={`relative ${
|
|
result.render_type === "html"
|
|
? "overflow-hidden rounded-xl border border-[var(--border)] bg-[var(--background)]"
|
|
: "overflow-hidden rounded-xl border border-[var(--border)] bg-[var(--background)] p-4"
|
|
}`}
|
|
>
|
|
{supportsFullscreen && (
|
|
<button
|
|
type="button"
|
|
onClick={() => setFullscreen(true)}
|
|
title={t("Fullscreen")}
|
|
className="absolute right-2 top-2 z-10 inline-flex items-center gap-1 rounded-md border border-[var(--border)] bg-[var(--background)]/90 px-2 py-1 text-[10px] font-medium text-[var(--muted-foreground)] backdrop-blur transition-colors hover:text-[var(--foreground)]"
|
|
>
|
|
<Maximize2 size={10} strokeWidth={1.8} />
|
|
{t("Fullscreen")}
|
|
</button>
|
|
)}
|
|
{renderTextVisualization(result)}
|
|
</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"
|
|
: result.render_type === "mermaid"
|
|
? `Mermaid · ${result.analysis.chart_type || "diagram"}`
|
|
: result.render_type === "html"
|
|
? `HTML · ${result.analysis.chart_type || "interactive"}`
|
|
: `Chart.js · ${result.analysis.chart_type || "chart"}`}
|
|
</span>
|
|
</div>
|
|
|
|
{/* Code panel — matches the always-dark .md-code-block style used by the
|
|
markdown renderers so a "Show code" toggle inside a chart message
|
|
looks identical to a fenced code block in the assistant response. */}
|
|
{showCode && (
|
|
<div className="md-code-block 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-[#e5e7eb]">
|
|
<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>
|
|
)}
|
|
|
|
{/* Fullscreen overlay */}
|
|
{fullscreen && supportsFullscreen && (
|
|
<div
|
|
className="fixed inset-0 z-[120] flex flex-col bg-black/85 p-4 backdrop-blur-sm"
|
|
onClick={() => setFullscreen(false)}
|
|
>
|
|
<div className="mb-2 flex shrink-0 items-center justify-between text-white">
|
|
<div className="text-xs uppercase tracking-wider opacity-80">
|
|
{result.render_type === "svg"
|
|
? "SVG"
|
|
: result.render_type === "mermaid"
|
|
? `Mermaid · ${result.analysis.chart_type || "diagram"}`
|
|
: `Chart.js · ${result.analysis.chart_type || "chart"}`}
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
setFullscreen(false);
|
|
}}
|
|
title={t("Close")}
|
|
className="inline-flex items-center gap-1 rounded-md bg-white/10 px-2.5 py-1.5 text-[11px] font-medium text-white transition-colors hover:bg-white/20"
|
|
>
|
|
<X size={12} strokeWidth={1.8} />
|
|
{t("Close")}
|
|
</button>
|
|
</div>
|
|
<div
|
|
className="flex flex-1 items-center justify-center overflow-auto rounded-xl bg-[var(--card)] p-6 shadow-2xl"
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
<div className="w-full max-w-[1600px]">
|
|
{renderTextVisualization(result)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|