index.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import type {
  2. FC,
  3. ReactNode,
  4. } from 'react'
  5. import { memo, useEffect, useRef, useState } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import type {
  8. ChatConfig,
  9. ChatItem,
  10. } from '../../types'
  11. import { useChatContext } from '../context'
  12. import Operation from './operation'
  13. import AgentContent from './agent-content'
  14. import BasicContent from './basic-content'
  15. import SuggestedQuestions from './suggested-questions'
  16. import More from './more'
  17. import WorkflowProcess from './workflow-process'
  18. import { AnswerTriangle } from '@/app/components/base/icons/src/vender/solid/general'
  19. import { MessageFast } from '@/app/components/base/icons/src/vender/solid/communication'
  20. import LoadingAnim from '@/app/components/base/chat/chat/loading-anim'
  21. import Citation from '@/app/components/base/chat/chat/citation'
  22. import { EditTitle } from '@/app/components/app/annotation/edit-annotation-modal/edit-item'
  23. import type { Emoji } from '@/app/components/tools/types'
  24. import type { AppData } from '@/models/share'
  25. type AnswerProps = {
  26. item: ChatItem
  27. question: string
  28. index: number
  29. config?: ChatConfig
  30. answerIcon?: ReactNode
  31. responding?: boolean
  32. allToolIcons?: Record<string, string | Emoji>
  33. showPromptLog?: boolean
  34. chatAnswerContainerInner?: string
  35. hideProcessDetail?: boolean
  36. appData?: AppData
  37. }
  38. const Answer: FC<AnswerProps> = ({
  39. item,
  40. question,
  41. index,
  42. config,
  43. answerIcon,
  44. responding,
  45. allToolIcons,
  46. showPromptLog,
  47. chatAnswerContainerInner,
  48. hideProcessDetail,
  49. appData,
  50. }) => {
  51. const { t } = useTranslation()
  52. const {
  53. content,
  54. citation,
  55. agent_thoughts,
  56. more,
  57. annotation,
  58. workflowProcess,
  59. } = item
  60. const hasAgentThoughts = !!agent_thoughts?.length
  61. const [containerWidth] = useState(0)
  62. const [contentWidth, setContentWidth] = useState(0)
  63. const containerRef = useRef<HTMLDivElement>(null)
  64. const contentRef = useRef<HTMLDivElement>(null)
  65. const {
  66. config: chatContextConfig,
  67. } = useChatContext()
  68. const voiceRef = useRef(chatContextConfig?.text_to_speech?.voice)
  69. const getContentWidth = () => {
  70. if (contentRef.current)
  71. setContentWidth(contentRef.current?.clientWidth)
  72. }
  73. useEffect(() => {
  74. voiceRef.current = chatContextConfig?.text_to_speech?.voice
  75. }
  76. , [chatContextConfig?.text_to_speech?.voice])
  77. useEffect(() => {
  78. if (!responding)
  79. getContentWidth()
  80. }, [responding])
  81. return (
  82. <div className='flex mb-2 last:mb-0'>
  83. <div className='shrink-0 relative w-10 h-10'>
  84. {
  85. answerIcon || (
  86. <div className='flex items-center justify-center w-full h-full rounded-full bg-[#d5f5f6] border-[0.5px] border-black/5 text-xl'>
  87. 🤖
  88. </div>
  89. )
  90. }
  91. {
  92. responding && (
  93. <div className='absolute -top-[3px] -left-[3px] pl-[6px] flex items-center w-4 h-4 bg-white rounded-full shadow-xs border-[0.5px] border-gray-50'>
  94. <LoadingAnim type='avatar' />
  95. </div>
  96. )
  97. }
  98. </div>
  99. <div className='chat-answer-container group grow w-0 ml-4' ref={containerRef}>
  100. <div className={`group relative pr-10 ${chatAnswerContainerInner}`}>
  101. <AnswerTriangle className='absolute -left-2 top-0 w-2 h-3 text-gray-100' />
  102. <div
  103. ref={contentRef}
  104. className={`
  105. relative inline-block px-4 py-3 max-w-full bg-gray-100 rounded-b-2xl rounded-tr-2xl text-sm text-gray-900
  106. ${workflowProcess && 'w-full'}
  107. `}
  108. >
  109. {annotation?.id && (
  110. <div
  111. className='absolute -top-3.5 -right-3.5 box-border flex items-center justify-center h-7 w-7 p-0.5 rounded-lg bg-white cursor-pointer text-[#444CE7] shadow-md group-hover:hidden'
  112. >
  113. <div className='p-1 rounded-lg bg-[#EEF4FF] '>
  114. <MessageFast className='w-4 h-4' />
  115. </div>
  116. </div>
  117. )}
  118. {
  119. !responding && (
  120. <Operation
  121. hasWorkflowProcess={!!workflowProcess}
  122. maxSize={containerWidth - contentWidth - 4}
  123. contentWidth={contentWidth}
  124. item={item}
  125. question={question}
  126. index={index}
  127. showPromptLog={showPromptLog}
  128. />
  129. )
  130. }
  131. {/** Render the normal steps */}
  132. {
  133. workflowProcess && !hideProcessDetail && (
  134. <WorkflowProcess
  135. data={workflowProcess}
  136. item={item}
  137. hideInfo
  138. hideProcessDetail={hideProcessDetail}
  139. />
  140. )
  141. }
  142. {/** Hide workflow steps by it's settings in siteInfo */}
  143. {
  144. workflowProcess && hideProcessDetail && appData && appData.site.show_workflow_steps && (
  145. <WorkflowProcess
  146. data={workflowProcess}
  147. item={item}
  148. hideInfo
  149. hideProcessDetail={hideProcessDetail}
  150. />
  151. )
  152. }
  153. {
  154. responding && !content && !hasAgentThoughts && (
  155. <div className='flex items-center justify-center w-6 h-5'>
  156. <LoadingAnim type='text' />
  157. </div>
  158. )
  159. }
  160. {
  161. content && !hasAgentThoughts && (
  162. <BasicContent item={item} />
  163. )
  164. }
  165. {
  166. hasAgentThoughts && (
  167. <AgentContent
  168. item={item}
  169. responding={responding}
  170. allToolIcons={allToolIcons}
  171. />
  172. )
  173. }
  174. {
  175. annotation?.id && annotation.authorName && (
  176. <EditTitle
  177. className='mt-1'
  178. title={t('appAnnotation.editBy', { author: annotation.authorName })}
  179. />
  180. )
  181. }
  182. <SuggestedQuestions item={item} />
  183. {
  184. !!citation?.length && !responding && (
  185. <Citation data={citation} showHitInfo={config?.supportCitationHitInfo} />
  186. )
  187. }
  188. </div>
  189. </div>
  190. <More more={more} />
  191. </div>
  192. </div>
  193. )
  194. }
  195. export default memo(Answer)