index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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()" :loading="conversationInProgress" v-if="conversationInProgress == false">
  153. {{ conversationInProgress ? '进行中' : '发送'}}
  154. </el-button>
  155. <el-button type="danger" size="default" @click="stopStream()" v-if="conversationInProgress == true">
  156. 停止
  157. </el-button>
  158. </div>
  159. </form>
  160. </el-footer>
  161. </el-container>
  162. </el-container>
  163. </template>
  164. <script setup lang="ts">
  165. import {ChatMessageApi, ChatMessageSendVO, ChatMessageVO} from "@/api/ai/chat/message";
  166. import {formatDate} from "@/utils/formatTime";
  167. import {useClipboard} from '@vueuse/core'
  168. // 初始化 copy 到粘贴板
  169. const { copy } = useClipboard();
  170. const searchName = ref('') // 查询的内容
  171. const conversationId = ref('1781604279872581648') // 对话id
  172. const conversationInProgress = ref<Boolean>() // 对话进行中
  173. conversationInProgress.value = false
  174. const conversationInAbortController = ref<any>() // 对话进行中 abort 控制器(控制 stream 对话)
  175. const prompt = ref<string>() // prompt
  176. // 判断 消息列表 滚动的位置(用于判断是否需要滚动到消息最下方)
  177. const messageContainer: any = ref(null);
  178. const isScrolling = ref(false)//用于判断用户是否在滚动
  179. /** chat message 列表 */
  180. defineOptions({ name: 'chatMessageList' })
  181. const list = ref<ChatMessageVO[]>([]) // 列表的数据
  182. const changeConversation = (conversation) => {
  183. console.log(conversation)
  184. conversationId.value = conversation.id
  185. // TODO 芋艿:待实现
  186. }
  187. const updateConversationTitle = (conversation) => {
  188. console.log(conversation)
  189. // TODO 芋艿:待实现
  190. }
  191. const deleteConversationTitle = (conversation) => {
  192. console.log(conversation)
  193. // TODO 芋艿:待实现
  194. }
  195. const searchConversation = () => {
  196. // TODO 芋艿:待实现
  197. }
  198. /** send */
  199. const onSend = async () => {
  200. // 进行中不允许发送
  201. if (conversationInProgress.value) {
  202. return
  203. }
  204. const content = prompt.value;
  205. // 清空输入框
  206. prompt.value = ''
  207. const requestParams = {
  208. conversationId: conversationId.value,
  209. content: content,
  210. } as unknown as ChatMessageSendVO
  211. // 添加 message
  212. const userMessage = await ChatMessageApi.add(requestParams) as unknown as ChatMessageVO;
  213. list.value.push(userMessage)
  214. // 滚动到住下面
  215. scrollToBottom();
  216. // stream
  217. await doSendStream(userMessage)
  218. //
  219. }
  220. const doSendStream = async (userMessage: ChatMessageVO) => {
  221. // 创建AbortController实例,以便中止请求
  222. conversationInAbortController.value = new AbortController()
  223. // 标记对话进行中
  224. conversationInProgress.value = true
  225. try {
  226. // 发送 event stream
  227. let isFirstMessage = true
  228. ChatMessageApi.sendStream(userMessage.id, conversationInAbortController.value,(message) => {
  229. console.log('message', message)
  230. const data = JSON.parse(message.data) as unknown as ChatMessageVO
  231. // 如果没有内容结束链接
  232. if (data.content === '') {
  233. // 标记对话结束
  234. conversationInProgress.value = false;
  235. // 结束 stream 对话
  236. conversationInAbortController.value.abort()
  237. }
  238. // 首次返回需要添加一个 message 到页面,后面的都是更新
  239. if (isFirstMessage) {
  240. isFirstMessage = false;
  241. list.value.push(data)
  242. } else {
  243. const lastMessage = list.value[list.value.length - 1];
  244. lastMessage.content = lastMessage.content + data.content
  245. list.value[list.value - 1] = lastMessage
  246. }
  247. // 滚动到最下面
  248. scrollToBottom();
  249. }, (error) => {
  250. console.log('error', error)
  251. // 标记对话结束
  252. conversationInProgress.value = false;
  253. // 结束 stream 对话
  254. conversationInAbortController.value.abort()
  255. }, () => {
  256. console.log('close')
  257. // 标记对话结束
  258. conversationInProgress.value = false;
  259. // 结束 stream 对话
  260. conversationInAbortController.value.abort()
  261. })
  262. } finally {
  263. }
  264. }
  265. /** 查询列表 */
  266. const messageList = async () => {
  267. try {
  268. // 获取列表数据
  269. const res = await ChatMessageApi.messageList(conversationId.value)
  270. list.value = res;
  271. // 滚动到最下面
  272. scrollToBottom();
  273. } finally {
  274. }
  275. }
  276. function scrollToBottom() {
  277. nextTick(() => {
  278. //注意要使用nexttick以免获取不到dom
  279. console.log('isScrolling.value', isScrolling.value)
  280. if (!isScrolling.value) {
  281. messageContainer.value.scrollTop = messageContainer.value.scrollHeight - messageContainer.value.offsetHeight
  282. }
  283. })
  284. }
  285. function handleScroll() {
  286. const scrollContainer = messageContainer.value
  287. const scrollTop = scrollContainer.scrollTop
  288. const scrollHeight = scrollContainer.scrollHeight
  289. const offsetHeight = scrollContainer.offsetHeight
  290. if (scrollTop + offsetHeight < scrollHeight) {
  291. // 用户开始滚动并在最底部之上,取消保持在最底部的效果
  292. isScrolling.value = true
  293. } else {
  294. // 用户停止滚动并滚动到最底部,开启保持到最底部的效果
  295. isScrolling.value = false
  296. }
  297. }
  298. function noCopy(content) {
  299. copy(content)
  300. ElMessage({
  301. message: '复制成功!',
  302. type: 'success',
  303. })
  304. }
  305. const onDelete = async (id) => {
  306. // 删除 message
  307. await ChatMessageApi.delete(id)
  308. ElMessage({
  309. message: '删除成功!',
  310. type: 'success',
  311. })
  312. // tip:如果 stream 进行中的 message,就需要调用 controller 结束
  313. stopStream()
  314. // 重新获取 message 列表
  315. await messageList();
  316. }
  317. const stopStream = async () => {
  318. // tip:如果 stream 进行中的 message,就需要调用 controller 结束
  319. if (conversationInAbortController.value) {
  320. conversationInAbortController.value.abort()
  321. }
  322. // 设置为 false
  323. conversationInProgress.value = false
  324. }
  325. /** 初始化 **/
  326. onMounted(async () => {
  327. // 获取列表数据
  328. messageList();
  329. // scrollToBottom();
  330. // await nextTick
  331. // 监听滚动事件,判断用户滚动状态
  332. messageContainer.value.addEventListener('scroll', handleScroll)
  333. })
  334. </script>
  335. <style lang="scss" scoped>
  336. .ai-layout {
  337. // TODO @范 这里height不能 100% 先这样临时处理
  338. position: absolute;
  339. flex: 1;
  340. top: 0;
  341. left: 0;
  342. height: 100%;
  343. width: 100%;
  344. //padding: 15px 15px;
  345. }
  346. .conversation-container {
  347. position: relative;
  348. display: flex;
  349. flex-direction: column;
  350. justify-content: space-between;
  351. padding: 0 10px;
  352. padding-top: 10px;
  353. .btn-new-conversation {
  354. padding: 18px 0;
  355. }
  356. .search-input {
  357. margin-top: 20px;
  358. }
  359. .conversation-list {
  360. margin-top: 20px;
  361. .conversation {
  362. display: flex;
  363. flex-direction: row;
  364. justify-content: space-between;
  365. flex: 1;
  366. padding: 0 5px;
  367. margin-top: 10px;
  368. cursor: pointer;
  369. border-radius: 5px;
  370. align-items: center;
  371. line-height: 30px;
  372. &.active {
  373. background-color: #e6e6e6;
  374. .button {
  375. display: inline-block;
  376. }
  377. }
  378. .title-wrapper {
  379. display: flex;
  380. flex-direction: row;
  381. align-items: center;
  382. }
  383. .title {
  384. padding: 5px 10px;
  385. max-width: 220px;
  386. font-size: 14px;
  387. overflow: hidden;
  388. white-space: nowrap;
  389. text-overflow: ellipsis;
  390. }
  391. .avatar {
  392. width: 28px;
  393. height: 28px;
  394. display: flex;
  395. flex-direction: row;
  396. justify-items: center;
  397. }
  398. // 对话编辑、删除
  399. .button-wrapper {
  400. right: 2px;
  401. display: flex;
  402. flex-direction: row;
  403. justify-items: center;
  404. color: #606266;
  405. .el-icon {
  406. margin-right: 5px;
  407. }
  408. }
  409. }
  410. }
  411. // 角色仓库、清空未设置对话
  412. .tool-box {
  413. line-height: 35px;
  414. display: flex;
  415. justify-content: space-between;
  416. align-items: center;
  417. color: var(--el-text-color);
  418. > div {
  419. display: flex;
  420. align-items: center;
  421. color: #606266;
  422. padding: 0;
  423. margin: 0;
  424. > span {
  425. margin-left: 5px;
  426. }
  427. }
  428. }
  429. }
  430. // 头部
  431. .detail-container {
  432. background: #ffffff;
  433. .header {
  434. display: flex;
  435. flex-direction: row;
  436. align-items: center;
  437. justify-content: space-between;
  438. background: #fbfbfb;
  439. box-shadow: 0 0 0 0 #dcdfe6;
  440. .title {
  441. font-size: 18px;
  442. font-weight: bold;
  443. }
  444. }
  445. }
  446. // main 容器
  447. .main-container {
  448. margin: 0;
  449. padding: 0;
  450. position: relative;
  451. }
  452. .message-container {
  453. position: absolute;
  454. top: 0;
  455. bottom: 0;
  456. left: 0;
  457. right: 0;
  458. //width: 100%;
  459. //height: 100%;
  460. overflow-y: scroll;
  461. padding: 0 15px;
  462. }
  463. // 中间
  464. .chat-list {
  465. display: flex;
  466. flex-direction: column;
  467. overflow-y: hidden;
  468. .message-item {
  469. margin-top: 50px;
  470. }
  471. .left-message {
  472. display: flex;
  473. flex-direction: row;
  474. }
  475. .right-message {
  476. display: flex;
  477. flex-direction: row-reverse;
  478. justify-content: flex-start;
  479. }
  480. .avatar {
  481. //height: 170px;
  482. //width: 170px;
  483. }
  484. .message {
  485. display: flex;
  486. flex-direction: column;
  487. text-align: left;
  488. margin: 0 15px;
  489. .time {
  490. text-align: left;
  491. line-height: 30px;
  492. }
  493. .left-text-container {
  494. display: flex;
  495. flex-direction: column;
  496. overflow-wrap: break-word;
  497. background-color: rgba(228, 228, 228, 0.80);
  498. box-shadow: 0 0 0 1px rgba(228, 228, 228, 0.80);
  499. border-radius: 10px;
  500. padding: 10px 10px 5px 10px;
  501. .left-text {
  502. color: #393939;
  503. }
  504. }
  505. .right-text-container {
  506. display: flex;
  507. flex-direction: column;
  508. overflow-wrap: break-word;
  509. background-color: #267fff;
  510. color: #FFF;
  511. box-shadow: 0 0 0 1px #267fff;
  512. border-radius: 10px;
  513. padding: 10px;
  514. .right-text {
  515. color: #FFF;
  516. }
  517. }
  518. .left-btns, .right-btns {
  519. display: flex;
  520. flex-direction: row;
  521. margin-top: 8px;
  522. }
  523. }
  524. // 复制、删除按钮
  525. .btn-cus {
  526. display: flex;
  527. background-color: transparent;
  528. align-items: center;
  529. .btn-image {
  530. height: 20px;
  531. margin-right: 5px;
  532. }
  533. .btn-cus-text {
  534. color: #757575;
  535. }
  536. }
  537. .btn-cus:hover {
  538. cursor: pointer;
  539. }
  540. .btn-cus:focus {
  541. background-color: #8c939d;
  542. }
  543. }
  544. // 底部
  545. .footer-container {
  546. display: flex;
  547. flex-direction: column;
  548. height: auto;
  549. margin: 0;
  550. padding: 0;
  551. .prompt-from {
  552. display: flex;
  553. flex-direction: column;
  554. height: auto;
  555. border: 1px solid #e3e3e3;
  556. border-radius: 10px;
  557. margin: 20px 20px;
  558. padding: 9px 10px;
  559. }
  560. .prompt-input {
  561. height: 80px;
  562. //box-shadow: none;
  563. border: none;
  564. box-sizing: border-box;
  565. resize: none;
  566. padding: 0px 2px;
  567. //padding: 5px 5px;
  568. overflow: hidden;
  569. }
  570. .prompt-input:focus {
  571. outline: none;
  572. }
  573. .prompt-btns {
  574. display: flex;
  575. justify-content: space-between;
  576. padding-bottom: 0px;
  577. padding-top: 5px;
  578. }
  579. }
  580. </style>