Conversation.vue 12 KB

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