Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 44cca59ec6 | |||
| 0dfa76dad6 | |||
| 5a6c0c41b3 | |||
| 2285755b34 |
143
www/app.js
143
www/app.js
@@ -13,6 +13,10 @@ let conversations = []; // 对话列表
|
|||||||
let currentConversation = null; // 当前对话
|
let currentConversation = null; // 当前对话
|
||||||
let isLoading = false;
|
let isLoading = false;
|
||||||
|
|
||||||
|
// 功能开关
|
||||||
|
let enableThinking = false; // 深度思考
|
||||||
|
let enableSearch = false; // 联网搜索
|
||||||
|
|
||||||
// DOM 元素(初始为 null,在 openConversation 时重新获取)
|
// DOM 元素(初始为 null,在 openConversation 时重新获取)
|
||||||
let appContainer = null;
|
let appContainer = null;
|
||||||
let messagesContainer = null;
|
let messagesContainer = null;
|
||||||
@@ -20,6 +24,8 @@ let messagesDiv = null;
|
|||||||
let userInput = null;
|
let userInput = null;
|
||||||
let sendBtn = null;
|
let sendBtn = null;
|
||||||
let welcome = null;
|
let welcome = null;
|
||||||
|
let thinkingBtn = null;
|
||||||
|
let searchBtn = null;
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
@@ -171,6 +177,18 @@ function openConversation(id) {
|
|||||||
<div class="messages" id="messages"></div>
|
<div class="messages" id="messages"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- 功能开关栏 -->
|
||||||
|
<div class="feature-bar">
|
||||||
|
<button class="feature-btn thinking-btn ${enableThinking ? 'active' : ''}" id="thinkingBtn">
|
||||||
|
<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>
|
||||||
|
</button>
|
||||||
|
<button class="feature-btn search-btn ${enableSearch ? 'active' : ''}" id="searchBtn">
|
||||||
|
<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 7 9.5 7 14 9.01 14 9.5 11.99 14 9.5 14z"/></svg>
|
||||||
|
<span>联网搜索</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="input-area">
|
<div class="input-area">
|
||||||
<button class="attach-btn" id="attachBtn" title="上传文件">
|
<button class="attach-btn" id="attachBtn" title="上传文件">
|
||||||
<svg viewBox="0 0 24 24" width="24" height="24"><path fill="currentColor" d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/></svg>
|
<svg viewBox="0 0 24 24" width="24" height="24"><path fill="currentColor" d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/></svg>
|
||||||
@@ -211,6 +229,8 @@ function openConversation(id) {
|
|||||||
userInput = document.getElementById('userInput');
|
userInput = document.getElementById('userInput');
|
||||||
sendBtn = document.getElementById('sendBtn');
|
sendBtn = document.getElementById('sendBtn');
|
||||||
welcome = document.getElementById('welcome');
|
welcome = document.getElementById('welcome');
|
||||||
|
thinkingBtn = document.getElementById('thinkingBtn');
|
||||||
|
searchBtn = document.getElementById('searchBtn');
|
||||||
|
|
||||||
// 绑定按钮事件
|
// 绑定按钮事件
|
||||||
const backBtn = document.getElementById('backBtn');
|
const backBtn = document.getElementById('backBtn');
|
||||||
@@ -219,6 +239,31 @@ function openConversation(id) {
|
|||||||
const clearBtn = document.getElementById('clearBtn');
|
const clearBtn = document.getElementById('clearBtn');
|
||||||
if (clearBtn) clearBtn.addEventListener('click', clearCurrentChat);
|
if (clearBtn) clearBtn.addEventListener('click', clearCurrentChat);
|
||||||
|
|
||||||
|
// 绑定功能开关按钮事件
|
||||||
|
if (thinkingBtn) {
|
||||||
|
thinkingBtn.addEventListener('click', () => {
|
||||||
|
enableThinking = !enableThinking;
|
||||||
|
thinkingBtn.classList.toggle('active', enableThinking);
|
||||||
|
// 如果开启深度思考,关闭联网搜索(智谱思考模型不支持联网)
|
||||||
|
if (enableThinking && enableSearch) {
|
||||||
|
enableSearch = false;
|
||||||
|
searchBtn.classList.remove('active');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (searchBtn) {
|
||||||
|
searchBtn.addEventListener('click', () => {
|
||||||
|
enableSearch = !enableSearch;
|
||||||
|
searchBtn.classList.toggle('active', enableSearch);
|
||||||
|
// 如果开启联网搜索,关闭深度思考
|
||||||
|
if (enableSearch && enableThinking) {
|
||||||
|
enableThinking = false;
|
||||||
|
thinkingBtn.classList.remove('active');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 绑定输入事件
|
// 绑定输入事件
|
||||||
userInput.addEventListener('keydown', handleKeyDown);
|
userInput.addEventListener('keydown', handleKeyDown);
|
||||||
userInput.addEventListener('input', (e) => autoResize(e.target));
|
userInput.addEventListener('input', (e) => autoResize(e.target));
|
||||||
@@ -349,29 +394,50 @@ async function streamGenerate(userMsgIndex) {
|
|||||||
sendBtn.disabled = true;
|
sendBtn.disabled = true;
|
||||||
|
|
||||||
const aiMessageIndex = currentConversation.messages.length;
|
const aiMessageIndex = currentConversation.messages.length;
|
||||||
currentConversation.messages.push({ role: 'assistant', content: '' });
|
|
||||||
|
// 只有开启深度思考时才添加 thinking 字段
|
||||||
|
currentConversation.messages.push({
|
||||||
|
role: 'assistant',
|
||||||
|
content: '',
|
||||||
|
...(enableThinking ? { thinking: '' } : {})
|
||||||
|
});
|
||||||
renderMessages();
|
renderMessages();
|
||||||
|
|
||||||
const lastMessageEl = messagesDiv.lastElementChild;
|
const lastMessageEl = messagesDiv.lastElementChild;
|
||||||
const contentEl = lastMessageEl.querySelector('.message-content');
|
const contentEl = lastMessageEl.querySelector('.message-content');
|
||||||
|
const thinkingEl = lastMessageEl.querySelector('.thinking-content');
|
||||||
|
|
||||||
|
// 深度思考模式:思考块默认展开
|
||||||
|
if (enableThinking && thinkingEl) {
|
||||||
|
const thinkingBlock = lastMessageEl.querySelector('.thinking-block');
|
||||||
|
if (thinkingBlock) thinkingBlock.classList.add('expanded');
|
||||||
|
thinkingEl.innerHTML = '<span class="streaming-cursor">思考中...</span>';
|
||||||
|
}
|
||||||
|
|
||||||
contentEl.innerHTML = '<span class="streaming-cursor">▌</span>';
|
contentEl.innerHTML = '<span class="streaming-cursor">▌</span>';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 构建请求体 - 统一使用 glm-4.5-air,通过 thinking 参数控制
|
||||||
|
const requestBody = {
|
||||||
|
model: CONFIG.model,
|
||||||
|
messages: currentConversation.messages.slice(0, aiMessageIndex).map(m => ({
|
||||||
|
role: m.role,
|
||||||
|
content: m.content
|
||||||
|
})),
|
||||||
|
max_tokens: CONFIG.maxTokens,
|
||||||
|
stream: true,
|
||||||
|
thinking: {
|
||||||
|
type: enableThinking ? 'enabled' : 'disabled'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const response = await fetch(CONFIG.apiUrl, {
|
const response = await fetch(CONFIG.apiUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `Bearer ${CONFIG.apiKey}`
|
'Authorization': `Bearer ${CONFIG.apiKey}`
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify(requestBody)
|
||||||
model: CONFIG.model,
|
|
||||||
messages: currentConversation.messages.slice(0, aiMessageIndex).map(m => ({
|
|
||||||
role: m.role,
|
|
||||||
content: m.content
|
|
||||||
})),
|
|
||||||
max_tokens: CONFIG.maxTokens,
|
|
||||||
stream: true
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -381,6 +447,7 @@ async function streamGenerate(userMsgIndex) {
|
|||||||
const reader = response.body.getReader();
|
const reader = response.body.getReader();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let buffer = '';
|
let buffer = '';
|
||||||
|
let thinkingOutputStarted = false; // 正式内容是否开始输出
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
@@ -397,16 +464,44 @@ async function streamGenerate(userMsgIndex) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(jsonStr);
|
const data = JSON.parse(jsonStr);
|
||||||
if (data.choices && data.choices[0]?.delta?.content) {
|
const delta = data.choices?.[0]?.delta;
|
||||||
currentConversation.messages[aiMessageIndex].content += data.choices[0].delta.content;
|
|
||||||
contentEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].content) + '<span class="streaming-cursor">▌</span>';
|
if (delta) {
|
||||||
scrollToBottom();
|
// 只有开启深度思考时才处理思考内容
|
||||||
|
if (enableThinking && (delta.reasoning_content || delta.thinking)) {
|
||||||
|
const thinkingChunk = delta.reasoning_content || delta.thinking;
|
||||||
|
currentConversation.messages[aiMessageIndex].thinking += thinkingChunk;
|
||||||
|
if (thinkingEl) {
|
||||||
|
thinkingEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].thinking) + '<span class="streaming-cursor">▌</span>';
|
||||||
|
}
|
||||||
|
scrollToBottom();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理正式回复内容
|
||||||
|
if (delta.content) {
|
||||||
|
// 如果开启深度思考且开始输出正式内容,折叠思考块
|
||||||
|
if (enableThinking && !thinkingOutputStarted && currentConversation.messages[aiMessageIndex].thinking) {
|
||||||
|
thinkingOutputStarted = true;
|
||||||
|
// 折叠思考内容
|
||||||
|
const thinkingBlock = lastMessageEl.querySelector('.thinking-block');
|
||||||
|
if (thinkingBlock) thinkingBlock.classList.remove('expanded');
|
||||||
|
if (thinkingEl) thinkingEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].thinking);
|
||||||
|
}
|
||||||
|
|
||||||
|
currentConversation.messages[aiMessageIndex].content += delta.content;
|
||||||
|
contentEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].content) + '<span class="streaming-cursor">▌</span>';
|
||||||
|
scrollToBottom();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 最终渲染
|
||||||
|
if (thinkingEl && enableThinking && currentConversation.messages[aiMessageIndex].thinking) {
|
||||||
|
thinkingEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].thinking);
|
||||||
|
}
|
||||||
contentEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].content);
|
contentEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].content);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -541,6 +636,20 @@ function renderMessages() {
|
|||||||
contentHtml = renderMarkdown(msg.content);
|
contentHtml = renderMarkdown(msg.content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 思考内容块(仅AI消息)
|
||||||
|
let thinkingHtml = '';
|
||||||
|
if (!isUser && msg.thinking) {
|
||||||
|
thinkingHtml = `
|
||||||
|
<div class="thinking-block" 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>`;
|
||||||
|
}
|
||||||
|
|
||||||
const copyIcon = `<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>`;
|
const copyIcon = `<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>`;
|
||||||
|
|
||||||
const actions = isUser
|
const actions = isUser
|
||||||
@@ -564,6 +673,7 @@ function renderMessages() {
|
|||||||
<div class="message ${msg.role}" data-index="${index}">
|
<div class="message ${msg.role}" data-index="${index}">
|
||||||
<div class="message-avatar">${avatar}</div>
|
<div class="message-avatar">${avatar}</div>
|
||||||
<div class="message-body">
|
<div class="message-body">
|
||||||
|
${thinkingHtml}
|
||||||
<div class="message-content">${contentHtml}</div>
|
<div class="message-content">${contentHtml}</div>
|
||||||
${actions}
|
${actions}
|
||||||
</div>
|
</div>
|
||||||
@@ -585,6 +695,11 @@ function renderMessages() {
|
|||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 折叠/展开思考内容
|
||||||
|
function toggleThinking(block) {
|
||||||
|
block.classList.toggle('expanded');
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== 工具函数 ====================
|
// ==================== 工具函数 ====================
|
||||||
|
|
||||||
// 渲染 Markdown
|
// 渲染 Markdown
|
||||||
|
|||||||
@@ -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.1.2">
|
<link rel="stylesheet" href="style.css?v=2.2.3">
|
||||||
<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.1.2"></script>
|
<script src="marked.min.js?v=2.2.3"></script>
|
||||||
<script src="app.js?v=2.1.2"></script>
|
<script src="app.js?v=2.2.3"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -582,6 +582,102 @@ body {
|
|||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 功能开关栏 */
|
||||||
|
.feature-bar {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 8px 16px;
|
||||||
|
background: rgba(255,255,255,0.95);
|
||||||
|
border-top: 1px solid var(--border-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.feature-btn {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 4px;
|
||||||
|
padding: 6px 12px;
|
||||||
|
background: white;
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 20px;
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--text-light);
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.feature-btn:hover {
|
||||||
|
border-color: var(--primary);
|
||||||
|
color: var(--primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.feature-btn.active {
|
||||||
|
background: linear-gradient(135deg, var(--primary) 0%, #764ba2 100%);
|
||||||
|
border-color: transparent;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.feature-btn svg {
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 思考内容块 */
|
||||||
|
.thinking-block {
|
||||||
|
margin-bottom: 12px;
|
||||||
|
background: rgba(102, 126, 234, 0.08);
|
||||||
|
border: 1px solid rgba(102, 126, 234, 0.2);
|
||||||
|
border-radius: 10px;
|
||||||
|
overflow: hidden;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-block:hover {
|
||||||
|
border-color: rgba(102, 126, 234, 0.4);
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 8px 12px;
|
||||||
|
background: rgba(102, 126, 234, 0.12);
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--primary);
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-header svg:first-child {
|
||||||
|
color: var(--primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-arrow {
|
||||||
|
margin-left: auto;
|
||||||
|
transition: transform 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-block.expanded .thinking-arrow {
|
||||||
|
transform: rotate(180deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-content {
|
||||||
|
padding: 0 12px;
|
||||||
|
max-height: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
transition: max-height 0.3s ease, padding 0.3s ease;
|
||||||
|
color: var(--text-light);
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 1.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-block.expanded .thinking-content {
|
||||||
|
padding: 12px;
|
||||||
|
max-height: 500px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thinking-content p {
|
||||||
|
margin: 8px 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* 输入区域 */
|
/* 输入区域 */
|
||||||
.input-area {
|
.input-area {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
Reference in New Issue
Block a user