agent-knowledge 1.5.3 → 1.6.0

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.
@@ -0,0 +1,482 @@
1
+ /* agent-knowledge — graph tab rendering (vis.js) */
2
+ (function () {
3
+ 'use strict';
4
+
5
+ var K = window.Knowledge;
6
+ var network = null;
7
+ var allNodes = [];
8
+ var allEdges = [];
9
+ var physicsEnabled = true;
10
+ var activeEdgeFilter = 'all';
11
+ var hideOrphans = false;
12
+ var searchQuery = '';
13
+ var controlsVersion = 0;
14
+
15
+ var CATEGORY_COLORS = {
16
+ projects: '#7aafc5',
17
+ decisions: '#d4a574',
18
+ notes: '#8db87a',
19
+ workflows: '#b196cc',
20
+ people: '#cc8888',
21
+ };
22
+
23
+ var CATEGORY_LABELS = {
24
+ projects: 'Projects',
25
+ decisions: 'Decisions',
26
+ notes: 'Notes',
27
+ workflows: 'Workflows',
28
+ people: 'People',
29
+ };
30
+
31
+ var EDGE_COLORS = {
32
+ part_of: 'rgba(122,175,197,0.4)',
33
+ depends_on: 'rgba(212,165,116,0.4)',
34
+ related_to: 'rgba(160,160,160,0.3)',
35
+ builds_on: 'rgba(141,184,122,0.4)',
36
+ supersedes: 'rgba(204,136,136,0.4)',
37
+ contradicts: 'rgba(220,68,68,0.4)',
38
+ specializes: 'rgba(177,150,204,0.4)',
39
+ alternative_to: 'rgba(170,136,68,0.4)',
40
+ };
41
+
42
+ function getOptions(isDark) {
43
+ var textColor = isDark ? '#d0d0d0' : '#444';
44
+ var bgColor = isDark ? '#1a1a2e' : '#fff';
45
+ return {
46
+ nodes: {
47
+ shape: 'dot',
48
+ font: {
49
+ color: textColor,
50
+ size: 12,
51
+ face: 'Inter, sans-serif',
52
+ strokeWidth: 3,
53
+ strokeColor: bgColor,
54
+ },
55
+ borderWidth: 0,
56
+ shadow: false,
57
+ },
58
+ edges: {
59
+ font: { size: 0 },
60
+ arrows: { to: { enabled: false } },
61
+ smooth: { type: 'continuous', roundness: 0.15 },
62
+ width: 1,
63
+ hoverWidth: 0.5,
64
+ selectionWidth: 1,
65
+ },
66
+ physics: {
67
+ enabled: physicsEnabled,
68
+ barnesHut: {
69
+ gravitationalConstant: -6000,
70
+ centralGravity: 0.15,
71
+ springLength: 200,
72
+ springConstant: 0.02,
73
+ damping: 0.12,
74
+ avoidOverlap: 0.5,
75
+ },
76
+ stabilization: { iterations: 250, fit: true },
77
+ },
78
+ interaction: {
79
+ hover: true,
80
+ tooltipDelay: 100,
81
+ keyboard: { enabled: true },
82
+ zoomView: true,
83
+ dragView: true,
84
+ },
85
+ layout: { improvedLayout: true },
86
+ };
87
+ }
88
+
89
+ function connectedIds(edges) {
90
+ var s = new Set();
91
+ edges.forEach(function (e) {
92
+ s.add(e.from);
93
+ s.add(e.to);
94
+ });
95
+ return s;
96
+ }
97
+
98
+ function cleanEdge(e) {
99
+ return {
100
+ from: e.from,
101
+ to: e.to,
102
+ title: e.title || (e.rel_type || '').replace(/_/g, ' '),
103
+ rel_type: e.rel_type,
104
+ color: {
105
+ color: EDGE_COLORS[e.rel_type] || 'rgba(160,160,160,0.3)',
106
+ highlight: EDGE_COLORS[e.rel_type] || 'rgba(160,160,160,0.5)',
107
+ hover: EDGE_COLORS[e.rel_type] || 'rgba(160,160,160,0.5)',
108
+ },
109
+ };
110
+ }
111
+
112
+ function getVisibleData() {
113
+ var edges =
114
+ activeEdgeFilter === 'all'
115
+ ? allEdges
116
+ : allEdges.filter(function (e) {
117
+ return e.rel_type === activeEdgeFilter;
118
+ });
119
+
120
+ var cids = connectedIds(edges);
121
+
122
+ var q = searchQuery.toLowerCase();
123
+ var searchMatchIds = null;
124
+ if (q) {
125
+ searchMatchIds = new Set();
126
+ allNodes.forEach(function (n) {
127
+ if (n.label.toLowerCase().indexOf(q) >= 0 || n.id.toLowerCase().indexOf(q) >= 0) {
128
+ searchMatchIds.add(n.id);
129
+ }
130
+ });
131
+ edges.forEach(function (e) {
132
+ if (searchMatchIds.has(e.from)) searchMatchIds.add(e.to);
133
+ if (searchMatchIds.has(e.to)) searchMatchIds.add(e.from);
134
+ });
135
+ }
136
+
137
+ var nodes = allNodes.filter(function (n) {
138
+ if (searchMatchIds && !searchMatchIds.has(n.id)) return false;
139
+ if (hideOrphans && !cids.has(n.id)) return false;
140
+ if (activeEdgeFilter !== 'all' && !cids.has(n.id)) return false;
141
+ return true;
142
+ });
143
+
144
+ var visibleIds = new Set(
145
+ nodes.map(function (n) {
146
+ return n.id;
147
+ }),
148
+ );
149
+ var visibleEdges = edges.filter(function (e) {
150
+ return visibleIds.has(e.from) && visibleIds.has(e.to);
151
+ });
152
+
153
+ return { nodes: nodes, edges: visibleEdges.map(cleanEdge) };
154
+ }
155
+
156
+ function applyFilter() {
157
+ if (!network) return;
158
+ var d = getVisibleData();
159
+ network.setData({ nodes: new vis.DataSet(d.nodes), edges: new vis.DataSet(d.edges) });
160
+ updateStats(d.nodes.length, d.edges.length);
161
+ setTimeout(function () {
162
+ network.fit({ animation: { duration: 400, easingFunction: 'easeInOutQuad' } });
163
+ }, 100);
164
+ }
165
+
166
+ function updateStats(nc, ec) {
167
+ var el = document.getElementById('graph-stats');
168
+ if (el) el.textContent = nc + ' nodes, ' + ec + ' edges';
169
+ }
170
+
171
+ function buildLegend() {
172
+ var el = document.getElementById('graph-legend');
173
+ if (!el) return;
174
+ var cats = {};
175
+ allNodes.forEach(function (n) {
176
+ cats[n.group || 'notes'] = (cats[n.group || 'notes'] || 0) + 1;
177
+ });
178
+ var html = '';
179
+ Object.keys(CATEGORY_COLORS).forEach(function (c) {
180
+ if (!cats[c]) return;
181
+ html +=
182
+ '<span class="legend-item"><span class="legend-dot" style="background:' +
183
+ CATEGORY_COLORS[c] +
184
+ '"></span>' +
185
+ (CATEGORY_LABELS[c] || c) +
186
+ ' <span class="legend-count">' +
187
+ cats[c] +
188
+ '</span></span>';
189
+ });
190
+ var et = {};
191
+ allEdges.forEach(function (e) {
192
+ et[e.rel_type] = (et[e.rel_type] || 0) + 1;
193
+ });
194
+ if (Object.keys(et).length > 0) {
195
+ html += '<span class="legend-separator"></span>';
196
+ Object.keys(et).forEach(function (t) {
197
+ html +=
198
+ '<span class="legend-item"><span class="legend-line" style="background:' +
199
+ (EDGE_COLORS[t] || '#888') +
200
+ '"></span>' +
201
+ t.replace(/_/g, ' ') +
202
+ ' <span class="legend-count">' +
203
+ et[t] +
204
+ '</span></span>';
205
+ });
206
+ }
207
+ el.innerHTML = html;
208
+ }
209
+
210
+ function ensureUI() {
211
+ var view = document.getElementById('view-graph');
212
+ if (!view) return null;
213
+ var gc = document.getElementById('graph-container');
214
+ if (!gc) return null;
215
+
216
+ if (!document.getElementById('btn-graph-orphans')) {
217
+ var actions = view.querySelector('.view-actions');
218
+ if (actions) {
219
+ var btn = document.createElement('button');
220
+ btn.id = 'btn-graph-orphans';
221
+ btn.className = 'header-action-btn';
222
+ btn.title = 'Show only connected nodes';
223
+ btn.innerHTML = '<span class="material-symbols-outlined">filter_alt</span>Connected';
224
+ actions.insertBefore(btn, actions.firstChild);
225
+ }
226
+ }
227
+
228
+ if (!document.getElementById('graph-search-input')) {
229
+ var bar = document.createElement('div');
230
+ bar.className = 'search-bar';
231
+ bar.innerHTML =
232
+ '<span class="material-symbols-outlined search-input-icon">search</span><input type="search" id="graph-search-input" class="search-input" placeholder="Search graph nodes..." autocomplete="off">';
233
+ gc.parentNode.insertBefore(bar, gc);
234
+ }
235
+
236
+ if (!document.getElementById('graph-legend')) {
237
+ var lg = document.createElement('div');
238
+ lg.id = 'graph-legend';
239
+ lg.className = 'graph-legend';
240
+ gc.parentNode.insertBefore(lg, gc);
241
+ }
242
+
243
+ if (!document.getElementById('graph-stats')) {
244
+ var st = document.createElement('div');
245
+ st.id = 'graph-stats';
246
+ st.className = 'graph-stats';
247
+ gc.parentNode.appendChild(st);
248
+ }
249
+
250
+ return gc;
251
+ }
252
+
253
+ function replaceWithClone(id) {
254
+ var el = document.getElementById(id);
255
+ if (!el) return null;
256
+ var clone = el.cloneNode(true);
257
+ el.parentNode.replaceChild(clone, el);
258
+ return clone;
259
+ }
260
+
261
+ function bindControls() {
262
+ controlsVersion++;
263
+ var ver = controlsVersion;
264
+
265
+ var fitBtn = replaceWithClone('btn-graph-fit');
266
+ var physicsBtn = replaceWithClone('btn-graph-physics');
267
+ var orphanBtn = replaceWithClone('btn-graph-orphans');
268
+ var filterCont = replaceWithClone('graph-edge-filters');
269
+ var searchInput = replaceWithClone('graph-search-input');
270
+
271
+ if (fitBtn)
272
+ fitBtn.addEventListener('click', function () {
273
+ if (network) network.fit({ animation: { duration: 500, easingFunction: 'easeInOutQuad' } });
274
+ });
275
+
276
+ if (physicsBtn) {
277
+ physicsBtn.classList.toggle('active', physicsEnabled);
278
+ physicsBtn.addEventListener('click', function () {
279
+ physicsEnabled = !physicsEnabled;
280
+ physicsBtn.classList.toggle('active', physicsEnabled);
281
+ if (network) network.setOptions({ physics: { enabled: physicsEnabled } });
282
+ });
283
+ }
284
+
285
+ if (orphanBtn) {
286
+ orphanBtn.classList.toggle('active', hideOrphans);
287
+ orphanBtn.addEventListener('click', function () {
288
+ hideOrphans = !hideOrphans;
289
+ orphanBtn.classList.toggle('active', hideOrphans);
290
+ applyFilter();
291
+ });
292
+ }
293
+
294
+ if (filterCont)
295
+ filterCont.addEventListener('click', function (e) {
296
+ if (ver !== controlsVersion) return;
297
+ var chip = e.target.closest('.chip');
298
+ if (!chip) return;
299
+ filterCont.querySelectorAll('.chip').forEach(function (c) {
300
+ c.classList.remove('active');
301
+ });
302
+ chip.classList.add('active');
303
+ activeEdgeFilter = chip.dataset.edgeType || 'all';
304
+ applyFilter();
305
+ });
306
+
307
+ if (searchInput) {
308
+ var timer;
309
+ searchInput.addEventListener('input', function () {
310
+ if (ver !== controlsVersion) return;
311
+ clearTimeout(timer);
312
+ timer = setTimeout(function () {
313
+ searchQuery = (searchInput.value || '').trim();
314
+ applyFilter();
315
+ }, 300);
316
+ });
317
+ }
318
+ }
319
+
320
+ async function loadGraph() {
321
+ var gc = ensureUI();
322
+ if (!gc) return;
323
+
324
+ gc.style.cssText =
325
+ 'width:100%;height:calc(100vh - 320px);min-height:300px;display:block;visibility:visible;';
326
+ var emptyEl = document.getElementById('graph-empty');
327
+ if (emptyEl) emptyEl.classList.add('hidden');
328
+
329
+ await new Promise(function (r) {
330
+ requestAnimationFrame(r);
331
+ });
332
+
333
+ try {
334
+ var data;
335
+ var baseUrl = K._baseUrl || '';
336
+
337
+ var resp = await fetch(baseUrl + '/api/knowledge/graph-data');
338
+ if (resp.ok) {
339
+ data = await resp.json();
340
+ } else {
341
+ var er = await fetch(baseUrl + '/api/knowledge');
342
+ var entries = await er.json();
343
+ if (!Array.isArray(entries)) entries = [];
344
+ var edgeArr = [];
345
+ var bs = 10;
346
+ for (var i = 0; i < entries.length; i += bs) {
347
+ var batch = entries.slice(i, i + bs);
348
+ var results = await Promise.all(
349
+ batch.map(function (e) {
350
+ return fetch(baseUrl + '/api/knowledge/' + encodeURIComponent(e.path) + '/links')
351
+ .then(function (r) {
352
+ return r.ok ? r.json() : [];
353
+ })
354
+ .catch(function () {
355
+ return [];
356
+ });
357
+ }),
358
+ );
359
+ var seen = new Set(
360
+ edgeArr.map(function (e) {
361
+ return e.source + '|' + e.target + '|' + e.rel_type;
362
+ }),
363
+ );
364
+ results.forEach(function (links) {
365
+ if (!Array.isArray(links)) return;
366
+ links.forEach(function (e) {
367
+ var k = e.source + '|' + e.target + '|' + e.rel_type;
368
+ if (!seen.has(k)) {
369
+ seen.add(k);
370
+ edgeArr.push(e);
371
+ }
372
+ });
373
+ });
374
+ }
375
+ data = {
376
+ nodes: entries.map(function (e) {
377
+ var cat = e.category || e.path.split('/')[0] || 'notes';
378
+ var deg = edgeArr.filter(function (d) {
379
+ return d.source === e.path || d.target === e.path;
380
+ }).length;
381
+ return {
382
+ id: e.path,
383
+ label: (e.title || e.path.split('/').pop().replace('.md', '') || e.path).slice(0, 40),
384
+ title: e.path + '\nCategory: ' + cat + '\nEdges: ' + deg,
385
+ group: cat,
386
+ size: Math.max(4, Math.min(16, 4 + deg * 2)),
387
+ };
388
+ }),
389
+ edges: edgeArr.map(function (e) {
390
+ return {
391
+ from: e.source,
392
+ to: e.target,
393
+ title: (e.rel_type || '').replace(/_/g, ' '),
394
+ rel_type: e.rel_type,
395
+ };
396
+ }),
397
+ };
398
+ }
399
+
400
+ allNodes = (data.nodes || []).map(function (n) {
401
+ var cat = n.group || 'notes';
402
+ var deg = (data.edges || []).filter(function (e) {
403
+ return e.from === n.id || e.to === n.id;
404
+ }).length;
405
+ var c = CATEGORY_COLORS[cat] || '#888';
406
+ return {
407
+ id: n.id,
408
+ label: n.label,
409
+ title: n.title,
410
+ group: cat,
411
+ color: {
412
+ background: c,
413
+ border: c,
414
+ highlight: { background: c, border: c },
415
+ hover: { background: c, border: c },
416
+ },
417
+ size: Math.max(4, Math.min(16, 4 + deg * 2)),
418
+ font: { size: deg > 3 ? 12 : deg > 0 ? 11 : 9 },
419
+ degree: deg,
420
+ };
421
+ });
422
+
423
+ allEdges = (data.edges || []).map(function (e) {
424
+ return {
425
+ from: e.from,
426
+ to: e.to,
427
+ title: e.title || (e.rel_type || '').replace(/_/g, ' '),
428
+ rel_type: e.rel_type,
429
+ };
430
+ });
431
+
432
+ console.log('[graph] nodes:', allNodes.length, 'edges:', allEdges.length);
433
+
434
+ if (allNodes.length === 0) {
435
+ gc.style.display = 'none';
436
+ if (emptyEl) emptyEl.classList.remove('hidden');
437
+ return;
438
+ }
439
+
440
+ buildLegend();
441
+ bindControls();
442
+
443
+ var d = getVisibleData();
444
+ updateStats(d.nodes.length, d.edges.length);
445
+
446
+ var isDark =
447
+ document.documentElement.getAttribute('data-theme') === 'dark' ||
448
+ localStorage.getItem('agent-knowledge-theme') === 'dark';
449
+
450
+ if (network) network.destroy();
451
+ network = new vis.Network(
452
+ gc,
453
+ { nodes: new vis.DataSet(d.nodes), edges: new vis.DataSet(d.edges) },
454
+ getOptions(isDark),
455
+ );
456
+
457
+ network.on('click', function (p) {
458
+ if (p.nodes.length > 0 && K.openKnowledgePanel) K.openKnowledgePanel(p.nodes[0]);
459
+ });
460
+ network.on('hoverNode', function () {
461
+ gc.style.cursor = 'pointer';
462
+ });
463
+ network.on('blurNode', function () {
464
+ gc.style.cursor = 'default';
465
+ });
466
+ network.on('stabilizationIterationsDone', function () {
467
+ network.setOptions({ physics: { stabilization: { enabled: false } } });
468
+ });
469
+ } catch (err) {
470
+ console.error('[graph] load failed:', err);
471
+ gc.style.display = 'none';
472
+ var em = document.getElementById('graph-empty');
473
+ if (em) em.classList.remove('hidden');
474
+ }
475
+ }
476
+
477
+ K.renderGraph = loadGraph;
478
+ K.setupGraphControls = function () {};
479
+ K.graphFit = function () {
480
+ if (network) network.fit();
481
+ };
482
+ })();
@@ -1823,3 +1823,75 @@ h1 {
1823
1823
  .duplicate-warn {
1824
1824
  cursor: help;
1825
1825
  }
1826
+
1827
+ /* ── Graph Tab ──────────────────────────────────────────────────────────── */
1828
+
1829
+ .header-action-btn.active {
1830
+ background: var(--accent);
1831
+ color: #fff;
1832
+ border-color: var(--accent);
1833
+ }
1834
+
1835
+ .header-action-btn.active .material-symbols-outlined {
1836
+ color: #fff;
1837
+ }
1838
+
1839
+ .graph-stats {
1840
+ font-size: 0.78rem;
1841
+ color: var(--text-dim);
1842
+ padding: 4px 0;
1843
+ }
1844
+
1845
+ .graph-legend {
1846
+ display: flex;
1847
+ flex-wrap: wrap;
1848
+ gap: 12px;
1849
+ padding: 4px 0 8px 0;
1850
+ font-size: 0.78rem;
1851
+ color: var(--text-dim);
1852
+ }
1853
+
1854
+ .legend-item {
1855
+ display: inline-flex;
1856
+ align-items: center;
1857
+ gap: 5px;
1858
+ }
1859
+
1860
+ .legend-dot {
1861
+ display: inline-block;
1862
+ width: 10px;
1863
+ height: 10px;
1864
+ border-radius: 50%;
1865
+ flex-shrink: 0;
1866
+ }
1867
+
1868
+ .legend-line {
1869
+ display: inline-block;
1870
+ width: 16px;
1871
+ height: 3px;
1872
+ border-radius: 2px;
1873
+ flex-shrink: 0;
1874
+ }
1875
+
1876
+ .legend-count {
1877
+ opacity: 0.6;
1878
+ font-size: 0.72rem;
1879
+ }
1880
+
1881
+ .legend-separator {
1882
+ display: inline-block;
1883
+ width: 1px;
1884
+ height: 14px;
1885
+ background: var(--border);
1886
+ margin: 0 4px;
1887
+ align-self: center;
1888
+ }
1889
+
1890
+ #graph-container {
1891
+ width: 100%;
1892
+ height: calc(100vh - 320px);
1893
+ min-height: 300px;
1894
+ border-radius: 8px;
1895
+ border: 1px solid var(--border);
1896
+ background: var(--bg);
1897
+ }