Просмотр исходного кода

feat:【ai 大模型】对话列表,增加 attachment-urls 附件的展示

YunaiV 6 месяцев назад
Родитель
Сommit
84deeacd4d

+ 5 - 2
src/api/ai/chat/message/index.ts

@@ -14,6 +14,7 @@ export interface ChatMessageVO {
   modelId: number // 模型编号
   content: string // 聊天内容
   reasoningContent?: string // 推理内容
+  attachmentUrls?: string[] // 附件 URL 数组
   tokens: number // 消耗 Token 数量
   segmentIds?: number[] // 段落编号
   segments?: {
@@ -45,7 +46,8 @@ export const ChatMessageApi = {
     enableContext: boolean,
     onMessage,
     onError,
-    onClose
+    onClose,
+    attachmentUrls?: string[]
   ) => {
     const token = getAccessToken()
     return fetchEventSource(`${config.base_url}/ai/chat/message/send-stream`, {
@@ -58,7 +60,8 @@ export const ChatMessageApi = {
       body: JSON.stringify({
         conversationId,
         content,
-        useContext: enableContext
+        useContext: enableContext,
+        attachmentUrls: attachmentUrls || []
       }),
       onmessage: onMessage,
       onerror: onError,

+ 19 - 0
src/utils/file.ts

@@ -0,0 +1,19 @@
+/** 从 URL 中提取文件名 */
+export const getFileNameFromUrl = (url: string): string => {
+  try {
+    const urlObj = new URL(url)
+    const pathname = urlObj.pathname
+    const fileName = pathname.split('/').pop() || 'unknown'
+    return decodeURIComponent(fileName)
+  } catch {
+    // 如果 URL 解析失败,尝试从字符串中提取
+    const parts = url.split('/')
+    return parts[parts.length - 1] || 'unknown'
+  }
+}
+
+/** 判断是否为图片 */
+export const isImage = (filename: string): boolean => {
+  const ext = filename.split('.').pop()?.toLowerCase() || ''
+  return ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'].includes(ext)
+}

+ 0 - 1
src/utils/index.ts

@@ -529,7 +529,6 @@ export function jsonParse(str: string) {
  * @param start 开始位置
  * @param end 结束位置
  */
-
 export const subString = (str: string, start: number, end: number) => {
   if (str.length > end) {
     return str.slice(start, end)

+ 165 - 0
src/views/ai/chat/index/components/message/MessageFiles.vue

@@ -0,0 +1,165 @@
+<template>
+  <div v-if="attachmentUrls && attachmentUrls.length > 0" class="message-files">
+    <div class="files-container">
+      <div
+        v-for="(url, index) in attachmentUrls"
+        :key="index"
+        class="file-card"
+        @click="handleFileClick(url)"
+      >
+        <div class="file-icon-wrapper">
+          <div class="file-icon" :class="getFileTypeClass(getFileNameFromUrl(url))">
+            <Icon :icon="getFileIcon(getFileNameFromUrl(url))" :size="20" />
+          </div>
+        </div>
+        <div class="file-content">
+          <div class="file-name" :title="getFileNameFromUrl(url)">
+            {{ getFileNameFromUrl(url) }}
+          </div>
+        </div>
+      </div>
+    </div>
+  </div>
+</template>
+
+<script setup lang="ts">
+import { getFileNameFromUrl, isImage } from '@/utils/file'
+
+defineOptions({ name: 'MessageFiles' })
+
+defineProps<{
+  attachmentUrls?: string[]
+}>()
+
+/** 获取文件图标 */
+const getFileIcon = (filename: string): string => {
+  const ext = filename.split('.').pop()?.toLowerCase() || ''
+  if (isImage(ext)) {
+    return 'ep:picture'
+  }
+  return 'ep:document'
+}
+
+/** 获取文件类型样式类 */
+const getFileTypeClass = (filename: string): string => {
+  const ext = filename.split('.').pop()?.toLowerCase() || ''
+  if (isImage(ext)) {
+    return 'file-image'
+  } else if (['pdf'].includes(ext)) {
+    return 'file-pdf'
+  } else if (['doc', 'docx'].includes(ext)) {
+    return 'file-word'
+  } else if (['xls', 'xlsx'].includes(ext)) {
+    return 'file-excel'
+  } else if (['ppt', 'pptx'].includes(ext)) {
+    return 'file-powerpoint'
+  } else if (['mp3', 'wav', 'm4a', 'aac'].includes(ext)) {
+    return 'file-audio'
+  } else if (['mp4', 'avi', 'mov', 'wmv'].includes(ext)) {
+    return 'file-video'
+  } else {
+    return 'file-default'
+  }
+}
+
+/** 点击文件 */
+const handleFileClick = (url: string) => {
+  window.open(url, '_blank')
+}
+</script>
+
+<style scoped>
+.message-files {
+  margin-top: 8px;
+}
+
+.files-container {
+  display: flex;
+  flex-wrap: wrap;
+  gap: 8px;
+}
+
+.file-card {
+  display: flex;
+  align-items: center;
+  padding: 12px;
+  background: #f5f5f5;
+  border-radius: 8px;
+  cursor: pointer;
+  transition: all 0.2s ease;
+  min-width: 160px;
+  max-width: 280px;
+  border: 1px solid transparent;
+}
+
+.file-card:hover {
+  background: #ebebeb;
+  transform: translateY(-1px);
+  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
+}
+
+.file-icon-wrapper {
+  margin-right: 12px;
+  flex-shrink: 0;
+}
+
+.file-icon {
+  display: flex;
+  align-items: center;
+  justify-content: center;
+  width: 32px;
+  height: 32px;
+  border-radius: 6px;
+  color: white;
+  font-weight: bold;
+}
+
+/* 不同文件类型的图标背景色 */
+.file-icon.file-image {
+  background: linear-gradient(135deg, #ffd700, #ffa500);
+}
+
+.file-icon.file-pdf {
+  background: linear-gradient(135deg, #ff4444, #cc1111);
+}
+
+.file-icon.file-word {
+  background: linear-gradient(135deg, #2b5797, #1e3f66);
+}
+
+.file-icon.file-excel {
+  background: linear-gradient(135deg, #1d6f42, #0f4728);
+}
+
+.file-icon.file-powerpoint {
+  background: linear-gradient(135deg, #d24726, #a73319);
+}
+
+.file-icon.file-audio {
+  background: linear-gradient(135deg, #9c27b0, #6a1b9a);
+}
+
+.file-icon.file-video {
+  background: linear-gradient(135deg, #f44336, #c62828);
+}
+
+.file-icon.file-default {
+  background: linear-gradient(135deg, #607d8b, #455a64);
+}
+
+.file-content {
+  flex: 1;
+  min-width: 0;
+}
+
+.file-name {
+  font-size: 14px;
+  font-weight: 500;
+  color: #333;
+  line-height: 1.3;
+  margin-bottom: 4px;
+  overflow: hidden;
+  text-overflow: ellipsis;
+  white-space: nowrap;
+}
+</style>

+ 8 - 0
src/views/ai/chat/index/components/message/MessageList.vue

@@ -22,6 +22,7 @@
               class="text-[var(--el-text-color-primary)] text-[0.95rem]"
               :content="item.content"
             />
+            <MessageFiles :attachment-urls="item.attachmentUrls" />
             <MessageKnowledge v-if="item.segments" :segments="item.segments" />
           </div>
           <div class="flex flex-row mt-8px">
@@ -52,8 +53,14 @@
           <div>
             <el-text class="text-left leading-30px">{{ formatDate(item.createTime) }}</el-text>
           </div>
+          <!-- 附件显示行 -->
+          <div v-if="item.attachmentUrls && item.attachmentUrls.length > 0" class="flex flex-row-reverse mb-8px">
+            <MessageFiles :attachment-urls="item.attachmentUrls" />
+          </div>
+          <!-- 文本内容行 -->
           <div class="flex flex-row-reverse">
             <div
+              v-if="item.content && item.content.trim()"
               class="text-[0.95rem] text-[var(--el-color-white)] inline bg-[var(--el-color-primary)] shadow-[0_0_0_1px_var(--el-color-primary)] rounded-10px p-10px w-auto break-words whitespace-pre-wrap"
             >
               {{ item.content }}
@@ -104,6 +111,7 @@ import { formatDate } from '@/utils/formatTime'
 import MarkdownView from '@/components/MarkdownView/index.vue'
 import MessageKnowledge from './MessageKnowledge.vue'
 import MessageReasoning from './MessageReasoning.vue'
+import MessageFiles from './MessageFiles.vue'
 import { useClipboard } from '@vueuse/core'
 import { ArrowDownBold, Edit, RefreshRight } from '@element-plus/icons-vue'
 import { ChatMessageApi, ChatMessageVO } from '@/api/ai/chat/message'