| | |
| | | import { useWorkflowRun } from '../../hooks' |
| | | import { NodeRunningStatus, WorkflowRunningStatus } from '../../types' |
| | | import { useWorkflowStore } from '../../store' |
| | | import { DEFAULT_ITER_TIMES, DEFAULT_LOOP_TIMES } from '../../constants' |
| | | import { DEFAULT_ITER_TIMES } from '../../constants' |
| | | import type { |
| | | ChatItem, |
| | | ChatItemInTree, |
| | |
| | | } from '@/app/components/base/file-uploader/utils' |
| | | import type { FileEntity } from '@/app/components/base/file-uploader/types' |
| | | import { getThreadMessages } from '@/app/components/base/chat/utils' |
| | | import type { NodeTracing } from '@/types/workflow' |
| | | |
| | | type GetAbortController = (abortController: AbortController) => void |
| | | type SendCallback = { |
| | |
| | | const suggestedQuestionsAbortControllerRef = useRef<AbortController | null>(null) |
| | | const { |
| | | setIterTimes, |
| | | setLoopTimes, |
| | | } = workflowStore.getState() |
| | | |
| | | const handleResponding = useCallback((isResponding: boolean) => { |
| | |
| | | if (stopChat && taskIdRef.current) |
| | | stopChat(taskIdRef.current) |
| | | setIterTimes(DEFAULT_ITER_TIMES) |
| | | setLoopTimes(DEFAULT_LOOP_TIMES) |
| | | if (suggestedQuestionsAbortControllerRef.current) |
| | | suggestedQuestionsAbortControllerRef.current.abort() |
| | | }, [handleResponding, setIterTimes, setLoopTimes, stopChat]) |
| | | }, [handleResponding, setIterTimes, stopChat]) |
| | | |
| | | const handleRestart = useCallback(() => { |
| | | conversationId.current = '' |
| | | taskIdRef.current = '' |
| | | handleStop() |
| | | setIterTimes(DEFAULT_ITER_TIMES) |
| | | setLoopTimes(DEFAULT_LOOP_TIMES) |
| | | setChatTree([]) |
| | | setSuggestQuestions([]) |
| | | }, [ |
| | | handleStop, |
| | | setIterTimes, |
| | | setLoopTimes, |
| | | ]) |
| | | |
| | | const updateCurrentQAOnTree = useCallback(({ |
| | |
| | | ) |
| | | setSuggestQuestions(data) |
| | | } |
| | | // eslint-disable-next-line unused-imports/no-unused-vars |
| | | catch (error) { |
| | | setSuggestQuestions([]) |
| | | } |
| | |
| | | responseItem.workflowProcess!.tracing!.push({ |
| | | ...data, |
| | | status: NodeRunningStatus.Running, |
| | | details: [], |
| | | } as any) |
| | | updateCurrentQAOnTree({ |
| | | placeholderQuestionId, |
| | | questionItem, |
| | | responseItem, |
| | | parentId: params.parent_message_id, |
| | | }) |
| | | }, |
| | | onIterationNext: ({ data }) => { |
| | | const tracing = responseItem.workflowProcess!.tracing! |
| | | const iterations = tracing.find(item => item.node_id === data.node_id |
| | | && (item.execution_metadata?.parallel_id === data.execution_metadata?.parallel_id || item.parallel_id === data.execution_metadata?.parallel_id))! |
| | | iterations.details!.push([]) |
| | | |
| | | updateCurrentQAOnTree({ |
| | | placeholderQuestionId, |
| | | questionItem, |
| | |
| | | }) |
| | | }, |
| | | onIterationFinish: ({ data }) => { |
| | | const currentTracingIndex = responseItem.workflowProcess!.tracing!.findIndex(item => item.id === data.id) |
| | | if (currentTracingIndex > -1) { |
| | | responseItem.workflowProcess!.tracing[currentTracingIndex] = { |
| | | ...responseItem.workflowProcess!.tracing[currentTracingIndex], |
| | | const tracing = responseItem.workflowProcess!.tracing! |
| | | const iterationsIndex = tracing.findIndex(item => item.node_id === data.node_id |
| | | && (item.execution_metadata?.parallel_id === data.execution_metadata?.parallel_id || item.parallel_id === data.execution_metadata?.parallel_id))! |
| | | tracing[iterationsIndex] = { |
| | | ...tracing[iterationsIndex], |
| | | ...data, |
| | | } |
| | | status: NodeRunningStatus.Succeeded, |
| | | } as any |
| | | updateCurrentQAOnTree({ |
| | | placeholderQuestionId, |
| | | questionItem, |
| | | responseItem, |
| | | parentId: params.parent_message_id, |
| | | }) |
| | | } |
| | | }, |
| | | onLoopStart: ({ data }) => { |
| | | responseItem.workflowProcess!.tracing!.push({ |
| | | ...data, |
| | | status: NodeRunningStatus.Running, |
| | | }) |
| | | updateCurrentQAOnTree({ |
| | | placeholderQuestionId, |
| | | questionItem, |
| | | responseItem, |
| | | parentId: params.parent_message_id, |
| | | }) |
| | | }, |
| | | onLoopFinish: ({ data }) => { |
| | | const currentTracingIndex = responseItem.workflowProcess!.tracing!.findIndex(item => item.id === data.id) |
| | | if (currentTracingIndex > -1) { |
| | | responseItem.workflowProcess!.tracing[currentTracingIndex] = { |
| | | ...responseItem.workflowProcess!.tracing[currentTracingIndex], |
| | | ...data, |
| | | } |
| | | updateCurrentQAOnTree({ |
| | | placeholderQuestionId, |
| | | questionItem, |
| | | responseItem, |
| | | parentId: params.parent_message_id, |
| | | }) |
| | | } |
| | | }, |
| | | onNodeStarted: ({ data }) => { |
| | | if (data.iteration_id) |
| | | return |
| | | |
| | | responseItem.workflowProcess!.tracing!.push({ |
| | | ...data, |
| | | status: NodeRunningStatus.Running, |
| | |
| | | }) |
| | | }, |
| | | onNodeRetry: ({ data }) => { |
| | | responseItem.workflowProcess!.tracing!.push(data) |
| | | if (data.iteration_id) |
| | | return |
| | | |
| | | updateCurrentQAOnTree({ |
| | | placeholderQuestionId, |
| | | questionItem, |
| | | responseItem, |
| | | parentId: params.parent_message_id, |
| | | const currentIndex = responseItem.workflowProcess!.tracing!.findIndex((item) => { |
| | | if (!item.execution_metadata?.parallel_id) |
| | | return item.node_id === data.node_id |
| | | return item.node_id === data.node_id && (item.execution_metadata?.parallel_id === data.execution_metadata?.parallel_id || item.parallel_id === data.execution_metadata?.parallel_id) |
| | | }) |
| | | if (responseItem.workflowProcess!.tracing[currentIndex].retryDetail) |
| | | responseItem.workflowProcess!.tracing[currentIndex].retryDetail?.push(data as NodeTracing) |
| | | else |
| | | responseItem.workflowProcess!.tracing[currentIndex].retryDetail = [data as NodeTracing] |
| | | |
| | | handleUpdateChatList(produce(chatListRef.current, (draft) => { |
| | | const currentIndex = draft.findIndex(item => item.id === responseItem.id) |
| | | draft[currentIndex] = { |
| | | ...draft[currentIndex], |
| | | ...responseItem, |
| | | } |
| | | })) |
| | | }, |
| | | onNodeFinished: ({ data }) => { |
| | | const currentTracingIndex = responseItem.workflowProcess!.tracing!.findIndex(item => item.id === data.id) |
| | | if (currentTracingIndex > -1) { |
| | | responseItem.workflowProcess!.tracing[currentTracingIndex] = { |
| | | ...responseItem.workflowProcess!.tracing[currentTracingIndex], |
| | | ...data, |
| | | } |
| | | updateCurrentQAOnTree({ |
| | | placeholderQuestionId, |
| | | questionItem, |
| | | responseItem, |
| | | parentId: params.parent_message_id, |
| | | }) |
| | | } |
| | | }, |
| | | 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 (data.iteration_id) |
| | | return |
| | | |
| | | 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], |
| | | const currentIndex = responseItem.workflowProcess!.tracing!.findIndex((item) => { |
| | | if (!item.execution_metadata?.parallel_id) |
| | | return item.node_id === data.node_id |
| | | return item.node_id === data.node_id && (item.execution_metadata?.parallel_id === data.execution_metadata?.parallel_id || item.parallel_id === data.execution_metadata?.parallel_id) |
| | | }) |
| | | responseItem.workflowProcess!.tracing[currentIndex] = { |
| | | ...(responseItem.workflowProcess!.tracing[currentIndex]?.extras |
| | | ? { extras: responseItem.workflowProcess!.tracing[currentIndex].extras } |
| | | : {}), |
| | | ...(responseItem.workflowProcess!.tracing[currentIndex]?.retryDetail |
| | | ? { retryDetail: responseItem.workflowProcess!.tracing[currentIndex].retryDetail } |
| | | : {}), |
| | | ...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 |
| | | } |
| | | |
| | | responseItem.workflowProcess!.tracing[currentNodeIndex] = { |
| | | ...current, |
| | | } |
| | | |
| | | updateCurrentQAOnTree({ |
| | | placeholderQuestionId, |
| | | questionItem, |
| | | responseItem, |
| | | parentId: params.parent_message_id, |
| | | }) |
| | | } |
| | | }, |
| | | }, |
| | | ) |