perf(webui): reduce repeated work and bound history rendering (#5703)

* perf(webui): reduce repeated work and bound history rendering

* fix(webui): keep continuation reasoning with its answer

* refactor(webui): reuse answer buffers and prepared copy text

* perf(webui): restore conditional replay buffer initialization

* fix(webui): remove code block pagination
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-09 14:02:26 +08:00
1 parent 1c451b5b5f
commit 1469d4811c
32 files changed
+1156 -361

No files matched your search

+28 -3
View File
@@ -7,7 +7,6 @@ import {
type ReactNode,
} from "react";
import { useTranslation } from "react-i18next";
import rehypeKatex from "rehype-katex";
import { Check, Globe2 } from "lucide-react";
import remarkBreaks from "remark-breaks";
import remarkGfm from "remark-gfm";
@@ -35,7 +34,6 @@ import { browserSafeFaviconUrls } from "@/lib/provider-brand";
import { remarkTexMath } from "@/lib/remark-tex-math";
import { cn } from "@/lib/utils";
import "katex/dist/katex.min.css";
import "streamdown/styles.css";
interface MarkdownTextRendererProps {
@@ -285,7 +283,19 @@ const remarkPlugins: NonNullable<StreamdownProps["remarkPlugins"]> = [
remarkCjkStrongBoundaries,
remarkSafeHtmlSubset,
];
const rehypePlugins: NonNullable<StreamdownProps["rehypePlugins"]> = [rehypeKatex];
type MathPlugin = typeof import("@/lib/markdown-math").default;
let loadedMathPlugin: MathPlugin | undefined;
let mathPluginPromise: Promise<MathPlugin> | undefined;
function loadMathPlugin(): Promise<MathPlugin> {
return mathPluginPromise ??= import("@/lib/markdown-math").then((module) => {
loadedMathPlugin = module.default;
return module.default;
}).catch((error) => {
mathPluginPromise = undefined;
throw error;
});
}
// Remend mistakes math comparisons like `j<i` for incomplete HTML and truncates
// the remaining text. HTML is handled by remarkSafeHtmlSubset, not raw rendering.
@@ -522,6 +532,20 @@ export default function MarkdownTextRenderer({
onOpenFilePreview,
}: MarkdownTextRendererProps) {
const { t } = useTranslation();
const [mathPlugin, setMathPlugin] = useState(() => loadedMathPlugin);
const needsMath = /\$|\\[([]/.test(children);
useEffect(() => {
if (!needsMath || mathPlugin) return;
let cancelled = false;
void loadMathPlugin().then((plugin) => {
if (!cancelled) setMathPlugin(() => plugin);
}).catch(() => {});
return () => { cancelled = true; };
}, [needsMath, mathPlugin]);
const rehypePlugins = useMemo<NonNullable<StreamdownProps["rehypePlugins"]>>(
() => needsMath && mathPlugin ? [mathPlugin] : [],
[needsMath, mathPlugin],
);
const components = useMemo<Components>(
() => ({
code({ className: cls, children: kids, node: _node, ...props }) {
@@ -798,6 +822,7 @@ export default function MarkdownTextRenderer({
return (
<Streamdown
key={needsMath && mathPlugin ? "math" : "text"}
mode={streaming ? "streaming" : "static"}
parseIncompleteMarkdown
remend={REMEND_OPTIONS}