fix: 标题生成添加详细日志和改进错误处理

This commit is contained in:
2026-04-26 22:33:25 +08:00
parent 8451f04302
commit 610ee8f409
2 changed files with 35 additions and 12 deletions

View File

@@ -1003,6 +1003,8 @@ function hideStopGenerateBtn() {
async function generateConversationTitle() { async function generateConversationTitle() {
if (!currentConversation) return; if (!currentConversation) return;
console.log('开始生成标题,当前消息数:', currentConversation.messages.length);
// 构建对话摘要 // 构建对话摘要
const conversationText = currentConversation.messages.map(m => const conversationText = currentConversation.messages.map(m =>
`${m.role === 'user' ? '用户' : 'AI'}: ${m.content.slice(0, 200)}` `${m.role === 'user' ? '用户' : 'AI'}: ${m.content.slice(0, 200)}`
@@ -1025,20 +1027,41 @@ ${conversationText}`;
}) })
}); });
console.log('标题API响应状态:', response.status);
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();
console.log('标题API响应数据:', data);
const newTitle = data.choices?.[0]?.message?.content?.trim(); const newTitle = data.choices?.[0]?.message?.content?.trim();
if (newTitle && newTitle.length > 0 && newTitle.length <= 15) { console.log('生成的标题:', newTitle);
currentConversation.title = newTitle;
currentConversation.updatedAt = Date.now(); if (newTitle && newTitle.length > 0) {
saveConversations(); // 去掉可能的引号和多余符号
const cleanTitle = newTitle.replace(/^["'"']+|["'"']+$/g, '').trim();
// 更新页面标题显示 if (cleanTitle.length > 0 && cleanTitle.length <= 20) {
const titleEl = document.querySelector('.header h1'); currentConversation.title = cleanTitle;
if (titleEl) { currentConversation.updatedAt = Date.now();
titleEl.textContent = newTitle; 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) { } catch (error) {
console.error('生成标题失败:', error); console.error('生成标题失败:', error);

View File

@@ -8,12 +8,12 @@
<meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0"> <meta http-equiv="Expires" content="0">
<title>AI助手</title> <title>AI助手</title>
<link rel="stylesheet" href="style.css?v=2.7.4"> <link rel="stylesheet" href="style.css?v=2.7.5">
<link rel="manifest" href="manifest.json"> <link rel="manifest" href="manifest.json">
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
<script src="marked.min.js?v=2.7.4"></script> <script src="marked.min.js?v=2.7.5"></script>
<script src="app.js?v=2.7.4"></script> <script src="app.js?v=2.7.5"></script>
</body> </body>
</html> </html>