superlocalmemory 3.3.29 → 3.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ATTRIBUTION.md +1 -1
- package/CHANGELOG.md +3 -0
- package/LICENSE +633 -70
- package/README.md +14 -11
- package/docs/screenshots/01-dashboard-main.png +0 -0
- package/docs/screenshots/02-knowledge-graph.png +0 -0
- package/docs/screenshots/03-patterns-learning.png +0 -0
- package/docs/screenshots/04-learning-dashboard.png +0 -0
- package/docs/screenshots/05-behavioral-analysis.png +0 -0
- package/docs/screenshots/06-graph-communities.png +0 -0
- package/docs/v2-archive/ACCESSIBILITY.md +1 -1
- package/docs/v2-archive/FRAMEWORK-INTEGRATIONS.md +1 -1
- package/docs/v2-archive/MCP-MANUAL-SETUP.md +1 -1
- package/docs/v2-archive/SEARCH-ENGINE-V2.2.0.md +2 -2
- package/docs/v2-archive/SEARCH-INTEGRATION-GUIDE.md +1 -1
- package/docs/v2-archive/UNIVERSAL-INTEGRATION.md +1 -1
- package/docs/v2-archive/V2.2.0-OPTIONAL-SEARCH.md +1 -1
- package/docs/v2-archive/example_graph_usage.py +1 -1
- package/ide/configs/codex-mcp.toml +1 -1
- package/ide/integrations/langchain/README.md +1 -1
- package/ide/integrations/langchain/langchain_superlocalmemory/__init__.py +1 -1
- package/ide/integrations/langchain/langchain_superlocalmemory/chat_message_history.py +1 -1
- package/ide/integrations/langchain/pyproject.toml +2 -2
- package/ide/integrations/langchain/tests/__init__.py +1 -1
- package/ide/integrations/langchain/tests/test_chat_message_history.py +1 -1
- package/ide/integrations/langchain/tests/test_security.py +1 -1
- package/ide/integrations/llamaindex/llama_index/storage/chat_store/superlocalmemory/__init__.py +1 -1
- package/ide/integrations/llamaindex/llama_index/storage/chat_store/superlocalmemory/base.py +1 -1
- package/ide/integrations/llamaindex/pyproject.toml +2 -2
- package/ide/integrations/llamaindex/tests/__init__.py +1 -1
- package/ide/integrations/llamaindex/tests/test_chat_store.py +1 -1
- package/ide/integrations/llamaindex/tests/test_security.py +1 -1
- package/ide/skills/slm-build-graph/SKILL.md +3 -3
- package/ide/skills/slm-list-recent/SKILL.md +3 -3
- package/ide/skills/slm-recall/SKILL.md +3 -3
- package/ide/skills/slm-remember/SKILL.md +3 -3
- package/ide/skills/slm-show-patterns/SKILL.md +3 -3
- package/ide/skills/slm-status/SKILL.md +3 -3
- package/ide/skills/slm-switch-profile/SKILL.md +3 -3
- package/package.json +3 -3
- package/pyproject.toml +3 -3
- package/src/superlocalmemory/core/engine_wiring.py +5 -1
- package/src/superlocalmemory/core/graph_analyzer.py +254 -12
- package/src/superlocalmemory/learning/consolidation_worker.py +240 -52
- package/src/superlocalmemory/retrieval/entity_channel.py +135 -4
- package/src/superlocalmemory/retrieval/spreading_activation.py +45 -0
- package/src/superlocalmemory/server/api.py +9 -1
- package/src/superlocalmemory/server/routes/behavioral.py +8 -4
- package/src/superlocalmemory/server/routes/chat.py +320 -0
- package/src/superlocalmemory/server/routes/insights.py +368 -0
- package/src/superlocalmemory/server/routes/learning.py +106 -6
- package/src/superlocalmemory/server/routes/memories.py +20 -9
- package/src/superlocalmemory/server/routes/stats.py +25 -3
- package/src/superlocalmemory/server/routes/timeline.py +252 -0
- package/src/superlocalmemory/server/routes/v3_api.py +161 -0
- package/src/superlocalmemory/server/ui.py +8 -0
- package/src/superlocalmemory/ui/index.html +168 -58
- package/src/superlocalmemory/ui/js/graph-event-bus.js +83 -0
- package/src/superlocalmemory/ui/js/graph-filters.js +1 -1
- package/src/superlocalmemory/ui/js/knowledge-graph.js +942 -0
- package/src/superlocalmemory/ui/js/memory-chat.js +344 -0
- package/src/superlocalmemory/ui/js/memory-timeline.js +265 -0
- package/src/superlocalmemory/ui/js/quick-actions.js +334 -0
- package/src/superlocalmemory.egg-info/PKG-INFO +597 -0
- package/src/superlocalmemory.egg-info/SOURCES.txt +287 -0
- package/src/superlocalmemory.egg-info/dependency_links.txt +1 -0
- package/src/superlocalmemory.egg-info/entry_points.txt +2 -0
- package/src/superlocalmemory.egg-info/requires.txt +47 -0
- package/src/superlocalmemory.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,942 @@
|
|
|
1
|
+
// SuperLocalMemory v3.4.1 — Sigma.js WebGL Knowledge Graph
|
|
2
|
+
// Copyright (c) 2026 Varun Pratap Bhardwaj — AGPL-3.0-or-later
|
|
3
|
+
// Replaces Cytoscape.js as default renderer. Cytoscape kept as fallback toggle.
|
|
4
|
+
|
|
5
|
+
// ============================================================================
|
|
6
|
+
// GLOBAL STATE (mirrors graph-core.js contract for interoperability)
|
|
7
|
+
// ============================================================================
|
|
8
|
+
|
|
9
|
+
var sigmaInstance = null;
|
|
10
|
+
var sigmaGraph = null; // graphology Graph instance
|
|
11
|
+
var sigmaState = {
|
|
12
|
+
hoveredNode: null,
|
|
13
|
+
selectedNode: null,
|
|
14
|
+
searchQuery: '',
|
|
15
|
+
suggestions: new Set(),
|
|
16
|
+
highlightedNodes: new Set(), // For event bus highlights (Phase 3)
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Define shared globals (previously in graph-core.js, removed in v3.4.1)
|
|
20
|
+
if (typeof graphData === 'undefined') var graphData = { nodes: [], links: [] };
|
|
21
|
+
if (typeof originalGraphData === 'undefined') var originalGraphData = { nodes: [], links: [] };
|
|
22
|
+
if (typeof filterState === 'undefined') var filterState = { cluster_id: null, entity: null };
|
|
23
|
+
if (typeof isInitialLoad === 'undefined') var isInitialLoad = true;
|
|
24
|
+
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// CLUSTER COLORS (same palette as graph-core.js for consistency)
|
|
27
|
+
// ============================================================================
|
|
28
|
+
|
|
29
|
+
const SIGMA_CLUSTER_COLORS = [
|
|
30
|
+
'#667eea', '#764ba2', '#43e97b', '#38f9d7',
|
|
31
|
+
'#4facfe', '#00f2fe', '#f093fb', '#f5576c',
|
|
32
|
+
'#fa709a', '#fee140', '#30cfd0', '#330867'
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
function getSigmaClusterColor(communityId) {
|
|
36
|
+
if (communityId === null || communityId === undefined || communityId === 0) return '#999999';
|
|
37
|
+
return SIGMA_CLUSTER_COLORS[Math.abs(communityId) % SIGMA_CLUSTER_COLORS.length];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Color by fact_type when communities are not available
|
|
41
|
+
var CATEGORY_COLORS = {
|
|
42
|
+
'semantic': '#667eea', // Indigo — knowledge facts
|
|
43
|
+
'episodic': '#43e97b', // Green — session events
|
|
44
|
+
'opinion': '#f093fb', // Pink — decisions & opinions
|
|
45
|
+
'temporal': '#4facfe', // Blue — time-referenced facts
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function getNodeColor(node) {
|
|
49
|
+
// Priority 1: community_id from graph intelligence
|
|
50
|
+
if (node.community_id && node.community_id !== 0) {
|
|
51
|
+
return getSigmaClusterColor(node.community_id);
|
|
52
|
+
}
|
|
53
|
+
// Priority 2: fact_type category coloring
|
|
54
|
+
if (node.category && CATEGORY_COLORS[node.category]) {
|
|
55
|
+
return CATEGORY_COLORS[node.category];
|
|
56
|
+
}
|
|
57
|
+
return '#667eea'; // Default indigo
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// RENDERER CHECK — Only activate if user chose Sigma
|
|
62
|
+
// ============================================================================
|
|
63
|
+
|
|
64
|
+
function isSigmaRenderer() {
|
|
65
|
+
// v3.4.1: Sigma.js is the ONLY renderer. Cytoscape removed.
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ============================================================================
|
|
70
|
+
// GRAPH DATA TRANSFORMATION (API response → graphology format)
|
|
71
|
+
// ============================================================================
|
|
72
|
+
|
|
73
|
+
function transformDataForSigma(data) {
|
|
74
|
+
// Import graphology from ESM module (loaded in index.html)
|
|
75
|
+
if (typeof graphology === 'undefined') {
|
|
76
|
+
console.error('[Sigma] graphology not loaded');
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
var graph = new graphology.Graph({ multi: false, type: 'undirected' });
|
|
80
|
+
|
|
81
|
+
var nodes = data.nodes || [];
|
|
82
|
+
var links = data.links || [];
|
|
83
|
+
var nodeCount = nodes.length;
|
|
84
|
+
|
|
85
|
+
// Compute degree for sizing
|
|
86
|
+
var degreeMap = {};
|
|
87
|
+
links.forEach(function(link) {
|
|
88
|
+
var s = String(link.source);
|
|
89
|
+
var t = String(link.target);
|
|
90
|
+
degreeMap[s] = (degreeMap[s] || 0) + 1;
|
|
91
|
+
degreeMap[t] = (degreeMap[t] || 0) + 1;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Add nodes with random initial positions (ForceAtlas2 will refine)
|
|
95
|
+
var spread = Math.sqrt(nodeCount) * 40;
|
|
96
|
+
nodes.forEach(function(node, i) {
|
|
97
|
+
var id = String(node.id);
|
|
98
|
+
var degree = degreeMap[id] || 0;
|
|
99
|
+
var importance = node.importance || 0.5;
|
|
100
|
+
var communityId = node.community_id || 0;
|
|
101
|
+
|
|
102
|
+
// Golden angle distribution for initial positions
|
|
103
|
+
var angle = i * (Math.PI * (3 - Math.sqrt(5)));
|
|
104
|
+
var radius = spread * Math.sqrt((i + 1) / nodeCount);
|
|
105
|
+
|
|
106
|
+
// Node size: blend of degree and importance
|
|
107
|
+
var size = Math.max(3, Math.min(20, 3 + degree * 1.5 + importance * 8));
|
|
108
|
+
|
|
109
|
+
// Label: first 4 words of content
|
|
110
|
+
var contentPreview = node.content_preview || node.content || '';
|
|
111
|
+
var label = contentPreview.split(/\s+/).slice(0, 4).join(' ') || node.category || 'Memory';
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
graph.addNode(id, {
|
|
115
|
+
x: Math.cos(angle) * radius,
|
|
116
|
+
y: Math.sin(angle) * radius,
|
|
117
|
+
size: size,
|
|
118
|
+
color: getNodeColor(node),
|
|
119
|
+
label: label,
|
|
120
|
+
// SLM-specific data (for detail panel)
|
|
121
|
+
slm_content: node.content || '',
|
|
122
|
+
slm_content_preview: contentPreview,
|
|
123
|
+
slm_category: node.category || '',
|
|
124
|
+
slm_project_name: node.project_name || '',
|
|
125
|
+
slm_importance: importance,
|
|
126
|
+
slm_community_id: communityId,
|
|
127
|
+
slm_pagerank: node.pagerank_score || 0,
|
|
128
|
+
slm_degree_centrality: node.degree_centrality || 0,
|
|
129
|
+
slm_created_at: node.created_at || '',
|
|
130
|
+
slm_entities: node.entities || [],
|
|
131
|
+
});
|
|
132
|
+
} catch (e) {
|
|
133
|
+
// Skip duplicate node IDs silently
|
|
134
|
+
if (!e.message.includes('already exist')) {
|
|
135
|
+
console.warn('[Sigma] Node add error:', e.message);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Add edges
|
|
141
|
+
links.forEach(function(link) {
|
|
142
|
+
var sourceId = String(link.source);
|
|
143
|
+
var targetId = String(link.target);
|
|
144
|
+
try {
|
|
145
|
+
if (graph.hasNode(sourceId) && graph.hasNode(targetId)) {
|
|
146
|
+
graph.addEdge(sourceId, targetId, {
|
|
147
|
+
weight: link.weight || 0.5,
|
|
148
|
+
color: getEdgeColor(link.relationship_type),
|
|
149
|
+
size: Math.max(0.5, (link.weight || 0.5) * 2),
|
|
150
|
+
slm_type: link.relationship_type || 'entity',
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
} catch (e) {
|
|
154
|
+
// Skip duplicate edges silently
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return graph;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function getEdgeColor(type) {
|
|
162
|
+
var colors = {
|
|
163
|
+
'entity': '#cccccc',
|
|
164
|
+
'temporal': '#4facfe',
|
|
165
|
+
'semantic': '#667eea',
|
|
166
|
+
'causal': '#43e97b',
|
|
167
|
+
'contradiction': '#f5576c',
|
|
168
|
+
'supersedes': '#f093fb',
|
|
169
|
+
};
|
|
170
|
+
return colors[type] || '#cccccc';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ============================================================================
|
|
174
|
+
// LAYOUT — ForceAtlas2 (synchronous for <500 nodes, batched for larger)
|
|
175
|
+
// ============================================================================
|
|
176
|
+
|
|
177
|
+
function runSigmaLayout(graph) {
|
|
178
|
+
if (typeof graphologyLibrary === 'undefined') {
|
|
179
|
+
console.warn('[Sigma] graphologyLibrary not loaded, skipping layout');
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
var nodeCount = graph.order;
|
|
183
|
+
var settings = graphologyLibrary.layoutForceAtlas2.inferSettings(graph);
|
|
184
|
+
settings.barnesHutOptimize = nodeCount > 500;
|
|
185
|
+
settings.slowDown = nodeCount > 1000 ? 5 : 1;
|
|
186
|
+
|
|
187
|
+
var iterations = nodeCount > 5000 ? 30 : nodeCount > 2000 ? 50 : nodeCount > 500 ? 100 : 200;
|
|
188
|
+
graphologyLibrary.layoutForceAtlas2.assign(graph, {
|
|
189
|
+
iterations: iterations,
|
|
190
|
+
settings: settings,
|
|
191
|
+
});
|
|
192
|
+
console.log('[Sigma] ForceAtlas2 done:', nodeCount, 'nodes,', iterations, 'iterations');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ============================================================================
|
|
196
|
+
// RENDER — Main entry point for Sigma.js graph
|
|
197
|
+
// ============================================================================
|
|
198
|
+
|
|
199
|
+
function renderSigmaGraph(data) {
|
|
200
|
+
if (typeof Sigma === 'undefined') {
|
|
201
|
+
console.error('[Sigma] Sigma.js not loaded — check CDN');
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
var container = document.getElementById('graph-container');
|
|
206
|
+
if (!container) return;
|
|
207
|
+
|
|
208
|
+
// CRITICAL: Don't render if container is hidden (Bootstrap tab not visible)
|
|
209
|
+
// Sigma.js needs real pixel dimensions to create WebGL canvases.
|
|
210
|
+
if (container.offsetWidth === 0 || container.offsetHeight === 0) {
|
|
211
|
+
console.log('[Sigma] Container hidden (0 dimensions), deferring render');
|
|
212
|
+
// Store data for deferred render when tab becomes visible
|
|
213
|
+
window._sigmaPendingData = data;
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Destroy previous instance
|
|
218
|
+
if (sigmaInstance) {
|
|
219
|
+
try { sigmaInstance.kill(); } catch (e) { /* ok */ }
|
|
220
|
+
sigmaInstance = null;
|
|
221
|
+
sigmaGraph = null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Clear container
|
|
225
|
+
container.textContent = '';
|
|
226
|
+
|
|
227
|
+
var nodes = data.nodes || [];
|
|
228
|
+
if (nodes.length === 0) {
|
|
229
|
+
var emptyMsg = document.createElement('div');
|
|
230
|
+
emptyMsg.style.cssText = 'text-align:center; padding:50px; color:#666;';
|
|
231
|
+
emptyMsg.textContent = 'No memories found. Try adjusting filters.';
|
|
232
|
+
container.appendChild(emptyMsg);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Transform and layout
|
|
237
|
+
sigmaGraph = transformDataForSigma(data);
|
|
238
|
+
if (!sigmaGraph) return;
|
|
239
|
+
|
|
240
|
+
runSigmaLayout(sigmaGraph);
|
|
241
|
+
|
|
242
|
+
// Create Sigma renderer
|
|
243
|
+
// allowInvalidContainer: Bootstrap tabs have display:none on inactive panes,
|
|
244
|
+
// so the container has 0 width on first render. Sigma will resize on refresh().
|
|
245
|
+
sigmaInstance = new Sigma(sigmaGraph, container, {
|
|
246
|
+
allowInvalidContainer: true,
|
|
247
|
+
// Rendering
|
|
248
|
+
renderLabels: true,
|
|
249
|
+
labelDensity: 0.5,
|
|
250
|
+
labelRenderedSizeThreshold: 8,
|
|
251
|
+
labelFont: "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif",
|
|
252
|
+
labelSize: 11,
|
|
253
|
+
labelColor: { color: '#333333' },
|
|
254
|
+
// Performance — scale with node count
|
|
255
|
+
hideEdgesOnMove: nodes.length > 300,
|
|
256
|
+
hideLabelsOnMove: nodes.length > 1000,
|
|
257
|
+
enableEdgeEvents: false,
|
|
258
|
+
labelGridCellSize: nodes.length > 1000 ? 200 : 100,
|
|
259
|
+
// Appearance
|
|
260
|
+
defaultNodeColor: '#999999',
|
|
261
|
+
defaultEdgeColor: '#cccccc',
|
|
262
|
+
stagePadding: 40,
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// Force resize + camera fit after Bootstrap tab transition completes
|
|
266
|
+
requestAnimationFrame(function() {
|
|
267
|
+
setTimeout(function() {
|
|
268
|
+
if (sigmaInstance) {
|
|
269
|
+
sigmaInstance.refresh();
|
|
270
|
+
// Auto-fit camera to show all nodes in viewport
|
|
271
|
+
sigmaInstance.getCamera().animatedReset({ duration: 300 });
|
|
272
|
+
}
|
|
273
|
+
}, 200);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// Node/Edge reducers for hover + search + community highlighting
|
|
277
|
+
sigmaInstance.setSetting('nodeReducer', function(node, data) {
|
|
278
|
+
var res = Object.assign({}, data);
|
|
279
|
+
var state = sigmaState;
|
|
280
|
+
|
|
281
|
+
// v3.4.1: Apply frontend Louvain community color when in live mode
|
|
282
|
+
if (communitySource === 'live' && sigmaGraph.hasNode(node)) {
|
|
283
|
+
var louvainComm = sigmaGraph.getNodeAttribute(node, 'community');
|
|
284
|
+
if (louvainComm !== undefined && louvainComm !== null) {
|
|
285
|
+
res.color = SIGMA_CLUSTER_COLORS[louvainComm % SIGMA_CLUSTER_COLORS.length];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Search highlighting
|
|
290
|
+
if (state.searchQuery && state.suggestions.size > 0) {
|
|
291
|
+
if (!state.suggestions.has(node)) {
|
|
292
|
+
res.color = '#e0e0e0';
|
|
293
|
+
res.label = '';
|
|
294
|
+
res.zIndex = 0;
|
|
295
|
+
} else {
|
|
296
|
+
res.highlighted = true;
|
|
297
|
+
res.zIndex = 1;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Hover highlighting
|
|
302
|
+
if (state.hoveredNode) {
|
|
303
|
+
if (node === state.hoveredNode) {
|
|
304
|
+
res.highlighted = true;
|
|
305
|
+
res.zIndex = 2;
|
|
306
|
+
} else if (sigmaGraph.hasNode(state.hoveredNode) && sigmaGraph.neighbors(state.hoveredNode).indexOf(node) !== -1) {
|
|
307
|
+
res.highlighted = true;
|
|
308
|
+
res.zIndex = 1;
|
|
309
|
+
} else if (!state.searchQuery) {
|
|
310
|
+
res.color = '#e0e0e0';
|
|
311
|
+
res.label = '';
|
|
312
|
+
res.zIndex = 0;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Selected node
|
|
317
|
+
if (state.selectedNode === node) {
|
|
318
|
+
res.highlighted = true;
|
|
319
|
+
res.zIndex = 3;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Event bus highlights (Phase 3)
|
|
323
|
+
if (state.highlightedNodes.has(node)) {
|
|
324
|
+
res.highlighted = true;
|
|
325
|
+
res.color = '#ff6b6b';
|
|
326
|
+
res.zIndex = 2;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return res;
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
sigmaInstance.setSetting('edgeReducer', function(edge, data) {
|
|
333
|
+
var res = Object.assign({}, data);
|
|
334
|
+
var state = sigmaState;
|
|
335
|
+
|
|
336
|
+
if (state.hoveredNode && sigmaGraph.hasNode(state.hoveredNode)) {
|
|
337
|
+
var extremities = sigmaGraph.extremities(edge);
|
|
338
|
+
if (extremities.indexOf(state.hoveredNode) === -1) {
|
|
339
|
+
res.hidden = true;
|
|
340
|
+
} else {
|
|
341
|
+
res.color = '#667eea';
|
|
342
|
+
res.size = Math.max(res.size, 2);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (state.searchQuery && state.suggestions.size > 0) {
|
|
347
|
+
var ext = sigmaGraph.extremities(edge);
|
|
348
|
+
if (!state.suggestions.has(ext[0]) && !state.suggestions.has(ext[1])) {
|
|
349
|
+
res.hidden = true;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return res;
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// Event handlers
|
|
357
|
+
sigmaInstance.on('enterNode', function(event) {
|
|
358
|
+
sigmaState.hoveredNode = event.node;
|
|
359
|
+
sigmaInstance.refresh();
|
|
360
|
+
showSigmaTooltip(event.node, event.event);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
sigmaInstance.on('leaveNode', function() {
|
|
364
|
+
sigmaState.hoveredNode = null;
|
|
365
|
+
sigmaInstance.refresh();
|
|
366
|
+
hideSigmaTooltip();
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
sigmaInstance.on('clickNode', function(event) {
|
|
370
|
+
sigmaState.selectedNode = event.node;
|
|
371
|
+
sigmaInstance.refresh();
|
|
372
|
+
openSigmaNodeDetail(event.node);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
sigmaInstance.on('doubleClickNode', function(event) {
|
|
376
|
+
// Double-click → ask chat about this node
|
|
377
|
+
var attrs = sigmaGraph.getNodeAttributes(event.node);
|
|
378
|
+
var label = attrs.label || '';
|
|
379
|
+
if (window.SLMEventBus && label) {
|
|
380
|
+
SLMEventBus.publish('slm:chat:queryAbout', {
|
|
381
|
+
query: 'Tell me everything about: ' + label,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
sigmaInstance.on('clickStage', function() {
|
|
387
|
+
sigmaState.selectedNode = null;
|
|
388
|
+
sigmaState.highlightedNodes.clear();
|
|
389
|
+
sigmaInstance.refresh();
|
|
390
|
+
hideSigmaTooltip();
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
// Update stats
|
|
394
|
+
if (typeof updateGraphStats === 'function') {
|
|
395
|
+
updateGraphStats(data);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Update panels
|
|
399
|
+
updateSigmaStatsPanel(data);
|
|
400
|
+
|
|
401
|
+
// v3.4.1: Run frontend Louvain by default (Live mode), fallback to backend
|
|
402
|
+
if (communitySource === 'live') {
|
|
403
|
+
var resolution = parseFloat((document.getElementById('community-resolution') || {}).value) || 1.0;
|
|
404
|
+
detectCommunitiesInBrowser(resolution);
|
|
405
|
+
} else {
|
|
406
|
+
loadCommunities();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
console.log('[Sigma] Rendered', sigmaGraph.order, 'nodes,', sigmaGraph.size, 'edges');
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// ============================================================================
|
|
413
|
+
// TOOLTIP
|
|
414
|
+
// ============================================================================
|
|
415
|
+
|
|
416
|
+
function showSigmaTooltip(nodeId, mouseEvent) {
|
|
417
|
+
if (!sigmaGraph || !sigmaGraph.hasNode(nodeId)) return;
|
|
418
|
+
var attrs = sigmaGraph.getNodeAttributes(nodeId);
|
|
419
|
+
var tooltip = document.getElementById('sigma-tooltip');
|
|
420
|
+
if (!tooltip) {
|
|
421
|
+
tooltip = document.createElement('div');
|
|
422
|
+
tooltip.id = 'sigma-tooltip';
|
|
423
|
+
tooltip.style.cssText = 'position:fixed;z-index:9999;background:#fff;border:1px solid #dee2e6;'
|
|
424
|
+
+ 'border-radius:8px;padding:10px 14px;box-shadow:0 4px 12px rgba(0,0,0,0.15);'
|
|
425
|
+
+ 'max-width:300px;pointer-events:none;font-size:13px;';
|
|
426
|
+
document.body.appendChild(tooltip);
|
|
427
|
+
}
|
|
428
|
+
var preview = (attrs.slm_content_preview || '').substring(0, 100);
|
|
429
|
+
tooltip.innerHTML = '<strong>' + escapeHtml(attrs.label || '') + '</strong>'
|
|
430
|
+
+ '<div class="text-muted small mt-1">' + escapeHtml(preview) + '</div>'
|
|
431
|
+
+ '<div class="mt-1"><span class="badge bg-primary me-1">' + escapeHtml(attrs.slm_category || '') + '</span>'
|
|
432
|
+
+ '<span class="badge bg-secondary">Trust: ' + (attrs.slm_importance || 0).toFixed(2) + '</span></div>';
|
|
433
|
+
tooltip.style.display = 'block';
|
|
434
|
+
|
|
435
|
+
if (mouseEvent && mouseEvent.original) {
|
|
436
|
+
tooltip.style.left = (mouseEvent.original.clientX + 15) + 'px';
|
|
437
|
+
tooltip.style.top = (mouseEvent.original.clientY + 15) + 'px';
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function hideSigmaTooltip() {
|
|
442
|
+
var tooltip = document.getElementById('sigma-tooltip');
|
|
443
|
+
if (tooltip) tooltip.style.display = 'none';
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// ============================================================================
|
|
447
|
+
// NODE DETAIL PANEL (reuses existing openMemoryDetail from modal.js)
|
|
448
|
+
// ============================================================================
|
|
449
|
+
|
|
450
|
+
function openSigmaNodeDetail(nodeId) {
|
|
451
|
+
if (!sigmaGraph || !sigmaGraph.hasNode(nodeId)) return;
|
|
452
|
+
var attrs = sigmaGraph.getNodeAttributes(nodeId);
|
|
453
|
+
|
|
454
|
+
// Populate right panel instead of opening modal
|
|
455
|
+
var panel = document.getElementById('sigma-detail-content');
|
|
456
|
+
if (panel) {
|
|
457
|
+
var neighbors = sigmaGraph.neighbors(nodeId);
|
|
458
|
+
var neighborList = neighbors.slice(0, 10).map(function(nid) {
|
|
459
|
+
var na = sigmaGraph.getNodeAttributes(nid);
|
|
460
|
+
return '<div class="border-bottom py-1 cursor-pointer" onclick="sigmaHighlightNode(\'' + nid + '\')">'
|
|
461
|
+
+ '<small class="text-primary">' + escapeHtml((na.label || '').substring(0, 40)) + '</small>'
|
|
462
|
+
+ '</div>';
|
|
463
|
+
}).join('');
|
|
464
|
+
|
|
465
|
+
panel.innerHTML = ''
|
|
466
|
+
+ '<div class="mb-2">'
|
|
467
|
+
+ '<span class="badge" style="background:' + attrs.color + '">' + escapeHtml(attrs.slm_category || 'memory') + '</span>'
|
|
468
|
+
+ ' <span class="badge bg-secondary">Trust: ' + (attrs.slm_importance || 0).toFixed(2) + '</span>'
|
|
469
|
+
+ '</div>'
|
|
470
|
+
+ '<div class="mb-2" style="line-height:1.5;">' + escapeHtml(attrs.slm_content || attrs.slm_content_preview || '') + '</div>'
|
|
471
|
+
+ '<div class="text-muted small mb-2">'
|
|
472
|
+
+ '<i class="bi bi-clock"></i> ' + escapeHtml(attrs.slm_created_at || 'Unknown')
|
|
473
|
+
+ ' • <i class="bi bi-diagram-3"></i> ' + neighbors.length + ' connections'
|
|
474
|
+
+ ' • PageRank: ' + (attrs.slm_pagerank || 0).toFixed(4)
|
|
475
|
+
+ '</div>'
|
|
476
|
+
+ '<hr class="my-2">'
|
|
477
|
+
+ '<h6 class="mb-1">Connected (' + neighbors.length + ')</h6>'
|
|
478
|
+
+ '<div style="max-height:250px;overflow-y:auto;">' + (neighborList || '<span class="text-muted">No connections</span>') + '</div>'
|
|
479
|
+
+ '<hr class="my-2">'
|
|
480
|
+
+ '<button class="btn btn-sm btn-outline-primary w-100" onclick="openMemoryDetail({id:\'' + nodeId + '\',content:\'' + escapeHtml((attrs.slm_content || '').substring(0, 80).replace(/'/g, "\\'")) + '\',category:\'' + (attrs.slm_category || '') + '\',importance:' + (attrs.slm_importance || 0.5) + '},\'graph\')"><i class="bi bi-box-arrow-up-right"></i> Full Detail</button>';
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// Stats panel update
|
|
485
|
+
function updateSigmaStatsPanel(data) {
|
|
486
|
+
var panel = document.getElementById('sigma-stats-panel');
|
|
487
|
+
if (!panel) return;
|
|
488
|
+
var nodes = data.nodes || [];
|
|
489
|
+
var links = data.links || [];
|
|
490
|
+
var categories = {};
|
|
491
|
+
nodes.forEach(function(n) {
|
|
492
|
+
var cat = n.category || 'unknown';
|
|
493
|
+
categories[cat] = (categories[cat] || 0) + 1;
|
|
494
|
+
});
|
|
495
|
+
var catHtml = Object.keys(categories).map(function(k) {
|
|
496
|
+
return '<div>' + k + ': <strong>' + categories[k] + '</strong></div>';
|
|
497
|
+
}).join('');
|
|
498
|
+
panel.innerHTML = '<div>Nodes: <strong>' + nodes.length + '</strong></div>'
|
|
499
|
+
+ '<div>Edges: <strong>' + links.length + '</strong></div>'
|
|
500
|
+
+ '<hr class="my-1">' + catHtml;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// Category filter
|
|
504
|
+
function sigmaFilterByCategory(category) {
|
|
505
|
+
if (!sigmaGraph || !sigmaInstance) return;
|
|
506
|
+
sigmaState.searchQuery = '';
|
|
507
|
+
sigmaState.suggestions.clear();
|
|
508
|
+
|
|
509
|
+
if (category) {
|
|
510
|
+
sigmaGraph.forEachNode(function(nodeId, attrs) {
|
|
511
|
+
if (attrs.slm_category === category) {
|
|
512
|
+
sigmaState.suggestions.add(nodeId);
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
sigmaState.searchQuery = '__filter__'; // trigger reducer
|
|
516
|
+
}
|
|
517
|
+
sigmaInstance.refresh();
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// ============================================================================
|
|
521
|
+
// SEARCH (filter nodes by label match)
|
|
522
|
+
// ============================================================================
|
|
523
|
+
|
|
524
|
+
function sigmaSearch(query) {
|
|
525
|
+
if (!sigmaGraph || !sigmaInstance) return;
|
|
526
|
+
sigmaState.searchQuery = query.trim().toLowerCase();
|
|
527
|
+
sigmaState.suggestions.clear();
|
|
528
|
+
|
|
529
|
+
if (sigmaState.searchQuery) {
|
|
530
|
+
sigmaGraph.forEachNode(function(nodeId, attrs) {
|
|
531
|
+
var label = (attrs.label || '').toLowerCase();
|
|
532
|
+
var content = (attrs.slm_content_preview || '').toLowerCase();
|
|
533
|
+
if (label.indexOf(sigmaState.searchQuery) !== -1 ||
|
|
534
|
+
content.indexOf(sigmaState.searchQuery) !== -1) {
|
|
535
|
+
sigmaState.suggestions.add(nodeId);
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
sigmaInstance.refresh();
|
|
541
|
+
|
|
542
|
+
// If exactly one match, focus camera on it
|
|
543
|
+
if (sigmaState.suggestions.size === 1) {
|
|
544
|
+
var matchedNode = sigmaState.suggestions.values().next().value;
|
|
545
|
+
var pos = sigmaGraph.getNodeAttributes(matchedNode);
|
|
546
|
+
sigmaInstance.getCamera().animate({ x: pos.x, y: pos.y, ratio: 0.3 }, { duration: 500 });
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return sigmaState.suggestions.size;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function sigmaClearSearch() {
|
|
553
|
+
sigmaState.searchQuery = '';
|
|
554
|
+
sigmaState.suggestions.clear();
|
|
555
|
+
if (sigmaInstance) sigmaInstance.refresh();
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// ============================================================================
|
|
559
|
+
// CAMERA CONTROLS
|
|
560
|
+
// ============================================================================
|
|
561
|
+
|
|
562
|
+
function sigmaZoomIn() {
|
|
563
|
+
if (sigmaInstance) sigmaInstance.getCamera().animatedZoom({ duration: 300 });
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function sigmaZoomOut() {
|
|
567
|
+
if (sigmaInstance) sigmaInstance.getCamera().animatedUnzoom({ duration: 300 });
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function sigmaResetView() {
|
|
571
|
+
if (sigmaInstance) sigmaInstance.getCamera().animatedReset({ duration: 500 });
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// ============================================================================
|
|
575
|
+
// HIGHLIGHT NODE (for event bus — Phase 3)
|
|
576
|
+
// ============================================================================
|
|
577
|
+
|
|
578
|
+
function sigmaHighlightNode(factId) {
|
|
579
|
+
if (!sigmaGraph || !sigmaInstance) return;
|
|
580
|
+
var nodeId = String(factId);
|
|
581
|
+
if (!sigmaGraph.hasNode(nodeId)) return;
|
|
582
|
+
|
|
583
|
+
sigmaState.highlightedNodes.clear();
|
|
584
|
+
sigmaState.highlightedNodes.add(nodeId);
|
|
585
|
+
sigmaInstance.refresh();
|
|
586
|
+
|
|
587
|
+
// Focus camera
|
|
588
|
+
var pos = sigmaGraph.getNodeAttributes(nodeId);
|
|
589
|
+
sigmaInstance.getCamera().animate({ x: pos.x, y: pos.y, ratio: 0.4 }, { duration: 400 });
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// ============================================================================
|
|
593
|
+
// INTEGRATION: Override loadGraph() to route to Sigma when active
|
|
594
|
+
// ============================================================================
|
|
595
|
+
|
|
596
|
+
// Store original loadGraph (from graph-core.js) before overriding
|
|
597
|
+
var _originalLoadGraph = (typeof loadGraph === 'function') ? loadGraph : null;
|
|
598
|
+
|
|
599
|
+
// This runs AFTER graph-core.js is loaded (script order in index.html)
|
|
600
|
+
function loadGraphSigma() {
|
|
601
|
+
if (!isSigmaRenderer()) {
|
|
602
|
+
// Delegate to Cytoscape
|
|
603
|
+
if (_originalLoadGraph) _originalLoadGraph();
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
var maxNodes = 100;
|
|
608
|
+
var maxNodesEl = document.getElementById('graph-max-nodes');
|
|
609
|
+
if (maxNodesEl) maxNodes = parseInt(maxNodesEl.value) || 100;
|
|
610
|
+
|
|
611
|
+
var minImportance = 1;
|
|
612
|
+
var minImpEl = document.getElementById('graph-min-importance');
|
|
613
|
+
if (minImpEl) minImportance = parseInt(minImpEl.value) || 1;
|
|
614
|
+
|
|
615
|
+
// Apply cluster filter for larger fetch
|
|
616
|
+
var fetchLimit = (typeof filterState !== 'undefined' && filterState.cluster_id) ? 200 : maxNodes;
|
|
617
|
+
|
|
618
|
+
if (typeof showLoadingSpinner === 'function') showLoadingSpinner();
|
|
619
|
+
|
|
620
|
+
fetch('/api/graph?max_nodes=' + fetchLimit + '&min_importance=' + minImportance)
|
|
621
|
+
.then(function(r) { return r.json(); })
|
|
622
|
+
.then(function(data) {
|
|
623
|
+
// Store in shared globals
|
|
624
|
+
if (typeof window.graphData !== 'undefined') window.graphData = data;
|
|
625
|
+
if (typeof window.originalGraphData !== 'undefined') {
|
|
626
|
+
window.originalGraphData = JSON.parse(JSON.stringify(data));
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// Apply filters if set
|
|
630
|
+
if (typeof filterState !== 'undefined') {
|
|
631
|
+
if (filterState.cluster_id && typeof filterByCluster === 'function') {
|
|
632
|
+
data = filterByCluster(window.originalGraphData || data, filterState.cluster_id);
|
|
633
|
+
}
|
|
634
|
+
if (filterState.entity && typeof filterByEntity === 'function') {
|
|
635
|
+
data = filterByEntity(window.originalGraphData || data, filterState.entity);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
renderSigmaGraph(data);
|
|
640
|
+
if (typeof hideLoadingSpinner === 'function') hideLoadingSpinner();
|
|
641
|
+
if (typeof updateFilterBadge === 'function') updateFilterBadge();
|
|
642
|
+
})
|
|
643
|
+
.catch(function(error) {
|
|
644
|
+
console.error('[Sigma] Load error:', error);
|
|
645
|
+
if (typeof showError === 'function') showError('Failed to load graph: ' + error.message);
|
|
646
|
+
if (typeof hideLoadingSpinner === 'function') hideLoadingSpinner();
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// ============================================================================
|
|
651
|
+
// RENDERER TOGGLE
|
|
652
|
+
// ============================================================================
|
|
653
|
+
|
|
654
|
+
// ============================================================================
|
|
655
|
+
// COMMUNITY DETECTION (v3.4.1: Frontend Louvain + Backend Leiden/LP)
|
|
656
|
+
// ============================================================================
|
|
657
|
+
|
|
658
|
+
var communitySource = 'live'; // 'live' (frontend Louvain) or 'backend' (API)
|
|
659
|
+
var communityResolutionTimer = null;
|
|
660
|
+
var liveCommunityMap = null; // { communityId: [nodeIds] }
|
|
661
|
+
|
|
662
|
+
// ── Frontend Louvain (in-browser, real-time) ─────────────────────
|
|
663
|
+
|
|
664
|
+
function detectCommunitiesInBrowser(resolution) {
|
|
665
|
+
if (!sigmaGraph || sigmaGraph.order === 0) return;
|
|
666
|
+
resolution = resolution || 1.0;
|
|
667
|
+
|
|
668
|
+
var t0 = performance.now();
|
|
669
|
+
try {
|
|
670
|
+
// graphology-library UMD exposes community methods
|
|
671
|
+
var louvainFn = null;
|
|
672
|
+
if (window.graphologyLibrary) {
|
|
673
|
+
var cl = window.graphologyLibrary.communitiesLouvain;
|
|
674
|
+
louvainFn = cl ? (cl.assign || cl) : null;
|
|
675
|
+
}
|
|
676
|
+
if (!louvainFn) {
|
|
677
|
+
console.warn('[knowledge-graph] graphology-communities-louvain not available, using backend communities');
|
|
678
|
+
handleCommunitySourceToggle('backend');
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// Louvain assign mutates the graph, adding 'community' attribute
|
|
683
|
+
louvainFn(sigmaGraph, { resolution: resolution });
|
|
684
|
+
|
|
685
|
+
// Build community map for panel rendering
|
|
686
|
+
liveCommunityMap = {};
|
|
687
|
+
sigmaGraph.forEachNode(function(nodeId, attrs) {
|
|
688
|
+
var commId = attrs.community;
|
|
689
|
+
if (commId === undefined || commId === null) commId = 0;
|
|
690
|
+
if (!liveCommunityMap[commId]) liveCommunityMap[commId] = [];
|
|
691
|
+
liveCommunityMap[commId].push(nodeId);
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
// Refresh Sigma to pick up community colors via nodeReducer
|
|
695
|
+
if (sigmaInstance) sigmaInstance.refresh();
|
|
696
|
+
|
|
697
|
+
var dt = Math.round(performance.now() - t0);
|
|
698
|
+
var commCount = Object.keys(liveCommunityMap).length;
|
|
699
|
+
console.log('[knowledge-graph] Frontend Louvain: ' + commCount + ' communities at resolution ' + resolution.toFixed(1) + ' (' + dt + 'ms)');
|
|
700
|
+
|
|
701
|
+
// Render the community list panel
|
|
702
|
+
renderLiveCommunityPanel(liveCommunityMap);
|
|
703
|
+
|
|
704
|
+
// Warn about edge cases
|
|
705
|
+
var nodeCount = sigmaGraph.order;
|
|
706
|
+
if (commCount === 1) {
|
|
707
|
+
showToast('All nodes in one community. Try increasing resolution.', 'info');
|
|
708
|
+
} else if (commCount >= nodeCount * 0.9) {
|
|
709
|
+
showToast('Nearly every node is its own community. Lower resolution.', 'warning');
|
|
710
|
+
} else if (commCount > 100) {
|
|
711
|
+
showToast('Very fine resolution: ' + commCount + ' communities.', 'info');
|
|
712
|
+
}
|
|
713
|
+
} catch (e) {
|
|
714
|
+
console.warn('[knowledge-graph] Frontend Louvain failed, falling back to backend:', e.message);
|
|
715
|
+
handleCommunitySourceToggle('backend');
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function renderLiveCommunityPanel(communityMap) {
|
|
720
|
+
var panel = document.getElementById('community-list-panel');
|
|
721
|
+
if (!panel || !communityMap) return;
|
|
722
|
+
|
|
723
|
+
var entries = Object.keys(communityMap).map(function(cid) {
|
|
724
|
+
return { community_id: parseInt(cid, 10), members: communityMap[cid] };
|
|
725
|
+
}).sort(function(a, b) { return b.members.length - a.members.length; });
|
|
726
|
+
|
|
727
|
+
if (entries.length === 0) {
|
|
728
|
+
panel.innerHTML = '<div class="text-muted small">No communities detected.</div>';
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
var html = '';
|
|
733
|
+
entries.forEach(function(c) {
|
|
734
|
+
var color = SIGMA_CLUSTER_COLORS[c.community_id % SIGMA_CLUSTER_COLORS.length];
|
|
735
|
+
// Generate simple label from most common entity
|
|
736
|
+
var label = generateLiveLabel(c.community_id, c.members);
|
|
737
|
+
html += '<div class="d-flex align-items-center mb-1 cursor-pointer" '
|
|
738
|
+
+ 'onclick="sigmaFilterByCommunity(' + c.community_id + ', \'' + communitySource + '\')" '
|
|
739
|
+
+ 'title="' + c.members.length + ' memories">'
|
|
740
|
+
+ '<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:' + color + ';flex-shrink:0;"></span>'
|
|
741
|
+
+ '<span class="ms-1 text-truncate" style="font-size:0.75rem;">' + escapeHtml(label) + '</span>'
|
|
742
|
+
+ '<span class="badge bg-light text-dark ms-auto" style="font-size:0.65rem;">' + c.members.length + '</span>'
|
|
743
|
+
+ '</div>';
|
|
744
|
+
});
|
|
745
|
+
html += '<button class="btn btn-sm btn-outline-secondary w-100 mt-1" onclick="sigmaClearSearch()">'
|
|
746
|
+
+ '<i class="bi bi-x-circle"></i> Clear Filter</button>';
|
|
747
|
+
panel.innerHTML = html;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
function generateLiveLabel(communityId, memberNodeIds) {
|
|
751
|
+
// Use most common entity names from node attributes
|
|
752
|
+
if (!sigmaGraph || !memberNodeIds || memberNodeIds.length === 0) return 'Community ' + communityId;
|
|
753
|
+
var entityFreq = {};
|
|
754
|
+
memberNodeIds.slice(0, 20).forEach(function(nid) {
|
|
755
|
+
try {
|
|
756
|
+
var entities = sigmaGraph.getNodeAttribute(nid, 'slm_entities');
|
|
757
|
+
if (entities && Array.isArray(entities)) {
|
|
758
|
+
entities.forEach(function(e) {
|
|
759
|
+
entityFreq[e] = (entityFreq[e] || 0) + 1;
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
} catch (_) { /* skip */ }
|
|
763
|
+
});
|
|
764
|
+
var sorted = Object.keys(entityFreq).sort(function(a, b) { return entityFreq[b] - entityFreq[a]; });
|
|
765
|
+
return sorted.length > 0 ? sorted.slice(0, 3).join(', ') : 'Community ' + communityId;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// ── Backend Communities (API, consolidated) ──────────────────────
|
|
769
|
+
|
|
770
|
+
function loadCommunities() {
|
|
771
|
+
fetch('/api/v3/graph/communities')
|
|
772
|
+
.then(function(r) { return r.json(); })
|
|
773
|
+
.then(function(data) {
|
|
774
|
+
var panel = document.getElementById('community-list-panel');
|
|
775
|
+
if (!panel) return;
|
|
776
|
+
|
|
777
|
+
var communities = data.communities || [];
|
|
778
|
+
if (communities.length === 0) {
|
|
779
|
+
panel.innerHTML = '<div class="text-muted small">No communities detected yet.</div>'
|
|
780
|
+
+ '<button class="btn btn-sm btn-outline-primary w-100 mt-1" onclick="runCommunityDetection()">'
|
|
781
|
+
+ '<i class="bi bi-cpu"></i> Detect Communities</button>';
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
var html = '';
|
|
786
|
+
communities.forEach(function(c) {
|
|
787
|
+
var color = c.color || SIGMA_CLUSTER_COLORS[c.community_id % SIGMA_CLUSTER_COLORS.length];
|
|
788
|
+
html += '<div class="d-flex align-items-center mb-1 cursor-pointer" '
|
|
789
|
+
+ 'onclick="sigmaFilterByCommunity(' + c.community_id + ', \'backend\')" '
|
|
790
|
+
+ 'title="' + (c.top_entities || []).join(', ') + '">'
|
|
791
|
+
+ '<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:' + color + ';flex-shrink:0;"></span>'
|
|
792
|
+
+ '<span class="ms-1 text-truncate" style="font-size:0.75rem;">' + escapeHtml(c.label) + '</span>'
|
|
793
|
+
+ '<span class="badge bg-light text-dark ms-auto" style="font-size:0.65rem;">' + c.member_count + '</span>'
|
|
794
|
+
+ '</div>';
|
|
795
|
+
});
|
|
796
|
+
html += '<button class="btn btn-sm btn-outline-secondary w-100 mt-1" onclick="sigmaClearSearch()">'
|
|
797
|
+
+ '<i class="bi bi-x-circle"></i> Clear Filter</button>';
|
|
798
|
+
html += '<button class="btn btn-sm btn-outline-primary w-100 mt-1" onclick="runCommunityDetection()">'
|
|
799
|
+
+ '<i class="bi bi-arrow-clockwise"></i> Refresh</button>';
|
|
800
|
+
panel.innerHTML = html;
|
|
801
|
+
})
|
|
802
|
+
.catch(function() { /* silent */ });
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
function runCommunityDetection() {
|
|
806
|
+
var panel = document.getElementById('community-list-panel');
|
|
807
|
+
if (panel) panel.innerHTML = '<div class="text-center"><div class="spinner-border spinner-border-sm text-primary"></div> Detecting...</div>';
|
|
808
|
+
|
|
809
|
+
fetch('/api/v3/graph/run-communities', { method: 'POST' })
|
|
810
|
+
.then(function(r) { return r.json(); })
|
|
811
|
+
.then(function(data) {
|
|
812
|
+
if (data.success) {
|
|
813
|
+
loadCommunities();
|
|
814
|
+
loadGraphSigma();
|
|
815
|
+
} else {
|
|
816
|
+
if (panel) panel.innerHTML = '<div class="text-danger small">Failed: ' + (data.error || 'Unknown') + '</div>';
|
|
817
|
+
}
|
|
818
|
+
})
|
|
819
|
+
.catch(function(e) {
|
|
820
|
+
if (panel) panel.innerHTML = '<div class="text-danger small">Error: ' + e.message + '</div>';
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// ── Source Toggle + Resolution Slider ────────────────────────────
|
|
825
|
+
|
|
826
|
+
function handleCommunitySourceToggle(source) {
|
|
827
|
+
communitySource = source;
|
|
828
|
+
var slider = document.getElementById('community-resolution-container');
|
|
829
|
+
|
|
830
|
+
if (source === 'live') {
|
|
831
|
+
if (slider) slider.style.display = 'block';
|
|
832
|
+
// Set radio button state
|
|
833
|
+
var liveRadio = document.getElementById('community-live');
|
|
834
|
+
if (liveRadio) liveRadio.checked = true;
|
|
835
|
+
var resolution = parseFloat(document.getElementById('community-resolution').value) || 1.0;
|
|
836
|
+
detectCommunitiesInBrowser(resolution);
|
|
837
|
+
} else {
|
|
838
|
+
if (slider) slider.style.display = 'none';
|
|
839
|
+
var backendRadio = document.getElementById('community-backend');
|
|
840
|
+
if (backendRadio) backendRadio.checked = true;
|
|
841
|
+
loadCommunities();
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
function handleResolutionChange(value) {
|
|
846
|
+
var resolution = parseFloat(value);
|
|
847
|
+
var display = document.getElementById('community-resolution-value');
|
|
848
|
+
if (display) display.textContent = resolution.toFixed(1);
|
|
849
|
+
|
|
850
|
+
// Debounce 300ms
|
|
851
|
+
if (communityResolutionTimer) clearTimeout(communityResolutionTimer);
|
|
852
|
+
communityResolutionTimer = setTimeout(function() {
|
|
853
|
+
detectCommunitiesInBrowser(resolution);
|
|
854
|
+
}, 300);
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
// ── Community Filter ─────────────────────────────────────────────
|
|
858
|
+
|
|
859
|
+
function sigmaFilterByCommunity(communityId, source) {
|
|
860
|
+
if (!sigmaGraph || !sigmaInstance) return;
|
|
861
|
+
sigmaState.searchQuery = '__community__';
|
|
862
|
+
sigmaState.suggestions.clear();
|
|
863
|
+
|
|
864
|
+
if (source === 'live' && liveCommunityMap && liveCommunityMap[communityId]) {
|
|
865
|
+
// Filter by frontend Louvain assignment
|
|
866
|
+
liveCommunityMap[communityId].forEach(function(nid) {
|
|
867
|
+
sigmaState.suggestions.add(nid);
|
|
868
|
+
});
|
|
869
|
+
} else {
|
|
870
|
+
// Filter by backend community_id stored in node attributes
|
|
871
|
+
sigmaGraph.forEachNode(function(nodeId, attrs) {
|
|
872
|
+
if (attrs.slm_community_id === communityId) {
|
|
873
|
+
sigmaState.suggestions.add(nodeId);
|
|
874
|
+
}
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
sigmaInstance.refresh();
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
function updateRendererUI() {
|
|
882
|
+
// v3.4.1: Sigma.js is the only renderer. Panels always visible.
|
|
883
|
+
var engineName = document.getElementById('graph-engine-name');
|
|
884
|
+
if (engineName) engineName.textContent = 'Sigma.js WebGL';
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
// ============================================================================
|
|
888
|
+
// INIT — Override loadGraph + set up UI on page load
|
|
889
|
+
// ============================================================================
|
|
890
|
+
|
|
891
|
+
// Set global functions — these are called by modal.js, clusters.js, core.js
|
|
892
|
+
if (typeof window !== 'undefined') {
|
|
893
|
+
window.loadGraph = loadGraphSigma;
|
|
894
|
+
window.renderGraph = renderSigmaGraph; // modal.js/clusters.js call renderGraph(data)
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// escapeHtml — needed by tooltip and detail panel (was in graph-core.js)
|
|
898
|
+
if (typeof escapeHtml === 'undefined') {
|
|
899
|
+
function escapeHtml(text) {
|
|
900
|
+
var div = document.createElement('div');
|
|
901
|
+
div.textContent = text || '';
|
|
902
|
+
return div.innerHTML;
|
|
903
|
+
}
|
|
904
|
+
window.escapeHtml = escapeHtml;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
// getClusterColor — called by graph-ui.js for badge colors
|
|
908
|
+
if (typeof getClusterColor === 'undefined') {
|
|
909
|
+
window.getClusterColor = getSigmaClusterColor;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
// Initialize renderer UI on DOM ready + auto-scroll on tab switch
|
|
913
|
+
document.addEventListener('DOMContentLoaded', function() {
|
|
914
|
+
updateRendererUI();
|
|
915
|
+
|
|
916
|
+
// When Knowledge Graph tab becomes visible, render or refresh Sigma
|
|
917
|
+
var graphTab = document.getElementById('graph-tab');
|
|
918
|
+
if (graphTab) {
|
|
919
|
+
graphTab.addEventListener('shown.bs.tab', function() {
|
|
920
|
+
// Tab is now visible — container has real dimensions
|
|
921
|
+
setTimeout(function() {
|
|
922
|
+
if (sigmaInstance) {
|
|
923
|
+
// Already rendered — just refresh + center
|
|
924
|
+
sigmaInstance.refresh();
|
|
925
|
+
sigmaInstance.getCamera().animatedReset({ duration: 300 });
|
|
926
|
+
} else if (window._sigmaPendingData) {
|
|
927
|
+
// Deferred render — data was fetched while tab was hidden
|
|
928
|
+
console.log('[Sigma] Rendering deferred data');
|
|
929
|
+
renderSigmaGraph(window._sigmaPendingData);
|
|
930
|
+
window._sigmaPendingData = null;
|
|
931
|
+
} else {
|
|
932
|
+
// First time — trigger load
|
|
933
|
+
loadGraphSigma();
|
|
934
|
+
}
|
|
935
|
+
var container = document.getElementById('graph-container');
|
|
936
|
+
if (container) {
|
|
937
|
+
container.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
938
|
+
}
|
|
939
|
+
}, 100); // Wait for Bootstrap transition to complete
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
});
|