<template>
|
<div class="message-item" :class="[`role-${message.role}`, { streaming: isStreaming }]">
|
<!-- 头像 -->
|
<div class="message-avatar" :class="message.role">
|
{{ message.role === 'user' ? userInitial : 'AI' }}
|
</div>
|
|
<div class="message-body">
|
<div class="message-header">
|
<span class="message-role">
|
{{ message.role === 'user' ? '你' : '助手' }}
|
<span v-if="message.model" class="message-model">{{ message.model }}</span>
|
</span>
|
|
<span class="message-actions">
|
<!-- 分支切换器:同一父节点下有多条回复时才出现 -->
|
<span v-if="branchInfo.total > 1" class="branch-switch">
|
<button
|
class="branch-btn"
|
:disabled="branchInfo.index <= 1"
|
title="上一个分支"
|
@click="$emit('switch-branch', -1)"
|
>
|
‹
|
</button>
|
<span class="branch-label">{{ branchInfo.index }}/{{ branchInfo.total }}</span>
|
<button
|
class="branch-btn"
|
:disabled="branchInfo.index >= branchInfo.total"
|
title="下一个分支"
|
@click="$emit('switch-branch', 1)"
|
>
|
›
|
</button>
|
</span>
|
|
<button
|
v-if="message.role === 'user' && !isStreaming"
|
class="action-btn"
|
title="编辑并由此产生新分支"
|
@click="startEdit"
|
>
|
编辑
|
</button>
|
<button
|
v-if="message.role === 'user' && !isStreaming"
|
class="action-btn"
|
title="重新生成回复"
|
@click="$emit('regenerate')"
|
>
|
重新生成
|
</button>
|
<button
|
v-if="!isStreaming"
|
class="action-btn"
|
title="复制内容"
|
@click="copyContent"
|
>
|
复制
|
</button>
|
<button
|
v-if="!isStreaming"
|
class="action-btn danger"
|
title="删除该消息及其后续对话"
|
@click="$emit('delete')"
|
>
|
删除
|
</button>
|
</span>
|
</div>
|
|
<!-- 编辑态 -->
|
<div v-if="editing" class="edit-area">
|
<n-input
|
v-model:value="editContent"
|
type="textarea"
|
:autosize="{ minRows: 2, maxRows: 12 }"
|
placeholder="修改消息内容"
|
/>
|
<div class="edit-actions">
|
<n-button size="small" @click="cancelEdit">取消</n-button>
|
<n-button size="small" type="primary" @click="commitEdit">保存</n-button>
|
</div>
|
</div>
|
|
<!-- 渲染态 -->
|
<div v-else>
|
<!-- 随消息上传的图片:显示原图,并可展开 OCR 文字 -->
|
<div v-if="images.length" class="message-images">
|
<div v-for="img in images" :key="img.id" class="message-image" @click="openPreview(img.id)">
|
<!--
|
必须用 AuthenticatedImage 而不是裸 <img>:
|
文件内容接口需要 Authorization 头,而 <img> 不会带自定义头,
|
直接写 src 会拿到 401 并显示成「图片损坏」。
|
-->
|
<AuthenticatedImage :file-id="img.id" alt="上传的图片" />
|
</div>
|
</div>
|
|
<div
|
class="message-content"
|
:class="{ 'typing-cursor': isStreaming }"
|
v-html="renderedContent"
|
></div>
|
|
<!-- 图片有识别文字时给个折叠入口:用户可能想核对模型看到的内容 -->
|
<div v-if="imageText" class="ocr-text">
|
<span class="ocr-toggle" @click="showOcrText = !showOcrText">
|
{{ showOcrText ? '收起' : '查看' }}图片识别文字({{ imageText.length }} 字)
|
</span>
|
<pre v-if="showOcrText" class="ocr-content">{{ imageText }}</pre>
|
</div>
|
</div>
|
|
<!-- 点击缩略图放大:复用同一个鉴权图片组件,避免再写一套取图逻辑 -->
|
<n-modal
|
v-if="previewImageId != null"
|
:show="previewImageId != null"
|
preset="card"
|
class="image-preview-modal"
|
:bordered="false"
|
@update:show="(v) => { if (!v) previewImageId = null }"
|
>
|
<AuthenticatedImage
|
:file-id="previewImageId"
|
alt="图片预览"
|
class="preview-large"
|
/>
|
</n-modal>
|
|
<!-- 停止 / 失败标记 -->
|
<div v-if="message.stopped" class="message-note">已手动停止生成</div>
|
<div v-if="message.failed" class="message-note error">生成失败</div>
|
|
<!-- RAG 引用来源:编号与正文里的 [1] 一一对应 -->
|
<div v-if="sources.length" class="rag-sources">
|
<div class="rag-sources-title">引用来源({{ sources.length }})</div>
|
<div
|
v-for="(source, index) in sources"
|
:key="index"
|
class="rag-source"
|
:title="source.snippet || ''"
|
>
|
<span class="rag-source-index">[{{ index + 1 }}]</span>
|
<span class="rag-source-body">
|
<span class="rag-source-name">{{ source.filename || '未知文档' }}</span>
|
<!-- 章节:把引用从「文件名」细化到「文档的哪一节」 -->
|
<span v-if="source.section" class="rag-source-section">
|
{{ source.section }}
|
</span>
|
</span>
|
<span v-if="source.score != null" class="rag-source-score">
|
{{ scoreLabel(source.score) }}
|
</span>
|
</div>
|
</div>
|
</div>
|
</div>
|
</template>
|
|
<script setup>
|
import { computed, ref } from 'vue'
|
import { NButton, NInput, NModal, useMessage } from 'naive-ui'
|
import AuthenticatedImage from './AuthenticatedImage.vue'
|
import { renderMarkdown } from '@/utils/markdown'
|
|
const props = defineProps({
|
message: { type: Object, required: true },
|
/** 该消息是否正在流式生成 */
|
isStreaming: { type: Boolean, default: false },
|
/** 当前显示的是第几个分支,由父组件从 store 计算后传入 */
|
branchInfo: {
|
type: Object,
|
default: () => ({ index: 0, total: 0 })
|
},
|
userInitial: { type: String, default: 'U' }
|
})
|
|
const emit = defineEmits(['switch-branch', 'edit', 'regenerate', 'delete'])
|
|
/**
|
* 注意这个命名 —— 踩过一个很隐蔽的坑:
|
*
|
* NaiveUI 的消息提示 API 习惯上写作 `const message = useMessage()`,
|
* 但本组件同时有一个名为 `message` 的 **prop**(要渲染的那条消息)。
|
* 两者同名时,`<script setup>` 中的 setup 绑定优先级高于 props,
|
* 于是模板里的 `message.role` / `message.content` 全部解析到
|
* **toast 对象**上 —— 结果是每条消息都被渲染成
|
* `role-undefined` + 「(空消息)」,而 props 明明是好的。
|
*
|
* 这类 bug 不报错、不报警告,只是内容凭空消失,非常难查。
|
* 因此这里把 toast API 改名为 `toast`,让 `message` 唯一地指向 prop。
|
* 其他组件(FileUpload / Sidebar / ChatView)没有同名 prop,
|
* 用 message 不会出问题,但新组件建议统一用 `toast`。
|
*/
|
const toast = useMessage()
|
const editing = ref(false)
|
const editContent = ref('')
|
|
const sources = computed(() => props.message.sources || [])
|
|
/** 随消息上传的图片 id 列表 */
|
const images = computed(() =>
|
(props.message.imageFileIds || []).map((id) => ({ id }))
|
)
|
|
/** 图片的 OCR 文字(有才显示折叠入口) */
|
const imageText = computed(() => props.message.imageText || '')
|
|
const showOcrText = ref(false)
|
/** 点击缩略图后要放大的图片 id(null 表示不显示预览) */
|
const previewImageId = ref(null)
|
|
function openPreview(fileId) {
|
previewImageId.value = fileId
|
}
|
|
/**
|
* 把 Chroma 的 cosine 距离换算成可读的相似度百分比。
|
*
|
* 距离 = 1 - 余弦相似度,所以相似度 = (1 - 距离)。
|
* 直接展示「距离 0.248」对用户没有意义,展示「相似度 75%」才直观。
|
*/
|
function scoreLabel(distance) {
|
const value = Number(distance)
|
if (!Number.isFinite(value)) return ''
|
const similarity = Math.max(0, Math.min(1, 1 - value))
|
return `相似度 ${(similarity * 100).toFixed(0)}%`
|
}
|
|
/**
|
* 正文渲染:Markdown → HTML。
|
*
|
* 具体的安全处理(先转义再解析、DOMPurify 兜底、引用编号高亮跳过代码块)
|
* 都封装在 utils/markdown.js 里,这里只负责把流式状态与引用数量传进去。
|
*/
|
const renderedContent = computed(() =>
|
renderMarkdown(props.message.content || '', {
|
streaming: props.isStreaming,
|
citationCount: sources.value.length
|
})
|
)
|
|
function startEdit() {
|
editing.value = true
|
editContent.value = props.message.content || ''
|
}
|
|
function cancelEdit() {
|
editing.value = false
|
editContent.value = ''
|
}
|
|
function commitEdit() {
|
const content = editContent.value.trim()
|
if (!content) {
|
toast.warning('内容不能为空')
|
return
|
}
|
editing.value = false
|
emit('edit', content)
|
}
|
|
async function copyContent() {
|
const text = props.message.content || ''
|
if (!text) return
|
|
try {
|
// navigator.clipboard 在非 HTTPS 环境下不可用(局域网 IP 访问就是这种情况),
|
// 因此保留 execCommand 回退方案
|
if (navigator.clipboard && window.isSecureContext) {
|
await navigator.clipboard.writeText(text)
|
} else {
|
const textarea = document.createElement('textarea')
|
textarea.value = text
|
textarea.style.position = 'fixed'
|
textarea.style.opacity = '0'
|
document.body.appendChild(textarea)
|
textarea.select()
|
document.execCommand('copy')
|
document.body.removeChild(textarea)
|
}
|
toast.success('已复制')
|
} catch {
|
toast.error('复制失败,请手动选择文本')
|
}
|
}
|
</script>
|
|
<style scoped>
|
.message-item {
|
display: flex;
|
gap: 14px;
|
padding: 16px 0;
|
max-width: 780px;
|
margin: 0 auto;
|
width: 100%;
|
}
|
|
/*
|
* 用户消息靠右、AI 消息靠左,符合 ChatGPT / Open-WebUI 的阅读习惯。
|
*
|
* 用 row-reverse 而不是 order:order 只改变视觉顺序,DOM 顺序仍是
|
* 「头像 → 内容」,屏幕阅读器与 Tab 焦点顺序会和视觉顺序不一致,
|
* 属于无障碍缺陷。row-reverse 翻转整个主轴,头像自然落到右侧,
|
* 且 DOM 结构完全不变。
|
*/
|
.message-item.role-user {
|
flex-direction: row-reverse;
|
}
|
|
/* 用户侧整体靠右 */
|
.message-item.role-user .message-body {
|
display: flex;
|
flex-direction: column;
|
align-items: flex-end;
|
}
|
|
/* 用户消息的标题行(角色名 + 操作按钮)同样靠右 */
|
.message-item.role-user .message-header {
|
flex-direction: row-reverse;
|
}
|
|
.message-avatar {
|
width: 30px;
|
height: 30px;
|
border-radius: 7px;
|
flex-shrink: 0;
|
display: flex;
|
align-items: center;
|
justify-content: center;
|
font-size: 12px;
|
font-weight: 600;
|
color: #fff;
|
}
|
|
.message-avatar.user {
|
background: #4b5563;
|
}
|
|
.message-avatar.assistant {
|
background: #0f766e;
|
}
|
|
.message-body {
|
flex: 1;
|
min-width: 0;
|
}
|
|
/*
|
* 用户消息套气泡底色,与 AI 的「白底通栏」形成视觉区分。
|
* 同时限制最大宽度 —— 长消息在宽屏上横向铺满会让视线来回扫,很难读。
|
*/
|
.message-item.role-user .message-content {
|
background: var(--bubble-user);
|
border-radius: 12px;
|
padding: 9px 14px;
|
max-width: 85%;
|
width: fit-content;
|
}
|
|
/* 用户消息的引用来源、状态提示同样限宽靠右 */
|
.message-item.role-user .message-images {
|
display: flex;
|
flex-wrap: wrap;
|
gap: 8px;
|
margin-bottom: 8px;
|
}
|
|
.message-image {
|
cursor: zoom-in;
|
}
|
|
/*
|
* 缩略图尺寸由 AuthenticatedImage 自己的 scoped 样式控制 ——
|
* 这里是父组件,scoped 属性作用不到子组件内部元素,写了也不生效。
|
* 只在这里定义「点开预览」时的覆盖。
|
*/
|
.message-image {
|
cursor: zoom-in;
|
}
|
|
.image-preview-modal {
|
width: auto;
|
max-width: 90vw;
|
}
|
|
/*
|
* 放大预览:用 :deep 穿透到子组件内部覆盖缩略图上限。
|
* 同时放开容器的 160px 限制,否则图片会被容器卡住放不大。
|
*/
|
.preview-large :deep(.authenticated-image) {
|
max-width: 86vw;
|
max-height: 82vh;
|
}
|
|
.preview-large :deep(.message-image-thumb) {
|
max-width: 86vw;
|
max-height: 82vh;
|
cursor: default;
|
}
|
|
.ocr-text {
|
margin-top: 8px;
|
font-size: 12px;
|
}
|
|
.ocr-toggle {
|
color: #0f766e;
|
cursor: pointer;
|
}
|
|
.ocr-toggle:hover {
|
text-decoration: underline;
|
}
|
|
.ocr-content {
|
margin: 6px 0 0;
|
padding: 8px 10px;
|
background: #f9fafb;
|
border: 1px solid var(--border-color);
|
border-radius: 6px;
|
white-space: pre-wrap;
|
word-break: break-word;
|
font-size: 12px;
|
color: var(--text-secondary);
|
max-height: 260px;
|
overflow-y: auto;
|
}
|
|
/* 用户消息里的图片靠右对齐 */
|
.message-item.role-user .message-images {
|
justify-content: flex-end;
|
}
|
|
.rag-sources {
|
max-width: 85%;
|
}
|
|
.message-item.role-user .message-note {
|
text-align: right;
|
}
|
|
/* 编辑态在用户侧也用同样的限宽,避免点「编辑」时布局跳动 */
|
.message-item.role-user .edit-area {
|
width: 85%;
|
}
|
|
.message-header {
|
display: flex;
|
align-items: center;
|
justify-content: space-between;
|
gap: 12px;
|
margin-bottom: 6px;
|
min-height: 20px;
|
}
|
|
.message-role {
|
font-size: 13px;
|
font-weight: 600;
|
color: var(--text-primary);
|
}
|
|
.message-model {
|
font-weight: 400;
|
font-size: 11.5px;
|
color: var(--text-muted);
|
margin-left: 8px;
|
padding: 1px 6px;
|
background: #f3f4f6;
|
border-radius: 4px;
|
}
|
|
.message-actions {
|
display: flex;
|
align-items: center;
|
gap: 10px;
|
/* 默认隐藏操作按钮,减少视觉噪音;悬停整条消息时出现 */
|
opacity: 0;
|
transition: opacity 0.12s ease;
|
}
|
|
.message-item:hover .message-actions {
|
opacity: 1;
|
}
|
|
.action-btn {
|
background: none;
|
border: none;
|
padding: 0;
|
font-size: 12px;
|
color: var(--text-secondary);
|
cursor: pointer;
|
font-family: inherit;
|
}
|
|
.action-btn:hover {
|
color: var(--text-primary);
|
text-decoration: underline;
|
}
|
|
.action-btn.danger:hover {
|
color: #dc2626;
|
}
|
|
.branch-switch {
|
display: flex;
|
align-items: center;
|
gap: 2px;
|
}
|
|
.branch-btn {
|
background: none;
|
border: none;
|
cursor: pointer;
|
font-size: 15px;
|
line-height: 1;
|
color: var(--text-secondary);
|
padding: 0 3px;
|
font-family: inherit;
|
}
|
|
.branch-btn:disabled {
|
color: #d1d5db;
|
cursor: default;
|
}
|
|
.branch-btn:not(:disabled):hover {
|
color: var(--text-primary);
|
}
|
|
.branch-label {
|
font-size: 12px;
|
color: var(--text-secondary);
|
font-variant-numeric: tabular-nums;
|
min-width: 26px;
|
text-align: center;
|
}
|
|
.thinking-hint {
|
color: var(--text-muted);
|
font-size: 13.5px;
|
}
|
|
.edit-area {
|
margin-top: 4px;
|
}
|
|
.edit-actions {
|
display: flex;
|
justify-content: flex-end;
|
gap: 8px;
|
margin-top: 8px;
|
}
|
|
.message-note {
|
font-size: 12px;
|
color: var(--text-muted);
|
margin-top: 8px;
|
font-style: italic;
|
}
|
|
.message-note.error {
|
color: #dc2626;
|
font-style: normal;
|
}
|
|
.message-images {
|
display: flex;
|
flex-wrap: wrap;
|
gap: 8px;
|
margin-bottom: 8px;
|
}
|
|
.message-image {
|
cursor: zoom-in;
|
}
|
|
/*
|
* 缩略图尺寸由 AuthenticatedImage 自己的 scoped 样式控制 ——
|
* 这里是父组件,scoped 属性作用不到子组件内部元素,写了也不生效。
|
* 只在这里定义「点开预览」时的覆盖。
|
*/
|
.message-image {
|
cursor: zoom-in;
|
}
|
|
.image-preview-modal {
|
width: auto;
|
max-width: 90vw;
|
}
|
|
/*
|
* 放大预览:用 :deep 穿透到子组件内部覆盖缩略图上限。
|
* 同时放开容器的 160px 限制,否则图片会被容器卡住放不大。
|
*/
|
.preview-large :deep(.authenticated-image) {
|
max-width: 86vw;
|
max-height: 82vh;
|
}
|
|
.preview-large :deep(.message-image-thumb) {
|
max-width: 86vw;
|
max-height: 82vh;
|
cursor: default;
|
}
|
|
.ocr-text {
|
margin-top: 8px;
|
font-size: 12px;
|
}
|
|
.ocr-toggle {
|
color: #0f766e;
|
cursor: pointer;
|
}
|
|
.ocr-toggle:hover {
|
text-decoration: underline;
|
}
|
|
.ocr-content {
|
margin: 6px 0 0;
|
padding: 8px 10px;
|
background: #f9fafb;
|
border: 1px solid var(--border-color);
|
border-radius: 6px;
|
white-space: pre-wrap;
|
word-break: break-word;
|
font-size: 12px;
|
color: var(--text-secondary);
|
max-height: 260px;
|
overflow-y: auto;
|
}
|
|
/* 用户消息里的图片靠右对齐 */
|
.message-item.role-user .message-images {
|
justify-content: flex-end;
|
}
|
|
.rag-sources {
|
margin-top: 12px;
|
padding: 10px 12px;
|
background: #f9fafb;
|
border: 1px solid var(--border-color);
|
border-radius: 8px;
|
}
|
|
.rag-sources-title {
|
font-size: 12px;
|
font-weight: 600;
|
color: var(--text-secondary);
|
margin-bottom: 6px;
|
}
|
|
.rag-source {
|
display: flex;
|
align-items: baseline;
|
gap: 7px;
|
font-size: 12px;
|
color: var(--text-secondary);
|
line-height: 1.75;
|
padding: 2px 0;
|
}
|
|
.rag-source-body {
|
flex: 1;
|
min-width: 0;
|
}
|
|
.rag-source-section {
|
color: var(--text-muted);
|
margin-left: 6px;
|
/* 章节名可能很长,单行截断避免把来源行撑成多行 */
|
display: inline-block;
|
max-width: 100%;
|
overflow: hidden;
|
text-overflow: ellipsis;
|
white-space: nowrap;
|
vertical-align: bottom;
|
}
|
|
.rag-source-section::before {
|
content: '·';
|
margin-right: 6px;
|
}
|
|
/*
|
* 正文里的引用编号高亮。
|
* 与下方「引用来源」列表的 [1] 视觉一致,方便用户对照核查。
|
*/
|
.message-content :deep(.citation-ref) {
|
color: #0f766e;
|
background: #ecfdf5;
|
border-radius: 3px;
|
padding: 0 3px;
|
font-size: 0.85em;
|
font-weight: 600;
|
cursor: help;
|
white-space: nowrap;
|
}
|
|
.rag-source-index {
|
color: var(--text-muted);
|
margin-right: 4px;
|
}
|
|
.rag-source-name {
|
color: var(--text-primary);
|
}
|
|
.rag-source-score {
|
color: var(--text-muted);
|
margin-left: 8px;
|
}
|
</style>
|