|
@@ -27,6 +27,9 @@
|
|
|
// 引入 github 代码主题
|
|
|
import 'highlight.js/styles/github.css';
|
|
|
import config from '@/config'
|
|
|
+ import {
|
|
|
+ fetchEventSource
|
|
|
+ } from '@microsoft/fetch-event-source';
|
|
|
const emits = defineEmits(['updateURL']);
|
|
|
const user = ref(useUserStore().userData);
|
|
|
// 对话历史
|
|
@@ -120,15 +123,19 @@
|
|
|
AIoutputs: ''
|
|
|
})
|
|
|
// 2. 初始化状态
|
|
|
- loading.value = true
|
|
|
+ loading.value = true;
|
|
|
currentAIResponse.value = ''
|
|
|
prompt.value = '' // 清空输入框
|
|
|
- const response = await fetch(`${config.baseURL}/api/ai/chat/run/7`, {
|
|
|
- method: "POST",
|
|
|
+ let result = '';
|
|
|
+ fetchEventSource(`${config.baseURL}/api/ai/chat/run/7`, {
|
|
|
+ method: 'POST',
|
|
|
headers: {
|
|
|
+ 'Accept': 'text/event-stream',
|
|
|
"Content-Type": "application/json",
|
|
|
"token": useUserStore().token
|
|
|
},
|
|
|
+ timeout: 1200000000, // 超时
|
|
|
+ openWhenHidden: true,
|
|
|
body: JSON.stringify({
|
|
|
query: userMessage,
|
|
|
conversationId: conversationId.value,
|
|
@@ -136,31 +143,43 @@
|
|
|
websiteURL: "",
|
|
|
fileURL: ""
|
|
|
}
|
|
|
- })
|
|
|
- });
|
|
|
- const reader = response.body.getReader();
|
|
|
- const decoder = new TextDecoder();
|
|
|
- let done = false;
|
|
|
- let result = '';
|
|
|
- while (!done) {
|
|
|
- const {
|
|
|
- value,
|
|
|
- done: doneReading
|
|
|
- } = await reader.read();
|
|
|
- done = doneReading;
|
|
|
- let resultString = decoder.decode(value, {
|
|
|
- stream: true
|
|
|
- });
|
|
|
- loading.value = false;
|
|
|
- result += resultString.replaceAll('data:', '');
|
|
|
- console.log(result);
|
|
|
- chatHistory.value[chatHistory.value.length - 1].AIoutputs = renderMarkdown(result);
|
|
|
- scrollToBottom();
|
|
|
- }
|
|
|
+ }),
|
|
|
+ onmessage(event) {
|
|
|
+ // 处理接收到的消息
|
|
|
+ if (!isStrictJSONString(event.data)) {
|
|
|
+ result += event.data;
|
|
|
+ console.log(result);
|
|
|
+ chatHistory.value[chatHistory.value.length - 1].AIoutputs = renderMarkdown(result);
|
|
|
+ scrollToBottom();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ onopen(response) {
|
|
|
+ // 连接打开时的回调
|
|
|
+ console.log('Connection opened:', response.status);
|
|
|
+ },
|
|
|
+ onerror(error) {
|
|
|
+ // 错误处理
|
|
|
+ console.error('Error:', error);
|
|
|
+ },
|
|
|
+ onclose() {
|
|
|
+ // 连接关闭时的回调
|
|
|
+ loading.value = false;
|
|
|
+ }
|
|
|
+ })
|
|
|
} catch (error) {
|
|
|
console.error("请求失败:", error);
|
|
|
}
|
|
|
}
|
|
|
+ const isStrictJSONString = str => {
|
|
|
+ console.log(str);
|
|
|
+ try {
|
|
|
+ const parsed = JSON.parse(str);
|
|
|
+ return parsed !== null && typeof parsed === 'object';
|
|
|
+ } catch (e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
const scrollToBottom = () => {
|
|
|
nextTick(() => {
|
|
|
if (chatBody.value) {
|
|
@@ -261,7 +280,7 @@
|
|
|
:autosize="{ minRows: 2, maxRows: 6 }">
|
|
|
</el-input>
|
|
|
<div class="input-button">
|
|
|
- <el-button style="margin-left: 10px;" size="default" :icon="Plus" circle
|
|
|
+ <el-button style="margin-left: 10px;" size="default" :icon="Plus" :disabled="loading" circle
|
|
|
@click="selectVisible = true">
|
|
|
</el-button>
|
|
|
<el-button circle type="primary" @click="sendMessage" :disabled="!prompt.trim() || loading"
|