| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   useCallback, | 
					
						
							|  |  |  |   useEffect, | 
					
						
							|  |  |  |   useRef, | 
					
						
							|  |  |  |   useState, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { produce, setAutoFreeze } from 'immer' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import { uniqBy } from 'lodash-es' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { useWorkflowRun } from '../../hooks' | 
					
						
							| 
									
										
										
										
											2024-05-11 16:23:31 +08:00
										 |  |  | import { NodeRunningStatus, WorkflowRunningStatus } from '../../types' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { | 
					
						
							|  |  |  |   ChatItem, | 
					
						
							|  |  |  |   Inputs, | 
					
						
							|  |  |  | } from '@/app/components/base/chat/types' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import type { InputForm } from '@/app/components/base/chat/chat/type' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   getProcessedInputs, | 
					
						
							|  |  |  |   processOpeningStatement, | 
					
						
							|  |  |  | } from '@/app/components/base/chat/chat/utils' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { useToastContext } from '@/app/components/base/toast' | 
					
						
							|  |  |  | import { TransferMethod } from '@/types/app' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   getProcessedFiles, | 
					
						
							|  |  |  |   getProcessedFilesFromResponse, | 
					
						
							|  |  |  | } from '@/app/components/base/file-uploader/utils' | 
					
						
							|  |  |  | import type { FileEntity } from '@/app/components/base/file-uploader/types' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type GetAbortController = (abortController: AbortController) => void | 
					
						
							|  |  |  | type SendCallback = { | 
					
						
							|  |  |  |   onGetSuggestedQuestions?: (responseItemId: string, getAbortController: GetAbortController) => Promise<any> | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export const useChat = ( | 
					
						
							|  |  |  |   config: any, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   formSettings?: { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     inputs: Inputs | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     inputsForm: InputForm[] | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   prevChatList?: ChatItem[], | 
					
						
							|  |  |  |   stopChat?: (taskId: string) => void, | 
					
						
							|  |  |  | ) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const { notify } = useToastContext() | 
					
						
							|  |  |  |   const { handleRun } = useWorkflowRun() | 
					
						
							|  |  |  |   const hasStopResponded = useRef(false) | 
					
						
							| 
									
										
										
										
											2024-08-06 13:33:21 +08:00
										 |  |  |   const conversationId = useRef('') | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const taskIdRef = useRef('') | 
					
						
							|  |  |  |   const [chatList, setChatList] = useState<ChatItem[]>(prevChatList || []) | 
					
						
							|  |  |  |   const chatListRef = useRef<ChatItem[]>(prevChatList || []) | 
					
						
							|  |  |  |   const [isResponding, setIsResponding] = useState(false) | 
					
						
							|  |  |  |   const isRespondingRef = useRef(false) | 
					
						
							|  |  |  |   const [suggestedQuestions, setSuggestQuestions] = useState<string[]>([]) | 
					
						
							|  |  |  |   const suggestedQuestionsAbortControllerRef = useRef<AbortController | null>(null) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     setAutoFreeze(false) | 
					
						
							|  |  |  |     return () => { | 
					
						
							|  |  |  |       setAutoFreeze(true) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleUpdateChatList = useCallback((newChatList: ChatItem[]) => { | 
					
						
							|  |  |  |     setChatList(newChatList) | 
					
						
							|  |  |  |     chatListRef.current = newChatList | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleResponding = useCallback((isResponding: boolean) => { | 
					
						
							|  |  |  |     setIsResponding(isResponding) | 
					
						
							|  |  |  |     isRespondingRef.current = isResponding | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getIntroduction = useCallback((str: string) => { | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     return processOpeningStatement(str, formSettings?.inputs || {}, formSettings?.inputsForm || []) | 
					
						
							|  |  |  |   }, [formSettings?.inputs, formSettings?.inputsForm]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (config?.opening_statement) { | 
					
						
							|  |  |  |       handleUpdateChatList(produce(chatListRef.current, (draft) => { | 
					
						
							|  |  |  |         const index = draft.findIndex(item => item.isOpeningStatement) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (index > -1) { | 
					
						
							|  |  |  |           draft[index] = { | 
					
						
							|  |  |  |             ...draft[index], | 
					
						
							|  |  |  |             content: getIntroduction(config.opening_statement), | 
					
						
							|  |  |  |             suggestedQuestions: config.suggested_questions, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |           draft.unshift({ | 
					
						
							|  |  |  |             id: `${Date.now()}`, | 
					
						
							|  |  |  |             content: getIntroduction(config.opening_statement), | 
					
						
							|  |  |  |             isAnswer: true, | 
					
						
							|  |  |  |             isOpeningStatement: true, | 
					
						
							|  |  |  |             suggestedQuestions: config.suggested_questions, | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       })) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [config?.opening_statement, getIntroduction, config?.suggested_questions, handleUpdateChatList]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleStop = useCallback(() => { | 
					
						
							|  |  |  |     hasStopResponded.current = true | 
					
						
							|  |  |  |     handleResponding(false) | 
					
						
							|  |  |  |     if (stopChat && taskIdRef.current) | 
					
						
							|  |  |  |       stopChat(taskIdRef.current) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (suggestedQuestionsAbortControllerRef.current) | 
					
						
							|  |  |  |       suggestedQuestionsAbortControllerRef.current.abort() | 
					
						
							|  |  |  |   }, [handleResponding, stopChat]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleRestart = useCallback(() => { | 
					
						
							| 
									
										
										
										
											2024-08-06 13:33:21 +08:00
										 |  |  |     conversationId.current = '' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     taskIdRef.current = '' | 
					
						
							|  |  |  |     handleStop() | 
					
						
							|  |  |  |     const newChatList = config?.opening_statement | 
					
						
							|  |  |  |       ? [{ | 
					
						
							|  |  |  |         id: `${Date.now()}`, | 
					
						
							|  |  |  |         content: config.opening_statement, | 
					
						
							|  |  |  |         isAnswer: true, | 
					
						
							|  |  |  |         isOpeningStatement: true, | 
					
						
							|  |  |  |         suggestedQuestions: config.suggested_questions, | 
					
						
							|  |  |  |       }] | 
					
						
							|  |  |  |       : [] | 
					
						
							|  |  |  |     handleUpdateChatList(newChatList) | 
					
						
							|  |  |  |     setSuggestQuestions([]) | 
					
						
							|  |  |  |   }, [ | 
					
						
							|  |  |  |     config, | 
					
						
							|  |  |  |     handleStop, | 
					
						
							|  |  |  |     handleUpdateChatList, | 
					
						
							|  |  |  |   ]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const updateCurrentQA = useCallback(({ | 
					
						
							|  |  |  |     responseItem, | 
					
						
							|  |  |  |     questionId, | 
					
						
							|  |  |  |     placeholderAnswerId, | 
					
						
							|  |  |  |     questionItem, | 
					
						
							|  |  |  |   }: { | 
					
						
							|  |  |  |     responseItem: ChatItem | 
					
						
							|  |  |  |     questionId: string | 
					
						
							|  |  |  |     placeholderAnswerId: string | 
					
						
							|  |  |  |     questionItem: ChatItem | 
					
						
							|  |  |  |   }) => { | 
					
						
							|  |  |  |     const newListWithAnswer = produce( | 
					
						
							|  |  |  |       chatListRef.current.filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId), | 
					
						
							|  |  |  |       (draft) => { | 
					
						
							|  |  |  |         if (!draft.find(item => item.id === questionId)) | 
					
						
							|  |  |  |           draft.push({ ...questionItem }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         draft.push({ ...responseItem }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     handleUpdateChatList(newListWithAnswer) | 
					
						
							|  |  |  |   }, [handleUpdateChatList]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleSend = useCallback(( | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     params: { | 
					
						
							|  |  |  |       query: string | 
					
						
							|  |  |  |       files?: FileEntity[] | 
					
						
							|  |  |  |       [key: string]: any | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |       onGetSuggestedQuestions, | 
					
						
							|  |  |  |     }: SendCallback, | 
					
						
							|  |  |  |   ) => { | 
					
						
							|  |  |  |     if (isRespondingRef.current) { | 
					
						
							|  |  |  |       notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') }) | 
					
						
							|  |  |  |       return false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const questionId = `question-${Date.now()}` | 
					
						
							|  |  |  |     const questionItem = { | 
					
						
							|  |  |  |       id: questionId, | 
					
						
							|  |  |  |       content: params.query, | 
					
						
							|  |  |  |       isAnswer: false, | 
					
						
							|  |  |  |       message_files: params.files, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const placeholderAnswerId = `answer-placeholder-${Date.now()}` | 
					
						
							|  |  |  |     const placeholderAnswerItem = { | 
					
						
							|  |  |  |       id: placeholderAnswerId, | 
					
						
							|  |  |  |       content: '', | 
					
						
							|  |  |  |       isAnswer: true, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newList = [...chatListRef.current, questionItem, placeholderAnswerItem] | 
					
						
							|  |  |  |     handleUpdateChatList(newList) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // answer
 | 
					
						
							|  |  |  |     const responseItem: ChatItem = { | 
					
						
							| 
									
										
										
										
											2024-05-11 16:23:31 +08:00
										 |  |  |       id: placeholderAnswerId, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       content: '', | 
					
						
							|  |  |  |       agent_thoughts: [], | 
					
						
							|  |  |  |       message_files: [], | 
					
						
							|  |  |  |       isAnswer: true, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     handleResponding(true) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     const { files, inputs, ...restParams } = params | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     const bodyParams = { | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |       files: getProcessedFiles(files || []), | 
					
						
							|  |  |  |       inputs: getProcessedInputs(inputs || {}, formSettings?.inputsForm || []), | 
					
						
							|  |  |  |       ...restParams, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (bodyParams?.files?.length) { | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |       bodyParams.files = bodyParams.files.map((item) => { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         if (item.transfer_method === TransferMethod.local_file) { | 
					
						
							|  |  |  |           return { | 
					
						
							|  |  |  |             ...item, | 
					
						
							|  |  |  |             url: '', | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return item | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let hasSetResponseId = false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     handleRun( | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |       bodyParams, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       { | 
					
						
							|  |  |  |         onData: (message: string, isFirstMessage: boolean, { conversationId: newConversationId, messageId, taskId }: any) => { | 
					
						
							|  |  |  |           responseItem.content = responseItem.content + message | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (messageId && !hasSetResponseId) { | 
					
						
							|  |  |  |             responseItem.id = messageId | 
					
						
							|  |  |  |             hasSetResponseId = true | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (isFirstMessage && newConversationId) | 
					
						
							| 
									
										
										
										
											2024-08-06 13:33:21 +08:00
										 |  |  |             conversationId.current = newConversationId | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           taskIdRef.current = taskId | 
					
						
							|  |  |  |           if (messageId) | 
					
						
							|  |  |  |             responseItem.id = messageId | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           updateCurrentQA({ | 
					
						
							|  |  |  |             responseItem, | 
					
						
							|  |  |  |             questionId, | 
					
						
							|  |  |  |             placeholderAnswerId, | 
					
						
							|  |  |  |             questionItem, | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         async onCompleted(hasError?: boolean, errorMessage?: string) { | 
					
						
							|  |  |  |           handleResponding(false) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (hasError) { | 
					
						
							|  |  |  |             if (errorMessage) { | 
					
						
							|  |  |  |               responseItem.content = errorMessage | 
					
						
							|  |  |  |               responseItem.isError = true | 
					
						
							|  |  |  |               const newListWithAnswer = produce( | 
					
						
							|  |  |  |                 chatListRef.current.filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId), | 
					
						
							|  |  |  |                 (draft) => { | 
					
						
							|  |  |  |                   if (!draft.find(item => item.id === questionId)) | 
					
						
							|  |  |  |                     draft.push({ ...questionItem }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                   draft.push({ ...responseItem }) | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |               handleUpdateChatList(newListWithAnswer) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (config?.suggested_questions_after_answer?.enabled && !hasStopResponded.current && onGetSuggestedQuestions) { | 
					
						
							| 
									
										
										
										
											2024-09-03 14:09:46 +08:00
										 |  |  |             try { | 
					
						
							|  |  |  |               const { data }: any = await onGetSuggestedQuestions( | 
					
						
							|  |  |  |                 responseItem.id, | 
					
						
							|  |  |  |                 newAbortController => suggestedQuestionsAbortControllerRef.current = newAbortController, | 
					
						
							|  |  |  |               ) | 
					
						
							|  |  |  |               setSuggestQuestions(data) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             catch (error) { | 
					
						
							|  |  |  |               setSuggestQuestions([]) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onMessageEnd: (messageEnd) => { | 
					
						
							|  |  |  |           responseItem.citation = messageEnd.metadata?.retriever_resources || [] | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |           const processedFilesFromResponse = getProcessedFilesFromResponse(messageEnd.files || []) | 
					
						
							|  |  |  |           responseItem.allFiles = uniqBy([...(responseItem.allFiles || []), ...(processedFilesFromResponse || [])], 'id') | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           const newListWithAnswer = produce( | 
					
						
							|  |  |  |             chatListRef.current.filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId), | 
					
						
							|  |  |  |             (draft) => { | 
					
						
							|  |  |  |               if (!draft.find(item => item.id === questionId)) | 
					
						
							|  |  |  |                 draft.push({ ...questionItem }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               draft.push({ ...responseItem }) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |           handleUpdateChatList(newListWithAnswer) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onMessageReplace: (messageReplace) => { | 
					
						
							|  |  |  |           responseItem.content = messageReplace.answer | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onError() { | 
					
						
							|  |  |  |           handleResponding(false) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onWorkflowStarted: ({ workflow_run_id, task_id }) => { | 
					
						
							|  |  |  |           taskIdRef.current = task_id | 
					
						
							|  |  |  |           responseItem.workflow_run_id = workflow_run_id | 
					
						
							|  |  |  |           responseItem.workflowProcess = { | 
					
						
							|  |  |  |             status: WorkflowRunningStatus.Running, | 
					
						
							|  |  |  |             tracing: [], | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           handleUpdateChatList(produce(chatListRef.current, (draft) => { | 
					
						
							|  |  |  |             const currentIndex = draft.findIndex(item => item.id === responseItem.id) | 
					
						
							|  |  |  |             draft[currentIndex] = { | 
					
						
							|  |  |  |               ...draft[currentIndex], | 
					
						
							|  |  |  |               ...responseItem, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onWorkflowFinished: ({ data }) => { | 
					
						
							|  |  |  |           responseItem.workflowProcess!.status = data.status as WorkflowRunningStatus | 
					
						
							|  |  |  |           handleUpdateChatList(produce(chatListRef.current, (draft) => { | 
					
						
							|  |  |  |             const currentIndex = draft.findIndex(item => item.id === responseItem.id) | 
					
						
							|  |  |  |             draft[currentIndex] = { | 
					
						
							|  |  |  |               ...draft[currentIndex], | 
					
						
							|  |  |  |               ...responseItem, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })) | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |         onIterationStart: ({ data }) => { | 
					
						
							| 
									
										
										
										
											2024-05-11 16:23:31 +08:00
										 |  |  |           responseItem.workflowProcess!.tracing!.push({ | 
					
						
							|  |  |  |             ...data, | 
					
						
							|  |  |  |             status: NodeRunningStatus.Running, | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |             details: [], | 
					
						
							| 
									
										
										
										
											2024-05-11 16:23:31 +08:00
										 |  |  |           } as any) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           handleUpdateChatList(produce(chatListRef.current, (draft) => { | 
					
						
							|  |  |  |             const currentIndex = draft.findIndex(item => item.id === responseItem.id) | 
					
						
							|  |  |  |             draft[currentIndex] = { | 
					
						
							|  |  |  |               ...draft[currentIndex], | 
					
						
							|  |  |  |               ...responseItem, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })) | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |         onIterationNext: ({ data }) => { | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           const tracing = responseItem.workflowProcess!.tracing! | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |           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))! | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           iterations.details!.push([]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           handleUpdateChatList(produce(chatListRef.current, (draft) => { | 
					
						
							|  |  |  |             const currentIndex = draft.length - 1 | 
					
						
							|  |  |  |             draft[currentIndex] = responseItem | 
					
						
							|  |  |  |           })) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onIterationFinish: ({ data }) => { | 
					
						
							|  |  |  |           const tracing = responseItem.workflowProcess!.tracing! | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |           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], | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             ...data, | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |             status: NodeRunningStatus.Succeeded, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           } as any | 
					
						
							|  |  |  |           handleUpdateChatList(produce(chatListRef.current, (draft) => { | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |             const currentIndex = draft.length - 1 | 
					
						
							|  |  |  |             draft[currentIndex] = responseItem | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           })) | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         onNodeStarted: ({ data }) => { | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |           if (data.iteration_id) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           responseItem.workflowProcess!.tracing!.push({ | 
					
						
							|  |  |  |             ...data, | 
					
						
							|  |  |  |             status: NodeRunningStatus.Running, | 
					
						
							|  |  |  |           } as any) | 
					
						
							|  |  |  |           handleUpdateChatList(produce(chatListRef.current, (draft) => { | 
					
						
							|  |  |  |             const currentIndex = draft.findIndex(item => item.id === responseItem.id) | 
					
						
							|  |  |  |             draft[currentIndex] = { | 
					
						
							|  |  |  |               ...draft[currentIndex], | 
					
						
							|  |  |  |               ...responseItem, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })) | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         onNodeFinished: ({ data }) => { | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |           if (data.iteration_id) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           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 } | 
					
						
							|  |  |  |               : {}), | 
					
						
							|  |  |  |             ...data, | 
					
						
							|  |  |  |           } as any | 
					
						
							|  |  |  |           handleUpdateChatList(produce(chatListRef.current, (draft) => { | 
					
						
							|  |  |  |             const currentIndex = draft.findIndex(item => item.id === responseItem.id) | 
					
						
							|  |  |  |             draft[currentIndex] = { | 
					
						
							|  |  |  |               ...draft[currentIndex], | 
					
						
							|  |  |  |               ...responseItem, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   }, [handleRun, handleResponding, handleUpdateChatList, notify, t, updateCurrentQA, config.suggested_questions_after_answer?.enabled, formSettings]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2024-08-06 13:33:21 +08:00
										 |  |  |     conversationId: conversationId.current, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     chatList, | 
					
						
							| 
									
										
										
										
											2024-09-22 03:15:11 +08:00
										 |  |  |     chatListRef, | 
					
						
							|  |  |  |     handleUpdateChatList, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     handleSend, | 
					
						
							|  |  |  |     handleStop, | 
					
						
							|  |  |  |     handleRestart, | 
					
						
							|  |  |  |     isResponding, | 
					
						
							|  |  |  |     suggestedQuestions, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |