diff --git a/www/app.js b/www/app.js index ac6e73f..314e337 100644 --- a/www/app.js +++ b/www/app.js @@ -45,69 +45,19 @@ let agents = [ let currentAgent = null; // 当前选中的智能体 -// 智能体使用历史记录 -let agentUsageHistory = []; - -// 加载智能体使用历史 -function loadAgentUsageHistory() { - const saved = localStorage.getItem('agentUsageHistory'); - if (saved) { - agentUsageHistory = JSON.parse(saved); - } -} - -// 保存智能体使用历史 -function saveAgentUsageHistory() { - localStorage.setItem('agentUsageHistory', JSON.stringify(agentUsageHistory)); -} - -// 记录智能体使用 -function recordAgentUsage(agentId) { - const record = agentUsageHistory.find(r => r.agentId === agentId); - if (record) { - record.usageCount++; - record.lastUsedAt = Date.now(); - } else { - agentUsageHistory.push({ - agentId: agentId, - usageCount: 1, - lastUsedAt: Date.now() - }); - } - saveAgentUsageHistory(); -} - -// 格式化使用时间 -function formatUsageTime(timestamp) { - const now = Date.now(); - const diff = now - timestamp; - - const minute = 60 * 1000; - const hour = 60 * minute; - const day = 24 * hour; - const week = 7 * day; - const month = 30 * day; - const year = 365 * day; - - if (diff < minute) return '刚刚'; - if (diff < hour) return Math.floor(diff / minute) + '分钟前'; - if (diff < day) return Math.floor(diff / hour) + '小时前'; - if (diff < week) return Math.floor(diff / day) + '天前'; - if (diff < month) return Math.floor(diff / week) + '周前'; - if (diff < year) return Math.floor(diff / month) + '月前'; - return Math.floor(diff / year) + '年前'; -} - -// 获取最近使用的智能体(最多5个) -function getRecentUsedAgents(limit = 5) { - return agentUsageHistory - .sort((a, b) => b.lastUsedAt - a.lastUsedAt) +// 获取使用智能体的对话列表(按时间倒序) +function getAgentConversationHistory(limit = 5) { + return conversations + .filter(conv => conv.agentId) // 筛选有智能体的对话 + .sort((a, b) => b.updatedAt - a.updatedAt) // 按更新时间倒序 .slice(0, limit) - .map(record => { - const agent = agents.find(a => a.id === record.agentId); - return { ...agent, ...record }; - }) - .filter(a => a.id); // 过滤掉可能被删除的智能体 + .map(conv => { + const agent = agents.find(a => a.id === conv.agentId); + return { + ...conv, + agent: agent + }; + }); } // 功能开关 @@ -136,9 +86,6 @@ document.addEventListener('DOMContentLoaded', () => { conversations = JSON.parse(saved); } - // 加载智能体使用历史 - loadAgentUsageHistory(); - // 兼容旧数据格式(chat_history) const oldHistory = localStorage.getItem('chat_history'); if (oldHistory && conversations.length === 0) { @@ -401,9 +348,9 @@ function renderAgentsPage() { const studyAgents = agents.filter(a => a.category === 'study'); const lifeAgents = agents.filter(a => a.category === 'life'); - // 获取最近使用的智能体(最多5个) - const recentAgents = getRecentUsedAgents(5); - const totalRecentCount = agentUsageHistory.length; + // 获取使用智能体的对话历史(最多5个) + const recentAgentConvos = getAgentConversationHistory(5); + const totalAgentConvos = conversations.filter(c => c.agentId).length; return `
@@ -413,24 +360,24 @@ function renderAgentsPage() {
- ${recentAgents.length > 0 ? ` + ${recentAgentConvos.length > 0 ? `
🕐 最近使用
- ${totalRecentCount > 5 ? ` + ${totalAgentConvos > 5 ? `
更多>>
` : ''}
- ${recentAgents.map(agent => ` -
+ ${recentAgentConvos.map(conv => ` +
- ${agent.avatar} - ${agent.name} + ${conv.agent ? conv.agent.avatar : '🤖'} + ${conv.title}
- ${agent.usageCount}次 - ${formatUsageTime(agent.lastUsedAt)} + ${conv.agent ? conv.agent.name : '未知智能体'} + ${formatTime(conv.updatedAt)}
`).join('')} @@ -499,7 +446,7 @@ function renderAgentsPage() { } function bindAgentsPageEvents() { - // 智能体卡片点击 + // 智能体卡片点击(新建对话) document.querySelectorAll('.agent-card').forEach(card => { card.addEventListener('click', () => { const id = card.getAttribute('data-id'); @@ -507,11 +454,13 @@ function bindAgentsPageEvents() { }); }); - // 最近使用智能体点击 + // 最近使用对话点击(打开已有对话) document.querySelectorAll('.recent-agent-item').forEach(item => { item.addEventListener('click', () => { - const id = item.getAttribute('data-id'); - openAgent(id); + const convId = item.getAttribute('data-conv-id'); + if (convId) { + openConversation(convId); + } }); }); @@ -579,7 +528,7 @@ function bindProfilePageEvents() { // 查看全部历史使用 function showAgentHistoryPage() { - const allRecentAgents = getRecentUsedAgents(100); // 获取所有 + const allAgentConvos = getAgentConversationHistory(100); // 获取所有 const historyHtml = `
@@ -591,17 +540,17 @@ function showAgentHistoryPage() {
- ${allRecentAgents.length === 0 + ${allAgentConvos.length === 0 ? '
暂无历史使用记录
' - : allRecentAgents.map(agent => ` -
+ : allAgentConvos.map(conv => ` +
- ${agent.avatar} - ${agent.name} + ${conv.agent ? conv.agent.avatar : '🤖'} + ${conv.title}
- ${agent.usageCount}次 - ${formatUsageTime(agent.lastUsedAt)} + ${conv.agent ? conv.agent.name : '未知智能体'} + ${formatTime(conv.updatedAt)}
`).join('') @@ -620,11 +569,13 @@ function showAgentHistoryPage() { }); } - // 绑定智能体点击 + // 绑定对话点击(打开已有对话) document.querySelectorAll('.agent-history-item').forEach(item => { item.addEventListener('click', () => { - const id = item.getAttribute('data-id'); - openAgent(id); + const convId = item.getAttribute('data-conv-id'); + if (convId) { + openConversation(convId); + } }); }); } @@ -1562,11 +1513,6 @@ async function sendMessage() { // 隐藏欢迎界面 welcome.style.display = 'none'; - - // 如果是智能体对话的第一条消息,记录智能体使用 - if (currentConversation.agentId && currentConversation.messages.length === 0) { - recordAgentUsage(currentConversation.agentId); - } // 添加用户消息 currentConversation.messages.push({ role: 'user', content: text }); diff --git a/www/index.html b/www/index.html index 0a61d23..5aaf1cf 100644 --- a/www/index.html +++ b/www/index.html @@ -8,12 +8,12 @@ AI助手 - +
- - + + \ No newline at end of file diff --git a/www/style.css b/www/style.css index 58dba16..0b3c597 100644 --- a/www/style.css +++ b/www/style.css @@ -301,6 +301,14 @@ body { color: var(--primary); } +.recent-agent-agent-name { + font-size: 12px; + color: var(--primary); + background: rgba(102, 126, 234, 0.1); + padding: 2px 8px; + border-radius: 4px; +} + /* 智能体历史页面 */ .agent-history-page { display: flex; @@ -370,6 +378,14 @@ body { color: var(--primary); } +.agent-history-agent { + font-size: 12px; + color: var(--primary); + background: rgba(102, 126, 234, 0.1); + padding: 2px 8px; + border-radius: 4px; +} + /* ==================== 我的页面 ==================== */ .profile-page {