Compare commits

...

4 Commits

2 changed files with 51 additions and 17 deletions

View File

@@ -878,12 +878,17 @@ async function streamGenerate(userMsgIndex) {
if (thinkingEl) {
thinkingEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].thinking) + '<span class="streaming-cursor">▌</span>';
}
// 确保思考块展开
const thinkingBlock = lastMessageEl.querySelector('.thinking-block');
if (thinkingBlock && !thinkingBlock.classList.contains('expanded')) {
thinkingBlock.classList.add('expanded');
}
scrollToBottom();
}
// 处理正式回复内容
if (delta.content) {
// 如果开启深度思考且开始输出正式内容,折叠思考块
// 如果开启深度思考且开始输出正式内容,说明思考完成,立即折叠思考块
if (enableThinking && !thinkingOutputStarted && currentConversation.messages[aiMessageIndex].thinking) {
thinkingOutputStarted = true;
// 折叠思考内容
@@ -922,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();
}
}
@@ -997,6 +1003,8 @@ 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)}`
@@ -1019,20 +1027,41 @@ ${conversationText}`;
})
});
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 <= 20) {
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);
@@ -1160,15 +1189,20 @@ function renderMessages() {
// 思考内容块仅AI消息
let thinkingHtml = '';
if (!isUser && msg.thinking) {
if (!isUser && 'thinking' in msg) {
// 判断是否是当前正在生成的消息有thinking字段且正在加载
const isGenerating = index === currentConversation.messages.length - 1 && isLoading && enableThinking;
// 思考进行中且没有正式内容时展开
const expandedClass = isGenerating && !msg.content ? 'expanded' : '';
thinkingHtml = `
<div class="thinking-block" onclick="toggleThinking(this)">
<div class="thinking-block ${expandedClass}" onclick="toggleThinking(this)">
<div class="thinking-header">
<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/></svg>
<span>思考过程</span>
<svg class="thinking-arrow" viewBox="0 0 24 24" width="14" height="14"><path fill="currentColor" d="M7 10l5 5 5-5z"/></svg>
</div>
<div class="thinking-content">${renderMarkdown(msg.thinking)}</div>
<div class="thinking-content">${renderMarkdown(msg.thinking || '思考中...')}</div>
</div>`;
}

View File

@@ -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.1">
<link rel="stylesheet" href="style.css?v=2.7.5">
<link rel="manifest" href="manifest.json">
</head>
<body>
<div id="app"></div>
<script src="marked.min.js?v=2.7.1"></script>
<script src="app.js?v=2.7.1"></script>
<script src="marked.min.js?v=2.7.5"></script>
<script src="app.js?v=2.7.5"></script>
</body>
</html>