index.vue 17 KB

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