// AI助手 - 前端应用 // 使用智谱 GLM-4.5-Air 模型(流式输出 + 多对话管理) const CONFIG = { apiUrl: 'https://open.bigmodel.cn/api/paas/v4/chat/completions', apiKey: '2259e33a1357460abe17919aaf81e73d.K44a8LPQTmFM5PKm', model: 'glm-4.5-air', maxTokens: 2048 }; // 数据结构 let conversations = []; // 对话列表 let currentConversation = null; // 当前对话 let isLoading = false; // DOM 元素 const appContainer = document.getElementById('app'); const messagesContainer = document.getElementById('messagesContainer'); const messagesDiv = document.getElementById('messages'); const userInput = document.getElementById('userInput'); const sendBtn = document.getElementById('sendBtn'); const welcome = document.getElementById('welcome'); // 初始化 document.addEventListener('DOMContentLoaded', () => { // 从本地存储加载对话列表 const saved = localStorage.getItem('conversations'); if (saved) { conversations = JSON.parse(saved); } // 兼容旧数据格式(chat_history) const oldHistory = localStorage.getItem('chat_history'); if (oldHistory && conversations.length === 0) { const oldMessages = JSON.parse(oldHistory); if (oldMessages.length > 0) { // 转换旧数据为新格式 const convertedConv = { id: Date.now().toString(), title: oldMessages[0].content.slice(0, 30) + (oldMessages[0].content.length > 30 ? '...' : ''), messages: oldMessages, createdAt: Date.now(), updatedAt: Date.now() }; conversations.push(convertedConv); saveConversations(); localStorage.removeItem('chat_history'); // 清理旧数据 } } // 显示对话列表页面 showConversationList(); }); // ==================== 对话列表页面 ==================== function showConversationList() { currentConversation = null; // 渲染对话列表 const listHtml = `