Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fb09560259 | |||
| 83bdab3205 | |||
| 610ee8f409 | |||
| 8451f04302 |
49
www/app.js
49
www/app.js
@@ -927,7 +927,8 @@ async function streamGenerate(userMsgIndex) {
|
||||
|
||||
// 自动总结标题:第一次对话和每隔5次对话
|
||||
const totalMessages = currentConversation.messages.length;
|
||||
if (totalMessages === 1 || totalMessages % 5 === 0) {
|
||||
// 第一次对话(用户+AI=2条)或每5次对话(10条)
|
||||
if (totalMessages === 2 || totalMessages % 10 === 0) {
|
||||
await generateConversationTitle();
|
||||
}
|
||||
}
|
||||
@@ -1002,12 +1003,14 @@ function hideStopGenerateBtn() {
|
||||
async function generateConversationTitle() {
|
||||
if (!currentConversation) return;
|
||||
|
||||
console.log('开始生成标题,当前消息数:', currentConversation.messages.length);
|
||||
|
||||
// 构建对话摘要
|
||||
const conversationText = currentConversation.messages.map(m =>
|
||||
`${m.role === 'user' ? '用户' : 'AI'}: ${m.content.slice(0, 200)}`
|
||||
).join('\n');
|
||||
|
||||
const titlePrompt = `请用不超过10个字总结以下对话的主题,只输出标题,不要其他内容:
|
||||
const titlePrompt = `请用不超过20个字总结以下对话的主题,只输出标题,不要其他内容:
|
||||
${conversationText}`;
|
||||
|
||||
try {
|
||||
@@ -1020,24 +1023,46 @@ ${conversationText}`;
|
||||
body: JSON.stringify({
|
||||
model: CONFIG.model,
|
||||
messages: [{ role: 'user', content: titlePrompt }],
|
||||
max_tokens: 50
|
||||
max_tokens: 100,
|
||||
thinking: { type: 'disabled' } // 禁用思考模式,直接输出标题
|
||||
})
|
||||
});
|
||||
|
||||
console.log('标题API响应状态:', response.status);
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
console.log('标题API响应数据:', data);
|
||||
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;
|
||||
console.log('生成的标题:', newTitle);
|
||||
|
||||
if (newTitle && newTitle.length > 0) {
|
||||
// 去掉可能的引号和多余符号
|
||||
const cleanTitle = newTitle.replace(/^["'"']+|["'"']+$/g, '').trim();
|
||||
if (cleanTitle.length > 0 && cleanTitle.length <= 30) {
|
||||
currentConversation.title = cleanTitle;
|
||||
currentConversation.updatedAt = Date.now();
|
||||
saveConversations();
|
||||
console.log('标题已保存:', cleanTitle);
|
||||
|
||||
// 更新页面标题显示
|
||||
const titleEl = document.querySelector('.header h1');
|
||||
if (titleEl) {
|
||||
titleEl.textContent = cleanTitle;
|
||||
}
|
||||
|
||||
// 更新侧边栏标题(如果在对话列表页面)
|
||||
const convItem = document.querySelector(`.conversation-item[data-id="${currentConversation.id}"] .conv-title`);
|
||||
if (convItem) {
|
||||
convItem.textContent = cleanTitle;
|
||||
}
|
||||
|
||||
showToast('标题已更新');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
console.error('标题API错误:', errorText);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('生成标题失败:', error);
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
<meta http-equiv="Pragma" content="no-cache">
|
||||
<meta http-equiv="Expires" content="0">
|
||||
<title>AI助手</title>
|
||||
<link rel="stylesheet" href="style.css?v=2.7.3">
|
||||
<link rel="stylesheet" href="style.css?v=2.7.7">
|
||||
<link rel="manifest" href="manifest.json">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script src="marked.min.js?v=2.7.3"></script>
|
||||
<script src="app.js?v=2.7.3"></script>
|
||||
<script src="marked.min.js?v=2.7.7"></script>
|
||||
<script src="app.js?v=2.7.7"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user