diff --git a/frontend/src/components/css/chat-markdown.less b/frontend/src/components/css/chat-markdown.less index 20843e0a..49b889b8 100644 --- a/frontend/src/components/css/chat-markdown.less +++ b/frontend/src/components/css/chat-markdown.less @@ -574,6 +574,38 @@ transition: transform 0.2s ease; } + // Protected images not yet hydrated still carry the 1x1 placeholder gif. + // Without a fixed size width:auto/height:auto collapse it to a ~1px line that + // jumps to full size on load (reads as flicker during streaming). Give it a + // stable skeleton box so the later swap to the real image is a calm fade-in. + // Match both the explicit loading marker and the transparent GIF itself. + // During a streaming DOM patch the marker can briefly be absent while src is + // already the 1x1 placeholder; matching src prevents that frame becoming a + // tall, one-pixel vertical line in Chromium. + :deep(img[data-img-loading]), + :deep(img[data-protected-src][src^='data:image/gif;base64']) { + width: 260px; + height: 180px; + min-width: min(260px, 80%); + min-height: 180px; + max-width: 80%; + object-fit: cover; + cursor: default; + background: linear-gradient( + 100deg, + var(--td-bg-color-secondarycontainer, #f3f3f3) 30%, + var(--td-bg-color-container-hover, #eaeaea) 50%, + var(--td-bg-color-secondarycontainer, #f3f3f3) 70% + ); + background-size: 200% 100%; + animation: chat-img-skeleton 1.2s ease-in-out infinite; + } + + @keyframes chat-img-skeleton { + 0% { background-position: 100% 0; } + 100% { background-position: -100% 0; } + } + :deep(p:has(> img:only-child)) { margin: 1.25em 0; line-height: 0; diff --git a/frontend/src/composables/useEmbedChatSession.ts b/frontend/src/composables/useEmbedChatSession.ts index c5fe81d7..4734abc3 100644 --- a/frontend/src/composables/useEmbedChatSession.ts +++ b/frontend/src/composables/useEmbedChatSession.ts @@ -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 @@ -90,6 +91,8 @@ export function useEmbedChatSession(options: { scrollToBottom(true) } + useStickyBottomOnResize(scrollContainer, userHasScrolledUp, scrollToBottom) + const debounce = void>(fn: T, delay: number) => { let timer: ReturnType return (...args: Parameters) => { diff --git a/frontend/src/composables/useStickyBottomOnResize.ts b/frontend/src/composables/useStickyBottomOnResize.ts new file mode 100644 index 00000000..fed95b61 --- /dev/null +++ b/frontend/src/composables/useStickyBottomOnResize.ts @@ -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, + userHasScrolledUp: Ref, + 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 + } + }) +} diff --git a/frontend/src/composables/useTypewriter.test.ts b/frontend/src/composables/useTypewriter.test.ts new file mode 100644 index 00000000..26de9b93 --- /dev/null +++ b/frontend/src/composables/useTypewriter.test.ts @@ -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) +}) diff --git a/frontend/src/composables/useTypewriter.ts b/frontend/src/composables/useTypewriter.ts new file mode 100644 index 00000000..0905f226 --- /dev/null +++ b/frontend/src/composables/useTypewriter.ts @@ -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 } { + 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 }; +} diff --git a/frontend/src/directives/stableHtml.ts b/frontend/src/directives/stableHtml.ts new file mode 100644 index 00000000..303ee746 --- /dev/null +++ b/frontend/src/directives/stableHtml.ts @@ -0,0 +1,116 @@ +import type { ObjectDirective } from 'vue'; + +function imageIdentity(img: HTMLImageElement): string { + return (img.getAttribute('data-protected-src') || img.currentSrc || img.src || '').trim(); +} + +function canMorph(current: Node, next: Node): boolean { + if (current.nodeType !== next.nodeType) return false; + if (current.nodeType !== Node.ELEMENT_NODE) return true; + return (current as Element).tagName === (next as Element).tagName; +} + +function syncAttributes(current: Element, next: Element, preserved: Set = new Set()): void { + Array.from(current.attributes).forEach(({ name }) => { + if (!preserved.has(name) && !next.hasAttribute(name)) current.removeAttribute(name); + }); + Array.from(next.attributes).forEach(({ name, value }) => { + if (!preserved.has(name) && current.getAttribute(name) !== value) { + current.setAttribute(name, value); + } + }); +} + +function morphImage(current: HTMLImageElement, next: HTMLImageElement): void { + const sameProtectedSource = Boolean( + current.getAttribute('data-protected-src') + && current.getAttribute('data-protected-src') === next.getAttribute('data-protected-src'), + ); + const sameIdentity = imageIdentity(current) === imageIdentity(next); + const sameProtectedAlt = Boolean( + current.getAttribute('data-protected-src') + && next.getAttribute('data-protected-src') + && current.alt + && current.alt === next.alt, + ); + const keepDecodedImage = current.complete + && current.naturalWidth > 1 + && (sameProtectedSource || sameIdentity || sameProtectedAlt); + + if (keepDecodedImage) { + // Never write the placeholder src (or loading flags) over an image Chrome + // has already decoded. More importantly, leave this exact DOM node attached + // to the document so Chromium keeps its painted image layer. + syncAttributes(current, next, new Set([ + 'src', + 'data-protected-src', + 'data-img-loading', + 'data-auth-hydrated', + ])); + return; + } + + syncAttributes(current, next); +} + +function morphNode(current: Node, next: Node): void { + if (current.nodeType === Node.TEXT_NODE || current.nodeType === Node.COMMENT_NODE) { + if (current.nodeValue !== next.nodeValue) current.nodeValue = next.nodeValue; + return; + } + + const currentElement = current as Element; + const nextElement = next as Element; + if (currentElement instanceof HTMLImageElement && nextElement instanceof HTMLImageElement) { + morphImage(currentElement, nextElement); + return; + } + + syncAttributes(currentElement, nextElement); + morphChildren(currentElement, nextElement); +} + +function morphChildren(currentParent: ParentNode, nextParent: ParentNode): void { + const desiredChildren = Array.from(nextParent.childNodes); + let current = currentParent.firstChild; + + for (const desired of desiredChildren) { + if (current && canMorph(current, desired)) { + const following = current.nextSibling; + morphNode(current, desired); + current = following; + continue; + } + + // Insert only the new/mismatched subtree. Existing matching ancestors and + // their image descendants remain connected throughout streaming updates. + currentParent.insertBefore(desired.cloneNode(true), current); + } + + while (current) { + const following = current.nextSibling; + currentParent.removeChild(current); + current = following; + } +} + +/** + * Patch sanitized streaming HTML in place. + * + * Replacing innerHTML — or moving decoded images through a detached template — + * makes Chromium briefly drop the image's painted layer on every typewriter + * frame. This directive morphs text and surrounding markup without detaching a + * matching loaded node. + */ +export const vStableHtml: ObjectDirective = { + beforeMount(el, binding) { + el.innerHTML = binding.value || ''; + }, + updated(el, binding) { + const html = binding.value || ''; + if (html === binding.oldValue) return; + const template = document.createElement('template'); + template.innerHTML = html; + morphChildren(el, template.content); + }, +}; diff --git a/frontend/src/utils/chatMarkdownRenderer.test.ts b/frontend/src/utils/chatMarkdownRenderer.test.ts index 1594b55a..114c7a26 100644 --- a/frontend/src/utils/chatMarkdownRenderer.test.ts +++ b/frontend/src/utils/chatMarkdownRenderer.test.ts @@ -6,8 +6,14 @@ import { preprocessMathDelimiters, renderChatMarkdown, replaceIncompleteImageWithPlaceholder, + stripTrailingStreamingHorizontalRule, } from './chatMarkdownRenderer.ts' -import { resolveCitationChunkId, joinCitationTagsToPreviousLine, collapseStandaloneCitationParagraphs } from './citationMarkdown.ts' +import { + collapseStandaloneCitationParagraphs, + joinCitationTagsToPreviousLine, + resolveCitationChunkId, + stripIncompleteCitationTag, +} from './citationMarkdown.ts' const SAMPLE_DOC = 'example-report.docx' const SAMPLE_CHUNK_A = '00000001-0000-4000-8000-000000000001' @@ -29,6 +35,35 @@ test('replaceIncompleteImageWithPlaceholder hides an unfinished streaming image' ) }) +test('stripIncompleteCitationTag hides only an unfinished streaming citation tail', () => { + const prefix = 'Source ' + const complete = '' + + for (const partial of ['<', ' { + for (const rule of ['---', '* * *', '___']) { + assert.equal(stripTrailingStreamingHorizontalRule(`- item\n\n${rule}`), '- item\n\n') + } + assert.equal(stripTrailingStreamingHorizontalRule('- item\n\n---\nnext'), '- item\n\n---\nnext') + + const renderer = createChatMarkdownRenderer() + const options = { + renderer, + escapeMarkdown: (text: string) => text, + sanitizeHtml: (html: string) => html, + } + const source = '- item\n\n---' + assert.doesNotMatch(renderChatMarkdown(source, { ...options, streaming: true }), /
/) + assert.match(renderChatMarkdown(source, { ...options, streaming: false }), /
/) +}) + test('renderChatMarkdown preserves citations, math, and sanitized output through one shared pipeline', () => { const renderer = createChatMarkdownRenderer({ imageRenderer: ({ href, title, text }) => diff --git a/frontend/src/utils/chatMarkdownRenderer.ts b/frontend/src/utils/chatMarkdownRenderer.ts index 44e020f6..0a208b26 100644 --- a/frontend/src/utils/chatMarkdownRenderer.ts +++ b/frontend/src/utils/chatMarkdownRenderer.ts @@ -9,6 +9,7 @@ import { preserveCitationTags, restoreCitationHtmlPlaceholders, restoreCitationTags, + stripIncompleteCitationTag, type CitationKnowledgeRef, } from './citationMarkdown.ts' @@ -34,6 +35,8 @@ export type RenderChatMarkdownOptions = { renderer: Renderer escapeMarkdown: (markdown: string) => string sanitizeHtml: (html: string) => string + /** The source is still growing and may end in ambiguous partial Markdown. */ + streaming?: boolean collapseStandaloneCitations?: boolean knowledgeReferences?: CitationKnowledgeRef[] | null cachedMermaidSvgHtml?: string | null @@ -75,6 +78,22 @@ export function replaceIncompleteImageWithPlaceholder(content: string): string { return content } +/** + * Hide a trailing Markdown horizontal-rule candidate while content is streaming. + * + * A model often emits `---` as the beginning of a table delimiter or another + * structure. At that exact typewriter frame, marked renders it as `
`, then + * removes it when more characters arrive. A real completed horizontal rule is + * still rendered because this guard is enabled only for an active stream. + */ +export function stripTrailingStreamingHorizontalRule(content: string): string { + if (!content) return content + return content.replace( + /(^|\n)[ \t]{0,3}(?:(?:-[ \t]*){3,}|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,})$/, + '$1', + ) +} + export function createChatMarkdownRenderer(options: ChatMarkdownRendererOptions = {}): Renderer { const renderer = new marked.Renderer() @@ -113,7 +132,11 @@ export function renderChatMarkdown(rawMarkdown: unknown, options: RenderChatMark configureMarkedForChatMarkdown() - const { text: tagSafe, tags } = preserveCitationTags(rawText) + const streamingSafeText = options.streaming + ? stripTrailingStreamingHorizontalRule(rawText) + : rawText + const citationSafeText = stripIncompleteCitationTag(streamingSafeText) + const { text: tagSafe, tags } = preserveCitationTags(citationSafeText) const imageSafe = replaceIncompleteImageWithPlaceholder(tagSafe) const mathSafe = preprocessMathDelimiters(imageSafe) const restoredTags = restoreCitationTags(mathSafe, tags) diff --git a/frontend/src/utils/citationMarkdown.ts b/frontend/src/utils/citationMarkdown.ts index 203ef53b..9703ea12 100644 --- a/frontend/src/utils/citationMarkdown.ts +++ b/frontend/src/utils/citationMarkdown.ts @@ -8,6 +8,29 @@ const WEB_TAG_ATTR_RE = /]*?)\s*\/?>/g const ATTRIBUTE_REGEX = /([\w-]+)\s*=\s*"([^"]*)"/g const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i +/** + * Hide a citation tag while the typewriter has only emitted part of it. + * + * Without this guard, Markdown renders the leading `<` as ordinary text until + * the closing `>` arrives. Only the unfinished tail is removed; a complete tag + * continues through the normal citation pipeline. + */ +export function stripIncompleteCitationTag(content: string): string { + if (!content) return content + + const start = content.lastIndexOf('<') + if (start < 0) return content + + const tail = content.slice(start) + if (tail.includes('>')) return content + + const isCitationPrefix = tail === '<' + || /^ { const restPath = decodeProviderURL(restPathRaw); const protectedSrc = `${provider}://${restPath}`; - return ``; + // A definitive 404 should not leave a skeleton behind. Streaming + // re-renders call this function repeatedly, so remember the missing + // source until the explicit end-of-stream retry clears the cache. + if (protectedFileMissingSources.has(protectedSrc)) { + return ''; + } + // Reuse the already-hydrated blob if we have one, so repeated re-renders + // (typewriter streaming) keep the same stable image instead of flashing + // back to the placeholder every frame. + const cachedBlobURL = protectedFileBlobBySource.get(protectedSrc); + if (cachedBlobURL) { + return ``; + } + // Not hydrated yet: render the 1x1 placeholder but tag it so CSS can give + // it a stable skeleton box. Otherwise width:auto/height:auto collapse the + // 1x1 gif to a ~1px line that violently jumps to full size once loaded. + return ``; }, ); } @@ -288,15 +304,54 @@ export function createSafeImage(src: string, alt: string = '', title: string = ' return `${safeAlt}`; } -const protectedFileBlobCache = new Map(); +type ProtectedFileLoadResult = + | { status: 'loaded'; blobURL: string } + | { status: 'missing' } + | { status: 'failed' }; + +type ProtectedFileCacheState = { + blobByRequest: Map; + blobBySource: Map; + missingSources: Set; + failures: Map; + inflight: Map>; +}; + +// Keep object URLs alive across Vite hot updates. A hot update replaces this +// module but not the page document, so module-local Maps would forget valid +// blob URLs and make already-loaded images fall back to the 1x1 placeholder. +const protectedFileCacheState = (() => { + const fresh = (): ProtectedFileCacheState => ({ + blobByRequest: new Map(), + blobBySource: new Map(), + missingSources: new Set(), + failures: new Map(), + inflight: new Map(), + }); + if (typeof window === 'undefined') return fresh(); + const scope = window as typeof window & { + __weknoraProtectedFileCacheV1__?: ProtectedFileCacheState; + }; + scope.__weknoraProtectedFileCacheV1__ ||= fresh(); + return scope.__weknoraProtectedFileCacheV1__; +})(); + +const protectedFileBlobCache = protectedFileCacheState.blobByRequest; +// Blob URL keyed by the protected source URL (e.g. `local://...`). Once an image +// has been hydrated, re-renders of the same markdown can emit the blob src +// directly instead of the placeholder. Without this, the typewriter re-renders +// the answer every frame, recreating each as a placeholder that hydration +// only restores a microtask later — which reads as a per-frame flicker. +const protectedFileBlobBySource = protectedFileCacheState.blobBySource; +const protectedFileMissingSources = protectedFileCacheState.missingSources; // Throttle retries of failed file fetches. During streaming the same markdown // is re-rendered on every chunk, producing brand-new elements (so the // per-element `authHydrated` flag is reset each time). Without throttling a // not-yet-generated file (404) would be re-requested on every chunk. We record // the last failure time per URL and skip re-fetching within a cooldown window, // while still allowing a later attempt once the file becomes available. -const protectedFileFailureCache = new Map(); -const protectedFileInflight = new Set(); +const protectedFileFailureCache = protectedFileCacheState.failures; +const protectedFileInflight = protectedFileCacheState.inflight; const PROTECTED_FILE_RETRY_COOLDOWN_MS = 5000; function getProtectedFileRequestHeaders(): Record { @@ -333,6 +388,43 @@ function getProtectedFileRequestHeaders(): Record { */ export function clearProtectedFileFailureCache(): void { protectedFileFailureCache.clear(); + protectedFileMissingSources.clear(); +} + +function protectedImageSource(img: HTMLImageElement): string { + return normalizeProtectedImageElement(img) + || (img.getAttribute('data-protected-src') || '').trim() + || (img.getAttribute('src') || '').trim(); +} + +function forEachProtectedImageWithSource( + root: ParentNode, + sourceURL: string, + callback: (img: HTMLImageElement) => void, +): void { + root.querySelectorAll('img[data-protected-src]').forEach((candidate) => { + if (protectedImageSource(candidate) === sourceURL) callback(candidate); + }); +} + +function removeMissingProtectedImages(root: ParentNode, sourceURL: string): void { + forEachProtectedImageWithSource(root, sourceURL, (img) => { + const parent = img.parentElement; + img.remove(); + // Markdown emits a dedicated paragraph for a standalone image. Remove that + // wrapper too so a missing image leaves no vertical placeholder/gap. + if (parent?.tagName === 'P' && !parent.textContent?.trim() && parent.children.length === 0) { + parent.remove(); + } + }); +} + +function applyHydratedProtectedImage(root: ParentNode, sourceURL: string, blobURL: string): void { + forEachProtectedImageWithSource(root, sourceURL, (img) => { + img.src = blobURL; + img.dataset.authHydrated = '1'; + img.removeAttribute('data-img-loading'); + }); } export async function hydrateProtectedFileImages( @@ -367,6 +459,10 @@ export async function hydrateProtectedFileImages( if (img.dataset.authHydrated === '1') { return; } + if (protectedFileMissingSources.has(sourceURL)) { + removeMissingProtectedImages(root, sourceURL); + return; + } img.dataset.authHydrated = '1'; const isProviderScheme = isProviderFileURL(sourceURL); @@ -388,46 +484,66 @@ export async function hydrateProtectedFileImages( const cachedBlobURL = protectedFileBlobCache.get(requestURL); if (cachedBlobURL) { - img.src = cachedBlobURL; + applyHydratedProtectedImage(root, sourceURL, cachedBlobURL); return; } - // Skip while a fetch for the same URL is already in flight, or while the - // last attempt failed recently. Allow a fresh attempt to fix the element. - if (protectedFileInflight.has(requestURL)) { - img.dataset.authHydrated = '0'; - return; - } const lastFailure = protectedFileFailureCache.get(requestURL); if (lastFailure !== undefined && Date.now() - lastFailure < PROTECTED_FILE_RETRY_COOLDOWN_MS) { img.dataset.authHydrated = '0'; return; } - protectedFileInflight.add(requestURL); - try { - const resp = await fetch(requestURL, { - method: 'GET', - headers, - credentials: 'include', - }); - if (!resp.ok) { - throw new Error(`HTTP ${resp.status}`); - } - const blob = await resp.blob(); - const blobURL = URL.createObjectURL(blob); - protectedFileBlobCache.set(requestURL, blobURL); - protectedFileFailureCache.delete(requestURL); - img.src = blobURL; - if (protectedSrc) { - img.removeAttribute('data-protected-src'); - } - } catch (error) { - console.warn('[security] hydrateProtectedFileImages failed:', error); - protectedFileFailureCache.set(requestURL, Date.now()); + // Every component that references the same image awaits the shared task. + // The previous Set-based de-dupe made later components return immediately; + // only the component that started the fetch was updated, leaving all other + // occurrences stuck on the transparent placeholder forever. + let loadTask = protectedFileInflight.get(requestURL); + if (!loadTask) { + loadTask = (async (): Promise => { + try { + const resp = await fetch(requestURL, { + method: 'GET', + headers, + credentials: 'include', + }); + if (!resp.ok) { + if (resp.status === 404) { + protectedFileFailureCache.set(requestURL, Date.now()); + return { status: 'missing' }; + } + throw new Error(`HTTP ${resp.status}`); + } + const blob = await resp.blob(); + const blobURL = URL.createObjectURL(blob); + protectedFileBlobCache.set(requestURL, blobURL); + protectedFileFailureCache.delete(requestURL); + return { status: 'loaded', blobURL }; + } catch (error) { + console.warn('[security] hydrateProtectedFileImages failed:', error); + protectedFileFailureCache.set(requestURL, Date.now()); + return { status: 'failed' }; + } finally { + protectedFileInflight.delete(requestURL); + } + })(); + protectedFileInflight.set(requestURL, loadTask); + } + + const result = await loadTask; + if (result.status === 'loaded') { + protectedFileBlobBySource.set(sourceURL, result.blobURL); + protectedFileMissingSources.delete(sourceURL); + applyHydratedProtectedImage(root, sourceURL, result.blobURL); + return; + } + if (result.status === 'missing') { + protectedFileMissingSources.add(sourceURL); + removeMissingProtectedImages(root, sourceURL); + return; + } + if (result.status === 'failed') { img.dataset.authHydrated = '0'; - } finally { - protectedFileInflight.delete(requestURL); } })); } diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue index 5aa44697..b50e6b67 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.vue +++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue @@ -260,7 +260,7 @@
-
+
{ return typeof active?.content === 'string' ? active.content : ''; }); +// The answer event whose text is currently streaming. The template renders the +// smoothed typewriter text for this event and the raw content for any others. +const activeAnswerEventRef = computed(() => { + const stream = eventStream.value; + if (!stream?.length) return null; + const answers = stream.filter((e: any) => e.type === 'answer' && !e.superseded); + return answers.find((e: any) => !e.done) ?? answers[answers.length - 1] ?? null; +}); + +// Smooth the streamed answer into a steady typewriter cadence (shared with the +// non-Agent markdown path). History reloads arrive already complete and snap to +// full instead of replaying. +const { displayed: typedAnswer } = useTypewriter( + () => activeAnswerMarkdown.value, + () => isConversationDone.value, +); + const cacheStreamingMermaidSvg = async () => { if (streamingMermaidSvgCache.value) return; const code = extractFirstMermaidCode(activeAnswerMarkdown.value); @@ -921,10 +940,21 @@ watch(activeAnswerMarkdown, () => { // Files referenced mid-stream (e.g. exported images) may only become available // at completion; throttling stops the chunk-by-chunk 404 spam during streaming, // and this final pass guarantees they load without waiting out the cooldown. -watch(isConversationDone, (done) => { - if (!done) return; +// +// Gate this on the typewriter having fully revealed the answer: when done flips, +// the smoothed text may still be catching up, so the tag is not in the DOM +// yet. Hydrating too early would find nothing and leave a permanent placeholder +// (until a manual reload). Waiting for full reveal guarantees the image exists. +const answerFullyRendered = computed( + () => isConversationDone.value && typedAnswer.value.length >= activeAnswerMarkdown.value.length, +); +watch(answerFullyRendered, (ready) => { + if (!ready) return; + // Clear before this reactive update renders, so a source that returned 404 + // mid-stream gets one real final-attempt node instead of remaining + // suppressed by the missing-source cache. + clearProtectedFileFailureCache(); nextTick(async () => { - clearProtectedFileFailureCache(); await hydrateProtectedFileImages(rootElement.value); }); }); @@ -1649,8 +1679,15 @@ onBeforeUnmount(() => { }); onUpdated(() => { - nextTick(() => { + nextTick(async () => { rebindCitations(); + // Hydrate protected-file images (e.g. local:// exports) as soon as the + // typewriter reveals their into the DOM, so they show in real time + // mid-stream instead of waiting for the turn to finish. Hydration is cheap + // and idempotent: blob results are cached per URL, in-flight fetches are + // de-duped, and failures back off for a cooldown — so a not-yet-ready file + // simply retries later (and the answerFullyRendered pass is the backstop). + await hydrateProtectedFileImages(rootElement.value); }); }); @@ -1676,6 +1713,7 @@ const renderAgentMarkdown = ( renderer: agentRenderer, escapeMarkdown, sanitizeHtml: sanitizeMarkdownHTML, + streaming: !isConversationDone.value, knowledgeReferences: props.session?.knowledge_references, cachedMermaidSvgHtml: streamingMermaidSvgCache.value, prepareMarkdown: prepareAgentMarkdown, @@ -2316,8 +2354,6 @@ const handleAddToKnowledge = (answerEvent: any) => { .chat-citation-pills(); :deep(img) { - min-height: 100px; - /* 防止流式输出时图片高度塌陷导致抖动 */ background-color: var(--td-bg-color-secondarycontainer); /* 加载时的占位背景色 */ } diff --git a/frontend/src/views/chat/components/botmsg.vue b/frontend/src/views/chat/components/botmsg.vue index 4db6c934..3f713251 100644 --- a/frontend/src/views/chat/components/botmsg.vue +++ b/frontend/src/views/chat/components/botmsg.vue @@ -31,7 +31,7 @@
-
+
@@ -101,6 +101,8 @@ import { } from '@/utils/mermaidShared'; import { refreshMarkdownEnhancements } from '@/utils/markdownEnhancements'; import { useChatCitationPopover } from '@/composables/useChatCitationPopover'; +import { useTypewriter } from '@/composables/useTypewriter'; +import { vStableHtml } from '@/directives/stableHtml'; ensureMermaidInitialized(); @@ -170,14 +172,26 @@ const mentionedItems = computed(() => { return props.session?.mentioned_items || []; }); +// Smooth the streamed answer into a steady typewriter cadence (shared with the +// Agent path). Copy/toolbar still read the full content; only display is paced. +const answerText = computed(() => { + const text = props.content || props.session?.content || ''; + return typeof text === 'string' ? text : ''; +}); +const { displayed: typedAnswer } = useTypewriter( + () => answerText.value, + () => Boolean(props.session?.is_completed), +); + // 单次渲染整个 Markdown 内容(替代 token-by-token,修复 KaTeX 公式在 streaming 时闪烁消失的问题) const renderedHTML = computed(() => { - const text = props.content || props.session?.content || ''; + const text = typedAnswer.value; if (!text || typeof text !== 'string') return ''; return renderChatMarkdown(text, { renderer: markdownRenderer, escapeMarkdown: safeMarkdownToHTML, sanitizeHtml: sanitizeMarkdownHTML, + streaming: !props.session?.is_completed, knowledgeReferences: props.session?.knowledge_references, }); }); diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index 73f7d8c3..21653a20 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -126,6 +126,7 @@ import { useUIStore } from '@/stores/ui'; import KnowledgeBaseEditorModal from '@/views/knowledge/KnowledgeBaseEditorModal.vue'; import { useKnowledgeBaseCreationNavigation } from '@/hooks/useKnowledgeBaseCreationNavigation'; import { useChatStreamHandler } from '@/composables/useChatStreamHandler'; +import { useStickyBottomOnResize } from '@/composables/useStickyBottomOnResize'; import { clearCitationChunkCache } from '@/utils/citationChunkCache'; const props = defineProps({ @@ -392,6 +393,12 @@ const onClickScrollToBottom = () => { userHasScrolledUp.value = false; scrollToBottom(true); } + +// Images and other rich Markdown content can grow after the SSE chunk that +// introduced them. Follow those delayed height changes while the user remains +// at the live edge; preserve position when they intentionally scroll upward. +useStickyBottomOnResize(scrollContainer, userHasScrolledUp, scrollToBottom); + const debounce = (fn, delay) => { let timer return (...args) => { diff --git a/frontend/src/views/embed/EmbedBotMessage.vue b/frontend/src/views/embed/EmbedBotMessage.vue index e219fe1f..9e7b0543 100644 --- a/frontend/src/views/embed/EmbedBotMessage.vue +++ b/frontend/src/views/embed/EmbedBotMessage.vue @@ -13,7 +13,7 @@
-
+
@@ -63,6 +63,8 @@ import { enhanceMarkdownContainer, } from '@/utils/mermaidShared' import { useEmbedCitationPopover } from '@/composables/useEmbedCitationPopover' +import { useTypewriter } from '@/composables/useTypewriter' +import { vStableHtml } from '@/directives/stableHtml' const RagPipelineProgress = defineAsyncComponent( () => import('@/views/chat/components/RagPipelineProgress.vue'), @@ -142,13 +144,22 @@ const scheduleCitationClose = () => { }, 120) } +// Smooth the streamed answer into a steady typewriter cadence (shared with the +// Agent path). History reloads arrive complete and snap to full. +const answerText = computed(() => String(props.content || props.session?.content || '')) +const { displayed: typedAnswer } = useTypewriter( + () => answerText.value, + () => Boolean(props.session?.is_completed), +) + const renderedHTML = computed(() => { - const text = String(props.content || props.session?.content || '') + const text = typedAnswer.value if (!text.trim()) return '' return renderChatMarkdown(text, { renderer: markdownRenderer, escapeMarkdown: safeMarkdownToHTML, sanitizeHtml: sanitizeMarkdownHTML, + streaming: !props.session?.is_completed, }) })