Conversation.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. }
  155. // 4、没有 任何对话情况
  156. if (conversationList.value.length === 0) {
  157. activeConversationId.value = null
  158. conversationMap.value = {}
  159. return
  160. }
  161. // 5、对话根据时间分组(置顶、今天、一天前、三天前、七天前、30天前)
  162. conversationMap.value = await conversationTimeGroup(conversationList.value)
  163. } finally {
  164. // 清理定时器
  165. if (loadingTime.value) {
  166. clearTimeout(loadingTime.value)
  167. }
  168. // 加载完成
  169. loading.value = false
  170. }
  171. }
  172. const conversationTimeGroup = async (list: ChatConversationVO[]) => {
  173. // 排序、指定、时间分组(今天、一天前、三天前、七天前、30天前)
  174. const groupMap = {
  175. '置顶': [],
  176. '今天': [],
  177. '一天前': [],
  178. '三天前': [],
  179. '七天前': [],
  180. '三十天前': []
  181. }
  182. // 当前时间的时间戳
  183. const now = Date.now();
  184. // 定义时间间隔常量(单位:毫秒)
  185. const oneDay = 24 * 60 * 60 * 1000;
  186. const threeDays = 3 * oneDay;
  187. const sevenDays = 7 * oneDay;
  188. const thirtyDays = 30 * oneDay;
  189. for (const conversation: ChatConversationVO of list) {
  190. // 置顶
  191. if (conversation.pinned) {
  192. groupMap['置顶'].push(conversation)
  193. continue
  194. }
  195. // 计算时间差(单位:毫秒)
  196. const diff = now - conversation.updateTime;
  197. // 根据时间间隔判断
  198. if (diff < oneDay) {
  199. groupMap['今天'].push(conversation)
  200. } else if (diff < threeDays) {
  201. groupMap['一天前'].push(conversation)
  202. } else if (diff < sevenDays) {
  203. groupMap['三天前'].push(conversation)
  204. } else if (diff < thirtyDays) {
  205. groupMap['七天前'].push(conversation)
  206. } else {
  207. groupMap['三十天前'].push(conversation)
  208. }
  209. }
  210. console.log('----groupMap', groupMap)
  211. return groupMap
  212. }
  213. /**
  214. * 对话 - 新建
  215. */
  216. const createConversation = async () => {
  217. // 1、新建对话
  218. const conversationId = await ChatConversationApi.createChatConversationMy(
  219. {} as unknown as ChatConversationVO
  220. )
  221. // 2、获取对话内容
  222. await getChatConversationList()
  223. // 3、选中对话
  224. await handleConversationClick(conversationId)
  225. }
  226. /**
  227. * 对话 - 更新标题
  228. */
  229. const updateConversationTitle = async (conversation: ChatConversationVO) => {
  230. // 1、二次确认
  231. const {value} = await ElMessageBox.prompt('修改标题', {
  232. inputPattern: /^[\s\S]*.*\S[\s\S]*$/, // 判断非空,且非空格
  233. inputErrorMessage: '标题不能为空',
  234. inputValue: conversation.title
  235. })
  236. // 2、发起修改
  237. await ChatConversationApi.updateChatConversationMy({
  238. id: conversation.id,
  239. title: value
  240. } as ChatConversationVO)
  241. message.success('重命名成功')
  242. // 刷新列表
  243. await getChatConversationList()
  244. // 过滤当前切换的
  245. const filterConversationList = conversationList.value.filter(item => {
  246. return item.id === conversation.id
  247. })
  248. if (filterConversationList.length > 0) {
  249. emits('onConversationClick', filterConversationList[0])
  250. }
  251. }
  252. /**
  253. * 删除聊天会话
  254. */
  255. const deleteChatConversation = async (conversation: ChatConversationVO) => {
  256. try {
  257. // 删除的二次确认
  258. await message.delConfirm(`是否确认删除会话 - ${conversation.title}?`)
  259. // 发起删除
  260. await ChatConversationApi.deleteChatConversationMy(conversation.id)
  261. message.success('会话已删除')
  262. // 刷新列表
  263. await getChatConversationList()
  264. // 回调
  265. emits('onConversationDelete', conversation)
  266. } catch {
  267. }
  268. }
  269. /**
  270. * 对话置顶
  271. */
  272. const handlerTop = async (conversation: ChatConversationVO) => {
  273. // 更新对话置顶
  274. conversation.pinned = !conversation.pinned
  275. await ChatConversationApi.updateChatConversationMy(conversation)
  276. // 刷新对话
  277. await getChatConversationList()
  278. }
  279. // ============ 角色仓库
  280. /**
  281. * 角色仓库抽屉
  282. */
  283. const handleRoleRepository = async () => {
  284. drawer.value = !drawer.value
  285. }
  286. // ============= 清空对话
  287. /**
  288. * 清空对话
  289. */
  290. const handleClearConversation = async () => {
  291. ElMessageBox.confirm(
  292. '确认后对话会全部清空,置顶的对话除外。',
  293. '确认提示',
  294. {
  295. confirmButtonText: '确认',
  296. cancelButtonText: '取消',
  297. type: 'warning',
  298. })
  299. .then(async () => {
  300. await ChatConversationApi.deleteMyAllExceptPinned()
  301. ElMessage({
  302. message: '操作成功!',
  303. type: 'success'
  304. })
  305. // 清空 对话 和 对话内容
  306. activeConversationId.value = null
  307. // 获取 对话列表
  308. await getChatConversationList()
  309. // 回调 方法
  310. emits('onConversationClear')
  311. })
  312. .catch(() => {
  313. })
  314. }
  315. // ============ 组件 onMounted
  316. const { activeId } = toRefs(props)
  317. watch(activeId, async (newValue, oldValue) => {
  318. // 更新选中
  319. activeConversationId.value = newValue as string
  320. })
  321. onMounted(async () => {
  322. // 默认选中
  323. if (props.activeId != null) {
  324. activeConversationId.value = props.activeId
  325. }
  326. // 获取 对话列表
  327. await getChatConversationList()
  328. })
  329. </script>
  330. <style scoped lang="scss">
  331. .conversation-container {
  332. position: relative;
  333. display: flex;
  334. flex-direction: column;
  335. justify-content: space-between;
  336. padding: 0 10px;
  337. padding-top: 10px;
  338. overflow: hidden;
  339. .btn-new-conversation {
  340. padding: 18px 0;
  341. }
  342. .search-input {
  343. margin-top: 20px;
  344. }
  345. .conversation-list {
  346. overflow: auto;
  347. height: 100%;
  348. .classify-title {
  349. padding-top: 10px;
  350. }
  351. .conversation-item {
  352. margin-top: 5px;
  353. }
  354. .conversation {
  355. display: flex;
  356. flex-direction: row;
  357. justify-content: space-between;
  358. flex: 1;
  359. padding: 0 5px;
  360. cursor: pointer;
  361. border-radius: 5px;
  362. align-items: center;
  363. line-height: 30px;
  364. &.active {
  365. background-color: #e6e6e6;
  366. .button {
  367. display: inline-block;
  368. }
  369. }
  370. .title-wrapper {
  371. display: flex;
  372. flex-direction: row;
  373. align-items: center;
  374. }
  375. .title {
  376. padding: 2px 10px;
  377. max-width: 220px;
  378. font-size: 14px;
  379. font-weight: 400;
  380. color: rgba(0, 0, 0, 0.77);
  381. overflow: hidden;
  382. white-space: nowrap;
  383. text-overflow: ellipsis;
  384. }
  385. .avatar {
  386. width: 25px;
  387. height: 25px;
  388. border-radius: 5px;
  389. display: flex;
  390. flex-direction: row;
  391. justify-items: center;
  392. }
  393. // 对话编辑、删除
  394. .button-wrapper {
  395. right: 2px;
  396. display: flex;
  397. flex-direction: row;
  398. justify-items: center;
  399. color: #606266;
  400. .btn {
  401. margin: 0;
  402. }
  403. }
  404. }
  405. }
  406. // 角色仓库、清空未设置对话
  407. .tool-box {
  408. position: absolute;
  409. bottom: 0;
  410. left: 0;
  411. right: 0;
  412. //width: 100%;
  413. padding: 0 20px;
  414. background-color: #f4f4f4;
  415. box-shadow: 0 0 1px 1px rgba(228, 228, 228, 0.8);
  416. line-height: 35px;
  417. display: flex;
  418. justify-content: space-between;
  419. align-items: center;
  420. color: var(--el-text-color);
  421. > div {
  422. display: flex;
  423. align-items: center;
  424. color: #606266;
  425. padding: 0;
  426. margin: 0;
  427. cursor: pointer;
  428. > span {
  429. margin-left: 5px;
  430. }
  431. }
  432. }
  433. }
  434. </style>