MessageContent.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 ThinkingContent from './ThinkingContent';
  9. import {
  10. Loader2,
  11. Check,
  12. X,
  13. } from 'lucide-react';
  14. import { useTranslation } from 'react-i18next';
  15. const MessageContent = ({
  16. message,
  17. className,
  18. styleState,
  19. onToggleReasoningExpansion,
  20. isEditing = false,
  21. onEditSave,
  22. onEditCancel,
  23. editValue,
  24. onEditValueChange
  25. }) => {
  26. const { t } = useTranslation();
  27. if (message.status === 'error') {
  28. let errorText;
  29. if (Array.isArray(message.content)) {
  30. const textContent = message.content.find(item => item.type === 'text');
  31. errorText = textContent && textContent.text && typeof textContent.text === 'string'
  32. ? textContent.text
  33. : t('请求发生错误');
  34. } else if (typeof message.content === 'string') {
  35. errorText = message.content;
  36. } else {
  37. errorText = t('请求发生错误');
  38. }
  39. return (
  40. <div className={`${className} flex items-center p-4 bg-red-50 rounded-xl`}>
  41. <Typography.Text type="danger" className="text-sm">
  42. {errorText}
  43. </Typography.Text>
  44. </div>
  45. );
  46. }
  47. const isThinkingStatus = message.status === 'loading' || message.status === 'incomplete';
  48. let currentExtractedThinkingContent = null;
  49. let currentDisplayableFinalContent = "";
  50. let thinkingSource = null;
  51. const getTextContent = (content) => {
  52. if (Array.isArray(content)) {
  53. const textItem = content.find(item => item.type === 'text');
  54. return textItem && textItem.text && typeof textItem.text === 'string' ? textItem.text : '';
  55. } else if (typeof content === 'string') {
  56. return content;
  57. }
  58. return '';
  59. };
  60. currentDisplayableFinalContent = getTextContent(message.content);
  61. if (message.role === 'assistant') {
  62. let baseContentForDisplay = getTextContent(message.content);
  63. let combinedThinkingContent = "";
  64. if (message.reasoningContent) {
  65. combinedThinkingContent = message.reasoningContent;
  66. thinkingSource = 'reasoningContent';
  67. }
  68. if (baseContentForDisplay.includes('<think>')) {
  69. const thinkTagRegex = /<think>([\s\S]*?)<\/think>/g;
  70. let match;
  71. let thoughtsFromPairedTags = [];
  72. let replyParts = [];
  73. let lastIndex = 0;
  74. while ((match = thinkTagRegex.exec(baseContentForDisplay)) !== null) {
  75. replyParts.push(baseContentForDisplay.substring(lastIndex, match.index));
  76. thoughtsFromPairedTags.push(match[1]);
  77. lastIndex = match.index + match[0].length;
  78. }
  79. replyParts.push(baseContentForDisplay.substring(lastIndex));
  80. if (thoughtsFromPairedTags.length > 0) {
  81. const pairedThoughtsStr = thoughtsFromPairedTags.join('\n\n---\n\n');
  82. if (combinedThinkingContent) {
  83. combinedThinkingContent += '\n\n---\n\n' + pairedThoughtsStr;
  84. } else {
  85. combinedThinkingContent = pairedThoughtsStr;
  86. }
  87. thinkingSource = thinkingSource ? thinkingSource + ' & <think> tags' : '<think> tags';
  88. }
  89. baseContentForDisplay = replyParts.join('');
  90. }
  91. if (isThinkingStatus) {
  92. const lastOpenThinkIndex = baseContentForDisplay.lastIndexOf('<think>');
  93. if (lastOpenThinkIndex !== -1) {
  94. const fragmentAfterLastOpen = baseContentForDisplay.substring(lastOpenThinkIndex);
  95. if (!fragmentAfterLastOpen.includes('</think>')) {
  96. const unclosedThought = fragmentAfterLastOpen.substring('<think>'.length).trim();
  97. if (unclosedThought) {
  98. if (combinedThinkingContent) {
  99. combinedThinkingContent += '\n\n---\n\n' + unclosedThought;
  100. } else {
  101. combinedThinkingContent = unclosedThought;
  102. }
  103. thinkingSource = thinkingSource ? thinkingSource + ' + streaming <think>' : 'streaming <think>';
  104. }
  105. baseContentForDisplay = baseContentForDisplay.substring(0, lastOpenThinkIndex);
  106. }
  107. }
  108. }
  109. currentExtractedThinkingContent = combinedThinkingContent || null;
  110. currentDisplayableFinalContent = baseContentForDisplay.replace(/<\/?think>/g, '').trim();
  111. }
  112. const finalExtractedThinkingContent = currentExtractedThinkingContent;
  113. const finalDisplayableFinalContent = currentDisplayableFinalContent;
  114. if (message.role === 'assistant' &&
  115. isThinkingStatus &&
  116. !finalExtractedThinkingContent &&
  117. (!finalDisplayableFinalContent || finalDisplayableFinalContent.trim() === '')) {
  118. return (
  119. <div className={`${className} flex items-center gap-2 sm:gap-4 bg-gradient-to-r from-purple-50 to-indigo-50`}>
  120. <div className="w-5 h-5 rounded-full bg-gradient-to-br from-purple-500 to-indigo-600 flex items-center justify-center shadow-lg">
  121. <Loader2 className="animate-spin text-white" size={styleState.isMobile ? 16 : 20} />
  122. </div>
  123. </div>
  124. );
  125. }
  126. return (
  127. <div className={className}>
  128. {message.role === 'system' && (
  129. <div className="mb-2 sm:mb-4">
  130. <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)' }}>
  131. <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">
  132. <Typography.Text className="text-white text-xs font-bold">S</Typography.Text>
  133. </div>
  134. <Typography.Text className="text-amber-700 text-xs sm:text-sm font-medium">
  135. {t('系统消息')}
  136. </Typography.Text>
  137. </div>
  138. </div>
  139. )}
  140. {message.role === 'assistant' && (
  141. <ThinkingContent
  142. message={message}
  143. finalExtractedThinkingContent={finalExtractedThinkingContent}
  144. thinkingSource={thinkingSource}
  145. styleState={styleState}
  146. onToggleReasoningExpansion={onToggleReasoningExpansion}
  147. />
  148. )}
  149. {isEditing ? (
  150. <div className="space-y-3">
  151. <TextArea
  152. value={editValue}
  153. onChange={(value) => onEditValueChange(value)}
  154. placeholder={t('请输入消息内容...')}
  155. autosize={{ minRows: 3, maxRows: 12 }}
  156. style={{
  157. resize: 'vertical',
  158. fontSize: styleState.isMobile ? '14px' : '15px',
  159. lineHeight: '1.6',
  160. }}
  161. className="!border-blue-200 focus:!border-blue-400 !bg-blue-50/50"
  162. />
  163. <div className="flex items-center gap-2 w-full">
  164. <Button
  165. size="small"
  166. type="danger"
  167. theme="light"
  168. icon={<X size={14} />}
  169. onClick={onEditCancel}
  170. className="flex-1"
  171. >
  172. {t('取消')}
  173. </Button>
  174. <Button
  175. size="small"
  176. type="warning"
  177. theme="solid"
  178. icon={<Check size={14} />}
  179. onClick={onEditSave}
  180. disabled={!editValue || editValue.trim() === ''}
  181. className="flex-1"
  182. >
  183. {t('保存')}
  184. </Button>
  185. </div>
  186. </div>
  187. ) : (
  188. (() => {
  189. if (Array.isArray(message.content)) {
  190. const textContent = message.content.find(item => item.type === 'text');
  191. const imageContents = message.content.filter(item => item.type === 'image_url');
  192. return (
  193. <div>
  194. {imageContents.length > 0 && (
  195. <div className="mb-3 space-y-2">
  196. {imageContents.map((imgItem, index) => (
  197. <div key={index} className="max-w-sm">
  198. <img
  199. src={imgItem.image_url.url}
  200. alt={`用户上传的图片 ${index + 1}`}
  201. className="rounded-lg max-w-full h-auto shadow-sm border"
  202. style={{ maxHeight: '300px' }}
  203. onError={(e) => {
  204. e.target.style.display = 'none';
  205. e.target.nextSibling.style.display = 'block';
  206. }}
  207. />
  208. <div
  209. className="text-red-500 text-sm p-2 bg-red-50 rounded-lg border border-red-200"
  210. style={{ display: 'none' }}
  211. >
  212. 图片加载失败: {imgItem.image_url.url}
  213. </div>
  214. </div>
  215. ))}
  216. </div>
  217. )}
  218. {textContent && textContent.text && typeof textContent.text === 'string' && textContent.text.trim() !== '' && (
  219. <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' : ''}`}>
  220. <MarkdownRenderer
  221. content={textContent.text}
  222. className={message.role === 'user' ? 'user-message' : ''}
  223. animated={false}
  224. />
  225. </div>
  226. )}
  227. </div>
  228. );
  229. }
  230. if (typeof message.content === 'string') {
  231. if (message.role === 'assistant') {
  232. if (finalDisplayableFinalContent && finalDisplayableFinalContent.trim() !== '') {
  233. return (
  234. <div className="prose prose-xs sm:prose-sm prose-gray max-w-none overflow-x-auto text-xs sm:text-sm">
  235. <MarkdownRenderer
  236. content={finalDisplayableFinalContent}
  237. className=""
  238. animated={isThinkingStatus}
  239. />
  240. </div>
  241. );
  242. }
  243. } else {
  244. return (
  245. <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' : ''}`}>
  246. <MarkdownRenderer
  247. content={message.content}
  248. className={message.role === 'user' ? 'user-message' : ''}
  249. animated={false}
  250. />
  251. </div>
  252. );
  253. }
  254. }
  255. return null;
  256. })()
  257. )}
  258. </div>
  259. );
  260. };
  261. export default MessageContent;