perf(webui): reduce repeated work and bound history rendering (#5703)
* perf(webui): reduce repeated work and bound history rendering * fix(webui): keep continuation reasoning with its answer * refactor(webui): reuse answer buffers and prepared copy text * perf(webui): restore conditional replay buffer initialization * fix(webui): remove code block pagination
This commit is contained in:
1 parent
1c451b5b5f
commit
1469d4811c
32 files changed
+1156
-361
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { memo, useCallback, useMemo, useRef } from "react";
|
||||
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { MessageBubble } from "@/components/MessageBubble";
|
||||
import { AgentActivityCluster } from "@/components/thread/AgentActivityCluster";
|
||||
@@ -281,24 +281,41 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
|
||||
onOpenFilePreview,
|
||||
onForkFromMessage,
|
||||
}: ThreadDisplayUnitProps) {
|
||||
// Introducing content-visibility after a unit has painted can move the
|
||||
// browser's scroll anchor. Only units deferred on their first render may
|
||||
// remain deferred.
|
||||
const hasRenderedEagerlyRef = useRef(!deferOffscreenRender);
|
||||
if (!deferOffscreenRender) hasRenderedEagerlyRef.current = true;
|
||||
const stableDeferOffscreenRender =
|
||||
deferOffscreenRender && !hasRenderedEagerlyRef.current;
|
||||
const elementRef = useRef<HTMLDivElement>(null);
|
||||
const heightRef = useRef(0);
|
||||
const [nearViewport, setNearViewport] = useState(true);
|
||||
const [interacted, setInteracted] = useState(false);
|
||||
const retainContent = !deferOffscreenRender || interacted || nearViewport;
|
||||
useEffect(() => {
|
||||
const element = elementRef.current;
|
||||
if (!element || !deferOffscreenRender || interacted || typeof IntersectionObserver === "undefined") return;
|
||||
const observer = new IntersectionObserver(([entry]) => {
|
||||
if (!entry) return;
|
||||
if (!entry.isIntersecting) {
|
||||
const height = element.getBoundingClientRect().height;
|
||||
if (height <= 0) return;
|
||||
heightRef.current = height;
|
||||
}
|
||||
setNearViewport(entry.isIntersecting);
|
||||
}, { rootMargin: "1000px 0px" });
|
||||
observer.observe(element);
|
||||
return () => observer.disconnect();
|
||||
}, [deferOffscreenRender, interacted]);
|
||||
const onForkFromHere = useCallback(() => {
|
||||
if (forkIndex !== undefined) onForkFromMessage?.(forkIndex);
|
||||
}, [forkIndex, onForkFromMessage]);
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
className={`${marginTop}${stableDeferOffscreenRender ? " thread-render-unit" : ""}`}
|
||||
ref={elementRef}
|
||||
className={marginTop}
|
||||
style={retainContent ? undefined : { height: heightRef.current }}
|
||||
onPointerDownCapture={() => setInteracted(true)}
|
||||
onFocusCapture={() => setInteracted(true)}
|
||||
data-thread-display-unit={unitKey}
|
||||
data-user-prompt-id={userPromptId}
|
||||
>
|
||||
{unit.type === "activity" ? (
|
||||
{retainContent ? unit.type === "activity" ? (
|
||||
<AgentActivityCluster
|
||||
messages={unit.messages}
|
||||
isTurnStreaming={isTurnStreaming}
|
||||
@@ -321,7 +338,7 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
onForkFromHere={forkIndex !== undefined ? onForkFromHere : undefined}
|
||||
/>
|
||||
)}
|
||||
) : null}
|
||||
</div>
|
||||
{showForkBoundary ? <ForkBoundaryDivider label={forkBoundaryLabel} /> : null}
|
||||
</>
|
||||
|
||||
Reference in new issue
Block a user