index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <template>
  2. <el-container class="ai-layout">
  3. <!-- 左侧:会话列表 -->
  4. <el-aside width="260px" class="conversation-container">
  5. <div>
  6. <!-- 左顶部:新建对话 -->
  7. <el-button class="w-1/1 btn-new-conversation" type="primary">
  8. <Icon icon="ep:plus" class="mr-5px"/>
  9. 新建对话
  10. </el-button>
  11. <!-- 左顶部:搜索对话 -->
  12. <el-input
  13. v-model="searchName"
  14. size="large"
  15. class="mt-10px search-input"
  16. placeholder="搜索历史记录"
  17. @keyup="searchConversation"
  18. >
  19. <template #prefix>
  20. <Icon icon="ep:search"/>
  21. </template>
  22. </el-input>
  23. <!-- 左中间:对话列表 -->
  24. <div class="conversation-list">
  25. <!-- TODO @芋艿,置顶、聊天记录、一星期钱、30天前,前端对数据重新做一下分组,或者后端接口改一下 -->
  26. <div>
  27. <el-text class="mx-1" size="small" tag="b">置顶</el-text>
  28. </div>
  29. <el-row v-for="conversation in conversationList" :key="conversation.id">
  30. <div
  31. :class="conversation.id === conversationId ? 'conversation active' : 'conversation'"
  32. @click="changeConversation(conversation)"
  33. >
  34. <div class="title-wrapper">
  35. <img class="avatar" :src="conversation.avatar"/>
  36. <span class="title">{{ conversation.title }}</span>
  37. </div>
  38. <div class="button-wrapper">
  39. <el-icon title="编辑" @click="updateConversationTitle(conversation)">
  40. <Icon icon="ep:edit"/>
  41. </el-icon>
  42. <el-icon title="删除会话" @click="deleteConversationTitle(conversation)">
  43. <Icon icon="ep:delete"/>
  44. </el-icon>
  45. </div>
  46. </div>
  47. </el-row>
  48. </div>
  49. </div>
  50. <!-- 左底部:工具栏 -->
  51. <div class="tool-box">
  52. <div>
  53. <Icon icon="ep:user"/>
  54. <el-text size="small">角色仓库</el-text>
  55. </div>
  56. <div>
  57. <Icon icon="ep:delete"/>
  58. <el-text size="small">清空未置顶对话</el-text>
  59. </div>
  60. </div>
  61. </el-aside>
  62. <!-- 右侧:会话详情 -->
  63. <el-container class="detail-container">
  64. <!-- 右顶部 TODO 芋艿:右对齐 -->
  65. <el-header class="header">
  66. <div class="title">
  67. 标题......
  68. </div>
  69. <div>
  70. <el-button>3.5-turbo-0125
  71. <Icon icon="ep:setting"/>
  72. </el-button>
  73. <el-button>
  74. <Icon icon="ep:user"/>
  75. </el-button>
  76. <el-button>
  77. <Icon icon="ep:download"/>
  78. </el-button>
  79. <el-button>
  80. <Icon icon="ep:arrow-up"/>
  81. </el-button>
  82. </div>
  83. </el-header>
  84. <!-- main -->
  85. <el-main class="main-container">
  86. <div class="message-container" ref="messageContainer">
  87. <div class="chat-list" v-for="(item, index) in list" :key="index">
  88. <!-- 靠左 message -->
  89. <div class="left-message message-item" v-if="item.type === 'system'">
  90. <div class="avatar" >
  91. <el-avatar
  92. src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
  93. />
  94. </div>
  95. <div class="message">
  96. <div>
  97. <el-text class="time">{{formatDate(item.createTime)}}</el-text>
  98. </div>
  99. <div class="left-text-container">
  100. <el-text class="left-text">
  101. {{item.content}}
  102. </el-text>
  103. </div>
  104. <div class="left-btns">
  105. <div class="btn-cus" @click="noCopy(item.content)">
  106. <img class="btn-image" src="@/assets/ai/copy.svg"/>
  107. <el-text class="btn-cus-text">复制</el-text>
  108. </div>
  109. <div class="btn-cus" style="margin-left: 20px;" @click="onDelete(item.id)">
  110. <img class="btn-image" src="@/assets/ai/delete.svg" style="height: 17px;"/>
  111. <el-text class="btn-cus-text">删除</el-text>
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. <!-- 靠右 message -->
  117. <div class="right-message message-item" v-if="item.type === 'user'">
  118. <div class="avatar">
  119. <el-avatar
  120. src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
  121. />
  122. </div>
  123. <div class="message">
  124. <div>
  125. <el-text class="time">{{formatDate(item.createTime)}}</el-text>
  126. </div>
  127. <div class="right-text-container">
  128. <el-text class="right-text">
  129. {{item.content}}
  130. </el-text>
  131. </div>
  132. <div class="right-btns">
  133. <div class="btn-cus" @click="noCopy(item.content)">
  134. <img class="btn-image" src="@/assets/ai/copy.svg"/>
  135. <el-text class="btn-cus-text">复制</el-text>
  136. </div>
  137. <div class="btn-cus" style="margin-left: 20px;" @click="onDelete(item.id)">
  138. <img class="btn-image" src="@/assets/ai/delete.svg" style="height: 17px;"/>
  139. <el-text class="btn-cus-text">删除</el-text>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. </div>
  146. </el-main>
  147. <el-footer class="footer-container">
  148. <form @submit.prevent="onSend" class="prompt-from">
  149. <textarea class="prompt-input" v-model="prompt" @keyup.enter="onSend" placeholder="问我任何问题...(Shift+Enter 换行,按下 Enter 发送)"></textarea>
  150. <div class="prompt-btns">
  151. <el-switch/>
  152. <el-button type="primary" size="default" @click="onSend()">发送</el-button>
  153. </div>
  154. </form>
  155. </el-footer>
  156. </el-container>
  157. </el-container>
  158. </template>
  159. <script setup lang="ts">
  160. import {ChatMessageApi, ChatMessageSendVO, ChatMessageVO} from "@/api/ai/chat/message";
  161. import {formatDate} from "@/utils/formatTime";
  162. import {useClipboard} from '@vueuse/core'
  163. // 初始化 copy 到粘贴板
  164. const { copy } = useClipboard();
  165. const searchName = ref('') // 查询的内容
  166. const conversationId = ref('1781604279872581648') // 对话id
  167. const conversationInProgress = ref<Boolean>() // 对话进行中
  168. const conversationInAbortController = ref<any>() // 对话进行中 abort 控制器(控制 stream 对话)
  169. const prompt = ref<string>() // prompt
  170. // 判断 消息列表 滚动的位置(用于判断是否需要滚动到消息最下方)
  171. const messageContainer: any = ref(null);
  172. const isScrolling = ref(false)//用于判断用户是否在滚动
  173. /** chat message 列表 */
  174. defineOptions({ name: 'chatMessageList' })
  175. const list = ref<ChatMessageVO[]>([]) // 列表的数据
  176. const changeConversation = (conversation) => {
  177. console.log(conversation)
  178. conversationId.value = conversation.id
  179. // TODO 芋艿:待实现
  180. }
  181. const updateConversationTitle = (conversation) => {
  182. console.log(conversation)
  183. // TODO 芋艿:待实现
  184. }
  185. const deleteConversationTitle = (conversation) => {
  186. console.log(conversation)
  187. // TODO 芋艿:待实现
  188. }
  189. const searchConversation = () => {
  190. // TODO 芋艿:待实现
  191. }
  192. /** send */
  193. const onSend = async () => {
  194. const content = prompt.value;
  195. // 清空输入框
  196. prompt.value = ''
  197. const requestParams = {
  198. conversationId: conversationId.value,
  199. content: content,
  200. } as unknown as ChatMessageSendVO
  201. // 添加 message
  202. const userMessage = await ChatMessageApi.add(requestParams) as unknown as ChatMessageVO;
  203. list.value.push(userMessage)
  204. // 滚动到住下面
  205. scrollToBottom();
  206. // stream
  207. await doSendStream(userMessage)
  208. //
  209. }
  210. const doSendStream = async (userMessage: ChatMessageVO) => {
  211. // 创建AbortController实例,以便中止请求
  212. conversationInAbortController.value = new AbortController()
  213. // 标记对话进行中
  214. conversationInProgress.value = true
  215. try {
  216. // 发送 event stream
  217. let isFirstMessage = true
  218. ChatMessageApi.sendStream(userMessage.id, conversationInAbortController.value,(message) => {
  219. console.log('message', message)
  220. const data = JSON.parse(message.data) as unknown as ChatMessageVO
  221. // 如果没有内容结束链接
  222. if (data.content === '') {
  223. // 标记对话结束
  224. conversationInProgress.value = false;
  225. // 结束 stream 对话
  226. conversationInAbortController.value.abort()
  227. }
  228. // 首次返回需要添加一个 message 到页面,后面的都是更新
  229. if (isFirstMessage) {
  230. isFirstMessage = false;
  231. list.value.push(data)
  232. } else {
  233. const lastMessage = list.value[list.value.length - 1];
  234. lastMessage.content = lastMessage.content + data.content
  235. list.value[list.value - 1] = lastMessage
  236. }
  237. // 滚动到最下面
  238. scrollToBottom();
  239. }, (error) => {
  240. console.log('error', error)
  241. // 标记对话结束
  242. conversationInProgress.value = false;
  243. // 结束 stream 对话
  244. conversationInAbortController.value.abort()
  245. }, () => {
  246. console.log('close')
  247. // 标记对话结束
  248. conversationInProgress.value = false;
  249. // 结束 stream 对话
  250. conversationInAbortController.value.abort()
  251. })
  252. } finally {
  253. }
  254. }
  255. /** 查询列表 */
  256. const messageList = async () => {
  257. try {
  258. // 获取列表数据
  259. const res = await ChatMessageApi.messageList(conversationId.value)
  260. list.value = res;
  261. // 滚动到最下面
  262. scrollToBottom();
  263. } finally {
  264. }
  265. }
  266. function scrollToBottom() {
  267. nextTick(() => {
  268. //注意要使用nexttick以免获取不到dom
  269. console.log('isScrolling.value', isScrolling.value)
  270. if (!isScrolling.value) {
  271. messageContainer.value.scrollTop = messageContainer.value.scrollHeight - messageContainer.value.offsetHeight
  272. }
  273. })
  274. }
  275. function handleScroll() {
  276. const scrollContainer = messageContainer.value
  277. const scrollTop = scrollContainer.scrollTop
  278. const scrollHeight = scrollContainer.scrollHeight
  279. const offsetHeight = scrollContainer.offsetHeight
  280. if (scrollTop + offsetHeight < scrollHeight) {
  281. // 用户开始滚动并在最底部之上,取消保持在最底部的效果
  282. isScrolling.value = true
  283. } else {
  284. // 用户停止滚动并滚动到最底部,开启保持到最底部的效果
  285. isScrolling.value = false
  286. }
  287. }
  288. function noCopy(content) {
  289. copy(content)
  290. ElMessage({
  291. message: '复制成功!',
  292. type: 'success',
  293. })
  294. }
  295. const onDelete = async (id) => {
  296. // 删除 message
  297. await ChatMessageApi.delete(id)
  298. ElMessage({
  299. message: '删除成功!',
  300. type: 'success',
  301. })
  302. // tip:如果 stream 进行中的 message,就需要调用 controller 结束
  303. if (conversationInAbortController.value) {
  304. conversationInAbortController.value.abort()
  305. }
  306. // 重新获取 message 列表
  307. await messageList();
  308. }
  309. /** 初始化 **/
  310. onMounted(async () => {
  311. // 获取列表数据
  312. messageList();
  313. // scrollToBottom();
  314. // await nextTick
  315. // 监听滚动事件,判断用户滚动状态
  316. messageContainer.value.addEventListener('scroll', handleScroll)
  317. })
  318. </script>
  319. <style lang="scss" scoped>
  320. .ai-layout {
  321. // TODO @范 这里height不能 100% 先这样临时处理
  322. position: absolute;
  323. flex: 1;
  324. top: 0;
  325. left: 0;
  326. height: 100%;
  327. width: 100%;
  328. //padding: 15px 15px;
  329. }
  330. .conversation-container {
  331. position: relative;
  332. display: flex;
  333. flex-direction: column;
  334. justify-content: space-between;
  335. padding: 0 10px;
  336. padding-top: 10px;
  337. .btn-new-conversation {
  338. padding: 18px 0;
  339. }
  340. .search-input {
  341. margin-top: 20px;
  342. }
  343. .conversation-list {
  344. margin-top: 20px;
  345. .conversation {
  346. display: flex;
  347. flex-direction: row;
  348. justify-content: space-between;
  349. flex: 1;
  350. padding: 0 5px;
  351. margin-top: 10px;
  352. cursor: pointer;
  353. border-radius: 5px;
  354. align-items: center;
  355. line-height: 30px;
  356. &.active {
  357. background-color: #e6e6e6;
  358. .button {
  359. display: inline-block;
  360. }
  361. }
  362. .title-wrapper {
  363. display: flex;
  364. flex-direction: row;
  365. align-items: center;
  366. }
  367. .title {
  368. padding: 5px 10px;
  369. max-width: 220px;
  370. font-size: 14px;
  371. overflow: hidden;
  372. white-space: nowrap;
  373. text-overflow: ellipsis;
  374. }
  375. .avatar {
  376. width: 28px;
  377. height: 28px;
  378. display: flex;
  379. flex-direction: row;
  380. justify-items: center;
  381. }
  382. // 对话编辑、删除
  383. .button-wrapper {
  384. right: 2px;
  385. display: flex;
  386. flex-direction: row;
  387. justify-items: center;
  388. color: #606266;
  389. .el-icon {
  390. margin-right: 5px;
  391. }
  392. }
  393. }
  394. }
  395. // 角色仓库、清空未设置对话
  396. .tool-box {
  397. line-height: 35px;
  398. display: flex;
  399. justify-content: space-between;
  400. align-items: center;
  401. color: var(--el-text-color);
  402. > div {
  403. display: flex;
  404. align-items: center;
  405. color: #606266;
  406. padding: 0;
  407. margin: 0;
  408. > span {
  409. margin-left: 5px;
  410. }
  411. }
  412. }
  413. }
  414. // 头部
  415. .detail-container {
  416. background: #ffffff;
  417. .header {
  418. display: flex;
  419. flex-direction: row;
  420. align-items: center;
  421. justify-content: space-between;
  422. background: #fbfbfb;
  423. box-shadow: 0 0 0 0 #dcdfe6;
  424. .title {
  425. font-size: 18px;
  426. font-weight: bold;
  427. }
  428. }
  429. }
  430. // main 容器
  431. .main-container {
  432. margin: 0;
  433. padding: 0;
  434. position: relative;
  435. }
  436. .message-container {
  437. position: absolute;
  438. top: 0;
  439. bottom: 0;
  440. overflow-y: scroll;
  441. padding: 0 15px;
  442. }
  443. // 中间
  444. .chat-list {
  445. display: flex;
  446. flex-direction: column;
  447. overflow-y: hidden;
  448. .message-item {
  449. margin-top: 50px;
  450. }
  451. .left-message {
  452. display: flex;
  453. flex-direction: row;
  454. }
  455. .right-message {
  456. display: flex;
  457. flex-direction: row-reverse;
  458. justify-content: flex-start;
  459. }
  460. .avatar {
  461. //height: 170px;
  462. //width: 170px;
  463. }
  464. .message {
  465. display: flex;
  466. flex-direction: column;
  467. text-align: left;
  468. margin: 0 15px;
  469. .time {
  470. text-align: left;
  471. line-height: 30px;
  472. }
  473. .left-text-container {
  474. display: flex;
  475. flex-direction: column;
  476. overflow-wrap: break-word;
  477. background-color: rgba(228, 228, 228, 0.80);
  478. box-shadow: 0 0 0 1px rgba(228, 228, 228, 0.80);
  479. border-radius: 10px;
  480. padding: 10px 10px 5px 10px;
  481. .left-text {
  482. color: #393939;
  483. }
  484. }
  485. .right-text-container {
  486. display: flex;
  487. flex-direction: column;
  488. overflow-wrap: break-word;
  489. background-color: #267fff;
  490. color: #FFF;
  491. box-shadow: 0 0 0 1px #267fff;
  492. border-radius: 10px;
  493. padding: 10px;
  494. .right-text {
  495. color: #FFF;
  496. }
  497. }
  498. .left-btns, .right-btns {
  499. display: flex;
  500. flex-direction: row;
  501. margin-top: 8px;
  502. }
  503. }
  504. // 复制、删除按钮
  505. .btn-cus {
  506. display: flex;
  507. background-color: transparent;
  508. align-items: center;
  509. .btn-image {
  510. height: 20px;
  511. margin-right: 5px;
  512. }
  513. .btn-cus-text {
  514. color: #757575;
  515. }
  516. }
  517. .btn-cus:hover {
  518. cursor: pointer;
  519. }
  520. .btn-cus:focus {
  521. background-color: #8c939d;
  522. }
  523. }
  524. // 底部
  525. .footer-container {
  526. display: flex;
  527. flex-direction: column;
  528. height: auto;
  529. margin: 0;
  530. padding: 0;
  531. .prompt-from {
  532. display: flex;
  533. flex-direction: column;
  534. height: auto;
  535. border: 1px solid #e3e3e3;
  536. border-radius: 10px;
  537. margin: 20px 20px;
  538. padding: 9px 10px;
  539. }
  540. .prompt-input {
  541. height: 80px;
  542. //box-shadow: none;
  543. border: none;
  544. box-sizing: border-box;
  545. resize: none;
  546. padding: 0px 2px;
  547. //padding: 5px 5px;
  548. overflow: hidden;
  549. }
  550. .prompt-input:focus {
  551. outline: none;
  552. }
  553. .prompt-btns {
  554. display: flex;
  555. justify-content: space-between;
  556. padding-bottom: 0px;
  557. padding-top: 5px;
  558. }
  559. }
  560. </style>