From 7977057be553827000ad487d20b03cdb89a6e297 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Sun, 26 Apr 2026 16:59:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E6=80=BB=E7=BB=93?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D=E6=A0=87=E9=A2=98=20-=20=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E5=92=8C=E6=AF=8F=E9=9A=945=E6=AC=A1=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- www/app.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ www/index.html | 6 +++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/www/app.js b/www/app.js index ccd5301..d8520c0 100644 --- a/www/app.js +++ b/www/app.js @@ -514,6 +514,58 @@ async function streamGenerate(userMsgIndex) { currentConversation.updatedAt = Date.now(); saveConversations(); renderMessages(); + + // 自动总结标题:第一次对话和每隔5次对话 + const totalMessages = currentConversation.messages.length; + if (totalMessages === 1 || totalMessages % 5 === 0) { + await generateConversationTitle(); + } + } +} + +// 生成对话标题 +async function generateConversationTitle() { + if (!currentConversation) return; + + // 构建对话摘要 + const conversationText = currentConversation.messages.map(m => + `${m.role === 'user' ? '用户' : 'AI'}: ${m.content.slice(0, 200)}` + ).join('\n'); + + const titlePrompt = `请用不超过10个字总结以下对话的主题,只输出标题,不要其他内容: +${conversationText}`; + + try { + const response = await fetch(CONFIG.apiUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${CONFIG.apiKey}` + }, + body: JSON.stringify({ + model: CONFIG.model, + messages: [{ role: 'user', content: titlePrompt }], + max_tokens: 50 + }) + }); + + if (response.ok) { + const data = await response.json(); + const newTitle = data.choices?.[0]?.message?.content?.trim(); + if (newTitle && newTitle.length > 0 && newTitle.length <= 15) { + currentConversation.title = newTitle; + currentConversation.updatedAt = Date.now(); + saveConversations(); + + // 更新页面标题显示 + const titleEl = document.querySelector('.header h1'); + if (titleEl) { + titleEl.textContent = newTitle; + } + } + } + } catch (error) { + console.error('生成标题失败:', error); } } diff --git a/www/index.html b/www/index.html index 11d4199..9fce4f5 100644 --- a/www/index.html +++ b/www/index.html @@ -8,12 +8,12 @@ AI助手 - +
- - + + \ No newline at end of file