feat(frontend): enhance chat streaming visuals with shimmer effect and fade tail

- Introduce a shimmer effect for in-progress step titles in chat components, providing visual feedback during processing.
- Implement a trailing fade effect for the most recently streamed characters in chat markdown, enhancing the typing experience.
- Update CSS styles to support the new shimmer and fade effects, ensuring smooth transitions and visual coherence.
- Add tests to verify the functionality of the shimmer effect and the application of the fade tail in markdown rendering.
This commit is contained in:
wizardchen
2026-06-19 23:28:47 +08:00
committed by lyingbug
parent 58461a5b68
commit cc9de85c25
8 changed files with 222 additions and 35 deletions
+42 -1
View File
@@ -229,6 +229,46 @@ export function stripTrailingStreamingListMarker(text: string): string {
return text.replace(/(^|\n)[ \t]*(?:[-*+]|[-=]{2,}|\d{1,9}[.)]?)[ \t]*$/, '$1')
}
const TAIL_TEXT_RE = />([^<>]+)</g
/**
* Soften the most-recently streamed characters with a trailing fade.
*
* While the answer streams, the newest tail text is wrapped in a
* `stream-fade-tail` span so CSS can fade its trailing edge (the newest words
* are faint and settle to full color as more text arrives — the "tail reveal"
* look). Runs on the final HTML so the span is never touched by the markdown
* parser or sanitizer, and only the innermost last text run is wrapped.
*/
export function applyStreamingTailFade(html: string, tailLength = 24): string {
if (!html) return html
// Track the last text run that actually has visible characters. Whitespace-only
// runs (e.g. the `\n` marked emits between `</li>` and `</ol>`) are skipped so
// they do not win the "last run" and suppress the fade.
let lastMatch: RegExpExecArray | null = null
let match: RegExpExecArray | null
TAIL_TEXT_RE.lastIndex = 0
while ((match = TAIL_TEXT_RE.exec(html)) !== null) {
if (match[1].trim()) lastMatch = match
}
if (!lastMatch) return html
const text = lastMatch[1]
const textStart = lastMatch.index + 1
const textEnd = textStart + text.length
const chars = Array.from(text)
const tailChars = chars.slice(Math.max(0, chars.length - tailLength))
const tail = tailChars.join('')
// Fade only the trailing run, keeping any leading whitespace outside the span.
const tailTrimmed = tail.replace(/^\s+/, '')
if (!tailTrimmed) return html
const head = text.slice(0, text.length - tailTrimmed.length)
const wrapped = `${head}<span class="stream-fade-tail">${tailTrimmed}</span>`
return html.slice(0, textStart) + wrapped + html.slice(textEnd)
}
export function createChatMarkdownRenderer(options: ChatMarkdownRendererOptions = {}): Renderer {
const renderer = new marked.Renderer()
@@ -335,7 +375,8 @@ export function renderChatMarkdown(rawMarkdown: unknown, options: RenderChatMark
const tableWrappedHtml = wrapChatMarkdownTables(citationHtml)
const strongTitleHtml = markStandaloneStrongParagraphs(tableWrappedHtml)
const sanitized = options.sanitizeHtml(strongTitleHtml)
return options.injectCachedMermaidSvg
const withMermaid = options.injectCachedMermaidSvg
? options.injectCachedMermaidSvg(sanitized, options.cachedMermaidSvgHtml)
: sanitized
return options.streaming ? applyStreamingTailFade(withMermaid) : withMermaid
}