|
|
|
@ -308,15 +308,15 @@
|
|
|
|
|
function submitQuestion() {
|
|
|
|
|
const question = document.getElementById('questionInput').value.trim();
|
|
|
|
|
const answerArea = document.getElementById('answerArea');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!question) {
|
|
|
|
|
alert('请输入问题!');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加加载动画
|
|
|
|
|
answerArea.innerHTML = '<div class="loading-animation"><div class="spinner"></div><div>思考中...</div></div>';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fetch('/api/rag', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
@ -332,18 +332,18 @@
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
let buffer = '';
|
|
|
|
|
let accumulatedContent = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function processChunk() {
|
|
|
|
|
return reader.read().then(({done, value}) => {
|
|
|
|
|
if (done) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
buffer += decoder.decode(value, {stream: true});
|
|
|
|
|
const lines = buffer.split('\n');
|
|
|
|
|
buffer = lines.pop();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
|
if (line.includes('data:')) {
|
|
|
|
|
const jsonStr = line.replace(/^data:\s*/, '').trim();
|
|
|
|
|
const jsonStr = line.replace(/^data:\s*/, '').replace(/^data:\s*/, '').trim();
|
|
|
|
|
if (jsonStr) {
|
|
|
|
|
try {
|
|
|
|
|
const data = JSON.parse(jsonStr);
|
|
|
|
@ -358,14 +358,23 @@
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return processChunk();
|
|
|
|
|
|
|
|
|
|
return processChunk().then(() => {
|
|
|
|
|
// 在流处理完成后保存完整的markdown内容
|
|
|
|
|
localStorage.setItem('lastMarkdownContent', accumulatedContent);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return processChunk();
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
answerArea.innerHTML = '<div style="color:red">请求出错,请重试</div>';
|
|
|
|
|
// 移除加载动画
|
|
|
|
|
answerArea.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
// 出错时也移除加载动画
|
|
|
|
|
answerArea.innerHTML = '<div style="color:red">请求出错,请重试</div>';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|