feat(frontend): enhance chat image handling and typewriter effect

- Introduce a stable skeleton box for protected images during loading to prevent flickering.
- Implement a new composable `useStickyBottomOnResize` to maintain chat scroll position as content loads.
- Add `useTypewriter` for smoother text rendering in chat, improving user experience during streaming.
- Create a directive `vStableHtml` to efficiently update HTML content without losing image states.
- Enhance chat markdown rendering to handle incomplete citation tags and trailing horizontal rules during streaming.
- Add tests for new typewriter functionality and citation handling.
This commit is contained in:
wizardchen
2026-06-18 16:44:39 +08:00
committed by lyingbug
parent 95b97dc379
commit 80bcf200b6
15 changed files with 738 additions and 50 deletions
@@ -20,6 +20,7 @@ import { buildQueryWithHostContext } from '@/utils/embedContext'
import { fileToDataURI } from '@/utils/embedFile'
import { useI18n } from 'vue-i18n'
import { useChatStreamHandler } from '@/composables/useChatStreamHandler'
import { useStickyBottomOnResize } from '@/composables/useStickyBottomOnResize'
export function useEmbedChatSession(options: {
sessionId: Ref<string>
@@ -90,6 +91,8 @@ export function useEmbedChatSession(options: {
scrollToBottom(true)
}
useStickyBottomOnResize(scrollContainer, userHasScrolledUp, scrollToBottom)
const debounce = <T extends (...args: never[]) => void>(fn: T, delay: number) => {
let timer: ReturnType<typeof setTimeout>
return (...args: Parameters<T>) => {
@@ -0,0 +1,44 @@
import { onMounted, onUnmounted, type Ref } from 'vue'
/**
* Keep a chat pinned to the bottom when asynchronously rendered content grows.
*
* Streaming text already requests scrolling when chunks arrive, but images,
* Mermaid diagrams, and other rich content can change height later. Observe the
* message list itself so all delayed layout changes share the same behavior.
* A user who has intentionally scrolled up is never pulled back down.
*/
export function useStickyBottomOnResize(
scrollContainer: Ref<HTMLElement | null>,
userHasScrolledUp: Ref<boolean>,
scrollToBottom: () => void,
): void {
let resizeObserver: ResizeObserver | null = null
let scrollFrame: number | null = null
const scheduleFollow = () => {
if (userHasScrolledUp.value || scrollFrame !== null) return
scrollFrame = requestAnimationFrame(() => {
scrollFrame = null
if (!userHasScrolledUp.value) scrollToBottom()
})
}
onMounted(() => {
const messageList = scrollContainer.value?.firstElementChild
if (!messageList || typeof ResizeObserver === 'undefined') return
resizeObserver = new ResizeObserver(scheduleFollow)
resizeObserver.observe(messageList)
})
onUnmounted(() => {
resizeObserver?.disconnect()
resizeObserver = null
if (scrollFrame !== null) {
cancelAnimationFrame(scrollFrame)
scrollFrame = null
}
})
}
@@ -0,0 +1,28 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { nextTypewriterReveal } from './useTypewriter.ts'
test('reveals Chinese text in compact phrase groups', () => {
const text = '这是一个自然流畅的回答。'
assert.equal(nextTypewriterReveal(text, 0, 1), 0)
assert.equal(nextTypewriterReveal(text, 0, 2), 2)
assert.equal(nextTypewriterReveal(text, 0, 20), 4)
})
test('waits for a complete English word instead of revealing letter by letter', () => {
const text = 'Hello world'
assert.equal(nextTypewriterReveal(text, 0, 3), 0)
assert.equal(nextTypewriterReveal(text, 0, 6), 6)
assert.equal(text.slice(0, nextTypewriterReveal(text, 0, 6)), 'Hello ')
})
test('uses punctuation as a natural reveal boundary', () => {
const text = '你好,接下来继续。'
assert.equal(nextTypewriterReveal(text, 0, 3), 3)
})
test('never splits a surrogate pair', () => {
const text = '🙂 hello'
assert.equal(nextTypewriterReveal(text, 0, 1), 2)
})
+200
View File
@@ -0,0 +1,200 @@
import { computed, onBeforeUnmount, ref, watch, type ComputedRef } from 'vue';
export interface TypewriterOptions {
/** Comfortable reveal floor in characters per second. */
minCps?: number;
/** Time window (seconds) over which ordinary backlog is drained. */
drainSeconds?: number;
/** Upper speed limit when a large SSE chunk arrives at once. */
maxCps?: number;
/** Largest frame delta honored after a backgrounded tab resumes. */
maxFrameSeconds?: number;
}
const NATURAL_BREAK_RE = /[\s,。!?;:、,.!?;:)\]】》」』]/u;
const CJK_RE = /[\u3400-\u9fff\uf900-\ufaff\u3040-\u30ff\uac00-\ud7af]/u;
const WORD_CHARACTER_RE = /[\p{L}\p{N}_]/u;
function nextCodePointEnd(text: string, index: number): number {
if (index >= text.length) return text.length;
const code = text.charCodeAt(index);
return index + (code >= 0xd800 && code <= 0xdbff ? 2 : 1);
}
function previousCodePointStart(text: string, index: number): number {
if (index <= 0) return 0;
const code = text.charCodeAt(index - 1);
return code >= 0xdc00 && code <= 0xdfff ? index - 2 : index - 1;
}
function advanceCodePoints(text: string, start: number, count: number): number {
let end = start;
for (let i = 0; i < count && end < text.length; i += 1) {
end = nextCodePointEnd(text, end);
}
return end;
}
/**
* Pick the next human-readable reveal boundary.
*
* CJK text advances in compact 2–4 character phrases. Latin text waits briefly
* for a whole-word boundary instead of crawling through a word letter by letter.
*/
export function nextTypewriterReveal(
text: string,
start: number,
availableCharacters: number,
): number {
if (start >= text.length) return text.length;
const firstEnd = nextCodePointEnd(text, start);
const firstCharacter = text.slice(start, firstEnd);
const isCjk = CJK_RE.test(firstCharacter);
const isWordCharacter = WORD_CHARACTER_RE.test(firstCharacter);
const minimumGroup = isCjk ? 2 : 1;
const maximumGroup = isCjk ? 4 : 14;
const available = Math.max(0, Math.floor(availableCharacters));
if (available < minimumGroup && text.length - start > available) return start;
const hardEnd = advanceCodePoints(text, start, Math.min(maximumGroup, Math.max(minimumGroup, available)));
const minimumEnd = advanceCodePoints(text, start, minimumGroup);
// Prefer the latest complete phrase/word already covered by the reveal budget.
let cursor = hardEnd;
while (cursor >= minimumEnd) {
const characterStart = previousCodePointStart(text, cursor);
if (NATURAL_BREAK_RE.test(text.slice(characterStart, cursor))) return cursor;
cursor = characterStart;
}
// CJK has no spaces between words; reveal small even groups. For Latin text,
// wait for the word to finish unless it has grown long enough to need a split.
if (isCjk || !isWordCharacter || hardEnd === text.length || available >= maximumGroup) return hardEnd;
return start;
}
/**
* Smooth streamed text into an adaptive phrase cadence.
*
* Network chunks arrive in uneven bursts. The displayed slice follows them with
* short semantic groups, accelerates under backlog, and eases back near the live
* edge. This feels closer to a model composing text than a mechanical cursor.
*/
export function useTypewriter(
getTarget: () => string,
getComplete: () => boolean,
options: TypewriterOptions = {},
): { displayed: ComputedRef<string> } {
const minCps = options.minCps ?? 72;
const drainSeconds = options.drainSeconds ?? 0.42;
const maxCps = options.maxCps ?? 240;
const maxFrameSeconds = options.maxFrameSeconds ?? 0.05;
const typedLength = ref(0);
let revealCredit = 0;
let raf: number | null = null;
let lastTs = 0;
let initialized = false;
let reduceMotion = false;
let motionQuery: MediaQueryList | null = null;
if (typeof window !== 'undefined' && typeof window.matchMedia === 'function') {
motionQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
reduceMotion = motionQuery.matches;
}
const displayed = computed(() => {
const full = getTarget();
let n = Math.min(typedLength.value, full.length);
// Never cut on a high surrogate, which would render a broken glyph.
if (n > 0 && n < full.length) {
const code = full.charCodeAt(n - 1);
if (code >= 0xd800 && code <= 0xdbff) n -= 1;
}
return full.slice(0, n);
});
const stop = () => {
if (raf !== null) {
cancelAnimationFrame(raf);
raf = null;
}
lastTs = 0;
};
const tick = (ts: number) => {
const full = getTarget();
const target = full.length;
if (typedLength.value > target) {
typedLength.value = 0;
revealCredit = 0;
}
if (typedLength.value >= target) {
stop();
return;
}
const dt = lastTs ? Math.min((ts - lastTs) / 1000, maxFrameSeconds) : 0;
lastTs = ts;
const remaining = target - typedLength.value;
const cps = Math.min(maxCps, Math.max(minCps, remaining / drainSeconds));
revealCredit += cps * dt;
const next = nextTypewriterReveal(full, typedLength.value, revealCredit);
if (next > typedLength.value) {
revealCredit = Math.max(0, revealCredit - (next - typedLength.value));
typedLength.value = next;
}
raf = requestAnimationFrame(tick);
};
const ensure = () => {
if (raf === null) {
lastTs = 0;
raf = requestAnimationFrame(tick);
}
};
const handleMotionChange = (event: MediaQueryListEvent) => {
reduceMotion = event.matches;
if (reduceMotion) {
typedLength.value = getTarget().length;
revealCredit = 0;
stop();
}
};
motionQuery?.addEventListener('change', handleMotionChange);
watch(
getTarget,
(full) => {
const target = full.length;
if (!initialized) {
initialized = true;
if (getComplete() || reduceMotion) {
typedLength.value = target;
return;
}
}
if (typedLength.value > target) {
typedLength.value = 0;
revealCredit = 0;
}
if (reduceMotion) {
typedLength.value = target;
} else if (typedLength.value < target) {
ensure();
}
},
{ immediate: true },
);
onBeforeUnmount(() => {
stop();
motionQuery?.removeEventListener('change', handleMotionChange);
});
return { displayed };
}