mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 05:59:41 +08:00
* fix(web): repair malformed strong-emphasis spacing in assistant output
* style(web): keep removeEmptyMarkdownTables' loop header as dev has it
The branch reformatted an untouched line ('; )' -> ';)'). The repo's real
style is not what .prettierrc reports, so a stray prettier pass here would
land as unrelated churn in the squashed commit.
* fix(web): make the strong-emphasis repair fail-safe
The rewrite paired an opening ``**`` with the next one on the line
regardless of whether the line's markers pair off at all, so it corrupted
Markdown the renderer already handles correctly (checked against the
repo's own micromark):
- ``In Markdown, use ** to make text **bold**.`` lost its bold — the
capture was allowed to start at whitespace, so a literal ``**`` in
prose swallowed the sentence up to the next opener.
- ``**Note: **Important**`` became ``**Note:** Important**``, dropping
the bold and leaking a stray ``**``.
- ``**重點 **必讀** 內容**`` — valid nested emphasis — lost most of its
bold.
Anchor the capture at a non-space, require it to end at a colon (the
reported shape), and skip any line whose ``**`` count is odd: with an odd
count at least one marker is literal or unclosed, and there is no way to
tell which two were meant to pair. Bailing out is free — the line then
renders exactly as it does without this repair. Also skip indented code
blocks, which are shown verbatim and are not masked with the fenced ones.
---------
Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
101 lines
3.4 KiB
TypeScript
101 lines
3.4 KiB
TypeScript
"use client";
|
|
|
|
import { Fragment, memo, useMemo } from "react";
|
|
|
|
import MarkdownRenderer from "@/components/common/MarkdownRenderer";
|
|
import ModelThinkingCard from "@/components/common/ModelThinkingCard";
|
|
import {
|
|
hasVisibleMarkdownContent,
|
|
repairMalformedStrongEmphasis,
|
|
stripArtifactAnnotations,
|
|
} from "@/lib/markdown-display";
|
|
import { parseModelThinkingSegments } from "@/lib/think-segments";
|
|
import { useSmoothStreamText } from "@/hooks/useSmoothStreamText";
|
|
|
|
interface AssistantResponseProps {
|
|
content: string;
|
|
className?: string;
|
|
/**
|
|
* When true, the renderer drives the visible text through a rAF
|
|
* typewriter (``useSmoothStreamText``) so the markdown grows at a
|
|
* steady, frame-aligned pace even when the upstream LLM emits
|
|
* uneven chunks. Pass ``false`` for completed turns and any non-
|
|
* streaming surface — the hook short-circuits to a pass-through
|
|
* in that case.
|
|
*/
|
|
isStreaming?: boolean;
|
|
}
|
|
|
|
function AssistantResponseImpl({
|
|
content,
|
|
className = "text-[16px] leading-[1.75]",
|
|
isStreaming = false,
|
|
}: AssistantResponseProps) {
|
|
const displayContent = useSmoothStreamText(content, isStreaming);
|
|
const segments = useMemo(
|
|
() => parseModelThinkingSegments(stripArtifactAnnotations(displayContent)),
|
|
[displayContent],
|
|
);
|
|
|
|
// Decide whether the message has anything worth rendering. We consider both
|
|
// ordinary markdown segments and model-thinking blocks: a turn that only
|
|
// ever produced a <think> scratchpad should still render the collapsed card
|
|
// instead of dropping the assistant bubble entirely.
|
|
const hasRenderableSegment = useMemo(() => {
|
|
return segments.some((segment) => {
|
|
if (segment.kind === "think") return segment.content.trim().length > 0;
|
|
return hasVisibleMarkdownContent(segment.content);
|
|
});
|
|
}, [segments]);
|
|
|
|
if (!hasRenderableSegment) return null;
|
|
|
|
// role="article" lets screen-reader users locate each assistant turn as a
|
|
// structured landmark. aria-live="polite" + aria-atomic="false" announces
|
|
// streamed-in content as the user pauses, without re-reading the whole
|
|
// bubble each token. Together this is the minimal pattern that turns a
|
|
// silent stream into an audible one.
|
|
return (
|
|
<div
|
|
role="article"
|
|
aria-live="polite"
|
|
aria-atomic="false"
|
|
className={className}
|
|
>
|
|
{segments.map((segment, index) => {
|
|
if (segment.kind === "think") {
|
|
return (
|
|
<ModelThinkingCard
|
|
key={`think-${index}`}
|
|
content={segment.content}
|
|
closed={segment.closed}
|
|
/>
|
|
);
|
|
}
|
|
const repairedContent = repairMalformedStrongEmphasis(segment.content);
|
|
|
|
if (!hasVisibleMarkdownContent(repairedContent)) {
|
|
return <Fragment key={`text-${index}`} />;
|
|
}
|
|
|
|
return (
|
|
<MarkdownRenderer
|
|
key={`text-${index}`}
|
|
content={repairedContent}
|
|
variant="prose"
|
|
className="text-[var(--foreground)]"
|
|
/>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// Memoize so completed messages don't re-parse markdown when an
|
|
// unrelated streaming sibling updates the parent — the streaming
|
|
// message gets a fresh ``msg.content`` per delta and re-renders
|
|
// naturally, but every other bubble keeps its previous render output.
|
|
const AssistantResponse = memo(AssistantResponseImpl);
|
|
AssistantResponse.displayName = "AssistantResponse";
|
|
export default AssistantResponse;
|