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:
chengyongru authored and GitHub committed 2026-09-09 14:02:26 +08:00
1 parent 1c451b5b5f
commit 1469d4811c
32 files changed
+1156 -361

No files matched your search

+60 -9
View File
@@ -1,6 +1,7 @@
import { acceptsCompactionPhase } from "../../../packages/client-events/notifications";
import { useCallback, useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { useThreadVisibility } from "@/hooks/useThreadVisibility";
import { useClient } from "@/providers/ClientProvider";
import { toMediaAttachment } from "@/lib/media";
@@ -49,6 +50,7 @@ import type {
interface StreamBuffer {
/** ID of the assistant message currently receiving deltas (cleared when its segment closes). */
messageId: string;
mergeReasoning?: boolean;
}
interface ActiveAssistantCursor {
@@ -274,6 +276,9 @@ export function useNanobotStream(
dismissStreamError: () => void;
} {
const { client } = useClient();
const threadVisible = useThreadVisibility();
const threadVisibleRef = useRef(threadVisible);
threadVisibleRef.current = threadVisible;
const { t } = useTranslation();
const initialRunStartedAt = chatId ? client.getRunStartedAt(chatId) : null;
const [messages, setMessages] = useState<UIMessage[]>(initialMessages);
@@ -509,7 +514,7 @@ export function useNanobotStream(
};
closedAssistantStreamIdsRef.current.delete(merged.id);
activeAssistantRef.current = { id: merged.id, index: targetIndex };
buffer.current = { messageId: merged.id };
if (buffer.current?.messageId !== merged.id) buffer.current = { messageId: merged.id };
return replaceMessageAt(next, targetIndex, merged);
},
[resolveActiveAssistantIndex],
@@ -518,26 +523,59 @@ export function useNanobotStream(
const applyPendingStreamEvents = useCallback(
(prev: UIMessage[], events: PendingStreamEvent[]): UIMessage[] => {
let next = prev;
for (const event of events) {
for (let index = 0; index < events.length; index++) {
const event = events[index];
const chunks = [event.text];
let turn = event.turn;
while (index + 1 < events.length) {
const nextEvent = events[index + 1];
if (nextEvent.kind !== event.kind
|| nextEvent.turn.turnId !== event.turn.turnId
|| nextEvent.turn.turnPhase !== event.turn.turnPhase
|| (nextEvent.kind === "delta" && event.kind === "delta" && nextEvent.source !== event.source)) break;
chunks.push(nextEvent.text);
turn = { ...turn, ...nextEvent.turn };
index++;
}
const text = chunks.join("");
if (event.kind === "delta") {
next = appendAnswerChunk(next, event.text, event.turn, event.source);
next = appendAnswerChunk(next, text, turn, event.source);
} else {
const continuationIndex = buffer.current?.mergeReasoning
? resolveActiveAssistantIndex(next, turn)
: null;
if (continuationIndex !== null) {
// Length continuation keeps one Markdown answer and its reasoning
// together. Ordinary reasoning still opens a new activity surface.
const target = next[continuationIndex];
const separator = target.reasoning && !target.reasoningStreaming ? "\n\n" : "";
next = replaceMessageAt(next, continuationIndex, {
...target,
reasoning: (target.reasoning ?? "") + separator + text,
reasoningStreaming: true,
});
continue;
}
if (closeActiveAssistantStream()) clearActivitySegment();
next = attachReasoningChunk(
next,
event.text,
text,
{ ensure: ensureActivitySegmentId },
event.turn,
turn,
);
}
}
return next;
},
[appendAnswerChunk, clearActivitySegment, closeActiveAssistantStream, ensureActivitySegmentId],
[
appendAnswerChunk, clearActivitySegment, closeActiveAssistantStream,
ensureActivitySegmentId, resolveActiveAssistantIndex,
],
);
const flushPendingStreamEvents = useCallback((options?: {
closeAnswerSegment?: boolean;
mergeReasoning?: boolean;
finalAnswerText?: string;
turn?: UIMessageTurnFields;
source?: UIMessage["source"];
@@ -554,7 +592,8 @@ export function useNanobotStream(
const finalAnswerText = options?.finalAnswerText;
const turn = options?.turn ?? {};
const source = options?.source;
if (events.length === 0 && finalAnswerText === undefined && source === undefined) {
if (events.length === 0 && finalAnswerText === undefined && source === undefined
&& !options?.mergeReasoning) {
if (options?.closeAnswerSegment) closeActiveAssistantStream();
return;
}
@@ -614,6 +653,7 @@ export function useNanobotStream(
});
}
}
if (options?.mergeReasoning && buffer.current) buffer.current.mergeReasoning = true;
if (options?.closeAnswerSegment) closeActiveAssistantStream();
return next;
});
@@ -621,7 +661,7 @@ export function useNanobotStream(
const schedulePendingStreamFlush = useCallback(() => {
if (streamFrameRef.current !== null || streamTimerRef.current !== null) return;
if (document.visibilityState === "hidden") {
if (document.visibilityState === "hidden" || !threadVisibleRef.current) {
streamTimerRef.current = window.setTimeout(() => {
streamTimerRef.current = null;
const events = pendingStreamEventsRef.current;
@@ -640,9 +680,19 @@ export function useNanobotStream(
});
}, [applyPendingStreamEvents]);
useEffect(() => {
if (threadVisible) {
flushPendingStreamEvents();
} else if (streamFrameRef.current !== null) {
window.cancelAnimationFrame(streamFrameRef.current);
streamFrameRef.current = null;
schedulePendingStreamFlush();
}
}, [threadVisible, flushPendingStreamEvents, schedulePendingStreamFlush]);
useEffect(() => {
const flushOnReturn = () => {
if (document.visibilityState !== "visible") return;
if (document.visibilityState !== "visible" || !threadVisibleRef.current) return;
if (pendingStreamEventsRef.current.length === 0) return;
flushPendingStreamEvents();
};
@@ -852,6 +902,7 @@ export function useNanobotStream(
const mergeNext = ev.resuming === true && ev.merge_next === true;
flushPendingStreamEvents({
closeAnswerSegment: !mergeNext,
mergeReasoning: mergeNext,
...(typeof ev.text === "string" ? { finalAnswerText: ev.text } : {}),
turn,
source: ev.source,