ConversationList.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <!-- AI 对话 -->
  2. <template>
  3. <el-aside width="260px" class="conversation-container h-100%">
  4. <!-- 左顶部:对话 -->
  5. <div class="h-100%">
  6. <el-button class="w-1/1 btn-new-conversation" type="primary" @click="createConversation">
  7. <Icon icon="ep:plus" class="mr-5px" />
  8. 新建对话
  9. </el-button>
  10. <!-- 左顶部:搜索对话 -->
  11. <el-input
  12. v-model="searchName"
  13. size="large"
  14. class="mt-10px search-input"
  15. placeholder="搜索历史记录"
  16. @keyup="searchConversation"
  17. >
  18. <template #prefix>
  19. <Icon icon="ep:search" />
  20. </template>
  21. </el-input>
  22. <!-- 左中间:对话列表 -->
  23. <div class="conversation-list">
  24. <!-- 情况一:加载中 -->
  25. <el-empty v-if="loading" description="." :v-loading="loading" />
  26. <!-- 情况二:按照 group 分组,展示聊天会话 list 列表 -->
  27. <div v-for="conversationKey in Object.keys(conversationMap)" :key="conversationKey">
  28. <div
  29. class="conversation-item classify-title"
  30. v-if="conversationMap[conversationKey].length"
  31. >
  32. <el-text class="mx-1" size="small" tag="b">
  33. {{ conversationKey }}
  34. </el-text>
  35. </div>
  36. <div
  37. class="conversation-item"
  38. v-for="conversation in conversationMap[conversationKey]"
  39. :key="conversation.id"
  40. @click="handleConversationClick(conversation.id)"
  41. @mouseover="hoverConversationId = conversation.id"
  42. @mouseout="hoverConversationId = ''"
  43. >
  44. <div
  45. :class="
  46. conversation.id === activeConversationId ? 'conversation active' : 'conversation'
  47. "
  48. >
  49. <div class="title-wrapper">
  50. <img class="avatar" :src="conversation.roleAvatar || roleAvatarDefaultImg" />
  51. <span class="title">{{ conversation.title }}</span>
  52. </div>
  53. <div class="button-wrapper" v-show="hoverConversationId === conversation.id">
  54. <el-button class="btn" link @click.stop="handleTop(conversation)">
  55. <el-icon title="置顶" v-if="!conversation.pinned"><Top /></el-icon>
  56. <el-icon title="置顶" v-if="conversation.pinned"><Bottom /></el-icon>
  57. </el-button>
  58. <el-button class="btn" link @click.stop="updateConversationTitle(conversation)">
  59. <el-icon title="编辑">
  60. <Icon icon="ep:edit" />
  61. </el-icon>
  62. </el-button>
  63. <el-button class="btn" link @click.stop="deleteChatConversation(conversation)">
  64. <el-icon title="删除对话">
  65. <Icon icon="ep:delete" />
  66. </el-icon>
  67. </el-button>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. <!-- 底部占位 -->
  73. <div class="h-160px w-100%"></div>
  74. </div>
  75. </div>
  76. <!-- 左底部:工具栏 -->
  77. <div class="tool-box">
  78. <div @click="handleRoleRepository">
  79. <Icon icon="ep:user" />
  80. <el-text size="small">角色仓库</el-text>
  81. </div>
  82. <div @click="handleClearConversation">
  83. <Icon icon="ep:delete" />
  84. <el-text size="small">清空未置顶对话</el-text>
  85. </div>
  86. </div>
  87. <!-- 角色仓库抽屉 -->
  88. <el-drawer v-model="roleRepositoryOpen" title="角色仓库" size="754px">
  89. <RoleRepository />
  90. </el-drawer>
  91. </el-aside>
  92. </template>
  93. <script setup lang="ts">
  94. import { ChatConversationApi, ChatConversationVO } from '@/api/ai/chat/conversation'
  95. import RoleRepository from '../role/RoleRepository.vue'
  96. import { Bottom, Top } from '@element-plus/icons-vue'
  97. import roleAvatarDefaultImg from '@/assets/ai/gpt.svg'
  98. const message = useMessage() // 消息弹窗
  99. // 定义属性
  100. const searchName = ref<string>('') // 对话搜索
  101. const activeConversationId = ref<number | null>(null) // 选中的对话,默认为 null
  102. const hoverConversationId = ref<number | null>(null) // 悬浮上去的对话
  103. const conversationList = ref([] as ChatConversationVO[]) // 对话列表
  104. const conversationMap = ref<any>({}) // 对话分组 (置顶、今天、三天前、一星期前、一个月前)
  105. const loading = ref<boolean>(false) // 加载中
  106. const loadingTime = ref<any>() // 加载中定时器
  107. // 定义组件 props
  108. const props = defineProps({
  109. activeId: {
  110. type: String || null,
  111. required: true
  112. }
  113. })
  114. // 定义钩子
  115. const emits = defineEmits([
  116. 'onConversationCreate',
  117. 'onConversationClick',
  118. 'onConversationClear',
  119. 'onConversationDelete'
  120. ])
  121. /** 搜索对话 */
  122. const searchConversation = async (e) => {
  123. // 恢复数据
  124. if (!searchName.value.trim().length) {
  125. conversationMap.value = await getConversationGroupByCreateTime(conversationList.value)
  126. } else {
  127. // 过滤
  128. const filterValues = conversationList.value.filter((item) => {
  129. return item.title.includes(searchName.value.trim())
  130. })
  131. conversationMap.value = await getConversationGroupByCreateTime(filterValues)
  132. }
  133. }
  134. /** 点击对话 */
  135. const handleConversationClick = async (id: number) => {
  136. // 过滤出选中的对话
  137. const filterConversation = conversationList.value.filter((item) => {
  138. return item.id === id
  139. })
  140. // 回调 onConversationClick
  141. // noinspection JSVoidFunctionReturnValueUsed
  142. const success = emits('onConversationClick', filterConversation[0])
  143. // 切换对话
  144. if (success) {
  145. activeConversationId.value = id
  146. }
  147. }
  148. /** 获取对话列表 */
  149. const getChatConversationList = async () => {
  150. try {
  151. // 加载中
  152. loadingTime.value = setTimeout(() => {
  153. loading.value = true
  154. }, 50)
  155. // 1.1 获取 对话数据
  156. conversationList.value = await ChatConversationApi.getChatConversationMyList()
  157. // 1.2 排序
  158. conversationList.value.sort((a, b) => {
  159. return b.createTime - a.createTime
  160. })
  161. // 1.3 没有任何对话情况
  162. if (conversationList.value.length === 0) {
  163. activeConversationId.value = null
  164. conversationMap.value = {}
  165. return
  166. }
  167. // 2. 对话根据时间分组(置顶、今天、一天前、三天前、七天前、30 天前)
  168. conversationMap.value = await getConversationGroupByCreateTime(conversationList.value)
  169. } finally {
  170. // 清理定时器
  171. if (loadingTime.value) {
  172. clearTimeout(loadingTime.value)
  173. }
  174. // 加载完成
  175. loading.value = false
  176. }
  177. }
  178. /** 按照 creteTime 创建时间,进行分组 */
  179. const getConversationGroupByCreateTime = async (list: ChatConversationVO[]) => {
  180. // 排序、指定、时间分组(今天、一天前、三天前、七天前、30天前)
  181. // noinspection NonAsciiCharacters
  182. const groupMap = {
  183. 置顶: [] as ChatConversationVO[],
  184. 今天: [] as ChatConversationVO[],
  185. 一天前: [] as ChatConversationVO[],
  186. 三天前: [] as ChatConversationVO[],
  187. 七天前: [] as ChatConversationVO[],
  188. 三十天前: [] as ChatConversationVO[]
  189. }
  190. // 当前时间的时间戳
  191. const now = Date.now()
  192. // 定义时间间隔常量(单位:毫秒)
  193. const oneDay = 24 * 60 * 60 * 1000
  194. const threeDays = 3 * oneDay
  195. const sevenDays = 7 * oneDay
  196. const thirtyDays = 30 * oneDay
  197. for (const conversation of list) {
  198. // 置顶
  199. if (conversation.pinned) {
  200. groupMap['置顶'].push(conversation)
  201. continue
  202. }
  203. // 计算时间差(单位:毫秒)
  204. const diff = now - conversation.createTime
  205. // 根据时间间隔判断
  206. if (diff < oneDay) {
  207. groupMap['今天'].push(conversation)
  208. } else if (diff < threeDays) {
  209. groupMap['一天前'].push(conversation)
  210. } else if (diff < sevenDays) {
  211. groupMap['三天前'].push(conversation)
  212. } else if (diff < thirtyDays) {
  213. groupMap['七天前'].push(conversation)
  214. } else {
  215. groupMap['三十天前'].push(conversation)
  216. }
  217. }
  218. return groupMap
  219. }
  220. /** 新建对话 */
  221. const createConversation = async () => {
  222. // 1. 新建对话
  223. const conversationId = await ChatConversationApi.createChatConversationMy(
  224. {} as unknown as ChatConversationVO
  225. )
  226. // 2. 获取对话内容
  227. await getChatConversationList()
  228. // 3. 选中对话
  229. await handleConversationClick(conversationId)
  230. // 4. 回调
  231. emits('onConversationCreate')
  232. }
  233. /** 修改对话的标题 */
  234. const updateConversationTitle = async (conversation: ChatConversationVO) => {
  235. // 1. 二次确认
  236. const { value } = await ElMessageBox.prompt('修改标题', {
  237. inputPattern: /^[\s\S]*.*\S[\s\S]*$/, // 判断非空,且非空格
  238. inputErrorMessage: '标题不能为空',
  239. inputValue: conversation.title
  240. })
  241. // 2. 发起修改
  242. await ChatConversationApi.updateChatConversationMy({
  243. id: conversation.id,
  244. title: value
  245. } as ChatConversationVO)
  246. message.success('重命名成功')
  247. // 3. 刷新列表
  248. await getChatConversationList()
  249. // 4. 过滤当前切换的
  250. const filterConversationList = conversationList.value.filter((item) => {
  251. return item.id === conversation.id
  252. })
  253. if (filterConversationList.length > 0) {
  254. // tip:避免切换对话
  255. if (activeConversationId.value === filterConversationList[0].id) {
  256. emits('onConversationClick', filterConversationList[0])
  257. }
  258. }
  259. }
  260. /** 删除聊天对话 */
  261. const deleteChatConversation = async (conversation: ChatConversationVO) => {
  262. try {
  263. // 删除的二次确认
  264. await message.delConfirm(`是否确认删除对话 - ${conversation.title}?`)
  265. // 发起删除
  266. await ChatConversationApi.deleteChatConversationMy(conversation.id)
  267. message.success('对话已删除')
  268. // 刷新列表
  269. await getChatConversationList()
  270. // 回调
  271. emits('onConversationDelete', conversation)
  272. } catch {}
  273. }
  274. /** 清空对话 */
  275. const handleClearConversation = async () => {
  276. try {
  277. await message.confirm('确认后对话会全部清空,置顶的对话除外。')
  278. await ChatConversationApi.deleteChatConversationMyByUnpinned()
  279. ElMessage({
  280. message: '操作成功!',
  281. type: 'success'
  282. })
  283. // 清空 对话 和 对话内容
  284. activeConversationId.value = null
  285. // 获取 对话列表
  286. await getChatConversationList()
  287. // 回调 方法
  288. emits('onConversationClear')
  289. } catch {}
  290. }
  291. /** 对话置顶 */
  292. const handleTop = async (conversation: ChatConversationVO) => {
  293. // 更新对话置顶
  294. conversation.pinned = !conversation.pinned
  295. await ChatConversationApi.updateChatConversationMy(conversation)
  296. // 刷新对话
  297. await getChatConversationList()
  298. }
  299. // ============ 角色仓库 ============
  300. /** 角色仓库抽屉 */
  301. const roleRepositoryOpen = ref<boolean>(false) // 角色仓库是否打开
  302. const handleRoleRepository = async () => {
  303. roleRepositoryOpen.value = !roleRepositoryOpen.value
  304. }
  305. /** 监听选中的对话 */
  306. const { activeId } = toRefs(props)
  307. watch(activeId, async (newValue, oldValue) => {
  308. activeConversationId.value = newValue as string
  309. })
  310. // 定义 public 方法
  311. defineExpose({ createConversation })
  312. /** 初始化 */
  313. onMounted(async () => {
  314. // 获取 对话列表
  315. await getChatConversationList()
  316. // 默认选中
  317. if (props.activeId) {
  318. activeConversationId.value = props.activeId
  319. } else {
  320. // 首次默认选中第一个
  321. if (conversationList.value.length) {
  322. activeConversationId.value = conversationList.value[0].id
  323. // 回调 onConversationClick
  324. await emits('onConversationClick', conversationList.value[0])
  325. }
  326. }
  327. })
  328. </script>
  329. <style scoped lang="scss">
  330. .conversation-container {
  331. position: relative;
  332. display: flex;
  333. flex-direction: column;
  334. justify-content: space-between;
  335. padding: 10px 10px 0;
  336. overflow: hidden;
  337. .btn-new-conversation {
  338. padding: 18px 0;
  339. }
  340. .search-input {
  341. margin-top: 20px;
  342. }
  343. .conversation-list {
  344. overflow: auto;
  345. height: 100%;
  346. .classify-title {
  347. padding-top: 10px;
  348. }
  349. .conversation-item {
  350. margin-top: 5px;
  351. }
  352. .conversation {
  353. display: flex;
  354. flex-direction: row;
  355. justify-content: space-between;
  356. flex: 1;
  357. padding: 0 5px;
  358. cursor: pointer;
  359. border-radius: 5px;
  360. align-items: center;
  361. line-height: 30px;
  362. &.active {
  363. background-color: var(--el-color-primary-light-9);
  364. border: 1px solid var(--el-color-primary-light-7);
  365. .button {
  366. display: inline-block;
  367. }
  368. }
  369. .title-wrapper {
  370. display: flex;
  371. flex-direction: row;
  372. align-items: center;
  373. }
  374. .title {
  375. padding: 2px 10px;
  376. max-width: 220px;
  377. font-size: 14px;
  378. font-weight: 400;
  379. color: var(--el-text-color-regular);
  380. overflow: hidden;
  381. white-space: nowrap;
  382. text-overflow: ellipsis;
  383. }
  384. .avatar {
  385. width: 25px;
  386. height: 25px;
  387. border-radius: 5px;
  388. display: flex;
  389. flex-direction: row;
  390. justify-items: center;
  391. }
  392. // 对话编辑、删除
  393. .button-wrapper {
  394. right: 2px;
  395. display: flex;
  396. flex-direction: row;
  397. justify-items: center;
  398. color: var(--el-text-color-regular);
  399. .btn {
  400. margin: 0;
  401. }
  402. }
  403. }
  404. }
  405. // 角色仓库、清空未设置对话
  406. .tool-box {
  407. position: absolute;
  408. bottom: 0;
  409. left: 0;
  410. right: 0;
  411. //width: 100%;
  412. padding: 0 20px;
  413. background-color: var(--el-fill-color-extra-light);
  414. box-shadow: 0 0 1px 1px var(--el-border-color-lighter);
  415. line-height: 35px;
  416. display: flex;
  417. justify-content: space-between;
  418. align-items: center;
  419. color: var(--el-text-color);
  420. > div {
  421. display: flex;
  422. align-items: center;
  423. color: var(--el-text-color-regular);
  424. padding: 0;
  425. margin: 0;
  426. cursor: pointer;
  427. > span {
  428. margin-left: 5px;
  429. }
  430. }
  431. }
  432. }
  433. </style>