chat-wrapper.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { useCallback, useEffect, useMemo } from 'react'
  2. import Chat from '../chat'
  3. import type {
  4. ChatConfig,
  5. ChatItem,
  6. OnSend,
  7. } from '../types'
  8. import { useChat } from '../chat/hooks'
  9. import { useEmbeddedChatbotContext } from './context'
  10. import ConfigPanel from './config-panel'
  11. import { isDify } from './utils'
  12. import cn from '@/utils/classnames'
  13. import {
  14. fetchSuggestedQuestions,
  15. getUrl,
  16. stopChatMessageResponding,
  17. } from '@/service/share'
  18. import LogoAvatar from '@/app/components/base/logo/logo-embedded-chat-avatar'
  19. import AnswerIcon from '@/app/components/base/answer-icon'
  20. const ChatWrapper = () => {
  21. const {
  22. appData,
  23. appParams,
  24. appPrevChatList,
  25. currentConversationId,
  26. currentConversationItem,
  27. inputsForms,
  28. newConversationInputs,
  29. handleNewConversationCompleted,
  30. isMobile,
  31. isInstalledApp,
  32. appId,
  33. appMeta,
  34. handleFeedback,
  35. currentChatInstanceRef,
  36. themeBuilder,
  37. } = useEmbeddedChatbotContext()
  38. const appConfig = useMemo(() => {
  39. const config = appParams || {}
  40. return {
  41. ...config,
  42. supportFeedback: true,
  43. opening_statement: currentConversationId ? currentConversationItem?.introduction : (config as any).opening_statement,
  44. } as ChatConfig
  45. }, [appParams, currentConversationItem?.introduction, currentConversationId])
  46. const {
  47. chatListRef,
  48. chatList,
  49. handleSend,
  50. handleStop,
  51. isResponding,
  52. suggestedQuestions,
  53. handleUpdateChatList,
  54. } = useChat(
  55. appConfig,
  56. {
  57. inputs: (currentConversationId ? currentConversationItem?.inputs : newConversationInputs) as any,
  58. promptVariables: inputsForms,
  59. },
  60. appPrevChatList,
  61. taskId => stopChatMessageResponding('', taskId, isInstalledApp, appId),
  62. )
  63. useEffect(() => {
  64. if (currentChatInstanceRef.current)
  65. currentChatInstanceRef.current.handleStop = handleStop
  66. }, [])
  67. const doSend: OnSend = useCallback((message, files, last_answer) => {
  68. const data: any = {
  69. query: message,
  70. inputs: currentConversationId ? currentConversationItem?.inputs : newConversationInputs,
  71. conversation_id: currentConversationId,
  72. parent_message_id: last_answer?.id || chatListRef.current.at(-1)?.id || null,
  73. }
  74. if (appConfig?.file_upload?.image.enabled && files?.length)
  75. data.files = files
  76. handleSend(
  77. getUrl('chat-messages', isInstalledApp, appId || ''),
  78. data,
  79. {
  80. onGetSuggestedQuestions: responseItemId => fetchSuggestedQuestions(responseItemId, isInstalledApp, appId),
  81. onConversationComplete: currentConversationId ? undefined : handleNewConversationCompleted,
  82. isPublicAPI: !isInstalledApp,
  83. },
  84. )
  85. }, [
  86. chatListRef,
  87. appConfig,
  88. currentConversationId,
  89. currentConversationItem,
  90. handleSend,
  91. newConversationInputs,
  92. handleNewConversationCompleted,
  93. isInstalledApp,
  94. appId,
  95. ])
  96. const doRegenerate = useCallback((chatItem: ChatItem) => {
  97. const index = chatList.findIndex(item => item.id === chatItem.id)
  98. if (index === -1)
  99. return
  100. const prevMessages = chatList.slice(0, index)
  101. const question = prevMessages.pop()
  102. const lastAnswer = prevMessages.at(-1)
  103. if (!question)
  104. return
  105. handleUpdateChatList(prevMessages)
  106. doSend(question.content, question.message_files, (!lastAnswer || lastAnswer.isOpeningStatement) ? undefined : lastAnswer)
  107. }, [chatList, handleUpdateChatList, doSend])
  108. const chatNode = useMemo(() => {
  109. if (inputsForms.length) {
  110. return (
  111. <>
  112. {!currentConversationId && (
  113. <div className={cn('mx-auto w-full max-w-full tablet:px-4', isMobile && 'px-4')}>
  114. <div className='mb-6' />
  115. <ConfigPanel />
  116. <div
  117. className='my-6 h-[1px]'
  118. style={{ background: 'linear-gradient(90deg, rgba(242, 244, 247, 0.00) 0%, #F2F4F7 49.17%, rgba(242, 244, 247, 0.00) 100%)' }}
  119. />
  120. </div>
  121. )}
  122. </>
  123. )
  124. }
  125. return null
  126. }, [currentConversationId, inputsForms, isMobile])
  127. const answerIcon = isDify()
  128. ? <LogoAvatar className='relative shrink-0' />
  129. : (appData?.site && appData.site.use_icon_as_answer_icon)
  130. ? <AnswerIcon
  131. iconType={appData.site.icon_type}
  132. icon={appData.site.icon}
  133. background={appData.site.icon_background}
  134. imageUrl={appData.site.icon_url}
  135. />
  136. : null
  137. return (
  138. <Chat
  139. appData={appData}
  140. config={appConfig}
  141. chatList={chatList}
  142. isResponding={isResponding}
  143. chatContainerInnerClassName={cn('mx-auto w-full max-w-full tablet:px-4', isMobile && 'px-4')}
  144. chatFooterClassName='pb-4'
  145. chatFooterInnerClassName={cn('mx-auto w-full max-w-full tablet:px-4', isMobile && 'px-4')}
  146. onSend={doSend}
  147. onRegenerate={doRegenerate}
  148. onStopResponding={handleStop}
  149. chatNode={chatNode}
  150. allToolIcons={appMeta?.tool_icons || {}}
  151. onFeedback={handleFeedback}
  152. suggestedQuestions={suggestedQuestions}
  153. answerIcon={answerIcon}
  154. hideProcessDetail
  155. themeBuilder={themeBuilder}
  156. />
  157. )
  158. }
  159. export default ChatWrapper