diff --git a/works/ai-chat-app/www/app.js b/works/ai-chat-app/www/app.js index e6921ab..41af9a6 100644 --- a/works/ai-chat-app/www/app.js +++ b/works/ai-chat-app/www/app.js @@ -1,5 +1,5 @@ // AI助手 - 前端应用 -// 使用智谱 GLM-4.5-Air 模型(流式输出) +// 使用智谱 GLM-4.5-Air 模型(流式输出 + 多对话管理) const CONFIG = { apiUrl: 'https://open.bigmodel.cn/api/paas/v4/chat/completions', @@ -8,11 +8,13 @@ const CONFIG = { maxTokens: 2048 }; -// 对话历史 -let messages = []; +// 数据结构 +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'); @@ -21,20 +23,151 @@ const welcome = document.getElementById('welcome'); // 初始化 document.addEventListener('DOMContentLoaded', () => { - // 从本地存储恢复对话 - const saved = localStorage.getItem('chat_history'); + // 从本地存储加载对话列表 + const saved = localStorage.getItem('conversations'); if (saved) { - messages = JSON.parse(saved); - renderMessages(); - if (messages.length > 0) { - welcome.style.display = 'none'; - } + conversations = JSON.parse(saved); } - // 聚焦输入框 - userInput.focus(); + // 显示对话列表页面 + showConversationList(); }); +// ==================== 对话列表页面 ==================== + +function showConversationList() { + currentConversation = null; + + // 渲染对话列表 + const listHtml = ` +