fix: 修复Agent执行历史为空的问题

- 在Agent初始化时添加示例执行历史
- 添加触发示例动作和清空历史的功能
- 完善Agent执行历史的显示界面
- 添加执行历史的操作按钮(触发示例、刷新、清空)
- 优化执行历史的显示格式,包括优先级、置信度、执行时间等
- 修复前端Agent数据加载逻辑
This commit is contained in:
zhaojie
2025-09-11 00:01:12 +08:00
parent 6ef72837a5
commit 23f460d997
9 changed files with 436 additions and 2 deletions

View File

@@ -344,6 +344,51 @@ def get_agent_status():
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/agent/action-history')
def get_agent_action_history():
"""获取Agent动作执行历史"""
try:
limit = request.args.get('limit', 50, type=int)
history = agent_assistant.get_action_history(limit)
return jsonify({
"success": True,
"history": history,
"count": len(history)
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/agent/trigger-sample', methods=['POST'])
def trigger_sample_action():
"""触发示例动作"""
try:
import asyncio
result = asyncio.run(agent_assistant.trigger_sample_actions())
return jsonify(result)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/agent/clear-history', methods=['POST'])
def clear_agent_history():
"""清空Agent执行历史"""
try:
result = agent_assistant.clear_execution_history()
return jsonify(result)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/agent/llm-stats')
def get_llm_stats():
"""获取LLM使用统计"""
try:
stats = agent_assistant.get_llm_usage_stats()
return jsonify({
"success": True,
"stats": stats
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/agent/toggle', methods=['POST'])
def toggle_agent_mode():
"""切换Agent模式"""

View File

@@ -615,7 +615,7 @@ class TSPDashboard {
this.updateToolsList(data.tools || []);
// 更新执行历史
this.updateExecutionHistory(data.execution_history || []);
this.updateAgentExecutionHistory(data.execution_history || []);
}
} catch (error) {
console.error('加载Agent数据失败:', error);
@@ -2009,6 +2009,136 @@ class TSPDashboard {
window.print();
}
// Agent执行历史相关功能
async refreshAgentHistory() {
try {
const response = await fetch('/api/agent/action-history?limit=20');
const data = await response.json();
if (data.success) {
this.updateAgentExecutionHistory(data.history);
this.showNotification(`已加载 ${data.count} 条执行历史`, 'success');
} else {
throw new Error(data.error || '获取执行历史失败');
}
} catch (error) {
console.error('刷新Agent历史失败:', error);
this.showNotification('刷新Agent历史失败: ' + error.message, 'error');
}
}
async triggerSampleAction() {
try {
const response = await fetch('/api/agent/trigger-sample', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
if (data.success) {
this.showNotification(data.message, 'success');
// 刷新执行历史
await this.refreshAgentHistory();
} else {
throw new Error(data.error || '触发示例动作失败');
}
} catch (error) {
console.error('触发示例动作失败:', error);
this.showNotification('触发示例动作失败: ' + error.message, 'error');
}
}
async clearAgentHistory() {
if (!confirm('确定要清空Agent执行历史吗此操作不可恢复。')) {
return;
}
try {
const response = await fetch('/api/agent/clear-history', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
if (data.success) {
this.showNotification(data.message, 'success');
// 清空显示
this.updateAgentExecutionHistory([]);
} else {
throw new Error(data.error || '清空历史失败');
}
} catch (error) {
console.error('清空Agent历史失败:', error);
this.showNotification('清空Agent历史失败: ' + error.message, 'error');
}
}
updateAgentExecutionHistory(history) {
const container = document.getElementById('agent-execution-history');
if (!history || history.length === 0) {
container.innerHTML = `
<div class="empty-state">
<i class="fas fa-history"></i>
<p>暂无执行历史</p>
</div>
`;
return;
}
const historyHtml = history.map(record => {
const startTime = new Date(record.start_time * 1000).toLocaleString();
const endTime = new Date(record.end_time * 1000).toLocaleString();
const duration = Math.round((record.end_time - record.start_time) * 100) / 100;
const priorityColor = {
5: 'danger',
4: 'warning',
3: 'info',
2: 'secondary',
1: 'light'
}[record.priority] || 'secondary';
const confidenceColor = record.confidence >= 0.8 ? 'success' :
record.confidence >= 0.5 ? 'warning' : 'danger';
return `
<div class="card mb-2">
<div class="card-body">
<div class="d-flex justify-content-between align-items-start">
<div class="flex-grow-1">
<h6 class="mb-1">${record.description}</h6>
<div class="d-flex gap-3 mb-2">
<span class="badge bg-${priorityColor}">优先级 ${record.priority}</span>
<span class="badge bg-${confidenceColor}">置信度 ${(record.confidence * 100).toFixed(0)}%</span>
<span class="badge bg-${record.success ? 'success' : 'danger'}">${record.success ? '成功' : '失败'}</span>
</div>
<small class="text-muted">
<i class="fas fa-clock me-1"></i>开始: ${startTime} |
<i class="fas fa-stopwatch me-1"></i>耗时: ${duration}
</small>
</div>
<div class="ms-3">
<span class="badge bg-primary">${record.action_type}</span>
</div>
</div>
${record.result && record.result.message ? `
<div class="mt-2">
<small class="text-muted">结果: ${record.result.message}</small>
</div>
` : ''}
</div>
</div>
`;
}).join('');
container.innerHTML = historyHtml;
}
// 更新分析报告
updateAnalyticsReport(data) {
const reportContainer = document.getElementById('analytics-report');

View File

@@ -667,8 +667,19 @@
</div>
<div class="card">
<div class="card-header">
<div class="card-header d-flex justify-content-between align-items-center">
<h6><i class="fas fa-list me-2"></i>Agent执行历史</h6>
<div class="btn-group btn-group-sm" role="group">
<button type="button" class="btn btn-outline-primary" onclick="dashboard.triggerSampleAction()">
<i class="fas fa-play me-1"></i>触发示例
</button>
<button type="button" class="btn btn-outline-secondary" onclick="dashboard.refreshAgentHistory()">
<i class="fas fa-sync-alt me-1"></i>刷新
</button>
<button type="button" class="btn btn-outline-danger" onclick="dashboard.clearAgentHistory()">
<i class="fas fa-trash me-1"></i>清空
</button>
</div>
</div>
<div class="card-body">
<div id="agent-execution-history">