Conversation.vue 13 KB

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