@claude-flow/cli 3.0.0-alpha.2 → 3.0.0-alpha.20

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.
Files changed (263) hide show
  1. package/README.md +172 -6
  2. package/bin/cli.js +0 -0
  3. package/dist/src/commands/agent.d.ts.map +1 -1
  4. package/dist/src/commands/agent.js +43 -27
  5. package/dist/src/commands/agent.js.map +1 -1
  6. package/dist/src/commands/analyze.d.ts +19 -0
  7. package/dist/src/commands/analyze.d.ts.map +1 -0
  8. package/dist/src/commands/analyze.js +1823 -0
  9. package/dist/src/commands/analyze.js.map +1 -0
  10. package/dist/src/commands/claims.d.ts +10 -0
  11. package/dist/src/commands/claims.d.ts.map +1 -0
  12. package/dist/src/commands/claims.js +288 -0
  13. package/dist/src/commands/claims.js.map +1 -0
  14. package/dist/src/commands/completions.d.ts +10 -0
  15. package/dist/src/commands/completions.d.ts.map +1 -0
  16. package/dist/src/commands/completions.js +539 -0
  17. package/dist/src/commands/completions.js.map +1 -0
  18. package/dist/src/commands/config.js +2 -2
  19. package/dist/src/commands/config.js.map +1 -1
  20. package/dist/src/commands/daemon.d.ts +8 -0
  21. package/dist/src/commands/daemon.d.ts.map +1 -0
  22. package/dist/src/commands/daemon.js +545 -0
  23. package/dist/src/commands/daemon.js.map +1 -0
  24. package/dist/src/commands/deployment.d.ts +10 -0
  25. package/dist/src/commands/deployment.d.ts.map +1 -0
  26. package/dist/src/commands/deployment.js +289 -0
  27. package/dist/src/commands/deployment.js.map +1 -0
  28. package/dist/src/commands/doctor.d.ts +10 -0
  29. package/dist/src/commands/doctor.d.ts.map +1 -0
  30. package/dist/src/commands/doctor.js +429 -0
  31. package/dist/src/commands/doctor.js.map +1 -0
  32. package/dist/src/commands/embeddings.d.ts +18 -0
  33. package/dist/src/commands/embeddings.d.ts.map +1 -0
  34. package/dist/src/commands/embeddings.js +616 -0
  35. package/dist/src/commands/embeddings.js.map +1 -0
  36. package/dist/src/commands/hive-mind.d.ts.map +1 -1
  37. package/dist/src/commands/hive-mind.js +252 -35
  38. package/dist/src/commands/hive-mind.js.map +1 -1
  39. package/dist/src/commands/hooks.d.ts.map +1 -1
  40. package/dist/src/commands/hooks.js +326 -2
  41. package/dist/src/commands/hooks.js.map +1 -1
  42. package/dist/src/commands/index.d.ts +13 -0
  43. package/dist/src/commands/index.d.ts.map +1 -1
  44. package/dist/src/commands/index.js +52 -1
  45. package/dist/src/commands/index.js.map +1 -1
  46. package/dist/src/commands/mcp.js +4 -4
  47. package/dist/src/commands/mcp.js.map +1 -1
  48. package/dist/src/commands/memory.d.ts.map +1 -1
  49. package/dist/src/commands/memory.js +236 -170
  50. package/dist/src/commands/memory.js.map +1 -1
  51. package/dist/src/commands/migrate.js +1 -1
  52. package/dist/src/commands/migrate.js.map +1 -1
  53. package/dist/src/commands/neural.d.ts +10 -0
  54. package/dist/src/commands/neural.d.ts.map +1 -0
  55. package/dist/src/commands/neural.js +224 -0
  56. package/dist/src/commands/neural.js.map +1 -0
  57. package/dist/src/commands/performance.d.ts +10 -0
  58. package/dist/src/commands/performance.d.ts.map +1 -0
  59. package/dist/src/commands/performance.js +262 -0
  60. package/dist/src/commands/performance.js.map +1 -0
  61. package/dist/src/commands/plugins.d.ts +10 -0
  62. package/dist/src/commands/plugins.d.ts.map +1 -0
  63. package/dist/src/commands/plugins.js +280 -0
  64. package/dist/src/commands/plugins.js.map +1 -0
  65. package/dist/src/commands/process.d.ts.map +1 -1
  66. package/dist/src/commands/process.js +95 -20
  67. package/dist/src/commands/process.js.map +1 -1
  68. package/dist/src/commands/providers.d.ts +10 -0
  69. package/dist/src/commands/providers.d.ts.map +1 -0
  70. package/dist/src/commands/providers.js +232 -0
  71. package/dist/src/commands/providers.js.map +1 -0
  72. package/dist/src/commands/route.d.ts +16 -0
  73. package/dist/src/commands/route.d.ts.map +1 -0
  74. package/dist/src/commands/route.js +603 -0
  75. package/dist/src/commands/route.js.map +1 -0
  76. package/dist/src/commands/security.d.ts +10 -0
  77. package/dist/src/commands/security.d.ts.map +1 -0
  78. package/dist/src/commands/security.js +261 -0
  79. package/dist/src/commands/security.js.map +1 -0
  80. package/dist/src/commands/start.js +2 -2
  81. package/dist/src/commands/start.js.map +1 -1
  82. package/dist/src/commands/status.d.ts.map +1 -1
  83. package/dist/src/commands/status.js +26 -2
  84. package/dist/src/commands/status.js.map +1 -1
  85. package/dist/src/commands/swarm.js +6 -6
  86. package/dist/src/commands/swarm.js.map +1 -1
  87. package/dist/src/index.d.ts +4 -2
  88. package/dist/src/index.d.ts.map +1 -1
  89. package/dist/src/index.js +63 -5
  90. package/dist/src/index.js.map +1 -1
  91. package/dist/src/init/claudemd-generator.d.ts.map +1 -1
  92. package/dist/src/init/claudemd-generator.js +218 -362
  93. package/dist/src/init/claudemd-generator.js.map +1 -1
  94. package/dist/src/init/executor.d.ts.map +1 -1
  95. package/dist/src/init/executor.js +5 -0
  96. package/dist/src/init/executor.js.map +1 -1
  97. package/dist/src/init/settings-generator.d.ts.map +1 -1
  98. package/dist/src/init/settings-generator.js +22 -12
  99. package/dist/src/init/settings-generator.js.map +1 -1
  100. package/dist/src/mcp-client.d.ts.map +1 -1
  101. package/dist/src/mcp-client.js +17 -1
  102. package/dist/src/mcp-client.js.map +1 -1
  103. package/dist/src/mcp-server.d.ts.map +1 -1
  104. package/dist/src/mcp-server.js +5 -0
  105. package/dist/src/mcp-server.js.map +1 -1
  106. package/dist/src/mcp-tools/agent-tools.d.ts +1 -1
  107. package/dist/src/mcp-tools/agent-tools.d.ts.map +1 -1
  108. package/dist/src/mcp-tools/agent-tools.js +350 -14
  109. package/dist/src/mcp-tools/agent-tools.js.map +1 -1
  110. package/dist/src/mcp-tools/analyze-tools.d.ts +38 -0
  111. package/dist/src/mcp-tools/analyze-tools.d.ts.map +1 -0
  112. package/dist/src/mcp-tools/analyze-tools.js +317 -0
  113. package/dist/src/mcp-tools/analyze-tools.js.map +1 -0
  114. package/dist/src/mcp-tools/config-tools.d.ts +1 -1
  115. package/dist/src/mcp-tools/config-tools.d.ts.map +1 -1
  116. package/dist/src/mcp-tools/config-tools.js +262 -15
  117. package/dist/src/mcp-tools/config-tools.js.map +1 -1
  118. package/dist/src/mcp-tools/hive-mind-tools.d.ts +8 -0
  119. package/dist/src/mcp-tools/hive-mind-tools.d.ts.map +1 -0
  120. package/dist/src/mcp-tools/hive-mind-tools.js +447 -0
  121. package/dist/src/mcp-tools/hive-mind-tools.js.map +1 -0
  122. package/dist/src/mcp-tools/hooks-tools.d.ts.map +1 -1
  123. package/dist/src/mcp-tools/hooks-tools.js +80 -15
  124. package/dist/src/mcp-tools/hooks-tools.js.map +1 -1
  125. package/dist/src/mcp-tools/index.d.ts +6 -0
  126. package/dist/src/mcp-tools/index.d.ts.map +1 -1
  127. package/dist/src/mcp-tools/index.js +6 -0
  128. package/dist/src/mcp-tools/index.js.map +1 -1
  129. package/dist/src/mcp-tools/memory-tools.d.ts +1 -1
  130. package/dist/src/mcp-tools/memory-tools.d.ts.map +1 -1
  131. package/dist/src/mcp-tools/memory-tools.js +157 -9
  132. package/dist/src/mcp-tools/memory-tools.js.map +1 -1
  133. package/dist/src/mcp-tools/session-tools.d.ts +8 -0
  134. package/dist/src/mcp-tools/session-tools.d.ts.map +1 -0
  135. package/dist/src/mcp-tools/session-tools.js +315 -0
  136. package/dist/src/mcp-tools/session-tools.js.map +1 -0
  137. package/dist/src/mcp-tools/swarm-tools.d.ts.map +1 -1
  138. package/dist/src/mcp-tools/swarm-tools.js +37 -2
  139. package/dist/src/mcp-tools/swarm-tools.js.map +1 -1
  140. package/dist/src/mcp-tools/task-tools.d.ts +8 -0
  141. package/dist/src/mcp-tools/task-tools.d.ts.map +1 -0
  142. package/dist/src/mcp-tools/task-tools.js +302 -0
  143. package/dist/src/mcp-tools/task-tools.js.map +1 -0
  144. package/dist/src/mcp-tools/workflow-tools.d.ts +8 -0
  145. package/dist/src/mcp-tools/workflow-tools.d.ts.map +1 -0
  146. package/dist/src/mcp-tools/workflow-tools.js +481 -0
  147. package/dist/src/mcp-tools/workflow-tools.js.map +1 -0
  148. package/dist/src/output.d.ts +16 -0
  149. package/dist/src/output.d.ts.map +1 -1
  150. package/dist/src/output.js +42 -0
  151. package/dist/src/output.js.map +1 -1
  152. package/dist/src/ruvector/ast-analyzer.d.ts +67 -0
  153. package/dist/src/ruvector/ast-analyzer.d.ts.map +1 -0
  154. package/dist/src/ruvector/ast-analyzer.js +277 -0
  155. package/dist/src/ruvector/ast-analyzer.js.map +1 -0
  156. package/dist/src/ruvector/coverage-router.d.ts +145 -0
  157. package/dist/src/ruvector/coverage-router.d.ts.map +1 -0
  158. package/dist/src/ruvector/coverage-router.js +451 -0
  159. package/dist/src/ruvector/coverage-router.js.map +1 -0
  160. package/dist/src/ruvector/coverage-tools.d.ts +33 -0
  161. package/dist/src/ruvector/coverage-tools.d.ts.map +1 -0
  162. package/dist/src/ruvector/coverage-tools.js +157 -0
  163. package/dist/src/ruvector/coverage-tools.js.map +1 -0
  164. package/dist/src/ruvector/diff-classifier.d.ts +154 -0
  165. package/dist/src/ruvector/diff-classifier.d.ts.map +1 -0
  166. package/dist/src/ruvector/diff-classifier.js +508 -0
  167. package/dist/src/ruvector/diff-classifier.js.map +1 -0
  168. package/dist/src/ruvector/graph-analyzer.d.ts +174 -0
  169. package/dist/src/ruvector/graph-analyzer.d.ts.map +1 -0
  170. package/dist/src/ruvector/graph-analyzer.js +878 -0
  171. package/dist/src/ruvector/graph-analyzer.js.map +1 -0
  172. package/dist/src/ruvector/index.d.ts +27 -0
  173. package/dist/src/ruvector/index.d.ts.map +1 -0
  174. package/dist/src/ruvector/index.js +47 -0
  175. package/dist/src/ruvector/index.js.map +1 -0
  176. package/dist/src/ruvector/q-learning-router.d.ts +211 -0
  177. package/dist/src/ruvector/q-learning-router.d.ts.map +1 -0
  178. package/dist/src/ruvector/q-learning-router.js +681 -0
  179. package/dist/src/ruvector/q-learning-router.js.map +1 -0
  180. package/dist/src/ruvector/vector-db.d.ts +69 -0
  181. package/dist/src/ruvector/vector-db.d.ts.map +1 -0
  182. package/dist/src/ruvector/vector-db.js +243 -0
  183. package/dist/src/ruvector/vector-db.js.map +1 -0
  184. package/dist/src/services/index.d.ts +7 -0
  185. package/dist/src/services/index.d.ts.map +1 -0
  186. package/dist/src/services/index.js +6 -0
  187. package/dist/src/services/index.js.map +1 -0
  188. package/dist/src/services/worker-daemon.d.ts +153 -0
  189. package/dist/src/services/worker-daemon.d.ts.map +1 -0
  190. package/dist/src/services/worker-daemon.js +567 -0
  191. package/dist/src/services/worker-daemon.js.map +1 -0
  192. package/dist/src/suggest.d.ts +53 -0
  193. package/dist/src/suggest.d.ts.map +1 -0
  194. package/dist/src/suggest.js +200 -0
  195. package/dist/src/suggest.js.map +1 -0
  196. package/dist/tsconfig.tsbuildinfo +1 -1
  197. package/package.json +28 -6
  198. package/.agentic-flow/intelligence.json +0 -16
  199. package/.claude-flow/metrics/agent-metrics.json +0 -1
  200. package/.claude-flow/metrics/performance.json +0 -87
  201. package/.claude-flow/metrics/task-metrics.json +0 -10
  202. package/__tests__/README.md +0 -140
  203. package/__tests__/TEST_SUMMARY.md +0 -144
  204. package/__tests__/cli.test.ts +0 -558
  205. package/__tests__/commands.test.ts +0 -726
  206. package/__tests__/config-adapter.test.ts +0 -362
  207. package/__tests__/config-loading.test.ts +0 -106
  208. package/__tests__/coverage/.tmp/coverage-0.json +0 -1
  209. package/__tests__/coverage/.tmp/coverage-1.json +0 -1
  210. package/__tests__/coverage/.tmp/coverage-2.json +0 -1
  211. package/__tests__/coverage/.tmp/coverage-3.json +0 -1
  212. package/__tests__/coverage/.tmp/coverage-4.json +0 -1
  213. package/__tests__/coverage/.tmp/coverage-5.json +0 -1
  214. package/__tests__/mcp-client.test.ts +0 -480
  215. package/__tests__/p1-commands.test.ts +0 -1064
  216. package/docs/CONFIG_LOADING.md +0 -236
  217. package/docs/IMPLEMENTATION_COMPLETE.md +0 -421
  218. package/docs/MCP_CLIENT_GUIDE.md +0 -620
  219. package/docs/REFACTORING_SUMMARY.md +0 -247
  220. package/src/commands/agent.ts +0 -941
  221. package/src/commands/config.ts +0 -452
  222. package/src/commands/hive-mind.ts +0 -762
  223. package/src/commands/hooks.ts +0 -2603
  224. package/src/commands/index.ts +0 -115
  225. package/src/commands/init.ts +0 -597
  226. package/src/commands/mcp.ts +0 -753
  227. package/src/commands/memory.ts +0 -1063
  228. package/src/commands/migrate.ts +0 -447
  229. package/src/commands/process.ts +0 -617
  230. package/src/commands/session.ts +0 -891
  231. package/src/commands/start.ts +0 -457
  232. package/src/commands/status.ts +0 -705
  233. package/src/commands/swarm.ts +0 -648
  234. package/src/commands/task.ts +0 -792
  235. package/src/commands/workflow.ts +0 -742
  236. package/src/config-adapter.ts +0 -210
  237. package/src/index.ts +0 -383
  238. package/src/infrastructure/in-memory-repositories.ts +0 -310
  239. package/src/init/claudemd-generator.ts +0 -631
  240. package/src/init/executor.ts +0 -756
  241. package/src/init/helpers-generator.ts +0 -628
  242. package/src/init/index.ts +0 -60
  243. package/src/init/mcp-generator.ts +0 -83
  244. package/src/init/settings-generator.ts +0 -274
  245. package/src/init/statusline-generator.ts +0 -211
  246. package/src/init/types.ts +0 -447
  247. package/src/mcp-client.ts +0 -227
  248. package/src/mcp-server.ts +0 -571
  249. package/src/mcp-tools/agent-tools.ts +0 -92
  250. package/src/mcp-tools/config-tools.ts +0 -88
  251. package/src/mcp-tools/hooks-tools.ts +0 -1819
  252. package/src/mcp-tools/index.ts +0 -12
  253. package/src/mcp-tools/memory-tools.ts +0 -89
  254. package/src/mcp-tools/swarm-tools.ts +0 -69
  255. package/src/mcp-tools/types.ts +0 -33
  256. package/src/output.ts +0 -593
  257. package/src/parser.ts +0 -417
  258. package/src/prompt.ts +0 -619
  259. package/src/types.ts +0 -287
  260. package/tmp.json +0 -0
  261. package/tsconfig.json +0 -16
  262. package/tsconfig.tsbuildinfo +0 -1
  263. package/vitest.config.ts +0 -13
@@ -0,0 +1,878 @@
1
+ /**
2
+ * Graph Analyzer Module
3
+ *
4
+ * Provides code dependency graph analysis using ruvector's graph algorithms:
5
+ * - MinCut for code boundary detection (refactoring suggestions)
6
+ * - Louvain for module/community detection
7
+ * - Circular dependency detection
8
+ * - DOT format export for visualization
9
+ *
10
+ * Falls back to built-in implementations when @ruvector/wasm is not available.
11
+ *
12
+ * @module @claude-flow/cli/ruvector/graph-analyzer
13
+ */
14
+ import { readFile, readdir, stat } from 'fs/promises';
15
+ import { join, relative, extname, dirname, basename } from 'path';
16
+ let ruVectorGraph = null;
17
+ let ruVectorLoadAttempted = false;
18
+ /**
19
+ * Attempt to load ruvector graph algorithms
20
+ */
21
+ async function loadRuVector() {
22
+ if (ruVectorLoadAttempted)
23
+ return ruVectorGraph;
24
+ ruVectorLoadAttempted = true;
25
+ // Use dynamic module names to bypass TypeScript static analysis
26
+ // These modules are optional and may not be installed
27
+ const ruvectorModule = 'ruvector';
28
+ const wasmModule = '@ruvector/wasm';
29
+ try {
30
+ // Try to load ruvector's graph module
31
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
+ const ruvector = await import(/* webpackIgnore: true */ ruvectorModule).catch(() => null);
33
+ if (ruvector && typeof ruvector.hooks_graph_mincut === 'function' && typeof ruvector.hooks_graph_cluster === 'function') {
34
+ ruVectorGraph = {
35
+ mincut: (nodes, edges) => ruvector.hooks_graph_mincut(nodes, edges),
36
+ louvain: (nodes, edges) => ruvector.hooks_graph_cluster(nodes, edges),
37
+ };
38
+ return ruVectorGraph;
39
+ }
40
+ }
41
+ catch {
42
+ // Try alternative import paths
43
+ try {
44
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
+ const wasm = await import(/* webpackIgnore: true */ wasmModule).catch(() => null);
46
+ if (wasm && wasm.GraphAnalyzer) {
47
+ const analyzer = new wasm.GraphAnalyzer();
48
+ ruVectorGraph = {
49
+ mincut: (nodes, edges) => analyzer.mincut(nodes, edges),
50
+ louvain: (nodes, edges) => analyzer.louvain(nodes, edges),
51
+ };
52
+ return ruVectorGraph;
53
+ }
54
+ }
55
+ catch {
56
+ // Fallback will be used
57
+ }
58
+ }
59
+ return null;
60
+ }
61
+ // ============================================================================
62
+ // Import/Require Parser
63
+ // ============================================================================
64
+ /**
65
+ * Extract imports from TypeScript/JavaScript file
66
+ */
67
+ function extractImports(content, _filePath) {
68
+ const imports = [];
69
+ // ES6 import statements
70
+ const esImportRegex = /import\s+(?:(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s*,?\s*)*\s*from\s*['"]([^'"]+)['"]/g;
71
+ let match;
72
+ while ((match = esImportRegex.exec(content)) !== null) {
73
+ imports.push({ path: match[1], type: 'import' });
74
+ }
75
+ // Side-effect imports: import 'module'
76
+ const sideEffectRegex = /import\s+['"]([^'"]+)['"]/g;
77
+ while ((match = sideEffectRegex.exec(content)) !== null) {
78
+ imports.push({ path: match[1], type: 'import' });
79
+ }
80
+ // CommonJS require
81
+ const requireRegex = /require\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
82
+ while ((match = requireRegex.exec(content)) !== null) {
83
+ imports.push({ path: match[1], type: 'require' });
84
+ }
85
+ // Dynamic imports
86
+ const dynamicImportRegex = /import\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
87
+ while ((match = dynamicImportRegex.exec(content)) !== null) {
88
+ imports.push({ path: match[1], type: 'dynamic' });
89
+ }
90
+ // Re-exports: export * from 'module'
91
+ const reExportRegex = /export\s+(?:\*|\{[^}]*\})\s+from\s*['"]([^'"]+)['"]/g;
92
+ while ((match = reExportRegex.exec(content)) !== null) {
93
+ imports.push({ path: match[1], type: 're-export' });
94
+ }
95
+ return imports;
96
+ }
97
+ /**
98
+ * Extract exports from TypeScript/JavaScript file
99
+ */
100
+ function extractExports(content) {
101
+ const exports = [];
102
+ // Named exports
103
+ const namedExportRegex = /export\s+(?:const|let|var|function|class|interface|type|enum)\s+(\w+)/g;
104
+ let match;
105
+ while ((match = namedExportRegex.exec(content)) !== null) {
106
+ exports.push(match[1]);
107
+ }
108
+ // Export list: export { a, b, c }
109
+ const exportListRegex = /export\s+\{([^}]+)\}/g;
110
+ while ((match = exportListRegex.exec(content)) !== null) {
111
+ const names = match[1].split(',').map(n => n.trim().split(/\s+as\s+/)[0].trim());
112
+ exports.push(...names.filter(n => n));
113
+ }
114
+ // Default export
115
+ if (/export\s+default/.test(content)) {
116
+ exports.push('default');
117
+ }
118
+ return exports;
119
+ }
120
+ /**
121
+ * Resolve import path to absolute file path
122
+ */
123
+ function resolveImportPath(importPath, fromFile, rootDir) {
124
+ // Skip external packages
125
+ if (!importPath.startsWith('.') && !importPath.startsWith('/')) {
126
+ return null;
127
+ }
128
+ const fromDir = dirname(fromFile);
129
+ let resolved;
130
+ if (importPath.startsWith('/')) {
131
+ resolved = join(rootDir, importPath);
132
+ }
133
+ else {
134
+ resolved = join(fromDir, importPath);
135
+ }
136
+ // Handle extension-less imports
137
+ const ext = extname(resolved);
138
+ if (!ext) {
139
+ // Try common extensions
140
+ const extensions = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'];
141
+ for (const tryExt of extensions) {
142
+ const tryPath = resolved + tryExt;
143
+ return tryPath; // Return normalized, existence check done later
144
+ }
145
+ // Could be index file
146
+ return join(resolved, 'index');
147
+ }
148
+ return resolved;
149
+ }
150
+ // ============================================================================
151
+ // Graph Builder
152
+ // ============================================================================
153
+ /**
154
+ * Build dependency graph from source directory
155
+ */
156
+ export async function buildDependencyGraph(rootDir, options = {}) {
157
+ const startTime = Date.now();
158
+ const nodes = new Map();
159
+ const edges = [];
160
+ const include = options.include || ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'];
161
+ const exclude = options.exclude || ['node_modules', 'dist', 'build', '.git', '__tests__', '*.test.*', '*.spec.*'];
162
+ const maxDepth = options.maxDepth ?? 10;
163
+ /**
164
+ * Check if path should be excluded
165
+ */
166
+ function shouldExclude(path) {
167
+ const name = basename(path);
168
+ return exclude.some(pattern => {
169
+ if (pattern.includes('*')) {
170
+ const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
171
+ return regex.test(name);
172
+ }
173
+ return name === pattern || path.includes(`/${pattern}/`);
174
+ });
175
+ }
176
+ /**
177
+ * Recursively scan directory for source files
178
+ */
179
+ async function scanDir(dir, depth) {
180
+ if (depth > maxDepth)
181
+ return;
182
+ try {
183
+ const entries = await readdir(dir, { withFileTypes: true });
184
+ for (const entry of entries) {
185
+ const fullPath = join(dir, entry.name);
186
+ const relPath = relative(rootDir, fullPath);
187
+ if (shouldExclude(fullPath))
188
+ continue;
189
+ if (entry.isDirectory()) {
190
+ await scanDir(fullPath, depth + 1);
191
+ }
192
+ else if (entry.isFile()) {
193
+ const ext = extname(entry.name);
194
+ if (include.includes(ext)) {
195
+ await processFile(fullPath, relPath);
196
+ }
197
+ }
198
+ }
199
+ }
200
+ catch {
201
+ // Directory not readable, skip
202
+ }
203
+ }
204
+ /**
205
+ * Process a single source file
206
+ */
207
+ async function processFile(fullPath, relPath) {
208
+ try {
209
+ const content = await readFile(fullPath, 'utf-8');
210
+ const fileStats = await stat(fullPath);
211
+ const imports = extractImports(content, fullPath);
212
+ const exportsList = extractExports(content);
213
+ // Create node
214
+ const node = {
215
+ id: relPath,
216
+ path: relPath,
217
+ name: basename(relPath, extname(relPath)),
218
+ type: 'file',
219
+ imports: imports.map(i => i.path),
220
+ exports: exportsList,
221
+ size: fileStats.size,
222
+ complexity: estimateComplexity(content),
223
+ };
224
+ nodes.set(relPath, node);
225
+ // Create edges for imports
226
+ for (const imp of imports) {
227
+ const resolved = resolveImportPath(imp.path, fullPath, rootDir);
228
+ if (resolved) {
229
+ const targetRel = relative(rootDir, resolved);
230
+ edges.push({
231
+ source: relPath,
232
+ target: targetRel,
233
+ type: imp.type,
234
+ weight: imp.type === 're-export' ? 2 : 1,
235
+ });
236
+ }
237
+ }
238
+ }
239
+ catch {
240
+ // File not readable, skip
241
+ }
242
+ }
243
+ // Build the graph
244
+ await scanDir(rootDir, 0);
245
+ // Normalize edges - ensure targets exist (with extension variations)
246
+ const normalizedEdges = [];
247
+ for (const edge of edges) {
248
+ // Try to find matching node
249
+ let targetKey = edge.target;
250
+ if (!nodes.has(targetKey)) {
251
+ // Try with different extensions
252
+ const extensions = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'];
253
+ const baseTarget = targetKey.replace(/\.[^.]+$/, '');
254
+ for (const ext of extensions) {
255
+ if (nodes.has(baseTarget + ext)) {
256
+ targetKey = baseTarget + ext;
257
+ break;
258
+ }
259
+ // Try index files
260
+ if (nodes.has(join(baseTarget, 'index' + ext))) {
261
+ targetKey = join(baseTarget, 'index' + ext);
262
+ break;
263
+ }
264
+ }
265
+ }
266
+ if (nodes.has(targetKey)) {
267
+ normalizedEdges.push({ ...edge, target: targetKey });
268
+ }
269
+ }
270
+ return {
271
+ nodes,
272
+ edges: normalizedEdges,
273
+ metadata: {
274
+ rootDir,
275
+ totalFiles: nodes.size,
276
+ totalEdges: normalizedEdges.length,
277
+ buildTime: Date.now() - startTime,
278
+ },
279
+ };
280
+ }
281
+ /**
282
+ * Estimate cyclomatic complexity from code
283
+ */
284
+ function estimateComplexity(content) {
285
+ let complexity = 1;
286
+ // Count branching statements
287
+ const patterns = [
288
+ /\bif\s*\(/g,
289
+ /\belse\s+if\s*\(/g,
290
+ /\bfor\s*\(/g,
291
+ /\bwhile\s*\(/g,
292
+ /\bcase\s+/g,
293
+ /\bcatch\s*\(/g,
294
+ /\?\s*[^:]+:/g, // Ternary operator
295
+ /&&/g,
296
+ /\|\|/g,
297
+ ];
298
+ for (const pattern of patterns) {
299
+ const matches = content.match(pattern);
300
+ if (matches)
301
+ complexity += matches.length;
302
+ }
303
+ return complexity;
304
+ }
305
+ // ============================================================================
306
+ // MinCut Algorithm (Fallback Implementation)
307
+ // ============================================================================
308
+ /**
309
+ * Stoer-Wagner MinCut algorithm (fallback when ruvector not available)
310
+ * Finds minimum cut with deterministic result
311
+ */
312
+ function fallbackMinCut(nodes, edges) {
313
+ if (nodes.length < 2) {
314
+ return {
315
+ cutValue: 0,
316
+ partition1: nodes,
317
+ partition2: [],
318
+ cutEdges: [],
319
+ };
320
+ }
321
+ // Build adjacency map with weights
322
+ const adj = new Map();
323
+ for (const node of nodes) {
324
+ adj.set(node, new Map());
325
+ }
326
+ for (const [u, v, w] of edges) {
327
+ if (adj.has(u) && adj.has(v)) {
328
+ adj.get(u).set(v, (adj.get(u).get(v) || 0) + w);
329
+ adj.get(v).set(u, (adj.get(v).get(u) || 0) + w);
330
+ }
331
+ }
332
+ let bestCut = Infinity;
333
+ let bestPartition1 = [];
334
+ let bestPartition2 = [];
335
+ let bestCutEdges = [];
336
+ // Run multiple iterations for better results
337
+ const iterations = Math.min(nodes.length * 2, 20);
338
+ for (let iter = 0; iter < iterations; iter++) {
339
+ // Start from different nodes
340
+ const startNode = nodes[iter % nodes.length];
341
+ const inSet = new Set([startNode]);
342
+ const remaining = new Set(nodes.filter(n => n !== startNode));
343
+ while (remaining.size > 1) {
344
+ // Find node with maximum connectivity to current set
345
+ let maxNode = '';
346
+ let maxConn = -1;
347
+ for (const node of Array.from(remaining)) {
348
+ let conn = 0;
349
+ for (const inNode of Array.from(inSet)) {
350
+ conn += adj.get(node)?.get(inNode) || 0;
351
+ }
352
+ if (conn > maxConn) {
353
+ maxConn = conn;
354
+ maxNode = node;
355
+ }
356
+ }
357
+ if (!maxNode)
358
+ break;
359
+ remaining.delete(maxNode);
360
+ inSet.add(maxNode);
361
+ }
362
+ if (remaining.size === 1) {
363
+ const lastNode = Array.from(remaining)[0];
364
+ let cutValue = 0;
365
+ const cutEdges = [];
366
+ for (const inNode of Array.from(inSet)) {
367
+ const weight = adj.get(lastNode)?.get(inNode) || 0;
368
+ if (weight > 0) {
369
+ cutValue += weight;
370
+ cutEdges.push([lastNode, inNode]);
371
+ }
372
+ }
373
+ if (cutValue < bestCut) {
374
+ bestCut = cutValue;
375
+ bestPartition1 = Array.from(inSet);
376
+ bestPartition2 = [lastNode];
377
+ bestCutEdges = cutEdges;
378
+ }
379
+ }
380
+ }
381
+ // If we didn't find a good cut, split roughly in half
382
+ if (bestCut === Infinity) {
383
+ const mid = Math.floor(nodes.length / 2);
384
+ bestPartition1 = nodes.slice(0, mid);
385
+ bestPartition2 = nodes.slice(mid);
386
+ bestCut = 0;
387
+ bestCutEdges = [];
388
+ for (const [u, v, w] of edges) {
389
+ const uIn1 = bestPartition1.includes(u);
390
+ const vIn1 = bestPartition1.includes(v);
391
+ if (uIn1 !== vIn1) {
392
+ bestCut += w;
393
+ bestCutEdges.push([u, v]);
394
+ }
395
+ }
396
+ }
397
+ return {
398
+ cutValue: bestCut,
399
+ partition1: bestPartition1,
400
+ partition2: bestPartition2,
401
+ cutEdges: bestCutEdges,
402
+ };
403
+ }
404
+ // ============================================================================
405
+ // Louvain Algorithm (Fallback Implementation)
406
+ // ============================================================================
407
+ /**
408
+ * Louvain community detection algorithm (fallback when ruvector not available)
409
+ * Greedy modularity optimization
410
+ */
411
+ function fallbackLouvain(nodes, edges) {
412
+ if (nodes.length === 0) {
413
+ return { communities: [], modularity: 0 };
414
+ }
415
+ // Build adjacency map
416
+ const adj = new Map();
417
+ for (const node of nodes) {
418
+ adj.set(node, new Map());
419
+ }
420
+ let totalWeight = 0;
421
+ for (const [u, v, w] of edges) {
422
+ if (adj.has(u) && adj.has(v)) {
423
+ adj.get(u).set(v, (adj.get(u).get(v) || 0) + w);
424
+ adj.get(v).set(u, (adj.get(v).get(u) || 0) + w);
425
+ totalWeight += w * 2;
426
+ }
427
+ }
428
+ if (totalWeight === 0) {
429
+ // No edges, each node is its own community
430
+ return {
431
+ communities: nodes.map((n, i) => ({ id: i, members: [n] })),
432
+ modularity: 0,
433
+ };
434
+ }
435
+ // Initialize: each node in its own community
436
+ const community = new Map();
437
+ let nextCommunityId = 0;
438
+ for (const node of nodes) {
439
+ community.set(node, nextCommunityId++);
440
+ }
441
+ // Calculate node degree
442
+ const degree = new Map();
443
+ for (const node of nodes) {
444
+ let d = 0;
445
+ for (const [, w] of Array.from(adj.get(node).entries())) {
446
+ d += w;
447
+ }
448
+ degree.set(node, d);
449
+ }
450
+ // Louvain phase 1: local moving
451
+ let improved = true;
452
+ const maxIterations = 10;
453
+ let iteration = 0;
454
+ while (improved && iteration < maxIterations) {
455
+ improved = false;
456
+ iteration++;
457
+ for (const node of nodes) {
458
+ const currentCommunity = community.get(node);
459
+ const nodeAdj = adj.get(node);
460
+ const nodeDegree = degree.get(node);
461
+ // Calculate modularity gain for moving to each neighbor's community
462
+ const communityWeights = new Map();
463
+ for (const [neighbor, weight] of Array.from(nodeAdj.entries())) {
464
+ const neighborCommunity = community.get(neighbor);
465
+ communityWeights.set(neighborCommunity, (communityWeights.get(neighborCommunity) || 0) + weight);
466
+ }
467
+ // Calculate community totals
468
+ const communityTotal = new Map();
469
+ for (const [n, c] of Array.from(community.entries())) {
470
+ communityTotal.set(c, (communityTotal.get(c) || 0) + (degree.get(n) || 0));
471
+ }
472
+ let bestCommunity = currentCommunity;
473
+ let bestGain = 0;
474
+ for (const [targetCommunity, edgeWeight] of Array.from(communityWeights.entries())) {
475
+ if (targetCommunity === currentCommunity)
476
+ continue;
477
+ // Calculate modularity gain
478
+ const currentTotal = communityTotal.get(currentCommunity) || 0;
479
+ const targetTotal = communityTotal.get(targetCommunity) || 0;
480
+ const currentEdges = communityWeights.get(currentCommunity) || 0;
481
+ const gain = (edgeWeight - currentEdges) / totalWeight -
482
+ (nodeDegree * (targetTotal - currentTotal + nodeDegree)) / (totalWeight * totalWeight);
483
+ if (gain > bestGain) {
484
+ bestGain = gain;
485
+ bestCommunity = targetCommunity;
486
+ }
487
+ }
488
+ if (bestCommunity !== currentCommunity) {
489
+ community.set(node, bestCommunity);
490
+ improved = true;
491
+ }
492
+ }
493
+ }
494
+ // Collect communities
495
+ const communityMembers = new Map();
496
+ for (const [node, comm] of Array.from(community.entries())) {
497
+ if (!communityMembers.has(comm)) {
498
+ communityMembers.set(comm, []);
499
+ }
500
+ communityMembers.get(comm).push(node);
501
+ }
502
+ // Renumber communities
503
+ const communities = [];
504
+ let id = 0;
505
+ for (const members of Array.from(communityMembers.values())) {
506
+ communities.push({ id: id++, members });
507
+ }
508
+ // Calculate modularity
509
+ let modularity = 0;
510
+ for (const [u, v, w] of edges) {
511
+ const cu = community.get(u);
512
+ const cv = community.get(v);
513
+ if (cu === cv) {
514
+ const du = degree.get(u);
515
+ const dv = degree.get(v);
516
+ modularity += w - (du * dv) / totalWeight;
517
+ }
518
+ }
519
+ modularity /= totalWeight;
520
+ return { communities, modularity };
521
+ }
522
+ // ============================================================================
523
+ // Circular Dependency Detection
524
+ // ============================================================================
525
+ /**
526
+ * Detect circular dependencies using DFS
527
+ */
528
+ export function detectCircularDependencies(graph) {
529
+ const cycles = [];
530
+ const visited = new Set();
531
+ const recursionStack = new Set();
532
+ const path = [];
533
+ // Build adjacency list
534
+ const adjList = new Map();
535
+ for (const node of Array.from(graph.nodes.keys())) {
536
+ adjList.set(node, []);
537
+ }
538
+ for (const edge of graph.edges) {
539
+ const list = adjList.get(edge.source);
540
+ if (list) {
541
+ list.push(edge.target);
542
+ }
543
+ }
544
+ function dfs(node) {
545
+ visited.add(node);
546
+ recursionStack.add(node);
547
+ path.push(node);
548
+ const neighbors = adjList.get(node) || [];
549
+ for (const neighbor of neighbors) {
550
+ if (!visited.has(neighbor)) {
551
+ dfs(neighbor);
552
+ }
553
+ else if (recursionStack.has(neighbor)) {
554
+ // Found cycle
555
+ const cycleStart = path.indexOf(neighbor);
556
+ const cycle = path.slice(cycleStart);
557
+ cycle.push(neighbor); // Complete the cycle
558
+ const severity = getCycleSeverity(cycle, graph);
559
+ cycles.push({
560
+ cycle,
561
+ severity,
562
+ suggestion: getCycleSuggestion(cycle, graph),
563
+ });
564
+ }
565
+ }
566
+ recursionStack.delete(node);
567
+ path.pop();
568
+ }
569
+ for (const node of Array.from(graph.nodes.keys())) {
570
+ if (!visited.has(node)) {
571
+ dfs(node);
572
+ }
573
+ }
574
+ return cycles;
575
+ }
576
+ function getCycleSeverity(cycle, _graph) {
577
+ // High severity if cycle involves many files or core modules
578
+ if (cycle.length > 5)
579
+ return 'high';
580
+ if (cycle.some(n => n.includes('index') || n.includes('core')))
581
+ return 'high';
582
+ if (cycle.length > 3)
583
+ return 'medium';
584
+ return 'low';
585
+ }
586
+ function getCycleSuggestion(cycle, graph) {
587
+ if (cycle.length === 2) {
588
+ return `Consider extracting shared code into a separate module to break the cycle between ${cycle[0]} and ${cycle[1]}`;
589
+ }
590
+ // Find the weakest link (least important edge)
591
+ let weakestEdge = '';
592
+ let minImports = Infinity;
593
+ for (let i = 0; i < cycle.length - 1; i++) {
594
+ const from = cycle[i];
595
+ const to = cycle[i + 1];
596
+ const fromNode = graph.nodes.get(from);
597
+ if (fromNode && fromNode.imports.length < minImports) {
598
+ minImports = fromNode.imports.length;
599
+ weakestEdge = `${from} -> ${to}`;
600
+ }
601
+ }
602
+ return `Break the cycle by refactoring the dependency: ${weakestEdge}. Consider dependency injection or extracting interfaces.`;
603
+ }
604
+ // ============================================================================
605
+ // Main Analysis Functions
606
+ // ============================================================================
607
+ /**
608
+ * Analyze graph boundaries using MinCut algorithm
609
+ */
610
+ export async function analyzeMinCutBoundaries(graph, numPartitions = 2) {
611
+ const nodes = Array.from(graph.nodes.keys());
612
+ const edges = graph.edges.map(e => [e.source, e.target, e.weight]);
613
+ const boundaries = [];
614
+ // Try to use ruvector, fallback to built-in
615
+ const ruVector = await loadRuVector();
616
+ // Get initial partition
617
+ let result;
618
+ if (ruVector) {
619
+ result = ruVector.mincut(nodes, edges);
620
+ }
621
+ else {
622
+ result = fallbackMinCut(nodes, edges);
623
+ }
624
+ boundaries.push({
625
+ cutValue: result.cutValue,
626
+ partition1: result.partition1,
627
+ partition2: result.partition2,
628
+ cutEdges: result.cutEdges.map(([s, t]) => {
629
+ const edge = graph.edges.find(e => e.source === s && e.target === t);
630
+ return edge || { source: s, target: t, type: 'import', weight: 1 };
631
+ }),
632
+ suggestion: generateBoundarySuggestion(result.partition1, result.partition2, graph),
633
+ });
634
+ // Recursively partition if needed
635
+ if (numPartitions > 2 && result.partition1.length > 2) {
636
+ const subEdges = edges.filter(([u, v]) => result.partition1.includes(u) && result.partition1.includes(v));
637
+ const subResult = ruVector
638
+ ? ruVector.mincut(result.partition1, subEdges)
639
+ : fallbackMinCut(result.partition1, subEdges);
640
+ if (subResult.cutValue > 0) {
641
+ boundaries.push({
642
+ cutValue: subResult.cutValue,
643
+ partition1: subResult.partition1,
644
+ partition2: subResult.partition2,
645
+ cutEdges: subResult.cutEdges.map(([s, t]) => {
646
+ const edge = graph.edges.find(e => e.source === s && e.target === t);
647
+ return edge || { source: s, target: t, type: 'import', weight: 1 };
648
+ }),
649
+ suggestion: generateBoundarySuggestion(subResult.partition1, subResult.partition2, graph),
650
+ });
651
+ }
652
+ }
653
+ return boundaries;
654
+ }
655
+ function generateBoundarySuggestion(partition1, partition2, _graph) {
656
+ // Analyze the partitions to suggest organization
657
+ const p1Dirs = partition1.map(p => dirname(p)).filter(d => d !== '.');
658
+ const p2Dirs = partition2.map(p => dirname(p)).filter(d => d !== '.');
659
+ const p1DirsSet = new Set(p1Dirs);
660
+ const p2DirsSet = new Set(p2Dirs);
661
+ if (p1DirsSet.size === 1 && p2DirsSet.size === 1) {
662
+ const dir1 = p1Dirs[0];
663
+ const dir2 = p2Dirs[0];
664
+ return `Natural boundary detected between ${dir1}/ and ${dir2}/. These could be separate packages.`;
665
+ }
666
+ if (partition1.length > partition2.length * 3) {
667
+ return `Consider extracting ${partition2.length} files into a separate module. They have minimal coupling to the rest.`;
668
+ }
669
+ return `Found ${partition1.length} and ${partition2.length} file groups with minimal coupling. Consider organizing into separate modules.`;
670
+ }
671
+ /**
672
+ * Analyze module communities using Louvain algorithm
673
+ */
674
+ export async function analyzeModuleCommunities(graph) {
675
+ const nodes = Array.from(graph.nodes.keys());
676
+ const edges = graph.edges.map(e => [e.source, e.target, e.weight]);
677
+ // Try to use ruvector, fallback to built-in
678
+ const ruVector = await loadRuVector();
679
+ const result = ruVector ? ruVector.louvain(nodes, edges) : fallbackLouvain(nodes, edges);
680
+ return result.communities.map(comm => {
681
+ // Find the most connected node as central
682
+ let maxConnections = 0;
683
+ let centralNode = comm.members[0];
684
+ for (const member of comm.members) {
685
+ const connections = graph.edges.filter(e => (e.source === member && comm.members.includes(e.target)) ||
686
+ (e.target === member && comm.members.includes(e.source))).length;
687
+ if (connections > maxConnections) {
688
+ maxConnections = connections;
689
+ centralNode = member;
690
+ }
691
+ }
692
+ // Calculate cohesion (internal edges / total possible edges)
693
+ const internalEdges = graph.edges.filter(e => comm.members.includes(e.source) && comm.members.includes(e.target)).length;
694
+ const possibleEdges = (comm.members.length * (comm.members.length - 1)) / 2;
695
+ const cohesion = possibleEdges > 0 ? internalEdges / possibleEdges : 1;
696
+ // Suggest name based on common directory
697
+ const dirs = comm.members.map(m => dirname(m));
698
+ const commonDir = findCommonPrefix(dirs);
699
+ const suggestedName = commonDir || basename(centralNode, extname(centralNode));
700
+ return {
701
+ id: comm.id,
702
+ members: comm.members,
703
+ cohesion,
704
+ centralNode,
705
+ suggestedName,
706
+ };
707
+ });
708
+ }
709
+ function findCommonPrefix(strings) {
710
+ if (strings.length === 0)
711
+ return '';
712
+ if (strings.length === 1)
713
+ return strings[0];
714
+ const sorted = [...strings].sort();
715
+ const first = sorted[0];
716
+ const last = sorted[sorted.length - 1];
717
+ let i = 0;
718
+ while (i < first.length && first[i] === last[i]) {
719
+ i++;
720
+ }
721
+ const prefix = first.slice(0, i);
722
+ // Return the last complete directory segment
723
+ const lastSlash = prefix.lastIndexOf('/');
724
+ return lastSlash > 0 ? prefix.slice(0, lastSlash) : '';
725
+ }
726
+ /**
727
+ * Full graph analysis
728
+ */
729
+ export async function analyzeGraph(rootDir, options = {}) {
730
+ const graph = await buildDependencyGraph(rootDir);
731
+ // Calculate statistics
732
+ const nodeCount = graph.nodes.size;
733
+ const edgeCount = graph.edges.length;
734
+ const degrees = new Map();
735
+ for (const node of Array.from(graph.nodes.keys())) {
736
+ degrees.set(node, 0);
737
+ }
738
+ for (const edge of graph.edges) {
739
+ degrees.set(edge.source, (degrees.get(edge.source) || 0) + 1);
740
+ degrees.set(edge.target, (degrees.get(edge.target) || 0) + 1);
741
+ }
742
+ const degreeValues = Array.from(degrees.values());
743
+ const avgDegree = degreeValues.length > 0 ? degreeValues.reduce((a, b) => a + b, 0) / degreeValues.length : 0;
744
+ const maxDegree = degreeValues.length > 0 ? Math.max(...degreeValues) : 0;
745
+ const density = nodeCount > 1 ? (2 * edgeCount) / (nodeCount * (nodeCount - 1)) : 0;
746
+ // Count connected components
747
+ const visited = new Set();
748
+ let componentCount = 0;
749
+ function dfs(node) {
750
+ visited.add(node);
751
+ for (const edge of graph.edges) {
752
+ if (edge.source === node && !visited.has(edge.target)) {
753
+ dfs(edge.target);
754
+ }
755
+ if (edge.target === node && !visited.has(edge.source)) {
756
+ dfs(edge.source);
757
+ }
758
+ }
759
+ }
760
+ for (const node of Array.from(graph.nodes.keys())) {
761
+ if (!visited.has(node)) {
762
+ componentCount++;
763
+ dfs(node);
764
+ }
765
+ }
766
+ // Detect circular dependencies
767
+ const circularDependencies = detectCircularDependencies(graph);
768
+ // Analyze boundaries and communities if requested
769
+ let boundaries;
770
+ let communities;
771
+ if (options.includeBoundaries !== false) {
772
+ boundaries = await analyzeMinCutBoundaries(graph, options.numPartitions);
773
+ }
774
+ if (options.includeModules !== false) {
775
+ communities = await analyzeModuleCommunities(graph);
776
+ }
777
+ return {
778
+ graph,
779
+ boundaries,
780
+ communities,
781
+ circularDependencies,
782
+ statistics: {
783
+ nodeCount,
784
+ edgeCount,
785
+ avgDegree,
786
+ maxDegree,
787
+ density,
788
+ componentCount,
789
+ },
790
+ };
791
+ }
792
+ // ============================================================================
793
+ // DOT Format Export
794
+ // ============================================================================
795
+ /**
796
+ * Export graph to DOT format for visualization
797
+ */
798
+ export function exportToDot(result, options = {}) {
799
+ const { graph, communities, circularDependencies } = result;
800
+ const lines = ['digraph DependencyGraph {'];
801
+ lines.push(' rankdir=LR;');
802
+ lines.push(' node [shape=box, style=rounded];');
803
+ lines.push('');
804
+ // Generate colors for communities
805
+ const communityColors = new Map();
806
+ if (options.colorByCommunity && communities) {
807
+ const colors = [
808
+ '#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231',
809
+ '#911eb4', '#42d4f4', '#f032e6', '#bfef45', '#fabed4',
810
+ ];
811
+ for (const comm of communities) {
812
+ const color = colors[comm.id % colors.length];
813
+ for (const member of comm.members) {
814
+ communityColors.set(member, color);
815
+ }
816
+ }
817
+ }
818
+ // Find nodes in cycles
819
+ const nodesInCycles = new Set();
820
+ if (options.highlightCycles && circularDependencies) {
821
+ for (const cycle of circularDependencies) {
822
+ for (const node of cycle.cycle) {
823
+ nodesInCycles.add(node);
824
+ }
825
+ }
826
+ }
827
+ // Output nodes
828
+ lines.push(' // Nodes');
829
+ for (const [id, node] of Array.from(graph.nodes.entries())) {
830
+ const attrs = [];
831
+ if (options.includeLabels !== false) {
832
+ attrs.push(`label="${node.name}"`);
833
+ }
834
+ if (communityColors.has(id)) {
835
+ attrs.push(`fillcolor="${communityColors.get(id)}"`, 'style="filled,rounded"');
836
+ }
837
+ if (nodesInCycles.has(id)) {
838
+ attrs.push('color=red', 'penwidth=2');
839
+ }
840
+ const attrStr = attrs.length > 0 ? ` [${attrs.join(', ')}]` : '';
841
+ lines.push(` "${id}"${attrStr};`);
842
+ }
843
+ lines.push('');
844
+ // Output edges
845
+ lines.push(' // Edges');
846
+ for (const edge of graph.edges) {
847
+ const attrs = [];
848
+ if (edge.type === 'dynamic') {
849
+ attrs.push('style=dashed');
850
+ }
851
+ else if (edge.type === 're-export') {
852
+ attrs.push('style=bold');
853
+ }
854
+ // Check if edge is part of a cycle
855
+ if (options.highlightCycles) {
856
+ const isCycleEdge = circularDependencies.some(cd => {
857
+ for (let i = 0; i < cd.cycle.length - 1; i++) {
858
+ if (cd.cycle[i] === edge.source && cd.cycle[i + 1] === edge.target) {
859
+ return true;
860
+ }
861
+ }
862
+ return false;
863
+ });
864
+ if (isCycleEdge) {
865
+ attrs.push('color=red', 'penwidth=2');
866
+ }
867
+ }
868
+ const attrStr = attrs.length > 0 ? ` [${attrs.join(', ')}]` : '';
869
+ lines.push(` "${edge.source}" -> "${edge.target}"${attrStr};`);
870
+ }
871
+ lines.push('}');
872
+ return lines.join('\n');
873
+ }
874
+ // ============================================================================
875
+ // Exports
876
+ // ============================================================================
877
+ export { loadRuVector, fallbackMinCut, fallbackLouvain, };
878
+ //# sourceMappingURL=graph-analyzer.js.map