@@ -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 totalRec entCount = agentUsageHistory . length ;
// 获取使用智能体的对话历史 ( 最多5个)
const recentAgentConvo s = getAgentConversationHistory ( 5 ) ;
const totalAg entConvos = conversations . filter ( c => c . agentId ) . length ;
return `
<div class="agents-page">
@@ -413,24 +360,24 @@ function renderAgentsPage() {
<div class="agents-content">
<!-- 最近使用 -->
${ recentAgents . length > 0 ? `
${ recentAgentConvo s . length > 0 ? `
<div class="agents-section">
<div class="section-title-row">
<div class="section-title">🕐 最近使用</div>
${ totalRec entCount > 5 ? `
${ totalAg entConvos > 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 } ">
${ recentAgentConvo s . 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 . nam e} </span>
<span class="recent-agent-avatar"> ${ conv . agent ? conv . agent. avatar : '🤖' } </span>
<span class="recent-agent-name"> ${ conv . titl e} </span>
</div>
<div class="recent-agent-right">
<span class="recent-agent-us age"> ${ agent . us ageCount } 次 </span>
<span class="recent-agent-time"> ${ formatUsage Time ( agent . lastUs edAt) } </span>
<span class="recent-agent-agent-name "> ${ conv . agent ? conv . agent . name : '未知智能体' } </span>
<span class="recent-agent-time"> ${ formatTime ( conv . updat edAt) } </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 i d = item . getAttribute ( 'data-id' ) ;
openAgent ( i d) ;
const convI d = item . getAttribute ( 'data-conv- id' ) ;
if ( convI d) {
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">
${ allRecent Agents . length === 0
${ allAgentConvo s . length === 0
? '<div class="empty-list">暂无历史使用记录</div>'
: allRecent Agents . map ( agent => `
<div class="agent-history-item" data-id=" ${ agent . id } ">
: allAgentConvo s . 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 . nam e} </span>
<span class="agent-history-avatar"> ${ conv . agent ? conv . agent. avatar : '🤖' } </span>
<span class="agent-history-name"> ${ conv . titl e} </span>
</div>
<div class="agent-history-right">
<span class="agent-history-us age"> ${ agent . us ageCount } 次 </span>
<span class="agent-history-time"> ${ formatUsage Time ( agent . lastUs edAt) } </span>
<span class="agent-history-agent "> ${ conv . agent ? conv . agent . name : '未知智能体' } </span>
<span class="agent-history-time"> ${ formatTime ( conv . updat edAt) } </span>
</div>
</div>
` ) . join ( '' )
@@ -620,11 +569,13 @@ function showAgentHistoryPage() {
} ) ;
}
// 绑定智能体点击
// 绑定对话点击(打开已有对话)
document . querySelectorAll ( '.agent-history-item' ) . forEach ( item => {
item . addEventListener ( 'click' , ( ) => {
const i d = item . getAttribute ( 'data-id' ) ;
openAgent ( i d) ;
const convI d = item . getAttribute ( 'data-conv- id' ) ;
if ( convI d) {
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 } ) ;