index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 Operation from './operation'
  12. import AgentContent from './agent-content'
  13. import BasicContent from './basic-content'
  14. import SuggestedQuestions from './suggested-questions'
  15. import More from './more'
  16. import WorkflowProcess from './workflow-process'
  17. import { AnswerTriangle } from '@/app/components/base/icons/src/vender/solid/general'
  18. import LoadingAnim from '@/app/components/base/chat/chat/loading-anim'
  19. import Citation from '@/app/components/base/chat/chat/citation'
  20. import { EditTitle } from '@/app/components/app/annotation/edit-annotation-modal/edit-item'
  21. import type { Emoji } from '@/app/components/tools/types'
  22. import type { AppData } from '@/models/share'
  23. import AnswerIcon from '@/app/components/base/answer-icon'
  24. import cn from '@/utils/classnames'
  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, setContainerWidth] = useState(0)
  62. const [contentWidth, setContentWidth] = useState(0)
  63. const containerRef = useRef<HTMLDivElement>(null)
  64. const contentRef = useRef<HTMLDivElement>(null)
  65. const getContainerWidth = () => {
  66. if (containerRef.current)
  67. setContainerWidth(containerRef.current?.clientWidth + 16)
  68. }
  69. useEffect(() => {
  70. getContainerWidth()
  71. }, [])
  72. const getContentWidth = () => {
  73. if (contentRef.current)
  74. setContentWidth(contentRef.current?.clientWidth)
  75. }
  76. useEffect(() => {
  77. if (!responding)
  78. getContentWidth()
  79. }, [responding])
  80. return (
  81. <div className='flex mb-2 last:mb-0'>
  82. <div className='shrink-0 relative w-10 h-10'>
  83. {answerIcon || <AnswerIcon />}
  84. {responding && (
  85. <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'>
  86. <LoadingAnim type='avatar' />
  87. </div>
  88. )}
  89. </div>
  90. <div className='chat-answer-container group grow w-0 ml-4' ref={containerRef}>
  91. <div className={cn('group relative pr-10', chatAnswerContainerInner)}>
  92. <AnswerTriangle className='absolute -left-2 top-0 w-2 h-3 text-gray-100' />
  93. <div
  94. ref={contentRef}
  95. className={cn('relative inline-block px-4 py-3 max-w-full bg-gray-100 rounded-b-2xl rounded-tr-2xl text-sm text-gray-900', workflowProcess && 'w-full')}
  96. >
  97. {
  98. !responding && (
  99. <Operation
  100. hasWorkflowProcess={!!workflowProcess}
  101. maxSize={containerWidth - contentWidth - 4}
  102. contentWidth={contentWidth}
  103. item={item}
  104. question={question}
  105. index={index}
  106. showPromptLog={showPromptLog}
  107. />
  108. )
  109. }
  110. {/** Render the normal steps */}
  111. {
  112. workflowProcess && !hideProcessDetail && (
  113. <WorkflowProcess
  114. data={workflowProcess}
  115. item={item}
  116. hideInfo
  117. hideProcessDetail={hideProcessDetail}
  118. />
  119. )
  120. }
  121. {/** Hide workflow steps by it's settings in siteInfo */}
  122. {
  123. workflowProcess && hideProcessDetail && appData && appData.site.show_workflow_steps && (
  124. <WorkflowProcess
  125. data={workflowProcess}
  126. item={item}
  127. hideInfo
  128. hideProcessDetail={hideProcessDetail}
  129. />
  130. )
  131. }
  132. {
  133. responding && !content && !hasAgentThoughts && (
  134. <div className='flex items-center justify-center w-6 h-5'>
  135. <LoadingAnim type='text' />
  136. </div>
  137. )
  138. }
  139. {
  140. content && !hasAgentThoughts && (
  141. <BasicContent item={item} />
  142. )
  143. }
  144. {
  145. hasAgentThoughts && (
  146. <AgentContent
  147. item={item}
  148. responding={responding}
  149. allToolIcons={allToolIcons}
  150. />
  151. )
  152. }
  153. {
  154. annotation?.id && annotation.authorName && (
  155. <EditTitle
  156. className='mt-1'
  157. title={t('appAnnotation.editBy', { author: annotation.authorName })}
  158. />
  159. )
  160. }
  161. <SuggestedQuestions item={item} />
  162. {
  163. !!citation?.length && !responding && (
  164. <Citation data={citation} showHitInfo={config?.supportCitationHitInfo} />
  165. )
  166. }
  167. </div>
  168. </div>
  169. <More more={more} />
  170. </div>
  171. </div>
  172. )
  173. }
  174. export default memo(Answer)