mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:50:36 +08:00
- Add new `deeptutor/book/` module with multi-agent pipeline (ideation, spine, page planner, source explorer, synthesizer) and rich block system (text, code, figure, quiz, flash cards, callout, deep dive, timeline, animation, concept graph, etc.) - Add `/api/routers/book.py` and `deeptutor_cli/book.py` entry points - Add web UI under `web/app/(workspace)/book/` with sidebar, chat panel, page reader, spine editor, progress timeline, and block renderers - Move `deeptutor/agents/co_writer` -> `deeptutor/co_writer` and `deeptutor/agents/guide` -> `deeptutor/guide` (then remove guide entirely now superseded by book) - Remove legacy guide UI, hooks, types, and API router - Update visualize agents/prompts, notebook service, prompt manager, session turn runtime, and shared services to support new flows - Refresh chat composer, sidebar, notebook selector, and locales Made-with: Cursor
244 lines
7.8 KiB
TypeScript
244 lines
7.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
import { Code2, Copy, Check, ExternalLink } from "lucide-react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Mermaid } from "@/components/Mermaid";
|
|
import { prepareIframeHtml } from "@/lib/iframe-html";
|
|
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 HtmlRenderer({ html }: { html: string }) {
|
|
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 blob = new Blob([prepared], { type: "text/html" });
|
|
const url = URL.createObjectURL(blob);
|
|
window.open(url, "_blank", "noopener,noreferrer");
|
|
// Best-effort cleanup; the new tab keeps its own reference.
|
|
setTimeout(() => URL.revokeObjectURL(url), 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="Open in new tab"
|
|
>
|
|
<ExternalLink size={10} strokeWidth={1.8} />
|
|
Open
|
|
</button>
|
|
<iframe
|
|
ref={iframeRef}
|
|
title="HTML visualization"
|
|
sandbox="allow-scripts"
|
|
className="w-full rounded-lg border border-[var(--border)] bg-white"
|
|
style={{ minHeight: 480, height: 560 }}
|
|
/>
|
|
</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={
|
|
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"
|
|
}
|
|
>
|
|
{result.render_type === "svg" ? (
|
|
<SvgRenderer svg={result.code.content} />
|
|
) : result.render_type === "mermaid" ? (
|
|
<Mermaid chart={result.code.content} />
|
|
) : result.render_type === "html" ? (
|
|
<HtmlRenderer html={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"
|
|
: 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 */}
|
|
{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>
|
|
);
|
|
}
|