fix(webui): snapshot received messages before pane unmount
This commit is contained in:
1 parent
bfb5129e6f
commit
8eea1cb7a4
2 files changed
+54
-14
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { acceptsCompactionPhase } from "../../../packages/client-events/notifications";
|
||||
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useThreadVisibility } from "@/hooks/useThreadVisibility";
|
||||
|
||||
@@ -173,9 +173,15 @@ export function useNanobotStream(
|
||||
threadVisibleRef.current = threadVisible;
|
||||
const { t } = useTranslation();
|
||||
const initialRunStartedAt = chatId ? client.getRunStartedAt(chatId) : null;
|
||||
const [messages, setMessages] = useState<UIMessage[]>(initialMessages);
|
||||
const committedMessagesRef = useRef(messages);
|
||||
useLayoutEffect(() => { committedMessagesRef.current = messages; }, [messages]);
|
||||
const [messages, setRenderedMessages] = useState<UIMessage[]>(initialMessages);
|
||||
const messagesRef = useRef(messages);
|
||||
// Keep received state ahead of React's batched render: navigation can unmount
|
||||
// this hook after a stream flush but before the queued render commits.
|
||||
const setMessages = useCallback<React.Dispatch<React.SetStateAction<UIMessage[]>>>((update) => {
|
||||
const next = typeof update === "function" ? update(messagesRef.current) : update;
|
||||
messagesRef.current = next;
|
||||
setRenderedMessages(next);
|
||||
}, []);
|
||||
const [messageOwnerChatId, setMessageOwnerChatId] = useState(chatId);
|
||||
/** If history ends in unfinished agent activity, keep the loading spinner alive. */
|
||||
const initialStreaming = hasPendingAgentActivity(initialMessages);
|
||||
@@ -305,7 +311,7 @@ export function useNanobotStream(
|
||||
suppressUntilTurnEnd: false,
|
||||
};
|
||||
}
|
||||
}, [chatId, client]);
|
||||
}, [chatId, client, setMessages]);
|
||||
|
||||
useEffect(() => client.onError(applyStreamError), [applyStreamError, client]);
|
||||
|
||||
@@ -415,7 +421,7 @@ export function useNanobotStream(
|
||||
projectionRef.current = projection;
|
||||
return projection.messages;
|
||||
});
|
||||
}, [applyPendingStreamEvents, chatId, closeActiveAssistantStream]);
|
||||
}, [applyPendingStreamEvents, chatId, closeActiveAssistantStream, setMessages]);
|
||||
|
||||
const schedulePendingStreamFlush = useCallback(function schedule() {
|
||||
if (streamFrameRef.current !== null || streamTimerRef.current !== null) return;
|
||||
@@ -446,7 +452,7 @@ export function useNanobotStream(
|
||||
lastStreamFlushRef.current = performance.now();
|
||||
setMessages((prev) => applyPendingStreamEvents(prev, events));
|
||||
});
|
||||
}, [applyPendingStreamEvents]);
|
||||
}, [applyPendingStreamEvents, setMessages]);
|
||||
|
||||
useEffect(() => {
|
||||
if (threadVisible) {
|
||||
@@ -500,7 +506,7 @@ export function useNanobotStream(
|
||||
setRunStartedAt(null);
|
||||
setIsStreaming(false);
|
||||
});
|
||||
}, [chatId, client, clearActivitySegment, flushPendingStreamEvents]);
|
||||
}, [chatId, client, clearActivitySegment, flushPendingStreamEvents, setMessages]);
|
||||
|
||||
// Reset local state when switching chats. Do not reset on every
|
||||
// ``initialMessages`` update: a brand-new chat can receive an empty/404
|
||||
@@ -523,7 +529,7 @@ export function useNanobotStream(
|
||||
clearPendingStreamWork();
|
||||
sideChannelTurnIdsRef.current.clear();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [chatId, client, clearActivitySegment, clearPendingStreamWork]);
|
||||
}, [chatId, client, clearActivitySegment, clearPendingStreamWork, setMessages]);
|
||||
|
||||
useEffect(() => {
|
||||
if (hasPendingToolCalls) setIsStreaming(true);
|
||||
@@ -546,7 +552,7 @@ export function useNanobotStream(
|
||||
projectionRef.current = projection;
|
||||
return projection.messages;
|
||||
});
|
||||
}, []);
|
||||
}, [setMessages]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!chatId) return;
|
||||
@@ -891,8 +897,8 @@ export function useNanobotStream(
|
||||
// deltas synchronously while they still belong to this subscription.
|
||||
const pending = pendingStreamEventsRef.current;
|
||||
const snapshot = pending.length > 0
|
||||
? applyPendingStreamEvents(committedMessagesRef.current, pending)
|
||||
: committedMessagesRef.current;
|
||||
? applyPendingStreamEvents(messagesRef.current, pending)
|
||||
: messagesRef.current;
|
||||
onStreamDetach?.(snapshot);
|
||||
if (pending.length > 0) setMessages(snapshot);
|
||||
projectionRef.current = resetThreadProjectionCursor(projectionRef.current);
|
||||
@@ -913,6 +919,7 @@ export function useNanobotStream(
|
||||
onTurnEnd,
|
||||
onStreamDetach,
|
||||
schedulePendingStreamFlush,
|
||||
setMessages,
|
||||
t,
|
||||
]);
|
||||
|
||||
@@ -987,7 +994,7 @@ export function useNanobotStream(
|
||||
client.sendMessage(chatId, outboundContent, wireMedia, clientOptions);
|
||||
return { turnId, userMessageId, sideChannel };
|
||||
},
|
||||
[chatId, clearActivitySegment, client, flushPendingStreamEvents],
|
||||
[chatId, clearActivitySegment, client, flushPendingStreamEvents, setMessages],
|
||||
);
|
||||
|
||||
const stop = useCallback(() => {
|
||||
@@ -1003,7 +1010,7 @@ export function useNanobotStream(
|
||||
setRunStartedAt(null);
|
||||
client.finishRunLocally(chatId);
|
||||
client.sendMessage(chatId, "/stop");
|
||||
}, [chatId, clearActivitySegment, client, flushPendingStreamEvents]);
|
||||
}, [chatId, clearActivitySegment, client, flushPendingStreamEvents, setMessages]);
|
||||
|
||||
const reconcileTurnComplete = useCallback(() => {
|
||||
clearPendingStreamWork();
|
||||
|
||||
Reference in new issue
Block a user