ConversationList.vue 13 KB

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