chore(ai): remove inappropriate fade-in animation

- Added AIMarkdownMessage.old.tsx for legacy markdown rendering.
- Introduced AIMarkdownMessage.v2.tsx with optimized streaming capabilities.
- Updated AIMessageParts to utilize the new AIMarkdownMessage.v2 component.
- Adjusted UserChatMessage and UserMessageParts to reference the appropriate AIMarkdownMessage versions.

This update enhances the markdown rendering experience during streaming interactions.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-09-01 21:39:33 +08:00
parent 3b83c27571
commit c8ba9aa2cd
5 changed files with 137 additions and 16 deletions
@@ -1,3 +1,7 @@
/**
* This AI Markdown Message use a optimize tokenization algorithm to render the streaming text.
* But tokenization put into the server response, so we can disable it.
*/
import type { LinkProps } from "@follow/components/ui/link/LinkWithTooltip.js"
import { parseMarkdown } from "@follow/components/utils/parse-markdown.js"
import { cn, isBizId } from "@follow/utils"
@@ -334,6 +338,9 @@ const useThrottledMarkdownParsing = (text: string, isProcessing: boolean) => {
}, [text, isProcessing, parseWithCache])
}
/**
* @deprecated
*/
export const AIMarkdownStreamingMessage = memo(
({
text,
@@ -0,0 +1,115 @@
import type { LinkProps } from "@follow/components/ui/link/LinkWithTooltip.js"
import { parseMarkdown } from "@follow/components/utils/parse-markdown.js"
import { cn, isBizId } from "@follow/utils"
import { createElement, isValidElement, memo, useMemo } from "react"
import { MemoizedShikiCode } from "~/components/ui/code-highlighter"
import { MarkdownLink } from "~/components/ui/markdown/renderers/MarkdownLink"
import { usePeekModal } from "~/hooks/biz/usePeekModal"
export const AIMarkdownStreamingMessage = memo(
({ text, className: classNameProp }: { text: string; className?: string }) => {
const className = `prose max-w-full dark:prose-invert text-sm
prose-h1:text-2xl prose-h2:text-xl prose-h3:text-lg prose-h4:text-base prose-h5:text-base prose-h6:text-sm
prose-li:list-disc prose-li:marker:text-accent prose-hr:border-border prose-hr:mx-8`
const parsedContent = useMemo(() => baseAIMarkdownParser(text), [text])
return <div className={cn(className, classNameProp)}>{parsedContent}</div>
},
)
const RelatedEntryLink = (props: LinkProps) => {
const { href, children } = props
const entryId = isBizId(href) ? href : null
const peekModal = usePeekModal()
if (!entryId) {
return <MarkdownLink {...props} />
}
return (
<button
type="button"
className="follow-link--underline text-text cursor-pointer font-semibold no-underline"
onClick={() => {
peekModal(entryId, "modal")
}}
>
{children}
<i className="i-mgc-arrow-right-up-cute-re size-[0.9em] translate-y-[2px] opacity-70" />
</button>
)
}
function baseAIMarkdownParser(content: string) {
return parseMarkdown(content, {
components: {
pre: ({ children }) => {
const props = isValidElement(children) && "props" in children && children.props
if (props) {
const { className, children } = props as any
if (className && className.includes("language-") && typeof children === "string") {
const language = className.replace("language-", "")
const code = children
return <MemoizedShikiCode code={code} language={language} showCopy />
}
}
return <pre className="text-text-secondary">{children}</pre>
},
a: ({ node, ...props }) => {
return createElement(RelatedEntryLink, { ...props } as any)
},
table: ({ children, ref, node, ...props }) => {
return (
<div className="border-border bg-material-thin overflow-x-auto rounded-lg border">
<table {...props} className="divide-border my-0 min-w-full divide-y text-sm">
{children}
</table>
</div>
)
},
thead: ({ children, ref, node, ...props }) => {
return (
<thead {...props} className="bg-fill-tertiary">
{children}
</thead>
)
},
th: ({ children, ref, node, ...props }) => {
return (
<th
{...props}
className="text-text-secondary whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase tracking-wider"
>
{children}
</th>
)
},
tbody: ({ children, ref, node, ...props }) => {
return (
<tbody {...props} className="bg-material-ultra-thin divide-border divide-y">
{children}
</tbody>
)
},
tr: ({ children, ref, node, ...props }) => {
return (
<tr {...props} className="hover:bg-material-thin transition-colors duration-150">
{children}
</tr>
)
},
td: ({ children, ref, node, ...props }) => {
return (
<td {...props} className="text-text whitespace-nowrap px-4 py-3 text-sm">
{children}
</td>
)
},
},
}).content
}
@@ -19,7 +19,7 @@ import {
AIDisplaySubscriptionsPart,
AIReasoningPart,
} from "../displays"
import { AIMarkdownStreamingMessage } from "./AIMarkdownMessage"
import { AIMarkdownStreamingMessage } from "./AIMarkdownMessage.v2"
import { ToolInvocationComponent } from "./ToolInvocationComponent"
const LazyAIDisplayFlowPart = React.lazy(() =>
@@ -36,14 +36,7 @@ export const AIMessageParts: React.FC<AIMessagePartsProps> = React.memo(({ messa
switch (part.type) {
case "text": {
return (
<AIMarkdownStreamingMessage
isProcessing={message.metadata?.totalTokens === undefined}
key={partKey}
text={part.text}
className={"text-text"}
/>
)
return <AIMarkdownStreamingMessage key={partKey} text={part.text} className={"text-text"} />
}
case "reasoning": {
@@ -5,7 +5,7 @@ import { AnimatePresence, m } from "motion/react"
import * as React from "react"
import { useEditingMessageId, useSetEditingMessageId } from "~/modules/ai-chat/atoms/session"
import { useChatActions } from "~/modules/ai-chat/store/hooks"
import { useChatActions, useChatStatus } from "~/modules/ai-chat/store/hooks"
import type { AIChatContextBlock, BizUIMessage } from "~/modules/ai-chat/store/types"
import type { RichTextPart } from "../../types/ChatSession"
@@ -29,6 +29,8 @@ export const UserChatMessage: React.FC<UserChatMessageProps> = React.memo(({ mes
const editingMessageId = useEditingMessageId()
const setEditingMessageId = useSetEditingMessageId()
const chatStatus = useChatStatus()
const isStreaming = chatStatus === "submitted" || chatStatus === "streaming"
const isEditing = editingMessageId === messageId
// Extract data-block parts for separate rendering
@@ -115,11 +117,15 @@ export const UserChatMessage: React.FC<UserChatMessageProps> = React.memo(({ mes
{/* Main chat message */}
<m.div
initial={{
opacity: 0,
y: 20,
scale: 0.95,
}}
initial={
isStreaming
? {
opacity: 0,
y: 20,
scale: 0.95,
}
: true
}
animate={{
opacity: 1,
y: 0,
@@ -2,7 +2,7 @@ import * as React from "react"
import type { BizUIMessage } from "~/modules/ai-chat/store/types"
import { AIMarkdownMessage } from "./AIMarkdownMessage"
import { AIMarkdownMessage } from "./AIMarkdownMessage.old"
import { AIRichTextMessage } from "./AIRichTextMessage"
interface UserMessagePartsProps {