diff --git a/web/src/hooks/use-send-message.ts b/web/src/hooks/use-send-message.ts index 35dd81a8e..bfa7cca91 100644 --- a/web/src/hooks/use-send-message.ts +++ b/web/src/hooks/use-send-message.ts @@ -13,6 +13,7 @@ export enum MessageEventType { MessageEnd = 'message_end', WorkflowFinished = 'workflow_finished', UserInputs = 'user_inputs', + NodeLogs = 'node_logs', } export interface IAnswerEvent { @@ -42,12 +43,25 @@ export interface IMessageData { content: string; } +export interface ILogData extends INodeData { + logs: { + name: string; + result: string; + args: { + query: string; + topic: string; + }; + }; +} + export type INodeEvent = IAnswerEvent; export type IMessageEvent = IAnswerEvent; export type IInputEvent = IAnswerEvent; +export type ILogEvent = IAnswerEvent; + export type IChatEvent = INodeEvent | IMessageEvent; export type IEventList = Array; diff --git a/web/src/pages/agent/log-sheet/index.tsx b/web/src/pages/agent/log-sheet/index.tsx index 9eafa2302..0e2903b2e 100644 --- a/web/src/pages/agent/log-sheet/index.tsx +++ b/web/src/pages/agent/log-sheet/index.tsx @@ -18,9 +18,14 @@ import { SheetHeader, SheetTitle, } from '@/components/ui/sheet'; -import { INodeEvent, MessageEventType } from '@/hooks/use-send-message'; +import { + ILogData, + ILogEvent, + MessageEventType, +} from '@/hooks/use-send-message'; import { IModalProps } from '@/interfaces/common'; import { cn } from '@/lib/utils'; +import { isEmpty } from 'lodash'; import { BellElectric, NotebookText } from 'lucide-react'; import { useCallback, useMemo } from 'react'; import JsonView from 'react18-json-view'; @@ -51,6 +56,25 @@ function JsonViewer({ ); } +function concatData( + firstRecord: Record | Array>, + nextRecord: Record | Array>, +) { + let result: Array> = []; + + if (!isEmpty(firstRecord)) { + result = result.concat(firstRecord); + } + + if (!isEmpty(nextRecord)) { + result = result.concat(nextRecord); + } + + return isEmpty(result) ? {} : result; +} + +type EventWithIndex = { startNodeIdx: number } & ILogEvent; + export function LogSheet({ hideModal, currentEventListWithoutMessage, @@ -64,12 +88,51 @@ export function LogSheet({ [getNode], ); + // Look up to find the nearest start component id and concatenate the finish and log data into one const finishedNodeList = useMemo(() => { return currentEventListWithoutMessage.filter( - (x) => x.event === MessageEventType.NodeFinished, - ) as INodeEvent[]; + (x) => + x.event === MessageEventType.NodeFinished || + x.event === MessageEventType.NodeLogs, + ) as ILogEvent[]; }, [currentEventListWithoutMessage]); - console.log('🚀 ~ finishedNodeList ~ finishedNodeList:', finishedNodeList); + + const nextList = useMemo(() => { + return finishedNodeList.reduce>((pre, cur) => { + const startNodeIdx = ( + currentEventListWithoutMessage as Array + ).findLastIndex( + (x) => + x.data.component_id === cur.data.component_id && + x.event === MessageEventType.NodeStarted, + ); + + const item = pre.find((x) => x.startNodeIdx === startNodeIdx); + + const { logs = {}, inputs = {}, outputs = {} } = cur.data; + if (item) { + const { + inputs: inputList, + outputs: outputList, + logs: logList, + } = item.data; + + item.data = { + ...item.data, + inputs: concatData(inputList, inputs), + outputs: concatData(outputList, outputs), + logs: concatData(logList, logs), + }; + } else { + pre.push({ + ...cur, + startNodeIdx, + }); + } + + return pre; + }, []); + }, [currentEventListWithoutMessage, finishedNodeList]); return ( @@ -82,7 +145,7 @@ export function LogSheet({
- {finishedNodeList.map((x, idx) => ( + {nextList.map((x, idx) => ( + {isEmpty((x.data as ILogData)?.logs) || ( + + )} +