You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. import {
  2. useCallback,
  3. useEffect,
  4. useMemo,
  5. useRef,
  6. useState,
  7. } from 'react'
  8. import { useTranslation } from 'react-i18next'
  9. import { produce, setAutoFreeze } from 'immer'
  10. import { uniqBy } from 'lodash-es'
  11. import { useWorkflowRun } from '../../hooks'
  12. import { NodeRunningStatus, WorkflowRunningStatus } from '../../types'
  13. import { useWorkflowStore } from '../../store'
  14. import { DEFAULT_ITER_TIMES, DEFAULT_LOOP_TIMES } from '../../constants'
  15. import type {
  16. ChatItem,
  17. ChatItemInTree,
  18. Inputs,
  19. } from '@/app/components/base/chat/types'
  20. import type { InputForm } from '@/app/components/base/chat/chat/type'
  21. import {
  22. getProcessedInputs,
  23. processOpeningStatement,
  24. } from '@/app/components/base/chat/chat/utils'
  25. import { useToastContext } from '@/app/components/base/toast'
  26. import { TransferMethod } from '@/types/app'
  27. import {
  28. getProcessedFiles,
  29. getProcessedFilesFromResponse,
  30. } from '@/app/components/base/file-uploader/utils'
  31. import type { FileEntity } from '@/app/components/base/file-uploader/types'
  32. import { getThreadMessages } from '@/app/components/base/chat/utils'
  33. import { useInvalidAllLastRun } from '@/service/use-workflow'
  34. import { useParams } from 'next/navigation'
  35. import useSetWorkflowVarsWithValue from '@/app/components/workflow-app/hooks/use-fetch-workflow-inspect-vars'
  36. type GetAbortController = (abortController: AbortController) => void
  37. type SendCallback = {
  38. onGetSuggestedQuestions?: (responseItemId: string, getAbortController: GetAbortController) => Promise<any>
  39. }
  40. export const useChat = (
  41. config: any,
  42. formSettings?: {
  43. inputs: Inputs
  44. inputsForm: InputForm[]
  45. },
  46. prevChatTree?: ChatItemInTree[],
  47. stopChat?: (taskId: string) => void,
  48. ) => {
  49. const { t } = useTranslation()
  50. const { notify } = useToastContext()
  51. const { handleRun } = useWorkflowRun()
  52. const hasStopResponded = useRef(false)
  53. const workflowStore = useWorkflowStore()
  54. const conversationId = useRef('')
  55. const taskIdRef = useRef('')
  56. const [isResponding, setIsResponding] = useState(false)
  57. const isRespondingRef = useRef(false)
  58. const { appId } = useParams()
  59. const invalidAllLastRun = useInvalidAllLastRun(appId as string)
  60. const { fetchInspectVars } = useSetWorkflowVarsWithValue()
  61. const [suggestedQuestions, setSuggestQuestions] = useState<string[]>([])
  62. const suggestedQuestionsAbortControllerRef = useRef<AbortController | null>(null)
  63. const {
  64. setIterTimes,
  65. setLoopTimes,
  66. } = workflowStore.getState()
  67. const handleResponding = useCallback((isResponding: boolean) => {
  68. setIsResponding(isResponding)
  69. isRespondingRef.current = isResponding
  70. }, [])
  71. const [chatTree, setChatTree] = useState<ChatItemInTree[]>(prevChatTree || [])
  72. const chatTreeRef = useRef<ChatItemInTree[]>(chatTree)
  73. const [targetMessageId, setTargetMessageId] = useState<string>()
  74. const threadMessages = useMemo(() => getThreadMessages(chatTree, targetMessageId), [chatTree, targetMessageId])
  75. const getIntroduction = useCallback((str: string) => {
  76. return processOpeningStatement(str, formSettings?.inputs || {}, formSettings?.inputsForm || [])
  77. }, [formSettings?.inputs, formSettings?.inputsForm])
  78. /** Final chat list that will be rendered */
  79. const chatList = useMemo(() => {
  80. const ret = [...threadMessages]
  81. if (config?.opening_statement) {
  82. const index = threadMessages.findIndex(item => item.isOpeningStatement)
  83. if (index > -1) {
  84. ret[index] = {
  85. ...ret[index],
  86. content: getIntroduction(config.opening_statement),
  87. suggestedQuestions: config.suggested_questions,
  88. }
  89. }
  90. else {
  91. ret.unshift({
  92. id: `${Date.now()}`,
  93. content: getIntroduction(config.opening_statement),
  94. isAnswer: true,
  95. isOpeningStatement: true,
  96. suggestedQuestions: config.suggested_questions,
  97. })
  98. }
  99. }
  100. return ret
  101. }, [threadMessages, config?.opening_statement, getIntroduction, config?.suggested_questions])
  102. useEffect(() => {
  103. setAutoFreeze(false)
  104. return () => {
  105. setAutoFreeze(true)
  106. }
  107. }, [])
  108. /** Find the target node by bfs and then operate on it */
  109. const produceChatTreeNode = useCallback((targetId: string, operation: (node: ChatItemInTree) => void) => {
  110. return produce(chatTreeRef.current, (draft) => {
  111. const queue: ChatItemInTree[] = [...draft]
  112. while (queue.length > 0) {
  113. const current = queue.shift()!
  114. if (current.id === targetId) {
  115. operation(current)
  116. break
  117. }
  118. if (current.children)
  119. queue.push(...current.children)
  120. }
  121. })
  122. }, [])
  123. const handleStop = useCallback(() => {
  124. hasStopResponded.current = true
  125. handleResponding(false)
  126. if (stopChat && taskIdRef.current)
  127. stopChat(taskIdRef.current)
  128. setIterTimes(DEFAULT_ITER_TIMES)
  129. setLoopTimes(DEFAULT_LOOP_TIMES)
  130. if (suggestedQuestionsAbortControllerRef.current)
  131. suggestedQuestionsAbortControllerRef.current.abort()
  132. }, [handleResponding, setIterTimes, setLoopTimes, stopChat])
  133. const handleRestart = useCallback(() => {
  134. conversationId.current = ''
  135. taskIdRef.current = ''
  136. handleStop()
  137. setIterTimes(DEFAULT_ITER_TIMES)
  138. setLoopTimes(DEFAULT_LOOP_TIMES)
  139. setChatTree([])
  140. setSuggestQuestions([])
  141. }, [
  142. handleStop,
  143. setIterTimes,
  144. setLoopTimes,
  145. ])
  146. const updateCurrentQAOnTree = useCallback(({
  147. parentId,
  148. responseItem,
  149. placeholderQuestionId,
  150. questionItem,
  151. }: {
  152. parentId?: string
  153. responseItem: ChatItem
  154. placeholderQuestionId: string
  155. questionItem: ChatItem
  156. }) => {
  157. let nextState: ChatItemInTree[]
  158. const currentQA = { ...questionItem, children: [{ ...responseItem, children: [] }] }
  159. if (!parentId && !chatTree.some(item => [placeholderQuestionId, questionItem.id].includes(item.id))) {
  160. // QA whose parent is not provided is considered as a first message of the conversation,
  161. // and it should be a root node of the chat tree
  162. nextState = produce(chatTree, (draft) => {
  163. draft.push(currentQA)
  164. })
  165. }
  166. else {
  167. // find the target QA in the tree and update it; if not found, insert it to its parent node
  168. nextState = produceChatTreeNode(parentId!, (parentNode) => {
  169. const questionNodeIndex = parentNode.children!.findIndex(item => [placeholderQuestionId, questionItem.id].includes(item.id))
  170. if (questionNodeIndex === -1)
  171. parentNode.children!.push(currentQA)
  172. else
  173. parentNode.children![questionNodeIndex] = currentQA
  174. })
  175. }
  176. setChatTree(nextState)
  177. chatTreeRef.current = nextState
  178. }, [chatTree, produceChatTreeNode])
  179. const handleSend = useCallback((
  180. params: {
  181. query: string
  182. files?: FileEntity[]
  183. parent_message_id?: string
  184. [key: string]: any
  185. },
  186. {
  187. onGetSuggestedQuestions,
  188. }: SendCallback,
  189. ) => {
  190. if (isRespondingRef.current) {
  191. notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') })
  192. return false
  193. }
  194. const parentMessage = threadMessages.find(item => item.id === params.parent_message_id)
  195. const placeholderQuestionId = `question-${Date.now()}`
  196. const questionItem = {
  197. id: placeholderQuestionId,
  198. content: params.query,
  199. isAnswer: false,
  200. message_files: params.files,
  201. parentMessageId: params.parent_message_id,
  202. }
  203. const placeholderAnswerId = `answer-placeholder-${Date.now()}`
  204. const placeholderAnswerItem = {
  205. id: placeholderAnswerId,
  206. content: '',
  207. isAnswer: true,
  208. parentMessageId: questionItem.id,
  209. siblingIndex: parentMessage?.children?.length ?? chatTree.length,
  210. }
  211. setTargetMessageId(parentMessage?.id)
  212. updateCurrentQAOnTree({
  213. parentId: params.parent_message_id,
  214. responseItem: placeholderAnswerItem,
  215. placeholderQuestionId,
  216. questionItem,
  217. })
  218. // answer
  219. const responseItem: ChatItem = {
  220. id: placeholderAnswerId,
  221. content: '',
  222. agent_thoughts: [],
  223. message_files: [],
  224. isAnswer: true,
  225. parentMessageId: questionItem.id,
  226. siblingIndex: parentMessage?.children?.length ?? chatTree.length,
  227. }
  228. handleResponding(true)
  229. const { files, inputs, ...restParams } = params
  230. const bodyParams = {
  231. files: getProcessedFiles(files || []),
  232. inputs: getProcessedInputs(inputs || {}, formSettings?.inputsForm || []),
  233. ...restParams,
  234. }
  235. if (bodyParams?.files?.length) {
  236. bodyParams.files = bodyParams.files.map((item) => {
  237. if (item.transfer_method === TransferMethod.local_file) {
  238. return {
  239. ...item,
  240. url: '',
  241. }
  242. }
  243. return item
  244. })
  245. }
  246. let hasSetResponseId = false
  247. handleRun(
  248. bodyParams,
  249. {
  250. onData: (message: string, isFirstMessage: boolean, { conversationId: newConversationId, messageId, taskId }: any) => {
  251. responseItem.content = responseItem.content + message
  252. if (messageId && !hasSetResponseId) {
  253. questionItem.id = `question-${messageId}`
  254. responseItem.id = messageId
  255. responseItem.parentMessageId = questionItem.id
  256. hasSetResponseId = true
  257. }
  258. if (isFirstMessage && newConversationId)
  259. conversationId.current = newConversationId
  260. taskIdRef.current = taskId
  261. if (messageId)
  262. responseItem.id = messageId
  263. updateCurrentQAOnTree({
  264. placeholderQuestionId,
  265. questionItem,
  266. responseItem,
  267. parentId: params.parent_message_id,
  268. })
  269. },
  270. async onCompleted(hasError?: boolean, errorMessage?: string) {
  271. handleResponding(false)
  272. fetchInspectVars()
  273. invalidAllLastRun()
  274. if (hasError) {
  275. if (errorMessage) {
  276. responseItem.content = errorMessage
  277. responseItem.isError = true
  278. updateCurrentQAOnTree({
  279. placeholderQuestionId,
  280. questionItem,
  281. responseItem,
  282. parentId: params.parent_message_id,
  283. })
  284. }
  285. return
  286. }
  287. if (config?.suggested_questions_after_answer?.enabled && !hasStopResponded.current && onGetSuggestedQuestions) {
  288. try {
  289. const { data }: any = await onGetSuggestedQuestions(
  290. responseItem.id,
  291. newAbortController => suggestedQuestionsAbortControllerRef.current = newAbortController,
  292. )
  293. setSuggestQuestions(data)
  294. }
  295. // eslint-disable-next-line unused-imports/no-unused-vars
  296. catch (error) {
  297. setSuggestQuestions([])
  298. }
  299. }
  300. },
  301. onMessageEnd: (messageEnd) => {
  302. responseItem.citation = messageEnd.metadata?.retriever_resources || []
  303. const processedFilesFromResponse = getProcessedFilesFromResponse(messageEnd.files || [])
  304. responseItem.allFiles = uniqBy([...(responseItem.allFiles || []), ...(processedFilesFromResponse || [])], 'id')
  305. updateCurrentQAOnTree({
  306. placeholderQuestionId,
  307. questionItem,
  308. responseItem,
  309. parentId: params.parent_message_id,
  310. })
  311. },
  312. onMessageReplace: (messageReplace) => {
  313. responseItem.content = messageReplace.answer
  314. },
  315. onError() {
  316. handleResponding(false)
  317. },
  318. onWorkflowStarted: ({ workflow_run_id, task_id }) => {
  319. taskIdRef.current = task_id
  320. responseItem.workflow_run_id = workflow_run_id
  321. responseItem.workflowProcess = {
  322. status: WorkflowRunningStatus.Running,
  323. tracing: [],
  324. }
  325. updateCurrentQAOnTree({
  326. placeholderQuestionId,
  327. questionItem,
  328. responseItem,
  329. parentId: params.parent_message_id,
  330. })
  331. },
  332. onWorkflowFinished: ({ data }) => {
  333. responseItem.workflowProcess!.status = data.status as WorkflowRunningStatus
  334. updateCurrentQAOnTree({
  335. placeholderQuestionId,
  336. questionItem,
  337. responseItem,
  338. parentId: params.parent_message_id,
  339. })
  340. },
  341. onIterationStart: ({ data }) => {
  342. responseItem.workflowProcess!.tracing!.push({
  343. ...data,
  344. status: NodeRunningStatus.Running,
  345. })
  346. updateCurrentQAOnTree({
  347. placeholderQuestionId,
  348. questionItem,
  349. responseItem,
  350. parentId: params.parent_message_id,
  351. })
  352. },
  353. onIterationFinish: ({ data }) => {
  354. const currentTracingIndex = responseItem.workflowProcess!.tracing!.findIndex(item => item.id === data.id)
  355. if (currentTracingIndex > -1) {
  356. responseItem.workflowProcess!.tracing[currentTracingIndex] = {
  357. ...responseItem.workflowProcess!.tracing[currentTracingIndex],
  358. ...data,
  359. }
  360. updateCurrentQAOnTree({
  361. placeholderQuestionId,
  362. questionItem,
  363. responseItem,
  364. parentId: params.parent_message_id,
  365. })
  366. }
  367. },
  368. onLoopStart: ({ data }) => {
  369. responseItem.workflowProcess!.tracing!.push({
  370. ...data,
  371. status: NodeRunningStatus.Running,
  372. })
  373. updateCurrentQAOnTree({
  374. placeholderQuestionId,
  375. questionItem,
  376. responseItem,
  377. parentId: params.parent_message_id,
  378. })
  379. },
  380. onLoopFinish: ({ data }) => {
  381. const currentTracingIndex = responseItem.workflowProcess!.tracing!.findIndex(item => item.id === data.id)
  382. if (currentTracingIndex > -1) {
  383. responseItem.workflowProcess!.tracing[currentTracingIndex] = {
  384. ...responseItem.workflowProcess!.tracing[currentTracingIndex],
  385. ...data,
  386. }
  387. updateCurrentQAOnTree({
  388. placeholderQuestionId,
  389. questionItem,
  390. responseItem,
  391. parentId: params.parent_message_id,
  392. })
  393. }
  394. },
  395. onNodeStarted: ({ data }) => {
  396. responseItem.workflowProcess!.tracing!.push({
  397. ...data,
  398. status: NodeRunningStatus.Running,
  399. } as any)
  400. updateCurrentQAOnTree({
  401. placeholderQuestionId,
  402. questionItem,
  403. responseItem,
  404. parentId: params.parent_message_id,
  405. })
  406. },
  407. onNodeRetry: ({ data }) => {
  408. responseItem.workflowProcess!.tracing!.push(data)
  409. updateCurrentQAOnTree({
  410. placeholderQuestionId,
  411. questionItem,
  412. responseItem,
  413. parentId: params.parent_message_id,
  414. })
  415. },
  416. onNodeFinished: ({ data }) => {
  417. const currentTracingIndex = responseItem.workflowProcess!.tracing!.findIndex(item => item.id === data.id)
  418. if (currentTracingIndex > -1) {
  419. responseItem.workflowProcess!.tracing[currentTracingIndex] = {
  420. ...responseItem.workflowProcess!.tracing[currentTracingIndex],
  421. ...data,
  422. }
  423. updateCurrentQAOnTree({
  424. placeholderQuestionId,
  425. questionItem,
  426. responseItem,
  427. parentId: params.parent_message_id,
  428. })
  429. }
  430. },
  431. onAgentLog: ({ data }) => {
  432. const currentNodeIndex = responseItem.workflowProcess!.tracing!.findIndex(item => item.node_id === data.node_id)
  433. if (currentNodeIndex > -1) {
  434. const current = responseItem.workflowProcess!.tracing![currentNodeIndex]
  435. if (current.execution_metadata) {
  436. if (current.execution_metadata.agent_log) {
  437. const currentLogIndex = current.execution_metadata.agent_log.findIndex(log => log.id === data.id)
  438. if (currentLogIndex > -1) {
  439. current.execution_metadata.agent_log[currentLogIndex] = {
  440. ...current.execution_metadata.agent_log[currentLogIndex],
  441. ...data,
  442. }
  443. }
  444. else {
  445. current.execution_metadata.agent_log.push(data)
  446. }
  447. }
  448. else {
  449. current.execution_metadata.agent_log = [data]
  450. }
  451. }
  452. else {
  453. current.execution_metadata = {
  454. agent_log: [data],
  455. } as any
  456. }
  457. responseItem.workflowProcess!.tracing[currentNodeIndex] = {
  458. ...current,
  459. }
  460. updateCurrentQAOnTree({
  461. placeholderQuestionId,
  462. questionItem,
  463. responseItem,
  464. parentId: params.parent_message_id,
  465. })
  466. }
  467. },
  468. },
  469. )
  470. }, [threadMessages, chatTree.length, updateCurrentQAOnTree, handleResponding, formSettings?.inputsForm, handleRun, notify, t, config?.suggested_questions_after_answer?.enabled])
  471. return {
  472. conversationId: conversationId.current,
  473. chatList,
  474. setTargetMessageId,
  475. handleSend,
  476. handleStop,
  477. handleRestart,
  478. isResponding,
  479. suggestedQuestions,
  480. }
  481. }