MessageContent.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import React from 'react';
  2. import {
  3. Typography,
  4. TextArea,
  5. Button,
  6. } from '@douyinfe/semi-ui';
  7. import MarkdownRenderer from '../common/markdown/MarkdownRenderer';
  8. import {
  9. ChevronRight,
  10. ChevronUp,
  11. Brain,
  12. Loader2,
  13. Check,
  14. X,
  15. } from 'lucide-react';
  16. import { useTranslation } from 'react-i18next';
  17. const MessageContent = ({
  18. message,
  19. className,
  20. styleState,
  21. onToggleReasoningExpansion,
  22. isEditing = false,
  23. onEditSave,
  24. onEditCancel,
  25. editValue,
  26. onEditValueChange
  27. }) => {
  28. const { t } = useTranslation();
  29. if (message.status === 'error') {
  30. let errorText;
  31. if (Array.isArray(message.content)) {
  32. const textContent = message.content.find(item => item.type === 'text');
  33. errorText = textContent && textContent.text && typeof textContent.text === 'string'
  34. ? textContent.text
  35. : t('请求发生错误');
  36. } else if (typeof message.content === 'string') {
  37. errorText = message.content;
  38. } else {
  39. errorText = t('请求发生错误');
  40. }
  41. return (
  42. <div className={`${className} flex items-center p-4 bg-red-50 rounded-xl`}>
  43. <Typography.Text type="danger" className="text-sm">
  44. {errorText}
  45. </Typography.Text>
  46. </div>
  47. );
  48. }
  49. const isThinkingStatus = message.status === 'loading' || message.status === 'incomplete';
  50. let currentExtractedThinkingContent = null;
  51. let currentDisplayableFinalContent = "";
  52. let thinkingSource = null;
  53. const getTextContent = (content) => {
  54. if (Array.isArray(content)) {
  55. const textItem = content.find(item => item.type === 'text');
  56. return textItem && textItem.text && typeof textItem.text === 'string' ? textItem.text : '';
  57. } else if (typeof content === 'string') {
  58. return content;
  59. }
  60. return '';
  61. };
  62. currentDisplayableFinalContent = getTextContent(message.content);
  63. if (message.role === 'assistant') {
  64. let baseContentForDisplay = getTextContent(message.content);
  65. let combinedThinkingContent = "";
  66. if (message.reasoningContent) {
  67. combinedThinkingContent = message.reasoningContent;
  68. thinkingSource = 'reasoningContent';
  69. }
  70. if (baseContentForDisplay.includes('<think>')) {
  71. const thinkTagRegex = /<think>([\s\S]*?)<\/think>/g;
  72. let match;
  73. let thoughtsFromPairedTags = [];
  74. let replyParts = [];
  75. let lastIndex = 0;
  76. while ((match = thinkTagRegex.exec(baseContentForDisplay)) !== null) {
  77. replyParts.push(baseContentForDisplay.substring(lastIndex, match.index));
  78. thoughtsFromPairedTags.push(match[1]);
  79. lastIndex = match.index + match[0].length;
  80. }
  81. replyParts.push(baseContentForDisplay.substring(lastIndex));
  82. if (thoughtsFromPairedTags.length > 0) {
  83. const pairedThoughtsStr = thoughtsFromPairedTags.join('\n\n---\n\n');
  84. if (combinedThinkingContent) {
  85. combinedThinkingContent += '\n\n---\n\n' + pairedThoughtsStr;
  86. } else {
  87. combinedThinkingContent = pairedThoughtsStr;
  88. }
  89. thinkingSource = thinkingSource ? thinkingSource + ' & <think> tags' : '<think> tags';
  90. }
  91. baseContentForDisplay = replyParts.join('');
  92. }
  93. if (isThinkingStatus) {
  94. const lastOpenThinkIndex = baseContentForDisplay.lastIndexOf('<think>');
  95. if (lastOpenThinkIndex !== -1) {
  96. const fragmentAfterLastOpen = baseContentForDisplay.substring(lastOpenThinkIndex);
  97. if (!fragmentAfterLastOpen.includes('</think>')) {
  98. const unclosedThought = fragmentAfterLastOpen.substring('<think>'.length).trim();
  99. if (unclosedThought) {
  100. if (combinedThinkingContent) {
  101. combinedThinkingContent += '\n\n---\n\n' + unclosedThought;
  102. } else {
  103. combinedThinkingContent = unclosedThought;
  104. }
  105. thinkingSource = thinkingSource ? thinkingSource + ' + streaming <think>' : 'streaming <think>';
  106. }
  107. baseContentForDisplay = baseContentForDisplay.substring(0, lastOpenThinkIndex);
  108. }
  109. }
  110. }
  111. currentExtractedThinkingContent = combinedThinkingContent || null;
  112. currentDisplayableFinalContent = baseContentForDisplay.replace(/<\/?think>/g, '').trim();
  113. }
  114. const headerText = (isThinkingStatus && !message.isThinkingComplete) ? t('思考中...') : t('思考过程');
  115. const finalExtractedThinkingContent = currentExtractedThinkingContent;
  116. const finalDisplayableFinalContent = currentDisplayableFinalContent;
  117. if (message.role === 'assistant' &&
  118. isThinkingStatus &&
  119. !finalExtractedThinkingContent &&
  120. (!finalDisplayableFinalContent || finalDisplayableFinalContent.trim() === '')) {
  121. return (
  122. <div className={`${className} flex items-center gap-2 sm:gap-4 p-4 sm:p-6 bg-gradient-to-r from-purple-50 to-indigo-50 rounded-xl sm:rounded-2xl`}>
  123. <div className="w-8 h-8 sm:w-10 sm:h-10 rounded-full bg-gradient-to-br from-purple-500 to-indigo-600 flex items-center justify-center shadow-lg">
  124. <Loader2 className="animate-spin text-white" size={styleState.isMobile ? 16 : 20} />
  125. </div>
  126. <div className="flex flex-col">
  127. <Typography.Text strong className="text-gray-800 text-sm sm:text-base">
  128. {t('正在思考...')}
  129. </Typography.Text>
  130. <Typography.Text className="text-gray-500 text-xs sm:text-sm">
  131. AI 正在分析您的问题
  132. </Typography.Text>
  133. </div>
  134. </div>
  135. );
  136. }
  137. return (
  138. <div className={className}>
  139. {/* 为system角色添加特殊标识 */}
  140. {message.role === 'system' && (
  141. <div className="mb-2 sm:mb-4">
  142. <div className="flex items-center gap-2 p-2 sm:p-3 bg-gradient-to-r from-amber-50 to-orange-50 rounded-lg" style={{ border: '1px solid var(--semi-color-border)' }}>
  143. <div className="w-4 h-4 sm:w-5 sm:h-5 rounded-full bg-gradient-to-br from-amber-500 to-orange-600 flex items-center justify-center shadow-sm">
  144. <Typography.Text className="text-white text-xs font-bold">S</Typography.Text>
  145. </div>
  146. <Typography.Text className="text-amber-700 text-xs sm:text-sm font-medium">
  147. {t('系统消息')}
  148. </Typography.Text>
  149. </div>
  150. </div>
  151. )}
  152. {/* 渲染推理内容 */}
  153. {message.role === 'assistant' && finalExtractedThinkingContent && (
  154. <div className="rounded-xl sm:rounded-2xl mb-2 sm:mb-4 overflow-hidden shadow-sm backdrop-blur-sm">
  155. <div
  156. className="flex items-center justify-between p-3 sm:p-5 cursor-pointer hover:bg-gradient-to-r hover:from-white/20 hover:to-purple-50/30 transition-all"
  157. style={{
  158. background: 'linear-gradient(135deg, #4c1d95 0%, #6d28d9 50%, #7c3aed 100%)',
  159. position: 'relative'
  160. }}
  161. onClick={() => onToggleReasoningExpansion(message.id)}
  162. >
  163. <div className="absolute inset-0 overflow-hidden">
  164. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  165. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  166. </div>
  167. <div className="flex items-center gap-2 sm:gap-4 relative">
  168. <div className="w-6 h-6 sm:w-8 sm:h-8 rounded-full bg-white/20 flex items-center justify-center shadow-lg">
  169. <Brain style={{ color: 'white' }} size={styleState.isMobile ? 12 : 16} />
  170. </div>
  171. <div className="flex flex-col">
  172. <Typography.Text strong style={{ color: 'white' }} className="text-sm sm:text-base">
  173. {headerText}
  174. </Typography.Text>
  175. {thinkingSource && (
  176. <Typography.Text style={{ color: 'white' }} className="text-xs mt-0.5 opacity-80 hidden sm:block">
  177. 来源: {thinkingSource}
  178. </Typography.Text>
  179. )}
  180. </div>
  181. </div>
  182. <div className="flex items-center gap-2 sm:gap-3 relative">
  183. {isThinkingStatus && !message.isThinkingComplete && (
  184. <div className="flex items-center gap-1 sm:gap-2">
  185. <Loader2 style={{ color: 'white' }} className="animate-spin" size={styleState.isMobile ? 14 : 18} />
  186. <Typography.Text style={{ color: 'white' }} className="text-xs sm:text-sm font-medium opacity-90">
  187. 思考中
  188. </Typography.Text>
  189. </div>
  190. )}
  191. {(!isThinkingStatus || message.isThinkingComplete) && (
  192. <div className="w-5 h-5 sm:w-6 sm:h-6 rounded-full bg-white/20 flex items-center justify-center">
  193. {message.isReasoningExpanded ?
  194. <ChevronUp size={styleState.isMobile ? 12 : 16} style={{ color: 'white' }} /> :
  195. <ChevronRight size={styleState.isMobile ? 12 : 16} style={{ color: 'white' }} />
  196. }
  197. </div>
  198. )}
  199. </div>
  200. </div>
  201. <div
  202. className={`transition-all duration-500 ease-out ${message.isReasoningExpanded ? 'max-h-96 opacity-100' : 'max-h-0 opacity-0'
  203. } overflow-hidden bg-gradient-to-br from-purple-50 via-indigo-50 to-violet-50`}
  204. >
  205. {message.isReasoningExpanded && (
  206. <div className="p-3 sm:p-5 pt-2 sm:pt-4">
  207. <div
  208. 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"
  209. style={{
  210. maxHeight: '200px',
  211. minHeight: '100px',
  212. scrollbarWidth: 'thin',
  213. scrollbarColor: 'rgba(0, 0, 0, 0.3) transparent'
  214. }}
  215. >
  216. <div className="prose prose-xs sm:prose-sm prose-purple max-w-none text-xs sm:text-sm">
  217. <MarkdownRenderer
  218. content={finalExtractedThinkingContent}
  219. className=""
  220. />
  221. </div>
  222. </div>
  223. </div>
  224. )}
  225. </div>
  226. </div>
  227. )}
  228. {/* 渲染消息内容 */}
  229. {isEditing ? (
  230. /* 编辑模式 */
  231. <div className="space-y-3">
  232. <TextArea
  233. value={editValue}
  234. onChange={(value) => onEditValueChange(value)}
  235. placeholder={t('请输入消息内容...')}
  236. autosize={{ minRows: 3, maxRows: 12 }}
  237. style={{
  238. resize: 'vertical',
  239. fontSize: styleState.isMobile ? '14px' : '15px',
  240. lineHeight: '1.6',
  241. }}
  242. className="!border-blue-200 focus:!border-blue-400 !bg-blue-50/50"
  243. />
  244. <div className="flex items-center gap-2 w-full">
  245. <Button
  246. size="small"
  247. type="danger"
  248. theme="light"
  249. icon={<X size={14} />}
  250. onClick={onEditCancel}
  251. className="flex-1"
  252. >
  253. {t('取消')}
  254. </Button>
  255. <Button
  256. size="small"
  257. type="warning"
  258. theme="solid"
  259. icon={<Check size={14} />}
  260. onClick={onEditSave}
  261. disabled={!editValue || editValue.trim() === ''}
  262. className="flex-1"
  263. >
  264. {t('保存')}
  265. </Button>
  266. </div>
  267. </div>
  268. ) : (
  269. /* 正常显示模式 */
  270. (() => {
  271. if (Array.isArray(message.content)) {
  272. const textContent = message.content.find(item => item.type === 'text');
  273. const imageContents = message.content.filter(item => item.type === 'image_url');
  274. return (
  275. <div>
  276. {/* 显示图片 */}
  277. {imageContents.length > 0 && (
  278. <div className="mb-3 space-y-2">
  279. {imageContents.map((imgItem, index) => (
  280. <div key={index} className="max-w-sm">
  281. <img
  282. src={imgItem.image_url.url}
  283. alt={`用户上传的图片 ${index + 1}`}
  284. className="rounded-lg max-w-full h-auto shadow-sm border"
  285. style={{ maxHeight: '300px' }}
  286. onError={(e) => {
  287. e.target.style.display = 'none';
  288. e.target.nextSibling.style.display = 'block';
  289. }}
  290. />
  291. <div
  292. className="text-red-500 text-sm p-2 bg-red-50 rounded-lg border border-red-200"
  293. style={{ display: 'none' }}
  294. >
  295. 图片加载失败: {imgItem.image_url.url}
  296. </div>
  297. </div>
  298. ))}
  299. </div>
  300. )}
  301. {/* 显示文本内容 */}
  302. {textContent && textContent.text && typeof textContent.text === 'string' && textContent.text.trim() !== '' && (
  303. <div className={`prose prose-xs sm:prose-sm prose-gray max-w-none overflow-x-auto text-xs sm:text-sm ${message.role === 'user' ? 'user-message' : ''}`}>
  304. <MarkdownRenderer
  305. content={textContent.text}
  306. className={message.role === 'user' ? 'user-message' : ''}
  307. />
  308. </div>
  309. )}
  310. </div>
  311. );
  312. }
  313. if (typeof message.content === 'string') {
  314. if (message.role === 'assistant') {
  315. if (finalDisplayableFinalContent && finalDisplayableFinalContent.trim() !== '') {
  316. return (
  317. <div className="prose prose-xs sm:prose-sm prose-gray max-w-none overflow-x-auto text-xs sm:text-sm">
  318. <MarkdownRenderer
  319. content={finalDisplayableFinalContent}
  320. className=""
  321. />
  322. </div>
  323. );
  324. }
  325. } else {
  326. return (
  327. <div className={`prose prose-xs sm:prose-sm prose-gray max-w-none overflow-x-auto text-xs sm:text-sm ${message.role === 'user' ? 'user-message' : ''}`}>
  328. <MarkdownRenderer
  329. content={message.content}
  330. className={message.role === 'user' ? 'user-message' : ''}
  331. />
  332. </div>
  333. );
  334. }
  335. }
  336. return null;
  337. })()
  338. )}
  339. </div>
  340. );
  341. };
  342. export default MessageContent;