Conversation.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 class="conversation-item classify-title" v-if="conversationMap[conversationKey].length">
  27. <el-text class="mx-1" size="small" tag="b">{{ conversationKey }}</el-text>
  28. </div>
  29. <div
  30. class="conversation-item"
  31. v-for="conversation in conversationMap[conversationKey]"
  32. :key="conversation.id"
  33. @click="handleConversationClick(conversation.id)"
  34. @mouseover="hoverConversationId = conversation.id"
  35. @mouseout="hoverConversationId = ''"
  36. >
  37. <div
  38. :class="conversation.id === activeConversationId ? 'conversation active' : 'conversation'"
  39. >
  40. <div class="title-wrapper">
  41. <img class="avatar" :src="conversation.roleAvatar || roleAvatarDefaultImg"/>
  42. <span class="title">{{ conversation.title }}</span>
  43. </div>
  44. <div class="button-wrapper" v-show="hoverConversationId === conversation.id">
  45. <el-button class="btn" link @click.stop="handlerTop(conversation)" >
  46. <el-icon title="置顶" v-if="!conversation.pinned"><Top /></el-icon>
  47. <el-icon title="置顶" v-if="conversation.pinned"><Bottom /></el-icon>
  48. </el-button>
  49. <el-button class="btn" link @click.stop="updateConversationTitle(conversation)">
  50. <el-icon title="编辑" >
  51. <Icon icon="ep:edit"/>
  52. </el-icon>
  53. </el-button>
  54. <el-button class="btn" link @click.stop="deleteChatConversation(conversation)">
  55. <el-icon title="删除对话" >
  56. <Icon icon="ep:delete"/>
  57. </el-icon>
  58. </el-button>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. <!-- 底部站位 -->
  64. <div style="height: 160px; width: 100%;"></div>
  65. </div>
  66. </div>
  67. <!-- 左底部:工具栏 -->
  68. <!-- TODO @fan:下面两个 icon,可以使用类似 <Icon icon="ep:question-filled" /> 替代哈 -->
  69. <div class="tool-box">
  70. <div @click="handleRoleRepository">
  71. <Icon icon="ep:user"/>
  72. <el-text size="small">角色仓库</el-text>
  73. </div>
  74. <div @click="handleClearConversation">
  75. <Icon icon="ep:delete"/>
  76. <el-text size="small">清空未置顶对话</el-text>
  77. </div>
  78. </div>
  79. <!-- ============= 额外组件 ============= -->
  80. <!-- 角色仓库抽屉 -->
  81. <el-drawer v-model="drawer" title="角色仓库" size="754px">
  82. <Role/>
  83. </el-drawer>
  84. </el-aside>
  85. </template>
  86. <script setup lang="ts">
  87. import {ChatConversationApi, ChatConversationVO} from '@/api/ai/chat/conversation'
  88. import {ref} from "vue";
  89. import Role from "@/views/ai/chat/role/index.vue";
  90. import {Bottom, Top} from "@element-plus/icons-vue";
  91. import roleAvatarDefaultImg from '@/assets/ai/gpt.svg'
  92. const message = useMessage() // 消息弹窗
  93. // 定义属性
  94. const searchName = ref<string>('') // 对话搜索
  95. const activeConversationId = ref<string | null>(null) // 选中的对话,默认为 null
  96. const hoverConversationId = ref<string | null>(null) // 悬浮上去的对话
  97. const conversationList = ref([] as ChatConversationVO[]) // 对话列表
  98. const conversationMap = ref<any>({}) // 对话分组 (置顶、今天、三天前、一星期前、一个月前)
  99. const drawer = ref<boolean>(false) // 角色仓库抽屉 TODO @fan:roleDrawer 会不会好点哈
  100. const loading = ref<boolean>(false) // 加载中
  101. const loadingTime = ref<any>() // 加载中定时器
  102. // 定义组件 props
  103. const props = defineProps({
  104. activeId: {
  105. type: String || null,
  106. required: true
  107. }
  108. })
  109. // 定义钩子
  110. const emits = defineEmits([
  111. 'onConversationCreate',
  112. 'onConversationClick',
  113. 'onConversationClear',
  114. 'onConversationDelete'
  115. ])
  116. /**
  117. * 对话 - 搜索
  118. */
  119. const searchConversation = async (e) => {
  120. // 恢复数据
  121. if (!searchName.value.trim().length) {
  122. conversationMap.value = await conversationTimeGroup(conversationList.value)
  123. } else {
  124. // 过滤
  125. const filterValues = conversationList.value.filter(item => {
  126. return item.title.includes(searchName.value.trim())
  127. })
  128. conversationMap.value = await conversationTimeGroup(filterValues)
  129. }
  130. }
  131. /**
  132. * 对话 - 点击
  133. */
  134. const handleConversationClick = async (id: string) => {
  135. // 过滤出选中的对话
  136. const filterConversation = conversationList.value.filter(item => {
  137. return item.id === id
  138. })
  139. // 回调 onConversationClick
  140. // TODO @fan: 这里 idea 会报黄色警告,有办法解下么?
  141. const res = emits('onConversationClick', filterConversation[0])
  142. // 切换对话
  143. if (res) {
  144. activeConversationId.value = id
  145. }
  146. }
  147. /**
  148. * 对话 - 获取列表
  149. */
  150. const getChatConversationList = async () => {
  151. try {
  152. // 0. 加载中
  153. loadingTime.value = setTimeout(() => {
  154. loading.value = true
  155. }, 50)
  156. // 1. 获取 对话数据
  157. const res = await ChatConversationApi.getChatConversationMyList()
  158. // 2. 排序
  159. res.sort((a, b) => {
  160. return b.createTime - a.createTime
  161. })
  162. conversationList.value = res
  163. // 3. 默认选中
  164. if (!activeId?.value) {
  165. // await handleConversationClick(res[0].id)
  166. } else {
  167. // tip: 删除的刚好是选中的,那么需要重新挑选一个来进行选中
  168. // const filterConversationList = conversationList.value.filter(item => {
  169. // return item.id === activeId.value
  170. // })
  171. // if (filterConversationList.length <= 0) {
  172. // await handleConversationClick(res[0].id)
  173. // }
  174. }
  175. // 4. 没有任何对话情况
  176. if (conversationList.value.length === 0) {
  177. activeConversationId.value = null
  178. conversationMap.value = {}
  179. return
  180. }
  181. // 5. 对话根据时间分组(置顶、今天、一天前、三天前、七天前、30天前)
  182. conversationMap.value = await conversationTimeGroup(conversationList.value)
  183. } finally {
  184. // 清理定时器
  185. if (loadingTime.value) {
  186. clearTimeout(loadingTime.value)
  187. }
  188. // 加载完成
  189. loading.value = false
  190. }
  191. }
  192. const conversationTimeGroup = async (list: ChatConversationVO[]) => {
  193. // 排序、指定、时间分组(今天、一天前、三天前、七天前、30天前)
  194. const groupMap = {
  195. '置顶': [],
  196. '今天': [],
  197. '一天前': [],
  198. '三天前': [],
  199. '七天前': [],
  200. '三十天前': []
  201. }
  202. // 当前时间的时间戳
  203. const now = Date.now();
  204. // 定义时间间隔常量(单位:毫秒)
  205. const oneDay = 24 * 60 * 60 * 1000;
  206. const threeDays = 3 * oneDay;
  207. const sevenDays = 7 * oneDay;
  208. const thirtyDays = 30 * oneDay;
  209. for (const conversation: ChatConversationVO of list) {
  210. // 置顶
  211. if (conversation.pinned) {
  212. groupMap['置顶'].push(conversation)
  213. continue
  214. }
  215. // 计算时间差(单位:毫秒)
  216. const diff = now - conversation.updateTime;
  217. // 根据时间间隔判断
  218. if (diff < oneDay) {
  219. groupMap['今天'].push(conversation)
  220. } else if (diff < threeDays) {
  221. groupMap['一天前'].push(conversation)
  222. } else if (diff < sevenDays) {
  223. groupMap['三天前'].push(conversation)
  224. } else if (diff < thirtyDays) {
  225. groupMap['七天前'].push(conversation)
  226. } else {
  227. groupMap['三十天前'].push(conversation)
  228. }
  229. }
  230. console.log('----groupMap', groupMap)
  231. return groupMap
  232. }
  233. /**
  234. * 对话 - 新建
  235. */
  236. const createConversation = async () => {
  237. // 1. 新建对话
  238. const conversationId = await ChatConversationApi.createChatConversationMy(
  239. {} as unknown as ChatConversationVO
  240. )
  241. // 2. 获取对话内容
  242. await getChatConversationList()
  243. // 3. 选中对话
  244. await handleConversationClick(conversationId)
  245. // 4. 回调
  246. emits('onConversationCreate')
  247. }
  248. /**
  249. * 对话 - 更新标题
  250. */
  251. const updateConversationTitle = async (conversation: ChatConversationVO) => {
  252. // 1. 二次确认
  253. const {value} = await ElMessageBox.prompt('修改标题', {
  254. inputPattern: /^[\s\S]*.*\S[\s\S]*$/, // 判断非空,且非空格
  255. inputErrorMessage: '标题不能为空',
  256. inputValue: conversation.title
  257. })
  258. // 2. 发起修改
  259. await ChatConversationApi.updateChatConversationMy({
  260. id: conversation.id,
  261. title: value
  262. } as ChatConversationVO)
  263. message.success('重命名成功')
  264. // 3. 刷新列表
  265. await getChatConversationList()
  266. // 4. 过滤当前切换的
  267. const filterConversationList = conversationList.value.filter(item => {
  268. return item.id === conversation.id
  269. })
  270. if (filterConversationList.length > 0) {
  271. // tip:避免切换对话
  272. if (activeConversationId.value === filterConversationList[0].id) {
  273. emits('onConversationClick', filterConversationList[0])
  274. }
  275. }
  276. }
  277. /**
  278. * 删除聊天对话
  279. */
  280. const deleteChatConversation = async (conversation: ChatConversationVO) => {
  281. try {
  282. // 删除的二次确认
  283. await message.delConfirm(`是否确认删除对话 - ${conversation.title}?`)
  284. // 发起删除
  285. await ChatConversationApi.deleteChatConversationMy(conversation.id)
  286. message.success('对话已删除')
  287. // 刷新列表
  288. await getChatConversationList()
  289. // 回调
  290. emits('onConversationDelete', conversation)
  291. } catch {
  292. }
  293. }
  294. /**
  295. * 对话置顶
  296. */
  297. // TODO @fan:应该是 handleXXX,handler 是名词哈
  298. const handlerTop = async (conversation: ChatConversationVO) => {
  299. // 更新对话置顶
  300. conversation.pinned = !conversation.pinned
  301. await ChatConversationApi.updateChatConversationMy(conversation)
  302. // 刷新对话
  303. await getChatConversationList()
  304. }
  305. // TODO @fan:类似 ============ 分块的,最后后面也有 ============ 哈
  306. // ============ 角色仓库
  307. /**
  308. * 角色仓库抽屉
  309. */
  310. const handleRoleRepository = async () => {
  311. drawer.value = !drawer.value
  312. }
  313. // ============= 清空对话
  314. /**
  315. * 清空对话
  316. */
  317. const handleClearConversation = async () => {
  318. // TODO @fan:可以使用 await message.confirm( 简化,然后使用 await 改成同步的逻辑,会更简洁
  319. ElMessageBox.confirm(
  320. '确认后对话会全部清空,置顶的对话除外。',
  321. '确认提示',
  322. {
  323. confirmButtonText: '确认',
  324. cancelButtonText: '取消',
  325. type: 'warning',
  326. })
  327. .then(async () => {
  328. await ChatConversationApi.deleteMyAllExceptPinned()
  329. ElMessage({
  330. message: '操作成功!',
  331. type: 'success'
  332. })
  333. // 清空 对话 和 对话内容
  334. activeConversationId.value = null
  335. // 获取 对话列表
  336. await getChatConversationList()
  337. // 回调 方法
  338. emits('onConversationClear')
  339. })
  340. .catch(() => {
  341. })
  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>