mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
feat: queue chat messages during runtime connection (#11687)
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
This commit is contained in:
parent
95a44f4248
commit
73fe865c7e
@ -142,6 +142,7 @@ export function WsClientProvider({
|
||||
const { addEvent, clearEvents } = useEventStore();
|
||||
const queryClient = useQueryClient();
|
||||
const sioRef = React.useRef<Socket | null>(null);
|
||||
const pendingEventsRef = React.useRef<Record<string, unknown>[]>([]);
|
||||
const [webSocketStatus, setWebSocketStatus] =
|
||||
React.useState<V0_WebSocketStatus>("DISCONNECTED");
|
||||
const lastEventRef = React.useRef<Record<string, unknown> | null>(null);
|
||||
@ -151,17 +152,37 @@ export function WsClientProvider({
|
||||
const { data: conversation, refetch: refetchConversation } =
|
||||
useActiveConversation();
|
||||
|
||||
function send(event: Record<string, unknown>) {
|
||||
if (!sioRef.current) {
|
||||
EventLogger.error("WebSocket is not connected.");
|
||||
function flushPendingEvents(socket: Socket | null = sioRef.current) {
|
||||
if (!socket || pendingEventsRef.current.length === 0) {
|
||||
return;
|
||||
}
|
||||
sioRef.current.emit("oh_user_action", event);
|
||||
|
||||
pendingEventsRef.current.forEach((queuedEvent) => {
|
||||
socket.emit("oh_user_action", queuedEvent);
|
||||
});
|
||||
pendingEventsRef.current = [];
|
||||
}
|
||||
|
||||
function send(event: Record<string, unknown>) {
|
||||
const socket = sioRef.current;
|
||||
|
||||
if (!socket) {
|
||||
EventLogger.error("WebSocket is not connected, queuing message...");
|
||||
pendingEventsRef.current.push(event);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pendingEventsRef.current.length > 0) {
|
||||
flushPendingEvents(socket);
|
||||
}
|
||||
|
||||
socket.emit("oh_user_action", event);
|
||||
}
|
||||
|
||||
function handleConnect() {
|
||||
setWebSocketStatus("CONNECTED");
|
||||
removeErrorMessage();
|
||||
flushPendingEvents();
|
||||
}
|
||||
|
||||
function handleMessage(event: Record<string, unknown>) {
|
||||
@ -292,6 +313,7 @@ export function WsClientProvider({
|
||||
|
||||
clearEvents();
|
||||
setWebSocketStatus("CONNECTING");
|
||||
pendingEventsRef.current = [];
|
||||
}, [conversationId]);
|
||||
|
||||
React.useEffect(() => {
|
||||
@ -301,6 +323,12 @@ export function WsClientProvider({
|
||||
|
||||
// Clear error messages when conversation is intentionally stopped
|
||||
if (conversation && conversation.status === "STOPPED") {
|
||||
const existingSocket = sioRef.current;
|
||||
if (existingSocket) {
|
||||
existingSocket.disconnect();
|
||||
}
|
||||
sioRef.current = null;
|
||||
pendingEventsRef.current = [];
|
||||
removeErrorMessage();
|
||||
setWebSocketStatus("DISCONNECTED");
|
||||
return () => undefined; // conversation intentionally stopped
|
||||
@ -320,6 +348,10 @@ export function WsClientProvider({
|
||||
!conversation.runtime_status ||
|
||||
conversation.runtime_status === "STATUS$STOPPED"
|
||||
) {
|
||||
if (sioRef.current) {
|
||||
sioRef.current.disconnect();
|
||||
}
|
||||
sioRef.current = null;
|
||||
return () => undefined; // conversation not ready for WebSocket connection
|
||||
}
|
||||
|
||||
@ -368,6 +400,7 @@ export function WsClientProvider({
|
||||
sio.on("disconnect", handleDisconnect);
|
||||
|
||||
sioRef.current = sio;
|
||||
flushPendingEvents(sio);
|
||||
|
||||
return () => {
|
||||
sio.off("connect", handleConnect);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user