ChatMessageItem.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <article
  3. :id="`ai-msg-${message.id}`"
  4. class="ai-msg"
  5. :class="[`ai-msg--${message.role}`, { 'is-error': message.messageType === 'error' }]"
  6. >
  7. <!-- 用户消息:右对齐气泡。气泡形状本身已表明"这是我说的",不再需要角色标签 -->
  8. <template v-if="isUser">
  9. <p class="ai-msg__bubble">{{ message.content }}</p>
  10. <!-- 附件徽标:本次提问随附的文件(落库后从 metadata.attachments 还原) -->
  11. <div v-if="message.attachments?.length" class="ai-msg__atts">
  12. <span v-for="att in message.attachments" :key="att.id" class="ai-msg__att">
  13. <Icon icon="mdi:paperclip" :size="11" />
  14. <span class="ai-msg__att-name" :title="att.name">{{ att.name }}</span>
  15. <span class="ai-msg__att-meta">{{ badgeMeta(att) }}</span>
  16. </span>
  17. </div>
  18. <div class="ai-msg__foot">
  19. <span v-if="message.messageType === 'interrupted'" class="ai-msg__flag">已中断</span>
  20. <span v-if="timeText" class="ai-msg__time">{{ timeText }}</span>
  21. </div>
  22. <MessageToolbar
  23. :message="message"
  24. :title="title"
  25. :get-body-element="() => bodyRef"
  26. @star="emit('star')"
  27. @remove="emit('remove')"
  28. @regenerate="emit('regenerate')"
  29. />
  30. </template>
  31. <!-- AI 消息:头像 + 内容列,不加气泡、不加边框 —— 长回答需要的是宽度和留白 -->
  32. <template v-else>
  33. <div class="ai-msg__avatar" aria-hidden="true">
  34. <Icon icon="mdi:sparkles" :size="15" />
  35. </div>
  36. <div class="ai-msg__main">
  37. <header class="ai-msg__head">
  38. <span class="ai-msg__role">AI 分析</span>
  39. <span v-if="timeText" class="ai-msg__time">{{ timeText }}</span>
  40. <span v-if="message.starred" class="ai-msg__star">已收藏</span>
  41. <span v-if="message.messageType === 'interrupted'" class="ai-msg__flag">已中断</span>
  42. </header>
  43. <div ref="bodyRef" class="ai-msg__body">
  44. <!-- 过程时间线(思考 + 工具调用):一行汇总 + 展开流水,渲染在正文之前 -->
  45. <ProcessTimeline
  46. :thinking="message.thinking"
  47. :thinking-active="!!message.thinkingActive"
  48. :steps="message.toolSteps"
  49. :trace="message.trace"
  50. :active="!!message.streaming"
  51. :message-id="message.id"
  52. />
  53. <div v-if="message.messageType === 'error'" class="ai-msg__error">
  54. <Icon class="ai-msg__error-icon" icon="mdi:alert-circle-outline" :size="16" />
  55. <span>{{ message.content || '本次请求失败,请稍后重试' }}</span>
  56. </div>
  57. <template v-else>
  58. <BlockRenderer
  59. v-for="block in message.blocks || []"
  60. :key="block.key"
  61. :block="block"
  62. :streaming="!!message.streaming"
  63. />
  64. <div v-if="message.streaming && !hasContent" class="ai-msg__skeleton">
  65. <div class="ai-skeleton" :style="{ height: '14px', width: '62%' }"></div>
  66. <div class="ai-skeleton" :style="{ height: '14px', width: '88%' }"></div>
  67. <div class="ai-skeleton" :style="{ height: '14px', width: '46%' }"></div>
  68. </div>
  69. <!-- 流式状态条放在正文之后:长回答输出到一半等待时,用户在阅读位置就能看到
  70. “正在思考 / 正在执行 / 连接正常但无新内容 / 心跳已断” -->
  71. <StreamStatusBar
  72. v-if="message.streaming"
  73. :streaming="true"
  74. :intent="message.intent"
  75. :steps="message.toolSteps"
  76. :has-content="hasContent"
  77. :thinking-active="!!message.thinkingActive"
  78. :stream-started-at="message.streamStartedAt"
  79. :last-activity-at="message.lastActivityAt"
  80. :last-heartbeat-at="message.lastHeartbeatAt"
  81. />
  82. </template>
  83. </div>
  84. <!-- 追问建议:有方向分组时按方向渲染,否则回退旧版平铺 -->
  85. <SuggestionBar
  86. v-if="!message.streaming && (message.suggestions?.length || message.suggestionGroups?.length)"
  87. :questions="message.suggestions"
  88. :directions="message.suggestionGroups"
  89. @pick="(q) => emit('pick', q)"
  90. />
  91. <!-- 操作条 -->
  92. <MessageToolbar
  93. :message="message"
  94. :title="title"
  95. :get-body-element="() => bodyRef"
  96. @star="emit('star')"
  97. @remove="emit('remove')"
  98. @regenerate="emit('regenerate')"
  99. />
  100. </div>
  101. </template>
  102. </article>
  103. </template>
  104. <script lang="ts" setup>
  105. /**
  106. * 单条消息
  107. *
  108. * 布局参考主流 AI 对话界面:
  109. * - 用户消息:右对齐气泡(最大 72% 宽),时间贴在气泡下方,不占头部空间;
  110. * - AI 消息:左侧头像 + 内容列,正文不加气泡/边框,回答可以吃满整列宽度。
  111. *
  112. * 角色之间不再画分隔线 —— 一问一答靠"间距节奏"成组(见 ai.less 中 .ai-msg 的相邻规则),
  113. * 分隔线会把一次问答从中间切断,反而破坏配对关系。
  114. */
  115. import { computed, ref } from 'vue';
  116. import type { ChatMessage } from '../store/chatStream';
  117. import Icon from '@/core/components/Icon/src/Icon.vue';
  118. import BlockRenderer from './BlockRenderer.vue';
  119. import ProcessTimeline from './ProcessTimeline.vue';
  120. import StreamStatusBar from './StreamStatusBar.vue';
  121. import SuggestionBar from './SuggestionBar.vue';
  122. import MessageToolbar from './MessageToolbar.vue';
  123. const props = withDefaults(
  124. defineProps<{
  125. message: ChatMessage;
  126. title?: string;
  127. }>(),
  128. { title: '' },
  129. );
  130. const emit = defineEmits<{
  131. pick: [question: string];
  132. star: [];
  133. remove: [];
  134. regenerate: [];
  135. }>();
  136. const bodyRef = ref<HTMLElement>();
  137. const isUser = computed(() => props.message.role === 'user');
  138. /** 附件徽标摘要文本:表格显示行数、文档显示字数,截断/索引有标 */
  139. function badgeMeta(att: import('../api/types').AttachmentBadge) {
  140. const base = att.kind === 'TABLE' ? `${att.rows ?? 0} 行` : `${att.chars ?? 0} 字`;
  141. const extra = att.indexedChunks ? ` · ${att.indexedChunks} 片` : att.truncated ? ' · 已截断' : '';
  142. return base + extra;
  143. }
  144. const hasContent = computed(() => !!(props.message.blocks || []).some((b) => b.text?.trim()));
  145. const timeText = computed(() => {
  146. const raw = props.message.createAt;
  147. if (!raw) return '';
  148. // 后端格式 yyyy-MM-dd HH:mm:ss;本地临时消息为 ISO
  149. const normalized = raw.includes('T') ? raw : raw.replace(/-/g, '/');
  150. const d = new Date(normalized);
  151. if (Number.isNaN(d.getTime())) return '';
  152. const p = (n: number) => String(n).padStart(2, '0');
  153. return `${p(d.getHours())}:${p(d.getMinutes())}`;
  154. });
  155. </script>
  156. <style lang="less" scoped>
  157. .ai-msg {
  158. /* AI 消息:头像列 + 内容列。用 grid 而不是 flex,保证正文宽度严格等于内容列 */
  159. &--assistant {
  160. display: grid;
  161. grid-template-columns: var(--ai-avatar) minmax(0, 1fr);
  162. column-gap: var(--ai-sp-3);
  163. align-items: start;
  164. }
  165. /* 用户消息:单列右对齐,气泡宽度随内容收缩(上限 72%) */
  166. &--user {
  167. display: flex;
  168. flex-direction: column;
  169. align-items: flex-end;
  170. }
  171. /* 附件徽标:气泡下方一行小胶囊 */
  172. &__atts {
  173. display: flex;
  174. flex-wrap: wrap;
  175. gap: 4px;
  176. justify-content: flex-end;
  177. margin-top: 4px;
  178. }
  179. &__att {
  180. display: inline-flex;
  181. align-items: center;
  182. gap: 4px;
  183. max-width: 240px;
  184. height: 22px;
  185. padding: 0 8px;
  186. color: #5b6577;
  187. font-size: 11px;
  188. background: #f4f6fa;
  189. border: 1px solid #e7ebf3;
  190. border-radius: 6px;
  191. &-name {
  192. overflow: hidden;
  193. color: #111827;
  194. white-space: nowrap;
  195. text-overflow: ellipsis;
  196. }
  197. &-meta {
  198. white-space: nowrap;
  199. }
  200. }
  201. &__avatar {
  202. display: flex;
  203. align-items: center;
  204. justify-content: center;
  205. width: var(--ai-avatar);
  206. height: var(--ai-avatar);
  207. margin-top: 3px;
  208. color: #2a50ec;
  209. background: linear-gradient(135deg, #eef2ff, #dfe8ff);
  210. border: 1px solid #dbe4ff;
  211. border-radius: 9px;
  212. }
  213. &__main {
  214. min-width: 0;
  215. }
  216. &__head {
  217. display: flex;
  218. align-items: center;
  219. gap: 8px;
  220. min-height: 18px;
  221. margin-bottom: 10px;
  222. padding-bottom: 8px;
  223. border-bottom: 1px dashed #eff2f7;
  224. }
  225. &__role {
  226. display: inline-flex;
  227. align-items: center;
  228. gap: 6px;
  229. color: #111827;
  230. font-size: 13px;
  231. font-weight: 600;
  232. &::before {
  233. content: '';
  234. width: 6px;
  235. height: 6px;
  236. background: #2a50ec;
  237. border-radius: 50%;
  238. }
  239. }
  240. &__time {
  241. color: #98a2b3;
  242. font-size: 11px;
  243. font-variant-numeric: tabular-nums;
  244. }
  245. &__star {
  246. padding: 0 6px;
  247. font-size: 11px;
  248. line-height: 17px;
  249. color: @warning-color;
  250. background: fade(@warning-color, 10%);
  251. border-radius: var(--ai-radius-sm);
  252. }
  253. &__flag {
  254. padding: 0 6px;
  255. font-size: 11px;
  256. line-height: 17px;
  257. color: var(--ai-text-2);
  258. background: var(--ai-bg-subtle);
  259. border-radius: var(--ai-radius-sm);
  260. }
  261. /* 用户气泡下方的时间行(父级右对齐,故整体靠右) */
  262. &__foot {
  263. display: flex;
  264. align-items: center;
  265. gap: var(--ai-sp-2);
  266. margin-top: var(--ai-sp-1);
  267. }
  268. &__bubble {
  269. max-width: 72%;
  270. margin: 0;
  271. padding: 10px 14px;
  272. color: #111827;
  273. font-size: 14px;
  274. line-height: 1.7;
  275. white-space: pre-wrap;
  276. word-break: break-word;
  277. background: #eef2ff;
  278. border: 1px solid #dbe4ff;
  279. border-radius: 14px 14px 4px 14px;
  280. box-shadow: 0 1px 2px rgb(16 24 40 / 4%);
  281. }
  282. &__body {
  283. min-width: 0;
  284. }
  285. &__error {
  286. display: flex;
  287. align-items: flex-start;
  288. gap: var(--ai-sp-2);
  289. padding: var(--ai-sp-3) var(--ai-sp-3) var(--ai-sp-3) 10px;
  290. font-size: 13px;
  291. line-height: 1.7;
  292. color: @error-color;
  293. background: fade(@error-color, 6%);
  294. border-left: 3px solid @error-color;
  295. border-radius: 0 8px 8px 0;
  296. word-break: break-word;
  297. }
  298. &__error-icon {
  299. flex: 0 0 auto;
  300. margin-top: 2px;
  301. }
  302. &__skeleton {
  303. display: flex;
  304. flex-direction: column;
  305. gap: var(--ai-sp-2);
  306. }
  307. }
  308. </style>