Conversation.vue 14 KB

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