File size: 8,953 Bytes
40d7073 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | "use strict";
/**
* Graph Wrapper - Hypergraph database for code relationships
*
* Wraps @ruvector/graph-node for dependency analysis, co-edit patterns,
* and code structure understanding.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeGraph = void 0;
exports.isGraphAvailable = isGraphAvailable;
exports.createCodeDependencyGraph = createCodeDependencyGraph;
let graphModule = null;
let loadError = null;
function getGraphModule() {
if (graphModule)
return graphModule;
if (loadError)
throw loadError;
try {
graphModule = require('@ruvector/graph-node');
return graphModule;
}
catch (e) {
loadError = new Error(`@ruvector/graph-node not installed: ${e.message}\n` +
`Install with: npm install @ruvector/graph-node`);
throw loadError;
}
}
function isGraphAvailable() {
try {
getGraphModule();
return true;
}
catch {
return false;
}
}
/**
* Graph Database for code relationships
*/
class CodeGraph {
constructor(options = {}) {
const graph = getGraphModule();
this.storagePath = options.storagePath;
this.inner = new graph.GraphDatabase({
storagePath: options.storagePath,
inMemory: options.inMemory ?? true,
});
}
// ===========================================================================
// Node Operations
// ===========================================================================
/**
* Create a node (file, function, class, etc.)
*/
createNode(id, labels, properties = {}) {
this.inner.createNode(id, labels, JSON.stringify(properties));
return { id, labels, properties };
}
/**
* Get a node by ID
*/
getNode(id) {
const result = this.inner.getNode(id);
if (!result)
return null;
return {
id: result.id,
labels: result.labels,
properties: result.properties ? JSON.parse(result.properties) : {},
};
}
/**
* Update node properties
*/
updateNode(id, properties) {
return this.inner.updateNode(id, JSON.stringify(properties));
}
/**
* Delete a node
*/
deleteNode(id) {
return this.inner.deleteNode(id);
}
/**
* Find nodes by label
*/
findNodesByLabel(label) {
const results = this.inner.findNodesByLabel(label);
return results.map((r) => ({
id: r.id,
labels: r.labels,
properties: r.properties ? JSON.parse(r.properties) : {},
}));
}
// ===========================================================================
// Edge Operations
// ===========================================================================
/**
* Create an edge (import, call, reference, etc.)
*/
createEdge(from, to, type, properties = {}) {
const id = this.inner.createEdge(from, to, type, JSON.stringify(properties));
return { id, from, to, type, properties };
}
/**
* Get edges from a node
*/
getOutgoingEdges(nodeId, type) {
const results = this.inner.getOutgoingEdges(nodeId, type);
return results.map((r) => ({
id: r.id,
from: r.from,
to: r.to,
type: r.type,
properties: r.properties ? JSON.parse(r.properties) : {},
}));
}
/**
* Get edges to a node
*/
getIncomingEdges(nodeId, type) {
const results = this.inner.getIncomingEdges(nodeId, type);
return results.map((r) => ({
id: r.id,
from: r.from,
to: r.to,
type: r.type,
properties: r.properties ? JSON.parse(r.properties) : {},
}));
}
/**
* Delete an edge
*/
deleteEdge(edgeId) {
return this.inner.deleteEdge(edgeId);
}
// ===========================================================================
// Hyperedge Operations (for co-edit patterns)
// ===========================================================================
/**
* Create a hyperedge connecting multiple nodes
*/
createHyperedge(nodes, type, properties = {}) {
const id = this.inner.createHyperedge(nodes, type, JSON.stringify(properties));
return { id, nodes, type, properties };
}
/**
* Get hyperedges containing a node
*/
getHyperedges(nodeId, type) {
const results = this.inner.getHyperedges(nodeId, type);
return results.map((r) => ({
id: r.id,
nodes: r.nodes,
type: r.type,
properties: r.properties ? JSON.parse(r.properties) : {},
}));
}
// ===========================================================================
// Query Operations
// ===========================================================================
/**
* Execute a Cypher query
*/
cypher(query, params = {}) {
const result = this.inner.cypher(query, JSON.stringify(params));
return {
columns: result.columns,
rows: result.rows,
};
}
/**
* Find shortest path between nodes
*/
shortestPath(from, to, maxDepth = 10) {
const result = this.inner.shortestPath(from, to, maxDepth);
if (!result)
return null;
return {
nodes: result.nodes.map((n) => ({
id: n.id,
labels: n.labels,
properties: n.properties ? JSON.parse(n.properties) : {},
})),
edges: result.edges.map((e) => ({
id: e.id,
from: e.from,
to: e.to,
type: e.type,
properties: e.properties ? JSON.parse(e.properties) : {},
})),
length: result.length,
};
}
/**
* Get all paths between nodes (up to maxPaths)
*/
allPaths(from, to, maxDepth = 5, maxPaths = 10) {
const results = this.inner.allPaths(from, to, maxDepth, maxPaths);
return results.map((r) => ({
nodes: r.nodes.map((n) => ({
id: n.id,
labels: n.labels,
properties: n.properties ? JSON.parse(n.properties) : {},
})),
edges: r.edges.map((e) => ({
id: e.id,
from: e.from,
to: e.to,
type: e.type,
properties: e.properties ? JSON.parse(e.properties) : {},
})),
length: r.length,
}));
}
/**
* Get neighbors of a node
*/
neighbors(nodeId, depth = 1) {
const results = this.inner.neighbors(nodeId, depth);
return results.map((n) => ({
id: n.id,
labels: n.labels,
properties: n.properties ? JSON.parse(n.properties) : {},
}));
}
// ===========================================================================
// Graph Algorithms
// ===========================================================================
/**
* Calculate PageRank for nodes
*/
pageRank(iterations = 20, dampingFactor = 0.85) {
const result = this.inner.pageRank(iterations, dampingFactor);
return new Map(Object.entries(result));
}
/**
* Find connected components
*/
connectedComponents() {
return this.inner.connectedComponents();
}
/**
* Detect communities (Louvain algorithm)
*/
communities() {
const result = this.inner.communities();
return new Map(Object.entries(result));
}
/**
* Calculate betweenness centrality
*/
betweennessCentrality() {
const result = this.inner.betweennessCentrality();
return new Map(Object.entries(result));
}
// ===========================================================================
// Persistence
// ===========================================================================
/**
* Save graph to storage
*/
save() {
if (!this.storagePath) {
throw new Error('No storage path configured');
}
this.inner.save();
}
/**
* Load graph from storage
*/
load() {
if (!this.storagePath) {
throw new Error('No storage path configured');
}
this.inner.load();
}
/**
* Clear all data
*/
clear() {
this.inner.clear();
}
/**
* Get graph statistics
*/
stats() {
return this.inner.stats();
}
}
exports.CodeGraph = CodeGraph;
/**
* Create a code dependency graph from file analysis
*/
function createCodeDependencyGraph(storagePath) {
return new CodeGraph({ storagePath, inMemory: !storagePath });
}
exports.default = CodeGraph;
|