fix(webui): stop repairing completed Markdown

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-30 15:50:27 +08:00
1 parent fc3effe39d
commit 450a85fe0e
6 files changed
+48 -7

No files matched your search

+1
View File
@@ -28,6 +28,7 @@
"remark-breaks": "^4.0.0", "remark-breaks": "^4.0.0",
"remark-gfm": "^4.0.0", "remark-gfm": "^4.0.0",
"remark-math": "^6.0.0", "remark-math": "^6.0.0",
"remend": "1.3.0",
"streamdown": "2.5.0", "streamdown": "2.5.0",
"tailwind-merge": "^2.6.0", "tailwind-merge": "^2.6.0",
}, },
+1
View File
@@ -36,6 +36,7 @@
"remark-breaks": "^4.0.0", "remark-breaks": "^4.0.0",
"remark-gfm": "^4.0.0", "remark-gfm": "^4.0.0",
"remark-math": "^6.0.0", "remark-math": "^6.0.0",
"remend": "1.3.0",
"streamdown": "2.5.0", "streamdown": "2.5.0",
"tailwind-merge": "^2.6.0" "tailwind-merge": "^2.6.0"
}, },
+5 -2
View File
@@ -25,12 +25,14 @@ const MemoizedMarkdownRenderer = memo(function MemoizedMarkdownRenderer({
className, className,
highlightCode, highlightCode,
streaming, streaming,
preserveStreamingLayout,
onOpenFilePreview, onOpenFilePreview,
}: { }: {
source: string; source: string;
className?: string; className?: string;
highlightCode: boolean; highlightCode: boolean;
streaming: boolean; streaming: boolean;
preserveStreamingLayout: boolean;
onOpenFilePreview?: (path: string) => void; onOpenFilePreview?: (path: string) => void;
}) { }) {
return ( return (
@@ -38,6 +40,7 @@ const MemoizedMarkdownRenderer = memo(function MemoizedMarkdownRenderer({
className={className} className={className}
highlightCode={highlightCode} highlightCode={highlightCode}
streaming={streaming} streaming={streaming}
preserveStreamingLayout={preserveStreamingLayout}
onOpenFilePreview={onOpenFilePreview} onOpenFilePreview={onOpenFilePreview}
> >
{source} {source}
@@ -81,7 +84,6 @@ export function MarkdownText({
const renderedSource = children; const renderedSource = children;
const renderPhase = streaming ? "streaming" : "complete"; const renderPhase = streaming ? "streaming" : "complete";
const highlightCode = !streaming; const highlightCode = !streaming;
const renderWithStreamingLayout = streaming || preserveStreamingLayout;
useEffect(() => { useEffect(() => {
if (streaming) void preloadMarkdownText(); if (streaming) void preloadMarkdownText();
@@ -106,7 +108,8 @@ export function MarkdownText({
source={renderedSource} source={renderedSource}
className={className} className={className}
highlightCode={highlightCode} highlightCode={highlightCode}
streaming={renderWithStreamingLayout} streaming={streaming}
preserveStreamingLayout={preserveStreamingLayout}
onOpenFilePreview={onOpenFilePreview} onOpenFilePreview={onOpenFilePreview}
/> />
</Suspense> </Suspense>
@@ -12,6 +12,7 @@ import remarkBreaks from "remark-breaks";
import remarkGfm from "remark-gfm"; import remarkGfm from "remark-gfm";
import remarkMath from "remark-math"; import remarkMath from "remark-math";
import { Streamdown, type Components, type StreamdownProps } from "streamdown"; import { Streamdown, type Components, type StreamdownProps } from "streamdown";
import remend from "remend";
import { AttachmentTile } from "@/components/AttachmentTile"; import { AttachmentTile } from "@/components/AttachmentTile";
import { CodeBlock } from "@/components/CodeBlock"; import { CodeBlock } from "@/components/CodeBlock";
@@ -42,6 +43,7 @@ interface MarkdownTextRendererProps {
className?: string; className?: string;
highlightCode?: boolean; highlightCode?: boolean;
streaming?: boolean; streaming?: boolean;
preserveStreamingLayout?: boolean;
onOpenFilePreview?: (path: string) => void; onOpenFilePreview?: (path: string) => void;
} }
@@ -530,6 +532,7 @@ export default function MarkdownTextRenderer({
className, className,
highlightCode = true, highlightCode = true,
streaming = false, streaming = false,
preserveStreamingLayout = false,
onOpenFilePreview, onOpenFilePreview,
}: MarkdownTextRendererProps) { }: MarkdownTextRendererProps) {
const { t } = useTranslation(); const { t } = useTranslation();
@@ -826,9 +829,8 @@ export default function MarkdownTextRenderer({
return ( return (
<Streamdown <Streamdown
key={needsMath && mathPlugin ? "math" : "text"} key={needsMath && mathPlugin ? "math" : "text"}
mode={streaming ? "streaming" : "static"} mode={streaming || preserveStreamingLayout ? "streaming" : "static"}
parseIncompleteMarkdown parseIncompleteMarkdown={false}
remend={REMEND_OPTIONS}
isAnimating={false} isAnimating={false}
animated={false} animated={false}
linkSafety={DIRECT_LINKS} linkSafety={DIRECT_LINKS}
@@ -851,7 +853,8 @@ export default function MarkdownTextRenderer({
className, className,
)} )}
> >
{children} {/* Streamdown 2.5 ignores repair-option changes in its memo comparator. */}
{streaming ? remend(children, REMEND_OPTIONS) : children}
</Streamdown> </Streamdown>
); );
} }
@@ -516,6 +516,30 @@ describe("MarkdownTextRenderer", () => {
expect(container.querySelector("[data-nanobot-stream-unit]")).not.toBeInTheDocument(); expect(container.querySelector("[data-nanobot-stream-unit]")).not.toBeInTheDocument();
}); });
it("stops repairing completed markdown without replacing the streaming layout", () => {
const source = "The old snip_history() / _legal_history_tail() path was removed.\n\nA real maintenance cost.";
const { container, rerender } = render(
<MarkdownTextRenderer streaming preserveStreamingLayout>{source}</MarkdownTextRenderer>,
);
const firstParagraph = container.querySelector("p");
expect(container.querySelector("p:last-child")?.textContent).toBe("A real maintenance cost._");
rerender(<MarkdownTextRenderer preserveStreamingLayout>{source}</MarkdownTextRenderer>);
expect(container.querySelector("p:last-child")?.textContent).toBe("A real maintenance cost.");
expect(container.querySelector("p")).toBe(firstParagraph);
});
it.each(["A literal trailing underscore_", "**unfinished emphasis", "_legal_history_tail()"])(
"preserves completed source syntax: %s",
(source) => {
const { container } = render(
<MarkdownTextRenderer preserveStreamingLayout>{source}</MarkdownTextRenderer>,
);
expect(container.textContent).toBe(source);
},
);
it("repairs incomplete streaming markdown without exposing syntax fragments", () => { it("repairs incomplete streaming markdown without exposing syntax fragments", () => {
const { container, rerender } = render( const { container, rerender } = render(
<MarkdownTextRenderer streaming>{"**partial answer"}</MarkdownTextRenderer>, <MarkdownTextRenderer streaming>{"**partial answer"}</MarkdownTextRenderer>,
+10 -1
View File
@@ -13,10 +13,12 @@ vi.mock("@/components/MarkdownTextRenderer", () => ({
children, children,
highlightCode, highlightCode,
streaming, streaming,
preserveStreamingLayout,
}: { }: {
children: string; children: string;
highlightCode?: boolean; highlightCode?: boolean;
streaming?: boolean; streaming?: boolean;
preserveStreamingLayout?: boolean;
}) { }) {
useEffect(() => { useEffect(() => {
rendererMountSpy(); rendererMountSpy();
@@ -29,7 +31,8 @@ vi.mock("@/components/MarkdownTextRenderer", () => ({
<div <div
data-testid="markdown-renderer" data-testid="markdown-renderer"
data-highlight-code={String(highlightCode)} data-highlight-code={String(highlightCode)}
data-streaming-layout={String(streaming)} data-streaming-layout={String(streaming || preserveStreamingLayout)}
data-streaming={String(streaming)}
> >
{children} {children}
</div> </div>
@@ -108,6 +111,7 @@ describe("MarkdownText", () => {
}); });
it("can complete without replacing the streaming renderer layout", async () => { it("can complete without replacing the streaming renderer layout", async () => {
rendererMountSpy.mockClear();
const source = "A layout-stable answer"; const source = "A layout-stable answer";
const { rerender } = render( const { rerender } = render(
<MarkdownText streaming preserveStreamingLayout>{source}</MarkdownText>, <MarkdownText streaming preserveStreamingLayout>{source}</MarkdownText>,
@@ -122,8 +126,13 @@ describe("MarkdownText", () => {
"true", "true",
); );
expect(screen.getByTestId("markdown-renderer")).toHaveAttribute("data-streaming", "true");
rerender(<MarkdownText preserveStreamingLayout>{source}</MarkdownText>); rerender(<MarkdownText preserveStreamingLayout>{source}</MarkdownText>);
expect(screen.getByTestId("markdown-renderer")).toHaveAttribute("data-streaming", "false");
expect(rendererMountSpy).toHaveBeenCalledTimes(1);
expect(screen.getByTestId("markdown-renderer")).toHaveAttribute( expect(screen.getByTestId("markdown-renderer")).toHaveAttribute(
"data-streaming-layout", "data-streaming-layout",
"true", "true",