综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.vue 56KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  1. <template>
  2. <div class="app-container">
  3. <!-- 主要内容区域 -->
  4. <div class="main-content">
  5. <!-- 左侧知识库列表 -->
  6. <div class="left-panel">
  7. <div class="panel-header">
  8. <h3>知识库列表</h3>
  9. <span class="knowledge-count">{{ knowledgeList.length }} 个知识库</span>
  10. <el-button type="primary" size="small" @click="handleAdd" v-hasPermi="['llm:knowledge:add']"
  11. icon="el-icon-plus">
  12. 新增知识库
  13. </el-button>
  14. </div>
  15. <!-- 搜索框 -->
  16. <div class="search-box">
  17. <el-input v-model="queryParams.collectionName" placeholder="请输入知识库名称" clearable @clear="handleQuery"
  18. @keyup.enter.native="handleQuery">
  19. <template slot="append">
  20. <el-button @click="handleQuery" icon="el-icon-search">
  21. </el-button>
  22. </template>
  23. </el-input>
  24. </div>
  25. <div class="knowledge-cards" v-loading="loading">
  26. <div v-for="(item, index) in knowledgeList" :key="index" class="knowledge-card"
  27. :class="{ 'active': (selectedKnowledge ? selectedKnowledge.collectionName : '') === item.collectionName }"
  28. @click="selectKnowledge(item)">
  29. <div class="card-header">
  30. <div class="card-title">
  31. <i class="el-icon-folder folder-icon">
  32. </i>
  33. <span class="title-text">{{ item.collectionName }}</span>
  34. </div>
  35. <div class="card-actions">
  36. <el-dropdown trigger="click" @command="handleCardAction">
  37. <el-button type="text" size="small" icon="el-icon-more">
  38. </el-button>
  39. <el-dropdown-menu slot="dropdown">
  40. <el-dropdown-item :command="{ action: 'edit', data: item }" icon="el-icon-edit">编辑</el-dropdown-item>
  41. <el-dropdown-item :command="{ action: 'upload', data: item }"
  42. icon="el-icon-upload">上传文件</el-dropdown-item>
  43. <el-dropdown-item :command="{ action: 'delete', data: item }" icon="el-icon-delete"
  44. divided>删除</el-dropdown-item>
  45. </el-dropdown-menu>
  46. </el-dropdown>
  47. </div>
  48. </div>
  49. <div class="card-content">
  50. <p class="description">{{ item.description || '暂无描述' }}</p>
  51. <div class="meta-info">
  52. <span class="create-time">{{ parseTime(item.createdTime, '{y}-{m}-{d}') }}</span>
  53. <span class="file-count">{{ item.fileCount || 0 }} 个文件</span>
  54. </div>
  55. </div>
  56. </div>
  57. <!-- 空状态 - 无论是否搜索,当列表为空且未加载时都显示创建按钮 -->
  58. <div v-if="knowledgeList.length === 0 && !loading" class="empty-state">
  59. <i class="el-icon-folder-opened empty-icon"></i>
  60. <p>{{ queryParams.collectionName ? '搜索结果为空' : '暂无知识库' }}</p>
  61. <el-button type="primary" @click="handleAdd">{{ queryParams.collectionName ? '创建知识库' : '创建第一个知识库'
  62. }}</el-button>
  63. </div>
  64. </div>
  65. </div>
  66. <!-- 右侧面板 -->
  67. <div class="right-panel">
  68. <div class="panel-header">
  69. <h3>{{ isChatMode ? '知识库对话' : (isTitleMode ? '标题列表' : '文件列表') }}</h3>
  70. <div class="header-actions">
  71. <el-button v-if="!isChatMode && !isTitleMode" type="primary" icon="el-icon-chat-round" @click="handleChat(selectedKnowledge)"
  72. :disabled="!selectedKnowledge" v-hasPermi="['llm:knowledge:chat']">
  73. 开始对话
  74. </el-button>
  75. <el-button v-if="!isChatMode && !isTitleMode" type="primary" icon="el-icon-upload" @click="handleUpload(selectedKnowledge)"
  76. :disabled="!selectedKnowledge" v-hasPermi="['llm:knowledge:upload']">
  77. 上传文件
  78. </el-button>
  79. <el-button v-if="!isChatMode && !isTitleMode" type="default" icon="el-icon-notebook-2" @click="switchToTitleMode"
  80. :disabled="!selectedKnowledge">
  81. 查看内容
  82. </el-button>
  83. <el-button v-if="isChatMode" type="default" icon="el-icon-document" @click="switchToFileMode">
  84. 返回文件列表
  85. </el-button>
  86. <el-button v-if="isTitleMode" type="default" icon="el-icon-document" @click="switchToFileMode">
  87. 返回文件列表
  88. </el-button>
  89. </div>
  90. </div>
  91. <!-- 文件列表模式 -->
  92. <div v-if="!isChatMode && !isTitleMode" class="file-content" v-loading="fileLoading">
  93. <div v-if="selectedKnowledge" class="selected-knowledge">
  94. <i class="el-icon-folder folder-icon">
  95. </i>
  96. <span class="knowledge-name">{{ selectedKnowledge.collectionName }}</span>
  97. </div>
  98. <div v-if="!selectedKnowledge" class="empty-state">
  99. <i class="el-icon-document empty-icon">
  100. </i>
  101. <p>请选择一个知识库查看文件</p>
  102. </div>
  103. <div v-else-if="fileList.length === 0" class="empty-state">
  104. <i class="el-icon-document empty-icon">
  105. </i>
  106. <p>该知识库暂无文件</p>
  107. <el-button type="primary" @click="handleUpload(selectedKnowledge)">上传文件</el-button>
  108. </div>
  109. <div v-else class="file-list">
  110. <div v-for="(file, index) in pagedFileList" :key="index" class="file-item">
  111. <div class="file-info">
  112. <i class="el-icon-document file-icon">
  113. </i>
  114. <div class="file-details">
  115. <div class="file-name">{{ file }}</div>
  116. <div class="file-meta">
  117. <span class="file-type">{{ getFileType(file) }}</span>
  118. </div>
  119. </div>
  120. </div>
  121. <div class="file-actions">
  122. <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDeleteFile(file)">
  123. 删除
  124. </el-button>
  125. </div>
  126. </div>
  127. </div>
  128. <!-- 文件列表分页 -->
  129. <pagination v-show="fileTotal > 0" :total="fileTotal" :page.sync="filePageNum" :limit.sync="filePageSize"
  130. @pagination="handleFilePagination" :autoScroll="false" />
  131. </div>
  132. <!-- 标题列表模式 -->
  133. <div v-if="!isChatMode && isTitleMode" class="title-content" v-loading="titleLoading">
  134. <div v-if="selectedKnowledge" class="selected-knowledge">
  135. <i class="el-icon-folder folder-icon">
  136. </i>
  137. <span class="knowledge-name">{{ selectedKnowledge.collectionName }}</span>
  138. <span class="title-mode-badge">标题模式</span>
  139. </div>
  140. <div v-if="!selectedKnowledge" class="empty-state">
  141. <i class="el-icon-notebook-2 empty-icon">
  142. </i>
  143. <p>请选择一个知识库查看内容</p>
  144. </div>
  145. <div v-else-if="titleList.length === 0" class="empty-state">
  146. <i class="el-icon-notebook-2 empty-icon">
  147. </i>
  148. <p>该知识库暂无标题</p>
  149. </div>
  150. <div v-else class="title-content-container">
  151. <!-- 左侧标题列表 -->
  152. <div class="title-list">
  153. <div v-for="(title, index) in titleList" :key="index" class="title-item"
  154. :class="{ 'active': selectedTitle === title }"
  155. @click="selectTitle(title)">
  156. <i class="el-icon-notebook-2 title-icon"></i>
  157. <span class="title-text">{{ title }}</span>
  158. </div>
  159. </div>
  160. <!-- 右侧内容列表 -->
  161. <div class="content-list" v-loading="contentLoading">
  162. <div v-if="!selectedTitle" class="empty-state">
  163. <i class="el-icon-document empty-icon"></i>
  164. <p>请选择一个标题查看内容</p>
  165. </div>
  166. <div v-else-if="contentList.length === 0" class="empty-state">
  167. <i class="el-icon-document empty-icon"></i>
  168. <p>该标题暂无内容</p>
  169. </div>
  170. <div v-else class="content-items">
  171. <div v-for="(content, index) in contentList" :key="index" class="content-item">
  172. <div class="content-header">
  173. <span class="content-index">{{ index + 1 }}</span>
  174. </div>
  175. <div class="content-body">
  176. {{ content }}
  177. </div>
  178. </div>
  179. </div>
  180. </div>
  181. </div>
  182. </div>
  183. <!-- 聊天模式 -->
  184. <div v-else class="chat-content">
  185. <div v-if="selectedKnowledge" class="selected-knowledge">
  186. <i class="el-icon-folder folder-icon">
  187. </i>
  188. <span class="knowledge-name">{{ selectedKnowledge.collectionName }}</span>
  189. <span class="chat-mode-badge">对话模式</span>
  190. </div>
  191. <!-- 聊天消息区域 -->
  192. <div class="chat-messages" ref="chatMessagesRef">
  193. <div v-if="chatMessages.length === 0" class="welcome-message">
  194. <div class="welcome-content">
  195. <div class="welcome-icon">🤖</div>
  196. <h3>知识库助手</h3>
  197. <p>我是基于「{{ selectedKnowledge ? selectedKnowledge.collectionName : "" }}」知识库的AI助手</p>
  198. <p>您可以询问关于该知识库内容的问题,我会为您提供准确的答案</p>
  199. </div>
  200. </div>
  201. <div v-else class="message-list">
  202. <div v-for="(message, index) in chatMessages" :key="index" class="message-item"
  203. :class="[message.type, { 'typing': message.type === 'ai' && isSending && index === chatMessages.length - 1 }]">
  204. <div class="message-avatar">
  205. <div v-if="message.type === 'user'" class="user-avatar">
  206. <i class="el-icon-user"></i>
  207. </div>
  208. <div v-else class="ai-avatar">
  209. 🤖
  210. </div>
  211. </div>
  212. <div class="message-content">
  213. <div class="message-bubble" :class="message.type">
  214. <div class="message-text" v-html="formatMessage(message.content)"></div>
  215. <!-- AI回答的引用文件信息 -->
  216. <div v-if="message.type === 'ai' && message.references && message.references.length > 0"
  217. class="message-references">
  218. <div class="references-header">
  219. <i class="el-icon-document reference-icon"></i>
  220. <span>参考文件 ({{ message.references.length }})</span>
  221. <el-button v-if="message.references.length > 3" type="text" size="small" class="toggle-references"
  222. @click="toggleReferences(index)">
  223. {{ message.showAllReferences ? '收起' : '展开全部' }}
  224. </el-button>
  225. </div>
  226. <div class="references-list">
  227. <div v-for="(ref, refIndex) in getDisplayedReferences(message)" :key="refIndex"
  228. class="reference-item">
  229. <div class="reference-info">
  230. <span class="reference-filename" :title="ref.fileName">{{ ref.fileName }}</span>
  231. <span class="reference-similarity" :class="getSimilarityClass(ref.similarity)">
  232. 相似度: {{ formatSimilarity(ref.similarity) }}%
  233. </span>
  234. </div>
  235. </div>
  236. </div>
  237. </div>
  238. <div class="message-time">{{ message.time }}</div>
  239. </div>
  240. </div>
  241. </div>
  242. <!-- 加载状态:仅在尚未收到首段流内容时显示,避免出现两个AI气泡 -->
  243. <div v-if="isSending && !streamingStarted" class="message-item ai">
  244. <div class="message-avatar">
  245. <div class="ai-avatar">
  246. 🤖
  247. </div>
  248. </div>
  249. <div class="message-content">
  250. <div class="message-bubble ai">
  251. <div class="loading-dots">
  252. <span></span><span></span><span></span>
  253. </div>
  254. </div>
  255. </div>
  256. </div>
  257. </div>
  258. </div>
  259. <!-- 输入区域 -->
  260. <div class="chat-input-area">
  261. <div class="input-container">
  262. <el-input v-model="chatInput" type="textarea" :rows="3"
  263. :placeholder="isSending ? 'AI正在思考中...' : '请输入您的问题...'" @keyup.enter.native="handleEnter"
  264. @keyup.ctrl.enter.native="handleCtrlEnter" :disabled="isSending" resize="none" />
  265. <div class="input-actions">
  266. <el-button v-if="!isSending" type="primary" icon="el-icon-promotion" @click="sendMessage"
  267. :disabled="!chatInput.trim() || chatInput.length > 2000">
  268. 发送
  269. </el-button>
  270. <el-button v-else type="danger" icon="el-icon-delete" @click="stopGenerating">
  271. 停止生成
  272. </el-button>
  273. </div>
  274. </div>
  275. <div class="input-tips">
  276. <span>按 Enter 发送,Ctrl + Enter 换行</span>
  277. <span v-if="chatInput" class="char-count" :class="{ 'warning': chatInput.length > 1500 }">
  278. {{ chatInput.length }} 字符
  279. <span v-if="chatInput.length > 2000" class="error-text">(超出限制)</span>
  280. </span>
  281. </div>
  282. </div>
  283. </div>
  284. </div>
  285. </div>
  286. <!-- 添加或修改知识库对话框 -->
  287. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  288. <el-form ref="knowledgeFormRef" :model="form" :rules="rules" label-width="120px">
  289. <el-form-item label="知识库名称" prop="collectionName">
  290. <el-input v-model="form.collectionName" placeholder="请输入知识库名称" :disabled="isModify" />
  291. </el-form-item>
  292. <el-form-item label="新知识库名称" prop="newCollectionName" v-if="isModify">
  293. <el-input v-model="form.newCollectionName" placeholder="请输入新知识库名称" />
  294. </el-form-item>
  295. <el-form-item label="描述" prop="description" v-if="!isModify">
  296. <el-input type="textarea" v-model="form.description" placeholder="请输入描述" />
  297. </el-form-item>
  298. </el-form>
  299. <div slot="footer" class="dialog-footer">
  300. <el-button type="primary" @click="submitForm">确 定</el-button>
  301. <el-button @click="cancel">取 消</el-button>
  302. </div>
  303. </el-dialog>
  304. <!-- 上传文件对话框 -->
  305. <el-dialog title="上传文件到知识库" :visible.sync="uploadOpen" width="500px" append-to-body>
  306. <el-form ref="uploadFormRef" :model="uploadForm" :rules="uploadRules" label-width="80px">
  307. <el-form-item label="知识库" prop="collectionName">
  308. <el-input v-model="uploadForm.collectionName" disabled />
  309. </el-form-item>
  310. <el-form-item label="选择文件" prop="file">
  311. <el-upload ref="knowledgeUpload" multiple accept=".docx,.doc,.pdf" :headers="upload.headers" :action="''"
  312. :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess"
  313. :auto-upload="false" drag :on-change="handleFileChange">
  314. <i class="el-icon-upload"></i>
  315. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  316. <div slot="tip" class="el-upload__tip text-center">
  317. <div class="el-upload__tip">
  318. <el-checkbox v-model="upload.updateSupport" />
  319. 是否更新已经存在的文件数据
  320. </div>
  321. <span>支持 .docx、.doc、.pdf 格式文件</span>
  322. </div>
  323. </el-upload>
  324. </el-form-item>
  325. </el-form>
  326. <div slot="footer" class="dialog-footer">
  327. <el-button type="primary" @click="submitUpload">确 定</el-button>
  328. <el-button @click="cancelUpload">取 消</el-button>
  329. </div>
  330. <div v-if="percentageDisplay">
  331. <el-progress :percentage="percentage" :stroke-width="25" :text-inside="true">
  332. <el-button text>{{ progress }}</el-button>
  333. </el-progress>
  334. </div>
  335. </el-dialog>
  336. </div>
  337. </template>
  338. <script>
  339. import { parseTime } from "@/utils/ruoyi";
  340. import { Message } from 'element-ui'
  341. import { getToken } from "@/utils/auth";
  342. import { listKnowledge, listKnowLedgeByCollectionName, addKnowledge, updateKnowledge, delKnowledge, insertKnowledgeFile, listKnowledgeDocument, deleteKnowledgeFile, getProcessValue, listTiles, listByTitle } from "@/api/llm/knowLedge";
  343. import { getAnswer, getAnswerStream, getContextFile } from '@/api/llm/rag';
  344. import { marked } from 'marked';
  345. function createTypewriter(appender, options = {}) {
  346. const intervalMs = typeof options.intervalMs === 'number' ? options.intervalMs : 25 // 约 40 字/秒
  347. const maxCharsPerTick = typeof options.maxCharsPerTick === 'number' ? options.maxCharsPerTick : 1
  348. let queue = ''
  349. let timer = null
  350. let ended = false
  351. let onDrained = null
  352. const tick = () => {
  353. if (!queue) {
  354. if (ended) {
  355. if (timer) clearInterval(timer)
  356. timer = null
  357. if (onDrained) onDrained()
  358. }
  359. return
  360. }
  361. const n = Math.min(maxCharsPerTick, queue.length)
  362. const chunk = queue.slice(0, n)
  363. queue = queue.slice(n)
  364. appender(chunk)
  365. }
  366. return {
  367. push(text) {
  368. if (!text) return
  369. queue += text
  370. if (!timer) timer = setInterval(tick, intervalMs)
  371. },
  372. end(cb) {
  373. ended = true
  374. onDrained = cb
  375. if (!timer) timer = setInterval(tick, intervalMs)
  376. },
  377. stop() {
  378. ended = true
  379. queue = ''
  380. if (timer) clearInterval(timer)
  381. timer = null
  382. }
  383. }
  384. }
  385. function trimToLastSentenceEnd(text) {
  386. const s = String(text || '')
  387. // 句末标点:中文/英文句号、问号、叹号,或换行
  388. const last = Math.max(
  389. s.lastIndexOf('。'),
  390. s.lastIndexOf('!'),
  391. s.lastIndexOf('?'),
  392. s.lastIndexOf('.'),
  393. s.lastIndexOf('!'),
  394. s.lastIndexOf('?'),
  395. s.lastIndexOf('\n')
  396. )
  397. if (last === -1) return s.trim()
  398. return s.slice(0, last + 1).trim()
  399. }
  400. export default {
  401. name: 'KnowledgeManager',
  402. // 处理引用文件的显示状态
  403. data() {
  404. return {
  405. knowledgeList: [],
  406. open: false,
  407. uploadOpen: false,
  408. loading: true,
  409. fileLoading: false,
  410. percentage: 0,
  411. progress: "",
  412. percentageDisplay: false,
  413. showSearch: true,
  414. ids: [],
  415. single: true,
  416. multiple: true,
  417. total: 0,
  418. title: "",
  419. isModify: false,
  420. selectedKnowledge: null,
  421. fileList: [],
  422. // 文件分页相关状态
  423. fileTotal: 0,
  424. filePageNum: 1,
  425. filePageSize: 10,
  426. // 聊天相关状态
  427. isChatMode: false,
  428. chatMessages: [],
  429. chatInput: '',
  430. isSending: false,
  431. streamingStarted: false,
  432. chatMessagesRef: null,
  433. // 表单数据
  434. form: {
  435. collectionName: '',
  436. description: '',
  437. newCollectionName: ''
  438. },
  439. // 查询参数
  440. queryParams: {
  441. collectionName: ''
  442. },
  443. // 表单验证规则
  444. rules: {
  445. collectionName: [
  446. { required: true, message: "知识库名称不能为空", trigger: "blur" }
  447. ]
  448. },
  449. // 上传配置
  450. upload: {
  451. file: null,
  452. fileList: [],
  453. // 是否禁用上传
  454. isUploading: false,
  455. // 是否更新已经存在的文件数据
  456. updateSupport: true,
  457. // 设置上传的请求头部
  458. headers: { Authorization: "Bearer " + getToken() }
  459. },
  460. // 上传表单
  461. uploadForm: {
  462. collectionName: '',
  463. file: null
  464. },
  465. // 上传验证规则
  466. uploadRules: {
  467. file: [
  468. { required: true, message: "请选择要上传的文件", trigger: "blur" }
  469. ],
  470. collectionName: [
  471. { required: true, message: "请选择知识库", trigger: "blur" }
  472. ]
  473. },
  474. // 定时器
  475. timer: null,
  476. // 标题和内容相关状态
  477. isTitleMode: false, // 是否显示标题模式
  478. titleList: [], // 标题列表
  479. contentList: [], // 内容列表
  480. selectedTitle: null, // 选中的标题
  481. titleLoading: false, // 标题加载状态
  482. contentLoading: false // 内容加载状态
  483. }
  484. },
  485. mounted() {
  486. // 重置状态,确保页面刷新后不会停留在对话模式或标题模式
  487. this.isChatMode = false;
  488. this.isTitleMode = false;
  489. this.chatMessages = [];
  490. this.chatInput = '';
  491. this.streamingStarted = false;
  492. this.titleList = [];
  493. this.contentList = [];
  494. this.selectedTitle = null;
  495. this.getList();
  496. },
  497. computed: {
  498. pagedFileList() {
  499. const start = (this.filePageNum - 1) * this.filePageSize;
  500. const end = start + this.filePageSize;
  501. return this.fileList.slice(start, end);
  502. }
  503. },
  504. // 生命周期钩子
  505. beforeDestroy() {
  506. // 组件卸载时清理请求控制器和超时定时器
  507. if (window.currentController) {
  508. window.currentController.abort();
  509. window.currentController = null;
  510. }
  511. if (window.responseTimeout) {
  512. clearTimeout(window.responseTimeout);
  513. window.responseTimeout = null;
  514. }
  515. },
  516. methods: {
  517. /** 查询知识库列表 */
  518. getList() {
  519. this.loading = true;
  520. // 将查询参数传递给API调用
  521. if (this.queryParams.collectionName == '') {
  522. listKnowledge().then(response => {
  523. // 确保返回的数据是数组格式
  524. if (Array.isArray(response.data)) {
  525. this.knowledgeList = response.data;
  526. console.log(this.knowledgeList);
  527. this.knowledgeList.map(item => {
  528. listKnowledgeDocument(item.collectionName).then(response => {
  529. item.fileCount = response.data.length;
  530. })
  531. })
  532. } else {
  533. this.knowledgeList = [];
  534. }
  535. this.loading = false;
  536. }).catch(error => {
  537. this.loading = false;
  538. this.$modal.msgError("获取知识库列表失败:" + error.message);
  539. });
  540. }
  541. else {
  542. listKnowLedgeByCollectionName({ collectionName: this.queryParams.collectionName }).then(response => {
  543. // 确保返回的数据是数组格式
  544. if (Array.isArray(response.data)) {
  545. this.knowledgeList = response.data;
  546. this.knowledgeList.map(item => {
  547. listKnowledgeDocument(item.collectionName).then(response => {
  548. item.fileCount = response.data.length;
  549. })
  550. })
  551. } else {
  552. this.knowledgeList = [];
  553. }
  554. this.loading = false;
  555. }).catch(error => {
  556. this.loading = false;
  557. this.$modal.msgError("获取知识库列表失败:" + error.message);
  558. });
  559. }
  560. },
  561. /** 选择知识库 */
  562. selectKnowledge(knowledge) {
  563. // 如果正在生成回答,先停止
  564. if (this.isSending) {
  565. this.stopGenerating();
  566. }
  567. this.selectedKnowledge = knowledge;
  568. // 重置聊天模式
  569. this.isChatMode = false;
  570. this.chatMessages = [];
  571. this.chatInput = '';
  572. this.streamingStarted = false;
  573. // 重置文件分页状态
  574. this.filePageNum = 1;
  575. this.filePageSize = 10;
  576. this.fileLoading = true;
  577. listKnowledgeDocument(knowledge.collectionName).then(response => {
  578. this.fileList = response.data;
  579. this.fileTotal = response.data.length;
  580. this.fileLoading = false;
  581. }).catch(error => {
  582. this.fileLoading = false;
  583. this.$modal.msgError("获取文件列表失败:" + error.message);
  584. });
  585. },
  586. /** 开始对话 */
  587. handleChat(knowledge) {
  588. if (!knowledge) {
  589. this.$modal.msgWarning("请先选择要对话的知识库");
  590. return;
  591. }
  592. // 如果正在生成回答,先停止
  593. if (this.isSending) {
  594. this.stopGenerating();
  595. }
  596. this.isChatMode = true;
  597. this.chatMessages = [];
  598. this.chatInput = '';
  599. this.streamingStarted = false;
  600. // 滚动到底部
  601. this.$nextTick(() => {
  602. this.scrollToBottom();
  603. });
  604. },
  605. /** 切换到文件模式 */
  606. switchToFileMode() {
  607. // 如果正在生成回答,先停止
  608. if (this.isSending) {
  609. this.stopGenerating();
  610. }
  611. this.isChatMode = false;
  612. this.isTitleMode = false;
  613. },
  614. /** 切换到标题模式 */
  615. switchToTitleMode() {
  616. this.isTitleMode = true;
  617. this.isChatMode = false;
  618. this.titleList = [];
  619. this.contentList = [];
  620. this.selectedTitle = null;
  621. // 获取标题列表
  622. this.titleLoading = true;
  623. listTiles(this.selectedKnowledge.collectionName).then(response => {
  624. if (Array.isArray(response.data)) {
  625. this.titleList = response.data;
  626. } else {
  627. this.titleList = [];
  628. }
  629. this.titleLoading = false;
  630. }).catch(error => {
  631. this.titleLoading = false;
  632. this.$modal.msgError("获取标题列表失败:" + error.message);
  633. });
  634. },
  635. /** 选择标题 */
  636. selectTitle(title) {
  637. this.selectedTitle = title;
  638. this.contentList = [];
  639. // 获取内容列表
  640. this.contentLoading = true;
  641. listByTitle(this.selectedKnowledge.collectionName, title).then(response => {
  642. if (Array.isArray(response.data)) {
  643. this.contentList = response.data;
  644. } else {
  645. this.contentList = [];
  646. }
  647. this.contentLoading = false;
  648. }).catch(error => {
  649. this.contentLoading = false;
  650. this.$modal.msgError("获取内容列表失败:" + error.message);
  651. });
  652. },
  653. /** 发送消息 */
  654. sendMessage() {
  655. if (!this.chatInput.trim()) return;
  656. // 如果正在发送,先停止当前请求
  657. if (this.isSending) {
  658. this.stopGenerating();
  659. }
  660. const userMessage = {
  661. type: 'user',
  662. content: this.chatInput.trim(),
  663. time: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
  664. };
  665. this.chatMessages.push(userMessage);
  666. const currentInput = this.chatInput;
  667. this.chatInput = '';
  668. this.isSending = true;
  669. this.streamingStarted = false;
  670. // 滚动到底部
  671. this.$nextTick(() => {
  672. this.scrollToBottom();
  673. });
  674. // 创建AI消息占位符
  675. const aiMessage = {
  676. type: 'ai',
  677. content: '',
  678. time: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
  679. references: [], // 引用的文件信息
  680. showAllReferences: false // 是否显示所有引用文件
  681. };
  682. this.chatMessages.push(aiMessage);
  683. const typewriter = createTypewriter((chunk) => {
  684. aiMessage.content += chunk
  685. aiMessage.time = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
  686. this.$nextTick(() => this.scrollToBottom())
  687. }, {
  688. intervalMs: 25,
  689. maxCharsPerTick: 1
  690. })
  691. // 使用流式API获取回答
  692. const eventSource = getAnswerStream(
  693. currentInput,
  694. this.selectedKnowledge.collectionName,
  695. // onMessage: 接收到每个字符时的回调
  696. (content) => {
  697. const that = this;
  698. if (!that.streamingStarted) that.streamingStarted = true;
  699. if (!content || !String(content).trim()) {
  700. return;
  701. }
  702. typewriter.push(String(content))
  703. if (window.responseTimeout) {
  704. clearTimeout(window.responseTimeout);
  705. }
  706. window.responseTimeout = setTimeout(() => {
  707. if (that.isSending) {
  708. console.log('=== 响应超时强制结束 ===');
  709. that.isSending = false;
  710. typewriter.stop()
  711. if (window.responseTimeout) {
  712. clearTimeout(window.responseTimeout);
  713. window.responseTimeout = null;
  714. }
  715. }
  716. }, 300000);
  717. },
  718. // onError: 发生错误时的回调
  719. (error) => {
  720. const that = this;
  721. console.error('=== 流式回答错误 ===', error);
  722. if (window.responseTimeout) {
  723. clearTimeout(window.responseTimeout);
  724. window.responseTimeout = null;
  725. }
  726. if (aiMessage.content === '') {
  727. aiMessage.content = '抱歉,我暂时无法回答您的问题,请稍后再试。';
  728. } else {
  729. aiMessage.content += '\n\n[回答生成中断]';
  730. }
  731. that.isSending = false;
  732. typewriter.stop()
  733. that.$nextTick(() => {
  734. that.scrollToBottom();
  735. });
  736. },
  737. // onComplete: 回答完成时的回调
  738. () => {
  739. const that = this;
  740. console.log('=== 回答完成 ===');
  741. if (window.responseTimeout) {
  742. clearTimeout(window.responseTimeout);
  743. window.responseTimeout = null;
  744. }
  745. typewriter.end(async () => {
  746. aiMessage.content = trimToLastSentenceEnd(aiMessage.content)
  747. try {
  748. // 获取上下文引用文件
  749. const response = await getContextFile(currentInput, that.selectedKnowledge.collectionName);
  750. console.log('=== 上下文文件 ===', response)
  751. if (response && Array.isArray(response)) {
  752. aiMessage.references = response.map(item => ({
  753. fileName: item.file_name,
  754. similarity: item.score,
  755. content: item.content
  756. }));
  757. that.$forceUpdate(); // 强制更新以显示引用文件
  758. }
  759. } catch (error) {
  760. console.error('获取上下文文件失败:', error);
  761. }
  762. that.isSending = false;
  763. that.$nextTick(() => {
  764. that.scrollToBottom();
  765. });
  766. })
  767. }
  768. );
  769. // 如果用户快速发送多条消息,取消之前的请求
  770. if (window.currentController) {
  771. window.currentController.abort();
  772. }
  773. if (window.responseTimeout) {
  774. clearTimeout(window.responseTimeout);
  775. }
  776. window.currentController = eventSource;
  777. },
  778. // 设置初始超时定时器(30秒无任何响应才超时)
  779. initResponseTimeout() {
  780. window.responseTimeout = setTimeout(() => {
  781. if (this.isSending) {
  782. console.log('=== 初始连接超时强制结束 ===')
  783. this.isSending = false;
  784. if (window.currentController) {
  785. window.currentController.abort();
  786. window.currentController = null;
  787. }
  788. window.responseTimeout = null;
  789. }
  790. }, 300000); // 5分钟超时
  791. },
  792. /** 停止生成回答 */
  793. stopGenerating() {
  794. // 清除超时定时器
  795. if (window.responseTimeout) {
  796. clearTimeout(window.responseTimeout);
  797. window.responseTimeout = null;
  798. }
  799. if (window.currentController) {
  800. window.currentController.abort();
  801. window.currentController = null;
  802. }
  803. this.isSending = false;
  804. },
  805. /** 滚动到底部 */
  806. scrollToBottom() {
  807. // 确保在 DOM 更新后执行滚动操作
  808. this.$nextTick(() => {
  809. const container = this.$refs.chatMessagesRef;
  810. if (container) {
  811. container.scrollTop = container.scrollHeight;
  812. }
  813. });
  814. },
  815. /** 处理Enter键 */
  816. handleEnter(event) {
  817. if (!event.shiftKey) {
  818. event.preventDefault();
  819. this.sendMessage();
  820. }
  821. },
  822. /** 处理Ctrl+Enter键 */
  823. handleCtrlEnter() {
  824. this.chatInput += '\n';
  825. },
  826. /** 格式化消息内容 */
  827. formatMessage(content) {
  828. // 直接使用marked解析markdown内容
  829. return marked(content);
  830. },
  831. /** 格式化相似度 */
  832. formatSimilarity(similarity) {
  833. // 如果相似度已经是百分比形式(>1),直接返回
  834. if (similarity > 1) {
  835. return similarity.toFixed(1);
  836. }
  837. // 如果是0-1之间的值,转换为百分比
  838. return (similarity * 100).toFixed(1);
  839. },
  840. /** 获取相似度的CSS类 */
  841. getSimilarityClass(similarity) {
  842. const percentage = similarity > 1 ? similarity : similarity * 100;
  843. if (percentage >= 80) return 'high-similarity';
  844. if (percentage >= 60) return 'medium-similarity';
  845. return 'low-similarity';
  846. },
  847. /** 切换引用文件的展开/收起状态 */
  848. toggleReferences(messageIndex) {
  849. const message = this.chatMessages[messageIndex];
  850. message.showAllReferences = !message.showAllReferences;
  851. this.$forceUpdate();
  852. },
  853. /** 获取要显示的引用文件列表 */
  854. getDisplayedReferences(message) {
  855. if (!message.references || message.references.length === 0) {
  856. return [];
  857. }
  858. // 如果引用文件数量 <= 3 或者设置为显示全部,则显示所有
  859. if (message.references.length <= 3 || message.showAllReferences) {
  860. return message.references;
  861. }
  862. // 否则只显示前3个
  863. return message.references.slice(0, 3);
  864. },
  865. /** 处理卡片操作 */
  866. handleCardAction(command) {
  867. const { action, data } = command;
  868. switch (action) {
  869. case 'edit':
  870. this.handleUpdate(data);
  871. break;
  872. case 'upload':
  873. this.handleUpload(data);
  874. break;
  875. case 'delete':
  876. this.handleDelete(data);
  877. break;
  878. }
  879. },
  880. /** 删除文件 */
  881. handleDeleteFile(file) {
  882. this.$modal.confirm(`是否确认删除文件"${file}"?`).then(() => {
  883. deleteKnowledgeFile(file, this.selectedKnowledge.collectionName).then(response => {
  884. this.$modal.msgSuccess("删除成功");
  885. // 刷新文件列表
  886. if (this.selectedKnowledge) {
  887. this.fileLoading = true;
  888. listKnowledgeDocument(this.selectedKnowledge.collectionName).then(response => {
  889. this.fileList = response.data;
  890. this.fileTotal = response.data.length;
  891. // 如果当前页没有数据了,回到上一页
  892. const maxPage = Math.ceil(this.fileTotal / this.filePageSize);
  893. if (this.filePageNum > maxPage && maxPage > 0) {
  894. this.filePageNum = maxPage;
  895. }
  896. this.fileLoading = false;
  897. }).catch(error => {
  898. this.fileLoading = false;
  899. this.$modal.msgError("刷新文件列表失败:" + error.message);
  900. });
  901. }
  902. }).catch(error => {
  903. this.$modal.msgError("删除失败:" + error.message);
  904. });
  905. }).catch(() => { });
  906. },
  907. /** 获取文件类型 */
  908. getFileType(fileName) {
  909. if (!fileName) return '未知';
  910. const extension = fileName.split('.').pop()?.toLowerCase();
  911. switch (extension) {
  912. case 'pdf':
  913. return 'PDF文档';
  914. case 'docx':
  915. return 'Word文档';
  916. case 'doc':
  917. return 'Word文档';
  918. default:
  919. return '文档';
  920. }
  921. },
  922. /** 文件分页处理 */
  923. handleFilePagination() {
  924. this.fileLoading = true;
  925. listKnowledgeDocument(this.selectedKnowledge.collectionName).then(response => {
  926. this.fileList = response.data;
  927. this.fileTotal = response.data.length;
  928. this.fileLoading = false;
  929. }).catch(error => {
  930. this.fileLoading = false;
  931. this.$modal.msgError("获取文件列表失败:" + error.message);
  932. });
  933. },
  934. // 取消按钮
  935. cancel() {
  936. this.open = false;
  937. this.reset();
  938. },
  939. // 表单重置
  940. reset() {
  941. this.form.collectionName = '';
  942. this.form.description = '';
  943. this.form.newCollectionName = '';
  944. // 强制更新DOM
  945. this.$nextTick(() => {
  946. this.$refs.form.resetFields();
  947. });
  948. },
  949. /** 重置表单 */
  950. resetForm(refName) {
  951. if (this.$refs[refName]) {
  952. this.$refs[refName].resetFields();
  953. }
  954. },
  955. /** 搜索按钮操作 */
  956. handleQuery() {
  957. // 重置页码
  958. this.queryParams.pageNum = 1;
  959. this.getList();
  960. },
  961. /** 重置按钮操作 */
  962. resetQuery() {
  963. this.resetForm("queryForm");
  964. this.handleQuery();
  965. },
  966. /** 新增按钮操作 */
  967. handleAdd() {
  968. this.reset();
  969. this.open = true;
  970. this.title = "添加知识库";
  971. this.isModify = false;
  972. },
  973. /** 修改按钮操作 */
  974. handleUpdate(row) {
  975. this.reset();
  976. const collectionName = row?.collectionName || this.ids[0];
  977. if (!collectionName) {
  978. this.$modal.msgWarning("请先选择要修改的知识库");
  979. return;
  980. }
  981. this.form.collectionName = collectionName;
  982. this.form.newCollectionName = '';
  983. this.isModify = true;
  984. this.open = true;
  985. this.title = "修改知识库";
  986. },
  987. /** 提交按钮 */
  988. submitForm() {
  989. this.$refs["knowledgeFormRef"].validate(valid => {
  990. if (valid) {
  991. if (this.form.newCollectionName) {
  992. // 修改操作
  993. updateKnowledge(this.form.collectionName, this.form.newCollectionName).then(response => {
  994. this.$modal.msgSuccess("修改成功");
  995. this.open = false;
  996. this.getList();
  997. }).catch(error => {
  998. this.$modal.msgError("修改失败:" + error.message);
  999. });
  1000. } else {
  1001. // 新增操作
  1002. addKnowledge(this.form.collectionName, this.form.description).then(response => {
  1003. this.$modal.msgSuccess("新增成功");
  1004. this.open = false;
  1005. this.getList();
  1006. }).catch(error => {
  1007. this.$modal.msgError("新增失败:" + error.message);
  1008. });
  1009. }
  1010. }
  1011. });
  1012. },
  1013. /** 删除按钮操作 */
  1014. handleDelete(row) {
  1015. const collectionNames = row?.collectionName || this.ids;
  1016. if (!collectionNames) {
  1017. this.$modal.msgWarning("请先选择要删除的知识库");
  1018. return;
  1019. }
  1020. this.$modal.confirm('是否确认删除知识库名称为"' + collectionNames + '"的数据项?').then(() => {
  1021. return delKnowledge(collectionNames);
  1022. }).then(() => {
  1023. this.getList();
  1024. // 如果删除的是当前选中的知识库,清空选择
  1025. if (this.selectedKnowledge?.collectionName === collectionNames) {
  1026. // 如果正在生成回答,先停止
  1027. if (this.isSending) {
  1028. this.stopGenerating();
  1029. }
  1030. this.selectedKnowledge = null;
  1031. this.fileList = [];
  1032. this.isChatMode = false;
  1033. this.chatMessages = [];
  1034. }
  1035. this.$modal.msgSuccess("删除成功");
  1036. }).catch((error) => {
  1037. if (error !== 'cancel') {
  1038. this.$modal.msgError("删除失败:" + error.message);
  1039. }
  1040. });
  1041. },
  1042. /** 上传文件按钮操作 */
  1043. handleUpload(row) {
  1044. this.percentage = 0;
  1045. this.percentageDisplay = false;
  1046. const collectionName = row?.collectionName;
  1047. if (!collectionName) {
  1048. this.$modal.msgWarning("请先选择要上传文件的知识库");
  1049. return;
  1050. }
  1051. this.uploadForm.collectionName = collectionName;
  1052. this.uploadOpen = true;
  1053. },
  1054. // 文件上传中处理
  1055. handleFileUploadProgress(event, file, fileList) {
  1056. this.upload.isUploading = true;
  1057. },
  1058. handleFileChange(file, fileList) {
  1059. this.upload.fileList = fileList.map(f => f.raw)
  1060. },
  1061. // 文件上传成功处理
  1062. handleFileSuccess(response, file, fileList) {
  1063. this.upload.isUploading = false;
  1064. this.$refs.upload.clearFiles();
  1065. this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
  1066. this.uploadOpen = false;
  1067. this.getList();
  1068. // 如果当前有选中的知识库,刷新文件列表
  1069. if (this.selectedKnowledge) {
  1070. this.fileLoading = true;
  1071. listKnowledgeDocument(this.selectedKnowledge.collectionName).then((response) => {
  1072. this.fileList = response.data;
  1073. this.fileTotal = response.data.length;
  1074. this.fileLoading = false;
  1075. }).catch((error) => {
  1076. this.fileLoading = false;
  1077. this.$modal.msgError("刷新文件列表失败:" + error.message);
  1078. });
  1079. }
  1080. },
  1081. // 提交上传
  1082. submitUpload() {
  1083. try {
  1084. this.percentageDisplay = true;
  1085. var timer;
  1086. clearInterval(timer);
  1087. this.getProcess()
  1088. } catch (error) {
  1089. console.error('文件上传失败:', error)
  1090. this.$message.error('文件上传失败')
  1091. }
  1092. },
  1093. getProcess() {
  1094. var timer = setInterval(() => { //隔2000毫秒获取进度
  1095. getProcessValue().then((res) => {
  1096. if (res.code == 200 && res.msg.includes(":")) {
  1097. this.progress = res.msg;
  1098. this.percentage = Number(res.msg.split(":")[1].replace("%", ""))
  1099. }
  1100. });
  1101. }, 30000);
  1102. insertKnowledgeFile(this.upload.fileList, this.uploadForm.collectionName).then((response) => {
  1103. this.percentageDisplay = false;
  1104. this.$modal.msgSuccess("上传成功");
  1105. this.uploadOpen = false;
  1106. this.$refs.knowledgeUpload.clearFiles();
  1107. this.getList();
  1108. }).catch((error) => {
  1109. this.$modal.msgError("上传失败:" + error.message);
  1110. });
  1111. },
  1112. // 取消上传
  1113. cancelUpload() {
  1114. this.uploadOpen = false;
  1115. this.$refs.knowledgeUpload.clearFiles();
  1116. },
  1117. }
  1118. }
  1119. </script>
  1120. <style lang="scss" scoped>
  1121. .app-container {
  1122. height: 100vh;
  1123. display: flex;
  1124. flex-direction: column;
  1125. background: #f5f7fa;
  1126. }
  1127. .main-content {
  1128. flex: 1;
  1129. display: flex;
  1130. gap: 16px;
  1131. padding: 16px;
  1132. overflow: hidden;
  1133. }
  1134. .left-panel {
  1135. width: 400px;
  1136. background: white;
  1137. border-radius: 8px;
  1138. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  1139. display: flex;
  1140. flex-direction: column;
  1141. overflow: hidden;
  1142. }
  1143. .right-panel {
  1144. flex: 1;
  1145. background: white;
  1146. border-radius: 8px;
  1147. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  1148. display: flex;
  1149. flex-direction: column;
  1150. overflow: hidden;
  1151. }
  1152. .panel-header {
  1153. padding: 20px 24px;
  1154. border-bottom: 1px solid #e4e7ed;
  1155. display: flex;
  1156. justify-content: space-between;
  1157. align-items: center;
  1158. background: #fafbfc;
  1159. h3 {
  1160. margin: 0;
  1161. font-size: 16px;
  1162. font-weight: 600;
  1163. color: #303133;
  1164. }
  1165. .knowledge-count {
  1166. font-size: 12px;
  1167. color: #909399;
  1168. background: #f0f2f5;
  1169. padding: 4px 8px;
  1170. border-radius: 4px;
  1171. }
  1172. .search-box {
  1173. padding: 16px;
  1174. border-bottom: 1px solid #e4e4e4;
  1175. }
  1176. .header-actions {
  1177. display: flex;
  1178. gap: 8px;
  1179. }
  1180. }
  1181. .knowledge-cards {
  1182. flex: 1;
  1183. padding: 16px;
  1184. overflow-y: auto;
  1185. display: flex;
  1186. flex-direction: column;
  1187. gap: 12px;
  1188. }
  1189. .knowledge-card {
  1190. background: white;
  1191. border: 1px solid #e4e7ed;
  1192. border-radius: 8px;
  1193. padding: 16px;
  1194. cursor: pointer;
  1195. transition: all 0.3s ease;
  1196. position: relative;
  1197. &:hover {
  1198. border-color: #409eff;
  1199. box-shadow: 0 4px 12px rgba(64, 158, 255, 0.15);
  1200. transform: translateY(-2px);
  1201. }
  1202. &.active {
  1203. border-color: #409eff;
  1204. background: #f0f9ff;
  1205. box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
  1206. }
  1207. .card-header {
  1208. display: flex;
  1209. justify-content: space-between;
  1210. align-items: flex-start;
  1211. margin-bottom: 12px;
  1212. .card-title {
  1213. display: flex;
  1214. align-items: center;
  1215. gap: 8px;
  1216. flex: 1;
  1217. .folder-icon {
  1218. color: #409eff;
  1219. font-size: 18px;
  1220. }
  1221. .title-text {
  1222. font-weight: 600;
  1223. color: #303133;
  1224. font-size: 14px;
  1225. line-height: 1.4;
  1226. }
  1227. }
  1228. .card-actions {
  1229. opacity: 0;
  1230. transition: opacity 0.3s ease;
  1231. }
  1232. }
  1233. &:hover .card-actions {
  1234. opacity: 1;
  1235. }
  1236. .card-content {
  1237. .description {
  1238. color: #606266;
  1239. font-size: 13px;
  1240. line-height: 1.5;
  1241. margin: 0 0 12px 0;
  1242. display: -webkit-box;
  1243. -webkit-line-clamp: 2;
  1244. -webkit-box-orient: vertical;
  1245. overflow: hidden;
  1246. }
  1247. .meta-info {
  1248. display: flex;
  1249. justify-content: space-between;
  1250. align-items: center;
  1251. font-size: 12px;
  1252. color: #909399;
  1253. .create-time {
  1254. background: #f0f2f5;
  1255. padding: 2px 6px;
  1256. border-radius: 3px;
  1257. }
  1258. .file-count {
  1259. background: #e1f3d8;
  1260. color: #67c23a;
  1261. padding: 2px 6px;
  1262. border-radius: 3px;
  1263. }
  1264. }
  1265. }
  1266. }
  1267. .file-content {
  1268. flex: 1;
  1269. padding: 20px;
  1270. overflow-y: auto;
  1271. }
  1272. .title-content {
  1273. flex: 1;
  1274. padding: 20px;
  1275. overflow: hidden;
  1276. }
  1277. .title-content-container {
  1278. display: flex;
  1279. height: calc(100vh - 240px);
  1280. gap: 20px;
  1281. overflow: hidden;
  1282. }
  1283. .title-list {
  1284. width: 300px;
  1285. background: #fafbfc;
  1286. border-radius: 8px;
  1287. border: 1px solid #e4e7ed;
  1288. padding: 12px;
  1289. overflow-y: auto;
  1290. }
  1291. .title-item {
  1292. display: flex;
  1293. align-items: center;
  1294. gap: 8px;
  1295. padding: 10px 12px;
  1296. margin-bottom: 8px;
  1297. border-radius: 6px;
  1298. cursor: pointer;
  1299. transition: all 0.3s ease;
  1300. background: white;
  1301. border: 1px solid #e4e7ed;
  1302. &:hover {
  1303. border-color: #409eff;
  1304. background: #f0f9ff;
  1305. }
  1306. &.active {
  1307. border-color: #409eff;
  1308. background: #e6f7ff;
  1309. box-shadow: 0 2px 8px rgba(64, 158, 255, 0.15);
  1310. }
  1311. .title-icon {
  1312. color: #409eff;
  1313. font-size: 16px;
  1314. }
  1315. .title-text {
  1316. flex: 1;
  1317. font-size: 14px;
  1318. color: #303133;
  1319. line-height: 1.4;
  1320. overflow: hidden;
  1321. text-overflow: ellipsis;
  1322. white-space: nowrap;
  1323. }
  1324. }
  1325. .content-list {
  1326. flex: 1;
  1327. background: #fafbfc;
  1328. border-radius: 8px;
  1329. border: 1px solid #e4e7ed;
  1330. padding: 20px;
  1331. overflow-y: auto;
  1332. }
  1333. .content-items {
  1334. display: flex;
  1335. flex-direction: column;
  1336. gap: 16px;
  1337. }
  1338. .content-item {
  1339. background: white;
  1340. border: 1px solid #e4e7ed;
  1341. border-radius: 8px;
  1342. padding: 16px;
  1343. transition: all 0.3s ease;
  1344. &:hover {
  1345. border-color: #409eff;
  1346. box-shadow: 0 2px 8px rgba(64, 158, 255, 0.15);
  1347. }
  1348. .content-header {
  1349. display: flex;
  1350. align-items: center;
  1351. margin-bottom: 12px;
  1352. .content-index {
  1353. background: #409eff;
  1354. color: white;
  1355. width: 24px;
  1356. height: 24px;
  1357. border-radius: 50%;
  1358. display: flex;
  1359. align-items: center;
  1360. justify-content: center;
  1361. font-size: 12px;
  1362. font-weight: 600;
  1363. }
  1364. }
  1365. .content-body {
  1366. font-size: 14px;
  1367. color: #303133;
  1368. line-height: 1.6;
  1369. white-space: pre-wrap;
  1370. word-break: break-word;
  1371. }
  1372. }
  1373. .title-mode-badge {
  1374. background: #67c23a;
  1375. color: white;
  1376. padding: 2px 8px;
  1377. border-radius: 12px;
  1378. font-size: 12px;
  1379. margin-left: auto;
  1380. }
  1381. // 聊天模式样式
  1382. .chat-content {
  1383. flex: 1;
  1384. display: flex;
  1385. flex-direction: column;
  1386. overflow: hidden;
  1387. }
  1388. .selected-knowledge {
  1389. display: flex;
  1390. align-items: center;
  1391. gap: 8px;
  1392. padding: 12px 16px;
  1393. background: #f0f9ff;
  1394. border-radius: 6px;
  1395. margin-bottom: 20px;
  1396. .folder-icon {
  1397. color: #409eff;
  1398. font-size: 16px;
  1399. }
  1400. .knowledge-name {
  1401. font-weight: 600;
  1402. color: #303133;
  1403. }
  1404. .chat-mode-badge {
  1405. background: #409eff;
  1406. color: white;
  1407. padding: 2px 8px;
  1408. border-radius: 12px;
  1409. font-size: 12px;
  1410. margin-left: auto;
  1411. }
  1412. }
  1413. .chat-messages {
  1414. flex: 1;
  1415. padding: 20px;
  1416. overflow-y: auto;
  1417. background: #fafbfc;
  1418. }
  1419. .welcome-message {
  1420. display: flex;
  1421. align-items: center;
  1422. justify-content: center;
  1423. height: 100%;
  1424. text-align: center;
  1425. .welcome-content {
  1426. max-width: 400px;
  1427. .welcome-icon {
  1428. font-size: 48px;
  1429. margin-bottom: 16px;
  1430. }
  1431. h3 {
  1432. margin: 0 0 12px 0;
  1433. color: #303133;
  1434. font-size: 18px;
  1435. }
  1436. p {
  1437. margin: 0 0 8px 0;
  1438. color: #606266;
  1439. font-size: 14px;
  1440. line-height: 1.6;
  1441. }
  1442. }
  1443. }
  1444. .message-list {
  1445. display: flex;
  1446. flex-direction: column;
  1447. gap: 20px;
  1448. }
  1449. .message-item {
  1450. display: flex;
  1451. gap: 12px;
  1452. &.user {
  1453. flex-direction: row-reverse;
  1454. .message-content {
  1455. align-items: flex-end;
  1456. }
  1457. .message-bubble {
  1458. background: #409eff;
  1459. color: white;
  1460. border-radius: 18px 18px 4px 18px;
  1461. }
  1462. }
  1463. &.ai {
  1464. .message-bubble {
  1465. background: white;
  1466. color: #303133;
  1467. border: 1px solid #e4e7ed;
  1468. border-radius: 18px 18px 18px 4px;
  1469. }
  1470. }
  1471. }
  1472. .message-avatar {
  1473. width: 40px;
  1474. height: 40px;
  1475. border-radius: 50%;
  1476. display: flex;
  1477. align-items: center;
  1478. justify-content: center;
  1479. flex-shrink: 0;
  1480. .user-avatar {
  1481. // background: #409eff;
  1482. color: #409eff;
  1483. font-size: 18px;
  1484. }
  1485. .ai-avatar {
  1486. // background: #67c23a;
  1487. color: white;
  1488. font-size: 18px;
  1489. }
  1490. }
  1491. .message-content {
  1492. display: flex;
  1493. flex-direction: column;
  1494. max-width: 70%;
  1495. }
  1496. .message-bubble {
  1497. padding: 12px 16px;
  1498. line-height: 1.5;
  1499. word-wrap: break-word;
  1500. .message-text {
  1501. margin-bottom: 8px;
  1502. white-space: pre-wrap;
  1503. }
  1504. .message-references {
  1505. margin: 12px 0;
  1506. padding: 12px;
  1507. background: rgba(0, 0, 0, 0.05);
  1508. border-radius: 6px;
  1509. border-left: 3px solid #409eff;
  1510. .references-header {
  1511. display: flex;
  1512. align-items: center;
  1513. justify-content: space-between;
  1514. gap: 6px;
  1515. margin-bottom: 8px;
  1516. font-size: 13px;
  1517. font-weight: 600;
  1518. color: #409eff;
  1519. .reference-icon {
  1520. font-size: 14px;
  1521. }
  1522. .toggle-references {
  1523. font-size: 11px;
  1524. color: #909399;
  1525. padding: 0;
  1526. height: auto;
  1527. &:hover {
  1528. color: #409eff;
  1529. }
  1530. }
  1531. }
  1532. .references-list {
  1533. display: flex;
  1534. flex-direction: column;
  1535. gap: 6px;
  1536. }
  1537. .reference-item {
  1538. padding: 6px 8px;
  1539. background: rgba(255, 255, 255, 0.8);
  1540. border-radius: 4px;
  1541. border: 1px solid rgba(0, 0, 0, 0.1);
  1542. .reference-info {
  1543. display: flex;
  1544. justify-content: space-between;
  1545. align-items: center;
  1546. gap: 8px;
  1547. .reference-filename {
  1548. font-size: 12px;
  1549. color: #303133;
  1550. font-weight: 500;
  1551. flex: 1;
  1552. overflow: hidden;
  1553. text-overflow: ellipsis;
  1554. white-space: nowrap;
  1555. }
  1556. .reference-similarity {
  1557. font-size: 11px;
  1558. padding: 2px 6px;
  1559. border-radius: 10px;
  1560. white-space: nowrap;
  1561. font-weight: 500;
  1562. transition: all 0.3s ease;
  1563. &.high-similarity {
  1564. color: #67c23a;
  1565. background: #e1f3d8;
  1566. }
  1567. &.medium-similarity {
  1568. color: #e6a23c;
  1569. background: #fdf5e6;
  1570. }
  1571. &.low-similarity {
  1572. color: #f56c6c;
  1573. background: #fef0f0;
  1574. }
  1575. }
  1576. }
  1577. }
  1578. }
  1579. .message-time {
  1580. font-size: 12px;
  1581. opacity: 0.7;
  1582. }
  1583. }
  1584. // 打字机效果的光标 - 只在正在生成时显示
  1585. .message-bubble.ai.typing .message-text:after {
  1586. content: '|';
  1587. animation: blink 1s infinite;
  1588. color: #409eff;
  1589. }
  1590. @keyframes blink {
  1591. 0%,
  1592. 50% {
  1593. opacity: 1;
  1594. }
  1595. 51%,
  1596. 100% {
  1597. opacity: 0;
  1598. }
  1599. }
  1600. .chat-input-area {
  1601. padding: 20px;
  1602. border-top: 1px solid #e4e7ed;
  1603. background: white;
  1604. }
  1605. .input-container {
  1606. display: flex;
  1607. gap: 12px;
  1608. align-items: flex-end;
  1609. .el-textarea {
  1610. flex: 1;
  1611. }
  1612. .input-actions {
  1613. flex-shrink: 0;
  1614. }
  1615. }
  1616. .input-tips {
  1617. margin-top: 8px;
  1618. text-align: center;
  1619. font-size: 12px;
  1620. color: #999;
  1621. display: flex;
  1622. justify-content: space-between;
  1623. align-items: center;
  1624. .char-count {
  1625. color: #409eff;
  1626. font-weight: 500;
  1627. &.warning {
  1628. color: #e6a23c;
  1629. }
  1630. .error-text {
  1631. color: #f56c6c;
  1632. font-size: 11px;
  1633. }
  1634. }
  1635. }
  1636. .file-list {
  1637. display: flex;
  1638. flex-direction: column;
  1639. gap: 12px;
  1640. }
  1641. .file-item {
  1642. display: flex;
  1643. justify-content: space-between;
  1644. align-items: center;
  1645. padding: 16px;
  1646. background: #fafbfc;
  1647. border: 1px solid #e4e7ed;
  1648. border-radius: 6px;
  1649. transition: all 0.3s ease;
  1650. &:hover {
  1651. border-color: #409eff;
  1652. box-shadow: 0 2px 8px rgba(64, 158, 255, 0.1);
  1653. }
  1654. .file-info {
  1655. display: flex;
  1656. align-items: center;
  1657. gap: 12px;
  1658. flex: 1;
  1659. .file-icon {
  1660. color: #409eff;
  1661. font-size: 20px;
  1662. }
  1663. .file-details {
  1664. .file-name {
  1665. font-weight: 500;
  1666. color: #303133;
  1667. margin-bottom: 4px;
  1668. }
  1669. .file-meta {
  1670. display: flex;
  1671. gap: 12px;
  1672. font-size: 12px;
  1673. color: #909399;
  1674. .file-type {
  1675. background: #e1f3d8;
  1676. color: #67c23a;
  1677. padding: 2px 6px;
  1678. border-radius: 3px;
  1679. }
  1680. }
  1681. }
  1682. }
  1683. .file-actions {
  1684. opacity: 0;
  1685. transition: opacity 0.3s ease;
  1686. }
  1687. &:hover .file-actions {
  1688. opacity: 1;
  1689. }
  1690. }
  1691. // 文件列表分页样式
  1692. .file-content {
  1693. :deep(.pagination-container) {
  1694. margin-top: 20px;
  1695. padding: 20px 0;
  1696. text-align: center;
  1697. background: transparent;
  1698. }
  1699. }
  1700. .empty-state {
  1701. display: flex;
  1702. flex-direction: column;
  1703. align-items: center;
  1704. justify-content: center;
  1705. padding: 60px 20px;
  1706. text-align: center;
  1707. color: #909399;
  1708. .empty-icon {
  1709. font-size: 48px;
  1710. margin-bottom: 16px;
  1711. opacity: 0.5;
  1712. }
  1713. p {
  1714. margin: 0 0 16px 0;
  1715. font-size: 14px;
  1716. }
  1717. }
  1718. .dialog-footer {
  1719. text-align: right;
  1720. }
  1721. .el-upload__tip {
  1722. font-size: 12px;
  1723. color: #606266;
  1724. margin-top: 7px;
  1725. }
  1726. :deep(.el-upload-dragger) {
  1727. width: 350px;
  1728. }
  1729. :deep(.el-upload__text) {
  1730. margin: 10px 0 16px;
  1731. color: #606266;
  1732. font-size: 14px;
  1733. }
  1734. :deep(.el-upload__tip) {
  1735. font-size: 12px;
  1736. color: #606266;
  1737. margin-top: 7px;
  1738. }
  1739. // 修复上传文件列表中文件名过长的问题
  1740. :deep(.el-upload-list) {
  1741. max-width: 100%;
  1742. .el-upload-list__item {
  1743. max-width: 100%;
  1744. overflow: hidden;
  1745. .el-upload-list__item-name {
  1746. width: 350px; // 为删除按钮等留出空间
  1747. white-space: nowrap;
  1748. overflow: hidden;
  1749. text-overflow: ellipsis;
  1750. display: inline-block;
  1751. vertical-align: middle;
  1752. }
  1753. }
  1754. }
  1755. // 修复拖拽区域内文件名显示问题
  1756. :deep(.el-upload-dragger) {
  1757. .el-upload-list {
  1758. .el-upload-list__item {
  1759. margin: 0 10px 10px 10px;
  1760. padding: 8px 12px;
  1761. background: #f5f7fa;
  1762. border-radius: 4px;
  1763. .el-upload-list__item-name {
  1764. color: #606266;
  1765. font-size: 13px;
  1766. max-width: calc(100% - 60px);
  1767. word-wrap: break-word;
  1768. word-break: break-all;
  1769. white-space: normal;
  1770. line-height: 1.4;
  1771. }
  1772. }
  1773. }
  1774. }
  1775. // 响应式设计
  1776. @media (max-width: 1200px) {
  1777. .main-content {
  1778. flex-direction: column;
  1779. }
  1780. .left-panel {
  1781. width: 100%;
  1782. height: 300px;
  1783. }
  1784. }
  1785. @media (max-width: 768px) {
  1786. .top-toolbar {
  1787. padding: 12px 16px;
  1788. }
  1789. .main-content {
  1790. padding: 12px;
  1791. gap: 12px;
  1792. }
  1793. .panel-header {
  1794. padding: 16px 20px;
  1795. }
  1796. .knowledge-cards {
  1797. padding: 12px;
  1798. }
  1799. .file-content {
  1800. padding: 16px;
  1801. }
  1802. .chat-messages {
  1803. padding: 16px;
  1804. }
  1805. .chat-input-area {
  1806. padding: 16px;
  1807. }
  1808. .message-content {
  1809. max-width: 85%;
  1810. }
  1811. .message-references {
  1812. .references-header {
  1813. flex-direction: column;
  1814. align-items: flex-start;
  1815. gap: 4px;
  1816. .toggle-references {
  1817. font-size: 10px;
  1818. }
  1819. }
  1820. .reference-item {
  1821. .reference-info {
  1822. flex-direction: column;
  1823. align-items: flex-start;
  1824. gap: 4px;
  1825. .reference-filename {
  1826. font-size: 11px;
  1827. }
  1828. .reference-similarity {
  1829. font-size: 10px;
  1830. align-self: flex-end;
  1831. }
  1832. }
  1833. }
  1834. }
  1835. }
  1836. // Loading动画样式
  1837. .loading-dots {
  1838. display: flex;
  1839. gap: 4px;
  1840. padding: 8px 0;
  1841. span {
  1842. width: 8px;
  1843. height: 8px;
  1844. border-radius: 50%;
  1845. background-color: #ccc;
  1846. animation: loading 1.4s infinite ease-in-out;
  1847. &:nth-child(1) {
  1848. animation-delay: -0.32s;
  1849. }
  1850. &:nth-child(2) {
  1851. animation-delay: -0.16s;
  1852. }
  1853. }
  1854. }
  1855. @keyframes loading {
  1856. 0%,
  1857. 80%,
  1858. 100% {
  1859. transform: scale(0);
  1860. }
  1861. 40% {
  1862. transform: scale(1);
  1863. }
  1864. }
  1865. </style>