ChatMessages.vue 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. <template>
  2. <div class="chat-messages-container">
  3. <!-- 左侧对话缩略导航(悬浮提示与滚动跟随内聚在组件内部) -->
  4. <ConversationNav
  5. ref="conversationNavRef"
  6. :messages="messages"
  7. :is-at-bottom="isAtBottom"
  8. :get-container="() => messagesRef"
  9. :scroll-to-message="scrollToMessage"
  10. />
  11. <div class="chat-messages" ref="messagesRef">
  12. <!-- 历史加载中:切换任务后新任务的 messages 要等接口返回才填充,
  13. 这里必须先占位,否则会先闪一屏「新对话缺省图」再突然出现内容 -->
  14. <div v-if="messages.length === 0 && historyLoading" class="history-loading">
  15. <a-spin size="large" />
  16. </div>
  17. <!-- 新对话缺省图 -->
  18. <div v-else-if="messages.length === 0" class="welcome-empty">
  19. <img :src="getCssUrl('--img-chat-welcome-img')" class="welcome-img" />
  20. </div>
  21. <div
  22. v-for="(message, index) in messages"
  23. :key="getMessageKey(message)"
  24. :class="['message-wrapper', message.type, { 'answer-hidden': isHiddenAnswer(index) }]"
  25. @mouseleave="copiedMessageKey = ''"
  26. >
  27. <!-- AI消息 -->
  28. <div v-if="message.type === 'ai'" class="ai-message">
  29. <div class="message-body">
  30. <ThinkingCard
  31. v-if="message.thinkingSteps && message.thinkingSteps.length > 0"
  32. :message="message"
  33. :collapsed="!!collapsedThinking[getMessageKey(message)]"
  34. :content-collapsed="!!collapsedThinkingContent[getMessageKey(message)]"
  35. @update:collapsed="collapsedThinking[getMessageKey(message)] = $event"
  36. @update:content-collapsed="collapsedThinkingContent[getMessageKey(message)] = $event"
  37. @agent-click="handleAgentGroupClick(index, $event)"
  38. />
  39. <div
  40. v-if="message.content"
  41. class="message-text"
  42. v-html="
  43. renderMarkdownCached(
  44. message.content,
  45. tableIcons,
  46. message.citations?.items?.length ? Math.max(...message.citations.items.map((i) => i.id)) : undefined,
  47. !message.isLoading
  48. )
  49. "
  50. @click="handleMessageTextClick($event, index)"
  51. ></div>
  52. <!-- 数据来源卡片 -->
  53. <CitationsCard
  54. v-if="message.citations"
  55. :message="message"
  56. :index="index"
  57. :collapsed="collapsedCitations[getMessageKey(message)] ?? true"
  58. @update:collapsed="collapsedCitations[getMessageKey(message)] = $event"
  59. />
  60. <div v-if="message.wordDownloadUrl" class="word-download-area">
  61. <a-button type="link" @click="emit('download-word', message.wordDownloadUrl)"> 下载报告文件 (.docx) </a-button>
  62. </div>
  63. <!-- 文件操作卡片 -->
  64. <DownloadFiles
  65. v-if="message.downloadFiles?.length"
  66. :files="message.downloadFiles"
  67. @file-preview="emit('file-preview', $event)"
  68. @open-file-preview="emit('open-file-preview', $event)"
  69. />
  70. <div v-show="message.isLoading" class="loading-dots">
  71. <span class="dot"></span>
  72. <span class="dot"></span>
  73. <span class="dot"></span>
  74. </div>
  75. <!-- ask_user 交互提问 UI(分步向导):ask_user 存在但 questions 缺失时不渲染,避免模板访问异常 -->
  76. <AskUserCard
  77. v-if="message.isPendingApproval && message.pendingApprovalData?.ask_user?.questions?.length"
  78. :ref="(el) => setAskCardRef(message, el)"
  79. :data="message.pendingApprovalData"
  80. @submit="emit('ask-user-submit', $event)"
  81. @cancel="emit('ask-user-cancel')"
  82. />
  83. </div>
  84. <!-- AI消息操作栏 -->
  85. <MessageActions
  86. :message="message"
  87. :copied="copiedMessageKey === getMessageKey(message)"
  88. :can-regenerate="canRegenerate(index, message)"
  89. :show-answer-switcher="showAnswerSwitcher(index)"
  90. :can-prev="canPrevAnswer(index)"
  91. :can-next="canNextAnswer(index)"
  92. :answer-position="answerPositionText(index)"
  93. @copy="copyMessageContent(message.content, getMessageKey(message))"
  94. @delete="emit('delete-message', message)"
  95. @regenerate="emit('regenerate')"
  96. @prev-answer="prevAnswer(index)"
  97. @next-answer="nextAnswer(index)"
  98. />
  99. </div>
  100. <!-- 用户消息 -->
  101. <div v-else class="user-message">
  102. <div class="message-body">
  103. <template v-if="editingKey === getMessageKey(message)">
  104. <textarea
  105. v-model="editDraft"
  106. class="message-edit-textarea"
  107. spellcheck="false"
  108. @keydown.enter.exact.prevent="submitEdit(index)"
  109. @keydown.escape.prevent="cancelEdit"
  110. ></textarea>
  111. <div class="message-edit-actions">
  112. <button class="message-edit-btn confirm" @click="submitEdit(index)">重发</button>
  113. <button class="message-edit-btn cancel" @click="cancelEdit">取消</button>
  114. </div>
  115. </template>
  116. <template v-else>
  117. <div
  118. class="message-text"
  119. v-html="renderMarkdownCached(message.content, tableIcons)"
  120. @click="handleMessageTextClick($event, index)"
  121. ></div>
  122. <div v-if="message.attachedFile" class="attached-file-card" @click="emit('file-preview', message.attachedFile)">
  123. <div class="file-icon-card" :style="{ backgroundImage: getFileIconVar(message.attachedFile.name) }"></div>
  124. <div class="file-info-card">
  125. <div class="file-name-card">{{ message.attachedFile.name }}</div>
  126. <div class="file-size-card">{{ formatFileSize(message.attachedFile.size) }}</div>
  127. </div>
  128. </div>
  129. </template>
  130. </div>
  131. <!-- 用户消息操作栏 -->
  132. <MessageActions
  133. :message="message"
  134. :copied="copiedMessageKey === getMessageKey(message)"
  135. :can-edit="canEdit(index, message)"
  136. @copy="copyMessageContent(message.content, getMessageKey(message))"
  137. @delete="emit('delete-message', message)"
  138. @edit="startEdit(index)"
  139. />
  140. </div>
  141. </div>
  142. </div>
  143. <!-- 回到底部按钮 -->
  144. <div v-if="!isAtBottom" class="scroll-to-bottom" @click="scrollToBottom">
  145. <div class="scroll-to-bottom-icon"></div>
  146. </div>
  147. </div>
  148. </template>
  149. <script setup lang="ts">
  150. import { ref, reactive, computed, watch, onMounted, onBeforeUnmount } from 'vue';
  151. import ConversationNav from './ConversationNav.vue';
  152. import CitationsCard from './CitationsCard.vue';
  153. import AskUserCard from './AskUserCard.vue';
  154. import DownloadFiles from './DownloadFiles.vue';
  155. import MessageActions from './MessageActions.vue';
  156. import ThinkingCard from './ThinkingCard.vue';
  157. import {
  158. renderMarkdownCached,
  159. formatFileSize,
  160. tableHtmlToMarkdown,
  161. tableHtmlToCsv,
  162. getFileIconVar,
  163. getCssUrl,
  164. getMessageKey,
  165. getTableIcons,
  166. copyToClipboard,
  167. flashCopySuccess,
  168. getUserInputText,
  169. panelOpenableUrl,
  170. webTabTitle,
  171. } from '/@/views/ventAI/manageAssistent/lib/index';
  172. import type { AgentGroup } from '/@/views/ventAI/manageAssistent/lib/index';
  173. import type { AgentDetailData, AskUserQuestion, Message, AttachedFile } from '/@/views/ventAI/manageAssistent/types/index';
  174. export type { AgentDetailData };
  175. const tableIcons = getTableIcons();
  176. const props = defineProps<{
  177. messages: Message[];
  178. /** 当前任务的历史是否正在拉取:为真且暂无消息时显示加载态,而不是空会话缺省图 */
  179. historyLoading?: boolean;
  180. }>();
  181. const emit = defineEmits<{
  182. (e: 'file-preview', file: AttachedFile): void;
  183. (e: 'download-word', url: string): void;
  184. (e: 'open-file-preview', file: AttachedFile): void;
  185. (e: 'ask-user-submit', answers: string[]): void;
  186. (e: 'ask-user-cancel'): void;
  187. (e: 'agent-detail-click', data: AgentDetailData): void;
  188. (e: 'open-markdown-tab', payload: { title: string; content: string }): void;
  189. (e: 'open-web', payload: { url: string; title?: string }): void;
  190. (e: 'delete-message', message: Message): void;
  191. (e: 'regenerate'): void;
  192. (e: 'edit-resend', index: number, newContent: string): void;
  193. }>();
  194. // 用户消息内联编辑状态
  195. const editingKey = ref('');
  196. const editDraft = ref('');
  197. // ===== 多回答折叠:重新生成产生的连续多条 AI 消息在视觉上合并为一条,可通过 prev/next 切换查看 =====
  198. interface AnswerGroup {
  199. indices: number[];
  200. }
  201. // 将「同一次提问的多个回答」划分为若干回答组:一条回答连同它后面**由重新生成追加的**回答算一组
  202. // (见 Message.isRegeneratedAnswer)。不能按「连续 AI 消息」推断分组 —— 删掉中间的提问后,上下两条
  203. // 互不相干的回答会变成连续消息,那样会被错误归成一组、用切换器藏起一条。
  204. const answerGroups = computed<AnswerGroup[]>(() => {
  205. const groups: AnswerGroup[] = [];
  206. let current: AnswerGroup | null = null;
  207. props.messages.forEach((msg, index) => {
  208. if (msg.type === 'ai') {
  209. // 组内除首条外都必须带「重新生成」标记,否则另起一组
  210. if (!current || !msg.isRegeneratedAnswer) {
  211. current = { indices: [] };
  212. groups.push(current);
  213. }
  214. current.indices.push(index);
  215. } else {
  216. current = null;
  217. }
  218. });
  219. return groups;
  220. });
  221. // 每条 AI 消息 → 所属组 key(组首条 index)、组内偏移、组大小
  222. const aiGroupMeta = computed(() => {
  223. const meta = new Map<number, { groupKey: string; offset: number; groupLen: number }>();
  224. for (const group of answerGroups.value) {
  225. const groupKey = getMessageKey(props.messages[group.indices[0]]);
  226. const groupLen = group.indices.length;
  227. group.indices.forEach((idx, offset) => {
  228. meta.set(idx, { groupKey, offset, groupLen });
  229. });
  230. }
  231. return meta;
  232. });
  233. // 每个回答组当前选中的回答偏移(默认组内最后一条,即最新生成)
  234. const activeAnswerOffset = reactive<Record<string, number>>({});
  235. const currentAnswerOffset = (groupKey: string, groupLen: number): number => {
  236. const val = activeAnswerOffset[groupKey];
  237. if (val == null) return groupLen - 1;
  238. return Math.min(Math.max(val, 0), groupLen - 1);
  239. };
  240. // 末尾回答组内是否存在正在生成(isLoading)的消息(按回答组判定,与分组规则保持一致)
  241. const lastAnswerGroupHasLoading = computed(() => {
  242. const msgs = props.messages;
  243. if (!msgs.length || msgs[msgs.length - 1].type !== 'ai') return false;
  244. const lastMeta = aiGroupMeta.value.get(msgs.length - 1);
  245. if (!lastMeta) return false;
  246. const group = answerGroups.value.find((g) => getMessageKey(msgs[g.indices[0]]) === lastMeta.groupKey);
  247. return !!group && group.indices.some((i) => msgs[i].isLoading);
  248. });
  249. // 新回答开始生成(false → true)时,自动切换到最新一条正在生成的消息所属的回答组
  250. watch(lastAnswerGroupHasLoading, (loading, wasLoading) => {
  251. if (loading && !wasLoading) {
  252. const lastMeta = aiGroupMeta.value.get(props.messages.length - 1);
  253. if (lastMeta) delete activeAnswerOffset[lastMeta.groupKey];
  254. }
  255. });
  256. // 该 AI 消息是否应被折叠隐藏(属于多回答组且非当前选中项)
  257. const isHiddenAnswer = (index: number): boolean => {
  258. const meta = aiGroupMeta.value.get(index);
  259. if (!meta || meta.groupLen <= 1) return false;
  260. return currentAnswerOffset(meta.groupKey, meta.groupLen) !== meta.offset;
  261. };
  262. // 该消息是否为当前选中显示的回答(供父组件标记折叠回答中的条目,如胶囊面板的智能体记录)
  263. const isAnswerVisible = (index: number): boolean => !isHiddenAnswer(index);
  264. // 该 AI 消息是否显示回答切换器(多回答组且为当前选中项)
  265. const showAnswerSwitcher = (index: number): boolean => {
  266. const meta = aiGroupMeta.value.get(index);
  267. if (!meta || meta.groupLen <= 1) return false;
  268. return currentAnswerOffset(meta.groupKey, meta.groupLen) === meta.offset;
  269. };
  270. const answerPositionText = (index: number): string => {
  271. const meta = aiGroupMeta.value.get(index);
  272. if (!meta) return '';
  273. return `${currentAnswerOffset(meta.groupKey, meta.groupLen) + 1}/${meta.groupLen}`;
  274. };
  275. const canPrevAnswer = (index: number): boolean => {
  276. const meta = aiGroupMeta.value.get(index);
  277. if (!meta) return false;
  278. return currentAnswerOffset(meta.groupKey, meta.groupLen) > 0;
  279. };
  280. const canNextAnswer = (index: number): boolean => {
  281. const meta = aiGroupMeta.value.get(index);
  282. if (!meta) return false;
  283. return currentAnswerOffset(meta.groupKey, meta.groupLen) < meta.groupLen - 1;
  284. };
  285. const prevAnswer = (index: number) => {
  286. const meta = aiGroupMeta.value.get(index);
  287. if (!meta) return;
  288. const cur = currentAnswerOffset(meta.groupKey, meta.groupLen);
  289. if (cur > 0) activeAnswerOffset[meta.groupKey] = cur - 1;
  290. };
  291. const nextAnswer = (index: number) => {
  292. const meta = aiGroupMeta.value.get(index);
  293. if (!meta) return;
  294. const cur = currentAnswerOffset(meta.groupKey, meta.groupLen);
  295. if (cur < meta.groupLen - 1) activeAnswerOffset[meta.groupKey] = cur + 1;
  296. };
  297. // 该消息是否属于末尾回答组:重新生成按钮只对最新一轮回答(含回答组内任意一条)显示
  298. const isInLastAnswerGroup = (index: number): boolean => {
  299. const msgs = props.messages;
  300. if (!msgs.length || msgs[msgs.length - 1].type !== 'ai') return false;
  301. if (msgs[index]?.type !== 'ai') return false;
  302. const meta = aiGroupMeta.value.get(index);
  303. const lastMeta = aiGroupMeta.value.get(msgs.length - 1);
  304. return !!meta && !!lastMeta && meta.groupKey === lastMeta.groupKey;
  305. };
  306. const canRegenerate = (index: number, message: Message): boolean => isInLastAnswerGroup(index) && !message.isLoading && !message.isPendingApproval;
  307. // 最后一条用户消息的索引(编辑重发只作用于最后一条用户消息,而非整段对话的最后一条)
  308. const lastUserMessageIndex = computed(() => {
  309. for (let i = props.messages.length - 1; i >= 0; i--) {
  310. if (props.messages[i].type === 'user') return i;
  311. }
  312. return -1;
  313. });
  314. const canEdit = (index: number, message: Message): boolean =>
  315. message.type === 'user' && index === lastUserMessageIndex.value && editingKey.value !== getMessageKey(message);
  316. const startEdit = (index: number) => {
  317. const msg = props.messages[index];
  318. editingKey.value = getMessageKey(msg);
  319. editDraft.value = getUserInputText(msg);
  320. };
  321. const cancelEdit = () => {
  322. editingKey.value = '';
  323. editDraft.value = '';
  324. };
  325. const submitEdit = (index: number) => {
  326. const content = editDraft.value;
  327. editingKey.value = '';
  328. editDraft.value = '';
  329. emit('edit-resend', index, content);
  330. };
  331. // 点击文件卡片:按类型拉取内容并在右侧辅助面板打开预览标签
  332. // ask_user 答案状态(按消息对象索引)
  333. // 审批卡实例按消息对象索引:SSE 侧直接以消息对象路由到对应卡片(卡片自身也会在 data 变化时重建答案)
  334. const askCardRefs = new Map<Message, InstanceType<typeof AskUserCard>>();
  335. const setAskCardRef = (msg: Message, el: unknown) => {
  336. if (el) askCardRefs.set(msg, el as InstanceType<typeof AskUserCard>);
  337. else askCardRefs.delete(msg);
  338. };
  339. const initAskUserAnswers = (msg: Message, questions: AskUserQuestion[]) => {
  340. askCardRefs.get(msg)?.init(questions);
  341. };
  342. const messagesRef = ref<HTMLElement>();
  343. const conversationNavRef = ref<InstanceType<typeof ConversationNav>>();
  344. const collapsedThinking = reactive<Record<string, boolean>>({});
  345. const collapsedThinkingContent = reactive<Record<string, boolean>>({});
  346. const collapsedCitations = reactive<Record<string, boolean>>({});
  347. const isAtBottom = ref(true);
  348. const copiedMessageKey = ref('');
  349. const handleTableAction = (e: Event) => {
  350. const target = (e.target as HTMLElement).closest('.table-action-btn') as HTMLElement;
  351. if (!target) return;
  352. const action = target.dataset.action;
  353. const encodedTable = target.dataset.table;
  354. if (!action || !encodedTable) return;
  355. const tableHtml = decodeURIComponent(encodedTable);
  356. switch (action) {
  357. case 'copy': {
  358. copyToClipboard(tableHtmlToMarkdown(tableHtml)).then(() => flashCopySuccess(target));
  359. break;
  360. }
  361. case 'csv': {
  362. const csv = tableHtmlToCsv(tableHtml);
  363. const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
  364. const url = URL.createObjectURL(blob);
  365. const a = document.createElement('a');
  366. a.href = url;
  367. a.download = `table_${Date.now()}.csv`;
  368. a.click();
  369. URL.revokeObjectURL(url);
  370. break;
  371. }
  372. case 'preview': {
  373. // 在右侧面板新建标签页展示表格渲染效果
  374. emit('open-markdown-tab', { title: '表格预览', content: tableHtmlToMarkdown(tableHtml) });
  375. break;
  376. }
  377. }
  378. };
  379. const checkScrollBottom = () => {
  380. const el = messagesRef.value;
  381. if (!el) return;
  382. const threshold = 50;
  383. isAtBottom.value = el.scrollHeight - el.scrollTop - el.clientHeight < threshold;
  384. };
  385. const scrollToBottom = () => {
  386. const el = messagesRef.value;
  387. if (el) {
  388. el.scrollTop = el.scrollHeight - el.clientHeight;
  389. }
  390. };
  391. const forceScrollToBottom = () => {
  392. const el = messagesRef.value;
  393. if (el) {
  394. el.scrollTop = el.scrollHeight - el.clientHeight;
  395. // 使用 requestAnimationFrame 确保 DOM 渲染完成后再滚动一次
  396. requestAnimationFrame(() => {
  397. el.scrollTop = el.scrollHeight - el.clientHeight;
  398. });
  399. }
  400. isAtBottom.value = true;
  401. };
  402. onMounted(() => {
  403. messagesRef.value?.addEventListener('click', handleTableAction);
  404. messagesRef.value?.addEventListener('scroll', handleMessagesScroll);
  405. });
  406. onBeforeUnmount(() => {
  407. messagesRef.value?.removeEventListener('click', handleTableAction);
  408. messagesRef.value?.removeEventListener('scroll', handleMessagesScroll);
  409. });
  410. const handleAgentGroupClick = (messageIndex: number, group: AgentGroup) => {
  411. emit('agent-detail-click', {
  412. messageIndex,
  413. agent: group.agent,
  414. agentId: group.agentId,
  415. });
  416. };
  417. const handleFootnoteClick = (e: Event, msgIndex: number) => {
  418. const target = (e.target as HTMLElement).closest('.footnote-ref') as HTMLElement;
  419. if (!target) return;
  420. const n = target.dataset.n;
  421. if (!n) return;
  422. const el = document.getElementById(`citation-${msgIndex}-${n}`);
  423. if (el) {
  424. scrollMessagesTo(el, 'center');
  425. el.classList.add('citation-highlight');
  426. setTimeout(() => el.classList.remove('citation-highlight'), 1500);
  427. }
  428. };
  429. /**
  430. * 消息正文的点击分发:http(s) 链接一律改在右侧面板打开(不区分站内站外,判定见 lib/webLink.ts),
  431. * 按住 Ctrl/Cmd/Shift 或点非 http 协议链接时保留原行为(新窗口)。
  432. * 没点到链接时按原逻辑处理脚注跳转。
  433. * 正文是 v-html 渲染出来的,只能靠容器上的事件委托。
  434. */
  435. const handleMessageTextClick = (e: Event, msgIndex: number) => {
  436. const anchor = (e.target as HTMLElement).closest('a[href]') as HTMLAnchorElement | null;
  437. if (anchor) {
  438. // 组合键留给用户的习惯操作(浏览器里 Ctrl/Cmd+点击就是「新标签页打开」)
  439. const ke = e as MouseEvent;
  440. if (ke.ctrlKey || ke.metaKey || ke.shiftKey) return;
  441. const url = panelOpenableUrl(anchor.getAttribute('href'));
  442. if (url) {
  443. e.preventDefault();
  444. emit('open-web', { url, title: webTabTitle(url, anchor.textContent || '') });
  445. }
  446. return;
  447. }
  448. handleFootnoteClick(e, msgIndex);
  449. };
  450. const copyMessageContent = async (content: string, messageKey: string) => {
  451. if (!content) return;
  452. await copyToClipboard(content);
  453. copiedMessageKey.value = messageKey;
  454. };
  455. // 仅滚动消息列表容器:scrollIntoView 会联动滚动 #adaptive-container 等祖先,导致整页上移、底部露出空白
  456. const scrollMessagesTo = (el: HTMLElement, block: 'start' | 'center' = 'start') => {
  457. const container = messagesRef.value;
  458. if (!container) return;
  459. // 容器处于自适应缩放环境:getBoundingClientRect 是缩放后的视口坐标,scrollTop 是未缩放的布局坐标,
  460. // 直接混算在缩放比非 1(打包部署后的其他分辨率)时定位偏差,需除以纵向缩放比换算回布局坐标
  461. //(与左侧对话缩略导航 tooltip 的换算方式一致)
  462. const rect = container.getBoundingClientRect();
  463. const scale = container.offsetHeight ? rect.height / container.offsetHeight : 1;
  464. const safeScale = scale > 0 && Number.isFinite(scale) ? scale : 1;
  465. const top = (el.getBoundingClientRect().top - rect.top) / safeScale + container.scrollTop;
  466. const offset = block === 'center' ? (container.clientHeight - el.offsetHeight) / 2 : 0;
  467. container.scrollTo({ top: Math.max(0, top - offset), behavior: 'smooth' });
  468. };
  469. const scrollToMessage = (index: number, block: 'start' | 'center' = 'start') => {
  470. const wrappers = messagesRef.value?.querySelectorAll('.message-wrapper');
  471. let el = wrappers?.[index] as HTMLElement | undefined;
  472. // 多回答折叠组中被隐藏的消息没有几何信息,回退到其前面最近一条可见消息(通常是该轮的用户提问)
  473. if (el && el.offsetParent === null) {
  474. el = undefined;
  475. for (let i = index - 1; i >= 0; i--) {
  476. const w = wrappers?.[i] as HTMLElement | undefined;
  477. if (w && w.offsetParent !== null) {
  478. el = w;
  479. break;
  480. }
  481. }
  482. }
  483. if (el) scrollMessagesTo(el, block);
  484. };
  485. const handleMessagesScroll = () => {
  486. checkScrollBottom();
  487. conversationNavRef.value?.onContainerScroll();
  488. };
  489. defineExpose({ messagesRef, isAtBottom, forceScrollToBottom, scrollToMessage, initAskUserAnswers, isAnswerVisible });
  490. </script>
  491. <style scoped lang="less">
  492. .chat-messages-container {
  493. flex: 1;
  494. position: relative;
  495. display: flex;
  496. flex-direction: row;
  497. overflow: hidden;
  498. }
  499. .chat-messages {
  500. flex: 1;
  501. overflow-y: auto;
  502. padding: 20px;
  503. position: relative;
  504. .welcome-empty {
  505. position: absolute;
  506. top: 50%;
  507. left: 50%;
  508. transform: translate(-50%, -50%);
  509. display: flex;
  510. align-items: center;
  511. justify-content: center;
  512. .welcome-img {
  513. max-width: 300px;
  514. max-height: 300px;
  515. object-fit: contain;
  516. }
  517. }
  518. /* 历史加载中的占位:与缺省图同样居中,避免两态之间出现位置跳动。
  519. antd 3.x 的点阵是固定像素(large 为 32px 框 + 14px 点),不随 font-size 缩放,
  520. 因此整体放大用 transform,既不动 antd 内部类名,比例也不会走样。 */
  521. .history-loading {
  522. position: absolute;
  523. top: 50%;
  524. left: 50%;
  525. transform: translate(-50%, -50%) scale(2.5);
  526. display: flex;
  527. align-items: center;
  528. justify-content: center;
  529. }
  530. .message-wrapper {
  531. // margin-bottom: 20px;
  532. &.answer-hidden {
  533. display: none;
  534. }
  535. &.nav-located-highlight {
  536. background: rgba(10, 132, 255, 0.08);
  537. border-radius: 6px;
  538. }
  539. .ai-message {
  540. display: flex;
  541. flex-wrap: wrap;
  542. .message-agent {
  543. display: flex;
  544. margin-bottom: 8px;
  545. opacity: 0;
  546. }
  547. .message-body {
  548. padding: 12px 12px 4px 12px;
  549. border-radius: 6px;
  550. width: 100%;
  551. min-width: 0;
  552. overflow: hidden;
  553. .message-text {
  554. font-size: 14px;
  555. font-weight: 400;
  556. line-height: 1.6;
  557. color: #e0e6ed;
  558. word-break: break-word;
  559. overflow-wrap: break-word;
  560. :deep(.session-ref-icon) {
  561. width: 14px;
  562. height: 14px;
  563. vertical-align: -2px;
  564. }
  565. :deep(h1),
  566. :deep(h2),
  567. :deep(h3),
  568. :deep(h4),
  569. :deep(h5),
  570. :deep(h6) {
  571. color: #fff;
  572. }
  573. :deep(p) {
  574. margin: 0;
  575. }
  576. :deep(code) {
  577. background: rgba(10, 132, 255, 0.1);
  578. padding: 1px 4px;
  579. border-radius: 3px;
  580. font-size: 12px;
  581. }
  582. :deep(pre) {
  583. background: rgba(10, 132, 255, 0.1);
  584. padding: 8px;
  585. border-radius: 4px;
  586. overflow-x: auto;
  587. margin: 6px 0;
  588. white-space: pre-wrap;
  589. }
  590. :deep(pre code) {
  591. background: transparent;
  592. padding: 0;
  593. }
  594. :deep(ul),
  595. :deep(ol) {
  596. padding-left: 18px;
  597. margin: 4px 0;
  598. }
  599. :deep(blockquote) {
  600. border-left: 3px solid rgba(10, 132, 255, 0.3);
  601. padding-left: 10px;
  602. margin: 6px 0;
  603. color: #8b949e;
  604. }
  605. :deep(table) {
  606. border-collapse: collapse;
  607. margin: 6px 0;
  608. }
  609. :deep(th),
  610. :deep(td) {
  611. border: 1px solid rgba(63, 80, 106, 0.5);
  612. padding: 4px 8px;
  613. }
  614. :deep(th) {
  615. background: rgba(10, 132, 255, 0.1);
  616. }
  617. :deep(strong) {
  618. font-weight: 600;
  619. }
  620. :deep(.markdown-table-wrapper) {
  621. position: relative;
  622. margin: 6px 0;
  623. max-width: 100%;
  624. overflow-x: auto;
  625. .table-actions {
  626. position: absolute;
  627. top: 4px;
  628. right: 4px;
  629. display: flex;
  630. gap: 2px;
  631. opacity: 0;
  632. transition: opacity 0.2s;
  633. z-index: 1;
  634. }
  635. &:hover .table-actions {
  636. opacity: 1;
  637. }
  638. .table-action-btn {
  639. width: 26px;
  640. height: 26px;
  641. display: flex;
  642. align-items: center;
  643. justify-content: center;
  644. border-radius: 4px;
  645. color: #8b949e;
  646. cursor: pointer;
  647. background: rgba(22, 27, 34, 0.8);
  648. backdrop-filter: blur(4px);
  649. transition: all 0.2s;
  650. &:hover {
  651. color: #e0e6ed;
  652. background: rgba(10, 132, 255, 0.3);
  653. }
  654. }
  655. }
  656. }
  657. }
  658. .loading-dots {
  659. display: flex;
  660. align-items: center;
  661. gap: 6px;
  662. padding: 4px 0;
  663. min-height: 16px;
  664. .dot {
  665. width: 8px;
  666. height: 8px;
  667. border-radius: 50%;
  668. background: rgba(10, 132, 255, 0.4);
  669. animation: dot-bounce 1.4s ease-in-out infinite both;
  670. &:nth-child(1) {
  671. animation-delay: 0s;
  672. }
  673. &:nth-child(2) {
  674. animation-delay: 0.2s;
  675. }
  676. &:nth-child(3) {
  677. animation-delay: 0.4s;
  678. }
  679. }
  680. }
  681. @keyframes dot-bounce {
  682. 0%,
  683. 80%,
  684. 100% {
  685. transform: scale(0.6);
  686. opacity: 0.4;
  687. }
  688. 40% {
  689. transform: scale(1);
  690. opacity: 1;
  691. }
  692. }
  693. .citations-card {
  694. overflow: hidden;
  695. .citations-header {
  696. display: flex;
  697. align-items: center;
  698. justify-content: space-between;
  699. padding: 8px 14px;
  700. border-bottom: 1px solid rgba(63, 80, 106, 0.3);
  701. cursor: pointer;
  702. user-select: none;
  703. transition: background 0.2s;
  704. &:hover {
  705. background: rgba(10, 132, 255, 0.08);
  706. }
  707. &.citations-header-simple {
  708. cursor: default;
  709. &:hover {
  710. background: transparent;
  711. }
  712. }
  713. .citations-header-left {
  714. display: flex;
  715. align-items: center;
  716. gap: 8px;
  717. }
  718. .citations-title {
  719. font-size: 13px;
  720. font-weight: 500;
  721. color: #8b949e;
  722. }
  723. .citations-count {
  724. font-size: 12px;
  725. color: #8b949e;
  726. background: rgba(10, 132, 255, 0.1);
  727. padding: 1px 6px;
  728. border-radius: 10px;
  729. }
  730. .thinking-arrow {
  731. display: flex;
  732. align-items: center;
  733. transition: transform 0.2s;
  734. &.expanded {
  735. transform: rotate(180deg);
  736. }
  737. }
  738. }
  739. .citations-body {
  740. padding: 4px 14px;
  741. .citation-item {
  742. padding: 6px 0;
  743. border-bottom: 1px solid rgba(63, 80, 106, 0.15);
  744. &:last-child {
  745. border-bottom: none;
  746. }
  747. &.citation-highlight {
  748. background: rgba(10, 132, 255, 0.08);
  749. border-radius: 4px;
  750. padding: 6px 8px;
  751. margin: 0 -8px;
  752. }
  753. .citation-item-row {
  754. display: flex;
  755. align-items: center;
  756. gap: 8px;
  757. line-height: 1.5;
  758. .citation-badge {
  759. font-size: 12px;
  760. color: #79c0ff;
  761. font-family: monospace;
  762. background: rgba(10, 132, 255, 0.1);
  763. padding: 1px 5px;
  764. border-radius: 3px;
  765. white-space: nowrap;
  766. flex-shrink: 0;
  767. }
  768. .citation-badge-knowledge_base {
  769. color: #3fb950;
  770. background: rgba(46, 160, 67, 0.1);
  771. }
  772. .citation-badge-web {
  773. color: #d29922;
  774. background: rgba(210, 153, 34, 0.1);
  775. }
  776. .citation-badge-calc {
  777. color: #a371f7;
  778. background: rgba(163, 113, 247, 0.1);
  779. }
  780. .citation-title-text {
  781. font-size: 13px;
  782. color: #b0b8c4;
  783. overflow: hidden;
  784. text-overflow: ellipsis;
  785. white-space: nowrap;
  786. min-width: 0;
  787. }
  788. .citation-link {
  789. font-size: 12px;
  790. color: #0a84ff;
  791. text-decoration: none;
  792. white-space: nowrap;
  793. flex-shrink: 0;
  794. }
  795. }
  796. .citation-item-attrs {
  797. display: flex;
  798. align-items: center;
  799. flex-wrap: wrap;
  800. gap: 4px 10px;
  801. padding-left: 0;
  802. margin-top: 2px;
  803. .citation-attr {
  804. font-size: 12px;
  805. color: #8b949e;
  806. white-space: nowrap;
  807. }
  808. .citation-low-score {
  809. color: #d29922;
  810. }
  811. }
  812. .citation-snippet {
  813. margin-top: 4px;
  814. font-size: 12px;
  815. color: #8b949e;
  816. line-height: 1.5;
  817. overflow: hidden;
  818. text-overflow: ellipsis;
  819. white-space: nowrap;
  820. }
  821. }
  822. }
  823. }
  824. .download-file-cards {
  825. display: flex;
  826. flex-direction: column;
  827. gap: 8px;
  828. margin-top: 12px;
  829. .download-file-card {
  830. display: flex;
  831. align-items: center;
  832. justify-content: space-between;
  833. padding: 12px 16px;
  834. background: linear-gradient(135deg, rgba(0, 47, 74, 0.8) 0%, rgba(0, 60, 90, 0.6) 100%);
  835. border: 1px solid rgba(0, 84, 130, 0.6);
  836. border-radius: 10px;
  837. cursor: pointer;
  838. transition: all 0.2s ease;
  839. &:hover {
  840. border-color: rgba(10, 132, 255, 0.4);
  841. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  842. }
  843. .file-card-info {
  844. display: flex;
  845. align-items: center;
  846. gap: 12px;
  847. min-width: 0;
  848. flex: 1;
  849. .file-card-icon {
  850. width: 36px;
  851. height: 36px;
  852. background-image: var(--img-chat-download-icon);
  853. background-repeat: no-repeat;
  854. background-size: 100% 100%;
  855. flex-shrink: 0;
  856. opacity: 0.9;
  857. }
  858. .file-card-text {
  859. display: flex;
  860. flex-direction: column;
  861. gap: 2px;
  862. min-width: 0;
  863. .file-card-name {
  864. font-size: 13px;
  865. font-weight: 500;
  866. color: #e0e6ed;
  867. overflow: hidden;
  868. text-overflow: ellipsis;
  869. white-space: nowrap;
  870. }
  871. .file-card-hint {
  872. font-size: 11px;
  873. color: #6e7681;
  874. }
  875. }
  876. }
  877. .file-card-actions {
  878. display: flex;
  879. gap: 8px;
  880. flex-shrink: 0;
  881. .file-action-btn {
  882. display: inline-flex;
  883. align-items: center;
  884. gap: 5px;
  885. padding: 6px 14px;
  886. border-radius: 7px;
  887. font-size: 12px;
  888. font-weight: 500;
  889. cursor: pointer;
  890. border: none;
  891. transition: all 0.2s ease;
  892. white-space: nowrap;
  893. &.download-btn {
  894. background: rgba(255, 255, 255, 0.08);
  895. color: #c9d1d9;
  896. border: 1px solid rgba(63, 80, 106, 0.5);
  897. &:hover {
  898. background: rgba(255, 255, 255, 0.14);
  899. color: #e0e6ed;
  900. border-color: rgba(63, 80, 106, 0.8);
  901. }
  902. }
  903. &.edit-btn {
  904. background: linear-gradient(135deg, #0a84ff 0%, #0066cc 100%);
  905. color: #fff;
  906. border: 1px solid rgba(10, 132, 255, 0.5);
  907. box-shadow: 0 2px 6px rgba(10, 132, 255, 0.2);
  908. &:hover {
  909. background: linear-gradient(135deg, #3a9bfd 0%, #0a84ff 100%);
  910. box-shadow: 0 3px 10px rgba(10, 132, 255, 0.35);
  911. }
  912. }
  913. }
  914. }
  915. }
  916. }
  917. }
  918. .user-message {
  919. display: flex;
  920. flex-wrap: wrap;
  921. justify-content: flex-end;
  922. align-items: flex-start;
  923. .message-agent {
  924. display: flex;
  925. margin-bottom: 8px;
  926. margin-left: 5px;
  927. }
  928. .message-body {
  929. background: rgba(10, 132, 255, 0.15);
  930. padding: 12px;
  931. border-radius: 6px 0 6px 6px;
  932. max-width: 700px;
  933. min-width: 0;
  934. overflow: hidden;
  935. .message-text {
  936. font-size: 14px;
  937. font-weight: 400;
  938. line-height: 1.6;
  939. color: #e0e6ed;
  940. word-break: break-word;
  941. overflow-wrap: break-word;
  942. :deep(.session-ref-icon) {
  943. width: 14px;
  944. height: 14px;
  945. vertical-align: -2px;
  946. }
  947. :deep(h1, h2, h3, h4, h5, h6) {
  948. color: #fff;
  949. }
  950. :deep(p) {
  951. margin: 0;
  952. }
  953. :deep(code) {
  954. background: rgba(0, 0, 0, 0.2);
  955. padding: 1px 4px;
  956. border-radius: 3px;
  957. font-size: 12px;
  958. }
  959. :deep(pre) {
  960. background: rgba(0, 0, 0, 0.2);
  961. padding: 8px;
  962. border-radius: 4px;
  963. overflow-x: auto;
  964. margin: 6px 0;
  965. white-space: pre-wrap;
  966. }
  967. :deep(pre code) {
  968. background: transparent;
  969. padding: 0;
  970. }
  971. }
  972. .attached-file-card {
  973. margin-top: 10px;
  974. background: rgba(255, 255, 255, 0.08);
  975. border-radius: 6px;
  976. display: flex;
  977. align-items: center;
  978. gap: 10px;
  979. padding: 8px;
  980. cursor: pointer;
  981. transition: all 0.3s;
  982. &:hover {
  983. background: rgba(255, 255, 255, 0.15);
  984. }
  985. .file-icon-card {
  986. width: 32px;
  987. height: 32px;
  988. background-image: var(--img-chat-attach-icon);
  989. background-repeat: no-repeat;
  990. background-size: 100% 100%;
  991. flex-shrink: 0;
  992. }
  993. .file-info-card {
  994. flex: 1;
  995. min-width: 0;
  996. .file-name-card {
  997. font-size: 12px;
  998. font-weight: 400;
  999. line-height: 1.4;
  1000. color: #e0e6ed;
  1001. overflow: hidden;
  1002. text-overflow: ellipsis;
  1003. white-space: nowrap;
  1004. margin-bottom: 2px;
  1005. }
  1006. .file-size-card {
  1007. font-size: 11px;
  1008. font-weight: 400;
  1009. line-height: 1.3;
  1010. color: #8b949e;
  1011. }
  1012. }
  1013. }
  1014. }
  1015. }
  1016. .ai-message,
  1017. .user-message {
  1018. .message-actions {
  1019. display: flex;
  1020. align-items: center;
  1021. gap: 8px;
  1022. width: 100%;
  1023. opacity: 0;
  1024. transition: opacity 0.2s;
  1025. .action-btn {
  1026. width: 26px;
  1027. height: 26px;
  1028. display: flex;
  1029. align-items: center;
  1030. justify-content: center;
  1031. border-radius: 4px;
  1032. color: #8b949e;
  1033. cursor: pointer;
  1034. transition: all 0.2s;
  1035. &:hover {
  1036. color: #e0e6ed;
  1037. background: rgba(10, 132, 255, 0.15);
  1038. }
  1039. }
  1040. .action-time {
  1041. font-size: 12px;
  1042. color: #8b949e;
  1043. }
  1044. .answer-switcher {
  1045. display: flex;
  1046. align-items: center;
  1047. gap: 2px;
  1048. padding: 0 4px;
  1049. border-radius: 4px;
  1050. background: rgba(255, 255, 255, 0.04);
  1051. .answer-nav-btn {
  1052. width: 22px;
  1053. height: 22px;
  1054. &.is-disabled {
  1055. opacity: 0.35;
  1056. cursor: not-allowed;
  1057. &:hover {
  1058. background: transparent;
  1059. color: #8b949e;
  1060. }
  1061. }
  1062. }
  1063. .answer-switcher-text {
  1064. font-size: 12px;
  1065. color: #8b949e;
  1066. min-width: 32px;
  1067. text-align: center;
  1068. user-select: none;
  1069. }
  1070. }
  1071. }
  1072. &:hover .message-actions {
  1073. opacity: 1;
  1074. }
  1075. }
  1076. .ai-message .message-actions {
  1077. padding: 0 0 0 10px;
  1078. }
  1079. .user-message .message-actions {
  1080. padding: 4px 0;
  1081. justify-content: flex-end;
  1082. }
  1083. }
  1084. }
  1085. .message-edit-textarea {
  1086. width: 100%;
  1087. min-width: 500px;
  1088. min-height: 80px;
  1089. padding: 8px 12px;
  1090. font-size: 14px;
  1091. line-height: 1.6;
  1092. color: #e0e6ed;
  1093. background: rgba(0, 0, 0, 0.2);
  1094. border: 1px solid rgba(10, 132, 255, 0.5);
  1095. border-radius: 6px;
  1096. outline: none;
  1097. resize: vertical;
  1098. font-family: inherit;
  1099. &:focus {
  1100. border-color: #0a84ff;
  1101. background: rgba(0, 0, 0, 0.3);
  1102. }
  1103. }
  1104. .message-edit-actions {
  1105. display: flex;
  1106. gap: 8px;
  1107. margin-top: 8px;
  1108. justify-content: flex-end;
  1109. .message-edit-btn {
  1110. padding: 3px 14px;
  1111. font-size: 12px;
  1112. border-radius: 4px;
  1113. cursor: pointer;
  1114. border: 1px solid transparent;
  1115. transition: all 0.2s;
  1116. &.confirm {
  1117. color: #fff;
  1118. background: linear-gradient(135deg, #0a84ff 0%, #06c 100%);
  1119. border-color: rgba(10, 132, 255, 0.5);
  1120. &:hover {
  1121. background: linear-gradient(135deg, #3a9bfd 0%, #0a84ff 100%);
  1122. }
  1123. }
  1124. &.cancel {
  1125. color: #8b949e;
  1126. background: rgba(255, 255, 255, 0.06);
  1127. border-color: rgba(63, 80, 106, 0.4);
  1128. &:hover {
  1129. color: #c9d1d9;
  1130. background: rgba(255, 255, 255, 0.1);
  1131. }
  1132. }
  1133. }
  1134. }
  1135. .scroll-to-bottom {
  1136. position: absolute;
  1137. bottom: 20px;
  1138. left: 50%;
  1139. transform: translateX(-50%);
  1140. z-index: 10;
  1141. .scroll-to-bottom-icon {
  1142. width: 40px;
  1143. height: 40px;
  1144. border-radius: 50%;
  1145. background: rgba(255, 255, 255, 0.15);
  1146. backdrop-filter: blur(4px);
  1147. background-image: var(--img-chat-scroll-bottom);
  1148. background-repeat: no-repeat;
  1149. background-size: 20px 20px;
  1150. background-position: center;
  1151. cursor: pointer;
  1152. transition: all 0.2s;
  1153. &:hover {
  1154. background-color: rgba(255, 255, 255, 0.25);
  1155. }
  1156. }
  1157. }
  1158. </style>