diff --git a/templates/chat.html b/templates/chat.html index 6eb2e98..e303392 100644 --- a/templates/chat.html +++ b/templates/chat.html @@ -809,10 +809,6 @@ function resetNewConversation({ preserveInput = false } = {}) { async function newConversation() { if (pending) return; - if (getPresetPrompt()) { - await startPresetConversation(); - return; - } resetNewConversation(); } @@ -1060,53 +1056,11 @@ async function streamChat(messages, aiBubble) { return full; } -async function startPresetConversation({ preserveInput = false } = {}) { +function ensurePresetLoaded() { + if (currentConvId || history.length > 0) return; const preset = getPresetPrompt(); - if (!preset) { - resetNewConversation({ preserveInput }); - return; - } - - pending = true; - setInputDisabled(true); - const keepText = preserveInput ? inputBox.value : ''; - let aiBubble = null; - - try { - resetNewConversation({ preserveInput }); - inputBox.value = keepText; - autoResize(); - clearImage(); - - const conv = await createConversation(); - currentConvId = conv.id; - await loadConversationList(); - - history = [ - { role: 'system', content: preset, hidden: true }, - { - role: 'user', - content: '请根据以上预先提示词,用简短自然的方式输出开场白,然后等待用户开始聊天。', - hidden: true - } - ]; - - aiBubble = addAIBubble(); - const full = await streamChat(history, aiBubble); - history.push({ role: 'assistant', content: full }); - scrollToBottom(); - await loadConversationList(); - } catch (e) { - if (!aiBubble) aiBubble = addAIBubble(); - aiBubble.txt.innerHTML = `错误: ${escHtml(e.message)}`; - aiBubble.bub.classList.remove('typing-cursor'); - } finally { - pending = false; - setInputDisabled(false); - inputBox.value = keepText; - autoResize(); - inputBox.focus(); - } + if (!preset) return; + history = [{ role: 'system', content: preset, hidden: true }]; } // ── 发送消息 ────────────────────────────────────────────── @@ -1115,17 +1069,13 @@ async function sendMessage() { const userText = inputBox.value.trim(); if (!userText && !imageB64) return; - if (!currentConvId && history.length === 0 && getPresetPrompt()) { - await startPresetConversation({ preserveInput: true }); - return; - } - pending = true; setInputDisabled(true); let aiBubble = null; try { if (!currentConvId) { + ensurePresetLoaded(); const conv = await createConversation(); currentConvId = conv.id; await loadConversationList();