AIFlowChat.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <script setup>
  2. import {
  3. onMounted,
  4. ref,
  5. nextTick,
  6. watch,
  7. onUnmounted
  8. } from 'vue';
  9. import {
  10. useRoute
  11. } from 'vue-router';
  12. import {
  13. getFlowChatStatus,
  14. getHistoryChatList
  15. } from '@/api/ai'
  16. import {
  17. Top,
  18. Plus,
  19. Check,
  20. Loading,
  21. Monitor
  22. } from '@element-plus/icons-vue'
  23. import {
  24. useUserStore
  25. } from '@/store'
  26. import {
  27. marked
  28. } from 'marked';
  29. import hljs from 'highlight.js/lib/common';
  30. // 引入 github 代码主题
  31. import 'highlight.js/styles/github.css';
  32. import config from '@/config'
  33. import {
  34. fetchEventSource
  35. } from '@microsoft/fetch-event-source';
  36. const emits = defineEmits(['updateURL']);
  37. const user = ref(useUserStore().userData);
  38. // 对话历史
  39. const chatHistory = ref([]);
  40. // 加载状态
  41. const loading = ref(false);
  42. // 输入框内容
  43. const prompt = ref('');
  44. // 当前AI流式响应内容
  45. const currentAIResponse = ref('');
  46. const paramId = ref(useRoute().params.id);
  47. const paramType = ref(useRoute().params.type);
  48. const conversationId = ref('');
  49. const chatBody = ref(null);
  50. const previewUrl = ref('');
  51. onMounted(() => {
  52. if (paramId.value.trim()) init();
  53. })
  54. // 监听对话历史变化,自动滚动到底部
  55. watch(chatHistory, () => {
  56. scrollToBottom();
  57. }, {
  58. deep: true
  59. })
  60. let timerId = null;
  61. const init = async () => {
  62. if (paramType.value === 'chat') {
  63. conversationId.value = paramId.value;
  64. initChatList();
  65. return;
  66. }
  67. let flowChatData = await getFlowChatStatus(paramId.value);
  68. if (flowChatData.state) {
  69. if (flowChatData.data.status === 'succeeded') {
  70. conversationId.value = flowChatData.data.conversationId;
  71. loading.value = false;
  72. initChatList();
  73. stopTimer();
  74. } else {
  75. let inputs = JSON.parse(flowChatData.data.inputs);
  76. chatHistory.value = [{
  77. query: inputs.query,
  78. AIoutputs: ''
  79. }]
  80. loading.value = true;
  81. startTimer();
  82. }
  83. }
  84. }
  85. // 启动定时器
  86. const startTimer = () => {
  87. if (timerId) return
  88. timerId = setInterval(() => {
  89. init();
  90. }, 3000)
  91. }
  92. // 停止定时器
  93. const stopTimer = () => {
  94. if (timerId) {
  95. clearInterval(timerId)
  96. timerId = null;
  97. }
  98. }
  99. const initChatList = async () => {
  100. let flowData = await getHistoryChatList(conversationId.value);
  101. if (flowData.state) {
  102. versionIndex.value = 1;
  103. chatHistory.value = flowData.data.data.map(node => {
  104. node['AIoutputs'] = returnAIoutputs(node.answer);
  105. return node
  106. });
  107. hljs.highlightAll();
  108. if (previewUrl.value) emits('updateURL', previewUrl.value);
  109. }
  110. }
  111. const reloadMessage = (value) => {
  112. prompt.value = value;
  113. sendMessage();
  114. }
  115. const sendMessage = () => {
  116. if (!prompt.value.trim() || loading.value) return;
  117. AIMessage(prompt.value)
  118. }
  119. const returnAIoutputs = (data) => {
  120. let str = '';
  121. if (data.indexOf('```markdown') > -1) {
  122. str = data.replaceAll('\n', '</n>').replaceAll('```markdown', '').replaceAll('```', '');
  123. } else {
  124. str = data;
  125. }
  126. let obj = str.split('<workark>');
  127. let newData = obj.map(node => {
  128. try {
  129. return JSON.parse(node)
  130. } catch {
  131. return {}
  132. }
  133. });
  134. return deduplicateByTypeAndId(newData);
  135. }
  136. const AIMessage = async (userMessage) => {
  137. try {
  138. // 1. 添加用户消息到历史记录
  139. chatHistory.value.push({
  140. isUser: true,
  141. query: userMessage,
  142. AIoutputs: ''
  143. })
  144. // 2. 初始化状态
  145. loading.value = true;
  146. currentAIResponse.value = ''
  147. prompt.value = '' // 清空输入框
  148. let result = '';
  149. let setTime = null;
  150. fetchEventSource(`${config.baseURL}/api/ai/chat/run/7`, {
  151. method: 'POST',
  152. headers: {
  153. 'Accept': 'text/event-stream',
  154. "Content-Type": "application/json",
  155. "token": useUserStore().token
  156. },
  157. timeout: 1200000000000000, // 超时
  158. openWhenHidden: true,
  159. body: JSON.stringify({
  160. query: userMessage,
  161. conversationId: conversationId.value,
  162. inputs: {
  163. websiteURL: "",
  164. fileURL: ""
  165. }
  166. }),
  167. onmessage(event) {
  168. // 处理接收到的消息
  169. result += event.data;
  170. if (!setTime) {
  171. setTime = setTimeout(() => {
  172. if (result.indexOf('markdown') > -1) {
  173. initChatList();
  174. } else {
  175. chatHistory.value[chatHistory.value.length - 1].AIoutputs =
  176. returnAIoutputs(result);
  177. scrollToBottom();
  178. clearTimeout(setTime);
  179. loading.value = false;
  180. setTime = null;
  181. }
  182. }, 1000)
  183. }
  184. },
  185. onopen(response) {
  186. // 连接打开时的回调
  187. console.log('Connection opened:', response.status);
  188. },
  189. onerror(error) {
  190. // 错误处理
  191. console.error('Error:', error);
  192. },
  193. onclose() {
  194. console.log('close');
  195. // 连接关闭时的回调
  196. loading.value = false;
  197. }
  198. })
  199. } catch (error) {
  200. console.error("请求失败:", error);
  201. }
  202. }
  203. const scrollToBottom = () => {
  204. nextTick(() => {
  205. if (chatBody.value) {
  206. chatBody.value.scrollTop = chatBody.value.scrollHeight
  207. }
  208. })
  209. }
  210. const returnUserInputs = (data) => {
  211. if (!data) return '';
  212. return JSON.parse(data).query;
  213. }
  214. const deduplicateByTypeAndId = (arr) => {
  215. const map = new Map();
  216. arr.forEach(item => {
  217. const key = item.type === 'step' && item.id ? `${item.type}-${item.id}` : item.type;
  218. map.set(key, item); // 后面覆盖前面的
  219. });
  220. return Array.from(map.values());
  221. }
  222. /**
  223. * 使用 marked 解析 Markdown
  224. * @param markdown 解析的文本
  225. */
  226. const versionIndex = ref(1);
  227. const renderMarkdown = markdown => {
  228. const renderer = new marked.Renderer();
  229. renderer.code = ({
  230. text,
  231. lang,
  232. escaped
  233. }) => {
  234. const language = hljs.getLanguage(lang) ? lang : 'plaintext';
  235. const highlighted = hljs.highlight(text, {
  236. language
  237. }).value;
  238. return `<div class="hljs-box"><div class="hljs-title">${language.toLocaleUpperCase()}</div><pre class="hljs-pre"><code class="hljs language-${language}">${highlighted}</code></pre></div>`;
  239. };
  240. renderer.link = (href) => {
  241. let divBox = document.createElement('div');
  242. let div = document.createElement('div');
  243. div.innerHTML = `version-${versionIndex.value}`
  244. div.id = href.href;
  245. div.className = 'version-href'
  246. versionIndex.value++;
  247. divBox.appendChild(div);
  248. previewUrl.value = href.href;
  249. return divBox.innerHTML;
  250. };
  251. return marked('```markdown' + markdown.replaceAll('</n>', '\n') + '```', {
  252. renderer
  253. });
  254. };
  255. const AIClick = (url) => {
  256. emits('updateURL', url);
  257. }
  258. const selectVisible = ref(false);
  259. const functionList = ref(['登录功能']);
  260. const selectFunction = data => {
  261. AIMessage('新增' + data);
  262. selectVisible.value = false;
  263. }
  264. // 组件卸载时清除定时器(重要!)
  265. onUnmounted(() => {
  266. stopTimer()
  267. })
  268. const showLine = (type, item, list) => {
  269. let arr = list.filter(node => node.type === 'step');
  270. let index = arr.findIndex(node => node.id === item.id);
  271. if (type === 'top') {
  272. if (index <= 0) return false;
  273. } else {
  274. if (index >= arr.length - 1) return false;
  275. }
  276. return true;
  277. }
  278. </script>
  279. <template>
  280. <div class="ai-chat-container">
  281. <div class="chat-body" ref="chatBody">
  282. <div v-for="(message, index) in chatHistory" :key="index">
  283. <div class="user-message">
  284. <div class="message user-message-box">
  285. <div class="message-content" v-if="message.query" v-html="message.query"></div>
  286. </div>
  287. </div>
  288. <div class="message ai-message" v-if="!loading || message.AIoutputs" style="margin-top: 15px;">
  289. <div class="message-header">
  290. <el-avatar :size="30"
  291. src="https://file-node.oss-cn-shanghai.aliyuncs.com/youji/f9617c7f80da485cb3cc72b6accc62ed">
  292. </el-avatar>
  293. <div class="message-header-label" style="width: 100px;">AI助手</div>
  294. </div>
  295. <div class="message-content">
  296. <div class="ai-website-boxs">
  297. <div v-for="(item,index) in message.AIoutputs">
  298. <div class="wk-query" v-if="item.type === 'query'">{{item.label}}</div>
  299. <div class="wk-markdown wk-query" v-if="item.type === 'markdown'"
  300. v-html="renderMarkdown(item.label)">
  301. </div>
  302. <div :class="'wk-step timeline-'+item.type" v-if="item.type === 'step'">
  303. <div :class="'step-icon ' + item.status">
  304. <el-icon>
  305. <Check v-if="item.status === 'success'" />
  306. <Loading v-else />
  307. </el-icon>
  308. </div>
  309. <div class="wk-content">
  310. <div class="title">{{ item.label }}</div>
  311. <div class="content" v-if="item.status === 'success'">
  312. <el-icon>
  313. <Monitor />
  314. </el-icon>
  315. <span class="content-label">{{ item.label }}</span>
  316. <span class="content-describe">{{ item.describe }}</span>
  317. </div>
  318. </div>
  319. <div class="line line-top" v-if="showLine('top',item,message.AIoutputs)"></div>
  320. <div class="line line-bottom" v-if="showLine('bottom',item,message.AIoutputs)">
  321. </div>
  322. </div>
  323. <div class="wk-url" v-if="item.type === 'url'">
  324. <div class="label">{{item.label || '网站生成成功'}}</div>
  325. <el-button size="small" type="primary" @click="AIClick(item.url)">点击查看</el-button>
  326. </div>
  327. </div>
  328. </div>
  329. <!-- <el-button size="small" @click="reloadMessage(message.query)" :disabled="loading"
  330. style="margin-top: 10px;" v-if="message.AIoutputs.indexOf('系统繁忙,请重试')>-1">
  331. 点击重试
  332. </el-button> -->
  333. </div>
  334. </div>
  335. </div>
  336. <div v-if="loading" class="typing-indicator">
  337. <div class="typing-dot"></div>
  338. <div class="typing-dot"></div>
  339. <div class="typing-dot"></div>
  340. <span style="margin-left: 10px;">思考中...</span>
  341. </div>
  342. </div>
  343. <div class="input-container">
  344. <div class="input-box">
  345. <el-input type="textarea" v-model="prompt" placeholder="给AI发送消息" resize="none" :rows="4"
  346. :autosize="{ minRows: 2, maxRows: 6 }">
  347. </el-input>
  348. <div class="input-button">
  349. <el-button style="margin-left: 10px;" size="default" :icon="Plus" :disabled="loading || !previewUrl"
  350. circle @click="selectVisible = true">
  351. </el-button>
  352. <el-button circle type="primary" @click="sendMessage" :disabled="!prompt.trim() || loading"
  353. :icon="Top">
  354. </el-button>
  355. </div>
  356. </div>
  357. </div>
  358. <el-dialog v-model="selectVisible" title="新增功能" width="800px" class="ai-dialog select-dialog">
  359. <div class="select-item">
  360. <el-row :gutter="15">
  361. <el-col :span="6" v-for="(item,index) in functionList" :key="index">
  362. <el-button style="width: 100%;" @click="selectFunction(item)">
  363. {{item}}
  364. </el-button>
  365. </el-col>
  366. </el-row>
  367. </div>
  368. </el-dialog>
  369. </div>
  370. </template>
  371. <style lang="scss">
  372. .message-image {
  373. width: 30px;
  374. }
  375. .hljs-box {
  376. border-radius: 5px;
  377. overflow: hidden;
  378. .hljs-title {
  379. background: #f2f3f5;
  380. padding: 5px 10px;
  381. color: #354052;
  382. font-weight: bold;
  383. border-bottom: 1px solid #d0d3d9;
  384. }
  385. .language-html,
  386. .language-markdown {
  387. background: #f1f2f5;
  388. }
  389. .hljs-pre {
  390. margin: 0;
  391. }
  392. }
  393. .version-href {
  394. border: 1px solid var(--el-border-color);
  395. padding: 5px 15px;
  396. border-radius: 6px;
  397. cursor: pointer;
  398. display: inline-block;
  399. margin-top: 5px;
  400. }
  401. .wk-query {
  402. padding: 12px 0;
  403. font-size: 15px;
  404. font-weight: 500;
  405. &:last-child {
  406. padding-bottom: 0;
  407. }
  408. }
  409. .wk-step {
  410. padding-top: 15px;
  411. padding-left: 20px;
  412. position: relative;
  413. .line {
  414. position: absolute;
  415. width: 1px;
  416. border-left: 1px dashed var(--el-border-color);
  417. left: 7px;
  418. z-index: 4;
  419. }
  420. .line-top {
  421. top: 0;
  422. height: 17px;
  423. }
  424. .line-bottom {
  425. top: 20px;
  426. bottom: 0;
  427. }
  428. .step-icon {
  429. position: absolute;
  430. left: 0;
  431. background: var(--el-color-warning);
  432. color: #fff;
  433. width: 14px;
  434. height: 14px;
  435. display: flex;
  436. align-items: center;
  437. justify-content: center;
  438. font-size: 12px;
  439. border-radius: 16px;
  440. top: 18px;
  441. z-index: 9;
  442. &.success {
  443. background: var(--el-color-success);
  444. }
  445. }
  446. .timeline-warning {
  447. .step-icon {
  448. animation: rotating 2s linear infinite;
  449. }
  450. }
  451. }
  452. .wk-url {
  453. padding-top: 15px;
  454. .label {
  455. font-size: 15px;
  456. font-weight: 500;
  457. margin-bottom: 10px;
  458. }
  459. }
  460. .wk-timeline {
  461. .el-timeline-item__icon {
  462. font-size: 10px;
  463. }
  464. .el-timeline-item {
  465. padding-bottom: 10px;
  466. }
  467. }
  468. .wk-content {
  469. .title {
  470. color: #222;
  471. }
  472. .content {
  473. background: rgb(246, 247, 249);
  474. border: 1px solid rgb(234, 234, 234);
  475. height: 30px;
  476. line-height: 28px;
  477. border-radius: 30px;
  478. padding: 0 15px;
  479. font-size: 13px;
  480. box-sizing: border-box;
  481. color: rgb(120, 121, 121);
  482. margin-top: 10px;
  483. display: flex;
  484. align-items: center;
  485. }
  486. .content-label {
  487. margin: 0 10px 0 5px;
  488. }
  489. .content-describe {
  490. font-size: 12px;
  491. }
  492. }
  493. </style>
  494. <style scoped lang="scss">
  495. .ai-chat-container {
  496. width: 100%;
  497. height: 100%;
  498. background: white;
  499. border-radius: 16px;
  500. box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  501. overflow: hidden;
  502. display: flex;
  503. flex-direction: column;
  504. }
  505. /* 聊天内容区域 */
  506. .chat-body {
  507. flex: 1;
  508. padding: 20px;
  509. overflow-y: auto;
  510. background: #f8fafc;
  511. display: flex;
  512. flex-direction: column;
  513. gap: 15px;
  514. overflow-x: hidden;
  515. }
  516. .message {
  517. padding: 12px 16px;
  518. border-radius: 18px;
  519. position: relative;
  520. animation: fadeIn 0.3s ease;
  521. line-height: 1.5;
  522. display: inline-block;
  523. max-width: 100%;
  524. }
  525. .user-message {
  526. display: flex;
  527. justify-content: end;
  528. .user-message-box {
  529. background: #7a72cf;
  530. color: white;
  531. border-bottom-right-radius: 4px;
  532. }
  533. }
  534. .ai-message {
  535. background: #ffffff;
  536. border: 1px solid #e9ecef;
  537. margin-right: auto;
  538. border-bottom-left-radius: 4px;
  539. width: 100%;
  540. }
  541. .message-header {
  542. display: flex;
  543. align-items: center;
  544. font-weight: 500;
  545. }
  546. .message-header-label {
  547. flex: 1;
  548. width: 0;
  549. overflow: hidden;
  550. margin-left: 5px;
  551. font-weight: bold;
  552. }
  553. .message-time {
  554. font-size: 0.7rem;
  555. opacity: 0.7;
  556. margin-top: 5px;
  557. text-align: right;
  558. }
  559. /* 加载指示器 */
  560. .typing-indicator {
  561. display: flex;
  562. align-items: center;
  563. padding: 12px 16px;
  564. background: white;
  565. border: 1px solid #e9ecef;
  566. border-radius: 18px;
  567. width: fit-content;
  568. margin-bottom: 15px;
  569. border-bottom-left-radius: 4px;
  570. }
  571. .typing-dot {
  572. width: 8px;
  573. height: 8px;
  574. background: #6c757d;
  575. border-radius: 50%;
  576. margin: 0 3px;
  577. animation: typing 1.4s infinite;
  578. }
  579. .typing-dot:nth-child(1) {
  580. animation-delay: 0s;
  581. }
  582. .typing-dot:nth-child(2) {
  583. animation-delay: 0.2s;
  584. }
  585. .typing-dot:nth-child(3) {
  586. animation-delay: 0.4s;
  587. }
  588. /* 输入区域 */
  589. .input-container {
  590. padding: 10px;
  591. box-sizing: border-box;
  592. background: #f8fafc;
  593. .input-box {
  594. background: #ffffff;
  595. padding: 5px 0;
  596. border: 1px solid var(--el-border-color);
  597. border-radius: 10px;
  598. }
  599. :deep(.el-textarea__inner) {
  600. border: none;
  601. background: transparent;
  602. box-shadow: none;
  603. &:hover {
  604. box-shadow: none;
  605. }
  606. }
  607. }
  608. .input-button {
  609. margin-top: 10px;
  610. text-align: right;
  611. padding: 5px 10px;
  612. }
  613. /* 动画 */
  614. @keyframes fadeIn {
  615. from {
  616. opacity: 0;
  617. transform: translateY(10px);
  618. }
  619. to {
  620. opacity: 1;
  621. transform: translateY(0);
  622. }
  623. }
  624. @keyframes typing {
  625. 0%,
  626. 60%,
  627. 100% {
  628. transform: translateY(0);
  629. }
  630. 30% {
  631. transform: translateY(-5px);
  632. }
  633. }
  634. </style>