perf(webui): bound reasoning previews and pace streaming updates

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-12 18:33:39 +08:00
1 parent 9e71087a2a
commit 7cede64f07
5 files changed
+167 -12

No files matched your search

+33 -9
View File
@@ -63,6 +63,8 @@ type PendingStreamEvent =
| { kind: "reasoning"; text: string; turn: UIMessageTurnFields };
const BACKGROUND_STREAM_FLUSH_INTERVAL_MS = 1_000;
// Markdown and layout work must leave room for input between visible updates.
const VISIBLE_STREAM_FLUSH_INTERVAL_MS = 50;
/**
* Append a reasoning chunk to the last open reasoning stream in ``prev``.
@@ -303,6 +305,7 @@ export function useNanobotStream(
const pendingStreamEventsRef = useRef<PendingStreamEvent[]>([]);
const streamFrameRef = useRef<number | null>(null);
const streamTimerRef = useRef<number | null>(null);
const lastStreamFlushRef = useRef(0);
const suppressStreamUntilTurnEndRef = useRef(false);
const sideChannelTurnIdsRef = useRef<Set<string>>(new Set());
@@ -328,6 +331,7 @@ export function useNanobotStream(
streamTimerRef.current = null;
}
pendingStreamEventsRef.current = [];
lastStreamFlushRef.current = 0;
}, []);
const isSideChannelEvent = useCallback((ev: InboundEvent) => {
@@ -580,6 +584,7 @@ export function useNanobotStream(
turn?: UIMessageTurnFields;
source?: UIMessage["source"];
}) => {
lastStreamFlushRef.current = 0;
if (streamFrameRef.current !== null) {
window.cancelAnimationFrame(streamFrameRef.current);
streamFrameRef.current = null;
@@ -659,7 +664,7 @@ export function useNanobotStream(
});
}, [applyPendingStreamEvents, closeActiveAssistantStream, resolveActiveAssistantIndex]);
const schedulePendingStreamFlush = useCallback(() => {
const schedulePendingStreamFlush = useCallback(function schedule() {
if (streamFrameRef.current !== null || streamTimerRef.current !== null) return;
if (document.visibilityState === "hidden" || !threadVisibleRef.current) {
streamTimerRef.current = window.setTimeout(() => {
@@ -671,11 +676,21 @@ export function useNanobotStream(
}, BACKGROUND_STREAM_FLUSH_INTERVAL_MS);
return;
}
const delay = VISIBLE_STREAM_FLUSH_INTERVAL_MS
- (performance.now() - lastStreamFlushRef.current);
if (delay > 0) {
streamTimerRef.current = window.setTimeout(() => {
streamTimerRef.current = null;
schedule();
}, delay);
return;
}
streamFrameRef.current = window.requestAnimationFrame(() => {
streamFrameRef.current = null;
const events = pendingStreamEventsRef.current;
if (events.length === 0) return;
pendingStreamEventsRef.current = [];
lastStreamFlushRef.current = performance.now();
setMessages((prev) => applyPendingStreamEvents(prev, events));
});
}, [applyPendingStreamEvents]);
@@ -683,22 +698,31 @@ export function useNanobotStream(
useEffect(() => {
if (threadVisible) {
flushPendingStreamEvents();
} else if (streamFrameRef.current !== null) {
window.cancelAnimationFrame(streamFrameRef.current);
} else if (streamFrameRef.current !== null || streamTimerRef.current !== null) {
if (streamFrameRef.current !== null) window.cancelAnimationFrame(streamFrameRef.current);
streamFrameRef.current = null;
if (streamTimerRef.current !== null) window.clearTimeout(streamTimerRef.current);
streamTimerRef.current = null;
schedulePendingStreamFlush();
}
}, [threadVisible, flushPendingStreamEvents, schedulePendingStreamFlush]);
useEffect(() => {
const flushOnReturn = () => {
if (document.visibilityState !== "visible" || !threadVisibleRef.current) return;
const onVisibilityChange = () => {
if (pendingStreamEventsRef.current.length === 0) return;
flushPendingStreamEvents();
if (document.visibilityState === "visible" && threadVisibleRef.current) {
flushPendingStreamEvents();
} else {
if (streamFrameRef.current !== null) window.cancelAnimationFrame(streamFrameRef.current);
streamFrameRef.current = null;
if (streamTimerRef.current !== null) window.clearTimeout(streamTimerRef.current);
streamTimerRef.current = null;
schedulePendingStreamFlush();
}
};
document.addEventListener("visibilitychange", flushOnReturn);
return () => document.removeEventListener("visibilitychange", flushOnReturn);
}, [flushPendingStreamEvents]);
document.addEventListener("visibilitychange", onVisibilityChange);
return () => document.removeEventListener("visibilitychange", onVisibilityChange);
}, [flushPendingStreamEvents, schedulePendingStreamFlush]);
useEffect(() => {
if (!chatId) return;