ThinkingContent.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useEffect, useRef } from 'react';
  16. import { Typography } from '@douyinfe/semi-ui';
  17. import MarkdownRenderer from '../common/markdown/MarkdownRenderer';
  18. import { ChevronRight, ChevronUp, Brain, Loader2 } from 'lucide-react';
  19. import { useTranslation } from 'react-i18next';
  20. const ThinkingContent = ({
  21. message,
  22. finalExtractedThinkingContent,
  23. thinkingSource,
  24. styleState,
  25. onToggleReasoningExpansion,
  26. }) => {
  27. const { t } = useTranslation();
  28. const scrollRef = useRef(null);
  29. const lastContentRef = useRef('');
  30. const isThinkingStatus =
  31. message.status === 'loading' || message.status === 'incomplete';
  32. const headerText =
  33. isThinkingStatus && !message.isThinkingComplete
  34. ? t('思考中...')
  35. : t('思考过程');
  36. useEffect(() => {
  37. if (
  38. scrollRef.current &&
  39. finalExtractedThinkingContent &&
  40. message.isReasoningExpanded
  41. ) {
  42. scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
  43. }
  44. }, [finalExtractedThinkingContent, message.isReasoningExpanded]);
  45. useEffect(() => {
  46. if (!isThinkingStatus) {
  47. lastContentRef.current = '';
  48. }
  49. }, [isThinkingStatus]);
  50. if (!finalExtractedThinkingContent) return null;
  51. let prevLength = 0;
  52. if (isThinkingStatus && lastContentRef.current) {
  53. if (finalExtractedThinkingContent.startsWith(lastContentRef.current)) {
  54. prevLength = lastContentRef.current.length;
  55. }
  56. }
  57. if (isThinkingStatus) {
  58. lastContentRef.current = finalExtractedThinkingContent;
  59. }
  60. return (
  61. <div className='rounded-xl sm:rounded-2xl mb-2 sm:mb-4 overflow-hidden shadow-sm backdrop-blur-sm'>
  62. <div
  63. className='flex items-center justify-between p-3 cursor-pointer hover:bg-gradient-to-r hover:from-white/20 hover:to-purple-50/30 transition-all'
  64. style={{
  65. background:
  66. 'linear-gradient(135deg, #4c1d95 0%, #6d28d9 50%, #7c3aed 100%)',
  67. position: 'relative',
  68. }}
  69. onClick={() => onToggleReasoningExpansion(message.id)}
  70. >
  71. <div className='absolute inset-0 overflow-hidden'>
  72. <div className='absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full'></div>
  73. <div className='absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full'></div>
  74. </div>
  75. <div className='flex items-center gap-2 sm:gap-4 relative'>
  76. <div className='w-6 h-6 sm:w-8 sm:h-8 rounded-full bg-white/20 flex items-center justify-center shadow-lg'>
  77. <Brain
  78. style={{ color: 'white' }}
  79. size={styleState.isMobile ? 12 : 16}
  80. />
  81. </div>
  82. <div className='flex flex-col'>
  83. <Typography.Text
  84. strong
  85. style={{ color: 'white' }}
  86. className='text-sm sm:text-base'
  87. >
  88. {headerText}
  89. </Typography.Text>
  90. {thinkingSource && (
  91. <Typography.Text
  92. style={{ color: 'white' }}
  93. className='text-xs mt-0.5 opacity-80 hidden sm:block'
  94. >
  95. 来源: {thinkingSource}
  96. </Typography.Text>
  97. )}
  98. </div>
  99. </div>
  100. <div className='flex items-center gap-2 sm:gap-3 relative'>
  101. {isThinkingStatus && !message.isThinkingComplete && (
  102. <div className='flex items-center gap-1 sm:gap-2'>
  103. <Loader2
  104. style={{ color: 'white' }}
  105. className='animate-spin'
  106. size={styleState.isMobile ? 14 : 18}
  107. />
  108. <Typography.Text
  109. style={{ color: 'white' }}
  110. className='text-xs sm:text-sm font-medium opacity-90'
  111. >
  112. 思考中
  113. </Typography.Text>
  114. </div>
  115. )}
  116. {(!isThinkingStatus || message.isThinkingComplete) && (
  117. <div className='w-5 h-5 sm:w-6 sm:h-6 rounded-full bg-white/20 flex items-center justify-center'>
  118. {message.isReasoningExpanded ? (
  119. <ChevronUp
  120. size={styleState.isMobile ? 12 : 16}
  121. style={{ color: 'white' }}
  122. />
  123. ) : (
  124. <ChevronRight
  125. size={styleState.isMobile ? 12 : 16}
  126. style={{ color: 'white' }}
  127. />
  128. )}
  129. </div>
  130. )}
  131. </div>
  132. </div>
  133. <div
  134. className={`transition-all duration-500 ease-out ${
  135. message.isReasoningExpanded
  136. ? 'max-h-96 opacity-100'
  137. : 'max-h-0 opacity-0'
  138. } overflow-hidden bg-gradient-to-br from-purple-50 via-indigo-50 to-violet-50`}
  139. >
  140. {message.isReasoningExpanded && (
  141. <div className='p-3 sm:p-5 pt-2 sm:pt-4'>
  142. <div
  143. ref={scrollRef}
  144. className='bg-white/70 backdrop-blur-sm rounded-lg sm:rounded-xl p-2 shadow-inner overflow-x-auto overflow-y-auto thinking-content-scroll'
  145. style={{
  146. maxHeight: '200px',
  147. scrollbarWidth: 'thin',
  148. scrollbarColor: 'rgba(0, 0, 0, 0.3) transparent',
  149. }}
  150. >
  151. <div className='prose prose-xs sm:prose-sm prose-purple max-w-none text-xs sm:text-sm'>
  152. <MarkdownRenderer
  153. content={finalExtractedThinkingContent}
  154. className=''
  155. animated={isThinkingStatus}
  156. previousContentLength={prevLength}
  157. />
  158. </div>
  159. </div>
  160. </div>
  161. )}
  162. </div>
  163. </div>
  164. );
  165. };
  166. export default ThinkingContent;