index.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 { MessageFast } from '@/app/components/base/icons/src/vender/solid/communication'
  19. import LoadingAnim from '@/app/components/app/chat/loading-anim'
  20. import Citation from '@/app/components/app/chat/citation'
  21. import { EditTitle } from '@/app/components/app/annotation/edit-annotation-modal/edit-item'
  22. import type { Emoji } from '@/app/components/tools/types'
  23. type AnswerProps = {
  24. item: ChatItem
  25. question: string
  26. index: number
  27. config?: ChatConfig
  28. answerIcon?: ReactNode
  29. responding?: boolean
  30. allToolIcons?: Record<string, string | Emoji>
  31. showPromptLog?: boolean
  32. chatAnswerContainerInner?: string
  33. }
  34. const Answer: FC<AnswerProps> = ({
  35. item,
  36. question,
  37. index,
  38. config,
  39. answerIcon,
  40. responding,
  41. allToolIcons,
  42. showPromptLog,
  43. chatAnswerContainerInner,
  44. }) => {
  45. const { t } = useTranslation()
  46. const {
  47. content,
  48. citation,
  49. agent_thoughts,
  50. more,
  51. annotation,
  52. workflowProcess,
  53. } = item
  54. const hasAgentThoughts = !!agent_thoughts?.length
  55. const [containerWidth, setContainerWidth] = useState(0)
  56. const [contentWidth, setContentWidth] = useState(0)
  57. const containerRef = useRef<HTMLDivElement>(null)
  58. const contentRef = useRef<HTMLDivElement>(null)
  59. const getContainerWidth = () => {
  60. if (containerRef.current)
  61. setContainerWidth(containerRef.current?.clientWidth + 16)
  62. }
  63. const getContentWidth = () => {
  64. if (contentRef.current)
  65. setContentWidth(contentRef.current?.clientWidth)
  66. }
  67. useEffect(() => {
  68. getContainerWidth()
  69. }, [])
  70. useEffect(() => {
  71. if (!responding)
  72. getContentWidth()
  73. }, [responding])
  74. return (
  75. <div className='flex mb-2 last:mb-0'>
  76. <div className='shrink-0 relative w-10 h-10'>
  77. {
  78. answerIcon || (
  79. <div className='flex items-center justify-center w-full h-full rounded-full bg-[#d5f5f6] border-[0.5px] border-black/5 text-xl'>
  80. 🤖
  81. </div>
  82. )
  83. }
  84. {
  85. responding && (
  86. <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'>
  87. <LoadingAnim type='avatar' />
  88. </div>
  89. )
  90. }
  91. </div>
  92. <div className='chat-answer-container grow w-0 ml-4' ref={containerRef}>
  93. <div className={`group relative pr-10 ${chatAnswerContainerInner}`}>
  94. <AnswerTriangle className='absolute -left-2 top-0 w-2 h-3 text-gray-100' />
  95. <div
  96. ref={contentRef}
  97. className={`
  98. relative inline-block px-4 py-3 max-w-full bg-gray-100 rounded-b-2xl rounded-tr-2xl text-sm text-gray-900
  99. ${workflowProcess && 'w-full'}
  100. `}
  101. >
  102. {annotation?.id && (
  103. <div
  104. 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'
  105. >
  106. <div className='p-1 rounded-lg bg-[#EEF4FF] '>
  107. <MessageFast className='w-4 h-4' />
  108. </div>
  109. </div>
  110. )}
  111. {
  112. !responding && (
  113. <Operation
  114. hasWorkflowProcess={!!workflowProcess}
  115. maxSize={containerWidth - contentWidth - 4}
  116. contentWidth={contentWidth}
  117. item={item}
  118. question={question}
  119. index={index}
  120. showPromptLog={showPromptLog}
  121. />
  122. )
  123. }
  124. {
  125. workflowProcess && (
  126. <WorkflowProcess data={workflowProcess} hideInfo />
  127. )
  128. }
  129. {
  130. responding && !content && !hasAgentThoughts && (
  131. <div className='flex items-center justify-center w-6 h-5'>
  132. <LoadingAnim type='text' />
  133. </div>
  134. )
  135. }
  136. {
  137. content && !hasAgentThoughts && (
  138. <BasicContent item={item} />
  139. )
  140. }
  141. {
  142. hasAgentThoughts && (
  143. <AgentContent
  144. item={item}
  145. responding={responding}
  146. allToolIcons={allToolIcons}
  147. />
  148. )
  149. }
  150. {
  151. annotation?.id && annotation.authorName && (
  152. <EditTitle
  153. className='mt-1'
  154. title={t('appAnnotation.editBy', { author: annotation.authorName })}
  155. />
  156. )
  157. }
  158. <SuggestedQuestions item={item} />
  159. {
  160. !!citation?.length && !responding && (
  161. <Citation data={citation} showHitInfo={config?.supportCitationHitInfo} />
  162. )
  163. }
  164. </div>
  165. </div>
  166. <More more={more} />
  167. </div>
  168. </div>
  169. )
  170. }
  171. export default memo(Answer)