From fe99905a2b107621aa8449df1c4f65a7a76d573d Mon Sep 17 00:00:00 2001 From: balibabu Date: Wed, 28 Jan 2026 12:04:30 +0800 Subject: [PATCH] Refactor: Remove the brute-force deduplication method for agent logs. (#12864) ### What problem does this PR solve? Refactor: Remove the brute-force deduplication method for agent logs. ### Type of change - [x] Refactoring --- web/src/locales/en.ts | 2 +- web/src/pages/agent/canvas/index.tsx | 8 ++-- .../pages/agent/hooks/use-cache-chat-log.ts | 39 ++++++++++--------- .../agent/log-sheet/workflow-timeline.tsx | 4 +- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/web/src/locales/en.ts b/web/src/locales/en.ts index 4573659d2..732a5a5c6 100644 --- a/web/src/locales/en.ts +++ b/web/src/locales/en.ts @@ -2235,7 +2235,7 @@ Important structured information may include: names, dates, locations, events, k 'Accepted Response: The system returns an acknowledgment immediately after the request is validated, while the workflow continues to execute asynchronously in the background. /Final Response: The system returns a response only after the workflow execution is completed.', authMethods: 'Authentication methods', authType: 'Authentication type', - limit: 'Request limit', + limit: 'Request frequency limit', per: 'Time period', maxBodySize: 'Maximum body size', ipWhitelist: 'IP whitelist', diff --git a/web/src/pages/agent/canvas/index.tsx b/web/src/pages/agent/canvas/index.tsx index ebf261915..074bd828a 100644 --- a/web/src/pages/agent/canvas/index.tsx +++ b/web/src/pages/agent/canvas/index.tsx @@ -157,10 +157,10 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) { currentEventListWithoutMessageById, clearEventList, currentMessageId, - currentTaskId, + latestTaskId, } = useCacheChatLog(); - const { stopMessage } = useStopMessageUnmount(chatVisible, currentTaskId); + const { stopMessage } = useStopMessageUnmount(chatVisible, latestTaskId); const { showLogSheet, logSheetVisible, hideLogSheet } = useShowLogSheet({ setCurrentMessageId, @@ -179,10 +179,10 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) { useEffect(() => { if (!chatVisible) { - stopMessage(currentTaskId); + stopMessage(latestTaskId); clearEventList(); } - }, [chatVisible, clearEventList, currentTaskId, stopMessage]); + }, [chatVisible, clearEventList, latestTaskId, stopMessage]); const setLastSendLoadingFunc = (loading: boolean, messageId: string) => { setCurrentSendLoading(!!loading); diff --git a/web/src/pages/agent/hooks/use-cache-chat-log.ts b/web/src/pages/agent/hooks/use-cache-chat-log.ts index 7e67cdd0b..45fa6b7f4 100644 --- a/web/src/pages/agent/hooks/use-cache-chat-log.ts +++ b/web/src/pages/agent/hooks/use-cache-chat-log.ts @@ -3,7 +3,8 @@ import { INodeEvent, MessageEventType, } from '@/hooks/use-send-message'; -import { useCallback, useEffect, useMemo, useState } from 'react'; +import { get, isEmpty } from 'lodash'; +import { useCallback, useMemo, useState } from 'react'; export const ExcludeTypes = [ MessageEventType.Message, @@ -11,15 +12,13 @@ export const ExcludeTypes = [ ]; export function useCacheChatLog() { - const [eventList, setEventList] = useState([]); const [messageIdPool, setMessageIdPool] = useState< Record >({}); + const [latestTaskId, setLatestTaskId] = useState(''); + const [currentMessageId, setCurrentMessageId] = useState(''); - useEffect(() => { - setMessageIdPool((prev) => ({ ...prev, [currentMessageId]: eventList })); - }, [currentMessageId, eventList]); const filterEventListByMessageId = useCallback( (messageId: string) => { @@ -40,16 +39,26 @@ export function useCacheChatLog() { ); const clearEventList = useCallback(() => { - setEventList([]); setMessageIdPool({}); }, []); const addEventList = useCallback((events: IEventList, message_id: string) => { - setEventList((x) => { - const list = [...x, ...events]; - setMessageIdPool((prev) => ({ ...prev, [message_id]: list })); - return list; - }); + if (!isEmpty(events)) { + const taskId = get(events, '0.task_id'); + setLatestTaskId(taskId); + + setMessageIdPool((prev) => { + const list = [...(prev[message_id] ?? [])]; + + events.forEach((event) => { + if (!list.some((y) => y === event)) { + list.push(event); + } + }); + + return { ...prev, [message_id]: list }; + }); + } }, []); const currentEventListWithoutMessage = useMemo(() => { @@ -73,21 +82,15 @@ export function useCacheChatLog() { [messageIdPool], ); - const currentTaskId = useMemo(() => { - return eventList.at(-1)?.task_id; - }, [eventList]); - return { - eventList, currentEventListWithoutMessage, currentEventListWithoutMessageById, - setEventList, clearEventList, addEventList, filterEventListByEventType, filterEventListByMessageId, setCurrentMessageId, currentMessageId, - currentTaskId, + latestTaskId, }; } diff --git a/web/src/pages/agent/log-sheet/workflow-timeline.tsx b/web/src/pages/agent/log-sheet/workflow-timeline.tsx index 5fdb8c3cc..59776907c 100644 --- a/web/src/pages/agent/log-sheet/workflow-timeline.tsx +++ b/web/src/pages/agent/log-sheet/workflow-timeline.tsx @@ -22,7 +22,7 @@ import { import { ITraceData } from '@/interfaces/database/agent'; import { cn } from '@/lib/utils'; import { t } from 'i18next'; -import { get, isEmpty, isEqual, uniqWith } from 'lodash'; +import { get, isEmpty } from 'lodash'; import { useCallback, useEffect, useMemo } from 'react'; import { Operator } from '../constant'; import { JsonViewer } from '../form/components/json-viewer'; @@ -88,7 +88,7 @@ function getInputsOrOutputs( return inputsOrOutputs[0] || {}; } - return uniqWith(inputsOrOutputs, isEqual); // TODO: Violence should not be used to + return inputsOrOutputs; } export const WorkFlowTimeline = ({ currentEventListWithoutMessage,