diff --git a/web/app/components/workflow/hooks/use-workflow-run-event/index.ts b/web/app/components/workflow/hooks/use-workflow-run-event/index.ts index 61216017e0..70528f7e79 100644 --- a/web/app/components/workflow/hooks/use-workflow-run-event/index.ts +++ b/web/app/components/workflow/hooks/use-workflow-run-event/index.ts @@ -9,3 +9,4 @@ export * from './use-workflow-node-iteration-finished' export * from './use-workflow-node-retry' export * from './use-workflow-text-chunk' export * from './use-workflow-text-replace' +export * from './use-workflow-agent-log' diff --git a/web/app/components/workflow/hooks/use-workflow-run-event/use-workflow-agent-log.ts b/web/app/components/workflow/hooks/use-workflow-run-event/use-workflow-agent-log.ts new file mode 100644 index 0000000000..9a9fa628c0 --- /dev/null +++ b/web/app/components/workflow/hooks/use-workflow-run-event/use-workflow-agent-log.ts @@ -0,0 +1,50 @@ +import { useCallback } from 'react' +import produce from 'immer' +import type { AgentLogResponse } from '@/types/workflow' +import { useWorkflowStore } from '@/app/components/workflow/store' + +export const useWorkflowAgentLog = () => { + const workflowStore = useWorkflowStore() + + const handleWorkflowAgentLog = useCallback((params: AgentLogResponse) => { + const { data } = params + const { + workflowRunningData, + setWorkflowRunningData, + } = workflowStore.getState() + + setWorkflowRunningData(produce(workflowRunningData!, (draft) => { + const currentIndex = draft.tracing!.findIndex(item => item.node_id === data.node_id) + if (currentIndex > -1) { + const current = draft.tracing![currentIndex] + + if (current.execution_metadata) { + if (current.execution_metadata.agent_log) { + const currentLogIndex = current.execution_metadata.agent_log.findIndex(log => log.id === data.id) + if (currentLogIndex > -1) { + current.execution_metadata.agent_log[currentLogIndex] = { + ...current.execution_metadata.agent_log[currentLogIndex], + ...data, + } + } + else { + current.execution_metadata.agent_log.push(data) + } + } + else { + current.execution_metadata.agent_log = [data] + } + } + else { + current.execution_metadata = { + agent_log: [data], + } as any + } + } + })) + }, [workflowStore]) + + return { + handleWorkflowAgentLog, + } +} diff --git a/web/app/components/workflow/hooks/use-workflow-run-event/use-workflow-run-event.ts b/web/app/components/workflow/hooks/use-workflow-run-event/use-workflow-run-event.ts index 82b76da22d..8ba6220818 100644 --- a/web/app/components/workflow/hooks/use-workflow-run-event/use-workflow-run-event.ts +++ b/web/app/components/workflow/hooks/use-workflow-run-event/use-workflow-run-event.ts @@ -1,4 +1,5 @@ import { + useWorkflowAgentLog, useWorkflowFailed, useWorkflowFinished, useWorkflowNodeFinished, @@ -24,6 +25,7 @@ export const useWorkflowRunEvent = () => { const { handleWorkflowNodeRetry } = useWorkflowNodeRetry() const { handleWorkflowTextChunk } = useWorkflowTextChunk() const { handleWorkflowTextReplace } = useWorkflowTextReplace() + const { handleWorkflowAgentLog } = useWorkflowAgentLog() return { handleWorkflowStarted, @@ -37,5 +39,6 @@ export const useWorkflowRunEvent = () => { handleWorkflowNodeRetry, handleWorkflowTextChunk, handleWorkflowTextReplace, + handleWorkflowAgentLog, } } diff --git a/web/app/components/workflow/hooks/use-workflow-run.ts b/web/app/components/workflow/hooks/use-workflow-run.ts index 1224e8136e..00e7faeeed 100644 --- a/web/app/components/workflow/hooks/use-workflow-run.ts +++ b/web/app/components/workflow/hooks/use-workflow-run.ts @@ -37,6 +37,7 @@ export const useWorkflowRun = () => { handleWorkflowNodeIterationNext, handleWorkflowNodeIterationFinished, handleWorkflowNodeRetry, + handleWorkflowAgentLog, handleWorkflowTextChunk, handleWorkflowTextReplace, } = useWorkflowRunEvent() @@ -118,6 +119,7 @@ export const useWorkflowRun = () => { onIterationNext, onIterationFinish, onNodeRetry, + onAgentLog, onError, ...restCallback } = callback || {} @@ -234,6 +236,12 @@ export const useWorkflowRun = () => { if (onNodeRetry) onNodeRetry(params) }, + onAgentLog: (params) => { + handleWorkflowAgentLog(params) + + if (onAgentLog) + onAgentLog(params) + }, onTextChunk: (params) => { handleWorkflowTextChunk(params) }, @@ -252,7 +260,7 @@ export const useWorkflowRun = () => { ...restCallback, }, ) - }, [store, workflowStore, doSyncWorkflowDraft, handleWorkflowStarted, handleWorkflowFinished, handleWorkflowFailed, handleWorkflowNodeStarted, handleWorkflowNodeFinished, handleWorkflowNodeIterationStarted, handleWorkflowNodeIterationNext, handleWorkflowNodeIterationFinished, handleWorkflowNodeRetry, handleWorkflowTextChunk, handleWorkflowTextReplace, pathname]) + }, [store, workflowStore, doSyncWorkflowDraft, handleWorkflowStarted, handleWorkflowFinished, handleWorkflowFailed, handleWorkflowNodeStarted, handleWorkflowNodeFinished, handleWorkflowNodeIterationStarted, handleWorkflowNodeIterationNext, handleWorkflowNodeIterationFinished, handleWorkflowNodeRetry, handleWorkflowTextChunk, handleWorkflowTextReplace, handleWorkflowAgentLog, pathname]) const handleStopRun = useCallback((taskId: string) => { const appId = useAppStore.getState().appDetail?.id diff --git a/web/app/components/workflow/panel/debug-and-preview/hooks.ts b/web/app/components/workflow/panel/debug-and-preview/hooks.ts index d91cf1082d..81cccd9cc7 100644 --- a/web/app/components/workflow/panel/debug-and-preview/hooks.ts +++ b/web/app/components/workflow/panel/debug-and-preview/hooks.ts @@ -404,6 +404,63 @@ export const useChat = ( })) } }, + onAgentLog: ({ data }) => { + const currentNodeIndex = responseItem.workflowProcess!.tracing!.findIndex(item => item.node_id === data.node_id) + if (currentNodeIndex > -1) { + const current = responseItem.workflowProcess!.tracing![currentNodeIndex] + + if (current.execution_metadata) { + if (current.execution_metadata.agent_log) { + const currentLogIndex = current.execution_metadata.agent_log.findIndex(log => log.id === data.id) + if (currentLogIndex > -1) { + current.execution_metadata.agent_log[currentLogIndex] = { + ...current.execution_metadata.agent_log[currentLogIndex], + ...data, + } + } + else { + current.execution_metadata.agent_log.push(data) + } + } + else { + current.execution_metadata.agent_log = [data] + } + } + else { + current.execution_metadata = { + agent_log: [data], + } as any + } + // if (current.agentLog) { + // const currentLogIndex = current.agentLog.findIndex(log => log.id === data.id) + + // if (currentLogIndex > -1) { + // current.agentLog[currentLogIndex] = { + // ...current.agentLog[currentLogIndex], + // ...data, + // } + // } + // else { + // current.agentLog.push(data) + // } + // } + // else { + // current.agentLog = [data] + // } + + responseItem.workflowProcess!.tracing[currentNodeIndex] = { + ...current, + } + + handleUpdateChatList(produce(chatListRef.current, (draft) => { + const currentIndex = draft.findIndex(item => item.id === responseItem.id) + draft[currentIndex] = { + ...draft[currentIndex], + ...responseItem, + } + })) + } + }, }, ) }, [handleRun, handleResponding, handleUpdateChatList, notify, t, updateCurrentQA, config.suggested_questions_after_answer?.enabled, formSettings]) diff --git a/web/app/components/workflow/run/agent-log/agent-log-item.tsx b/web/app/components/workflow/run/agent-log/agent-log-item.tsx index 49c279d58a..0b84827500 100644 --- a/web/app/components/workflow/run/agent-log/agent-log-item.tsx +++ b/web/app/components/workflow/run/agent-log/agent-log-item.tsx @@ -1,4 +1,7 @@ -import { useState } from 'react' +import { + useMemo, + useState, +} from 'react' import { RiArrowRightSLine, RiListView, @@ -9,6 +12,9 @@ import type { AgentLogItemWithChildren } from '@/types/workflow' import NodeStatusIcon from '@/app/components/workflow/nodes/_base/components/node-status-icon' import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor' import { CodeLanguage } from '@/app/components/workflow/nodes/code/types' +import BlockIcon from '@/app/components/workflow/block-icon' +import { BlockEnum } from '@/app/components/workflow/types' +import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-icon' type AgentLogItemProps = { item: AgentLogItemWithChildren @@ -26,6 +32,26 @@ const AgentLogItem = ({ metadata, } = item const [expanded, setExpanded] = useState(false) + const { getIconUrl } = useGetIcon() + const toolIcon = useMemo(() => { + const icon = metadata?.icon + + if (icon) { + if (icon.includes('http')) + return icon + + return getIconUrl(icon) + } + + return '' + }, [getIconUrl, metadata?.icon]) + + const mergeStatus = useMemo(() => { + if (status === 'start') + return 'running' + + return status + }, [status]) return (