From 4ace68af4236a1be8b57dbcfc185871eda76fed9 Mon Sep 17 00:00:00 2001
From: yinbangzhong <zhongbangyin@126.com>
Date: 星期四, 08 八月 2024 16:48:08 +0800
Subject: [PATCH] 会话实时显示
---
yarn.lock | 5 ++
package.json | 15 ++-----
src/views/session/sessionRecordsManager/index.vue | 63 +++++++++++++++++++++----------
config/vite.config.prod.ts | 23 -----------
4 files changed, 52 insertions(+), 54 deletions(-)
diff --git a/config/vite.config.prod.ts b/config/vite.config.prod.ts
index 9083aae..79a8864 100644
--- a/config/vite.config.prod.ts
+++ b/config/vite.config.prod.ts
@@ -14,29 +14,6 @@
configArcoResolverPlugin(),
configImageminPlugin(),
],
- server: {
- open: true, // 椤圭洰鍚姩鍚庯紝鑷姩鎵撳紑
- fs: {
- strict: true,
- },
- proxy: {
- '/base': {
- target: 'http://192.168.20.116:8089',
- changeOrigin: true,
- ws: true,
- },
- '/api': {
- target: 'http://192.168.20.116:8089',
- changeOrigin: true,
- ws: true,
- },
- '/api/v1': {
- target: 'http://192.168.20.116:8089',
- changeOrigin: true,
- ws: true,
- },
- },
- },
build: {
rollupOptions: {
output: {
diff --git a/package.json b/package.json
index eadf093..2ef733a 100644
--- a/package.json
+++ b/package.json
@@ -15,17 +15,9 @@
"prepare": "husky install"
},
"lint-staged": {
- "*.{js,ts,jsx,tsx}": [
-
-
- ],
- "*.vue": [
-
-
- ],
- "*.{less,css}": [
-
- ]
+ "*.{js,ts,jsx,tsx}": [],
+ "*.vue": [],
+ "*.{less,css}": []
},
"pnpm": {
"overrides": {
@@ -41,6 +33,7 @@
"axios": "^0.24.0",
"dayjs": "^1.11.5",
"echarts": "^5.4.0",
+ "eventsource-parser": "^2.0.1",
"js-base64": "^3.7.5",
"jsencrypt": "^3.3.2",
"lodash": "^4.17.21",
diff --git a/src/views/session/sessionRecordsManager/index.vue b/src/views/session/sessionRecordsManager/index.vue
index d64b2df..4b22809 100644
--- a/src/views/session/sessionRecordsManager/index.vue
+++ b/src/views/session/sessionRecordsManager/index.vue
@@ -306,6 +306,7 @@
import { useAppStore } from '@/store';
import { computed, nextTick, onMounted, reactive, ref } from 'vue';
import { Message } from '@arco-design/web-vue';
+ import { EventSourceParserStream } from 'eventsource-parser/stream';
import moment from 'moment';
import AddSession from '@/views/session/sessionManager/components/addSession.vue';
import {
@@ -314,6 +315,8 @@
getSessionDetailsApi,
sessionListApi,
} from '@/api/session';
+ import { getAuthorization } from "@/utils/auth";
+
const sessionDetailList = ref([]); //鏍规嵁浼氳瘽id鍑烘潵鐨勪細璇濊鎯�
const sessionList = ref([]); //浼氳瘽鍒楄〃
@@ -353,30 +356,49 @@
return;
}
if (inputMsg.value) {
- const data = await chatApi({
- conversation_id: activeSessionId.value,
- messages: inputMsg.value,
+ sessionDetailList.value.push({ "content": inputMsg.value, "role": "user" });
+ sessionDetailList.value.push({ "role": "last" });
+ refreshScroll();
+ const response = await fetch("/api/tech/cloudminds/query?modeltype=localragflow", {
+ method: 'POST',
+ headers: {
+ 'Authorization': getAuthorization(),
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ conversation_id: activeSessionId.value,
+ messages: inputMsg.value,
+ }),
});
- if (data) {
- chatDis.value = false;
- // inputMsg.value = '';
- // querySessionDetail({ id: activeSessionId.value });
- }
- const res = await getSessionDetailsApi(activeSessionId.value);
- if (res.code === 200) {
- sessionDetailList.value = res.data.message.map((item, index) => {
- if (index === res.data.message.length - 1) {
- item.role = 'last';
- displayedText.value = '';
- currIndex.value = 0;
- streamStr.value = item.content;
- startDisplayStr();
+ const reader = response?.body
+ ?.pipeThrough(new TextDecoderStream())
+ .pipeThrough(new EventSourceParserStream())
+ .getReader();
+ currIndex.value = 0;
+ while (true) {
+ const x = await reader?.read();
+ if (x) {
+ const { done, value } = x;
+ try {
+ const val = JSON.parse(value?.data || '');
+ const d = val?.data;
+ if (typeof d !== "boolean") {
+
+ console.info("data:", d);
+ streamStr.value = d.content;
+ startDisplayStr();
+ }
+ } catch (e) {
+ console.warn(e);
}
- return item;
- });
- refreshScroll();
+ if (done) {
+ console.info('done');
+ break;
+ }
+ }
}
+ chatDis.value=false;
inputMsg.value = '';
} else {
Message.warning('娑堟伅涓嶈兘涓虹┖');
@@ -428,6 +450,7 @@
displayedText.value += res[currIndex.value];
currIndex.value++;
setTimeout(startDisplayStr, 100);
+ refreshScroll();
} else {
clearTimeout(timer!);
timer = null;
diff --git a/yarn.lock b/yarn.lock
index 7ee4e52..6cb148f 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3253,6 +3253,11 @@
resolved "https://registry.npmmirror.com/eventemitter3/-/eventemitter3-5.0.1.tgz"
integrity sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==
+eventsource-parser@^2.0.1:
+ version "2.0.1"
+ resolved "https://registry.npmmirror.com/eventsource-parser/-/eventsource-parser-2.0.1.tgz#6464341d64de5bea9f31d8063dd312b616146c7a"
+ integrity sha512-gMaRLm5zejEH9mNXC54AnIteFI9YwL/q5JKMdBnoG+lEI1JWVGFVk0Taaj9Xb5SKgzIBDZoQX5IzMe44ILWODg==
+
exec-buffer@^3.0.0:
version "3.2.0"
resolved "https://registry.npmmirror.com/exec-buffer/-/exec-buffer-3.2.0.tgz"
--
Gitblit v1.8.0