Conversation.vue 14 KB

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