fix: 修复用户消息无法获得模型回复的问题
- 修复 TDZ 崩溃: conversationHistory 声明移到 IIFE 之前(API Key 模式) - 修复前端错误处理: DEV_ID 模式下后端返回错误时显示错误信息给用户 - 添加 "思考中" 状态指示: 用户发送消息后显示思考中提示 - 修复后端错误响应: 500 错误时也包含 reply 字段确保用户始终看到回复 - 添加知秋人格体 system prompt 到 API Key 模式对话 Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
This commit is contained in:
parent
b56083c6f4
commit
6063f7f8f4
|
|
@ -62,7 +62,8 @@ router.post('/message', async (req, res) => {
|
|||
res.status(500).json({
|
||||
error: true,
|
||||
code: 'CHAT_ERROR',
|
||||
message: '对话服务暂时不可用'
|
||||
message: '对话服务暂时不可用',
|
||||
reply: '抱歉,我现在遇到了一些技术问题,暂时无法正常回复。请稍后再试 🙏'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -104,9 +104,13 @@ async function sendMessage() {
|
|||
var sendBtn = document.getElementById('sendBtn');
|
||||
sendBtn.disabled = true;
|
||||
|
||||
// 显示"思考中"状态
|
||||
var thinkingEl = appendThinking();
|
||||
|
||||
try {
|
||||
if (LOGIN_MODE === 'apikey') {
|
||||
// API Key 模式:浏览器直连用户 API(无需后端代理)
|
||||
// API Key 模式:通过后端代理调用用户 API
|
||||
removeThinking(thinkingEl);
|
||||
await streamApiKeyReply(text);
|
||||
} else {
|
||||
// 开发编号模式:使用原有后端接口
|
||||
|
|
@ -120,11 +124,25 @@ async function sendMessage() {
|
|||
})
|
||||
});
|
||||
|
||||
var data = await res.json();
|
||||
removeThinking(thinkingEl);
|
||||
|
||||
var data;
|
||||
try {
|
||||
data = await res.json();
|
||||
} catch (_parseErr) {
|
||||
appendMessage('system', '服务器返回异常,请稍后再试');
|
||||
sendBtn.disabled = false;
|
||||
input.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.reply) {
|
||||
appendMessage('persona', data.reply);
|
||||
conversationHistory.push({ role: 'assistant', content: data.reply });
|
||||
} else if (data.error) {
|
||||
appendMessage('system', '⚠️ ' + (data.message || '对话服务暂时不可用'));
|
||||
} else {
|
||||
appendMessage('system', '未收到有效回复,请稍后再试');
|
||||
}
|
||||
|
||||
if (data.build_ready) {
|
||||
|
|
@ -133,7 +151,8 @@ async function sendMessage() {
|
|||
}
|
||||
}
|
||||
} catch (_err) {
|
||||
appendMessage('system', '消息发送失败,请稍后再试');
|
||||
removeThinking(thinkingEl);
|
||||
appendMessage('system', '消息发送失败,请检查网络连接后再试');
|
||||
}
|
||||
|
||||
sendBtn.disabled = false;
|
||||
|
|
@ -194,6 +213,23 @@ async function streamApiKeyReply(text) {
|
|||
}
|
||||
}
|
||||
|
||||
/* ---- 思考中状态 ---- */
|
||||
function appendThinking() {
|
||||
var chatBody = document.getElementById('chatBody');
|
||||
var msgDiv = document.createElement('div');
|
||||
msgDiv.className = 'message message-persona thinking';
|
||||
msgDiv.innerHTML = '<span class="avatar">🧠</span><div class="msg-content">思考中…</div>';
|
||||
chatBody.appendChild(msgDiv);
|
||||
chatBody.scrollTop = chatBody.scrollHeight;
|
||||
return msgDiv;
|
||||
}
|
||||
|
||||
function removeThinking(el) {
|
||||
if (el && el.parentNode) {
|
||||
el.parentNode.removeChild(el);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- 创建流式消息气泡 ---- */
|
||||
function appendStreamMessage() {
|
||||
var chatBody = document.getElementById('chatBody');
|
||||
|
|
|
|||
Loading…
Reference in New Issue