Download src/utils/graph-intelligence.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 9.39 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/graph-intelligence.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/utils/graph-intelligence.js
-
curl -L -o graph-intelligence.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/graph-intelligence.js
9.39 kB
| const db = require('../../knowledge-db'); | |
| /** | |
| * Pure JS ๊ธฐ๋ฐ์ ๊ฒฝ๋ Louvain ์ปค๋ฎค๋ํฐ ํ์ง ๋ฐ ๊ทธ๋ํ ์ธํ ๋ฆฌ์ ์ค ๋ชจ๋ | |
| */ | |
| /** | |
| * ๊ทธ๋ํ์ ์ปค๋ฎค๋ํฐ๋ฅผ ๋ถ๋ฅํ๊ณ ๋ฐ์ง์ฑ(Cohesion)์ ์ฐ์ฐํฉ๋๋ค. | |
| * @param {object} graphData { nodes, edges } ๊ตฌ์กฐ์ ๊ทธ๋ํ ๋ฐ์ดํฐ | |
| * @returns {object} { communityMap, communitiesDetail } | |
| */ | |
| function detectCommunities(graphData) { | |
| const nodes = graphData.nodes || []; | |
| const edges = graphData.edges || []; | |
| if (nodes.length === 0) { | |
| return { communityMap: {}, communitiesDetail: {} }; | |
| } | |
| // 1. ์ด๊ธฐํ: ๊ฐ ๋ ธ๋๋ฅผ ๊ณ ์ ์ปค๋ฎค๋ํฐ ID๋ก ์ง์ | |
| const communityMap = {}; | |
| nodes.forEach((n, idx) => { | |
| communityMap[n.id] = idx; | |
| }); | |
| // ๋ ธ๋ ์ฐจ์(degree) ๋ฐ ๊ฐ์ค์น ํฉ ๊ณ์ฐ | |
| const degrees = {}; | |
| nodes.forEach(n => { | |
| degrees[n.id] = 0; | |
| }); | |
| const adj = {}; | |
| nodes.forEach(n => { | |
| adj[n.id] = {}; | |
| }); | |
| edges.forEach(e => { | |
| if (degrees[e.source] !== undefined && degrees[e.target] !== undefined) { | |
| degrees[e.source]++; | |
| degrees[e.target]++; | |
| // ๊ฐ์ค์น(๊ธฐ๋ณธ๊ฐ 1.0) | |
| const weight = parseFloat(e.properties?.weight) || 1.0; | |
| adj[e.source][e.target] = (adj[e.source][e.target] || 0) + weight; | |
| adj[e.target][e.source] = (adj[e.target][e.source] || 0) + weight; | |
| } | |
| }); | |
| // ์ ์ฒด ์ฐจ์ ํฉ (2m) | |
| let totalWeight2m = 0; | |
| nodes.forEach(n => { | |
| Object.keys(adj[n.id]).forEach(nbr => { | |
| totalWeight2m += adj[n.id][nbr]; | |
| }); | |
| }); | |
| if (totalWeight2m === 0) { | |
| totalWeight2m = nodes.length; // fallback | |
| } | |
| // 2. Modularity ์ต์ ํ ๋ฃจํ (์ต๋ 15ํ ๋ฐ๋ณต) | |
| let improvement = true; | |
| let iterations = 0; | |
| while (improvement && iterations < 15) { | |
| improvement = false; | |
| iterations++; | |
| // ๊ฐ ๋ ธ๋ ์ํํ๋ฉฐ ์ด์ ์ปค๋ฎค๋ํฐ๋ก ์ด์ํ์ฌ modularity delta ๊ณ์ฐ | |
| for (const node of nodes) { | |
| const u = node.id; | |
| const currentComm = communityMap[u]; | |
| const neighbors = Object.keys(adj[u]); | |
| if (neighbors.length === 0) continue; | |
| // ์ด์ ๋ ธ๋๋ค์ด ์ํด์๋ ์ปค๋ฎค๋ํฐ ๋ฆฌ์คํธ ์ถ์ถ | |
| const neighborComms = new Set(); | |
| neighbors.forEach(v => neighborComms.add(communityMap[v])); | |
| let bestComm = currentComm; | |
| let maxDeltaQ = 0; | |
| // ๊ฐ ์ด์ ์ปค๋ฎค๋ํฐ๋ก u๋ฅผ ์ฎ๊ฒผ์ ๋์ Delta Q ์ฐ์ฐ | |
| neighborComms.forEach(c => { | |
| if (c === currentComm) return; | |
| // u์ ์ปค๋ฎค๋ํฐ c ๋ด์ ๋ ธ๋๋ค ๊ฐ์ ๊ฐ์ค์น ํฉ (Ki,in) | |
| let k_i_in = 0; | |
| neighbors.forEach(v => { | |
| if (communityMap[v] === c) { | |
| k_i_in += adj[u][v] || 0; | |
| } | |
| }); | |
| // ์ปค๋ฎค๋ํฐ c์ ์ํ ์ ์ฒด ๋ ธ๋๋ค์ ์ฐจ์ ํฉ (Sigma_tot) | |
| let sigma_tot = 0; | |
| nodes.forEach(n => { | |
| if (communityMap[n.id] === c) { | |
| Object.keys(adj[n.id]).forEach(nbr => { | |
| sigma_tot += adj[n.id][nbr] || 0; | |
| }); | |
| } | |
| }); | |
| // u์ ์์ฒด ์ฐจ์ (Ki) | |
| let k_i = 0; | |
| Object.keys(adj[u]).forEach(nbr => { | |
| k_i += adj[u][nbr] || 0; | |
| }); | |
| // Delta Q ๊ฐ์ ๊ณ์ฐ ๊ณต์ | |
| const deltaQ = (k_i_in / totalWeight2m) - (sigma_tot * k_i) / (totalWeight2m * totalWeight2m); | |
| if (deltaQ > maxDeltaQ) { | |
| maxDeltaQ = deltaQ; | |
| bestComm = c; | |
| } | |
| }); | |
| if (bestComm !== currentComm) { | |
| communityMap[u] = bestComm; | |
| improvement = true; | |
| } | |
| } | |
| } | |
| // 3. ์ปค๋ฎค๋ํฐ ์์ธ ์งํ ๋ฐ Cohesion ๊ณ์ฐ | |
| const communitiesDetail = {}; | |
| nodes.forEach(n => { | |
| const cId = communityMap[n.id]; | |
| if (!communitiesDetail[cId]) { | |
| communitiesDetail[cId] = { | |
| id: cId, | |
| nodes: [], | |
| internalEdgesCount: 0, | |
| cohesion: 1.0 | |
| }; | |
| } | |
| communitiesDetail[cId].nodes.push(n.id); | |
| }); | |
| // ๊ฐ ์ปค๋ฎค๋ํฐ ๋ด๋ถ ์ฃ์ง ์ ๊ณ์ฐ | |
| edges.forEach(e => { | |
| const cSrc = communityMap[e.source]; | |
| const cTgt = communityMap[e.target]; | |
| if (cSrc === cTgt && communitiesDetail[cSrc]) { | |
| communitiesDetail[cSrc].internalEdgesCount++; | |
| } | |
| }); | |
| // Cohesion ์ ์ ๊ณต์ ์ ์ฉ | |
| Object.keys(communitiesDetail).forEach(cId => { | |
| const comm = communitiesDetail[cId]; | |
| const n = comm.nodes.length; | |
| if (n >= 2) { | |
| const maxPossibleEdges = (n * (n - 1)) / 2; | |
| comm.cohesion = comm.internalEdgesCount / maxPossibleEdges; | |
| } else { | |
| comm.cohesion = 1.0; | |
| } | |
| }); | |
| return { communityMap, communitiesDetail }; | |
| } | |
| /** | |
| * ํ๋ก์ ํธ ์ง์์ ๊ธฐ๋ฐํ Heuristics Graph Insights๋ฅผ ์์ฑํฉ๋๋ค. | |
| * @param {string} projectId ํ๋ก์ ํธ ํด๋๋ช | |
| * @returns {object} { surprisingConnections, knowledgeGaps } | |
| */ | |
| function generateGraphInsights(projectId) { | |
| const graphData = db.exportProjectData(projectId); | |
| if (!graphData || !graphData.nodes || graphData.nodes.length === 0) { | |
| return { surprisingConnections: [], knowledgeGaps: { isolatedNodes: [], sparseCommunities: [], bridgeNodes: [] } }; | |
| } | |
| const { communityMap, communitiesDetail } = detectCommunities(graphData); | |
| // 1. ์ฐจ์(degree) ๋ฐ ์ธ์ ๋ฆฌ์คํธ ์์ฑ | |
| const degrees = {}; | |
| const adj = {}; | |
| graphData.nodes.forEach(n => { | |
| degrees[n.id] = 0; | |
| adj[n.id] = new Set(); | |
| }); | |
| graphData.edges.forEach(e => { | |
| if (degrees[e.source] !== undefined && degrees[e.target] !== undefined) { | |
| degrees[e.source]++; | |
| degrees[e.target]++; | |
| adj[e.source].add(e.target); | |
| adj[e.target].add(e.source); | |
| } | |
| }); | |
| // 2. Surprising Connections (์์ธ์ ์ฐ๊ฒฐ) ํ์ง ๊ท์น Heuristics | |
| const surprisingConnections = []; | |
| graphData.edges.forEach(e => { | |
| // ๊ตฌ์กฐ ๋ ธ๋(index, log ๋ฑ)๋ ๋ ธ์ด์ฆ ์ ๋ฐํ๋ฏ๋ก ์ ์ธ | |
| if (e.source.includes('index') || e.target.includes('index')) return; | |
| let score = 0; | |
| const cSrc = communityMap[e.source]; | |
| const cTgt = communityMap[e.target]; | |
| // ๊ท์น 1: ์๋ก ๋ค๋ฅธ ์ปค๋ฎค๋ํฐ ๊ฐ์ ์ฐ๊ฒฐ | |
| if (cSrc !== undefined && cTgt !== undefined && cSrc !== cTgt) { | |
| score += 3.0; | |
| } | |
| const nodeSrc = graphData.nodes.find(n => n.id === e.source); | |
| const nodeTgt = graphData.nodes.find(n => n.id === e.target); | |
| if (nodeSrc && nodeTgt) { | |
| // ๊ท์น 2: ํฌ๋ก์ค ํ์ (์: ์์คํ์ผ๊ณผ ์ธ์ ๋๋ ์์คํ์ผ๊ณผ ์ํค๊ฐ ๋จผ ๊ฒฐํฉ์ ๊ฐ์ง ๋) | |
| if (nodeSrc.type !== nodeTgt.type) { | |
| if ((nodeSrc.type === 'file' && nodeTgt.type === 'session') || (nodeSrc.type === 'session' && nodeTgt.type === 'file')) { | |
| score += 2.0; | |
| } else { | |
| score += 1.0; | |
| } | |
| } | |
| // ๊ท์น 3: ๋ณ๋๋ฆฌ ๋ ธ๋(์ฐจ์ <= 2)์ ํ๋ธ ๋ ธ๋(์ฐจ์ >= 8) ๊ฐ์ ์ฐ๊ฒฐ | |
| const degSrc = degrees[e.source] || 0; | |
| const degTgt = degrees[e.target] || 0; | |
| if ((degSrc <= 2 && degTgt >= 8) || (degTgt <= 2 && degSrc >= 8)) { | |
| score += 2.0; | |
| } | |
| } | |
| if (score >= 4.0) { | |
| surprisingConnections.push({ | |
| edge: e, | |
| sourceLabel: nodeSrc ? nodeSrc.label : e.source, | |
| targetLabel: nodeTgt ? nodeTgt.label : e.target, | |
| score | |
| }); | |
| } | |
| }); | |
| // ์ค๋ณต ์ฃ์ง ์ ๊ฑฐ ๋ฐ ์ ๋ ฌ | |
| const uniqueSurprising = surprisingConnections | |
| .sort((a, b) => b.score - a.score) | |
| .slice(0, 10); | |
| // 3. Knowledge Gaps (์ง์ ๊ฒฐํจ) ํ์ง ๊ท์น Heuristics | |
| // A. Isolated Nodes (๊ณ ๋ฆฝ ๋ ธ๋: ์ฐ๊ฒฐ ์ฐจ์ <= 1 ์ธ ์ํค/๋ ธํธ ๋ฐ ์์ค ํ์ผ) | |
| const isolatedNodes = graphData.nodes | |
| .filter(n => { | |
| if (n.id.includes('index') || n.id.includes('claude')) return false; | |
| const deg = degrees[n.id] || 0; | |
| return deg <= 1 && (n.type === 'note' || n.type === 'wiki' || n.type === 'file'); | |
| }) | |
| .map(n => ({ id: n.id, label: n.label, type: n.type })); | |
| // B. Sparse Communities (ํฌ์ ์ปค๋ฎค๋ํฐ: ๋ ธ๋ >= 3๊ฐ, Cohesion < 0.2) | |
| const sparseCommunities = []; | |
| Object.keys(communitiesDetail).forEach(cId => { | |
| const comm = communitiesDetail[cId]; | |
| if (comm.nodes.length >= 3 && comm.cohesion < 0.20) { | |
| // ํด๋น ์ปค๋ฎค๋ํฐ์ ๋ ธ๋ ๋ผ๋ฒจ ์์ง | |
| const commLabels = comm.nodes.map(nId => { | |
| const found = graphData.nodes.find(n => n.id === nId); | |
| return found ? found.label : nId; | |
| }); | |
| sparseCommunities.push({ | |
| communityId: cId, | |
| nodeCount: comm.nodes.length, | |
| cohesion: comm.cohesion, | |
| nodes: commLabels | |
| }); | |
| } | |
| }); | |
| // C. Bridge Nodes (๋ธ๋ฆฌ์ง ๋ ธ๋: 3๊ฐ ์ด์์ ๋ค๋ฅธ ์ปค๋ฎค๋ํฐ์ ๋ ธ๋๋ค๊ณผ ๊ฐ๊ฐ ์ฃ์ง๋ฅผ ๊ฐ์ง๋ ๋ ธ๋) | |
| const bridgeNodes = []; | |
| graphData.nodes.forEach(n => { | |
| const connectedComms = new Set(); | |
| const neighbors = adj[n.id] || new Set(); | |
| neighbors.forEach(nbrId => { | |
| const cNbr = communityMap[nbrId]; | |
| if (cNbr !== undefined && cNbr !== communityMap[n.id]) { | |
| connectedComms.add(cNbr); | |
| } | |
| }); | |
| if (connectedComms.size >= 3) { | |
| bridgeNodes.push({ | |
| id: n.id, | |
| label: n.label, | |
| type: n.type, | |
| connectedCommunitiesCount: connectedComms.size | |
| }); | |
| } | |
| }); | |
| return { | |
| surprisingConnections: uniqueSurprising, | |
| knowledgeGaps: { | |
| isolatedNodes, | |
| sparseCommunities, | |
| bridgeNodes | |
| } | |
| }; | |
| } | |
| module.exports = { | |
| detectCommunities, | |
| generateGraphInsights | |
| }; | |