Conversation.vue 14 KB

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