基本完成深度思考内容
This commit is contained in:
655
index.html
655
index.html
@@ -1182,6 +1182,41 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.thinking-loading {
|
||||||
|
color: #888;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-box {
|
||||||
|
margin-bottom: 12px;
|
||||||
|
border: 1px dashed #ddd;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 13px;
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-header {
|
||||||
|
padding: 6px 10px;
|
||||||
|
cursor: pointer;
|
||||||
|
background: #f7f7f7;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-content {
|
||||||
|
padding: 8px 10px;
|
||||||
|
line-height: 1.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-content.collapsed {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.answer-box {
|
||||||
|
font-size: 15px;
|
||||||
|
line-height: 1.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@media (max-width: 768px) {
|
@media (max-width: 768px) {
|
||||||
.popbgc {
|
.popbgc {
|
||||||
@@ -2113,7 +2148,7 @@
|
|||||||
if (msg.image_base64) {
|
if (msg.image_base64) {
|
||||||
hasImage = true; // 只要有一个不为空,就置为 true
|
hasImage = true; // 只要有一个不为空,就置为 true
|
||||||
}
|
}
|
||||||
addMessage(msg.content, msg.role, msg.image_base64);
|
addMessage(msg.content, msg.role, msg.image_base64,msg.thinking);
|
||||||
});
|
});
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
}
|
}
|
||||||
@@ -2138,89 +2173,122 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 内容 角色 图片 创造时间
|
// 内容 角色 图片 创造时间
|
||||||
function addMessage(content = '', role = "assistant", imgUrl = null, time = null) {
|
function addMessage(content = '', role = "assistant", imgUrl = null, time = null,thinking='') {
|
||||||
const msg = document.createElement("div");
|
const msg = document.createElement("div");
|
||||||
msg.className = `message ${role}`;
|
msg.className = `message ${role}`;
|
||||||
if (time) {
|
|
||||||
const timeNode = document.createElement("div");
|
|
||||||
timeNode.textContent = time;
|
|
||||||
timeNode.style.fontSize = "12px";
|
|
||||||
timeNode.style.color = "#999";
|
|
||||||
timeNode.style.marginTop = "4px";
|
|
||||||
// msg.appendChild(timeNode);
|
|
||||||
}
|
|
||||||
// 如果有文字内容,先创建文字节点
|
|
||||||
if (content) {
|
|
||||||
const textNode = document.createElement("div");
|
|
||||||
textNode.innerHTML = marked.parse(content);
|
|
||||||
msg.appendChild(textNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果有图片,创建图片节点
|
// =========================
|
||||||
if (imgUrl) {
|
// 时间
|
||||||
const img = document.createElement("img");
|
// =========================
|
||||||
img.src = imgUrl;
|
if (time) {
|
||||||
img.style.marginTop = content ? "8px" : "0"; // 如果有文字,图片加一点间距
|
const timeNode = document.createElement("div");
|
||||||
msg.appendChild(img);
|
timeNode.textContent = time;
|
||||||
}
|
timeNode.style.fontSize = "12px";
|
||||||
// 如果是 assistant,添加分享按钮
|
timeNode.style.color = "#999";
|
||||||
if (role === "assistant") {
|
timeNode.style.marginBottom = "6px";
|
||||||
const shareSpan = document.createElement("span");
|
msg.appendChild(timeNode);
|
||||||
shareSpan.style.display = "inline-flex";
|
}
|
||||||
shareSpan.style.alignItems = "center";
|
|
||||||
shareSpan.style.cursor = "pointer";
|
|
||||||
shareSpan.style.marginTop = "8px";
|
|
||||||
shareSpan.style.opacity = "0.75";
|
|
||||||
shareSpan.style.transition = "opacity .2s";
|
|
||||||
shareSpan.title = "分享";
|
|
||||||
|
|
||||||
shareSpan.onmouseenter = () => shareSpan.style.opacity = "1";
|
// =========================
|
||||||
shareSpan.onmouseleave = () => shareSpan.style.opacity = "0.75";
|
// 思考过程(历史:已完成)
|
||||||
|
// =========================
|
||||||
|
let thinkingContentDiv = null;
|
||||||
|
|
||||||
shareSpan.innerHTML = `
|
if (role === "assistant" && thinking) {
|
||||||
<svg t="1764059839985" class="icon" viewBox="0 0 1236 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5667" width="20" height="20"><path d="M741.743 1018.343c-28.287 0-50.917-11.315-73.547-28.288-22.63-22.63-39.602-50.917-39.602-84.862V792.044c-124.464 0-328.133 33.945-435.624 181.039-16.973 28.287-56.575 45.26-90.52 50.917H85.478C28.903 1012.685-5.042 961.768 0.616 905.193c28.287-243.27 113.15-418.652 260.243-537.458 107.492-84.862 231.956-130.122 367.735-141.437V118.807c0-50.917 22.63-96.177 67.89-113.15C736.086-5.657 781.345 0 815.29 33.945l362.077 367.735c28.288 22.63 45.26 56.574 50.918 96.176 5.657 39.603-5.658 79.205-33.945 107.492-5.658 5.658-11.315 16.972-22.63 22.63l-350.762 356.42c-22.63 22.63-50.918 33.945-79.205 33.945z m-90.52-339.448h90.52v226.298l356.42-367.734 5.658-5.658c5.657-5.657 5.657-16.972 5.657-22.63 0-11.315-5.657-16.972-11.315-22.63l-5.657-5.657-356.42-362.077V333.79l-79.205 5.658c-118.806 0-231.956 39.602-328.132 113.149-113.15 90.519-186.696 237.613-209.326 429.967 141.436-175.382 390.364-203.669 531.8-203.669z" p-id="5668" fill="#878895"></path></svg>
|
const thinkingBox = document.createElement("div");
|
||||||
`;
|
thinkingBox.className = "thinking-box";
|
||||||
|
|
||||||
shareSpan.onclick = () => {
|
const header = document.createElement("div");
|
||||||
generatePoster(content, imgUrl);
|
header.className = "thinking-header";
|
||||||
};
|
|
||||||
|
|
||||||
msg.appendChild(shareSpan);
|
const title = document.createElement("span");
|
||||||
const copySpan = document.createElement("span");
|
title.className = "thinking-title";
|
||||||
copySpan.style.display = "inline-flex";
|
title.textContent = "";
|
||||||
copySpan.style.alignItems = "center";
|
|
||||||
copySpan.style.cursor = "pointer";
|
|
||||||
copySpan.style.marginTop = "8px";
|
|
||||||
copySpan.style.marginLeft = "10px";
|
|
||||||
copySpan.style.opacity = "0.75";
|
|
||||||
copySpan.style.transition = "opacity .2s";
|
|
||||||
copySpan.title = "复制内容";
|
|
||||||
|
|
||||||
copySpan.onmouseenter = () => copySpan.style.opacity = "1";
|
const status = document.createElement("span");
|
||||||
copySpan.onmouseleave = () => copySpan.style.opacity = "0.75";
|
status.className = "thinking-status thinking-done";
|
||||||
|
status.textContent = "已完成思考";
|
||||||
|
|
||||||
copySpan.innerHTML = `
|
const toggle = document.createElement("span");
|
||||||
<svg t="1764549398754" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5335" width="20" height="20"><path d="M639.9 256.3H192.4c-35.2 0-63.9 28.8-63.9 63.9v575.4c0 35.2 28.8 63.9 63.9 63.9h447.5c35.2 0 63.9-28.8 63.9-63.9V320.2c0-35.1-28.8-63.9-63.9-63.9z m0 639.3H192.4V320.2h447.5v575.4z" fill="#878895" p-id="5336"></path><path d="M831.6 64.5H384.1c-35.2 0-63.9 28.8-63.9 63.9v63.9h63.9v-63.9h447.5v575.4h-63.9v63.9h63.9c35.2 0 63.9-28.8 63.9-63.9V128.4c0.1-35.1-28.7-63.9-63.9-63.9z" fill="#878895" p-id="5337"></path></svg>
|
toggle.className = "thinking-toggle";
|
||||||
`;
|
toggle.textContent = "▶";
|
||||||
copySpan.onclick = async () => {
|
|
||||||
try {
|
|
||||||
await navigator.clipboard.writeText(content);
|
|
||||||
copySpan.style.opacity = "1";
|
|
||||||
showToast("复制成功")
|
|
||||||
copySpan.title = "复制成功";
|
|
||||||
setTimeout(() => { copySpan.title = "复制内容"; }, 1200);
|
|
||||||
} catch (err) {
|
|
||||||
alert("复制失败,可能不支持 HTTPS 环境以外复制。");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
msg.appendChild(copySpan);
|
thinkingContentDiv = document.createElement("div");
|
||||||
}
|
thinkingContentDiv.className = "thinking-content collapsed";
|
||||||
|
thinkingContentDiv.innerHTML = marked.parse(thinking);
|
||||||
|
|
||||||
// 如果有时间,可以显示
|
header.onclick = () => {
|
||||||
|
thinkingContentDiv.classList.toggle("collapsed");
|
||||||
|
toggle.textContent =
|
||||||
|
thinkingContentDiv.classList.contains("collapsed") ? "▶" : "⌄";
|
||||||
|
};
|
||||||
|
|
||||||
chatMessages.appendChild(msg);
|
header.appendChild(title);
|
||||||
chatMessages.scrollTop = chatMessages.scrollHeight;
|
header.appendChild(status);
|
||||||
|
header.appendChild(toggle);
|
||||||
|
|
||||||
|
thinkingBox.appendChild(header);
|
||||||
|
thinkingBox.appendChild(thinkingContentDiv);
|
||||||
|
msg.appendChild(thinkingBox);
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// 最终结论(answer)
|
||||||
|
// =========================
|
||||||
|
if (content) {
|
||||||
|
const textNode = document.createElement("div");
|
||||||
|
textNode.className = "answer-content";
|
||||||
|
textNode.innerHTML = marked.parse(content);
|
||||||
|
msg.appendChild(textNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// 图片
|
||||||
|
// =========================
|
||||||
|
if (imgUrl) {
|
||||||
|
const img = document.createElement("img");
|
||||||
|
img.src = imgUrl;
|
||||||
|
img.style.marginTop = "8px";
|
||||||
|
msg.appendChild(img);
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// assistant 工具按钮
|
||||||
|
// =========================
|
||||||
|
if (role === "assistant") {
|
||||||
|
const shareSpan = document.createElement("span");
|
||||||
|
shareSpan.style.display = "inline-flex";
|
||||||
|
shareSpan.style.alignItems = "center";
|
||||||
|
shareSpan.style.cursor = "pointer";
|
||||||
|
shareSpan.style.marginTop = "8px";
|
||||||
|
shareSpan.style.opacity = "0.75";
|
||||||
|
shareSpan.title = "分享";
|
||||||
|
|
||||||
|
shareSpan.innerHTML = `<!-- 你的分享 SVG 原样放这里 -->`;
|
||||||
|
shareSpan.onclick = () => {
|
||||||
|
generatePoster(content, imgUrl);
|
||||||
|
};
|
||||||
|
|
||||||
|
const copySpan = document.createElement("span");
|
||||||
|
copySpan.style.display = "inline-flex";
|
||||||
|
copySpan.style.alignItems = "center";
|
||||||
|
copySpan.style.cursor = "pointer";
|
||||||
|
copySpan.style.marginLeft = "10px";
|
||||||
|
copySpan.style.opacity = "0.75";
|
||||||
|
copySpan.title = "复制内容";
|
||||||
|
|
||||||
|
copySpan.innerHTML = `<!-- 你的复制 SVG 原样放这里 -->`;
|
||||||
|
copySpan.onclick = async () => {
|
||||||
|
await navigator.clipboard.writeText(content);
|
||||||
|
showToast("复制成功");
|
||||||
|
};
|
||||||
|
|
||||||
|
msg.appendChild(shareSpan);
|
||||||
|
msg.appendChild(copySpan);
|
||||||
|
}
|
||||||
|
|
||||||
|
chatMessages.appendChild(msg);
|
||||||
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generatePoster(text, imgUrl) {
|
function generatePoster(text, imgUrl) {
|
||||||
@@ -2367,279 +2435,212 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
let reader;
|
let reader;
|
||||||
const getAiResponse = async (img) => {
|
const getAiResponse = async (img) => {
|
||||||
|
deleteImg();
|
||||||
|
|
||||||
deleteImg();
|
// =========================
|
||||||
const aiMsg = { role: "assistant", content: "", isImage: false };
|
// 消息容器
|
||||||
const msgDiv = document.createElement("div");
|
// =========================
|
||||||
msgDiv.className = "message assistant";
|
const msgDiv = document.createElement("div");
|
||||||
msgDiv.textContent = '思考中';
|
msgDiv.className = "message assistant";
|
||||||
let dots = 0;
|
chatMessages.appendChild(msgDiv);
|
||||||
const interval = setInterval(() => {
|
scrollToBottom();
|
||||||
dots = (dots + 1) % 4; // 0,1,2,3
|
|
||||||
msgDiv.textContent = '思考中' + '.'.repeat(dots);
|
|
||||||
}, 200);
|
|
||||||
|
|
||||||
msgDiv.className = "message assistant thinking ";
|
// =========================
|
||||||
chatMessages.appendChild(msgDiv);
|
// 状态变量
|
||||||
chatMessages.scrollTop = chatMessages.scrollHeight;
|
// =========================
|
||||||
sessId = sendParam.sessionId;
|
let currentPhase = null;
|
||||||
sendParam.imageBase64 = img;
|
let thinkingFinished = false;
|
||||||
// sendParam.userId= encodedUserId;
|
|
||||||
sendParam.userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
|
|
||||||
|
|
||||||
|
let thinkingContentDiv = null;
|
||||||
|
let answerContentDiv = null;
|
||||||
|
|
||||||
// ✅ 创建 AbortController
|
let thinkingStatusSpan = null;
|
||||||
controller = new AbortController();
|
let thinkingToggleSpan = null;
|
||||||
// sendParam.requestId = crypto.randomUUID();
|
|
||||||
sendParam.requestId = uuid();
|
|
||||||
|
|
||||||
const res = await fetch("https://serviceai.satabot.com/analyze", {
|
let thinkingBuffer = "";
|
||||||
method: "POST",
|
let answerBuffer = "";
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify(sendParam),
|
|
||||||
signal: controller.signal, // ✅ 加上 signal
|
|
||||||
}).catch(err => {
|
|
||||||
console.log("✅ 流被中断:", err.name);
|
|
||||||
clearInterval(interval);
|
|
||||||
if (controller) {
|
|
||||||
controller.abort();
|
|
||||||
controller = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// loading(phase 前占位)
|
||||||
|
// =========================
|
||||||
|
const loadingDiv = document.createElement("div");
|
||||||
|
loadingDiv.className = "thinking-loading";
|
||||||
|
loadingDiv.textContent = "深度思考中";
|
||||||
|
msgDiv.appendChild(loadingDiv);
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// DOM 工具函数
|
||||||
|
// =========================
|
||||||
|
const ensureThinkingBox = () => {
|
||||||
|
if (thinkingContentDiv) return;
|
||||||
|
|
||||||
|
loadingDiv?.remove();
|
||||||
|
|
||||||
sendBtnImg.classList.remove('none');
|
const box = document.createElement("div");
|
||||||
stopBtn.forEach(e => {
|
box.className = "thinking-box";
|
||||||
e.classList.add('none')
|
|
||||||
})
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(res, 7665)
|
const header = document.createElement("div");
|
||||||
if (!res) {
|
header.className = "thinking-header";
|
||||||
|
|
||||||
|
const title = document.createElement("span");
|
||||||
|
title.className = "thinking-title";
|
||||||
|
|
||||||
if (sendBtnImg.classList.contains("none")) {
|
thinkingStatusSpan = document.createElement("span");
|
||||||
sendBtnImg.classList.remove('none');
|
thinkingStatusSpan.className = "thinking-status thinking-running";
|
||||||
}
|
thinkingStatusSpan.textContent = "深度思考中";
|
||||||
stopBtn.forEach(e => {
|
|
||||||
if (!e.classList.contains('none')) {
|
|
||||||
e.classList.add('none')
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
thinkingToggleSpan = document.createElement("span");
|
||||||
|
thinkingToggleSpan.className = "thinking-toggle";
|
||||||
|
thinkingToggleSpan.textContent = "⌄";
|
||||||
|
|
||||||
return null;
|
thinkingContentDiv = document.createElement("div");
|
||||||
}
|
thinkingContentDiv.className = "thinking-content";
|
||||||
if (res.status === 400) {
|
|
||||||
clearInterval(interval);
|
|
||||||
if (controller) {
|
|
||||||
controller.abort();
|
|
||||||
controller = null;
|
|
||||||
}
|
|
||||||
if (sendBtnImg.classList.contains("none")) {
|
|
||||||
sendBtnImg.classList.remove('none');
|
|
||||||
|
|
||||||
}
|
header.onclick = () => {
|
||||||
stopBtn.forEach(e => {
|
thinkingContentDiv.classList.toggle("collapsed");
|
||||||
if (!e.classList.contains("none")) {
|
thinkingToggleSpan.textContent =
|
||||||
e.classList.add('none')
|
thinkingContentDiv.classList.contains("collapsed") ? "▶" : "⌄";
|
||||||
}
|
};
|
||||||
})
|
|
||||||
showpop();
|
header.appendChild(title);
|
||||||
return;
|
header.appendChild(thinkingStatusSpan);
|
||||||
|
header.appendChild(thinkingToggleSpan);
|
||||||
|
|
||||||
|
box.appendChild(header);
|
||||||
|
box.appendChild(thinkingContentDiv);
|
||||||
|
msgDiv.appendChild(box);
|
||||||
|
};
|
||||||
|
|
||||||
|
const ensureAnswerBox = () => {
|
||||||
|
if (answerContentDiv) return;
|
||||||
|
|
||||||
|
const box = document.createElement("answer-box");
|
||||||
|
answerContentDiv = document.createElement("div");
|
||||||
|
answerContentDiv.className = "answer-content";
|
||||||
|
box.appendChild(answerContentDiv);
|
||||||
|
msgDiv.appendChild(box);
|
||||||
|
};
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// 请求参数
|
||||||
|
// =========================
|
||||||
|
sessId = sendParam.sessionId;
|
||||||
|
sendParam.imageBase64 = img;
|
||||||
|
sendParam.userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
|
||||||
|
sendParam.requestId = uuid();
|
||||||
|
|
||||||
|
controller = new AbortController();
|
||||||
|
|
||||||
|
let res;
|
||||||
|
try {
|
||||||
|
res = await fetch("https://serviceai.satabot.com/analyze", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(sendParam),
|
||||||
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
loadingDiv.textContent = "请求中断";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!res?.body) {
|
||||||
|
loadingDiv.textContent = "无响应";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// SSE 读取
|
||||||
|
// =========================
|
||||||
|
const reader = res.body.getReader();
|
||||||
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
let buffer = "";
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
|
||||||
|
buffer += decoder.decode(value, { stream: true });
|
||||||
|
const lines = buffer.split("\n");
|
||||||
|
buffer = lines.pop();
|
||||||
|
|
||||||
|
for (let line of lines) {
|
||||||
|
line = line.trim();
|
||||||
|
if (!line.startsWith("data:")) continue;
|
||||||
|
|
||||||
|
const data = line.slice(5).trim();
|
||||||
|
if (data === "[DONE]") {
|
||||||
|
scrollToBottom();
|
||||||
|
controller = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let obj;
|
||||||
|
try {
|
||||||
|
obj = JSON.parse(data);
|
||||||
|
} catch {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// phase
|
||||||
|
// =========================
|
||||||
|
if (obj.type === "phase") {
|
||||||
|
currentPhase = obj.content;
|
||||||
|
|
||||||
|
if (currentPhase === "thinking") {
|
||||||
|
ensureThinkingBox();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (currentPhase === "answer") {
|
||||||
sendParam.imageBase64 = '';
|
thinkingFinished = true;
|
||||||
|
if (thinkingStatusSpan) {
|
||||||
reader = res.body.getReader();
|
thinkingStatusSpan.textContent = "已完成思考";
|
||||||
const decoder = new TextDecoder("utf-8");
|
thinkingStatusSpan.classList.remove("thinking-running");
|
||||||
let buffer = "";
|
thinkingStatusSpan.classList.add("thinking-done");
|
||||||
msgDiv.classList.remove("thinking");
|
|
||||||
showPop(userId);
|
|
||||||
clearInterval(interval);
|
|
||||||
console.log(isStopped, '是否点击停止')
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
console.log("回答")
|
|
||||||
let result;
|
|
||||||
try {
|
|
||||||
result = await reader.read();
|
|
||||||
} catch (e) {
|
|
||||||
console.log("流已中断:", e);
|
|
||||||
break; // ← 必须退出循环
|
|
||||||
}
|
|
||||||
|
|
||||||
const { done, value } = result;
|
|
||||||
if (done) {
|
|
||||||
controller = null;
|
|
||||||
|
|
||||||
sendBtnImg.classList.add('none');
|
|
||||||
stopBtn.forEach(e => {
|
|
||||||
e.classList.remove('none')
|
|
||||||
|
|
||||||
})
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
buffer += decoder.decode(value, { stream: true });
|
|
||||||
|
|
||||||
let lines = buffer.split("\n");
|
|
||||||
buffer = lines.pop();
|
|
||||||
|
|
||||||
for (let line of lines) {
|
|
||||||
line = line.trim();
|
|
||||||
if (!line.startsWith("data:")) continue;
|
|
||||||
|
|
||||||
const data = line.slice(5).trim();
|
|
||||||
if (data == "[DONE]") {
|
|
||||||
console.log("完成回答");
|
|
||||||
controller = null;
|
|
||||||
// 更新聊天滚动条
|
|
||||||
|
|
||||||
sendBtnImg.classList.remove('none');
|
|
||||||
stopBtn.forEach(e => {
|
|
||||||
e.classList.add('none')
|
|
||||||
|
|
||||||
})
|
|
||||||
// renderChatList();
|
|
||||||
msgDiv.innerHTML = marked.parse(aiMsg.content);
|
|
||||||
// const shareSpan = document.createElement("span");
|
|
||||||
// shareSpan.style.display = "inline-flex";
|
|
||||||
// shareSpan.style.alignItems = "center";
|
|
||||||
// shareSpan.style.cursor = "pointer";
|
|
||||||
// shareSpan.style.marginTop = "8px";
|
|
||||||
// shareSpan.style.opacity = "0.75";
|
|
||||||
// shareSpan.style.transition = "opacity .2s";
|
|
||||||
// shareSpan.title = "分享";
|
|
||||||
|
|
||||||
// shareSpan.onmouseenter = () => shareSpan.style.opacity = "1";
|
|
||||||
// shareSpan.onmouseleave = () => shareSpan.style.opacity = "0.75";
|
|
||||||
|
|
||||||
// shareSpan.innerHTML = `
|
|
||||||
// <svg t="1764059839985" class="icon" viewBox="0 0 1236 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5667" width="20" height="20"><path d="M741.743 1018.343c-28.287 0-50.917-11.315-73.547-28.288-22.63-22.63-39.602-50.917-39.602-84.862V792.044c-124.464 0-328.133 33.945-435.624 181.039-16.973 28.287-56.575 45.26-90.52 50.917H85.478C28.903 1012.685-5.042 961.768 0.616 905.193c28.287-243.27 113.15-418.652 260.243-537.458 107.492-84.862 231.956-130.122 367.735-141.437V118.807c0-50.917 22.63-96.177 67.89-113.15C736.086-5.657 781.345 0 815.29 33.945l362.077 367.735c28.288 22.63 45.26 56.574 50.918 96.176 5.657 39.603-5.658 79.205-33.945 107.492-5.658 5.658-11.315 16.972-22.63 22.63l-350.762 356.42c-22.63 22.63-50.918 33.945-79.205 33.945z m-90.52-339.448h90.52v226.298l356.42-367.734 5.658-5.658c5.657-5.657 5.657-16.972 5.657-22.63 0-11.315-5.657-16.972-11.315-22.63l-5.657-5.657-356.42-362.077V333.79l-79.205 5.658c-118.806 0-231.956 39.602-328.132 113.149-113.15 90.519-186.696 237.613-209.326 429.967 141.436-175.382 390.364-203.669 531.8-203.669z" p-id="5668"></path></svg>
|
|
||||||
// `;
|
|
||||||
|
|
||||||
// shareSpan.onclick = () => {
|
|
||||||
// generatePoster(aiMsg.content);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// if (role === "assistant") {
|
|
||||||
// 分享按钮
|
|
||||||
const shareSpan = document.createElement("span");
|
|
||||||
shareSpan.style.display = "inline-flex";
|
|
||||||
shareSpan.style.alignItems = "center";
|
|
||||||
shareSpan.style.cursor = "pointer";
|
|
||||||
shareSpan.style.marginTop = "8px";
|
|
||||||
shareSpan.style.opacity = "0.75";
|
|
||||||
shareSpan.style.transition = "opacity .2s";
|
|
||||||
shareSpan.title = "分享";
|
|
||||||
|
|
||||||
shareSpan.onmouseenter = () => shareSpan.style.opacity = "1";
|
|
||||||
shareSpan.onmouseleave = () => shareSpan.style.opacity = "0.75";
|
|
||||||
|
|
||||||
shareSpan.innerHTML = `
|
|
||||||
<svg t="1764059839985" class="icon" viewBox="0 0 1236 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5667" width="20" height="20"><path d="M741.743 1018.343c-28.287 0-50.917-11.315-73.547-28.288-22.63-22.63-39.602-50.917-39.602-84.862V792.044c-124.464 0-328.133 33.945-435.624 181.039-16.973 28.287-56.575 45.26-90.52 50.917H85.478C28.903 1012.685-5.042 961.768 0.616 905.193c28.287-243.27 113.15-418.652 260.243-537.458 107.492-84.862 231.956-130.122 367.735-141.437V118.807c0-50.917 22.63-96.177 67.89-113.15C736.086-5.657 781.345 0 815.29 33.945l362.077 367.735c28.288 22.63 45.26 56.574 50.918 96.176 5.657 39.603-5.658 79.205-33.945 107.492-5.658 5.658-11.315 16.972-22.63 22.63l-350.762 356.42c-22.63 22.63-50.918 33.945-79.205 33.945z m-90.52-339.448h90.52v226.298l356.42-367.734 5.658-5.658c5.657-5.657 5.657-16.972 5.657-22.63 0-11.315-5.657-16.972-11.315-22.63l-5.657-5.657-356.42-362.077V333.79l-79.205 5.658c-118.806 0-231.956 39.602-328.132 113.149-113.15 90.519-186.696 237.613-209.326 429.967 141.436-175.382 390.364-203.669 531.8-203.669z" p-id="5668" fill="#878895"></path></svg>
|
|
||||||
`;
|
|
||||||
|
|
||||||
|
|
||||||
shareSpan.onclick = () => {
|
|
||||||
generatePoster(content, imgUrl);
|
|
||||||
};
|
|
||||||
|
|
||||||
// msg.appendChild(shareSpan);
|
|
||||||
|
|
||||||
// ======================
|
|
||||||
// ✅ 复制按钮(新增)
|
|
||||||
// ======================
|
|
||||||
const copySpan = document.createElement("span");
|
|
||||||
copySpan.style.display = "inline-flex";
|
|
||||||
copySpan.style.alignItems = "center";
|
|
||||||
copySpan.style.cursor = "pointer";
|
|
||||||
copySpan.style.marginTop = "8px";
|
|
||||||
copySpan.style.marginLeft = "10px";
|
|
||||||
copySpan.style.opacity = "0.75";
|
|
||||||
copySpan.style.transition = "opacity .2s";
|
|
||||||
copySpan.title = "复制内容";
|
|
||||||
|
|
||||||
copySpan.onmouseenter = () => copySpan.style.opacity = "1";
|
|
||||||
copySpan.onmouseleave = () => copySpan.style.opacity = "0.75";
|
|
||||||
|
|
||||||
copySpan.innerHTML = `
|
|
||||||
<svg t="1764549398754" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5335" width="20" height="20"><path d="M639.9 256.3H192.4c-35.2 0-63.9 28.8-63.9 63.9v575.4c0 35.2 28.8 63.9 63.9 63.9h447.5c35.2 0 63.9-28.8 63.9-63.9V320.2c0-35.1-28.8-63.9-63.9-63.9z m0 639.3H192.4V320.2h447.5v575.4z" fill="#878895" p-id="5336"></path><path d="M831.6 64.5H384.1c-35.2 0-63.9 28.8-63.9 63.9v63.9h63.9v-63.9h447.5v575.4h-63.9v63.9h63.9c35.2 0 63.9-28.8 63.9-63.9V128.4c0.1-35.1-28.7-63.9-63.9-63.9z" fill="#878895" p-id="5337"></path></svg>
|
|
||||||
`;
|
|
||||||
|
|
||||||
copySpan.onclick = async () => {
|
|
||||||
try {
|
|
||||||
await navigator.clipboard.writeText(content);
|
|
||||||
copySpan.style.opacity = "1";
|
|
||||||
showToast("复制成功")
|
|
||||||
copySpan.title = "复制成功";
|
|
||||||
setTimeout(() => { copySpan.title = "复制内容"; }, 1200);
|
|
||||||
} catch (err) {
|
|
||||||
alert("复制失败,可能不支持 HTTPS 环境以外复制。");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
msgDiv.appendChild(shareSpan);
|
|
||||||
msgDiv.appendChild(copySpan);
|
|
||||||
}, 1000)
|
|
||||||
|
|
||||||
|
|
||||||
console.log("shareSpan appended?", msgDiv.contains(shareSpan));
|
|
||||||
|
|
||||||
|
|
||||||
scrollToBottom();
|
|
||||||
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const obj = JSON.parse(data);
|
|
||||||
// if (obj.content && obj.type != 'title') {
|
|
||||||
// // 每次追加内容到 AI 消息
|
|
||||||
// aiMsg.content += obj.content;
|
|
||||||
// msgDiv.innerHTML = marked.parse(aiMsg.content);
|
|
||||||
// // msgDiv.textContent = aiMsg.content;
|
|
||||||
// chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
||||||
// }
|
|
||||||
if (obj.content && obj.type !== "title") {
|
|
||||||
for (const char of obj.content) {
|
|
||||||
await new Promise(r => setTimeout(r, 5)); // 控制字速
|
|
||||||
if (isStopped) return;
|
|
||||||
aiMsg.content += char;
|
|
||||||
smartRender(msgDiv, aiMsg.content);
|
|
||||||
|
|
||||||
// msgDiv.insertAdjacentText("beforeend", char);
|
|
||||||
|
|
||||||
// msgDiv.innerHTML = marked.parse(aiMsg.content);
|
|
||||||
|
|
||||||
// chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
||||||
// setTimeout(scrollToBottom, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (obj.content && obj.type == 'title') {
|
|
||||||
const chatItem = document.querySelector(`.chat-item[data-sessionid="${sessId}"]`);
|
|
||||||
if (chatItem) {
|
|
||||||
const chatTitle = chatItem.querySelector('.chat-title');
|
|
||||||
if (chatTitle) {
|
|
||||||
chatTitle.textContent = obj.content; // 设置标题
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
console.error("JSON parse error", data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
ensureAnswerBox();
|
||||||
}
|
}
|
||||||
};
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// thinking 内容(不依赖 phase)
|
||||||
|
// =========================
|
||||||
|
if (obj.type === "thinking" && obj.content) {
|
||||||
|
ensureThinkingBox();
|
||||||
|
thinkingBuffer += obj.content;
|
||||||
|
thinkingContentDiv.innerHTML = marked.parse(thinkingBuffer);
|
||||||
|
scrollToBottom();
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// answer 内容
|
||||||
|
// =========================
|
||||||
|
if (obj.type === "answer" && obj.content) {
|
||||||
|
ensureAnswerBox();
|
||||||
|
answerBuffer += obj.content;
|
||||||
|
answerContentDiv.innerHTML = marked.parse(answerBuffer);
|
||||||
|
scrollToBottom();
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// 标题
|
||||||
|
// =========================
|
||||||
|
if (obj.type === "title" && obj.content) {
|
||||||
|
const chatItem = document.querySelector(
|
||||||
|
`.chat-item[data-sessionid="${sessId}"]`
|
||||||
|
);
|
||||||
|
if (chatItem) {
|
||||||
|
const chatTitle = chatItem.querySelector(".chat-title");
|
||||||
|
if (chatTitle) chatTitle.textContent = obj.content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function newChat() {
|
function newChat() {
|
||||||
|
|||||||
@@ -1436,7 +1436,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
|
|||||||
console.log(existingTitle, rows?.[0], 9999, rows);
|
console.log(existingTitle, rows?.[0], 9999, rows);
|
||||||
|
|
||||||
const sessId = sessionId || Date.now().toString();
|
const sessId = sessionId || Date.now().toString();
|
||||||
|
const MAX_HISTORY = 6;
|
||||||
// ------------------ 获取历史对话(上下文) ------------------
|
// ------------------ 获取历史对话(上下文) ------------------
|
||||||
const [historyRows] = await pool.query(
|
const [historyRows] = await pool.query(
|
||||||
"SELECT role, content, image_base64 FROM ai_chat_messages WHERE session_id = ? AND user_id = ? ORDER BY created_at ASC",
|
"SELECT role, content, image_base64 FROM ai_chat_messages WHERE session_id = ? AND user_id = ? ORDER BY created_at ASC",
|
||||||
@@ -1444,7 +1444,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// 构建历史消息数组
|
// 构建历史消息数组
|
||||||
const historyMessages = historyRows.map(row => {
|
const historyMessages = historyRows.slice(-MAX_HISTORY).map(row => {
|
||||||
const message = {
|
const message = {
|
||||||
role: row.role,
|
role: row.role,
|
||||||
content: []
|
content: []
|
||||||
@@ -1532,7 +1532,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
const completion = await client.chat.completions.create({
|
const completion = await client.chat.completions.create({
|
||||||
model: "gpt-4.1",
|
model: "gpt-4.1-mini",
|
||||||
stream: true,
|
stream: true,
|
||||||
messages: imageAnalysisMessages
|
messages: imageAnalysisMessages
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user