feat: add insights history feature with UI and backend support

- Implemented insights history section in the dashboard with cards displaying analysis results.
- Added CSS styles for insights cards, detail panel, and empty state.
- Enhanced i18n support for insights-related strings in English and Chinese.
- Developed backend methods for saving, retrieving, and deleting insights in the CLI history store.
- Integrated insights loading and rendering logic in the memory view.
- Added functionality to refresh insights and handle detail view interactions.
This commit is contained in:
catlog22
2025-12-14 09:48:02 +08:00
parent d3a522f3e8
commit 958cf290e2
7 changed files with 1034 additions and 92 deletions

View File

@@ -1689,6 +1689,471 @@
max-width: 300px;
}
/* ========================================
* Insights History Cards
* ======================================== */
.memory-insights-section {
margin-top: 1.5rem;
}
.insights-section {
background: hsl(var(--card));
border: 1px solid hsl(var(--border));
border-radius: 0.75rem;
overflow: hidden;
}
.insights-section-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.875rem 1rem;
border-bottom: 1px solid hsl(var(--border));
background: hsl(var(--muted) / 0.3);
}
.insights-section-header h3 {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.875rem;
font-weight: 600;
color: hsl(var(--foreground));
margin: 0;
}
.insights-section-header h3 i {
color: hsl(var(--primary));
}
.insights-section-header .section-count {
font-size: 0.6875rem;
font-weight: 500;
color: hsl(var(--muted-foreground));
background: hsl(var(--muted));
padding: 0.125rem 0.5rem;
border-radius: 0.75rem;
margin-left: 0.5rem;
}
.insights-section-actions {
display: flex;
align-items: center;
gap: 0.5rem;
}
/* Insights Cards Container */
.insights-cards-container {
padding: 1rem;
}
.insights-cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 0.75rem;
}
/* Individual Insight Card */
.insight-card {
display: flex;
flex-direction: column;
padding: 0.875rem;
background: hsl(var(--background));
border: 1px solid hsl(var(--border));
border-radius: 0.5rem;
cursor: pointer;
transition: all 0.2s ease;
border-left: 3px solid hsl(var(--border));
}
.insight-card:hover {
background: hsl(var(--hover));
border-color: hsl(var(--primary) / 0.3);
box-shadow: 0 2px 8px hsl(var(--foreground) / 0.08);
transform: translateY(-1px);
}
/* Severity-based colors */
.insight-card.high {
border-left-color: hsl(0 84% 60%);
}
.insight-card.high:hover {
border-left-color: hsl(0 84% 50%);
}
.insight-card.medium {
border-left-color: hsl(38 92% 50%);
}
.insight-card.medium:hover {
border-left-color: hsl(38 92% 45%);
}
.insight-card.low {
border-left-color: hsl(142 76% 36%);
}
.insight-card.low:hover {
border-left-color: hsl(142 76% 30%);
}
/* Insight Card Header */
.insight-card-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 0.625rem;
}
.insight-card-tool {
display: flex;
align-items: center;
gap: 0.375rem;
font-size: 0.75rem;
font-weight: 600;
color: hsl(var(--foreground));
}
.insight-card-tool i {
color: hsl(var(--primary));
font-size: 0.875rem;
}
.insight-card-time {
font-size: 0.6875rem;
color: hsl(var(--muted-foreground));
}
/* Insight Card Stats */
.insight-card-stats {
display: flex;
align-items: center;
gap: 0.75rem;
margin-bottom: 0.625rem;
padding-bottom: 0.5rem;
border-bottom: 1px solid hsl(var(--border));
}
.insight-stat {
display: flex;
align-items: center;
gap: 0.25rem;
font-size: 0.6875rem;
color: hsl(var(--muted-foreground));
}
.insight-stat i {
font-size: 0.75rem;
}
.insight-stat span {
font-weight: 500;
}
/* Insight Card Preview */
.insight-card-preview {
font-size: 0.8125rem;
color: hsl(var(--foreground));
line-height: 1.5;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
.insight-card-preview .preview-item {
display: flex;
align-items: flex-start;
gap: 0.375rem;
margin-bottom: 0.25rem;
}
.insight-card-preview .preview-item i {
color: hsl(var(--primary));
font-size: 0.6875rem;
margin-top: 0.25rem;
flex-shrink: 0;
}
.insight-card-preview .preview-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* Insight Detail Panel */
.insight-detail-panel {
position: fixed;
top: 0;
right: 0;
width: 480px;
max-width: 100vw;
height: 100vh;
background: hsl(var(--card));
border-left: 1px solid hsl(var(--border));
box-shadow: -4px 0 24px hsl(var(--foreground) / 0.1);
z-index: 1000;
display: flex;
flex-direction: column;
animation: slideInRight 0.3s ease;
}
@keyframes slideInRight {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.insight-detail {
display: flex;
flex-direction: column;
height: 100%;
}
.insight-detail-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem 1.25rem;
border-bottom: 1px solid hsl(var(--border));
background: hsl(var(--muted) / 0.3);
}
.insight-detail-header h3 {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 1rem;
font-weight: 600;
color: hsl(var(--foreground));
margin: 0;
}
.insight-detail-header h3 i {
color: hsl(var(--primary));
}
.insight-detail-close {
padding: 0.5rem;
background: transparent;
border: none;
color: hsl(var(--muted-foreground));
cursor: pointer;
border-radius: 0.375rem;
transition: all 0.15s ease;
}
.insight-detail-close:hover {
background: hsl(var(--muted));
color: hsl(var(--foreground));
}
.insight-detail-meta {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
padding: 1rem 1.25rem;
border-bottom: 1px solid hsl(var(--border));
background: hsl(var(--background));
}
.meta-item {
display: flex;
align-items: center;
gap: 0.375rem;
font-size: 0.75rem;
color: hsl(var(--muted-foreground));
}
.meta-item i {
color: hsl(var(--primary));
font-size: 0.875rem;
}
.meta-item span {
font-weight: 500;
color: hsl(var(--foreground));
}
.insight-detail-content {
flex: 1;
overflow-y: auto;
padding: 1.25rem;
}
.insight-detail-section {
margin-bottom: 1.5rem;
}
.insight-detail-section:last-child {
margin-bottom: 0;
}
.insight-detail-section h4 {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.875rem;
font-weight: 600;
color: hsl(var(--foreground));
margin: 0 0 0.75rem 0;
padding-bottom: 0.5rem;
border-bottom: 1px solid hsl(var(--border));
}
.insight-detail-section h4 i {
color: hsl(var(--primary));
}
/* Pattern and Suggestion Items */
.pattern-item,
.suggestion-item {
display: flex;
align-items: flex-start;
gap: 0.75rem;
padding: 0.75rem;
background: hsl(var(--background));
border: 1px solid hsl(var(--border));
border-radius: 0.5rem;
margin-bottom: 0.5rem;
transition: all 0.15s ease;
}
.pattern-item:hover,
.suggestion-item:hover {
background: hsl(var(--hover));
border-color: hsl(var(--primary) / 0.3);
}
.pattern-item:last-child,
.suggestion-item:last-child {
margin-bottom: 0;
}
.item-icon {
display: flex;
align-items: center;
justify-content: center;
width: 2rem;
height: 2rem;
border-radius: 0.375rem;
flex-shrink: 0;
}
.pattern-item .item-icon {
background: hsl(var(--primary) / 0.1);
color: hsl(var(--primary));
}
.suggestion-item .item-icon {
background: hsl(142 76% 36% / 0.1);
color: hsl(142 76% 36%);
}
.item-content {
flex: 1;
min-width: 0;
}
.item-title {
font-size: 0.8125rem;
font-weight: 500;
color: hsl(var(--foreground));
margin-bottom: 0.25rem;
}
.item-description {
font-size: 0.75rem;
color: hsl(var(--muted-foreground));
line-height: 1.5;
}
.item-example {
margin-top: 0.5rem;
padding: 0.5rem;
background: hsl(var(--muted) / 0.5);
border-radius: 0.25rem;
font-family: var(--font-mono);
font-size: 0.6875rem;
color: hsl(var(--foreground));
overflow-x: auto;
white-space: pre-wrap;
word-wrap: break-word;
}
/* Detail Actions */
.insight-detail-actions {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 0.5rem;
padding: 1rem 1.25rem;
border-top: 1px solid hsl(var(--border));
background: hsl(var(--muted) / 0.2);
}
.insight-detail-actions button {
display: flex;
align-items: center;
gap: 0.375rem;
padding: 0.5rem 1rem;
font-size: 0.8125rem;
font-weight: 500;
border-radius: 0.375rem;
cursor: pointer;
transition: all 0.15s ease;
}
.btn-delete-insight {
background: hsl(0 84% 60% / 0.1);
border: 1px solid hsl(0 84% 60% / 0.3);
color: hsl(0 84% 60%);
}
.btn-delete-insight:hover {
background: hsl(0 84% 60% / 0.2);
border-color: hsl(0 84% 60%);
}
.btn-close-insight {
background: hsl(var(--muted));
border: 1px solid hsl(var(--border));
color: hsl(var(--foreground));
}
.btn-close-insight:hover {
background: hsl(var(--hover));
border-color: hsl(var(--primary) / 0.3);
}
/* Empty Insights State */
.insights-empty {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2rem;
text-align: center;
color: hsl(var(--muted-foreground));
}
.insights-empty i {
font-size: 2.5rem;
opacity: 0.3;
margin-bottom: 0.75rem;
}
.insights-empty p {
font-size: 0.875rem;
margin: 0;
}
/* ========================================
* Responsive Adjustments
* ======================================== */
@@ -1704,4 +2169,12 @@
.insights-grid {
grid-template-columns: 1fr;
}
.insights-cards {
grid-template-columns: 1fr;
}
.insight-detail-panel {
width: 100%;
}
}

View File

@@ -613,6 +613,16 @@ const i18n = {
'memory.autoSyncActive': 'Auto-sync',
'memory.configUpdated': 'Configuration updated',
'memory.configError': 'Failed to update configuration',
'memory.insightsHistory': 'Insights History',
'memory.insightsEmpty': 'No insights yet. Run an analysis to generate insights.',
'memory.insightPatterns': 'Patterns',
'memory.insightSuggestions': 'Suggestions',
'memory.insightDetail': 'Insight Detail',
'memory.insightDelete': 'Delete',
'memory.insightDeleteConfirm': 'Are you sure you want to delete this insight?',
'memory.insightDeleted': 'Insight deleted',
'memory.prompts': 'prompts',
'memory.refreshInsights': 'Refresh',
// Common
'common.cancel': 'Cancel',
@@ -1237,6 +1247,16 @@ const i18n = {
'memory.autoSyncActive': '自动同步',
'memory.configUpdated': '配置已更新',
'memory.configError': '配置更新失败',
'memory.insightsHistory': '洞察历史',
'memory.insightsEmpty': '暂无洞察记录。运行分析以生成洞察。',
'memory.insightPatterns': '模式',
'memory.insightSuggestions': '建议',
'memory.insightDetail': '洞察详情',
'memory.insightDelete': '删除',
'memory.insightDeleteConfirm': '确定要删除此洞察吗?',
'memory.insightDeleted': '洞察已删除',
'memory.prompts': '提示',
'memory.refreshInsights': '刷新',
// Common
'common.cancel': '取消',

View File

@@ -14,6 +14,8 @@ var activeMemoryConfig = {
tool: 'gemini' // gemini, qwen
};
var activeMemorySyncTimer = null; // Timer for automatic periodic sync
var insightsHistory = []; // Insights analysis history
var selectedInsight = null; // Currently selected insight for detail view
// ========== Main Render Function ==========
async function renderMemoryView() {
@@ -37,7 +39,8 @@ async function renderMemoryView() {
loadMemoryStats(),
loadMemoryGraph(),
loadRecentContext(),
loadActiveMemoryStatus()
loadActiveMemoryStatus(),
loadInsightsHistory()
]);
// Render layout with Active Memory header
@@ -55,12 +58,14 @@ async function renderMemoryView() {
'<div class="memory-column center" id="memory-graph"></div>' +
'<div class="memory-column right" id="memory-context"></div>' +
'</div>' +
'<div class="memory-insights-section" id="memory-insights"></div>' +
'</div>';
// Render each column
renderHotspotsColumn();
renderGraphColumn();
renderContextColumn();
renderInsightsSection();
// Initialize Lucide icons
if (window.lucide) lucide.createIcons();
@@ -159,6 +164,20 @@ async function loadRecentContext() {
}
}
async function loadInsightsHistory() {
try {
var response = await fetch('/api/memory/insights?limit=10');
if (!response.ok) throw new Error('Failed to load insights history');
var data = await response.json();
insightsHistory = data.insights || [];
return insightsHistory;
} catch (err) {
console.error('Failed to load insights history:', err);
insightsHistory = [];
return [];
}
}
// ========== Active Memory Functions ==========
// Timer management for automatic sync
function startActiveMemorySyncTimer() {
@@ -885,6 +904,208 @@ function showNodeDetails(node) {
if (window.lucide) lucide.createIcons();
}
// ========== Insights Section ==========
function renderInsightsSection() {
var container = document.getElementById('memory-insights');
if (!container) return;
container.innerHTML = '<div class="insights-section">' +
'<div class="section-header">' +
'<div class="section-header-left">' +
'<h3><i data-lucide="lightbulb" class="w-4 h-4"></i> ' + t('memory.insightsHistory') + '</h3>' +
'<span class="section-count">' + insightsHistory.length + ' ' + t('memory.analyses') + '</span>' +
'</div>' +
'<div class="section-header-actions">' +
'<button class="btn-icon" onclick="refreshInsightsHistory()" title="' + t('common.refresh') + '">' +
'<i data-lucide="refresh-cw" class="w-4 h-4"></i>' +
'</button>' +
'</div>' +
'</div>' +
'<div class="insights-cards-container">' +
renderInsightsCards() +
'</div>' +
(selectedInsight ? '<div class="insight-detail-panel" id="insightDetailPanel">' + renderInsightDetail(selectedInsight) + '</div>' : '') +
'</div>';
if (window.lucide) lucide.createIcons();
}
function renderInsightsCards() {
if (!insightsHistory || insightsHistory.length === 0) {
return '<div class="insights-empty">' +
'<i data-lucide="lightbulb-off" class="w-8 h-8"></i>' +
'<p>' + t('memory.noInsightsYet') + '</p>' +
'<p class="insights-empty-hint">' + t('memory.triggerAnalysis') + '</p>' +
'</div>';
}
return '<div class="insights-cards">' +
insightsHistory.map(function(insight) {
var patternCount = (insight.patterns || []).length;
var suggestionCount = (insight.suggestions || []).length;
var severity = getInsightSeverity(insight.patterns);
var date = new Date(insight.created_at);
var timeAgo = formatTimestamp(insight.created_at);
return '<div class="insight-card ' + severity + '" onclick="showInsightDetail(\'' + insight.id + '\')">' +
'<div class="insight-card-header">' +
'<div class="insight-card-tool">' +
'<i data-lucide="' + getToolIcon(insight.tool) + '" class="w-4 h-4"></i>' +
'<span>' + insight.tool + '</span>' +
'</div>' +
'<div class="insight-card-time">' + timeAgo + '</div>' +
'</div>' +
'<div class="insight-card-stats">' +
'<div class="insight-stat">' +
'<span class="insight-stat-value">' + patternCount + '</span>' +
'<span class="insight-stat-label">' + t('memory.patterns') + '</span>' +
'</div>' +
'<div class="insight-stat">' +
'<span class="insight-stat-value">' + suggestionCount + '</span>' +
'<span class="insight-stat-label">' + t('memory.suggestions') + '</span>' +
'</div>' +
'<div class="insight-stat">' +
'<span class="insight-stat-value">' + insight.prompt_count + '</span>' +
'<span class="insight-stat-label">' + t('memory.prompts') + '</span>' +
'</div>' +
'</div>' +
'<div class="insight-card-preview">' +
(insight.patterns && insight.patterns.length > 0 ?
'<div class="pattern-preview ' + (insight.patterns[0].severity || 'low') + '">' +
'<span class="pattern-type">' + escapeHtml(insight.patterns[0].type || 'pattern') + '</span>' +
'<span class="pattern-desc">' + escapeHtml((insight.patterns[0].description || '').substring(0, 60)) + '...</span>' +
'</div>' : '') +
'</div>' +
'</div>';
}).join('') +
'</div>';
}
function getInsightSeverity(patterns) {
if (!patterns || patterns.length === 0) return 'low';
var hasHigh = patterns.some(function(p) { return p.severity === 'high'; });
var hasMedium = patterns.some(function(p) { return p.severity === 'medium'; });
return hasHigh ? 'high' : (hasMedium ? 'medium' : 'low');
}
function getToolIcon(tool) {
switch(tool) {
case 'gemini': return 'sparkles';
case 'qwen': return 'bot';
case 'codex': return 'code-2';
default: return 'cpu';
}
}
async function showInsightDetail(insightId) {
try {
var response = await fetch('/api/memory/insights/' + insightId);
if (!response.ok) throw new Error('Failed to load insight detail');
var data = await response.json();
selectedInsight = data.insight;
renderInsightsSection();
} catch (err) {
console.error('Failed to load insight detail:', err);
if (window.showToast) {
showToast(t('memory.loadInsightError'), 'error');
}
}
}
function closeInsightDetail() {
selectedInsight = null;
renderInsightsSection();
}
function renderInsightDetail(insight) {
if (!insight) return '';
var html = '<div class="insight-detail">' +
'<div class="insight-detail-header">' +
'<h4><i data-lucide="lightbulb" class="w-4 h-4"></i> ' + t('memory.insightDetail') + '</h4>' +
'<button class="btn-icon" onclick="closeInsightDetail()" title="' + t('common.close') + '">' +
'<i data-lucide="x" class="w-4 h-4"></i>' +
'</button>' +
'</div>' +
'<div class="insight-detail-meta">' +
'<span><i data-lucide="' + getToolIcon(insight.tool) + '" class="w-3 h-3"></i> ' + insight.tool + '</span>' +
'<span><i data-lucide="clock" class="w-3 h-3"></i> ' + formatTimestamp(insight.created_at) + '</span>' +
'<span><i data-lucide="file-text" class="w-3 h-3"></i> ' + insight.prompt_count + ' ' + t('memory.promptsAnalyzed') + '</span>' +
'</div>';
// Patterns
if (insight.patterns && insight.patterns.length > 0) {
html += '<div class="insight-patterns">' +
'<h5><i data-lucide="alert-triangle" class="w-3.5 h-3.5"></i> ' + t('memory.patternsFound') + ' (' + insight.patterns.length + ')</h5>' +
'<div class="patterns-list">' +
insight.patterns.map(function(p) {
return '<div class="pattern-item ' + (p.severity || 'low') + '">' +
'<div class="pattern-header">' +
'<span class="pattern-type-badge">' + escapeHtml(p.type || 'pattern') + '</span>' +
'<span class="pattern-severity">' + (p.severity || 'low') + '</span>' +
(p.occurrences ? '<span class="pattern-occurrences">' + p.occurrences + 'x</span>' : '') +
'</div>' +
'<div class="pattern-description">' + escapeHtml(p.description || '') + '</div>' +
(p.suggestion ? '<div class="pattern-suggestion"><i data-lucide="arrow-right" class="w-3 h-3"></i> ' + escapeHtml(p.suggestion) + '</div>' : '') +
'</div>';
}).join('') +
'</div>' +
'</div>';
}
// Suggestions
if (insight.suggestions && insight.suggestions.length > 0) {
html += '<div class="insight-suggestions">' +
'<h5><i data-lucide="lightbulb" class="w-3.5 h-3.5"></i> ' + t('memory.suggestionsProvided') + ' (' + insight.suggestions.length + ')</h5>' +
'<div class="suggestions-list">' +
insight.suggestions.map(function(s) {
return '<div class="suggestion-item">' +
'<div class="suggestion-title">' + escapeHtml(s.title || '') + '</div>' +
'<div class="suggestion-description">' + escapeHtml(s.description || '') + '</div>' +
(s.example ? '<div class="suggestion-example"><code>' + escapeHtml(s.example) + '</code></div>' : '') +
'</div>';
}).join('') +
'</div>' +
'</div>';
}
html += '<div class="insight-detail-actions">' +
'<button class="btn btn-sm btn-danger" onclick="deleteInsight(\'' + insight.id + '\')">' +
'<i data-lucide="trash-2" class="w-3.5 h-3.5"></i> ' + t('common.delete') +
'</button>' +
'</div>' +
'</div>';
return html;
}
async function deleteInsight(insightId) {
if (!confirm(t('memory.confirmDeleteInsight'))) return;
try {
var response = await fetch('/api/memory/insights/' + insightId, { method: 'DELETE' });
if (!response.ok) throw new Error('Failed to delete insight');
selectedInsight = null;
await loadInsightsHistory();
renderInsightsSection();
if (window.showToast) {
showToast(t('memory.insightDeleted'), 'success');
}
} catch (err) {
console.error('Failed to delete insight:', err);
if (window.showToast) {
showToast(t('memory.deleteInsightError'), 'error');
}
}
}
async function refreshInsightsHistory() {
await loadInsightsHistory();
renderInsightsSection();
}
// ========== Actions ==========
async function setMemoryTimeFilter(filter) {
memoryTimeFilter = filter;