Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

FIX: Optimized Socket Log Handling and Throttle Updates (#665)

Tahier Hussaina1234382c0b9
diff --git a/frontend/src/components/helpers/socket-messages/SocketMessages.js b/frontend/src/components/helpers/socket-messages/SocketMessages.js
index 72bb2d3af..a877755c3 100644
--- a/frontend/src/components/helpers/socket-messages/SocketMessages.js
+++ b/frontend/src/components/helpers/socket-messages/SocketMessages.js
@@ -1,5 +1,5 @@
-import { useContext, useEffect, useState } from "react";
-
+import { useContext, useEffect, useRef, useState, useCallback } from "react";
+import throttle from "lodash/throttle";
import { SocketContext } from "../../../helpers/SocketContext";
import { useExceptionHandler } from "../../../hooks/useExceptionHandler";
import { useAlertStore } from "../../../store/alert-store";
@@ -9,6 +9,8 @@ import { useSocketCustomToolStore } from "../../../store/socket-custom-tool";
import { useSessionStore } from "../../../store/session-store";
import { useUsageStore } from "../../../store/usage-store";
+const THROTTLE_DELAY = 2000; // 2 seconds
+
function SocketMessages() {
const [logId, setLogId] = useState("");
const {
@@ -26,10 +28,55 @@ function SocketMessages() {
const handleException = useExceptionHandler();
const { setLLMTokenUsage } = useUsageStore();
+ // Buffer to hold the logs between throttle intervals
+ const psLogs = useRef([]);
+ const wfLogs = useRef([]);
+
useEffect(() => {
setLogId(sessionDetails?.logEventsId || "");
}, [sessionDetails]);
+ // Throttled function for PS logs
+ const psLogsThrottledUpdate = useRef(
+ throttle((psLogMessages) => {
+ updateCusToolMessages(psLogMessages);
+ psLogs.current = [];
+ }, THROTTLE_DELAY)
+ ).current;
+
+ // Throttled function for WF logs
+ const wfLogsThrottledUpdate = useRef(
+ throttle((wfLogMessages) => {
+ pushLogMessages(wfLogMessages);
+ wfLogs.current = [];
+ }, THROTTLE_DELAY)
+ ).current;
+
+ // Clean up throttling functions on unmount
+ useEffect(() => {
+ return () => {
+ psLogsThrottledUpdate.cancel();
+ wfLogsThrottledUpdate.cancel();
+ };
+ }, [psLogsThrottledUpdate, wfLogsThrottledUpdate]);
+
+ const handlePsLogs = useCallback(
+ (msg) => {
+ psLogs.current = [...psLogs.current, msg];
+ psLogsThrottledUpdate(psLogs.current);
+ },
+ [psLogsThrottledUpdate]
+ );
+
+ const handleWfLogs = useCallback(
+ (msg) => {
+ wfLogs.current = [...wfLogs.current, msg];
+ wfLogsThrottledUpdate(wfLogs.current);
+ },
+ [wfLogsThrottledUpdate]
+ );
+
+ // Handle incoming socket messages
const onMessage = (data) => {
try {
let msg = data.data;
@@ -47,15 +94,12 @@ function SocketMessages() {
msg?.service !== "prompt"
) {
msg.message = msg?.log;
- pushLogMessages(msg);
- }
- if (msg?.type === "UPDATE") {
+ handleWfLogs(msg);
+ } else if (msg?.type === "UPDATE") {
pushStagedMessage(msg);
- }
- if (msg?.type === "LOG" && msg?.service === "prompt") {
- updateCusToolMessages(msg);
- }
- if (msg?.type === "LOG" && msg?.service === "usage") {
+ } else if (msg?.type === "LOG" && msg?.service === "prompt") {
+ handlePsLogs(msg);
+ } else if (msg?.type === "LOG" && msg?.service === "usage") {
const remainingTokens =
msg?.max_token_count_set - msg?.added_token_count;
setLLMTokenUsage(Math.max(remainingTokens, 0));
@@ -66,9 +110,8 @@ function SocketMessages() {
};
useEffect(() => {
- if (!logId) {
- return;
- }
+ if (!logId) return;
+
const logMessageChannel = `logs:${logId}`;
socket.on(logMessageChannel, onMessage);
@@ -79,15 +122,15 @@ function SocketMessages() {
}, [logId]);
useEffect(() => {
- if (pointer > stagedMessages?.length - 1) {
- return;
- }
+ if (pointer > stagedMessages?.length - 1) return;
const stagedMsg = stagedMessages[pointer];
- setTimeout(() => {
+ const timer = setTimeout(() => {
updateMessage(stagedMsg);
setPointer(pointer + 1);
}, 0);
+
+ return () => clearTimeout(timer); // Cleanup timer on unmount
}, [stagedMessages, pointer]);
}
diff --git a/frontend/src/store/socket-custom-tool.js b/frontend/src/store/socket-custom-tool.js
index db276ebe0..d4f221ea6 100644
--- a/frontend/src/store/socket-custom-tool.js
+++ b/frontend/src/store/socket-custom-tool.js
@@ -8,10 +8,9 @@ const STORE_VARIABLES = defaultState;
const useSocketCustomToolStore = create((setState, getState) => ({
...STORE_VARIABLES,
- updateCusToolMessages: (message) => {
+ updateCusToolMessages: (messages) => {
const existingState = { ...getState() };
- let data = [...(existingState?.messages || [])];
- data.push(message);
+ let data = [...(existingState?.messages || []), ...messages];
// Remove the previous messages if the length exceeds 200
const dataLength = data?.length;
diff --git a/frontend/src/store/socket-logs-store.js b/frontend/src/store/socket-logs-store.js
index 7ef8f8595..1b78389a2 100644
--- a/frontend/src/store/socket-logs-store.js
+++ b/frontend/src/store/socket-logs-store.js
@@ -8,13 +8,13 @@ const STORE_VARIABLES = {
const useSocketLogsStore = create((setState, getState) => ({
...STORE_VARIABLES,
- pushLogMessages: (msg) => {
+ pushLogMessages: (messages) => {
const existingState = { ...getState() };
let logsData = [...(existingState?.logs || [])];
- const newLog = {
+ const newLogs = messages.map((msg, index) => ({
timestamp: getTimeForLogs(),
- key: logsData?.length + 1,
+ key: logsData?.length + index + 1,
level: msg?.level,
stage: msg?.stage,
step: msg?.step,
@@ -24,9 +24,9 @@ const useSocketLogsStore = create((setState, getState) => ({
cost_value: msg?.cost,
iteration: msg?.iteration,
iteration_total: msg?.iteration_total,
- };
+ }));
- logsData.push(newLog);
+ logsData = [...logsData, ...newLogs];
// Remove the previous logs if the length exceeds 200
const logsDataLength = logsData?.length;