MessageContent.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import React from 'react';
  2. import {
  3. Typography,
  4. TextArea,
  5. Button,
  6. } from '@douyinfe/semi-ui';
  7. import MarkdownRenderer from '../common/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="bg-gradient-to-br from-indigo-50 via-purple-50 to-pink-50 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/40 hover:to-purple-50/60 transition-all"
  157. onClick={() => onToggleReasoningExpansion(message.id)}
  158. >
  159. <div className="flex items-center gap-2 sm:gap-4">
  160. <div className="w-6 h-6 sm:w-8 sm:h-8 rounded-full bg-gradient-to-br from-purple-500 to-indigo-600 flex items-center justify-center shadow-lg">
  161. <Brain className="text-white" size={styleState.isMobile ? 12 : 16} />
  162. </div>
  163. <div className="flex flex-col">
  164. <Typography.Text strong className="text-gray-800 text-sm sm:text-base">
  165. {headerText}
  166. </Typography.Text>
  167. {thinkingSource && (
  168. <Typography.Text className="text-gray-500 text-xs mt-0.5 hidden sm:block">
  169. 来源: {thinkingSource}
  170. </Typography.Text>
  171. )}
  172. </div>
  173. </div>
  174. <div className="flex items-center gap-2 sm:gap-3">
  175. {isThinkingStatus && !message.isThinkingComplete && (
  176. <div className="flex items-center gap-1 sm:gap-2">
  177. <Loader2 className="animate-spin text-purple-500" size={styleState.isMobile ? 14 : 18} />
  178. <Typography.Text className="text-purple-600 text-xs sm:text-sm font-medium">
  179. 思考中
  180. </Typography.Text>
  181. </div>
  182. )}
  183. {(!isThinkingStatus || message.isThinkingComplete) && (
  184. <div className="w-5 h-5 sm:w-6 sm:h-6 rounded-full bg-purple-100 flex items-center justify-center">
  185. {message.isReasoningExpanded ?
  186. <ChevronUp size={styleState.isMobile ? 12 : 16} className="text-purple-600" /> :
  187. <ChevronRight size={styleState.isMobile ? 12 : 16} className="text-purple-600" />
  188. }
  189. </div>
  190. )}
  191. </div>
  192. </div>
  193. <div
  194. className={`transition-all duration-500 ease-out ${message.isReasoningExpanded ? 'max-h-96 opacity-100' : 'max-h-0 opacity-0'
  195. } overflow-hidden`}
  196. >
  197. {message.isReasoningExpanded && (
  198. <div className="p-3 sm:p-5 pt-2 sm:pt-4">
  199. <div
  200. 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"
  201. style={{
  202. maxHeight: '200px',
  203. minHeight: '100px',
  204. scrollbarWidth: 'thin',
  205. scrollbarColor: 'rgba(0, 0, 0, 0.3) transparent'
  206. }}
  207. >
  208. <div className="prose prose-xs sm:prose-sm prose-purple max-w-none text-xs sm:text-sm">
  209. <MarkdownRenderer
  210. content={finalExtractedThinkingContent}
  211. className=""
  212. />
  213. </div>
  214. </div>
  215. </div>
  216. )}
  217. </div>
  218. </div>
  219. )}
  220. {/* 渲染消息内容 */}
  221. {isEditing ? (
  222. /* 编辑模式 */
  223. <div className="space-y-3">
  224. <TextArea
  225. value={editValue}
  226. onChange={(value) => onEditValueChange(value)}
  227. placeholder={t('请输入消息内容...')}
  228. autosize={{ minRows: 3, maxRows: 12 }}
  229. style={{
  230. resize: 'vertical',
  231. fontSize: styleState.isMobile ? '14px' : '15px',
  232. lineHeight: '1.6',
  233. }}
  234. className="!border-blue-200 focus:!border-blue-400 !bg-blue-50/50"
  235. />
  236. <div className="flex items-center gap-2 w-full">
  237. <Button
  238. size="small"
  239. type="danger"
  240. theme="light"
  241. icon={<X size={14} />}
  242. onClick={onEditCancel}
  243. className="flex-1"
  244. >
  245. {t('取消')}
  246. </Button>
  247. <Button
  248. size="small"
  249. type="warning"
  250. theme="solid"
  251. icon={<Check size={14} />}
  252. onClick={onEditSave}
  253. disabled={!editValue || editValue.trim() === ''}
  254. className="flex-1"
  255. >
  256. {t('保存')}
  257. </Button>
  258. </div>
  259. </div>
  260. ) : (
  261. /* 正常显示模式 */
  262. (() => {
  263. if (Array.isArray(message.content)) {
  264. const textContent = message.content.find(item => item.type === 'text');
  265. const imageContents = message.content.filter(item => item.type === 'image_url');
  266. return (
  267. <div>
  268. {/* 显示图片 */}
  269. {imageContents.length > 0 && (
  270. <div className="mb-3 space-y-2">
  271. {imageContents.map((imgItem, index) => (
  272. <div key={index} className="max-w-sm">
  273. <img
  274. src={imgItem.image_url.url}
  275. alt={`用户上传的图片 ${index + 1}`}
  276. className="rounded-lg max-w-full h-auto shadow-sm border"
  277. style={{ maxHeight: '300px' }}
  278. onError={(e) => {
  279. e.target.style.display = 'none';
  280. e.target.nextSibling.style.display = 'block';
  281. }}
  282. />
  283. <div
  284. className="text-red-500 text-sm p-2 bg-red-50 rounded-lg border border-red-200"
  285. style={{ display: 'none' }}
  286. >
  287. 图片加载失败: {imgItem.image_url.url}
  288. </div>
  289. </div>
  290. ))}
  291. </div>
  292. )}
  293. {/* 显示文本内容 */}
  294. {textContent && textContent.text && typeof textContent.text === 'string' && textContent.text.trim() !== '' && (
  295. <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' : ''}`}>
  296. <MarkdownRenderer
  297. content={textContent.text}
  298. className={message.role === 'user' ? 'user-message' : ''}
  299. />
  300. </div>
  301. )}
  302. </div>
  303. );
  304. }
  305. if (typeof message.content === 'string') {
  306. if (message.role === 'assistant') {
  307. if (finalDisplayableFinalContent && finalDisplayableFinalContent.trim() !== '') {
  308. return (
  309. <div className="prose prose-xs sm:prose-sm prose-gray max-w-none overflow-x-auto text-xs sm:text-sm">
  310. <MarkdownRenderer
  311. content={finalDisplayableFinalContent}
  312. className=""
  313. />
  314. </div>
  315. );
  316. }
  317. } else {
  318. return (
  319. <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' : ''}`}>
  320. <MarkdownRenderer
  321. content={message.content}
  322. className={message.role === 'user' ? 'user-message' : ''}
  323. />
  324. </div>
  325. );
  326. }
  327. }
  328. return null;
  329. })()
  330. )}
  331. </div>
  332. );
  333. };
  334. export default MessageContent;