Conversation.vue 11 KB

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