feat: 智能体历史改为显示每次对话记录而非合并统计
This commit is contained in:
138
www/app.js
138
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 `
|
||||
<div class="agents-page">
|
||||
@@ -413,24 +360,24 @@ function renderAgentsPage() {
|
||||
|
||||
<div class="agents-content">
|
||||
<!-- 最近使用 -->
|
||||
${recentAgents.length > 0 ? `
|
||||
${recentAgentConvos.length > 0 ? `
|
||||
<div class="agents-section">
|
||||
<div class="section-title-row">
|
||||
<div class="section-title">🕐 最近使用</div>
|
||||
${totalRecentCount > 5 ? `
|
||||
${totalAgentConvos > 5 ? `
|
||||
<div class="section-more" id="showAllRecentBtn">更多>></div>
|
||||
` : ''}
|
||||
</div>
|
||||
<div class="recent-agents-list">
|
||||
${recentAgents.map(agent => `
|
||||
<div class="recent-agent-item" data-id="${agent.id}">
|
||||
${recentAgentConvos.map(conv => `
|
||||
<div class="recent-agent-item" data-conv-id="${conv.id}">
|
||||
<div class="recent-agent-left">
|
||||
<span class="recent-agent-avatar">${agent.avatar}</span>
|
||||
<span class="recent-agent-name">${agent.name}</span>
|
||||
<span class="recent-agent-avatar">${conv.agent ? conv.agent.avatar : '🤖'}</span>
|
||||
<span class="recent-agent-name">${conv.title}</span>
|
||||
</div>
|
||||
<div class="recent-agent-right">
|
||||
<span class="recent-agent-usage">${agent.usageCount}次</span>
|
||||
<span class="recent-agent-time">${formatUsageTime(agent.lastUsedAt)}</span>
|
||||
<span class="recent-agent-agent-name">${conv.agent ? conv.agent.name : '未知智能体'}</span>
|
||||
<span class="recent-agent-time">${formatTime(conv.updatedAt)}</span>
|
||||
</div>
|
||||
</div>
|
||||
`).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 = `
|
||||
<div class="agent-history-page">
|
||||
@@ -591,17 +540,17 @@ function showAgentHistoryPage() {
|
||||
</header>
|
||||
|
||||
<div class="agent-history-content">
|
||||
${allRecentAgents.length === 0
|
||||
${allAgentConvos.length === 0
|
||||
? '<div class="empty-list">暂无历史使用记录</div>'
|
||||
: allRecentAgents.map(agent => `
|
||||
<div class="agent-history-item" data-id="${agent.id}">
|
||||
: allAgentConvos.map(conv => `
|
||||
<div class="agent-history-item" data-conv-id="${conv.id}">
|
||||
<div class="agent-history-left">
|
||||
<span class="agent-history-avatar">${agent.avatar}</span>
|
||||
<span class="agent-history-name">${agent.name}</span>
|
||||
<span class="agent-history-avatar">${conv.agent ? conv.agent.avatar : '🤖'}</span>
|
||||
<span class="agent-history-name">${conv.title}</span>
|
||||
</div>
|
||||
<div class="agent-history-right">
|
||||
<span class="agent-history-usage">${agent.usageCount}次</span>
|
||||
<span class="agent-history-time">${formatUsageTime(agent.lastUsedAt)}</span>
|
||||
<span class="agent-history-agent">${conv.agent ? conv.agent.name : '未知智能体'}</span>
|
||||
<span class="agent-history-time">${formatTime(conv.updatedAt)}</span>
|
||||
</div>
|
||||
</div>
|
||||
`).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 });
|
||||
|
||||
@@ -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=3.1.2">
|
||||
<link rel="stylesheet" href="style.css?v=3.1.3">
|
||||
<link rel="manifest" href="manifest.json">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script src="marked.min.js?v=3.1.2"></script>
|
||||
<script src="app.js?v=3.1.2"></script>
|
||||
<script src="marked.min.js?v=3.1.3"></script>
|
||||
<script src="app.js?v=3.1.3"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user