temp
This commit is contained in:
42
index.html
42
index.html
@@ -2073,6 +2073,12 @@
|
||||
chatMessages.appendChild(msg);
|
||||
chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
}
|
||||
function uuid() {
|
||||
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
|
||||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
function sendMessage(isPre) {
|
||||
@@ -2130,6 +2136,22 @@
|
||||
}
|
||||
getAiResponse(imgUrl);
|
||||
}
|
||||
let renderPending = false;
|
||||
let lastRendered = "";
|
||||
|
||||
// 实时渲染 + 不闪烁 + 性能好
|
||||
function smartRender(msgDiv, text) {
|
||||
if (renderPending) return; // 避免频繁渲染
|
||||
|
||||
renderPending = true;
|
||||
requestAnimationFrame(() => {
|
||||
if (lastRendered !== text) {
|
||||
msgDiv.innerHTML = marked.parse(text); // 实时解析 Markdown
|
||||
lastRendered = text;
|
||||
}
|
||||
renderPending = false;
|
||||
});
|
||||
}
|
||||
let reader;
|
||||
const getAiResponse = async (img) => {
|
||||
|
||||
@@ -2158,7 +2180,8 @@
|
||||
|
||||
// ✅ 创建 AbortController
|
||||
controller = new AbortController();
|
||||
sendParam.requestId = crypto.randomUUID();
|
||||
// sendParam.requestId = crypto.randomUUID();
|
||||
sendParam.requestId=uuid();
|
||||
|
||||
const res = await fetch("https://serviceai.satabot.com/analyze", {
|
||||
method: "POST",
|
||||
@@ -2168,7 +2191,7 @@
|
||||
}).catch(err => {
|
||||
console.log("✅ 流被中断:", err.name);
|
||||
clearInterval(interval);
|
||||
controller.abort();
|
||||
// controller.abort();
|
||||
|
||||
controller = null;
|
||||
|
||||
@@ -2240,6 +2263,9 @@
|
||||
const stopBtn = document.querySelector('#stopBtn');
|
||||
sendBtnImg.classList.remove('none');
|
||||
stopBtn.classList.add('none');
|
||||
msgDiv.innerHTML = marked.parse(aiMsg.content);
|
||||
|
||||
|
||||
setTimeout(scrollToBottom, 0); // chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
|
||||
return;
|
||||
@@ -2256,10 +2282,14 @@
|
||||
// }
|
||||
if (obj.content && obj.type !== "title") {
|
||||
for (const char of obj.content) {
|
||||
await new Promise(r => setTimeout(r, 1)); // 控制字速
|
||||
await new Promise(r => setTimeout(r, 2)); // 控制字速
|
||||
if (isStopped) return;
|
||||
aiMsg.content += char;
|
||||
msgDiv.innerHTML = marked.parse(aiMsg.content);
|
||||
smartRender(msgDiv, aiMsg.content);
|
||||
|
||||
// msgDiv.insertAdjacentText("beforeend", char);
|
||||
|
||||
// msgDiv.innerHTML = marked.parse(aiMsg.content);
|
||||
|
||||
// chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
setTimeout(scrollToBottom, 0);
|
||||
@@ -2429,7 +2459,7 @@
|
||||
const deleteSession = (sessionId, userId) => {
|
||||
if (activeChatId == sessionId) {
|
||||
if (controller) {
|
||||
controller.abort();
|
||||
// controller.abort();
|
||||
controller = null;
|
||||
if (sendBtnImg.classList.contains('none')) {
|
||||
sendBtnImg.classList.remove('none');
|
||||
@@ -2706,7 +2736,7 @@
|
||||
}
|
||||
|
||||
if (controller) {
|
||||
controller.abort();
|
||||
// controller.abort();
|
||||
controller = null;
|
||||
if (sendBtnImg.classList.contains('none')) {
|
||||
sendBtnImg.classList.remove('none');
|
||||
|
||||
Reference in New Issue
Block a user